Changeset 2a301ff for src/Validate/LinkReferenceToTypes.cpp
- Timestamp:
- Aug 31, 2023, 11:31:15 PM (2 years ago)
- 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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Validate/LinkReferenceToTypes.cpp
r92355883 r2a301ff 10 10 // Created On : Thr Apr 21 11:41:00 2022 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Tue Sep 20 16:17:00 202213 // Update Count : 212 // Last Modified On : Fri Jul 14 9:19:00 2023 13 // Update Count : 3 14 14 // 15 15 … … 27 27 struct LinkTypesCore : public WithNoIdSymbolTable, 28 28 public ast::WithCodeLocation, 29 public ast::WithDeclsToAdd<>, 29 30 public ast::WithGuards, 30 31 public ast::WithShortCircuiting, … … 63 64 template<typename AggrDecl> 64 65 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 * ); 65 76 }; 77 78 void LinkTypesCore::enterNamespace() { 79 if ( inNamespace ) return; 80 inNamespace = true; 81 GuardAction( [this](){ 82 inNamespace = false; 83 declsToAddAfter.splice( declsToAddAfter.begin(), declsToAddOutside ); 84 } ); 85 } 86 87 void LinkTypesCore::addDeclAfterOutside( ast::Decl const * decl ) { 88 if ( inNamespace ) { 89 declsToAddOutside.emplace_back( decl ); 90 } else { 91 declsToAddAfter.emplace_back( decl ); 92 } 93 } 66 94 67 95 ast::TypeInstType const * LinkTypesCore::postvisit( ast::TypeInstType const * type ) { … … 81 109 ast::EnumDecl const * decl = symtab.lookupEnum( type->name ); 82 110 // 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 ) { 91 128 forwardEnums[ mut->name ].push_back( mut ); 92 type = mut; 93 } 94 return type; 129 } 130 return mut; 95 131 } 96 132 … … 98 134 ast::StructDecl const * decl = symtab.lookupStruct( type->name ); 99 135 // 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 ) { 108 152 forwardStructs[ mut->name ].push_back( mut ); 109 type = mut; 110 } 111 return type; 153 } 154 return mut; 112 155 } 113 156 … … 115 158 ast::UnionDecl const * decl = symtab.lookupUnion( type->name ); 116 159 // 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 ) { 125 176 forwardUnions[ mut->name ].push_back( mut ); 126 type = mut; 127 } 128 return type; 177 } 178 return mut; 129 179 } 130 180 … … 228 278 229 279 ast::StructDecl const * LinkTypesCore::previsit( ast::StructDecl const * decl ) { 280 enterNamespace(); 230 281 return renameGenericParams( decl ); 231 282 } … … 246 297 247 298 ast::UnionDecl const * LinkTypesCore::previsit( ast::UnionDecl const * decl ) { 299 enterNamespace(); 248 300 return renameGenericParams( decl ); 249 301 } … … 264 316 265 317 ast::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 274 318 // There is some overlap with code from decayForallPointers, 275 319 // perhaps reorganization or shared helper functions are called for. 276 320 // Move assertions from type parameters into the body of the trait. 321 auto mut = ast::mutate( decl ); 277 322 for ( ast::ptr<ast::TypeDecl> const & td : decl->params ) { 278 323 auto expanded = expandAssertions( td->assertions );
Note:
See TracChangeset
for help on using the changeset viewer.