Ignore:
Timestamp:
Aug 27, 2018, 4:40:34 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
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.
Message:

Merge branch 'master' into cleanup-dtors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Validate.cc

    rf9feab8 r90152a4  
    4848#include "CodeGen/CodeGenerator.h"     // for genName
    4949#include "CodeGen/OperatorTable.h"     // for isCtorDtor, isCtorDtorAssign
     50#include "ControlStruct/Mutate.h"      // for ForExprMutator
    5051#include "Common/PassVisitor.h"        // for PassVisitor, WithDeclsToAdd
    5152#include "Common/ScopedMap.h"          // for ScopedMap
     
    6061#include "Parser/LinkageSpec.h"        // for C
    6162#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
    6365#include "SymTab/Autogen.h"            // for SizeType
    6466#include "SynTree/Attribute.h"         // for noAttributes, Attribute
     
    7274#include "SynTree/TypeSubstitution.h"  // for TypeSubstitution
    7375#include "SynTree/Visitor.h"           // for Visitor
     76#include "Validate/HandleAttributes.h" // for handleAttributes
    7477
    7578class CompoundStmt;
     
    7780class SwitchStmt;
    7881
    79 
    80 #define debugPrint( x ) if ( doDebug ) { std::cout << x; }
     82#define debugPrint( x ) if ( doDebug ) x
    8183
    8284namespace 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
    8398        struct HoistStruct final : public WithDeclsToAdd, public WithGuards {
    8499                /// Flattens nested struct types
    85100                static void hoistStruct( std::list< Declaration * > &translationUnit );
    86101
    87                 void previsit( EnumInstType * enumInstType );
    88                 void previsit( StructInstType * structInstType );
    89                 void previsit( UnionInstType * unionInstType );
    90102                void previsit( StructDecl * aggregateDecl );
    91103                void previsit( UnionDecl * aggregateDecl );
     104                void previsit( StaticAssertDecl * assertDecl );
     105                void previsit( StructInstType * type );
     106                void previsit( UnionInstType * type );
     107                void previsit( EnumInstType * type );
    92108
    93109          private:
    94110                template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl );
    95111
    96                 bool inStruct = false;
     112                AggregateDecl * parentAggr = nullptr;
    97113        };
    98114
     
    112128
    113129        /// 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 {
    115131                LinkReferenceToTypes( const Indexer *indexer );
    116132                void postvisit( TypeInstType *typeInst );
     
    120136                void postvisit( UnionInstType *unionInst );
    121137                void postvisit( TraitInstType *traitInst );
     138                void previsit( QualifiedType * qualType );
     139                void postvisit( QualifiedType * qualType );
    122140
    123141                void postvisit( EnumDecl *enumDecl );
     
    148166                void previsit( ObjectDecl * object );
    149167                void previsit( FunctionDecl * func );
     168                void previsit( FunctionType * ftype );
    150169                void previsit( StructDecl * aggrDecl );
    151170                void previsit( UnionDecl * aggrDecl );
     
    164183        };
    165184
    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 ) {}
    168187                /// 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 );
    171192                Type * postmutate( TypeInstType * aggregateUseType );
    172193                Declaration * postmutate( TypedefDecl * typeDecl );
     
    179200
    180201                void premutate( CompoundStmt * compoundStmt );
    181                 CompoundStmt * postmutate( CompoundStmt * compoundStmt );
    182202
    183203                void premutate( StructDecl * structDecl );
    184                 Declaration * postmutate( StructDecl * structDecl );
    185204                void premutate( UnionDecl * unionDecl );
    186                 Declaration * postmutate( UnionDecl * unionDecl );
    187205                void premutate( EnumDecl * enumDecl );
    188                 Declaration * postmutate( EnumDecl * enumDecl );
    189                 Declaration * postmutate( TraitDecl * contextDecl );
     206                void premutate( TraitDecl * );
    190207
    191208                void premutate( FunctionType * ftype );
     
    193210          private:
    194211                template<typename AggDecl>
    195                 AggDecl *handleAggregate( AggDecl * aggDecl );
    196 
    197                 template<typename AggDecl>
    198212                void addImplicitTypedef( AggDecl * aggDecl );
     213                template< typename AggDecl >
     214                void handleAggregate( AggDecl * aggr );
    199215
    200216                typedef std::unique_ptr<TypedefDecl> TypedefDeclPtr;
    201217                typedef ScopedMap< std::string, std::pair< TypedefDeclPtr, int > > TypedefMap;
    202                 typedef std::map< std::string, TypeDecl * > TypeDeclMap;
     218                typedef ScopedMap< std::string, TypeDecl * > TypeDeclMap;
    203219                TypedefMap typedefNames;
    204220                TypeDeclMap typedeclNames;
    205221                int scopeLevel;
    206222                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 );
    207235        };
    208236
     
    222250        };
    223251
    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 {
    225262                /// for array types without an explicit length, compute the length and store it so that it
    226263                /// is known to the rest of the phases. For example,
     
    233270
    234271                void previsit( ObjectDecl * objDecl );
     272                void previsit( ArrayType * arrayType );
    235273        };
    236274
     
    262300                PassVisitor<FindSpecialDeclarations> finder;
    263301                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 );
    267307                ReturnTypeFixer::fix( translationUnit ); // must happen before autogen
    268308                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
    269309                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 ); //
    270313                acceptAll( translationUnit, genericParams );  // check as early as possible - can't happen before LinkReferenceToTypes
    271314                VerifyCtorDtorAssign::verify( translationUnit );  // must happen before autogen, because autogen examines existing ctor/dtors
     
    274317                Concurrency::applyKeywords( translationUnit );
    275318                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
    276320                autogenerateRoutines( translationUnit ); // moved up, used to be below compoundLiteral - currently needs EnumAndPointerDecay
    277321                Concurrency::implementMutexFuncs( translationUnit );
    278322                Concurrency::implementThreadStarter( translationUnit );
    279323                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 );
    280326                ArrayLength::computeLength( translationUnit );
    281327                acceptAll( translationUnit, finder ); // xxx - remove this pass soon
    282328                mutateAll( translationUnit, labelAddrFixer );
     329                Validate::handleAttributes( translationUnit );
    283330        }
    284331
     
    292339        }
    293340
     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
    294447        void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) {
    295448                PassVisitor<HoistStruct> hoister;
     
    297450        }
    298451
    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                }
    301468        }
    302469
    303470        template< typename AggDecl >
    304471        void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) {
    305                 if ( inStruct ) {
     472                if ( parentAggr ) {
     473                        aggregateDecl->parent = parentAggr;
     474                        aggregateDecl->name = qualifiedName( aggregateDecl );
    306475                        // Add elements in stack order corresponding to nesting structure.
    307476                        declsToAddBefore.push_front( aggregateDecl );
    308477                } else {
    309                         GuardValue( inStruct );
    310                         inStruct = true;
     478                        GuardValue( parentAggr );
     479                        parentAggr = aggregateDecl;
    311480                } // if
    312481                // 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 );
    331488                }
    332489        }
     
    340497        }
    341498
     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
    342551        void EnumAndPointerDecay::previsit( EnumDecl *enumDecl ) {
    343552                // 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 ) {
    345554                        ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i );
    346555                        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 ) );
    348557                } // for
    349558        }
     
    351560        namespace {
    352561                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                        }
    380580                }
    381581        }
     
    383583        void EnumAndPointerDecay::previsit( FunctionType *func ) {
    384584                // 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 );
    387587        }
    388588
     
    396596
    397597        void LinkReferenceToTypes::postvisit( EnumInstType *enumInst ) {
    398                 EnumDecl *st = local_indexer->lookupEnum( enumInst->get_name() );
     598                EnumDecl *st = local_indexer->lookupEnum( enumInst->name );
    399599                // it's not a semantic error if the enum is not found, just an implicit forward declaration
    400600                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 ) {
    405604                        // use of forward declaration
    406                         forwardEnums[ enumInst->get_name() ].push_back( enumInst );
     605                        forwardEnums[ enumInst->name ].push_back( enumInst );
    407606                } // if
    408607        }
     
    411610                for ( Expression * param : inst->parameters ) {
    412611                        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: " );
    414613                        }
    415614                }
     
    417616
    418617        void LinkReferenceToTypes::postvisit( StructInstType *structInst ) {
    419                 StructDecl *st = local_indexer->lookupStruct( structInst->get_name() );
     618                StructDecl *st = local_indexer->lookupStruct( structInst->name );
    420619                // it's not a semantic error if the struct is not found, just an implicit forward declaration
    421620                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 ) {
    426624                        // use of forward declaration
    427                         forwardStructs[ structInst->get_name() ].push_back( structInst );
     625                        forwardStructs[ structInst->name ].push_back( structInst );
    428626                } // if
    429627                checkGenericParameters( structInst );
     
    431629
    432630        void LinkReferenceToTypes::postvisit( UnionInstType *unionInst ) {
    433                 UnionDecl *un = local_indexer->lookupUnion( unionInst->get_name() );
     631                UnionDecl *un = local_indexer->lookupUnion( unionInst->name );
    434632                // it's not a semantic error if the union is not found, just an implicit forward declaration
    435633                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 ) {
    439637                        // use of forward declaration
    440                         forwardUnions[ unionInst->get_name() ].push_back( unionInst );
     638                        forwardUnions[ unionInst->name ].push_back( unionInst );
    441639                } // if
    442640                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 );
    443650        }
    444651
     
    451658                        DeclarationWithType * dwt2 = dynamic_cast<DeclarationWithType *>( d2 );
    452659                        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() ) ) {
    454661                                        // std::cerr << "=========== equal:" << std::endl;
    455662                                        // std::cerr << "d1: " << d1 << std::endl;
     
    476683        template< typename Iterator >
    477684        void expandAssertions( TraitInstType * inst, Iterator out ) {
    478                 assertf( inst->baseTrait, "Trait instance not linked to base trait: %s", toString( inst ).c_str() );
     685                assertf( inst->baseTrait, "Trait instance not linked to base trait: %s", toCString( inst ) );
    479686                std::list< DeclarationWithType * > asserts;
    480687                for ( Declaration * decl : inst->baseTrait->members ) {
     
    511718                TraitDecl *traitDecl = local_indexer->lookupTrait( traitInst->name );
    512719                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: " );
    517724                } // if
    518725                traitInst->baseTrait = traitDecl;
    519726
    520727                // 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 ) ) {
    522729                        TypeExpr * expr = dynamic_cast< TypeExpr * >( std::get<1>(p) );
    523730                        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: " );
    525732                        }
    526733                        if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( expr->get_type() ) ) {
    527734                                TypeDecl * formalDecl = std::get<0>(p);
    528                                 TypeDecl * instDecl = inst->get_baseType();
     735                                TypeDecl * instDecl = inst->baseType;
    529736                                if ( formalDecl->get_sized() ) instDecl->set_sized( true );
    530737                        }
     
    535742        void LinkReferenceToTypes::postvisit( EnumDecl *enumDecl ) {
    536743                // 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 );
    539746                        if ( fwds != forwardEnums.end() ) {
    540747                                for ( std::list< EnumInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
    541                                         (*inst )->set_baseEnum( enumDecl );
     748                                        (*inst)->baseEnum = enumDecl;
    542749                                } // for
    543750                                forwardEnums.erase( fwds );
    544751                        } // 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                        }
    545761                } // if
    546762        }
     
    575791                // visit struct members first so that the types of self-referencing members are updated properly
    576792                // 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 );
    579795                        if ( fwds != forwardStructs.end() ) {
    580796                                for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
    581                                         (*inst )->set_baseStruct( structDecl );
     797                                        (*inst)->baseStruct = structDecl;
    582798                                } // for
    583799                                forwardStructs.erase( fwds );
     
    587803
    588804        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 );
    591807                        if ( fwds != forwardUnions.end() ) {
    592808                                for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
    593                                         (*inst )->set_baseUnion( unionDecl );
     809                                        (*inst)->baseUnion = unionDecl;
    594810                                } // for
    595811                                forwardUnions.erase( fwds );
     
    601817                // ensure generic parameter instances are renamed like the base type
    602818                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 ) ) {
    604820                        if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
    605821                                typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
     
    626842                        // apply FixFunction to every assertion to check for invalid void type
    627843                        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 " );
    632847                                } // if
    633848                        } // for
     
    637852
    638853        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                 } // if
     854                // 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                }
    643858                object->fixUniqueId();
    644859        }
    645860
    646861        void ForallPointerDecay::previsit( FunctionDecl *func ) {
    647                 forallFixer( func->type->forall, func );
    648862                func->fixUniqueId();
     863        }
     864
     865        void ForallPointerDecay::previsit( FunctionType * ftype ) {
     866                forallFixer( ftype->forall, ftype );
    649867        }
    650868
     
    673891                // were cast to void.
    674892                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;
    686900                mutateAll( translationUnit, eliminator );
    687901                if ( eliminator.pass.typedefNames.count( "size_t" ) ) {
    688902                        // 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();
    690904                } else {
    691905                        // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong
     
    693907                        SizeType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
    694908                }
    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 ) {
    699922                // instances of typedef types will come here. If it is an instance
    700923                // 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 );
    702925                if ( def != typedefNames.end() ) {
    703926                        Type *ret = def->second.first->base->clone();
     927                        ret->location = typeInst->location;
    704928                        ret->get_qualifiers() |= typeInst->get_qualifiers();
    705929                        // attributes are not carried over from typedef to function parameters/return values
     
    714938                                ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret);
    715939                                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 );
    717941                                }
    718                                 rtt->get_parameters().clear();
     942                                rtt->parameters.clear();
    719943                                cloneAll( typeInst->parameters, rtt->parameters );
    720944                                mutateAll( rtt->parameters, *visitor );  // recursively fix typedefs on parameters
     
    723947                        return ret;
    724948                } 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                        }
    727953                        typeInst->set_baseType( base->second );
    728                 } // if
    729                 return typeInst;
     954                        return typeInst;
     955                } // if
     956                assert( false );
    730957        }
    731958
     
    744971        }
    745972
    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 ) {
    748975                        // typedef to the same name from the same scope
    749976                        // must be from the same type
    750977
    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;
    753980                        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.
    757987                        if ( isVariableLength( t1 ) || isVariableLength( t2 ) ) {
    758                                 throw SemanticError( "Cannot redefine typedef: " + tyDecl->name );
     988                                SemanticError( tyDecl->location, "Cannot redefine typedef: " + tyDecl->name );
    759989                        }
    760990                } else {
    761                         typedefNames[ tyDecl->get_name() ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel );
     991                        typedefNames[ tyDecl->name ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel );
    762992                } // if
    763993
     
    7711001                // Note, qualifiers on the typedef are superfluous for the forward declaration.
    7721002
    773                 Type *designatorType = tyDecl->get_base()->stripDeclarator();
     1003                Type *designatorType = tyDecl->base->stripDeclarator();
    7741004                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 ) );
    7761006                } 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 ) );
    7781008                } 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 );
    7871016                if ( i != typedefNames.end() ) {
    7881017                        typedefNames.erase( i ) ;
    7891018                } // if
    7901019
    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 * ) {
    7951024                GuardScope( typedefNames );
    796         }
    797 
    798         void EliminateTypedef::premutate( ObjectDecl * ) {
     1025                GuardScope( typedeclNames );
     1026        }
     1027
     1028        void ReplaceTypedef::premutate( ObjectDecl * ) {
    7991029                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?
    8041035                        // 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();
    8071038                        objDecl->set_type( nullptr );
    8081039                        delete objDecl;
     
    8121043        }
    8131044
    814         void EliminateTypedef::premutate( CastExpr * ) {
     1045        void ReplaceTypedef::premutate( CastExpr * ) {
    8151046                GuardScope( typedefNames );
    816         }
    817 
    818         void EliminateTypedef::premutate( CompoundStmt * ) {
     1047                GuardScope( typedeclNames );
     1048        }
     1049
     1050        void ReplaceTypedef::premutate( CompoundStmt * ) {
    8191051                GuardScope( typedefNames );
     1052                GuardScope( typedeclNames );
    8201053                scopeLevel += 1;
    8211054                GuardAction( [this](){ scopeLevel -= 1; } );
    8221055        }
    8231056
    824         CompoundStmt *EliminateTypedef::postmutate( CompoundStmt * compoundStmt ) {
    825                 // remove and delete decl stmts
    826                 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                                 } // if
    831                         } // if
    832                         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 removed
    838         // as well
    8391057        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 ) {
    8471059                if ( typedefNames.count( aggDecl->get_name() ) == 0 ) {
    8481060                        Type *type = nullptr;
     
    8541066                                type = new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() );
    8551067                        } // 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() ) );
    8571069                        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;
    8621107                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;
    8711113                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 ) {
    8791118                addImplicitTypedef( enumDecl );
    8801119        }
    8811120
    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 * ) {
    8911122                GuardValue( inFunctionType );
    8921123                inFunctionType = true;
     1124        }
     1125
     1126        void ReplaceTypedef::premutate( TraitDecl * ) {
     1127                GuardScope( typedefNames );
     1128                GuardScope( typedeclNames);
    8931129        }
    8941130
     
    9051141                if ( CodeGen::isCtorDtorAssign( funcDecl->get_name() ) ) { // TODO: also check /=, etc.
    9061142                        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 " );
    9081144                        }
    9091145                        ReferenceType * refType = dynamic_cast< ReferenceType * >( params.front()->get_type() );
    9101146                        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 " );
    9121148                        }
    9131149                        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 " );
    9151151                        }
    9161152                }
     
    9471183
    9481184                        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 " );
    9511187                }
    9521188        }
     
    10141250        }
    10151251
     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
    10161277        void ArrayLength::computeLength( std::list< Declaration * > & translationUnit ) {
    10171278                PassVisitor<ArrayLength> len;
     
    10201281
    10211282        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 );
    10271300                }
    10281301        }
Note: See TracChangeset for help on using the changeset viewer.