Ignore:
Timestamp:
Aug 31, 2023, 11:31:15 PM (2 years ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
950c58e
Parents:
92355883 (diff), 686912c (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Resolve conflict

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Validate/LinkReferenceToTypes.cpp

    r92355883 r2a301ff  
    1010// Created On       : Thr Apr 21 11:41:00 2022
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Tue Sep 20 16:17:00 2022
    13 // Update Count     : 2
     12// Last Modified On : Fri Jul 14  9:19:00 2023
     13// Update Count     : 3
    1414//
    1515
     
    2727struct LinkTypesCore : public WithNoIdSymbolTable,
    2828                public ast::WithCodeLocation,
     29                public ast::WithDeclsToAdd<>,
    2930                public ast::WithGuards,
    3031                public ast::WithShortCircuiting,
     
    6364        template<typename AggrDecl>
    6465        AggrDecl const * renameGenericParams( AggrDecl const * decl );
     66
     67        // This cluster is used to add declarations (before) but outside of
     68        // any "namespaces" which would qualify the names.
     69        bool inNamespace = false;
     70        std::list<ast::ptr<ast::Decl>> declsToAddOutside;
     71        /// The "leaveNamespace" is handled by guard.
     72        void enterNamespace();
     73        /// Puts the decl on the back of declsToAddAfter once traversal is
     74        /// outside of any namespaces.
     75        void addDeclAfterOutside( ast::Decl const * );
    6576};
     77
     78void LinkTypesCore::enterNamespace() {
     79        if ( inNamespace ) return;
     80        inNamespace = true;
     81        GuardAction( [this](){
     82                inNamespace = false;
     83                declsToAddAfter.splice( declsToAddAfter.begin(), declsToAddOutside );
     84        } );
     85}
     86
     87void LinkTypesCore::addDeclAfterOutside( ast::Decl const * decl ) {
     88        if ( inNamespace ) {
     89                declsToAddOutside.emplace_back( decl );
     90        } else {
     91                declsToAddAfter.emplace_back( decl );
     92        }
     93}
    6694
    6795ast::TypeInstType const * LinkTypesCore::postvisit( ast::TypeInstType const * type ) {
     
    81109        ast::EnumDecl const * decl = symtab.lookupEnum( type->name );
    82110        // It's not a semantic error if the enum is not found, just an implicit forward declaration.
    83         if ( decl ) {
    84                 // Just linking in the node.
    85                 auto mut = ast::mutate( type );
    86                 mut->base = decl;
    87                 type = mut;
    88         }
    89         if ( !decl || !decl->body ) {
    90                 auto mut = ast::mutate( type );
     111        // The unset code location is used to detect imaginary declarations.
     112        // (They may never be used for enumerations.)
     113        if ( !decl || decl->location.isUnset() ) {
     114                assert( location );
     115                ast::EnumDecl * mut = new ast::EnumDecl( *location, type->name );
     116                mut->linkage = ast::Linkage::Compiler;
     117                decl = mut;
     118                symtab.addEnum( decl );
     119                addDeclAfterOutside( decl );
     120        }
     121
     122        ast::EnumInstType * mut = ast::mutate( type );
     123
     124        // Just linking in the node.
     125        mut->base = decl;
     126
     127        if ( !decl->body ) {
    91128                forwardEnums[ mut->name ].push_back( mut );
    92                 type = mut;
    93         }
    94         return type;
     129        }
     130        return mut;
    95131}
    96132
     
    98134        ast::StructDecl const * decl = symtab.lookupStruct( type->name );
    99135        // It's not a semantic error if the struct is not found, just an implicit forward declaration.
    100         if ( decl ) {
    101                 // Just linking in the node.
    102                 auto mut = ast::mutate( type );
    103                 mut->base = decl;
    104                 type = mut;
    105         }
    106         if ( !decl || !decl->body ) {
    107                 auto mut = ast::mutate( type );
     136        // The unset code location is used to detect imaginary declarations.
     137        if ( !decl || decl->location.isUnset() ) {
     138                assert( location );
     139                ast::StructDecl * mut = new ast::StructDecl( *location, type->name );
     140                mut->linkage = ast::Linkage::Compiler;
     141                decl = mut;
     142                symtab.addStruct( decl );
     143                addDeclAfterOutside( decl );
     144        }
     145
     146        ast::StructInstType * mut = ast::mutate( type );
     147
     148        // Just linking in the node.
     149        mut->base = decl;
     150
     151        if ( !decl->body ) {
    108152                forwardStructs[ mut->name ].push_back( mut );
    109                 type = mut;
    110         }
    111         return type;
     153        }
     154        return mut;
    112155}
    113156
     
    115158        ast::UnionDecl const * decl = symtab.lookupUnion( type->name );
    116159        // It's not a semantic error if the union is not found, just an implicit forward declaration.
    117         if ( decl ) {
    118                 // Just linking in the node.
    119                 auto mut = ast::mutate( type );
    120                 mut->base = decl;
    121                 type = mut;
    122         }
    123         if ( !decl || !decl->body ) {
    124                 auto mut = ast::mutate( type );
     160        // The unset code location is used to detect imaginary declarations.
     161        if ( !decl || decl->location.isUnset() ) {
     162                assert( location );
     163                ast::UnionDecl * mut = new ast::UnionDecl( *location, type->name );
     164                mut->linkage = ast::Linkage::Compiler;
     165                decl = mut;
     166                symtab.addUnion( decl );
     167                addDeclAfterOutside( decl );
     168        }
     169
     170        ast::UnionInstType * mut = ast::mutate( type );
     171
     172        // Just linking in the node.
     173        mut->base = decl;
     174
     175        if ( !decl->body ) {
    125176                forwardUnions[ mut->name ].push_back( mut );
    126                 type = mut;
    127         }
    128         return type;
     177        }
     178        return mut;
    129179}
    130180
     
    228278
    229279ast::StructDecl const * LinkTypesCore::previsit( ast::StructDecl const * decl ) {
     280        enterNamespace();
    230281        return renameGenericParams( decl );
    231282}
     
    246297
    247298ast::UnionDecl const * LinkTypesCore::previsit( ast::UnionDecl const * decl ) {
     299        enterNamespace();
    248300        return renameGenericParams( decl );
    249301}
     
    264316
    265317ast::TraitDecl const * LinkTypesCore::postvisit( ast::TraitDecl const * decl ) {
    266         auto mut = ast::mutate( decl );
    267         if ( mut->name == "sized" ) {
    268                 // "sized" is a special trait - flick the sized status on for the type variable.
    269                 assertf( mut->params.size() == 1, "Built-in trait 'sized' has incorrect number of parameters: %zd", decl->params.size() );
    270                 ast::TypeDecl * td = mut->params.front().get_and_mutate();
    271                 td->sized = true;
    272         }
    273 
    274318        // There is some overlap with code from decayForallPointers,
    275319        // perhaps reorganization or shared helper functions are called for.
    276320        // Move assertions from type parameters into the body of the trait.
     321        auto mut = ast::mutate( decl );
    277322        for ( ast::ptr<ast::TypeDecl> const & td : decl->params ) {
    278323                auto expanded = expandAssertions( td->assertions );
Note: See TracChangeset for help on using the changeset viewer.