Changes in / [1d386a7:638ac26]
- Location:
- src
- Files:
-
- 23 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
r1d386a7 r638ac26 164 164 previsit( (BaseSyntaxNode *)node ); 165 165 GuardAction( [this, node](){ 166 if ( printExprTypes ) {166 if ( printExprTypes && node->result ) { 167 167 output << " /* " << genType( node->result, "", pretty, genC ) << " */ "; 168 168 } … … 224 224 225 225 void CodeGenerator::handleAggregate( AggregateDecl * aggDecl, const std::string & kind ) { 226 if( ! aggDecl-> get_parameters().empty() && ! genC ) {226 if( ! aggDecl->parameters.empty() && ! genC ) { 227 227 // assertf( ! genC, "Aggregate type parameters should not reach code generation." ); 228 228 output << "forall("; 229 genCommaList( aggDecl-> get_parameters().begin(), aggDecl->get_parameters().end() );229 genCommaList( aggDecl->parameters.begin(), aggDecl->parameters.end() ); 230 230 output << ")" << endl; 231 231 output << indent; … … 233 233 234 234 output << kind; 235 genAttributes( aggDecl-> get_attributes());236 output << aggDecl-> get_name();235 genAttributes( aggDecl->attributes ); 236 output << aggDecl->name; 237 237 238 238 if ( aggDecl->has_body() ) { 239 std::list< Declaration * > & memb = aggDecl-> get_members();239 std::list< Declaration * > & memb = aggDecl->members; 240 240 output << " {" << endl; 241 241 -
src/CodeGen/GenType.cc
r1d386a7 r638ac26 48 48 void postvisit( ZeroType * zeroType ); 49 49 void postvisit( OneType * oneType ); 50 void postvisit( GlobalScopeType * globalType ); 50 51 void postvisit( TraitInstType * inst ); 51 52 void postvisit( TypeofType * typeof ); 53 void postvisit( QualifiedType * qualType ); 52 54 53 55 private: … … 291 293 } 292 294 295 void GenType::postvisit( GlobalScopeType * globalType ) { 296 assertf( ! genC, "Global scope type should not reach code generation." ); 297 handleQualifiers( globalType ); 298 } 299 293 300 void GenType::postvisit( TraitInstType * inst ) { 294 301 assertf( ! genC, "Trait types should not reach code generation." ); … … 307 314 } 308 315 316 void GenType::postvisit( QualifiedType * qualType ) { 317 assertf( ! genC, "Qualified types should not reach code generation." ); 318 std::ostringstream os; 319 os << genType( qualType->parent, "", pretty, genC, lineMarks ) << "." << genType( qualType->child, "", pretty, genC, lineMarks ) << typeString; 320 typeString = os.str(); 321 handleQualifiers( qualType ); 322 } 323 309 324 void GenType::handleQualifiers( Type * type ) { 310 325 if ( type->get_const() ) { -
src/Common/PassVisitor.h
r1d386a7 r638ac26 133 133 virtual void visit( ArrayType * arrayType ) override final; 134 134 virtual void visit( ReferenceType * referenceType ) override final; 135 virtual void visit( QualifiedType * qualType ) override final; 135 136 virtual void visit( FunctionType * functionType ) override final; 136 137 virtual void visit( StructInstType * aggregateUseType ) override final; … … 145 146 virtual void visit( ZeroType * zeroType ) override final; 146 147 virtual void visit( OneType * oneType ) override final; 148 virtual void visit( GlobalScopeType * globalType ) override final; 147 149 148 150 virtual void visit( Designation * designation ) override final; … … 233 235 virtual Type * mutate( ArrayType * arrayType ) override final; 234 236 virtual Type * mutate( ReferenceType * referenceType ) override final; 237 virtual Type * mutate( QualifiedType * qualType ) override final; 235 238 virtual Type * mutate( FunctionType * functionType ) override final; 236 239 virtual Type * mutate( StructInstType * aggregateUseType ) override final; … … 245 248 virtual Type * mutate( ZeroType * zeroType ) override final; 246 249 virtual Type * mutate( OneType * oneType ) override final; 250 virtual Type * mutate( GlobalScopeType * globalType ) override final; 247 251 248 252 virtual Designation * mutate( Designation * designation ) override final; -
src/Common/PassVisitor.impl.h
r1d386a7 r638ac26 2262 2262 2263 2263 //-------------------------------------------------------------------------- 2264 // QualifiedType 2265 template< typename pass_type > 2266 void PassVisitor< pass_type >::visit( QualifiedType * node ) { 2267 VISIT_START( node ); 2268 2269 maybeAccept_impl( node->forall, *this ); 2270 maybeAccept_impl( node->parent, *this ); 2271 maybeAccept_impl( node->child, *this ); 2272 2273 VISIT_END( node ); 2274 } 2275 2276 template< typename pass_type > 2277 Type * PassVisitor< pass_type >::mutate( QualifiedType * node ) { 2278 MUTATE_START( node ); 2279 2280 maybeMutate_impl( node->forall, *this ); 2281 maybeMutate_impl( node->parent, *this ); 2282 maybeMutate_impl( node->child, *this ); 2283 2284 MUTATE_END( Type, node ); 2285 } 2286 2287 //-------------------------------------------------------------------------- 2264 2288 // FunctionType 2265 2289 template< typename pass_type > … … 2554 2578 2555 2579 //-------------------------------------------------------------------------- 2580 // GlobalScopeType 2581 template< typename pass_type > 2582 void PassVisitor< pass_type >::visit( GlobalScopeType * node ) { 2583 VISIT_START( node ); 2584 2585 maybeAccept_impl( node->forall, *this ); 2586 2587 VISIT_END( node ); 2588 } 2589 2590 template< typename pass_type > 2591 Type * PassVisitor< pass_type >::mutate( GlobalScopeType * node ) { 2592 MUTATE_START( node ); 2593 2594 maybeMutate_impl( node->forall, *this ); 2595 2596 MUTATE_END( Type, node ); 2597 } 2598 2599 //-------------------------------------------------------------------------- 2556 2600 // Designation 2557 2601 template< typename pass_type > -
src/Concurrency/Keywords.cc
r1d386a7 r638ac26 501 501 void MutexKeyword::postvisit(StructDecl* decl) { 502 502 503 if( decl->name == "monitor_desc" ) {503 if( decl->name == "monitor_desc" && decl->body ) { 504 504 assert( !monitor_decl ); 505 505 monitor_decl = decl; 506 506 } 507 else if( decl->name == "monitor_guard_t" ) {507 else if( decl->name == "monitor_guard_t" && decl->body ) { 508 508 assert( !guard_decl ); 509 509 guard_decl = decl; 510 510 } 511 else if( decl->name == "monitor_dtor_guard_t" ) {511 else if( decl->name == "monitor_dtor_guard_t" && decl->body ) { 512 512 assert( !dtor_guard_decl ); 513 513 dtor_guard_decl = decl; -
src/InitTweak/FixInit.cc
r1d386a7 r638ac26 1163 1163 1164 1164 std::string fname = getFunctionName( appExpr ); 1165 if ( fname == function-> get_name()) {1165 if ( fname == function->name ) { 1166 1166 // call to same kind of function 1167 Expression * firstParam = appExpr-> get_args().front();1167 Expression * firstParam = appExpr->args.front(); 1168 1168 1169 1169 if ( isThisExpression( firstParam, thisParam ) ) { … … 1174 1174 // if first parameter is a member expression on the this parameter, 1175 1175 // then remove the member from unhandled set. 1176 if ( isThisExpression( memberExpr-> get_aggregate(), thisParam ) ) {1177 unhandled.erase( memberExpr-> get_member());1176 if ( isThisExpression( memberExpr->aggregate, thisParam ) ) { 1177 unhandled.erase( memberExpr->member ); 1178 1178 } 1179 1179 } -
src/Parser/DeclarationNode.cc
r1d386a7 r638ac26 253 253 return newnode; 254 254 } // DeclarationNode::newFromTypedef 255 256 DeclarationNode * DeclarationNode::newFromGlobalScope() { 257 DeclarationNode * newnode = new DeclarationNode; 258 newnode->type = new TypeData( TypeData::GlobalScope ); 259 return newnode; 260 } 261 262 DeclarationNode * DeclarationNode::newQualifiedType( DeclarationNode * parent, DeclarationNode * child) { 263 DeclarationNode * newnode = new DeclarationNode; 264 newnode->type = new TypeData( TypeData::Qualified ); 265 newnode->type->qualified.parent = parent->type; 266 newnode->type->qualified.child = child->type; 267 parent->type = nullptr; 268 child->type = nullptr; 269 delete parent; 270 delete child; 271 return newnode; 272 } 255 273 256 274 DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) { … … 985 1003 try { 986 1004 Declaration * decl = cur->build(); 987 if ( decl ) {988 989 990 991 992 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );993 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr);994 obj->location = cur->location;995 * out++ = obj;996 delete agg;997 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {998 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );999 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr);1000 obj->location = cur->location;1001 * out++ = obj;1002 } // if1005 assert( decl ); 1006 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { 1007 dwt->location = cur->location; 1008 * out++ = dwt; 1009 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) { 1010 // xxx - this might be where anonymous struct members are added - should be conditional on struct name 1011 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->name ); 1012 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); 1013 obj->location = cur->location; 1014 * out++ = obj; 1015 delete agg; 1016 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) { 1017 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->name ); 1018 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr ); 1019 obj->location = cur->location; 1020 * out++ = obj; 1003 1021 } // if 1004 1022 } catch( SemanticErrorException &e ) { -
src/Parser/ParseNode.h
r1d386a7 r638ac26 231 231 static DeclarationNode * newForall( DeclarationNode * ); 232 232 static DeclarationNode * newFromTypedef( const std::string * ); 233 static DeclarationNode * newFromGlobalScope(); 234 static DeclarationNode * newQualifiedType( DeclarationNode *, DeclarationNode * ); 233 235 static DeclarationNode * newFunction( const std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body ); 234 236 static DeclarationNode * newAggregate( Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ); -
src/Parser/TypeData.cc
r1d386a7 r638ac26 37 37 case Reference: 38 38 case EnumConstant: 39 case GlobalScope: 39 40 // nothing else to initialize 40 41 break; … … 98 99 case Builtin: 99 100 // builtin = new Builtin_t; 101 case Qualified: 102 qualified.parent = nullptr; 103 qualified.child = nullptr; 100 104 break; 101 105 } // switch … … 112 116 case Reference: 113 117 case EnumConstant: 118 case GlobalScope: 114 119 // nothing to destroy 115 120 break; … … 165 170 // delete builtin; 166 171 break; 172 case Qualified: 173 delete qualified.parent; 174 delete qualified.child; 167 175 } // switch 168 176 } // TypeData::~TypeData … … 180 188 case Pointer: 181 189 case Reference: 190 case GlobalScope: 182 191 // nothing else to copy 183 192 break; … … 237 246 assert( builtintype == DeclarationNode::Zero || builtintype == DeclarationNode::One ); 238 247 newtype->builtintype = builtintype; 248 break; 249 case Qualified: 250 newtype->qualified.parent = maybeClone( qualified.parent ); 251 newtype->qualified.child = maybeClone( qualified.child ); 239 252 break; 240 253 } // switch … … 465 478 return new EnumInstType( buildQualifiers( td ), "" ); 466 479 case TypeData::SymbolicInst: 467 return buildSymbolicInst( td ); ;480 return buildSymbolicInst( td ); 468 481 case TypeData::Tuple: 469 482 return buildTuple( td ); … … 480 493 return new VarArgsType( buildQualifiers( td ) ); 481 494 } 495 case TypeData::GlobalScope: 496 return new GlobalScopeType(); 497 case TypeData::Qualified: 498 return new QualifiedType( buildQualifiers( td ), typebuild( td->qualified.parent ), typebuild( td->qualified.child ) ); 482 499 case TypeData::Symbolic: 483 500 case TypeData::Enum: … … 485 502 assert( false ); 486 503 } // switch 504 487 505 return nullptr; 488 506 } // typebuild … … 893 911 assert( td->kind == TypeData::Function ); 894 912 FunctionType * ft = new FunctionType( buildQualifiers( td ), ! td->function.params || td->function.params->hasEllipsis ); 895 buildList( td->function.params, ft-> get_parameters());896 buildForall( td->forall, ft-> get_forall());913 buildList( td->function.params, ft->parameters ); 914 buildForall( td->forall, ft->forall ); 897 915 if ( td->base ) { 898 916 switch ( td->base->kind ) { 899 917 case TypeData::Tuple: 900 buildList( td->base->tuple, ft-> get_returnVals());918 buildList( td->base->tuple, ft->returnVals ); 901 919 break; 902 920 default: -
src/Parser/TypeData.h
r1d386a7 r638ac26 27 27 struct TypeData { 28 28 enum Kind { Basic, Pointer, Array, Reference, Function, Aggregate, AggregateInst, Enum, EnumConstant, Symbolic, 29 SymbolicInst, Tuple, Typeof, Builtin, Unknown };29 SymbolicInst, Tuple, Typeof, Builtin, GlobalScope, Qualified, Unknown }; 30 30 31 31 struct Aggregate_t { … … 75 75 }; 76 76 77 struct Qualified_t { // qualified type S.T 78 TypeData * parent; 79 TypeData * child; 80 }; 81 77 82 CodeLocation location; 78 83 … … 88 93 DeclarationNode * forall; 89 94 90 // Basic_t basic;91 95 Aggregate_t aggregate; 92 96 AggInst_t aggInst; 93 97 Array_t array; 94 98 Enumeration_t enumeration; 95 // Variable_t variable;96 99 Function_t function; 97 100 Symbolic_t symbolic; 101 Qualified_t qualified; 98 102 DeclarationNode * tuple; 99 103 ExpressionNode * typeexpr; -
src/Parser/parser.yy
r1d386a7 r638ac26 1821 1821 { $$ = DeclarationNode::newFromTypedef( $1 ); } 1822 1822 | '.' TYPEDEFname 1823 { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }1823 { $$ = DeclarationNode::newQualifiedType( DeclarationNode::newFromGlobalScope(), DeclarationNode::newFromTypedef( $2 ) ); } 1824 1824 | type_name '.' TYPEDEFname 1825 { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }1825 { $$ = DeclarationNode::newQualifiedType( $1, DeclarationNode::newFromTypedef( $3 ) ); } 1826 1826 | typegen_name 1827 1827 | '.' typegen_name 1828 { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }1828 { $$ = DeclarationNode::newQualifiedType( DeclarationNode::newFromGlobalScope(), $2 ); } 1829 1829 | type_name '.' typegen_name 1830 { SemanticError( yylloc, "Qualified name is currently unimplemented." ); $$ = nullptr; }1830 { $$ = DeclarationNode::newQualifiedType( $1, $3 ); } 1831 1831 ; 1832 1832 … … 1929 1929 { distExt( $3 ); $$ = distAttr( $2, $3 ); } // mark all fields in list 1930 1930 | typedef_declaration ';' // CFA 1931 { SemanticError( yylloc, "Typedef in aggregate is currently unimplemented." ); $$ = nullptr; }1932 1931 | cfa_field_declaring_list ';' // CFA, new style field declaration 1933 1932 | EXTENSION cfa_field_declaring_list ';' // GCC 1934 1933 { distExt( $2 ); $$ = $2; } // mark all fields in list 1935 1934 | cfa_typedef_declaration ';' // CFA 1936 { SemanticError( yylloc, "Typedef in aggregate is currently unimplemented." ); $$ = nullptr; }1937 1935 | static_assert // C11 1938 1936 ; -
src/SymTab/Indexer.cc
r1d386a7 r638ac26 272 272 } 273 273 274 NamedTypeDecl *Indexer::globalLookupType( const std::string &id ) const { 275 return lookupTypeAtScope( id, 0 ); 276 } 277 278 StructDecl *Indexer::globalLookupStruct( const std::string &id ) const { 279 return lookupStructAtScope( id, 0 ); 280 } 281 282 UnionDecl *Indexer::globalLookupUnion( const std::string &id ) const { 283 return lookupUnionAtScope( id, 0 ); 284 } 285 286 EnumDecl *Indexer::globalLookupEnum( const std::string &id ) const { 287 return lookupEnumAtScope( id, 0 ); 288 } 289 274 290 EnumDecl *Indexer::lookupEnum( const std::string &id ) const { 275 291 if ( ! tables ) return 0; … … 347 363 if ( ! tables ) return 0; 348 364 if ( tables->scope < scope ) return 0; 365 if ( tables->scope > scope ) return tables->base.lookupTypeAtScope( id, scope ); 349 366 350 367 TypeTable::const_iterator ret = tables->typeTable.find( id ); … … 355 372 if ( ! tables ) return 0; 356 373 if ( tables->scope < scope ) return 0; 374 if ( tables->scope > scope ) return tables->base.lookupStructAtScope( id, scope ); 357 375 358 376 StructTable::const_iterator ret = tables->structTable.find( id ); … … 363 381 if ( ! tables ) return 0; 364 382 if ( tables->scope < scope ) return 0; 383 if ( tables->scope > scope ) return tables->base.lookupEnumAtScope( id, scope ); 365 384 366 385 EnumTable::const_iterator ret = tables->enumTable.find( id ); … … 371 390 if ( ! tables ) return 0; 372 391 if ( tables->scope < scope ) return 0; 392 if ( tables->scope > scope ) return tables->base.lookupUnionAtScope( id, scope ); 373 393 374 394 UnionTable::const_iterator ret = tables->unionTable.find( id ); … … 379 399 if ( ! tables ) return 0; 380 400 if ( tables->scope < scope ) return 0; 401 if ( tables->scope > scope ) return tables->base.lookupTraitAtScope( id, scope ); 381 402 382 403 TraitTable::const_iterator ret = tables->traitTable.find( id ); … … 486 507 487 508 bool addedTypeConflicts( NamedTypeDecl *existing, NamedTypeDecl *added ) { 488 if ( existing-> get_base() == 0) {509 if ( existing->base == nullptr ) { 489 510 return false; 490 } else if ( added-> get_base() == 0) {511 } else if ( added->base == nullptr ) { 491 512 return true; 492 513 } else { 493 SemanticError( added, "redeclaration of " ); 494 } 514 assert( existing->base && added->base ); 515 // typedef redeclarations are errors only if types are different 516 if ( ! ResolvExpr::typesCompatible( existing->base, added->base, Indexer() ) ) { 517 SemanticError( added->location, "redeclaration of " + added->name ); 518 } 519 } 520 // does not need to be added to the table if both existing and added have a base that are the same 521 return true; 495 522 } 496 523 … … 499 526 makeWritable(); 500 527 501 const std::string &id = decl-> get_name();528 const std::string &id = decl->name; 502 529 TypeTable::iterator existing = tables->typeTable.find( id ); 503 530 if ( existing == tables->typeTable.end() ) { … … 532 559 makeWritable(); 533 560 534 const std::string &id = decl-> get_name();561 const std::string &id = decl->name; 535 562 StructTable::iterator existing = tables->structTable.find( id ); 536 563 if ( existing == tables->structTable.end() ) { … … 551 578 makeWritable(); 552 579 553 const std::string &id = decl-> get_name();580 const std::string &id = decl->name; 554 581 EnumTable::iterator existing = tables->enumTable.find( id ); 555 582 if ( existing == tables->enumTable.end() ) { … … 575 602 makeWritable(); 576 603 577 const std::string &id = decl-> get_name();604 const std::string &id = decl->name; 578 605 UnionTable::iterator existing = tables->unionTable.find( id ); 579 606 if ( existing == tables->unionTable.end() ) { … … 594 621 makeWritable(); 595 622 596 const std::string &id = decl-> get_name();623 const std::string &id = decl->name; 597 624 TraitTable::iterator existing = tables->traitTable.find( id ); 598 625 if ( existing == tables->traitTable.end() ) { -
src/SymTab/Indexer.h
r1d386a7 r638ac26 70 70 /// Gets the top-most trait declaration with the given ID 71 71 TraitDecl *lookupTrait( const std::string &id ) const; 72 73 /// Gets the type declaration with the given ID at global scope 74 NamedTypeDecl *globalLookupType( const std::string &id ) const; 75 /// Gets the struct declaration with the given ID at global scope 76 StructDecl *globalLookupStruct( const std::string &id ) const; 77 /// Gets the union declaration with the given ID at global scope 78 UnionDecl *globalLookupUnion( const std::string &id ) const; 79 /// Gets the enum declaration with the given ID at global scope 80 EnumDecl *globalLookupEnum( const std::string &id ) const; 72 81 73 82 void print( std::ostream &os, int indent = 0 ) const; -
src/SymTab/Mangler.cc
r1d386a7 r638ac26 60 60 void postvisit( ZeroType * zeroType ); 61 61 void postvisit( OneType * oneType ); 62 void postvisit( QualifiedType * qualType ); 62 63 63 64 std::string get_mangleName() { return mangleName.str(); } … … 171 172 "w", // SignedInt128 172 173 "Uw", // UnsignedInt128 173 "x", 174 "y", 174 "x", // Float80 175 "y", // Float128 175 176 }; 176 177 static_assert( … … 312 313 void Mangler::postvisit( OneType * ) { 313 314 mangleName << "O"; 315 } 316 317 void Mangler::postvisit( QualifiedType * qualType ) { 318 maybeAccept( qualType->parent, *visitor ); 319 mangleName << "__"; 320 maybeAccept( qualType->child, *visitor ); 314 321 } 315 322 -
src/SymTab/Validate.cc
r1d386a7 r638ac26 77 77 class SwitchStmt; 78 78 79 #define debugPrint( x ) if ( doDebug ) { std::cout << x; }79 #define debugPrint( x ) if ( doDebug ) x 80 80 81 81 namespace SymTab { 82 /// hoists declarations that are difficult to hoist while parsing 83 struct HoistTypeDecls final : public WithDeclsToAdd { 84 void previsit( SizeofExpr * ); 85 void previsit( AlignofExpr * ); 86 void previsit( UntypedOffsetofExpr * ); 87 void handleType( Type * ); 88 }; 89 90 struct FixQualifiedTypes final : public WithIndexer { 91 Type * postmutate( QualifiedType * ); 92 }; 93 82 94 struct HoistStruct final : public WithDeclsToAdd, public WithGuards { 83 95 /// Flattens nested struct types 84 96 static void hoistStruct( std::list< Declaration * > &translationUnit ); 85 97 86 void previsit( EnumInstType * enumInstType );87 void previsit( StructInstType * structInstType );88 void previsit( UnionInstType * unionInstType );89 98 void previsit( StructDecl * aggregateDecl ); 90 99 void previsit( UnionDecl * aggregateDecl ); 91 100 void previsit( StaticAssertDecl * assertDecl ); 101 void previsit( StructInstType * type ); 102 void previsit( UnionInstType * type ); 103 void previsit( EnumInstType * type ); 92 104 93 105 private: … … 112 124 113 125 /// Associates forward declarations of aggregates with their definitions 114 struct LinkReferenceToTypes final : public WithIndexer, public WithGuards {126 struct LinkReferenceToTypes final : public WithIndexer, public WithGuards, public WithVisitorRef<LinkReferenceToTypes>, public WithShortCircuiting { 115 127 LinkReferenceToTypes( const Indexer *indexer ); 116 128 void postvisit( TypeInstType *typeInst ); … … 120 132 void postvisit( UnionInstType *unionInst ); 121 133 void postvisit( TraitInstType *traitInst ); 134 void previsit( QualifiedType * qualType ); 135 void postvisit( QualifiedType * qualType ); 122 136 123 137 void postvisit( EnumDecl *enumDecl ); … … 165 179 }; 166 180 167 struct EliminateTypedef final : public WithVisitorRef<EliminateTypedef>, public WithGuards{168 EliminateTypedef() : scopeLevel( 0 ) {}181 struct ReplaceTypedef final : public WithVisitorRef<ReplaceTypedef>, public WithGuards, public WithShortCircuiting, public WithDeclsToAdd { 182 ReplaceTypedef() : scopeLevel( 0 ) {} 169 183 /// Replaces typedefs by forward declarations 170 static void eliminateTypedef( std::list< Declaration * > &translationUnit ); 171 184 static void replaceTypedef( std::list< Declaration * > &translationUnit ); 185 186 void premutate( QualifiedType * ); 187 Type * postmutate( QualifiedType * qualType ); 172 188 Type * postmutate( TypeInstType * aggregateUseType ); 173 189 Declaration * postmutate( TypedefDecl * typeDecl ); … … 180 196 181 197 void premutate( CompoundStmt * compoundStmt ); 182 CompoundStmt * postmutate( CompoundStmt * compoundStmt );183 198 184 199 void premutate( StructDecl * structDecl ); 185 Declaration * postmutate( StructDecl * structDecl );186 200 void premutate( UnionDecl * unionDecl ); 187 Declaration * postmutate( UnionDecl * unionDecl );188 201 void premutate( EnumDecl * enumDecl ); 189 Declaration * postmutate( EnumDecl * enumDecl );190 Declaration * postmutate( TraitDecl * contextDecl );191 202 192 203 void premutate( FunctionType * ftype ); … … 194 205 private: 195 206 template<typename AggDecl> 196 AggDecl *handleAggregate( AggDecl * aggDecl );197 198 template<typename AggDecl>199 207 void addImplicitTypedef( AggDecl * aggDecl ); 208 template< typename AggDecl > 209 void handleAggregate( AggDecl * aggr ); 200 210 201 211 typedef std::unique_ptr<TypedefDecl> TypedefDeclPtr; 202 212 typedef ScopedMap< std::string, std::pair< TypedefDeclPtr, int > > TypedefMap; 203 typedef std::map< std::string, TypeDecl * > TypeDeclMap; 213 typedef std::map< std::string, TypeDecl * > TypeDeclMap; // xxx - convert to ScopedMap 204 214 TypedefMap typedefNames; 205 215 TypeDeclMap typedeclNames; 206 216 int scopeLevel; 207 217 bool inFunctionType = false; 218 }; 219 220 struct EliminateTypedef { 221 /// removes TypedefDecls from the AST 222 static void eliminateTypedef( std::list< Declaration * > &translationUnit ); 223 224 template<typename AggDecl> 225 void handleAggregate( AggDecl *aggregateDecl ); 226 227 void previsit( StructDecl * aggregateDecl ); 228 void previsit( UnionDecl * aggregateDecl ); 229 void previsit( CompoundStmt * compoundStmt ); 208 230 }; 209 231 … … 263 285 PassVisitor<FindSpecialDeclarations> finder; 264 286 PassVisitor<LabelAddressFixer> labelAddrFixer; 265 266 EliminateTypedef::eliminateTypedef( translationUnit ); 267 HoistStruct::hoistStruct( translationUnit ); // must happen after EliminateTypedef, so that aggregate typedefs occur in the correct order 287 PassVisitor<HoistTypeDecls> hoistDecls; 288 PassVisitor<FixQualifiedTypes> fixQual; 289 290 acceptAll( translationUnit, hoistDecls ); 291 ReplaceTypedef::replaceTypedef( translationUnit ); 268 292 ReturnTypeFixer::fix( translationUnit ); // must happen before autogen 269 293 acceptAll( translationUnit, epc ); // must happen before VerifyCtorDtorAssign, because void return objects should not exist; before LinkReferenceToTypes because it is an indexer and needs correct types for mangling 270 294 acceptAll( translationUnit, lrt ); // must happen before autogen, because sized flag needs to propagate to generated functions 295 mutateAll( translationUnit, fixQual ); // must happen after LinkReferenceToTypes, because aggregate members are accessed 296 HoistStruct::hoistStruct( translationUnit ); // must happen after EliminateTypedef, so that aggregate typedefs occur in the correct order 297 EliminateTypedef::eliminateTypedef( translationUnit ); // 271 298 acceptAll( translationUnit, genericParams ); // check as early as possible - can't happen before LinkReferenceToTypes 272 299 VerifyCtorDtorAssign::verify( translationUnit ); // must happen before autogen, because autogen examines existing ctor/dtors … … 294 321 } 295 322 323 324 void HoistTypeDecls::handleType( Type * type ) { 325 // some type declarations are buried in expressions and not easy to hoist during parsing; hoist them here 326 AggregateDecl * aggr = nullptr; 327 if ( StructInstType * inst = dynamic_cast< StructInstType * >( type ) ) { 328 aggr = inst->baseStruct; 329 } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( type ) ) { 330 aggr = inst->baseUnion; 331 } else if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( type ) ) { 332 aggr = inst->baseEnum; 333 } 334 if ( aggr && aggr->body ) { 335 declsToAddBefore.push_front( aggr ); 336 } 337 } 338 339 void HoistTypeDecls::previsit( SizeofExpr * expr ) { 340 handleType( expr->type ); 341 } 342 343 void HoistTypeDecls::previsit( AlignofExpr * expr ) { 344 handleType( expr->type ); 345 } 346 347 void HoistTypeDecls::previsit( UntypedOffsetofExpr * expr ) { 348 handleType( expr->type ); 349 } 350 351 352 Type * FixQualifiedTypes::postmutate( QualifiedType * qualType ) { 353 // TODO: change asserts to SemanticErrors as necessary 354 Type * parent = qualType->parent; 355 Type * child = qualType->child; 356 if ( dynamic_cast< GlobalScopeType * >( qualType->parent ) ) { 357 // .T => lookup T at global scope 358 if ( StructInstType * inst = dynamic_cast< StructInstType * >( child ) ) { 359 auto aggr = indexer.globalLookupStruct( inst->name ); 360 return new StructInstType( qualType->get_qualifiers(), aggr ); 361 } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( child ) ) { 362 auto aggr = indexer.globalLookupUnion( inst->name ); 363 return new UnionInstType( qualType->get_qualifiers(), aggr ); 364 } else if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( child ) ) { 365 auto aggr = indexer.globalLookupEnum( inst->name ); 366 return new EnumInstType( qualType->get_qualifiers(), aggr ); 367 } else if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( child ) ) { 368 auto td = indexer.globalLookupType( inst->name ); 369 assertf( td, "did not find type at global scope with name: %s", inst->name.c_str() ); 370 auto base = td->base; 371 if ( base ) return td->base->clone(); 372 assert( false ); 373 } else { 374 // .T => T is not a SUE type name 375 assert( false ); 376 } 377 } else { 378 // S.T => S must be an aggregate type, find the declaration for T in S. 379 AggregateDecl * aggr = nullptr; 380 if ( StructInstType * inst = dynamic_cast< StructInstType * >( parent ) ) { 381 aggr = inst->baseStruct; 382 } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * > ( parent ) ) { 383 aggr = inst->baseUnion; 384 } else { 385 assert( false ); 386 } 387 assert( aggr ); // TODO: need to handle forward declarations 388 for ( Declaration * member : aggr->members ) { 389 if ( StructInstType * inst = dynamic_cast< StructInstType * >( child ) ) { 390 if ( StructDecl * aggr = dynamic_cast< StructDecl * >( member ) ) { 391 if ( aggr->name == inst->name ) { 392 return new StructInstType( qualType->get_qualifiers(), aggr ); 393 } 394 } 395 } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( child ) ) { 396 if ( UnionDecl * aggr = dynamic_cast< UnionDecl * > ( member ) ) { 397 if ( aggr->name == inst->name ) { 398 return new UnionInstType( qualType->get_qualifiers(), aggr ); 399 } 400 } 401 } else if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( child ) ) { 402 if ( EnumDecl * aggr = dynamic_cast< EnumDecl * > ( member ) ) { 403 if ( aggr->name == inst->name ) { 404 return new EnumInstType( qualType->get_qualifiers(), aggr ); 405 } 406 } 407 } else if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( child ) ) { 408 // struct typedefs are being replaced by forward decls too early; move it to hoist struct 409 if ( NamedTypeDecl * aggr = dynamic_cast< NamedTypeDecl * > ( member ) ) { 410 if ( aggr->name == inst->name ) { 411 if ( aggr->base ) return aggr->base->clone(); 412 assert( false ); 413 } 414 } 415 } else { 416 // S.T - S is not an aggregate => error 417 assertf( false, "unhandled qualified child type: %s", toCString(qualType) ); 418 } 419 } 420 // failed to find a satisfying definition of type 421 assertf( false, "failed to find a satisfying definition of %s in %s", toCString(child), toCString(parent) ); 422 } 423 424 // ... may want to link canonical SUE definition to each forward decl so that it becomes easier to lookup? 425 } 426 427 296 428 void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) { 297 429 PassVisitor<HoistStruct> hoister; … … 303 435 } 304 436 437 namespace { 438 void qualifiedName( AggregateDecl * aggr, std::ostringstream & ss ) { 439 if ( aggr->parent ) qualifiedName( aggr->parent, ss ); 440 ss << "__" << aggr->name; 441 } 442 443 // mangle nested type names using entire parent chain 444 std::string qualifiedName( AggregateDecl * aggr ) { 445 std::ostringstream ss; 446 qualifiedName( aggr, ss ); 447 return ss.str(); 448 } 449 } 450 305 451 template< typename AggDecl > 306 452 void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) { 307 453 if ( parentAggr ) { 454 aggregateDecl->parent = parentAggr; 455 aggregateDecl->name = qualifiedName( aggregateDecl ); 308 456 // Add elements in stack order corresponding to nesting structure. 309 457 declsToAddBefore.push_front( aggregateDecl ); … … 316 464 } 317 465 318 void HoistStruct::previsit( EnumInstType * inst ) {319 if ( inst->baseEnum && inst->baseEnum->body ) {320 declsToAddBefore.push_front( inst->baseEnum );321 }322 }323 324 void HoistStruct::previsit( StructInstType * inst ) {325 if ( inst->baseStruct && inst->baseStruct->body ) {326 declsToAddBefore.push_front( inst->baseStruct );327 }328 }329 330 void HoistStruct::previsit( UnionInstType * inst ) {331 if ( inst->baseUnion && inst->baseUnion->body ) {332 declsToAddBefore.push_front( inst->baseUnion );333 }334 }335 336 466 void HoistStruct::previsit( StaticAssertDecl * assertDecl ) { 337 467 if ( parentAggr ) { … … 348 478 } 349 479 480 void HoistStruct::previsit( StructInstType * type ) { 481 // need to reset type name after expanding to qualified name 482 assert( type->baseStruct ); 483 type->name = type->baseStruct->name; 484 } 485 486 void HoistStruct::previsit( UnionInstType * type ) { 487 assert( type->baseUnion ); 488 type->name = type->baseUnion->name; 489 } 490 491 void HoistStruct::previsit( EnumInstType * type ) { 492 assert( type->baseEnum ); 493 type->name = type->baseEnum->name; 494 } 495 496 497 bool isTypedef( Declaration *decl ) { 498 return dynamic_cast< TypedefDecl * >( decl ); 499 } 500 501 void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) { 502 PassVisitor<EliminateTypedef> eliminator; 503 acceptAll( translationUnit, eliminator ); 504 filter( translationUnit, isTypedef, true ); 505 } 506 507 template< typename AggDecl > 508 void EliminateTypedef::handleAggregate( AggDecl *aggregateDecl ) { 509 filter( aggregateDecl->members, isTypedef, true ); 510 } 511 512 void EliminateTypedef::previsit( StructDecl * aggregateDecl ) { 513 handleAggregate( aggregateDecl ); 514 } 515 516 void EliminateTypedef::previsit( UnionDecl * aggregateDecl ) { 517 handleAggregate( aggregateDecl ); 518 } 519 520 void EliminateTypedef::previsit( CompoundStmt * compoundStmt ) { 521 // remove and delete decl stmts 522 filter( compoundStmt->kids, [](Statement * stmt) { 523 if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( stmt ) ) { 524 if ( dynamic_cast< TypedefDecl * >( declStmt->decl ) ) { 525 return true; 526 } // if 527 } // if 528 return false; 529 }, true); 530 } 531 350 532 void EnumAndPointerDecay::previsit( EnumDecl *enumDecl ) { 351 533 // Set the type of each member of the enumeration to be EnumConstant 352 for ( std::list< Declaration * >::iterator i = enumDecl-> get_members().begin(); i != enumDecl->get_members().end(); ++i ) {534 for ( std::list< Declaration * >::iterator i = enumDecl->members.begin(); i != enumDecl->members.end(); ++i ) { 353 535 ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i ); 354 536 assert( obj ); 355 obj->set_type( new EnumInstType( Type::Qualifiers( Type::Const ), enumDecl-> get_name()) );537 obj->set_type( new EnumInstType( Type::Qualifiers( Type::Const ), enumDecl->name ) ); 356 538 } // for 357 539 } … … 395 577 396 578 void LinkReferenceToTypes::postvisit( EnumInstType *enumInst ) { 397 EnumDecl *st = local_indexer->lookupEnum( enumInst-> get_name());579 EnumDecl *st = local_indexer->lookupEnum( enumInst->name ); 398 580 // it's not a semantic error if the enum is not found, just an implicit forward declaration 399 581 if ( st ) { 400 //assert( ! enumInst->get_baseEnum() || enumInst->get_baseEnum()->get_members().empty() || ! st->get_members().empty() ); 401 enumInst->set_baseEnum( st ); 402 } // if 403 if ( ! st || st->get_members().empty() ) { 582 enumInst->baseEnum = st; 583 } // if 584 if ( ! st || ! st->body ) { 404 585 // use of forward declaration 405 forwardEnums[ enumInst-> get_name()].push_back( enumInst );586 forwardEnums[ enumInst->name ].push_back( enumInst ); 406 587 } // if 407 588 } … … 416 597 417 598 void LinkReferenceToTypes::postvisit( StructInstType *structInst ) { 418 StructDecl *st = local_indexer->lookupStruct( structInst-> get_name());599 StructDecl *st = local_indexer->lookupStruct( structInst->name ); 419 600 // it's not a semantic error if the struct is not found, just an implicit forward declaration 420 601 if ( st ) { 421 //assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() ); 422 structInst->set_baseStruct( st ); 423 } // if 424 if ( ! st || st->get_members().empty() ) { 602 structInst->baseStruct = st; 603 } // if 604 if ( ! st || ! st->body ) { 425 605 // use of forward declaration 426 forwardStructs[ structInst-> get_name()].push_back( structInst );606 forwardStructs[ structInst->name ].push_back( structInst ); 427 607 } // if 428 608 checkGenericParameters( structInst ); … … 430 610 431 611 void LinkReferenceToTypes::postvisit( UnionInstType *unionInst ) { 432 UnionDecl *un = local_indexer->lookupUnion( unionInst-> get_name());612 UnionDecl *un = local_indexer->lookupUnion( unionInst->name ); 433 613 // it's not a semantic error if the union is not found, just an implicit forward declaration 434 614 if ( un ) { 435 unionInst-> set_baseUnion( un );436 } // if 437 if ( ! un || un->get_members().empty()) {615 unionInst->baseUnion = un; 616 } // if 617 if ( ! un || ! un->body ) { 438 618 // use of forward declaration 439 forwardUnions[ unionInst-> get_name()].push_back( unionInst );619 forwardUnions[ unionInst->name ].push_back( unionInst ); 440 620 } // if 441 621 checkGenericParameters( unionInst ); 622 } 623 624 void LinkReferenceToTypes::previsit( QualifiedType * ) { 625 visit_children = false; 626 } 627 628 void LinkReferenceToTypes::postvisit( QualifiedType * qualType ) { 629 // linking only makes sense for the 'oldest ancestor' of the qualified type 630 qualType->parent->accept( *visitor ); 442 631 } 443 632 … … 450 639 DeclarationWithType * dwt2 = dynamic_cast<DeclarationWithType *>( d2 ); 451 640 if ( dwt1 && dwt2 ) { 452 if ( dwt1-> get_name() == dwt2->get_name()&& ResolvExpr::typesCompatible( dwt1->get_type(), dwt2->get_type(), SymTab::Indexer() ) ) {641 if ( dwt1->name == dwt2->name && ResolvExpr::typesCompatible( dwt1->get_type(), dwt2->get_type(), SymTab::Indexer() ) ) { 453 642 // std::cerr << "=========== equal:" << std::endl; 454 643 // std::cerr << "d1: " << d1 << std::endl; … … 475 664 template< typename Iterator > 476 665 void expandAssertions( TraitInstType * inst, Iterator out ) { 477 assertf( inst->baseTrait, "Trait instance not linked to base trait: %s", to String( inst ).c_str() );666 assertf( inst->baseTrait, "Trait instance not linked to base trait: %s", toCString( inst ) ); 478 667 std::list< DeclarationWithType * > asserts; 479 668 for ( Declaration * decl : inst->baseTrait->members ) { … … 512 701 SemanticError( traitInst->location, "use of undeclared trait " + traitInst->name ); 513 702 } // if 514 if ( traitDecl-> get_parameters().size() != traitInst->get_parameters().size() ) {703 if ( traitDecl->parameters.size() != traitInst->parameters.size() ) { 515 704 SemanticError( traitInst, "incorrect number of trait parameters: " ); 516 705 } // if … … 518 707 519 708 // need to carry over the 'sized' status of each decl in the instance 520 for ( auto p : group_iterate( traitDecl-> get_parameters(), traitInst->get_parameters()) ) {709 for ( auto p : group_iterate( traitDecl->parameters, traitInst->parameters ) ) { 521 710 TypeExpr * expr = dynamic_cast< TypeExpr * >( std::get<1>(p) ); 522 711 if ( ! expr ) { … … 525 714 if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( expr->get_type() ) ) { 526 715 TypeDecl * formalDecl = std::get<0>(p); 527 TypeDecl * instDecl = inst-> get_baseType();716 TypeDecl * instDecl = inst->baseType; 528 717 if ( formalDecl->get_sized() ) instDecl->set_sized( true ); 529 718 } … … 534 723 void LinkReferenceToTypes::postvisit( EnumDecl *enumDecl ) { 535 724 // visit enum members first so that the types of self-referencing members are updated properly 536 if ( ! enumDecl->get_members().empty()) {537 ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl-> get_name());725 if ( enumDecl->body ) { 726 ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl->name ); 538 727 if ( fwds != forwardEnums.end() ) { 539 728 for ( std::list< EnumInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { 540 (*inst )->set_baseEnum( enumDecl );729 (*inst)->baseEnum = enumDecl; 541 730 } // for 542 731 forwardEnums.erase( fwds ); … … 574 763 // visit struct members first so that the types of self-referencing members are updated properly 575 764 // xxx - need to ensure that type parameters match up between forward declarations and definition (most importantly, number of type parameters and their defaults) 576 if ( ! structDecl->get_members().empty()) {577 ForwardStructsType::iterator fwds = forwardStructs.find( structDecl-> get_name());765 if ( structDecl->body ) { 766 ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->name ); 578 767 if ( fwds != forwardStructs.end() ) { 579 768 for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { 580 (*inst )->set_baseStruct( structDecl );769 (*inst)->baseStruct = structDecl; 581 770 } // for 582 771 forwardStructs.erase( fwds ); … … 586 775 587 776 void LinkReferenceToTypes::postvisit( UnionDecl *unionDecl ) { 588 if ( ! unionDecl->get_members().empty()) {589 ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl-> get_name());777 if ( unionDecl->body ) { 778 ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->name ); 590 779 if ( fwds != forwardUnions.end() ) { 591 780 for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { 592 (*inst )->set_baseUnion( unionDecl );781 (*inst)->baseUnion = unionDecl; 593 782 } // for 594 783 forwardUnions.erase( fwds ); … … 600 789 // ensure generic parameter instances are renamed like the base type 601 790 if ( inGeneric && typeInst->baseType ) typeInst->name = typeInst->baseType->name; 602 if ( NamedTypeDecl *namedTypeDecl = local_indexer->lookupType( typeInst-> get_name()) ) {791 if ( NamedTypeDecl *namedTypeDecl = local_indexer->lookupType( typeInst->name ) ) { 603 792 if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) { 604 793 typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype ); … … 679 868 680 869 681 bool isTypedef( Declaration *decl ) { 682 return dynamic_cast< TypedefDecl * >( decl ); 683 } 684 685 void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) { 686 PassVisitor<EliminateTypedef> eliminator; 870 void ReplaceTypedef::replaceTypedef( std::list< Declaration * > &translationUnit ) { 871 PassVisitor<ReplaceTypedef> eliminator; 687 872 mutateAll( translationUnit, eliminator ); 688 873 if ( eliminator.pass.typedefNames.count( "size_t" ) ) { 689 874 // grab and remember declaration of size_t 690 SizeType = eliminator.pass.typedefNames["size_t"].first-> get_base()->clone();875 SizeType = eliminator.pass.typedefNames["size_t"].first->base->clone(); 691 876 } else { 692 877 // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong … … 694 879 SizeType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); 695 880 } 696 filter( translationUnit, isTypedef, true ); 697 } 698 699 Type * EliminateTypedef::postmutate( TypeInstType * typeInst ) { 881 } 882 883 void ReplaceTypedef::premutate( QualifiedType * ) { 884 visit_children = false; 885 } 886 887 Type * ReplaceTypedef::postmutate( QualifiedType * qualType ) { 888 // replacing typedefs only makes sense for the 'oldest ancestor' of the qualified type 889 qualType->parent = qualType->parent->acceptMutator( *visitor ); 890 return qualType; 891 } 892 893 Type * ReplaceTypedef::postmutate( TypeInstType * typeInst ) { 700 894 // instances of typedef types will come here. If it is an instance 701 895 // of a typdef type, link the instance to its actual type. 702 TypedefMap::const_iterator def = typedefNames.find( typeInst-> get_name());896 TypedefMap::const_iterator def = typedefNames.find( typeInst->name ); 703 897 if ( def != typedefNames.end() ) { 704 898 Type *ret = def->second.first->base->clone(); … … 717 911 SemanticError( typeInst->location, "Cannot apply type parameters to base type of " + typeInst->name ); 718 912 } 719 rtt-> get_parameters().clear();913 rtt->parameters.clear(); 720 914 cloneAll( typeInst->parameters, rtt->parameters ); 721 915 mutateAll( rtt->parameters, *visitor ); // recursively fix typedefs on parameters … … 724 918 return ret; 725 919 } else { 726 TypeDeclMap::const_iterator base = typedeclNames.find( typeInst-> get_name());920 TypeDeclMap::const_iterator base = typedeclNames.find( typeInst->name ); 727 921 assertf( base != typedeclNames.end(), "Cannot find typedecl name %s", typeInst->name.c_str() ); 728 922 typeInst->set_baseType( base->second ); … … 745 939 } 746 940 747 Declaration * EliminateTypedef::postmutate( TypedefDecl * tyDecl ) {748 if ( typedefNames.count( tyDecl-> get_name() ) == 1 && typedefNames[ tyDecl->get_name()].second == scopeLevel ) {941 Declaration * ReplaceTypedef::postmutate( TypedefDecl * tyDecl ) { 942 if ( typedefNames.count( tyDecl->name ) == 1 && typedefNames[ tyDecl->name ].second == scopeLevel ) { 749 943 // typedef to the same name from the same scope 750 944 // must be from the same type 751 945 752 Type * t1 = tyDecl-> get_base();753 Type * t2 = typedefNames[ tyDecl-> get_name() ].first->get_base();946 Type * t1 = tyDecl->base; 947 Type * t2 = typedefNames[ tyDecl->name ].first->base; 754 948 if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) { 755 949 SemanticError( tyDecl->location, "Cannot redefine typedef: " + tyDecl->name ); … … 763 957 } 764 958 } else { 765 typedefNames[ tyDecl-> get_name()] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel );959 typedefNames[ tyDecl->name ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel ); 766 960 } // if 767 961 … … 775 969 // Note, qualifiers on the typedef are superfluous for the forward declaration. 776 970 777 Type *designatorType = tyDecl-> get_base()->stripDeclarator();971 Type *designatorType = tyDecl->base->stripDeclarator(); 778 972 if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( designatorType ) ) { 779 return new StructDecl( aggDecl->get_name(), DeclarationNode::Struct, noAttributes, tyDecl->get_linkage() );973 declsToAddBefore.push_back( new StructDecl( aggDecl->name, DeclarationNode::Struct, noAttributes, tyDecl->linkage ) ); 780 974 } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( designatorType ) ) { 781 return new UnionDecl( aggDecl->get_name(), noAttributes, tyDecl->get_linkage() );975 declsToAddBefore.push_back( new UnionDecl( aggDecl->name, noAttributes, tyDecl->linkage ) ); 782 976 } else if ( EnumInstType *enumDecl = dynamic_cast< EnumInstType * >( designatorType ) ) { 783 return new EnumDecl( enumDecl->get_name(), noAttributes, tyDecl->get_linkage() ); 784 } else { 785 return tyDecl->clone(); 786 } // if 787 } 788 789 void EliminateTypedef::premutate( TypeDecl * typeDecl ) { 790 TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() ); 977 declsToAddBefore.push_back( new EnumDecl( enumDecl->name, noAttributes, tyDecl->linkage ) ); 978 } // if 979 return tyDecl->clone(); 980 } 981 982 void ReplaceTypedef::premutate( TypeDecl * typeDecl ) { 983 TypedefMap::iterator i = typedefNames.find( typeDecl->name ); 791 984 if ( i != typedefNames.end() ) { 792 985 typedefNames.erase( i ) ; 793 986 } // if 794 987 795 typedeclNames[ typeDecl-> get_name()] = typeDecl;796 } 797 798 void EliminateTypedef::premutate( FunctionDecl * ) {988 typedeclNames[ typeDecl->name ] = typeDecl; 989 } 990 991 void ReplaceTypedef::premutate( FunctionDecl * ) { 799 992 GuardScope( typedefNames ); 800 993 } 801 994 802 void EliminateTypedef::premutate( ObjectDecl * ) {995 void ReplaceTypedef::premutate( ObjectDecl * ) { 803 996 GuardScope( typedefNames ); 804 997 } 805 998 806 DeclarationWithType * EliminateTypedef::postmutate( ObjectDecl * objDecl ) {807 if ( FunctionType *funtype = dynamic_cast<FunctionType *>( objDecl-> get_type()) ) { // function type?999 DeclarationWithType * ReplaceTypedef::postmutate( ObjectDecl * objDecl ) { 1000 if ( FunctionType *funtype = dynamic_cast<FunctionType *>( objDecl->type ) ) { // function type? 808 1001 // replace the current object declaration with a function declaration 809 FunctionDecl * newDecl = new FunctionDecl( objDecl-> get_name(), objDecl->get_storageClasses(), objDecl->get_linkage(), funtype, 0, objDecl->get_attributes(), objDecl->get_funcSpec() );810 objDecl-> get_attributes().clear();1002 FunctionDecl * newDecl = new FunctionDecl( objDecl->name, objDecl->get_storageClasses(), objDecl->linkage, funtype, 0, objDecl->attributes, objDecl->get_funcSpec() ); 1003 objDecl->attributes.clear(); 811 1004 objDecl->set_type( nullptr ); 812 1005 delete objDecl; … … 816 1009 } 817 1010 818 void EliminateTypedef::premutate( CastExpr * ) {1011 void ReplaceTypedef::premutate( CastExpr * ) { 819 1012 GuardScope( typedefNames ); 820 1013 } 821 1014 822 void EliminateTypedef::premutate( CompoundStmt * ) {1015 void ReplaceTypedef::premutate( CompoundStmt * ) { 823 1016 GuardScope( typedefNames ); 824 1017 scopeLevel += 1; … … 826 1019 } 827 1020 828 CompoundStmt *EliminateTypedef::postmutate( CompoundStmt * compoundStmt ) {829 // remove and delete decl stmts830 filter( compoundStmt->kids, [](Statement * stmt) {831 if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( stmt ) ) {832 if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {833 return true;834 } // if835 } // if836 return false;837 }, true);838 return compoundStmt;839 }840 841 // there may be typedefs nested within aggregates. in order for everything to work properly, these should be removed842 // as well843 1021 template<typename AggDecl> 844 AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) { 845 filter( aggDecl->members, isTypedef, true ); 846 return aggDecl; 847 } 848 849 template<typename AggDecl> 850 void EliminateTypedef::addImplicitTypedef( AggDecl * aggDecl ) { 1022 void ReplaceTypedef::addImplicitTypedef( AggDecl * aggDecl ) { 851 1023 if ( typedefNames.count( aggDecl->get_name() ) == 0 ) { 852 1024 Type *type = nullptr; … … 860 1032 TypedefDeclPtr tyDecl( new TypedefDecl( aggDecl->get_name(), aggDecl->location, Type::StorageClasses(), type, aggDecl->get_linkage() ) ); 861 1033 typedefNames[ aggDecl->get_name() ] = std::make_pair( std::move( tyDecl ), scopeLevel ); 862 } // if 863 } 864 865 void EliminateTypedef::premutate( StructDecl * structDecl ) { 1034 // add the implicit typedef to the AST 1035 declsToAddBefore.push_back( new TypedefDecl( aggDecl->get_name(), aggDecl->location, Type::StorageClasses(), type->clone(), aggDecl->get_linkage() ) ); 1036 } // if 1037 } 1038 1039 template< typename AggDecl > 1040 void ReplaceTypedef::handleAggregate( AggDecl * aggr ) { 1041 SemanticErrorException errors; 1042 1043 ValueGuard< std::list<Declaration * > > oldBeforeDecls( declsToAddBefore ); 1044 ValueGuard< std::list<Declaration * > > oldAfterDecls ( declsToAddAfter ); 1045 declsToAddBefore.clear(); 1046 declsToAddAfter.clear(); 1047 1048 GuardScope( typedefNames ); 1049 mutateAll( aggr->parameters, *visitor ); 1050 1051 // unroll mutateAll for aggr->members so that implicit typedefs for nested types are added to the aggregate body. 1052 for ( std::list< Declaration * >::iterator i = aggr->members.begin(); i != aggr->members.end(); ++i ) { 1053 if ( !declsToAddAfter.empty() ) { aggr->members.splice( i, declsToAddAfter ); } 1054 1055 try { 1056 *i = maybeMutate( *i, *visitor ); 1057 } catch ( SemanticErrorException &e ) { 1058 errors.append( e ); 1059 } 1060 1061 if ( !declsToAddBefore.empty() ) { aggr->members.splice( i, declsToAddBefore ); } 1062 } 1063 1064 if ( !declsToAddAfter.empty() ) { aggr->members.splice( aggr->members.end(), declsToAddAfter ); } 1065 if ( !errors.isEmpty() ) { throw errors; } 1066 } 1067 1068 void ReplaceTypedef::premutate( StructDecl * structDecl ) { 1069 visit_children = false; 866 1070 addImplicitTypedef( structDecl ); 867 } 868 869 870 Declaration *EliminateTypedef::postmutate( StructDecl * structDecl ) { 871 return handleAggregate( structDecl ); 872 } 873 874 void EliminateTypedef::premutate( UnionDecl * unionDecl ) { 1071 handleAggregate( structDecl ); 1072 } 1073 1074 void ReplaceTypedef::premutate( UnionDecl * unionDecl ) { 1075 visit_children = false; 875 1076 addImplicitTypedef( unionDecl ); 876 } 877 878 Declaration *EliminateTypedef::postmutate( UnionDecl * unionDecl ) { 879 return handleAggregate( unionDecl ); 880 } 881 882 void EliminateTypedef::premutate( EnumDecl * enumDecl ) { 1077 handleAggregate( unionDecl ); 1078 } 1079 1080 void ReplaceTypedef::premutate( EnumDecl * enumDecl ) { 883 1081 addImplicitTypedef( enumDecl ); 884 1082 } 885 1083 886 Declaration *EliminateTypedef::postmutate( EnumDecl * enumDecl ) { 887 return handleAggregate( enumDecl ); 888 } 889 890 Declaration *EliminateTypedef::postmutate( TraitDecl * traitDecl ) { 891 return handleAggregate( traitDecl ); 892 } 893 894 void EliminateTypedef::premutate( FunctionType * ) { 1084 void ReplaceTypedef::premutate( FunctionType * ) { 895 1085 GuardValue( inFunctionType ); 896 1086 inFunctionType = true; … … 1024 1214 1025 1215 void ArrayLength::previsit( ObjectDecl * objDecl ) { 1026 if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl-> get_type()) ) {1216 if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->type ) ) { 1027 1217 if ( at->get_dimension() ) return; 1028 if ( ListInit * init = dynamic_cast< ListInit * >( objDecl-> get_init()) ) {1029 at->set_dimension( new ConstantExpr( Constant::from_ulong( init-> get_initializers().size() ) ) );1218 if ( ListInit * init = dynamic_cast< ListInit * >( objDecl->init ) ) { 1219 at->set_dimension( new ConstantExpr( Constant::from_ulong( init->initializers.size() ) ) ); 1030 1220 } 1031 1221 } -
src/SynTree/Declaration.h
r1d386a7 r638ac26 266 266 bool body; 267 267 std::list< Attribute * > attributes; 268 AggregateDecl * parent = nullptr; 268 269 269 270 AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ); -
src/SynTree/Mutator.h
r1d386a7 r638ac26 101 101 virtual Type * mutate( ArrayType * arrayType ) = 0; 102 102 virtual Type * mutate( ReferenceType * refType ) = 0; 103 virtual Type * mutate( QualifiedType * qualType ) = 0; 103 104 virtual Type * mutate( FunctionType * functionType ) = 0; 104 105 virtual Type * mutate( StructInstType * aggregateUseType ) = 0; … … 113 114 virtual Type * mutate( ZeroType * zeroType ) = 0; 114 115 virtual Type * mutate( OneType * oneType ) = 0; 116 virtual Type * mutate( GlobalScopeType * globalType ) = 0; 115 117 116 118 virtual Designation * mutate( Designation * designation ) = 0 ; -
src/SynTree/ReferenceToType.cc
r1d386a7 r638ac26 76 76 bool StructInstType::isComplete() const { return baseStruct ? baseStruct->has_body() : false; } 77 77 78 AggregateDecl * StructInstType::getAggr() { return baseStruct; }78 AggregateDecl * StructInstType::getAggr() const { return baseStruct; } 79 79 80 80 TypeSubstitution StructInstType::genericSubstitution() const { … … 119 119 bool UnionInstType::isComplete() const { return baseUnion ? baseUnion->has_body() : false; } 120 120 121 AggregateDecl * UnionInstType::getAggr() { return baseUnion; }121 AggregateDecl * UnionInstType::getAggr() const { return baseUnion; } 122 122 123 123 TypeSubstitution UnionInstType::genericSubstitution() const { … … 152 152 bool EnumInstType::isComplete() const { return baseEnum ? baseEnum->has_body() : false; } 153 153 154 AggregateDecl * EnumInstType::getAggr() const { return baseEnum; } 155 154 156 void EnumInstType::print( std::ostream &os, Indenter indent ) const { 155 157 using std::endl; -
src/SynTree/SynTree.h
r1d386a7 r638ac26 110 110 class ArrayType; 111 111 class ReferenceType; 112 class QualifiedType; 112 113 class FunctionType; 113 114 class ReferenceToType; … … 123 124 class ZeroType; 124 125 class OneType; 126 class GlobalScopeType; 125 127 126 128 class Designation; -
src/SynTree/Type.cc
r1d386a7 r638ac26 105 105 } 106 106 107 108 QualifiedType::QualifiedType( const Type::Qualifiers & tq, Type * parent, Type * child ) : Type( tq, {} ), parent( parent ), child( child ) { 109 } 110 111 QualifiedType::QualifiedType( const QualifiedType & other ) : Type( other ), parent( maybeClone( other.parent ) ), child( maybeClone( other.child ) ) { 112 } 113 114 QualifiedType::~QualifiedType() { 115 delete parent; 116 delete child; 117 } 118 119 void QualifiedType::print( std::ostream & os, Indenter indent ) const { 120 os << "Qualified Type: " << endl; 121 os << indent+1; 122 parent->print( os, indent+1 ); 123 os << endl << indent+1; 124 child->print( os, indent+1 ); 125 os << endl; 126 Type::print( os, indent+1 ); 127 } 128 129 GlobalScopeType::GlobalScopeType() : Type( Type::Qualifiers(), {} ) {} 130 131 void GlobalScopeType::print( std::ostream & os, Indenter ) const { 132 os << "Global Scope Type" << endl; 133 } 134 135 107 136 // Empty Variable declarations: 108 137 const Type::FuncSpecifiers noFuncSpecifiers; -
src/SynTree/Type.h
r1d386a7 r638ac26 178 178 virtual bool isComplete() const { return true; } 179 179 180 virtual AggregateDecl * getAggr() { assertf( false, "Non-aggregate type: %s", toCString( this ) ); }180 virtual AggregateDecl * getAggr() const { assertf( false, "Non-aggregate type: %s", toCString( this ) ); } 181 181 182 182 virtual TypeSubstitution genericSubstitution() const; … … 315 315 }; 316 316 317 class QualifiedType : public Type { 318 public: 319 Type * parent; 320 Type * child; 321 322 QualifiedType( const Type::Qualifiers & tq, Type * parent, Type * child ); 323 QualifiedType( const QualifiedType & tq ); 324 virtual ~QualifiedType(); 325 326 virtual QualifiedType *clone() const override { return new QualifiedType( *this ); } 327 virtual void accept( Visitor & v ) override { v.visit( this ); } 328 virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } 329 virtual void print( std::ostream & os, Indenter indent = {} ) const override; 330 }; 331 317 332 class ReferenceType : public Type { 318 333 public: … … 416 431 virtual bool isComplete() const override; 417 432 418 virtual AggregateDecl * getAggr() override;433 virtual AggregateDecl * getAggr() const override; 419 434 420 435 virtual TypeSubstitution genericSubstitution() const override; … … 453 468 virtual bool isComplete() const override; 454 469 455 virtual AggregateDecl * getAggr() override;470 virtual AggregateDecl * getAggr() const override; 456 471 457 472 virtual TypeSubstitution genericSubstitution() const override; … … 485 500 486 501 virtual bool isComplete() const override; 502 503 virtual AggregateDecl * getAggr() const; 487 504 488 505 virtual EnumInstType *clone() const override { return new EnumInstType( *this ); } … … 665 682 }; 666 683 684 class GlobalScopeType : public Type { 685 public: 686 GlobalScopeType(); 687 688 virtual GlobalScopeType *clone() const override { return new GlobalScopeType( *this ); } 689 virtual void accept( Visitor & v ) override { v.visit( this ); } 690 virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } 691 virtual void print( std::ostream & os, Indenter indent = {} ) const override; 692 }; 693 667 694 // Local Variables: // 668 695 // tab-width: 4 // -
src/SynTree/Visitor.h
r1d386a7 r638ac26 103 103 virtual void visit( ArrayType * arrayType ) = 0; 104 104 virtual void visit( ReferenceType * refType ) = 0; 105 virtual void visit( QualifiedType * qualType ) = 0; 105 106 virtual void visit( FunctionType * functionType ) = 0; 106 107 virtual void visit( StructInstType * aggregateUseType ) = 0; … … 115 116 virtual void visit( ZeroType * zeroType ) = 0; 116 117 virtual void visit( OneType * oneType ) = 0; 118 virtual void visit( GlobalScopeType * globalType ) = 0; 117 119 118 120 virtual void visit( Designation * designation ) = 0; -
src/prelude/prelude.old.cf
r1d386a7 r638ac26 728 728 forall( dtype DT ) void ?{}( volatile DT * &, DT * ); 729 729 forall( dtype DT ) void ?{}( volatile DT * &, volatile DT * ); 730 731 730 forall( dtype DT ) void ?{}( const volatile DT * &, DT * ); 732 731 forall( dtype DT ) void ?{}( const volatile DT * &, const DT * );
Note: See TracChangeset
for help on using the changeset viewer.