Changeset 90152a4 for src/SymTab/Validate.cc
- Timestamp:
- Aug 27, 2018, 4:40:34 PM (7 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- b7c89aa
- Parents:
- f9feab8 (diff), 305581d (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
-
src/SymTab/Validate.cc (modified) (44 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/SymTab/Validate.cc
rf9feab8 r90152a4 48 48 #include "CodeGen/CodeGenerator.h" // for genName 49 49 #include "CodeGen/OperatorTable.h" // for isCtorDtor, isCtorDtorAssign 50 #include "ControlStruct/Mutate.h" // for ForExprMutator 50 51 #include "Common/PassVisitor.h" // for PassVisitor, WithDeclsToAdd 51 52 #include "Common/ScopedMap.h" // for ScopedMap … … 60 61 #include "Parser/LinkageSpec.h" // for C 61 62 #include "ResolvExpr/typeops.h" // for typesCompatible 62 #include "SymTab/AddVisit.h" // for addVisit 63 #include "ResolvExpr/Resolver.h" // for findSingleExpression 64 #include "ResolvExpr/ResolveTypeof.h" // for resolveTypeof 63 65 #include "SymTab/Autogen.h" // for SizeType 64 66 #include "SynTree/Attribute.h" // for noAttributes, Attribute … … 72 74 #include "SynTree/TypeSubstitution.h" // for TypeSubstitution 73 75 #include "SynTree/Visitor.h" // for Visitor 76 #include "Validate/HandleAttributes.h" // for handleAttributes 74 77 75 78 class CompoundStmt; … … 77 80 class SwitchStmt; 78 81 79 80 #define debugPrint( x ) if ( doDebug ) { std::cout << x; } 82 #define debugPrint( x ) if ( doDebug ) x 81 83 82 84 namespace SymTab { 85 /// hoists declarations that are difficult to hoist while parsing 86 struct HoistTypeDecls final : public WithDeclsToAdd { 87 void previsit( SizeofExpr * ); 88 void previsit( AlignofExpr * ); 89 void previsit( UntypedOffsetofExpr * ); 90 void previsit( CompoundLiteralExpr * ); 91 void handleType( Type * ); 92 }; 93 94 struct FixQualifiedTypes final : public WithIndexer { 95 Type * postmutate( QualifiedType * ); 96 }; 97 83 98 struct HoistStruct final : public WithDeclsToAdd, public WithGuards { 84 99 /// Flattens nested struct types 85 100 static void hoistStruct( std::list< Declaration * > &translationUnit ); 86 101 87 void previsit( EnumInstType * enumInstType );88 void previsit( StructInstType * structInstType );89 void previsit( UnionInstType * unionInstType );90 102 void previsit( StructDecl * aggregateDecl ); 91 103 void previsit( UnionDecl * aggregateDecl ); 104 void previsit( StaticAssertDecl * assertDecl ); 105 void previsit( StructInstType * type ); 106 void previsit( UnionInstType * type ); 107 void previsit( EnumInstType * type ); 92 108 93 109 private: 94 110 template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl ); 95 111 96 bool inStruct = false;112 AggregateDecl * parentAggr = nullptr; 97 113 }; 98 114 … … 112 128 113 129 /// Associates forward declarations of aggregates with their definitions 114 struct LinkReferenceToTypes final : public WithIndexer, public WithGuards {130 struct LinkReferenceToTypes final : public WithIndexer, public WithGuards, public WithVisitorRef<LinkReferenceToTypes>, public WithShortCircuiting { 115 131 LinkReferenceToTypes( const Indexer *indexer ); 116 132 void postvisit( TypeInstType *typeInst ); … … 120 136 void postvisit( UnionInstType *unionInst ); 121 137 void postvisit( TraitInstType *traitInst ); 138 void previsit( QualifiedType * qualType ); 139 void postvisit( QualifiedType * qualType ); 122 140 123 141 void postvisit( EnumDecl *enumDecl ); … … 148 166 void previsit( ObjectDecl * object ); 149 167 void previsit( FunctionDecl * func ); 168 void previsit( FunctionType * ftype ); 150 169 void previsit( StructDecl * aggrDecl ); 151 170 void previsit( UnionDecl * aggrDecl ); … … 164 183 }; 165 184 166 struct EliminateTypedef final : public WithVisitorRef<EliminateTypedef>, public WithGuards{167 EliminateTypedef() : scopeLevel( 0 ) {}185 struct ReplaceTypedef final : public WithVisitorRef<ReplaceTypedef>, public WithGuards, public WithShortCircuiting, public WithDeclsToAdd { 186 ReplaceTypedef() : scopeLevel( 0 ) {} 168 187 /// Replaces typedefs by forward declarations 169 static void eliminateTypedef( std::list< Declaration * > &translationUnit ); 170 188 static void replaceTypedef( std::list< Declaration * > &translationUnit ); 189 190 void premutate( QualifiedType * ); 191 Type * postmutate( QualifiedType * qualType ); 171 192 Type * postmutate( TypeInstType * aggregateUseType ); 172 193 Declaration * postmutate( TypedefDecl * typeDecl ); … … 179 200 180 201 void premutate( CompoundStmt * compoundStmt ); 181 CompoundStmt * postmutate( CompoundStmt * compoundStmt );182 202 183 203 void premutate( StructDecl * structDecl ); 184 Declaration * postmutate( StructDecl * structDecl );185 204 void premutate( UnionDecl * unionDecl ); 186 Declaration * postmutate( UnionDecl * unionDecl );187 205 void premutate( EnumDecl * enumDecl ); 188 Declaration * postmutate( EnumDecl * enumDecl ); 189 Declaration * postmutate( TraitDecl * contextDecl ); 206 void premutate( TraitDecl * ); 190 207 191 208 void premutate( FunctionType * ftype ); … … 193 210 private: 194 211 template<typename AggDecl> 195 AggDecl *handleAggregate( AggDecl * aggDecl );196 197 template<typename AggDecl>198 212 void addImplicitTypedef( AggDecl * aggDecl ); 213 template< typename AggDecl > 214 void handleAggregate( AggDecl * aggr ); 199 215 200 216 typedef std::unique_ptr<TypedefDecl> TypedefDeclPtr; 201 217 typedef ScopedMap< std::string, std::pair< TypedefDeclPtr, int > > TypedefMap; 202 typedef std::map< std::string, TypeDecl * > TypeDeclMap;218 typedef ScopedMap< std::string, TypeDecl * > TypeDeclMap; 203 219 TypedefMap typedefNames; 204 220 TypeDeclMap typedeclNames; 205 221 int scopeLevel; 206 222 bool inFunctionType = false; 223 }; 224 225 struct EliminateTypedef { 226 /// removes TypedefDecls from the AST 227 static void eliminateTypedef( std::list< Declaration * > &translationUnit ); 228 229 template<typename AggDecl> 230 void handleAggregate( AggDecl *aggregateDecl ); 231 232 void previsit( StructDecl * aggregateDecl ); 233 void previsit( UnionDecl * aggregateDecl ); 234 void previsit( CompoundStmt * compoundStmt ); 207 235 }; 208 236 … … 222 250 }; 223 251 224 struct ArrayLength { 252 struct FixObjectType : public WithIndexer { 253 /// resolves typeof type in object, function, and type declarations 254 static void fix( std::list< Declaration * > & translationUnit ); 255 256 void previsit( ObjectDecl * ); 257 void previsit( FunctionDecl * ); 258 void previsit( TypeDecl * ); 259 }; 260 261 struct ArrayLength : public WithIndexer { 225 262 /// for array types without an explicit length, compute the length and store it so that it 226 263 /// is known to the rest of the phases. For example, … … 233 270 234 271 void previsit( ObjectDecl * objDecl ); 272 void previsit( ArrayType * arrayType ); 235 273 }; 236 274 … … 262 300 PassVisitor<FindSpecialDeclarations> finder; 263 301 PassVisitor<LabelAddressFixer> labelAddrFixer; 264 265 EliminateTypedef::eliminateTypedef( translationUnit ); 266 HoistStruct::hoistStruct( translationUnit ); // must happen after EliminateTypedef, so that aggregate typedefs occur in the correct order 302 PassVisitor<HoistTypeDecls> hoistDecls; 303 PassVisitor<FixQualifiedTypes> fixQual; 304 305 acceptAll( translationUnit, hoistDecls ); 306 ReplaceTypedef::replaceTypedef( translationUnit ); 267 307 ReturnTypeFixer::fix( translationUnit ); // must happen before autogen 268 308 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 269 309 acceptAll( translationUnit, lrt ); // must happen before autogen, because sized flag needs to propagate to generated functions 310 mutateAll( translationUnit, fixQual ); // must happen after LinkReferenceToTypes, because aggregate members are accessed 311 HoistStruct::hoistStruct( translationUnit ); // must happen after EliminateTypedef, so that aggregate typedefs occur in the correct order 312 EliminateTypedef::eliminateTypedef( translationUnit ); // 270 313 acceptAll( translationUnit, genericParams ); // check as early as possible - can't happen before LinkReferenceToTypes 271 314 VerifyCtorDtorAssign::verify( translationUnit ); // must happen before autogen, because autogen examines existing ctor/dtors … … 274 317 Concurrency::applyKeywords( translationUnit ); 275 318 acceptAll( translationUnit, fpd ); // must happen before autogenerateRoutines, after Concurrency::applyKeywords because uniqueIds must be set on declaration before resolution 319 ControlStruct::hoistControlDecls( translationUnit ); // hoist initialization out of for statements; must happen before autogenerateRoutines 276 320 autogenerateRoutines( translationUnit ); // moved up, used to be below compoundLiteral - currently needs EnumAndPointerDecay 277 321 Concurrency::implementMutexFuncs( translationUnit ); 278 322 Concurrency::implementThreadStarter( translationUnit ); 279 323 mutateAll( translationUnit, compoundliteral ); 324 ResolvExpr::resolveWithExprs( translationUnit ); // must happen before FixObjectType because user-code is resolved and may contain with variables 325 FixObjectType::fix( translationUnit ); 280 326 ArrayLength::computeLength( translationUnit ); 281 327 acceptAll( translationUnit, finder ); // xxx - remove this pass soon 282 328 mutateAll( translationUnit, labelAddrFixer ); 329 Validate::handleAttributes( translationUnit ); 283 330 } 284 331 … … 292 339 } 293 340 341 342 void HoistTypeDecls::handleType( Type * type ) { 343 // some type declarations are buried in expressions and not easy to hoist during parsing; hoist them here 344 AggregateDecl * aggr = nullptr; 345 if ( StructInstType * inst = dynamic_cast< StructInstType * >( type ) ) { 346 aggr = inst->baseStruct; 347 } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( type ) ) { 348 aggr = inst->baseUnion; 349 } else if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( type ) ) { 350 aggr = inst->baseEnum; 351 } 352 if ( aggr && aggr->body ) { 353 declsToAddBefore.push_front( aggr ); 354 } 355 } 356 357 void HoistTypeDecls::previsit( SizeofExpr * expr ) { 358 handleType( expr->type ); 359 } 360 361 void HoistTypeDecls::previsit( AlignofExpr * expr ) { 362 handleType( expr->type ); 363 } 364 365 void HoistTypeDecls::previsit( UntypedOffsetofExpr * expr ) { 366 handleType( expr->type ); 367 } 368 369 void HoistTypeDecls::previsit( CompoundLiteralExpr * expr ) { 370 handleType( expr->result ); 371 } 372 373 374 Type * FixQualifiedTypes::postmutate( QualifiedType * qualType ) { 375 Type * parent = qualType->parent; 376 Type * child = qualType->child; 377 if ( dynamic_cast< GlobalScopeType * >( qualType->parent ) ) { 378 // .T => lookup T at global scope 379 if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( child ) ) { 380 auto td = indexer.globalLookupType( inst->name ); 381 if ( ! td ) { 382 SemanticError( qualType->location, toString("Use of undefined global type ", inst->name) ); 383 } 384 auto base = td->base; 385 assert( base ); 386 Type * ret = base->clone(); 387 ret->get_qualifiers() = qualType->get_qualifiers(); 388 return ret; 389 } else { 390 // .T => T is not a type name 391 assertf( false, "unhandled global qualified child type: %s", toCString(child) ); 392 } 393 } else { 394 // S.T => S must be an aggregate type, find the declaration for T in S. 395 AggregateDecl * aggr = nullptr; 396 if ( StructInstType * inst = dynamic_cast< StructInstType * >( parent ) ) { 397 aggr = inst->baseStruct; 398 } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * > ( parent ) ) { 399 aggr = inst->baseUnion; 400 } else { 401 SemanticError( qualType->location, toString("Qualified type requires an aggregate on the left, but has: ", parent) ); 402 } 403 assert( aggr ); // TODO: need to handle forward declarations 404 for ( Declaration * member : aggr->members ) { 405 if ( StructInstType * inst = dynamic_cast< StructInstType * >( child ) ) { 406 if ( StructDecl * aggr = dynamic_cast< StructDecl * >( member ) ) { 407 if ( aggr->name == inst->name ) { 408 // TODO: is this case, and other non-TypeInstType cases, necessary? 409 return new StructInstType( qualType->get_qualifiers(), aggr ); 410 } 411 } 412 } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( child ) ) { 413 if ( UnionDecl * aggr = dynamic_cast< UnionDecl * > ( member ) ) { 414 if ( aggr->name == inst->name ) { 415 return new UnionInstType( qualType->get_qualifiers(), aggr ); 416 } 417 } 418 } else if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( child ) ) { 419 if ( EnumDecl * aggr = dynamic_cast< EnumDecl * > ( member ) ) { 420 if ( aggr->name == inst->name ) { 421 return new EnumInstType( qualType->get_qualifiers(), aggr ); 422 } 423 } 424 } else if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( child ) ) { 425 // name on the right is a typedef 426 if ( NamedTypeDecl * aggr = dynamic_cast< NamedTypeDecl * > ( member ) ) { 427 if ( aggr->name == inst->name ) { 428 assert( aggr->base ); 429 Type * ret = aggr->base->clone(); 430 ret->get_qualifiers() = qualType->get_qualifiers(); 431 return ret; 432 } 433 } 434 } else { 435 // S.T - S is not an aggregate => error 436 assertf( false, "unhandled qualified child type: %s", toCString(qualType) ); 437 } 438 } 439 // failed to find a satisfying definition of type 440 SemanticError( qualType->location, toString("Undefined type in qualified type: ", qualType) ); 441 } 442 443 // ... may want to link canonical SUE definition to each forward decl so that it becomes easier to lookup? 444 } 445 446 294 447 void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) { 295 448 PassVisitor<HoistStruct> hoister; … … 297 450 } 298 451 299 bool isStructOrUnion( Declaration *decl ) { 300 return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl ); 452 bool shouldHoist( Declaration *decl ) { 453 return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl ) || dynamic_cast< StaticAssertDecl * >( decl ); 454 } 455 456 namespace { 457 void qualifiedName( AggregateDecl * aggr, std::ostringstream & ss ) { 458 if ( aggr->parent ) qualifiedName( aggr->parent, ss ); 459 ss << "__" << aggr->name; 460 } 461 462 // mangle nested type names using entire parent chain 463 std::string qualifiedName( AggregateDecl * aggr ) { 464 std::ostringstream ss; 465 qualifiedName( aggr, ss ); 466 return ss.str(); 467 } 301 468 } 302 469 303 470 template< typename AggDecl > 304 471 void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) { 305 if ( inStruct ) { 472 if ( parentAggr ) { 473 aggregateDecl->parent = parentAggr; 474 aggregateDecl->name = qualifiedName( aggregateDecl ); 306 475 // Add elements in stack order corresponding to nesting structure. 307 476 declsToAddBefore.push_front( aggregateDecl ); 308 477 } else { 309 GuardValue( inStruct);310 inStruct = true;478 GuardValue( parentAggr ); 479 parentAggr = aggregateDecl; 311 480 } // if 312 481 // Always remove the hoisted aggregate from the inner structure. 313 GuardAction( [aggregateDecl]() { filter( aggregateDecl->members, isStructOrUnion, false ); } ); 314 } 315 316 void HoistStruct::previsit( EnumInstType * inst ) { 317 if ( inst->baseEnum ) { 318 declsToAddBefore.push_front( inst->baseEnum ); 319 } 320 } 321 322 void HoistStruct::previsit( StructInstType * inst ) { 323 if ( inst->baseStruct ) { 324 declsToAddBefore.push_front( inst->baseStruct ); 325 } 326 } 327 328 void HoistStruct::previsit( UnionInstType * inst ) { 329 if ( inst->baseUnion ) { 330 declsToAddBefore.push_front( inst->baseUnion ); 482 GuardAction( [aggregateDecl]() { filter( aggregateDecl->members, shouldHoist, false ); } ); 483 } 484 485 void HoistStruct::previsit( StaticAssertDecl * assertDecl ) { 486 if ( parentAggr ) { 487 declsToAddBefore.push_back( assertDecl ); 331 488 } 332 489 } … … 340 497 } 341 498 499 void HoistStruct::previsit( StructInstType * type ) { 500 // need to reset type name after expanding to qualified name 501 assert( type->baseStruct ); 502 type->name = type->baseStruct->name; 503 } 504 505 void HoistStruct::previsit( UnionInstType * type ) { 506 assert( type->baseUnion ); 507 type->name = type->baseUnion->name; 508 } 509 510 void HoistStruct::previsit( EnumInstType * type ) { 511 assert( type->baseEnum ); 512 type->name = type->baseEnum->name; 513 } 514 515 516 bool isTypedef( Declaration *decl ) { 517 return dynamic_cast< TypedefDecl * >( decl ); 518 } 519 520 void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) { 521 PassVisitor<EliminateTypedef> eliminator; 522 acceptAll( translationUnit, eliminator ); 523 filter( translationUnit, isTypedef, true ); 524 } 525 526 template< typename AggDecl > 527 void EliminateTypedef::handleAggregate( AggDecl *aggregateDecl ) { 528 filter( aggregateDecl->members, isTypedef, true ); 529 } 530 531 void EliminateTypedef::previsit( StructDecl * aggregateDecl ) { 532 handleAggregate( aggregateDecl ); 533 } 534 535 void EliminateTypedef::previsit( UnionDecl * aggregateDecl ) { 536 handleAggregate( aggregateDecl ); 537 } 538 539 void EliminateTypedef::previsit( CompoundStmt * compoundStmt ) { 540 // remove and delete decl stmts 541 filter( compoundStmt->kids, [](Statement * stmt) { 542 if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( stmt ) ) { 543 if ( dynamic_cast< TypedefDecl * >( declStmt->decl ) ) { 544 return true; 545 } // if 546 } // if 547 return false; 548 }, true); 549 } 550 342 551 void EnumAndPointerDecay::previsit( EnumDecl *enumDecl ) { 343 552 // Set the type of each member of the enumeration to be EnumConstant 344 for ( std::list< Declaration * >::iterator i = enumDecl-> get_members().begin(); i != enumDecl->get_members().end(); ++i ) {553 for ( std::list< Declaration * >::iterator i = enumDecl->members.begin(); i != enumDecl->members.end(); ++i ) { 345 554 ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i ); 346 555 assert( obj ); 347 obj->set_type( new EnumInstType( Type::Qualifiers( Type::Const ), enumDecl-> get_name()) );556 obj->set_type( new EnumInstType( Type::Qualifiers( Type::Const ), enumDecl->name ) ); 348 557 } // for 349 558 } … … 351 560 namespace { 352 561 template< typename DWTList > 353 void fixFunctionList( DWTList & dwts, FunctionType * func ) { 354 // the only case in which "void" is valid is where it is the only one in the list; then it should be removed 355 // entirely. other fix ups are handled by the FixFunction class 356 typedef typename DWTList::iterator DWTIterator; 357 DWTIterator begin( dwts.begin() ), end( dwts.end() ); 358 if ( begin == end ) return; 359 PassVisitor<FixFunction> fixer; 360 DWTIterator i = begin; 361 *i = (*i)->acceptMutator( fixer ); 362 if ( fixer.pass.isVoid ) { 363 DWTIterator j = i; 364 ++i; 365 delete *j; 366 dwts.erase( j ); 367 if ( i != end ) { 368 throw SemanticError( "invalid type void in function type ", func ); 369 } // if 370 } else { 371 ++i; 372 for ( ; i != end; ++i ) { 373 PassVisitor<FixFunction> fixer; 374 *i = (*i)->acceptMutator( fixer ); 375 if ( fixer.pass.isVoid ) { 376 throw SemanticError( "invalid type void in function type ", func ); 377 } // if 378 } // for 379 } // if 562 void fixFunctionList( DWTList & dwts, bool isVarArgs, FunctionType * func ) { 563 auto nvals = dwts.size(); 564 bool containsVoid = false; 565 for ( auto & dwt : dwts ) { 566 // fix each DWT and record whether a void was found 567 containsVoid |= fixFunction( dwt ); 568 } 569 570 // the only case in which "void" is valid is where it is the only one in the list 571 if ( containsVoid && ( nvals > 1 || isVarArgs ) ) { 572 SemanticError( func, "invalid type void in function type " ); 573 } 574 575 // one void is the only thing in the list; remove it. 576 if ( containsVoid ) { 577 delete dwts.front(); 578 dwts.clear(); 579 } 380 580 } 381 581 } … … 383 583 void EnumAndPointerDecay::previsit( FunctionType *func ) { 384 584 // Fix up parameters and return types 385 fixFunctionList( func-> get_parameters(), func );386 fixFunctionList( func-> get_returnVals(), func );585 fixFunctionList( func->parameters, func->isVarArgs, func ); 586 fixFunctionList( func->returnVals, false, func ); 387 587 } 388 588 … … 396 596 397 597 void LinkReferenceToTypes::postvisit( EnumInstType *enumInst ) { 398 EnumDecl *st = local_indexer->lookupEnum( enumInst-> get_name());598 EnumDecl *st = local_indexer->lookupEnum( enumInst->name ); 399 599 // it's not a semantic error if the enum is not found, just an implicit forward declaration 400 600 if ( st ) { 401 //assert( ! enumInst->get_baseEnum() || enumInst->get_baseEnum()->get_members().empty() || ! st->get_members().empty() ); 402 enumInst->set_baseEnum( st ); 403 } // if 404 if ( ! st || st->get_members().empty() ) { 601 enumInst->baseEnum = st; 602 } // if 603 if ( ! st || ! st->body ) { 405 604 // use of forward declaration 406 forwardEnums[ enumInst-> get_name()].push_back( enumInst );605 forwardEnums[ enumInst->name ].push_back( enumInst ); 407 606 } // if 408 607 } … … 411 610 for ( Expression * param : inst->parameters ) { 412 611 if ( ! dynamic_cast< TypeExpr * >( param ) ) { 413 throw SemanticError( "Expression parameters for generic types are currently unsupported: ", inst);612 SemanticError( inst, "Expression parameters for generic types are currently unsupported: " ); 414 613 } 415 614 } … … 417 616 418 617 void LinkReferenceToTypes::postvisit( StructInstType *structInst ) { 419 StructDecl *st = local_indexer->lookupStruct( structInst-> get_name());618 StructDecl *st = local_indexer->lookupStruct( structInst->name ); 420 619 // it's not a semantic error if the struct is not found, just an implicit forward declaration 421 620 if ( st ) { 422 //assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() ); 423 structInst->set_baseStruct( st ); 424 } // if 425 if ( ! st || st->get_members().empty() ) { 621 structInst->baseStruct = st; 622 } // if 623 if ( ! st || ! st->body ) { 426 624 // use of forward declaration 427 forwardStructs[ structInst-> get_name()].push_back( structInst );625 forwardStructs[ structInst->name ].push_back( structInst ); 428 626 } // if 429 627 checkGenericParameters( structInst ); … … 431 629 432 630 void LinkReferenceToTypes::postvisit( UnionInstType *unionInst ) { 433 UnionDecl *un = local_indexer->lookupUnion( unionInst-> get_name());631 UnionDecl *un = local_indexer->lookupUnion( unionInst->name ); 434 632 // it's not a semantic error if the union is not found, just an implicit forward declaration 435 633 if ( un ) { 436 unionInst-> set_baseUnion( un );437 } // if 438 if ( ! un || un->get_members().empty()) {634 unionInst->baseUnion = un; 635 } // if 636 if ( ! un || ! un->body ) { 439 637 // use of forward declaration 440 forwardUnions[ unionInst-> get_name()].push_back( unionInst );638 forwardUnions[ unionInst->name ].push_back( unionInst ); 441 639 } // if 442 640 checkGenericParameters( unionInst ); 641 } 642 643 void LinkReferenceToTypes::previsit( QualifiedType * ) { 644 visit_children = false; 645 } 646 647 void LinkReferenceToTypes::postvisit( QualifiedType * qualType ) { 648 // linking only makes sense for the 'oldest ancestor' of the qualified type 649 qualType->parent->accept( *visitor ); 443 650 } 444 651 … … 451 658 DeclarationWithType * dwt2 = dynamic_cast<DeclarationWithType *>( d2 ); 452 659 if ( dwt1 && dwt2 ) { 453 if ( dwt1-> get_name() == dwt2->get_name()&& ResolvExpr::typesCompatible( dwt1->get_type(), dwt2->get_type(), SymTab::Indexer() ) ) {660 if ( dwt1->name == dwt2->name && ResolvExpr::typesCompatible( dwt1->get_type(), dwt2->get_type(), SymTab::Indexer() ) ) { 454 661 // std::cerr << "=========== equal:" << std::endl; 455 662 // std::cerr << "d1: " << d1 << std::endl; … … 476 683 template< typename Iterator > 477 684 void expandAssertions( TraitInstType * inst, Iterator out ) { 478 assertf( inst->baseTrait, "Trait instance not linked to base trait: %s", to String( inst ).c_str() );685 assertf( inst->baseTrait, "Trait instance not linked to base trait: %s", toCString( inst ) ); 479 686 std::list< DeclarationWithType * > asserts; 480 687 for ( Declaration * decl : inst->baseTrait->members ) { … … 511 718 TraitDecl *traitDecl = local_indexer->lookupTrait( traitInst->name ); 512 719 if ( ! traitDecl ) { 513 throw SemanticError("use of undeclared trait " + traitInst->name );514 } // if 515 if ( traitDecl-> get_parameters().size() != traitInst->get_parameters().size() ) {516 throw SemanticError( "incorrect number of trait parameters: ", traitInst);720 SemanticError( traitInst->location, "use of undeclared trait " + traitInst->name ); 721 } // if 722 if ( traitDecl->parameters.size() != traitInst->parameters.size() ) { 723 SemanticError( traitInst, "incorrect number of trait parameters: " ); 517 724 } // if 518 725 traitInst->baseTrait = traitDecl; 519 726 520 727 // need to carry over the 'sized' status of each decl in the instance 521 for ( auto p : group_iterate( traitDecl-> get_parameters(), traitInst->get_parameters()) ) {728 for ( auto p : group_iterate( traitDecl->parameters, traitInst->parameters ) ) { 522 729 TypeExpr * expr = dynamic_cast< TypeExpr * >( std::get<1>(p) ); 523 730 if ( ! expr ) { 524 throw SemanticError( "Expression parameters for trait instances are currently unsupported: ", std::get<1>(p));731 SemanticError( std::get<1>(p), "Expression parameters for trait instances are currently unsupported: " ); 525 732 } 526 733 if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( expr->get_type() ) ) { 527 734 TypeDecl * formalDecl = std::get<0>(p); 528 TypeDecl * instDecl = inst-> get_baseType();735 TypeDecl * instDecl = inst->baseType; 529 736 if ( formalDecl->get_sized() ) instDecl->set_sized( true ); 530 737 } … … 535 742 void LinkReferenceToTypes::postvisit( EnumDecl *enumDecl ) { 536 743 // visit enum members first so that the types of self-referencing members are updated properly 537 if ( ! enumDecl->get_members().empty()) {538 ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl-> get_name());744 if ( enumDecl->body ) { 745 ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl->name ); 539 746 if ( fwds != forwardEnums.end() ) { 540 747 for ( std::list< EnumInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { 541 (*inst )->set_baseEnum( enumDecl );748 (*inst)->baseEnum = enumDecl; 542 749 } // for 543 750 forwardEnums.erase( fwds ); 544 751 } // if 752 753 for ( Declaration * member : enumDecl->members ) { 754 ObjectDecl * field = strict_dynamic_cast<ObjectDecl *>( member ); 755 if ( field->init ) { 756 // need to resolve enumerator initializers early so that other passes that determine if an expression is constexpr have the appropriate information. 757 SingleInit * init = strict_dynamic_cast<SingleInit *>( field->init ); 758 ResolvExpr::findSingleExpression( init->value, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), indexer ); 759 } 760 } 545 761 } // if 546 762 } … … 575 791 // visit struct members first so that the types of self-referencing members are updated properly 576 792 // xxx - need to ensure that type parameters match up between forward declarations and definition (most importantly, number of type parameters and their defaults) 577 if ( ! structDecl->get_members().empty()) {578 ForwardStructsType::iterator fwds = forwardStructs.find( structDecl-> get_name());793 if ( structDecl->body ) { 794 ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->name ); 579 795 if ( fwds != forwardStructs.end() ) { 580 796 for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { 581 (*inst )->set_baseStruct( structDecl );797 (*inst)->baseStruct = structDecl; 582 798 } // for 583 799 forwardStructs.erase( fwds ); … … 587 803 588 804 void LinkReferenceToTypes::postvisit( UnionDecl *unionDecl ) { 589 if ( ! unionDecl->get_members().empty()) {590 ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl-> get_name());805 if ( unionDecl->body ) { 806 ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->name ); 591 807 if ( fwds != forwardUnions.end() ) { 592 808 for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { 593 (*inst )->set_baseUnion( unionDecl );809 (*inst)->baseUnion = unionDecl; 594 810 } // for 595 811 forwardUnions.erase( fwds ); … … 601 817 // ensure generic parameter instances are renamed like the base type 602 818 if ( inGeneric && typeInst->baseType ) typeInst->name = typeInst->baseType->name; 603 if ( NamedTypeDecl *namedTypeDecl = local_indexer->lookupType( typeInst-> get_name()) ) {819 if ( NamedTypeDecl *namedTypeDecl = local_indexer->lookupType( typeInst->name ) ) { 604 820 if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) { 605 821 typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype ); … … 626 842 // apply FixFunction to every assertion to check for invalid void type 627 843 for ( DeclarationWithType *& assertion : type->assertions ) { 628 PassVisitor<FixFunction> fixer; 629 assertion = assertion->acceptMutator( fixer ); 630 if ( fixer.pass.isVoid ) { 631 throw SemanticError( "invalid type void in assertion of function ", node ); 844 bool isVoid = fixFunction( assertion ); 845 if ( isVoid ) { 846 SemanticError( node, "invalid type void in assertion of function " ); 632 847 } // if 633 848 } // for … … 637 852 638 853 void ForallPointerDecay::previsit( ObjectDecl *object ) { 639 forallFixer( object->type->forall, object );640 if ( PointerType *pointer = dynamic_cast< PointerType * >( object->type) ) {641 forallFixer( pointer->base->forall, object);642 } // if854 // ensure that operator names only apply to functions or function pointers 855 if ( CodeGen::isOperator( object->name ) && ! dynamic_cast< FunctionType * >( object->type->stripDeclarator() ) ) { 856 SemanticError( object->location, toCString( "operator ", object->name.c_str(), " is not a function or function pointer." ) ); 857 } 643 858 object->fixUniqueId(); 644 859 } 645 860 646 861 void ForallPointerDecay::previsit( FunctionDecl *func ) { 647 forallFixer( func->type->forall, func );648 862 func->fixUniqueId(); 863 } 864 865 void ForallPointerDecay::previsit( FunctionType * ftype ) { 866 forallFixer( ftype->forall, ftype ); 649 867 } 650 868 … … 673 891 // were cast to void. 674 892 if ( ! returnStmt->get_expr() && returnVals.size() != 0 ) { 675 throw SemanticError( "Non-void function returns no values: " , returnStmt ); 676 } 677 } 678 679 680 bool isTypedef( Declaration *decl ) { 681 return dynamic_cast< TypedefDecl * >( decl ); 682 } 683 684 void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) { 685 PassVisitor<EliminateTypedef> eliminator; 893 SemanticError( returnStmt, "Non-void function returns no values: " ); 894 } 895 } 896 897 898 void ReplaceTypedef::replaceTypedef( std::list< Declaration * > &translationUnit ) { 899 PassVisitor<ReplaceTypedef> eliminator; 686 900 mutateAll( translationUnit, eliminator ); 687 901 if ( eliminator.pass.typedefNames.count( "size_t" ) ) { 688 902 // grab and remember declaration of size_t 689 SizeType = eliminator.pass.typedefNames["size_t"].first-> get_base()->clone();903 SizeType = eliminator.pass.typedefNames["size_t"].first->base->clone(); 690 904 } else { 691 905 // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong … … 693 907 SizeType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); 694 908 } 695 filter( translationUnit, isTypedef, true ); 696 } 697 698 Type * EliminateTypedef::postmutate( TypeInstType * typeInst ) { 909 } 910 911 void ReplaceTypedef::premutate( QualifiedType * ) { 912 visit_children = false; 913 } 914 915 Type * ReplaceTypedef::postmutate( QualifiedType * qualType ) { 916 // replacing typedefs only makes sense for the 'oldest ancestor' of the qualified type 917 qualType->parent = qualType->parent->acceptMutator( *visitor ); 918 return qualType; 919 } 920 921 Type * ReplaceTypedef::postmutate( TypeInstType * typeInst ) { 699 922 // instances of typedef types will come here. If it is an instance 700 923 // of a typdef type, link the instance to its actual type. 701 TypedefMap::const_iterator def = typedefNames.find( typeInst-> get_name());924 TypedefMap::const_iterator def = typedefNames.find( typeInst->name ); 702 925 if ( def != typedefNames.end() ) { 703 926 Type *ret = def->second.first->base->clone(); 927 ret->location = typeInst->location; 704 928 ret->get_qualifiers() |= typeInst->get_qualifiers(); 705 929 // attributes are not carried over from typedef to function parameters/return values … … 714 938 ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret); 715 939 if ( ! rtt ) { 716 throw SemanticError("Cannot apply type parameters to base type of " + typeInst->name);940 SemanticError( typeInst->location, "Cannot apply type parameters to base type of " + typeInst->name ); 717 941 } 718 rtt-> get_parameters().clear();942 rtt->parameters.clear(); 719 943 cloneAll( typeInst->parameters, rtt->parameters ); 720 944 mutateAll( rtt->parameters, *visitor ); // recursively fix typedefs on parameters … … 723 947 return ret; 724 948 } else { 725 TypeDeclMap::const_iterator base = typedeclNames.find( typeInst->get_name() ); 726 assertf( base != typedeclNames.end(), "Cannot find typedecl name %s", typeInst->name.c_str() ); 949 TypeDeclMap::const_iterator base = typedeclNames.find( typeInst->name ); 950 if ( base == typedeclNames.end() ) { 951 SemanticError( typeInst->location, toString("Use of undefined type ", typeInst->name) ); 952 } 727 953 typeInst->set_baseType( base->second ); 728 } // if 729 return typeInst; 954 return typeInst; 955 } // if 956 assert( false ); 730 957 } 731 958 … … 744 971 } 745 972 746 Declaration * EliminateTypedef::postmutate( TypedefDecl * tyDecl ) {747 if ( typedefNames.count( tyDecl-> get_name() ) == 1 && typedefNames[ tyDecl->get_name()].second == scopeLevel ) {973 Declaration * ReplaceTypedef::postmutate( TypedefDecl * tyDecl ) { 974 if ( typedefNames.count( tyDecl->name ) == 1 && typedefNames[ tyDecl->name ].second == scopeLevel ) { 748 975 // typedef to the same name from the same scope 749 976 // must be from the same type 750 977 751 Type * t1 = tyDecl-> get_base();752 Type * t2 = typedefNames[ tyDecl-> get_name() ].first->get_base();978 Type * t1 = tyDecl->base; 979 Type * t2 = typedefNames[ tyDecl->name ].first->base; 753 980 if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) { 754 throw SemanticError( "Cannot redefine typedef: " + tyDecl->name ); 755 } 756 // cannot redefine VLA typedefs 981 SemanticError( tyDecl->location, "Cannot redefine typedef: " + tyDecl->name ); 982 } 983 // Cannot redefine VLA typedefs. Note: this is slightly incorrect, because our notion of VLAs 984 // at this point in the translator is imprecise. In particular, this will disallow redefining typedefs 985 // with arrays whose dimension is an enumerator or a cast of a constant/enumerator. The effort required 986 // to fix this corner case likely outweighs the utility of allowing it. 757 987 if ( isVariableLength( t1 ) || isVariableLength( t2 ) ) { 758 throw SemanticError("Cannot redefine typedef: " + tyDecl->name );988 SemanticError( tyDecl->location, "Cannot redefine typedef: " + tyDecl->name ); 759 989 } 760 990 } else { 761 typedefNames[ tyDecl-> get_name()] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel );991 typedefNames[ tyDecl->name ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel ); 762 992 } // if 763 993 … … 771 1001 // Note, qualifiers on the typedef are superfluous for the forward declaration. 772 1002 773 Type *designatorType = tyDecl-> get_base()->stripDeclarator();1003 Type *designatorType = tyDecl->base->stripDeclarator(); 774 1004 if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( designatorType ) ) { 775 return new StructDecl( aggDecl->get_name(), DeclarationNode::Struct, noAttributes, tyDecl->get_linkage() );1005 declsToAddBefore.push_back( new StructDecl( aggDecl->name, DeclarationNode::Struct, noAttributes, tyDecl->linkage ) ); 776 1006 } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( designatorType ) ) { 777 return new UnionDecl( aggDecl->get_name(), noAttributes, tyDecl->get_linkage() );1007 declsToAddBefore.push_back( new UnionDecl( aggDecl->name, noAttributes, tyDecl->linkage ) ); 778 1008 } else if ( EnumInstType *enumDecl = dynamic_cast< EnumInstType * >( designatorType ) ) { 779 return new EnumDecl( enumDecl->get_name(), noAttributes, tyDecl->get_linkage() ); 780 } else { 781 return tyDecl->clone(); 782 } // if 783 } 784 785 void EliminateTypedef::premutate( TypeDecl * typeDecl ) { 786 TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() ); 1009 declsToAddBefore.push_back( new EnumDecl( enumDecl->name, noAttributes, tyDecl->linkage ) ); 1010 } // if 1011 return tyDecl->clone(); 1012 } 1013 1014 void ReplaceTypedef::premutate( TypeDecl * typeDecl ) { 1015 TypedefMap::iterator i = typedefNames.find( typeDecl->name ); 787 1016 if ( i != typedefNames.end() ) { 788 1017 typedefNames.erase( i ) ; 789 1018 } // if 790 1019 791 typedeclNames [ typeDecl->get_name() ] = typeDecl;792 } 793 794 void EliminateTypedef::premutate( FunctionDecl * ) {1020 typedeclNames.insert( typeDecl->name, typeDecl ); 1021 } 1022 1023 void ReplaceTypedef::premutate( FunctionDecl * ) { 795 1024 GuardScope( typedefNames ); 796 } 797 798 void EliminateTypedef::premutate( ObjectDecl * ) { 1025 GuardScope( typedeclNames ); 1026 } 1027 1028 void ReplaceTypedef::premutate( ObjectDecl * ) { 799 1029 GuardScope( typedefNames ); 800 } 801 802 DeclarationWithType *EliminateTypedef::postmutate( ObjectDecl * objDecl ) { 803 if ( FunctionType *funtype = dynamic_cast<FunctionType *>( objDecl->get_type() ) ) { // function type? 1030 GuardScope( typedeclNames ); 1031 } 1032 1033 DeclarationWithType * ReplaceTypedef::postmutate( ObjectDecl * objDecl ) { 1034 if ( FunctionType *funtype = dynamic_cast<FunctionType *>( objDecl->type ) ) { // function type? 804 1035 // replace the current object declaration with a function declaration 805 FunctionDecl * newDecl = new FunctionDecl( objDecl-> get_name(), objDecl->get_storageClasses(), objDecl->get_linkage(), funtype, 0, objDecl->get_attributes(), objDecl->get_funcSpec() );806 objDecl-> get_attributes().clear();1036 FunctionDecl * newDecl = new FunctionDecl( objDecl->name, objDecl->get_storageClasses(), objDecl->linkage, funtype, 0, objDecl->attributes, objDecl->get_funcSpec() ); 1037 objDecl->attributes.clear(); 807 1038 objDecl->set_type( nullptr ); 808 1039 delete objDecl; … … 812 1043 } 813 1044 814 void EliminateTypedef::premutate( CastExpr * ) {1045 void ReplaceTypedef::premutate( CastExpr * ) { 815 1046 GuardScope( typedefNames ); 816 } 817 818 void EliminateTypedef::premutate( CompoundStmt * ) { 1047 GuardScope( typedeclNames ); 1048 } 1049 1050 void ReplaceTypedef::premutate( CompoundStmt * ) { 819 1051 GuardScope( typedefNames ); 1052 GuardScope( typedeclNames ); 820 1053 scopeLevel += 1; 821 1054 GuardAction( [this](){ scopeLevel -= 1; } ); 822 1055 } 823 1056 824 CompoundStmt *EliminateTypedef::postmutate( CompoundStmt * compoundStmt ) {825 // remove and delete decl stmts826 filter( compoundStmt->kids, [](Statement * stmt) {827 if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( stmt ) ) {828 if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {829 return true;830 } // if831 } // if832 return false;833 }, true);834 return compoundStmt;835 }836 837 // there may be typedefs nested within aggregates. in order for everything to work properly, these should be removed838 // as well839 1057 template<typename AggDecl> 840 AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) { 841 filter( aggDecl->members, isTypedef, true ); 842 return aggDecl; 843 } 844 845 template<typename AggDecl> 846 void EliminateTypedef::addImplicitTypedef( AggDecl * aggDecl ) { 1058 void ReplaceTypedef::addImplicitTypedef( AggDecl * aggDecl ) { 847 1059 if ( typedefNames.count( aggDecl->get_name() ) == 0 ) { 848 1060 Type *type = nullptr; … … 854 1066 type = new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ); 855 1067 } // if 856 TypedefDeclPtr tyDecl( new TypedefDecl( aggDecl->get_name(), Type::StorageClasses(), type, aggDecl->get_linkage() ) );1068 TypedefDeclPtr tyDecl( new TypedefDecl( aggDecl->get_name(), aggDecl->location, Type::StorageClasses(), type, aggDecl->get_linkage() ) ); 857 1069 typedefNames[ aggDecl->get_name() ] = std::make_pair( std::move( tyDecl ), scopeLevel ); 858 } // if 859 } 860 861 void EliminateTypedef::premutate( StructDecl * structDecl ) { 1070 // add the implicit typedef to the AST 1071 declsToAddBefore.push_back( new TypedefDecl( aggDecl->get_name(), aggDecl->location, Type::StorageClasses(), type->clone(), aggDecl->get_linkage() ) ); 1072 } // if 1073 } 1074 1075 template< typename AggDecl > 1076 void ReplaceTypedef::handleAggregate( AggDecl * aggr ) { 1077 SemanticErrorException errors; 1078 1079 ValueGuard< std::list<Declaration * > > oldBeforeDecls( declsToAddBefore ); 1080 ValueGuard< std::list<Declaration * > > oldAfterDecls ( declsToAddAfter ); 1081 declsToAddBefore.clear(); 1082 declsToAddAfter.clear(); 1083 1084 GuardScope( typedefNames ); 1085 GuardScope( typedeclNames ); 1086 mutateAll( aggr->parameters, *visitor ); 1087 1088 // unroll mutateAll for aggr->members so that implicit typedefs for nested types are added to the aggregate body. 1089 for ( std::list< Declaration * >::iterator i = aggr->members.begin(); i != aggr->members.end(); ++i ) { 1090 if ( !declsToAddAfter.empty() ) { aggr->members.splice( i, declsToAddAfter ); } 1091 1092 try { 1093 *i = maybeMutate( *i, *visitor ); 1094 } catch ( SemanticErrorException &e ) { 1095 errors.append( e ); 1096 } 1097 1098 if ( !declsToAddBefore.empty() ) { aggr->members.splice( i, declsToAddBefore ); } 1099 } 1100 1101 if ( !declsToAddAfter.empty() ) { aggr->members.splice( aggr->members.end(), declsToAddAfter ); } 1102 if ( !errors.isEmpty() ) { throw errors; } 1103 } 1104 1105 void ReplaceTypedef::premutate( StructDecl * structDecl ) { 1106 visit_children = false; 862 1107 addImplicitTypedef( structDecl ); 863 } 864 865 866 Declaration *EliminateTypedef::postmutate( StructDecl * structDecl ) { 867 return handleAggregate( structDecl ); 868 } 869 870 void EliminateTypedef::premutate( UnionDecl * unionDecl ) { 1108 handleAggregate( structDecl ); 1109 } 1110 1111 void ReplaceTypedef::premutate( UnionDecl * unionDecl ) { 1112 visit_children = false; 871 1113 addImplicitTypedef( unionDecl ); 872 } 873 874 Declaration *EliminateTypedef::postmutate( UnionDecl * unionDecl ) { 875 return handleAggregate( unionDecl ); 876 } 877 878 void EliminateTypedef::premutate( EnumDecl * enumDecl ) { 1114 handleAggregate( unionDecl ); 1115 } 1116 1117 void ReplaceTypedef::premutate( EnumDecl * enumDecl ) { 879 1118 addImplicitTypedef( enumDecl ); 880 1119 } 881 1120 882 Declaration *EliminateTypedef::postmutate( EnumDecl * enumDecl ) { 883 return handleAggregate( enumDecl ); 884 } 885 886 Declaration *EliminateTypedef::postmutate( TraitDecl * traitDecl ) { 887 return handleAggregate( traitDecl ); 888 } 889 890 void EliminateTypedef::premutate( FunctionType * ) { 1121 void ReplaceTypedef::premutate( FunctionType * ) { 891 1122 GuardValue( inFunctionType ); 892 1123 inFunctionType = true; 1124 } 1125 1126 void ReplaceTypedef::premutate( TraitDecl * ) { 1127 GuardScope( typedefNames ); 1128 GuardScope( typedeclNames); 893 1129 } 894 1130 … … 905 1141 if ( CodeGen::isCtorDtorAssign( funcDecl->get_name() ) ) { // TODO: also check /=, etc. 906 1142 if ( params.size() == 0 ) { 907 throw SemanticError( "Constructors, destructors, and assignment functions require at least one parameter ", funcDecl);1143 SemanticError( funcDecl, "Constructors, destructors, and assignment functions require at least one parameter " ); 908 1144 } 909 1145 ReferenceType * refType = dynamic_cast< ReferenceType * >( params.front()->get_type() ); 910 1146 if ( ! refType ) { 911 throw SemanticError( "First parameter of a constructor, destructor, or assignment function must be a reference ", funcDecl);1147 SemanticError( funcDecl, "First parameter of a constructor, destructor, or assignment function must be a reference " ); 912 1148 } 913 1149 if ( CodeGen::isCtorDtor( funcDecl->get_name() ) && returnVals.size() != 0 ) { 914 throw SemanticError( "Constructors and destructors cannot have explicit return values ", funcDecl);1150 SemanticError( funcDecl, "Constructors and destructors cannot have explicit return values " ); 915 1151 } 916 1152 } … … 947 1183 948 1184 sub.apply( inst ); 949 if ( args.size() < params->size() ) throw SemanticError( "Too few type arguments in generic type ", inst);950 if ( args.size() > params->size() ) throw SemanticError( "Too many type arguments in generic type ", inst);1185 if ( args.size() < params->size() ) SemanticError( inst, "Too few type arguments in generic type " ); 1186 if ( args.size() > params->size() ) SemanticError( inst, "Too many type arguments in generic type " ); 951 1187 } 952 1188 } … … 1014 1250 } 1015 1251 1252 void FixObjectType::fix( std::list< Declaration * > & translationUnit ) { 1253 PassVisitor<FixObjectType> fixer; 1254 acceptAll( translationUnit, fixer ); 1255 } 1256 1257 void FixObjectType::previsit( ObjectDecl * objDecl ) { 1258 Type *new_type = ResolvExpr::resolveTypeof( objDecl->get_type(), indexer ); 1259 new_type->get_qualifiers() -= Type::Lvalue; // even if typeof is lvalue, variable can never have lvalue-qualified type 1260 objDecl->set_type( new_type ); 1261 } 1262 1263 void FixObjectType::previsit( FunctionDecl * funcDecl ) { 1264 Type *new_type = ResolvExpr::resolveTypeof( funcDecl->type, indexer ); 1265 new_type->get_qualifiers() -= Type::Lvalue; // even if typeof is lvalue, variable can never have lvalue-qualified type 1266 funcDecl->set_type( new_type ); 1267 } 1268 1269 void FixObjectType::previsit( TypeDecl *typeDecl ) { 1270 if ( typeDecl->get_base() ) { 1271 Type *new_type = ResolvExpr::resolveTypeof( typeDecl->get_base(), indexer ); 1272 new_type->get_qualifiers() -= Type::Lvalue; // even if typeof is lvalue, variable can never have lvalue-qualified type 1273 typeDecl->set_base( new_type ); 1274 } // if 1275 } 1276 1016 1277 void ArrayLength::computeLength( std::list< Declaration * > & translationUnit ) { 1017 1278 PassVisitor<ArrayLength> len; … … 1020 1281 1021 1282 void ArrayLength::previsit( ObjectDecl * objDecl ) { 1022 if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->get_type() ) ) { 1023 if ( at->get_dimension() ) return; 1024 if ( ListInit * init = dynamic_cast< ListInit * >( objDecl->get_init() ) ) { 1025 at->set_dimension( new ConstantExpr( Constant::from_ulong( init->get_initializers().size() ) ) ); 1026 } 1283 if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->type ) ) { 1284 if ( at->dimension ) return; 1285 if ( ListInit * init = dynamic_cast< ListInit * >( objDecl->init ) ) { 1286 at->dimension = new ConstantExpr( Constant::from_ulong( init->initializers.size() ) ); 1287 } 1288 } 1289 } 1290 1291 void ArrayLength::previsit( ArrayType * type ) { 1292 if ( type->dimension ) { 1293 // need to resolve array dimensions early so that constructor code can correctly determine 1294 // if a type is a VLA (and hence whether its elements need to be constructed) 1295 ResolvExpr::findSingleExpression( type->dimension, SymTab::SizeType->clone(), indexer ); 1296 1297 // must re-evaluate whether a type is a VLA, now that more information is available 1298 // (e.g. the dimension may have been an enumerator, which was unknown prior to this step) 1299 type->isVarLen = ! InitTweak::isConstExpr( type->dimension ); 1027 1300 } 1028 1301 }
Note:
See TracChangeset
for help on using the changeset viewer.