Changeset f53acdf8 for src/SymTab


Ignore:
Timestamp:
Jul 19, 2019, 2:16:01 PM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
4eb43fa
Parents:
1f1c102 (diff), 8ac3b0e (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 new-ast

Location:
src/SymTab
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Indexer.cc

    r1f1c102 rf53acdf8  
    7474        }
    7575
    76         Indexer::Indexer() 
    77         : idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable(), 
    78           prevScope(), scope( 0 ), repScope( 0 ) { ++*stats().count; }
     76        Indexer::Indexer()
     77        : idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable(),
     78          prevScope(), scope( 0 ), repScope( 0 ) { ++* stats().count; }
    7979
    8080        Indexer::~Indexer() {
     
    8484        void Indexer::lazyInitScope() {
    8585                if ( repScope < scope ) {
    86                         ++*stats().lazy_scopes;
     86                        ++* stats().lazy_scopes;
    8787                        // create rollback
    88                         prevScope = std::make_shared<Indexer>( *this );
     88                        prevScope = std::make_shared<Indexer>( * this );
    8989                        // update repScope
    9090                        repScope = scope;
     
    9595                ++scope;
    9696
    97                 ++*stats().new_scopes;
     97                ++* stats().new_scopes;
    9898                stats().avg_scope_depth->push( scope );
    9999                stats().max_scope_depth->push( scope );
     
    103103                if ( repScope == scope ) {
    104104                        Ptr prev = prevScope;           // make sure prevScope stays live
    105                         *this = std::move(*prevScope);  // replace with previous scope
     105                        * this = std::move(* prevScope);  // replace with previous scope
    106106                }
    107107
     
    109109        }
    110110
    111         void Indexer::lookupId( const std::string &id, std::list< IdData > &out ) const {
    112                 ++*stats().lookup_calls;
     111        void Indexer::lookupId( const std::string & id, std::list< IdData > &out ) const {
     112                ++* stats().lookup_calls;
    113113                if ( ! idTable ) return;
    114114
    115                 ++*stats().map_lookups;
     115                ++* stats().map_lookups;
    116116                auto decls = idTable->find( id );
    117117                if ( decls == idTable->end() ) return;
     
    122122        }
    123123
    124         NamedTypeDecl *Indexer::lookupType( const std::string &id ) const {
    125                 ++*stats().lookup_calls;
     124        const NamedTypeDecl * Indexer::lookupType( const std::string & id ) const {
     125                ++* stats().lookup_calls;
    126126                if ( ! typeTable ) return nullptr;
    127                 ++*stats().map_lookups;
     127                ++* stats().map_lookups;
    128128                auto it = typeTable->find( id );
    129129                return it == typeTable->end() ? nullptr : it->second.decl;
    130130        }
    131131
    132         StructDecl *Indexer::lookupStruct( const std::string &id ) const {
    133                 ++*stats().lookup_calls;
     132        const StructDecl * Indexer::lookupStruct( const std::string & id ) const {
     133                ++* stats().lookup_calls;
    134134                if ( ! structTable ) return nullptr;
    135                 ++*stats().map_lookups;
     135                ++* stats().map_lookups;
    136136                auto it = structTable->find( id );
    137137                return it == structTable->end() ? nullptr : it->second.decl;
    138138        }
    139139
    140         EnumDecl *Indexer::lookupEnum( const std::string &id ) const {
    141                 ++*stats().lookup_calls;
     140        const EnumDecl * Indexer::lookupEnum( const std::string & id ) const {
     141                ++* stats().lookup_calls;
    142142                if ( ! enumTable ) return nullptr;
    143                 ++*stats().map_lookups;
     143                ++* stats().map_lookups;
    144144                auto it = enumTable->find( id );
    145145                return it == enumTable->end() ? nullptr : it->second.decl;
    146146        }
    147147
    148         UnionDecl *Indexer::lookupUnion( const std::string &id ) const {
    149                 ++*stats().lookup_calls;
     148        const UnionDecl * Indexer::lookupUnion( const std::string & id ) const {
     149                ++* stats().lookup_calls;
    150150                if ( ! unionTable ) return nullptr;
    151                 ++*stats().map_lookups;
     151                ++* stats().map_lookups;
    152152                auto it = unionTable->find( id );
    153153                return it == unionTable->end() ? nullptr : it->second.decl;
    154154        }
    155155
    156         TraitDecl *Indexer::lookupTrait( const std::string &id ) const {
    157                 ++*stats().lookup_calls;
     156        const TraitDecl * Indexer::lookupTrait( const std::string & id ) const {
     157                ++* stats().lookup_calls;
    158158                if ( ! traitTable ) return nullptr;
    159                 ++*stats().map_lookups;
     159                ++* stats().map_lookups;
    160160                auto it = traitTable->find( id );
    161161                return it == traitTable->end() ? nullptr : it->second.decl;
    162162        }
    163163
    164         const Indexer* Indexer::atScope( unsigned long target ) const {
     164        const Indexer * Indexer::atScope( unsigned long target ) const {
    165165                // by lazy construction, final indexer in list has repScope 0, cannot be > target
    166166                // otherwise, will find first scope representing the target
    167                 const Indexer* indexer = this;
     167                const Indexer * indexer = this;
    168168                while ( indexer->repScope > target ) {
    169169                        indexer = indexer->prevScope.get();
     
    172172        }
    173173
    174         NamedTypeDecl *Indexer::globalLookupType( const std::string &id ) const {
     174        const NamedTypeDecl * Indexer::globalLookupType( const std::string & id ) const {
    175175                return atScope( 0 )->lookupType( id );
    176176        }
    177177
    178         StructDecl *Indexer::globalLookupStruct( const std::string &id ) const {
     178        const StructDecl * Indexer::globalLookupStruct( const std::string & id ) const {
    179179                return atScope( 0 )->lookupStruct( id );
    180180        }
    181181
    182         UnionDecl *Indexer::globalLookupUnion( const std::string &id ) const {
     182        const UnionDecl * Indexer::globalLookupUnion( const std::string & id ) const {
    183183                return atScope( 0 )->lookupUnion( id );
    184184        }
    185185
    186         EnumDecl *Indexer::globalLookupEnum( const std::string &id ) const {
     186        const EnumDecl * Indexer::globalLookupEnum( const std::string & id ) const {
    187187                return atScope( 0 )->lookupEnum( id );
    188188        }
    189189
    190         bool isFunction( DeclarationWithType * decl ) {
     190        bool isFunction( const DeclarationWithType * decl ) {
    191191                return GenPoly::getFunctionType( decl->get_type() );
    192192        }
    193193
    194         bool isObject( DeclarationWithType * decl ) {
     194        bool isObject( const DeclarationWithType * decl ) {
    195195                return ! isFunction( decl );
    196196        }
    197197
    198         bool isDefinition( DeclarationWithType * decl ) {
    199                 if ( FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl ) ) {
     198        bool isDefinition( const DeclarationWithType * decl ) {
     199                if ( const FunctionDecl * func = dynamic_cast< const FunctionDecl * >( decl ) ) {
    200200                        // a function is a definition if it has a body
    201201                        return func->statements;
     
    207207        }
    208208
    209        
    210         bool Indexer::addedIdConflicts( 
    211                         const Indexer::IdData & existing, DeclarationWithType *added,
    212                         Indexer::OnConflict handleConflicts, BaseSyntaxNode * deleteStmt ) {
    213                 // if we're giving the same name mangling to things of different types then there is 
     209
     210        bool Indexer::addedIdConflicts(
     211                        const Indexer::IdData & existing, const DeclarationWithType * added,
     212                        Indexer::OnConflict handleConflicts, const Declaration * deleteStmt ) {
     213                // if we're giving the same name mangling to things of different types then there is
    214214                // something wrong
    215215                assert( (isObject( added ) && isObject( existing.id ) )
     
    219219                        // new definition shadows the autogenerated one, even at the same scope
    220220                        return false;
    221                 } else if ( LinkageSpec::isMangled( added->linkage ) 
    222                                 || ResolvExpr::typesCompatible( 
     221                } else if ( LinkageSpec::isMangled( added->linkage )
     222                                || ResolvExpr::typesCompatible(
    223223                                        added->get_type(), existing.id->get_type(), Indexer() ) ) {
    224224
     
    238238                        if ( isDefinition( added ) && isDefinition( existing.id ) ) {
    239239                                if ( handleConflicts.mode == OnConflict::Error ) {
    240                                         SemanticError( added, 
    241                                                 isFunction( added ) ? 
    242                                                         "duplicate function definition for " : 
     240                                        SemanticError( added,
     241                                                isFunction( added ) ?
     242                                                        "duplicate function definition for " :
    243243                                                        "duplicate object definition for " );
    244244                                }
     
    255255        }
    256256
    257         bool Indexer::hasCompatibleCDecl( const std::string &id, const std::string &mangleName ) const {
     257        bool Indexer::hasCompatibleCDecl( const std::string & id, const std::string &mangleName ) const {
    258258                if ( ! idTable ) return false;
    259259
    260                 ++*stats().map_lookups;
     260                ++* stats().map_lookups;
    261261                auto decls = idTable->find( id );
    262262                if ( decls == idTable->end() ) return false;
     
    270270                        }
    271271                }
    272                
     272
    273273                return false;
    274274        }
    275275
    276         bool Indexer::hasIncompatibleCDecl(
    277                         const std::string &id, const std::string &mangleName ) const {
     276        bool Indexer::hasIncompatibleCDecl(const std::string & id, const std::string &mangleName ) const {
    278277                if ( ! idTable ) return false;
    279278
    280                 ++*stats().map_lookups;
     279                ++* stats().map_lookups;
    281280                auto decls = idTable->find( id );
    282281                if ( decls == idTable->end() ) return false;
     
    295294
    296295        /// gets the base type of the first parameter; decl must be a ctor/dtor/assignment function
    297         std::string getOtypeKey( FunctionDecl* function ) {
     296        std::string getOtypeKey( const FunctionDecl * function ) {
    298297                auto& params = function->type->parameters;
    299298                assert( ! params.empty() );
    300299                // use base type of pointer, so that qualifiers on the pointer type aren't considered.
    301                 Type* base = InitTweak::getPointerBase( params.front()->get_type() );
     300                Type * base = InitTweak::getPointerBase( params.front()->get_type() );
    302301                assert( base );
    303302                return Mangler::mangle( base );
    304303        }
    305304
    306         /// gets the declaration for the function acting on a type specified by otype key, 
     305        /// gets the declaration for the function acting on a type specified by otype key,
    307306        /// nullptr if none such
    308         FunctionDecl * getFunctionForOtype( DeclarationWithType * decl, const std::string& otypeKey ) {
    309                 FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl );
     307        const FunctionDecl * getFunctionForOtype( const DeclarationWithType * decl, const std::string& otypeKey ) {
     308                const FunctionDecl * func = dynamic_cast< const FunctionDecl * >( decl );
    310309                if ( ! func || otypeKey != getOtypeKey( func ) ) return nullptr;
    311310                return func;
    312311        }
    313312
    314         bool Indexer::removeSpecialOverrides(
    315                         Indexer::IdData& data, Indexer::MangleTable::Ptr& mangleTable ) {
    316                 // if a type contains user defined ctor/dtor/assign, then special rules trigger, which
    317                 // determinethe set of ctor/dtor/assign that can be used  by the requester. In particular,
    318                 // if the user defines a default ctor, then the generated default ctor is unavailable,
    319                 // likewise for copy ctor and dtor. If the user defines any ctor/dtor, then no generated
    320                 // field ctors are available. If the user defines any ctor then the generated default ctor
    321                 // is unavailable (intrinsic default ctor must be overridden exactly). If the user defines
    322                 // anything that looks like a copy constructor, then the generated copy constructor is
     313        bool Indexer::removeSpecialOverrides(Indexer::IdData& data, Indexer::MangleTable::Ptr& mangleTable ) {
     314                // if a type contains user defined ctor/dtor/assign, then special rules trigger, which
     315                // determinethe set of ctor/dtor/assign that can be used  by the requester. In particular,
     316                // if the user defines a default ctor, then the generated default ctor is unavailable,
     317                // likewise for copy ctor and dtor. If the user defines any ctor/dtor, then no generated
     318                // field ctors are available. If the user defines any ctor then the generated default ctor
     319                // is unavailable (intrinsic default ctor must be overridden exactly). If the user defines
     320                // anything that looks like a copy constructor, then the generated copy constructor is
    323321                // unavailable, and likewise for the assignment operator.
    324322
    325323                // only relevant on function declarations
    326                 FunctionDecl * function = dynamic_cast< FunctionDecl * >( data.id );
     324                const FunctionDecl * function = dynamic_cast< const FunctionDecl * >( data.id );
    327325                if ( ! function ) return true;
    328326                // only need to perform this check for constructors, destructors, and assignment functions
     
    340338                        std::vector< MangleTable::value_type > deleted;
    341339                        bool alreadyUserDefinedFunc = false;
    342                        
    343                         for ( const auto& entry : *mangleTable ) {
     340
     341                        for ( const auto& entry : * mangleTable ) {
    344342                                // skip decls that aren't functions or are for the wrong type
    345                                 FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey );
     343                                const FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey );
    346344                                if ( ! decl ) continue;
    347345
     
    368366                        // perform removals from mangle table, and deletions if necessary
    369367                        for ( const auto& key : removed ) {
    370                                 ++*stats().map_mutations;
     368                                ++* stats().map_mutations;
    371369                                mangleTable = mangleTable->erase( key );
    372370                        }
    373371                        if ( ! alreadyUserDefinedFunc ) for ( const auto& entry : deleted ) {
    374                                 ++*stats().map_mutations;
     372                                ++* stats().map_mutations;
    375373                                mangleTable = mangleTable->set( entry.first, IdData{ entry.second, function } );
    376374                        }
     
    379377                        // if this is the first user-defined function, delete non-user-defined overloads
    380378                        std::vector< MangleTable::value_type > deleted;
    381                        
    382                         for ( const auto& entry : *mangleTable ) {
     379
     380                        for ( const auto& entry : * mangleTable ) {
    383381                                // skip decls that aren't functions or are for the wrong type
    384                                 FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey );
     382                                const FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey );
    385383                                if ( ! decl ) continue;
    386384
     
    402400                        // this needs to be a separate loop because of iterator invalidation
    403401                        for ( const auto& entry : deleted ) {
    404                                 ++*stats().map_mutations;
     402                                ++* stats().map_mutations;
    405403                                mangleTable = mangleTable->set( entry.first, IdData{ entry.second, function } );
    406404                        }
     
    408406                        // this is an overridable generated function
    409407                        // if there already exists a matching user-defined function, delete this appropriately
    410                         for ( const auto& entry : *mangleTable ) {
     408                        for ( const auto& entry : * mangleTable ) {
    411409                                // skip decls that aren't functions or are for the wrong type
    412                                 FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey );
     410                                const FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey );
    413411                                if ( ! decl ) continue;
    414412
     
    418416                                if ( dataIsCopyFunc ) {
    419417                                        // remove current function if exists a user-defined copy function
    420                                         // since the signatures for copy functions don't need to match exactly, using 
     418                                        // since the signatures for copy functions don't need to match exactly, using
    421419                                        // a delete statement is the wrong approach
    422420                                        if ( InitTweak::isCopyFunction( decl, decl->name ) ) return false;
     
    428426                        }
    429427                }
    430                
     428
    431429                // nothing (more) to fix, return true
    432430                return true;
    433431        }
    434432
    435         void Indexer::addId(
    436                         DeclarationWithType *decl, OnConflict handleConflicts, Expression * baseExpr,
    437                         BaseSyntaxNode * deleteStmt ) {
    438                 ++*stats().add_calls;
     433        void Indexer::addId(const DeclarationWithType * decl, OnConflict handleConflicts, const Expression * baseExpr,
     434                        const Declaration * deleteStmt ) {
     435                ++* stats().add_calls;
    439436                const std::string &name = decl->name;
    440437                if ( name == "" ) return;
    441                
     438
    442439                std::string mangleName;
    443440                if ( LinkageSpec::isOverridable( decl->linkage ) ) {
    444                         // mangle the name without including the appropriate suffix, so overridable routines 
     441                        // mangle the name without including the appropriate suffix, so overridable routines
    445442                        // are placed into the same "bucket" as their user defined versions.
    446443                        mangleName = Mangler::mangle( decl, false );
     
    449446                } // if
    450447
    451                 // this ensures that no two declarations with the same unmangled name at the same scope 
     448                // this ensures that no two declarations with the same unmangled name at the same scope
    452449                // both have C linkage
    453450                if ( LinkageSpec::isMangled( decl->linkage ) ) {
     
    457454                        }
    458455                } else {
    459                         // NOTE: only correct if name mangling is completely isomorphic to C 
     456                        // NOTE: only correct if name mangling is completely isomorphic to C
    460457                        // type-compatibility, which it may not be.
    461458                        if ( hasIncompatibleCDecl( name, mangleName ) ) {
     
    470467                        mangleTable = MangleTable::new_ptr();
    471468                } else {
    472                         ++*stats().map_lookups;
     469                        ++* stats().map_lookups;
    473470                        auto decls = idTable->find( name );
    474471                        if ( decls == idTable->end() ) {
     
    477474                                mangleTable = decls->second;
    478475                                // skip in-scope repeat declarations of same identifier
    479                                 ++*stats().map_lookups;
     476                                ++* stats().map_lookups;
    480477                                auto existing = mangleTable->find( mangleName );
    481478                                if ( existing != mangleTable->end()
     
    486483                                                        // set delete expression for conflicting identifier
    487484                                                        lazyInitScope();
    488                                                         *stats().map_mutations += 2;
     485                                                        * stats().map_mutations += 2;
    489486                                                        idTable = idTable->set(
    490487                                                                name,
    491                                                                 mangleTable->set( 
    492                                                                         mangleName, 
     488                                                                mangleTable->set(
     489                                                                        mangleName,
    493490                                                                        IdData{ existing->second, handleConflicts.deleteStmt } ) );
    494491                                                }
     
    504501                // Ensure that auto-generated ctor/dtor/assignment are deleted if necessary
    505502                if ( ! removeSpecialOverrides( data, mangleTable ) ) return;
    506                 *stats().map_mutations += 2;
     503                * stats().map_mutations += 2;
    507504                idTable = idTable->set( name, mangleTable->set( mangleName, std::move(data) ) );
    508505        }
    509506
    510         void Indexer::addId( DeclarationWithType * decl, Expression * baseExpr ) {
     507        void Indexer::addId( const DeclarationWithType * decl, const Expression * baseExpr ) {
    511508                // default handling of conflicts is to raise an error
    512509                addId( decl, OnConflict::error(), baseExpr, decl->isDeleted ? decl : nullptr );
    513510        }
    514511
    515         void Indexer::addDeletedId( DeclarationWithType * decl, BaseSyntaxNode * deleteStmt ) {
     512        void Indexer::addDeletedId( const DeclarationWithType * decl, const Declaration * deleteStmt ) {
    516513                // default handling of conflicts is to raise an error
    517514                addId( decl, OnConflict::error(), nullptr, deleteStmt );
    518515        }
    519516
    520         bool addedTypeConflicts( NamedTypeDecl *existing, NamedTypeDecl *added ) {
     517        bool addedTypeConflicts( const NamedTypeDecl * existing, const NamedTypeDecl * added ) {
    521518                if ( existing->base == nullptr ) {
    522519                        return false;
     
    530527                        }
    531528                }
    532                 // does not need to be added to the table if both existing and added have a base that are 
     529                // does not need to be added to the table if both existing and added have a base that are
    533530                // the same
    534531                return true;
    535532        }
    536533
    537         void Indexer::addType( NamedTypeDecl *decl ) {
    538                 ++*stats().add_calls;
    539                 const std::string &id = decl->name;
    540 
    541                 if ( ! typeTable ) { 
     534        void Indexer::addType( const NamedTypeDecl * decl ) {
     535                ++* stats().add_calls;
     536                const std::string & id = decl->name;
     537
     538                if ( ! typeTable ) {
    542539                        typeTable = TypeTable::new_ptr();
    543540                } else {
    544                         ++*stats().map_lookups;
     541                        ++* stats().map_lookups;
    545542                        auto existing = typeTable->find( id );
    546                         if ( existing != typeTable->end() 
    547                                 && existing->second.scope == scope 
     543                        if ( existing != typeTable->end()
     544                                && existing->second.scope == scope
    548545                                && addedTypeConflicts( existing->second.decl, decl ) ) return;
    549546                }
    550                
     547
    551548                lazyInitScope();
    552                 ++*stats().map_mutations;
     549                ++* stats().map_mutations;
    553550                typeTable = typeTable->set( id, Scoped<NamedTypeDecl>{ decl, scope } );
    554551        }
    555552
    556         bool addedDeclConflicts( AggregateDecl *existing, AggregateDecl *added ) {
     553        bool addedDeclConflicts( const AggregateDecl * existing, const AggregateDecl * added ) {
    557554                if ( ! existing->body ) {
    558555                        return false;
     
    563560        }
    564561
    565         void Indexer::addStruct( const std::string &id ) {
     562        void Indexer::addStruct( const std::string & id ) {
    566563                addStruct( new StructDecl( id ) );
    567564        }
    568565
    569         void Indexer::addStruct( StructDecl *decl ) {
    570                 ++*stats().add_calls;
    571                 const std::string &id = decl->name;
     566        void Indexer::addStruct( const StructDecl * decl ) {
     567                ++* stats().add_calls;
     568                const std::string & id = decl->name;
    572569
    573570                if ( ! structTable ) {
    574571                        structTable = StructTable::new_ptr();
    575572                } else {
    576                         ++*stats().map_lookups;
     573                        ++* stats().map_lookups;
    577574                        auto existing = structTable->find( id );
    578                         if ( existing != structTable->end() 
    579                                 && existing->second.scope == scope 
     575                        if ( existing != structTable->end()
     576                                && existing->second.scope == scope
    580577                                && addedDeclConflicts( existing->second.decl, decl ) ) return;
    581578                }
    582579
    583580                lazyInitScope();
    584                 ++*stats().map_mutations;
     581                ++* stats().map_mutations;
    585582                structTable = structTable->set( id, Scoped<StructDecl>{ decl, scope } );
    586583        }
    587584
    588         void Indexer::addEnum( EnumDecl *decl ) {
    589                 ++*stats().add_calls;
    590                 const std::string &id = decl->name;
     585        void Indexer::addEnum( const EnumDecl * decl ) {
     586                ++* stats().add_calls;
     587                const std::string & id = decl->name;
    591588
    592589                if ( ! enumTable ) {
    593590                        enumTable = EnumTable::new_ptr();
    594591                } else {
    595                         ++*stats().map_lookups;
     592                        ++* stats().map_lookups;
    596593                        auto existing = enumTable->find( id );
    597                         if ( existing != enumTable->end() 
    598                                 && existing->second.scope == scope 
     594                        if ( existing != enumTable->end()
     595                                && existing->second.scope == scope
    599596                                && addedDeclConflicts( existing->second.decl, decl ) ) return;
    600597                }
    601                
     598
    602599                lazyInitScope();
    603                 ++*stats().map_mutations;
     600                ++* stats().map_mutations;
    604601                enumTable = enumTable->set( id, Scoped<EnumDecl>{ decl, scope } );
    605602        }
    606603
    607         void Indexer::addUnion( const std::string &id ) {
     604        void Indexer::addUnion( const std::string & id ) {
    608605                addUnion( new UnionDecl( id ) );
    609606        }
    610607
    611         void Indexer::addUnion( UnionDecl *decl ) {
    612                 ++*stats().add_calls;
    613                 const std::string &id = decl->name;
     608        void Indexer::addUnion( const UnionDecl * decl ) {
     609                ++* stats().add_calls;
     610                const std::string & id = decl->name;
    614611
    615612                if ( ! unionTable ) {
    616613                        unionTable = UnionTable::new_ptr();
    617614                } else {
    618                         ++*stats().map_lookups;
     615                        ++* stats().map_lookups;
    619616                        auto existing = unionTable->find( id );
    620                         if ( existing != unionTable->end() 
    621                                 && existing->second.scope == scope 
     617                        if ( existing != unionTable->end()
     618                                && existing->second.scope == scope
    622619                                && addedDeclConflicts( existing->second.decl, decl ) ) return;
    623620                }
    624621
    625622                lazyInitScope();
    626                 ++*stats().map_mutations;
     623                ++* stats().map_mutations;
    627624                unionTable = unionTable->set( id, Scoped<UnionDecl>{ decl, scope } );
    628625        }
    629626
    630         void Indexer::addTrait( TraitDecl *decl ) {
    631                 ++*stats().add_calls;
    632                 const std::string &id = decl->name;
     627        void Indexer::addTrait( const TraitDecl * decl ) {
     628                ++* stats().add_calls;
     629                const std::string & id = decl->name;
    633630
    634631                if ( ! traitTable ) {
    635632                        traitTable = TraitTable::new_ptr();
    636633                } else {
    637                         ++*stats().map_lookups;
     634                        ++* stats().map_lookups;
    638635                        auto existing = traitTable->find( id );
    639                         if ( existing != traitTable->end() 
    640                                 && existing->second.scope == scope 
     636                        if ( existing != traitTable->end()
     637                                && existing->second.scope == scope
    641638                                && addedDeclConflicts( existing->second.decl, decl ) ) return;
    642639                }
    643640
    644641                lazyInitScope();
    645                 ++*stats().map_mutations;
     642                ++* stats().map_mutations;
    646643                traitTable = traitTable->set( id, Scoped<TraitDecl>{ decl, scope } );
    647644        }
    648645
    649         void Indexer::addMembers( AggregateDecl * aggr, Expression * expr,
    650                         OnConflict handleConflicts ) {
     646        void Indexer::addMembers( const AggregateDecl * aggr, const Expression * expr, OnConflict handleConflicts ) {
    651647                for ( Declaration * decl : aggr->members ) {
    652648                        if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
    653649                                addId( dwt, handleConflicts, expr );
    654650                                if ( dwt->name == "" ) {
    655                                         Type * t = dwt->get_type()->stripReferences();
    656                                         if ( dynamic_cast<StructInstType*>( t ) || dynamic_cast<UnionInstType*>( t ) ) {
     651                                        const Type * t = dwt->get_type()->stripReferences();
     652                                        if ( dynamic_cast<const StructInstType *>( t ) || dynamic_cast<const UnionInstType *>( t ) ) {
    657653                                                Expression * base = expr->clone();
    658654                                                ResolvExpr::Cost cost = ResolvExpr::Cost::zero; // xxx - carry this cost into the indexer as a base cost?
     
    665661        }
    666662
    667         void Indexer::addWith( std::list< Expression * > & withExprs, BaseSyntaxNode * withStmt ) {
    668                 for ( Expression * expr : withExprs ) {
     663        void Indexer::addWith( const std::list< Expression * > & withExprs, const Declaration * withStmt ) {
     664                for ( const Expression * expr : withExprs ) {
    669665                        if ( expr->result ) {
    670666                                AggregateDecl * aggr = expr->result->stripReferences()->getAggr();
     
    689685        }
    690686
    691         void Indexer::addFunctionType( FunctionType * ftype ) {
     687        void Indexer::addFunctionType( const FunctionType * ftype ) {
    692688                addTypes( ftype->forall );
    693689                addIds( ftype->returnVals );
     
    700696                        Expression * base = baseExpr->clone();
    701697                        ResolvExpr::referenceToRvalueConversion( base, cost );
    702                         ret = new MemberExpr( id, base );
     698                        ret = new MemberExpr( const_cast<DeclarationWithType *>(id), base );
    703699                        // xxx - this introduces hidden environments, for now remove them.
    704700                        // std::swap( base->env, ret->env );
     
    706702                        base->env = nullptr;
    707703                } else {
    708                         ret = new VariableExpr( id );
    709                 }
    710                 if ( deleteStmt ) ret = new DeletedExpr( ret, deleteStmt );
     704                        ret = new VariableExpr( const_cast<DeclarationWithType *>(id) );
     705                }
     706                if ( deleteStmt ) ret = new DeletedExpr( ret, const_cast<Declaration *>(deleteStmt) );
    711707                return ret;
    712708        }
  • src/SymTab/Indexer.h

    r1f1c102 rf53acdf8  
    3434                virtual ~Indexer();
    3535
    36                 // when using an indexer manually (e.g., within a mutator traversal), it is necessary to 
     36                // when using an indexer manually (e.g., within a mutator traversal), it is necessary to
    3737                // tell the indexer explicitly when scopes begin and end
    3838                void enterScope();
     
    4040
    4141                struct IdData {
    42                         DeclarationWithType * id = nullptr;
    43                         Expression * baseExpr = nullptr; // WithExpr
     42                        const DeclarationWithType * id = nullptr;
     43                        const Expression * baseExpr = nullptr; // WithExpr
    4444
    4545                        /// non-null if this declaration is deleted
    46                         BaseSyntaxNode * deleteStmt = nullptr;
     46                        const Declaration * deleteStmt = nullptr;
    4747                        /// scope of identifier
    4848                        unsigned long scope = 0;
     
    5050                        // NOTE: shouldn't need either of these constructors, but gcc-4 does not properly support initializer lists with default members.
    5151                        IdData() = default;
    52                         IdData( 
    53                                 DeclarationWithType * id, Expression * baseExpr, BaseSyntaxNode * deleteStmt,
    54                                 unsigned long scope ) 
     52                        IdData(
     53                                const DeclarationWithType * id, const Expression * baseExpr, const Declaration * deleteStmt,
     54                                unsigned long scope )
    5555                                : id( id ), baseExpr( baseExpr ), deleteStmt( deleteStmt ), scope( scope ) {}
    56                         IdData( const IdData& o, BaseSyntaxNode * deleteStmt )
     56                        IdData( const IdData& o, const Declaration * deleteStmt )
    5757                                : id( o.id ), baseExpr( o.baseExpr ), deleteStmt( deleteStmt ), scope( o.scope ) {}
    5858
     
    6161
    6262                /// Gets all declarations with the given ID
    63                 void lookupId( const std::string &id, std::list< IdData > &out ) const;
     63                void lookupId( const std::string & id, std::list< IdData > &out ) const;
    6464                /// Gets the top-most type declaration with the given ID
    65                 NamedTypeDecl *lookupType( const std::string &id ) const;
     65                const NamedTypeDecl * lookupType( const std::string & id ) const;
    6666                /// Gets the top-most struct declaration with the given ID
    67                 StructDecl *lookupStruct( const std::string &id ) const;
     67                const StructDecl * lookupStruct( const std::string & id ) const;
    6868                /// Gets the top-most enum declaration with the given ID
    69                 EnumDecl *lookupEnum( const std::string &id ) const;
     69                const EnumDecl * lookupEnum( const std::string & id ) const;
    7070                /// Gets the top-most union declaration with the given ID
    71                 UnionDecl *lookupUnion( const std::string &id ) const;
     71                const UnionDecl * lookupUnion( const std::string & id ) const;
    7272                /// Gets the top-most trait declaration with the given ID
    73                 TraitDecl *lookupTrait( const std::string &id ) const;
     73                const TraitDecl * lookupTrait( const std::string & id ) const;
    7474
    7575                /// Gets the type declaration with the given ID at global scope
    76                 NamedTypeDecl *globalLookupType( const std::string &id ) const;
     76                const NamedTypeDecl * globalLookupType( const std::string & id ) const;
    7777                /// Gets the struct declaration with the given ID at global scope
    78                 StructDecl *globalLookupStruct( const std::string &id ) const;
     78                const StructDecl * globalLookupStruct( const std::string & id ) const;
    7979                /// Gets the union declaration with the given ID at global scope
    80                 UnionDecl *globalLookupUnion( const std::string &id ) const;
     80                const UnionDecl * globalLookupUnion( const std::string & id ) const;
    8181                /// Gets the enum declaration with the given ID at global scope
    82                 EnumDecl *globalLookupEnum( const std::string &id ) const;
     82                const EnumDecl * globalLookupEnum( const std::string & id ) const;
    8383
    84                 void addId( DeclarationWithType * decl, Expression * baseExpr = nullptr );
    85                 void addDeletedId( DeclarationWithType * decl, BaseSyntaxNode * deleteStmt );
     84                void addId( const DeclarationWithType * decl, const Expression * baseExpr = nullptr );
     85                void addDeletedId( const DeclarationWithType * decl, const Declaration * deleteStmt );
    8686
    87                 void addType( NamedTypeDecl *decl );
    88                 void addStruct( const std::string &id );
    89                 void addStruct( StructDecl *decl );
    90                 void addEnum( EnumDecl *decl );
    91                 void addUnion( const std::string &id );
    92                 void addUnion( UnionDecl *decl );
    93                 void addTrait( TraitDecl *decl );
     87                void addType( const NamedTypeDecl * decl );
     88                void addStruct( const std::string & id );
     89                void addStruct( const StructDecl * decl );
     90                void addEnum( const EnumDecl * decl );
     91                void addUnion( const std::string & id );
     92                void addUnion( const UnionDecl * decl );
     93                void addTrait( const TraitDecl * decl );
    9494
    9595                /// adds all of the IDs from WithStmt exprs
    96                 void addWith( std::list< Expression * > & withExprs, BaseSyntaxNode * withStmt );
     96                void addWith( const std::list< Expression * > & withExprs, const Declaration * withStmt );
    9797
    9898                /// convenience function for adding a list of Ids to the indexer
     
    103103
    104104                /// convenience function for adding all of the declarations in a function type to the indexer
    105                 void addFunctionType( FunctionType * ftype );
     105                void addFunctionType( const FunctionType * ftype );
    106106
    107107          private:
    108                 /// Wraps a Decl* with a scope
     108                /// Wraps a Decl * with a scope
    109109                template<typename Decl>
    110110                struct Scoped {
    111                         Decl* decl;           ///< declaration
     111                        const Decl * decl;           ///< declaration
    112112                        unsigned long scope;  ///< scope of this declaration
    113113
    114                         Scoped(Decl* d, unsigned long s) : decl(d), scope(s) {}
     114                        Scoped(const Decl * d, unsigned long s) : decl(d), scope(s) {}
    115115                };
    116116
     
    140140
    141141                /// Gets the indexer at the given scope
    142                 const Indexer* atScope( unsigned long scope ) const;
     142                const Indexer * atScope( unsigned long scope ) const;
    143143
    144                 /// Removes matching autogenerated constructors and destructors so that they will not be 
     144                /// Removes matching autogenerated constructors and destructors so that they will not be
    145145                /// selected. If returns false, passed decl should not be added.
    146                 bool removeSpecialOverrides( IdData& decl, MangleTable::Ptr& mangleTable );
     146                bool removeSpecialOverrides( IdData & decl, MangleTable::Ptr & mangleTable );
    147147
    148148                /// Options for handling identifier conflicts
     
    152152                                Delete  ///< Delete the earlier version with the delete statement
    153153                        } mode;
    154                         BaseSyntaxNode * deleteStmt;  ///< Statement that deletes this expression
     154                        const Declaration * deleteStmt;  ///< Statement that deletes this expression
    155155
    156156                private:
    157157                        OnConflict() : mode(Error), deleteStmt(nullptr) {}
    158                         OnConflict( BaseSyntaxNode * d ) : mode(Delete), deleteStmt(d) {}
     158                        OnConflict( const Declaration * d ) : mode(Delete), deleteStmt(d) {}
    159159                public:
    160160                        OnConflict( const OnConflict& ) = default;
    161161
    162162                        static OnConflict error() { return {}; }
    163                         static OnConflict deleteWith( BaseSyntaxNode * d ) { return { d }; }
     163                        static OnConflict deleteWith( const Declaration * d ) { return { d }; }
    164164                };
    165165
    166166                /// true if the existing identifier conflicts with the added identifier
    167167                bool addedIdConflicts(
    168                         const IdData& existing, DeclarationWithType * added, OnConflict handleConflicts,
    169                         BaseSyntaxNode * deleteStmt );
     168                        const IdData & existing, const DeclarationWithType * added, OnConflict handleConflicts,
     169                        const Declaration * deleteStmt );
    170170
    171171                /// common code for addId, addDeletedId, etc.
    172                 void addId(
    173                         DeclarationWithType * decl, OnConflict handleConflicts,
    174                         Expression * baseExpr = nullptr, BaseSyntaxNode * deleteStmt = nullptr );
     172                void addId(const DeclarationWithType * decl, OnConflict handleConflicts,
     173                        const Expression * baseExpr = nullptr, const Declaration * deleteStmt = nullptr );
    175174
    176175                /// adds all of the members of the Aggregate (addWith helper)
    177                 void addMembers( AggregateDecl * aggr, Expression * expr, OnConflict handleConflicts );
     176                void addMembers( const AggregateDecl * aggr, const Expression * expr, OnConflict handleConflicts );
    178177
    179178                /// returns true if there exists a declaration with C linkage and the given name with the same mangled name
    180                 bool hasCompatibleCDecl( const std::string &id, const std::string &mangleName ) const;
     179                bool hasCompatibleCDecl( const std::string & id, const std::string & mangleName ) const;
    181180                /// returns true if there exists a declaration with C linkage and the given name with a different mangled name
    182                 bool hasIncompatibleCDecl( const std::string &id, const std::string &mangleName ) const;
     181                bool hasIncompatibleCDecl( const std::string & id, const std::string & mangleName ) const;
    183182        };
    184183} // namespace SymTab
  • src/SymTab/Mangler.cc

    r1f1c102 rf53acdf8  
    4242                                Mangler_old( const Mangler_old & ) = delete;
    4343
    44                                 void previsit( BaseSyntaxNode * ) { visit_children = false; }
    45 
    46                                 void postvisit( ObjectDecl * declaration );
    47                                 void postvisit( FunctionDecl * declaration );
    48                                 void postvisit( TypeDecl * declaration );
    49 
    50                                 void postvisit( VoidType * voidType );
    51                                 void postvisit( BasicType * basicType );
    52                                 void postvisit( PointerType * pointerType );
    53                                 void postvisit( ArrayType * arrayType );
    54                                 void postvisit( ReferenceType * refType );
    55                                 void postvisit( FunctionType * functionType );
    56                                 void postvisit( StructInstType * aggregateUseType );
    57                                 void postvisit( UnionInstType * aggregateUseType );
    58                                 void postvisit( EnumInstType * aggregateUseType );
    59                                 void postvisit( TypeInstType * aggregateUseType );
    60                                 void postvisit( TraitInstType * inst );
    61                                 void postvisit( TupleType * tupleType );
    62                                 void postvisit( VarArgsType * varArgsType );
    63                                 void postvisit( ZeroType * zeroType );
    64                                 void postvisit( OneType * oneType );
    65                                 void postvisit( QualifiedType * qualType );
     44                                void previsit( const BaseSyntaxNode * ) { visit_children = false; }
     45
     46                                void postvisit( const ObjectDecl * declaration );
     47                                void postvisit( const FunctionDecl * declaration );
     48                                void postvisit( const TypeDecl * declaration );
     49
     50                                void postvisit( const VoidType * voidType );
     51                                void postvisit( const BasicType * basicType );
     52                                void postvisit( const PointerType * pointerType );
     53                                void postvisit( const ArrayType * arrayType );
     54                                void postvisit( const ReferenceType * refType );
     55                                void postvisit( const FunctionType * functionType );
     56                                void postvisit( const StructInstType * aggregateUseType );
     57                                void postvisit( const UnionInstType * aggregateUseType );
     58                                void postvisit( const EnumInstType * aggregateUseType );
     59                                void postvisit( const TypeInstType * aggregateUseType );
     60                                void postvisit( const TraitInstType * inst );
     61                                void postvisit( const TupleType * tupleType );
     62                                void postvisit( const VarArgsType * varArgsType );
     63                                void postvisit( const ZeroType * zeroType );
     64                                void postvisit( const OneType * oneType );
     65                                void postvisit( const QualifiedType * qualType );
    6666
    6767                                std::string get_mangleName() { return mangleName.str(); }
     
    7979
    8080                          public:
    81                                 Mangler_old( bool mangleOverridable, bool typeMode, bool mangleGenericParams, 
     81                                Mangler_old( bool mangleOverridable, bool typeMode, bool mangleGenericParams,
    8282                                        int nextVarNum, const VarMapType& varNums );
    8383
    8484                          private:
    85                                 void mangleDecl( DeclarationWithType *declaration );
    86                                 void mangleRef( ReferenceToType *refType, std::string prefix );
    87 
    88                                 void printQualifiers( Type *type );
     85                                void mangleDecl( const DeclarationWithType * declaration );
     86                                void mangleRef( const ReferenceToType * refType, std::string prefix );
     87
     88                                void printQualifiers( const Type *type );
    8989                        }; // Mangler_old
    9090                } // namespace
    9191
    92                 std::string mangle( BaseSyntaxNode * decl, bool mangleOverridable, bool typeMode, bool mangleGenericParams ) {
     92                std::string mangle( const BaseSyntaxNode * decl, bool mangleOverridable, bool typeMode, bool mangleGenericParams ) {
    9393                        PassVisitor<Mangler_old> mangler( mangleOverridable, typeMode, mangleGenericParams );
    9494                        maybeAccept( decl, mangler );
     
    9696                }
    9797
    98                 std::string mangleType( Type * ty ) {
     98                std::string mangleType( const Type * ty ) {
    9999                        PassVisitor<Mangler_old> mangler( false, true, true );
    100100                        maybeAccept( ty, mangler );
     
    102102                }
    103103
    104                 std::string mangleConcrete( Type * ty ) {
     104                std::string mangleConcrete( const Type * ty ) {
    105105                        PassVisitor<Mangler_old> mangler( false, false, false );
    106106                        maybeAccept( ty, mangler );
     
    110110                namespace {
    111111                        Mangler_old::Mangler_old( bool mangleOverridable, bool typeMode, bool mangleGenericParams )
    112                                 : nextVarNum( 0 ), isTopLevel( true ), 
    113                                 mangleOverridable( mangleOverridable ), typeMode( typeMode ), 
     112                                : nextVarNum( 0 ), isTopLevel( true ),
     113                                mangleOverridable( mangleOverridable ), typeMode( typeMode ),
    114114                                mangleGenericParams( mangleGenericParams ) {}
    115                        
    116                         Mangler_old::Mangler_old( bool mangleOverridable, bool typeMode, bool mangleGenericParams, 
     115
     116                        Mangler_old::Mangler_old( bool mangleOverridable, bool typeMode, bool mangleGenericParams,
    117117                                int nextVarNum, const VarMapType& varNums )
    118                                 : varNums( varNums ), nextVarNum( nextVarNum ), isTopLevel( false ), 
    119                                 mangleOverridable( mangleOverridable ), typeMode( typeMode ), 
     118                                : varNums( varNums ), nextVarNum( nextVarNum ), isTopLevel( false ),
     119                                mangleOverridable( mangleOverridable ), typeMode( typeMode ),
    120120                                mangleGenericParams( mangleGenericParams ) {}
    121121
    122                         void Mangler_old::mangleDecl( DeclarationWithType * declaration ) {
     122                        void Mangler_old::mangleDecl( const DeclarationWithType * declaration ) {
    123123                                bool wasTopLevel = isTopLevel;
    124124                                if ( isTopLevel ) {
     
    150150                        }
    151151
    152                         void Mangler_old::postvisit( ObjectDecl * declaration ) {
     152                        void Mangler_old::postvisit( const ObjectDecl * declaration ) {
    153153                                mangleDecl( declaration );
    154154                        }
    155155
    156                         void Mangler_old::postvisit( FunctionDecl * declaration ) {
     156                        void Mangler_old::postvisit( const FunctionDecl * declaration ) {
    157157                                mangleDecl( declaration );
    158158                        }
    159159
    160                         void Mangler_old::postvisit( VoidType * voidType ) {
     160                        void Mangler_old::postvisit( const VoidType * voidType ) {
    161161                                printQualifiers( voidType );
    162162                                mangleName << Encoding::void_t;
    163163                        }
    164164
    165                         void Mangler_old::postvisit( BasicType * basicType ) {
     165                        void Mangler_old::postvisit( const BasicType * basicType ) {
    166166                                printQualifiers( basicType );
    167                                 assertf( basicType->get_kind() < BasicType::NUMBER_OF_BASIC_TYPES, "Unhandled basic type: %d", basicType->get_kind() );
    168                                 mangleName << Encoding::basicTypes[ basicType->get_kind() ];
    169                         }
    170 
    171                         void Mangler_old::postvisit( PointerType * pointerType ) {
     167                                assertf( basicType->kind < BasicType::NUMBER_OF_BASIC_TYPES, "Unhandled basic type: %d", basicType->kind );
     168                                mangleName << Encoding::basicTypes[ basicType->kind ];
     169                        }
     170
     171                        void Mangler_old::postvisit( const PointerType * pointerType ) {
    172172                                printQualifiers( pointerType );
    173173                                // mangle void (*f)() and void f() to the same name to prevent overloading on functions and function pointers
     
    176176                        }
    177177
    178                         void Mangler_old::postvisit( ArrayType * arrayType ) {
     178                        void Mangler_old::postvisit( const ArrayType * arrayType ) {
    179179                                // TODO: encode dimension
    180180                                printQualifiers( arrayType );
     
    183183                        }
    184184
    185                         void Mangler_old::postvisit( ReferenceType * refType ) {
     185                        void Mangler_old::postvisit( const ReferenceType * refType ) {
    186186                                // don't print prefix (e.g. 'R') for reference types so that references and non-references do not overload.
    187187                                // Further, do not print the qualifiers for a reference type (but do run printQualifers because of TypeDecls, etc.),
     
    202202                        }
    203203
    204                         void Mangler_old::postvisit( FunctionType * functionType ) {
     204                        void Mangler_old::postvisit( const FunctionType * functionType ) {
    205205                                printQualifiers( functionType );
    206206                                mangleName << Encoding::function;
     
    219219                        }
    220220
    221                         void Mangler_old::mangleRef( ReferenceToType * refType, std::string prefix ) {
     221                        void Mangler_old::mangleRef( const ReferenceToType * refType, std::string prefix ) {
    222222                                printQualifiers( refType );
    223223
     
    225225
    226226                                if ( mangleGenericParams ) {
    227                                         std::list< Expression* >& params = refType->parameters;
     227                                        const std::list< Expression* > & params = refType->parameters;
    228228                                        if ( ! params.empty() ) {
    229229                                                mangleName << "_";
    230                                                 for ( std::list< Expression* >::const_iterator param = params.begin(); param != params.end(); ++param ) {
    231                                                         TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
    232                                                         assertf(paramType, "Aggregate parameters should be type expressions: %s", toCString(*param));
     230                                                for ( const Expression * param : params ) {
     231                                                        const TypeExpr * paramType = dynamic_cast< const TypeExpr * >( param );
     232                                                        assertf(paramType, "Aggregate parameters should be type expressions: %s", toCString(param));
    233233                                                        maybeAccept( paramType->type, *visitor );
    234234                                                }
     
    238238                        }
    239239
    240                         void Mangler_old::postvisit( StructInstType * aggregateUseType ) {
     240                        void Mangler_old::postvisit( const StructInstType * aggregateUseType ) {
    241241                                mangleRef( aggregateUseType, Encoding::struct_t );
    242242                        }
    243243
    244                         void Mangler_old::postvisit( UnionInstType * aggregateUseType ) {
     244                        void Mangler_old::postvisit( const UnionInstType * aggregateUseType ) {
    245245                                mangleRef( aggregateUseType, Encoding::union_t );
    246246                        }
    247247
    248                         void Mangler_old::postvisit( EnumInstType * aggregateUseType ) {
     248                        void Mangler_old::postvisit( const EnumInstType * aggregateUseType ) {
    249249                                mangleRef( aggregateUseType, Encoding::enum_t );
    250250                        }
    251251
    252                         void Mangler_old::postvisit( TypeInstType * typeInst ) {
     252                        void Mangler_old::postvisit( const TypeInstType * typeInst ) {
    253253                                VarMapType::iterator varNum = varNums.find( typeInst->get_name() );
    254254                                if ( varNum == varNums.end() ) {
     
    266266                        }
    267267
    268                         void Mangler_old::postvisit( TraitInstType * inst ) {
     268                        void Mangler_old::postvisit( const TraitInstType * inst ) {
    269269                                printQualifiers( inst );
    270270                                mangleName << inst->name.size() << inst->name;
    271271                        }
    272272
    273                         void Mangler_old::postvisit( TupleType * tupleType ) {
     273                        void Mangler_old::postvisit( const TupleType * tupleType ) {
    274274                                printQualifiers( tupleType );
    275275                                mangleName << Encoding::tuple << tupleType->types.size();
     
    277277                        }
    278278
    279                         void Mangler_old::postvisit( VarArgsType * varArgsType ) {
     279                        void Mangler_old::postvisit( const VarArgsType * varArgsType ) {
    280280                                printQualifiers( varArgsType );
    281281                                static const std::string vargs = "__builtin_va_list";
     
    283283                        }
    284284
    285                         void Mangler_old::postvisit( ZeroType * ) {
     285                        void Mangler_old::postvisit( const ZeroType * ) {
    286286                                mangleName << Encoding::zero;
    287287                        }
    288288
    289                         void Mangler_old::postvisit( OneType * ) {
     289                        void Mangler_old::postvisit( const OneType * ) {
    290290                                mangleName << Encoding::one;
    291291                        }
    292292
    293                         void Mangler_old::postvisit( QualifiedType * qualType ) {
     293                        void Mangler_old::postvisit( const QualifiedType * qualType ) {
    294294                                bool inqual = inQualifiedType;
    295295                                if (! inqual ) {
     
    307307                        }
    308308
    309                         void Mangler_old::postvisit( TypeDecl * decl ) {
     309                        void Mangler_old::postvisit( const TypeDecl * decl ) {
    310310                                // TODO: is there any case where mangling a TypeDecl makes sense? If so, this code needs to be
    311311                                // fixed to ensure that two TypeDecls mangle to the same name when they are the same type and vice versa.
     
    314314                                // aside from the assert false.
    315315                                assertf(false, "Mangler_old should not visit typedecl: %s", toCString(decl));
    316                                 assertf( decl->get_kind() < TypeDecl::NUMBER_OF_KINDS, "Unhandled type variable kind: %d", decl->get_kind() );
    317                                 mangleName << Encoding::typeVariables[ decl->get_kind() ] << ( decl->name.length() ) << decl->name;
     316                                assertf( decl->kind < TypeDecl::NUMBER_OF_KINDS, "Unhandled type variable kind: %d", decl->kind );
     317                                mangleName << Encoding::typeVariables[ decl->kind ] << ( decl->name.length() ) << decl->name;
    318318                        }
    319319
     
    324324                        }
    325325
    326                         void Mangler_old::printQualifiers( Type * type ) {
     326                        void Mangler_old::printQualifiers( const Type * type ) {
    327327                                // skip if not including qualifiers
    328328                                if ( typeMode ) return;
    329                                 if ( ! type->get_forall().empty() ) {
     329                                if ( ! type->forall.empty() ) {
    330330                                        std::list< std::string > assertionNames;
    331331                                        int dcount = 0, fcount = 0, vcount = 0, acount = 0;
    332332                                        mangleName << Encoding::forall;
    333                                         for ( Type::ForallList::iterator i = type->forall.begin(); i != type->forall.end(); ++i ) {
    334                                                 switch ( (*i)->get_kind() ) {
     333                                        for ( const TypeDecl * i : type->forall ) {
     334                                                switch ( i->kind ) {
    335335                                                  case TypeDecl::Dtype:
    336336                                                        dcount++;
     
    345345                                                        assert( false );
    346346                                                } // switch
    347                                                 varNums[ (*i)->name ] = std::make_pair( nextVarNum, (int)(*i)->get_kind() );
    348                                                 for ( std::list< DeclarationWithType* >::iterator assert = (*i)->assertions.begin(); assert != (*i)->assertions.end(); ++assert ) {
    349                                                         PassVisitor<Mangler_old> sub_mangler( 
     347                                                varNums[ i->name ] = std::make_pair( nextVarNum, (int)i->kind );
     348                                                for ( const DeclarationWithType * assert : i->assertions ) {
     349                                                        PassVisitor<Mangler_old> sub_mangler(
    350350                                                                mangleOverridable, typeMode, mangleGenericParams, nextVarNum, varNums );
    351                                                         (*assert)->accept( sub_mangler );
     351                                                        assert->accept( sub_mangler );
    352352                                                        assertionNames.push_back( sub_mangler.pass.get_mangleName() );
    353353                                                        acount++;
     
    436436
    437437                  private:
    438                         Mangler_new( bool mangleOverridable, bool typeMode, bool mangleGenericParams, 
     438                        Mangler_new( bool mangleOverridable, bool typeMode, bool mangleGenericParams,
    439439                                int nextVarNum, const VarMapType& varNums );
    440440                        friend class ast::Pass<Mangler_new>;
     
    457457        namespace {
    458458                Mangler_new::Mangler_new( Mangle::Mode mode )
    459                         : nextVarNum( 0 ), isTopLevel( true ), 
     459                        : nextVarNum( 0 ), isTopLevel( true ),
    460460                        mangleOverridable  ( ! mode.no_overrideable   ),
    461                         typeMode           (   mode.type              ), 
     461                        typeMode           (   mode.type              ),
    462462                        mangleGenericParams( ! mode.no_generic_params ) {}
    463                
    464                 Mangler_new::Mangler_new( bool mangleOverridable, bool typeMode, bool mangleGenericParams, 
     463
     464                Mangler_new::Mangler_new( bool mangleOverridable, bool typeMode, bool mangleGenericParams,
    465465                        int nextVarNum, const VarMapType& varNums )
    466                         : varNums( varNums ), nextVarNum( nextVarNum ), isTopLevel( false ), 
    467                         mangleOverridable( mangleOverridable ), typeMode( typeMode ), 
     466                        : varNums( varNums ), nextVarNum( nextVarNum ), isTopLevel( false ),
     467                        mangleOverridable( mangleOverridable ), typeMode( typeMode ),
    468468                        mangleGenericParams( mangleGenericParams ) {}
    469469
     
    693693                                                varNums[ decl->name ] = std::make_pair( nextVarNum, (int)decl->kind );
    694694                                                for ( const ast::DeclWithType * assert : decl->assertions ) {
    695                                                         ast::Pass<Mangler_new> sub_mangler( 
     695                                                        ast::Pass<Mangler_new> sub_mangler(
    696696                                                                mangleOverridable, typeMode, mangleGenericParams, nextVarNum, varNums );
    697697                                                        assert->accept( sub_mangler );
  • src/SymTab/Mangler.h

    r1f1c102 rf53acdf8  
    4040        namespace Mangler {
    4141                /// Mangle syntax tree object; primary interface to clients
    42                 std::string mangle( BaseSyntaxNode * decl, bool mangleOverridable = true, bool typeMode = false, bool mangleGenericParams = true );
     42                std::string mangle( const BaseSyntaxNode * decl, bool mangleOverridable = true, bool typeMode = false, bool mangleGenericParams = true );
    4343
    4444                /// Mangle a type name; secondary interface
    45                 std::string mangleType( Type* ty );
     45                std::string mangleType( const Type * ty );
    4646                /// Mangle ignoring generic type parameters
    47                 std::string mangleConcrete( Type* ty );
     47                std::string mangleConcrete( const Type * ty );
    4848
    4949                namespace Encoding {
  • src/SymTab/Validate.cc

    r1f1c102 rf53acdf8  
    119119
    120120          private:
    121                 template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl );
     121                template< typename AggDecl > void handleAggregate( AggDecl * aggregateDecl );
    122122
    123123                AggregateDecl * parentAggr = nullptr;
     
    134134        /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers.
    135135        struct EnumAndPointerDecay_old {
    136                 void previsit( EnumDecl *aggregateDecl );
    137                 void previsit( FunctionType *func );
     136                void previsit( EnumDecl * aggregateDecl );
     137                void previsit( FunctionType * func );
    138138        };
    139139
    140140        /// Associates forward declarations of aggregates with their definitions
    141141        struct LinkReferenceToTypes_old final : public WithIndexer, public WithGuards, public WithVisitorRef<LinkReferenceToTypes_old>, public WithShortCircuiting {
    142                 LinkReferenceToTypes_old( const Indexer *indexer );
    143                 void postvisit( TypeInstType *typeInst );
    144 
    145                 void postvisit( EnumInstType *enumInst );
    146                 void postvisit( StructInstType *structInst );
    147                 void postvisit( UnionInstType *unionInst );
    148                 void postvisit( TraitInstType *traitInst );
     142                LinkReferenceToTypes_old( const Indexer * indexer );
     143                void postvisit( TypeInstType * typeInst );
     144
     145                void postvisit( EnumInstType * enumInst );
     146                void postvisit( StructInstType * structInst );
     147                void postvisit( UnionInstType * unionInst );
     148                void postvisit( TraitInstType * traitInst );
    149149                void previsit( QualifiedType * qualType );
    150150                void postvisit( QualifiedType * qualType );
    151151
    152                 void postvisit( EnumDecl *enumDecl );
    153                 void postvisit( StructDecl *structDecl );
    154                 void postvisit( UnionDecl *unionDecl );
     152                void postvisit( EnumDecl * enumDecl );
     153                void postvisit( StructDecl * structDecl );
     154                void postvisit( UnionDecl * unionDecl );
    155155                void postvisit( TraitDecl * traitDecl );
    156156
    157                 void previsit( StructDecl *structDecl );
    158                 void previsit( UnionDecl *unionDecl );
     157                void previsit( StructDecl * structDecl );
     158                void previsit( UnionDecl * unionDecl );
    159159
    160160                void renameGenericParams( std::list< TypeDecl * > & params );
    161161
    162162          private:
    163                 const Indexer *local_indexer;
     163                const Indexer * local_indexer;
    164164
    165165                typedef std::map< std::string, std::list< EnumInstType * > > ForwardEnumsType;
     
    239239
    240240                template<typename AggDecl>
    241                 void handleAggregate( AggDecl *aggregateDecl );
     241                void handleAggregate( AggDecl * aggregateDecl );
    242242
    243243                void previsit( StructDecl * aggregateDecl );
     
    252252                static void verify( std::list< Declaration * > &translationUnit );
    253253
    254                 void previsit( FunctionDecl *funcDecl );
     254                void previsit( FunctionDecl * funcDecl );
    255255        };
    256256
     
    287287                Type::StorageClasses storageClasses;
    288288
    289                 void premutate( ObjectDecl *objectDecl );
    290                 Expression * postmutate( CompoundLiteralExpr *compLitExpr );
     289                void premutate( ObjectDecl * objectDecl );
     290                Expression * postmutate( CompoundLiteralExpr * compLitExpr );
    291291        };
    292292
     
    393393        }
    394394
    395         void validateType( Type *type, const Indexer *indexer ) {
     395        void validateType( Type * type, const Indexer * indexer ) {
    396396                PassVisitor<EnumAndPointerDecay_old> epc;
    397397                PassVisitor<LinkReferenceToTypes_old> lrt( indexer );
     
    496496        }
    497497
    498         bool shouldHoist( Declaration *decl ) {
     498        bool shouldHoist( Declaration * decl ) {
    499499                return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl ) || dynamic_cast< StaticAssertDecl * >( decl );
    500500        }
     
    515515
    516516        template< typename AggDecl >
    517         void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) {
     517        void HoistStruct::handleAggregate( AggDecl * aggregateDecl ) {
    518518                if ( parentAggr ) {
    519519                        aggregateDecl->parent = parentAggr;
     
    560560
    561561
    562         bool isTypedef( Declaration *decl ) {
     562        bool isTypedef( Declaration * decl ) {
    563563                return dynamic_cast< TypedefDecl * >( decl );
    564564        }
     
    571571
    572572        template< typename AggDecl >
    573         void EliminateTypedef::handleAggregate( AggDecl *aggregateDecl ) {
     573        void EliminateTypedef::handleAggregate( AggDecl * aggregateDecl ) {
    574574                filter( aggregateDecl->members, isTypedef, true );
    575575        }
     
    586586                // remove and delete decl stmts
    587587                filter( compoundStmt->kids, [](Statement * stmt) {
    588                         if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( stmt ) ) {
     588                        if ( DeclStmt * declStmt = dynamic_cast< DeclStmt * >( stmt ) ) {
    589589                                if ( dynamic_cast< TypedefDecl * >( declStmt->decl ) ) {
    590590                                        return true;
     
    595595        }
    596596
    597         void EnumAndPointerDecay_old::previsit( EnumDecl *enumDecl ) {
     597        void EnumAndPointerDecay_old::previsit( EnumDecl * enumDecl ) {
    598598                // Set the type of each member of the enumeration to be EnumConstant
    599599                for ( std::list< Declaration * >::iterator i = enumDecl->members.begin(); i != enumDecl->members.end(); ++i ) {
    600                         ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i );
     600                        ObjectDecl * obj = dynamic_cast< ObjectDecl * >( * i );
    601601                        assert( obj );
    602602                        obj->set_type( new EnumInstType( Type::Qualifiers( Type::Const ), enumDecl->name ) );
     
    627627        }
    628628
    629         void EnumAndPointerDecay_old::previsit( FunctionType *func ) {
     629        void EnumAndPointerDecay_old::previsit( FunctionType * func ) {
    630630                // Fix up parameters and return types
    631631                fixFunctionList( func->parameters, func->isVarArgs, func );
     
    633633        }
    634634
    635         LinkReferenceToTypes_old::LinkReferenceToTypes_old( const Indexer *other_indexer ) {
     635        LinkReferenceToTypes_old::LinkReferenceToTypes_old( const Indexer * other_indexer ) {
    636636                if ( other_indexer ) {
    637637                        local_indexer = other_indexer;
     
    641641        }
    642642
    643         void LinkReferenceToTypes_old::postvisit( EnumInstType *enumInst ) {
    644                 EnumDecl *st = local_indexer->lookupEnum( enumInst->name );
     643        void LinkReferenceToTypes_old::postvisit( EnumInstType * enumInst ) {
     644                const EnumDecl * st = local_indexer->lookupEnum( enumInst->name );
    645645                // it's not a semantic error if the enum is not found, just an implicit forward declaration
    646646                if ( st ) {
    647                         enumInst->baseEnum = st;
     647                        enumInst->baseEnum = const_cast<EnumDecl *>(st); // Just linking in the node
    648648                } // if
    649649                if ( ! st || ! st->body ) {
     
    661661        }
    662662
    663         void LinkReferenceToTypes_old::postvisit( StructInstType *structInst ) {
    664                 StructDecl *st = local_indexer->lookupStruct( structInst->name );
     663        void LinkReferenceToTypes_old::postvisit( StructInstType * structInst ) {
     664                const StructDecl * st = local_indexer->lookupStruct( structInst->name );
    665665                // it's not a semantic error if the struct is not found, just an implicit forward declaration
    666666                if ( st ) {
    667                         structInst->baseStruct = st;
     667                        structInst->baseStruct = const_cast<StructDecl *>(st); // Just linking in the node
    668668                } // if
    669669                if ( ! st || ! st->body ) {
     
    674674        }
    675675
    676         void LinkReferenceToTypes_old::postvisit( UnionInstType *unionInst ) {
    677                 UnionDecl *un = local_indexer->lookupUnion( unionInst->name );
     676        void LinkReferenceToTypes_old::postvisit( UnionInstType * unionInst ) {
     677                const UnionDecl * un = local_indexer->lookupUnion( unionInst->name );
    678678                // it's not a semantic error if the union is not found, just an implicit forward declaration
    679679                if ( un ) {
    680                         unionInst->baseUnion = un;
     680                        unionInst->baseUnion = const_cast<UnionDecl *>(un); // Just linking in the node
    681681                } // if
    682682                if ( ! un || ! un->body ) {
     
    693693        void LinkReferenceToTypes_old::postvisit( QualifiedType * qualType ) {
    694694                // linking only makes sense for the 'oldest ancestor' of the qualified type
    695                 qualType->parent->accept( *visitor );
     695                qualType->parent->accept( * visitor );
    696696        }
    697697
     
    762762        void LinkReferenceToTypes_old::postvisit( TraitInstType * traitInst ) {
    763763                // handle other traits
    764                 TraitDecl *traitDecl = local_indexer->lookupTrait( traitInst->name );
     764                const TraitDecl * traitDecl = local_indexer->lookupTrait( traitInst->name );
    765765                if ( ! traitDecl ) {
    766766                        SemanticError( traitInst->location, "use of undeclared trait " + traitInst->name );
     
    769769                        SemanticError( traitInst, "incorrect number of trait parameters: " );
    770770                } // if
    771                 traitInst->baseTrait = traitDecl;
     771                traitInst->baseTrait = const_cast<TraitDecl *>(traitDecl); // Just linking in the node
    772772
    773773                // need to carry over the 'sized' status of each decl in the instance
     
    786786        }
    787787
    788         void LinkReferenceToTypes_old::postvisit( EnumDecl *enumDecl ) {
     788        void LinkReferenceToTypes_old::postvisit( EnumDecl * enumDecl ) {
    789789                // visit enum members first so that the types of self-referencing members are updated properly
    790790                if ( enumDecl->body ) {
     
    792792                        if ( fwds != forwardEnums.end() ) {
    793793                                for ( std::list< EnumInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
    794                                         (*inst)->baseEnum = enumDecl;
     794                                        (* inst)->baseEnum = enumDecl;
    795795                                } // for
    796796                                forwardEnums.erase( fwds );
     
    834834        }
    835835
    836         void LinkReferenceToTypes_old::postvisit( StructDecl *structDecl ) {
     836        void LinkReferenceToTypes_old::postvisit( StructDecl * structDecl ) {
    837837                // visit struct members first so that the types of self-referencing members are updated properly
    838838                // xxx - need to ensure that type parameters match up between forward declarations and definition (most importantly, number of type parameters and their defaults)
     
    841841                        if ( fwds != forwardStructs.end() ) {
    842842                                for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
    843                                         (*inst)->baseStruct = structDecl;
     843                                        (* inst)->baseStruct = structDecl;
    844844                                } // for
    845845                                forwardStructs.erase( fwds );
     
    848848        }
    849849
    850         void LinkReferenceToTypes_old::postvisit( UnionDecl *unionDecl ) {
     850        void LinkReferenceToTypes_old::postvisit( UnionDecl * unionDecl ) {
    851851                if ( unionDecl->body ) {
    852852                        ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->name );
    853853                        if ( fwds != forwardUnions.end() ) {
    854854                                for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
    855                                         (*inst)->baseUnion = unionDecl;
     855                                        (* inst)->baseUnion = unionDecl;
    856856                                } // for
    857857                                forwardUnions.erase( fwds );
     
    860860        }
    861861
    862         void LinkReferenceToTypes_old::postvisit( TypeInstType *typeInst ) {
     862        void LinkReferenceToTypes_old::postvisit( TypeInstType * typeInst ) {
    863863                // ensure generic parameter instances are renamed like the base type
    864864                if ( inGeneric && typeInst->baseType ) typeInst->name = typeInst->baseType->name;
    865                 if ( NamedTypeDecl *namedTypeDecl = local_indexer->lookupType( typeInst->name ) ) {
    866                         if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
    867                                 typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
     865                if ( const NamedTypeDecl * namedTypeDecl = local_indexer->lookupType( typeInst->name ) ) {
     866                        if ( const TypeDecl * typeDecl = dynamic_cast< const TypeDecl * >( namedTypeDecl ) ) {
     867                                typeInst->set_isFtype( typeDecl->kind == TypeDecl::Ftype );
    868868                        } // if
    869869                } // if
     
    877877                        // expand trait instances into their members
    878878                        for ( DeclarationWithType * assertion : asserts ) {
    879                                 if ( TraitInstType *traitInst = dynamic_cast< TraitInstType * >( assertion->get_type() ) ) {
     879                                if ( TraitInstType * traitInst = dynamic_cast< TraitInstType * >( assertion->get_type() ) ) {
    880880                                        // expand trait instance into all of its members
    881881                                        expandAssertions( traitInst, back_inserter( type->assertions ) );
     
    897897        }
    898898
    899         void ForallPointerDecay_old::previsit( ObjectDecl *object ) {
     899        void ForallPointerDecay_old::previsit( ObjectDecl * object ) {
    900900                // ensure that operator names only apply to functions or function pointers
    901901                if ( CodeGen::isOperator( object->name ) && ! dynamic_cast< FunctionType * >( object->type->stripDeclarator() ) ) {
     
    905905        }
    906906
    907         void ForallPointerDecay_old::previsit( FunctionDecl *func ) {
     907        void ForallPointerDecay_old::previsit( FunctionDecl * func ) {
    908908                func->fixUniqueId();
    909909        }
     
    961961        Type * ReplaceTypedef::postmutate( QualifiedType * qualType ) {
    962962                // replacing typedefs only makes sense for the 'oldest ancestor' of the qualified type
    963                 qualType->parent = qualType->parent->acceptMutator( *visitor );
     963                qualType->parent = qualType->parent->acceptMutator( * visitor );
    964964                return qualType;
    965965        }
     
    970970                TypedefMap::const_iterator def = typedefNames.find( typeInst->name );
    971971                if ( def != typedefNames.end() ) {
    972                         Type *ret = def->second.first->base->clone();
     972                        Type * ret = def->second.first->base->clone();
    973973                        ret->location = typeInst->location;
    974974                        ret->get_qualifiers() |= typeInst->get_qualifiers();
     
    982982                        // place instance parameters on the typedef'd type
    983983                        if ( ! typeInst->parameters.empty() ) {
    984                                 ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret);
     984                                ReferenceToType * rtt = dynamic_cast<ReferenceToType *>(ret);
    985985                                if ( ! rtt ) {
    986986                                        SemanticError( typeInst->location, "Cannot apply type parameters to base type of " + typeInst->name );
     
    988988                                rtt->parameters.clear();
    989989                                cloneAll( typeInst->parameters, rtt->parameters );
    990                                 mutateAll( rtt->parameters, *visitor );  // recursively fix typedefs on parameters
     990                                mutateAll( rtt->parameters, * visitor );  // recursively fix typedefs on parameters
    991991                        } // if
    992992                        delete typeInst;
     
    10431043                //    struct screen;
    10441044                // because the expansion of the typedef is:
    1045                 //    void rtn( SCREEN *p ) => void rtn( struct screen *p )
     1045                //    void rtn( SCREEN * p ) => void rtn( struct screen * p )
    10461046                // hence the type-name "screen" must be defined.
    10471047                // Note, qualifiers on the typedef are superfluous for the forward declaration.
    10481048
    1049                 Type *designatorType = tyDecl->base->stripDeclarator();
    1050                 if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( designatorType ) ) {
     1049                Type * designatorType = tyDecl->base->stripDeclarator();
     1050                if ( StructInstType * aggDecl = dynamic_cast< StructInstType * >( designatorType ) ) {
    10511051                        declsToAddBefore.push_back( new StructDecl( aggDecl->name, DeclarationNode::Struct, noAttributes, tyDecl->linkage ) );
    1052                 } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( designatorType ) ) {
     1052                } else if ( UnionInstType * aggDecl = dynamic_cast< UnionInstType * >( designatorType ) ) {
    10531053                        declsToAddBefore.push_back( new UnionDecl( aggDecl->name, noAttributes, tyDecl->linkage ) );
    1054                 } else if ( EnumInstType *enumDecl = dynamic_cast< EnumInstType * >( designatorType ) ) {
     1054                } else if ( EnumInstType * enumDecl = dynamic_cast< EnumInstType * >( designatorType ) ) {
    10551055                        declsToAddBefore.push_back( new EnumDecl( enumDecl->name, noAttributes, tyDecl->linkage ) );
    10561056                } // if
     
    10781078
    10791079        DeclarationWithType * ReplaceTypedef::postmutate( ObjectDecl * objDecl ) {
    1080                 if ( FunctionType *funtype = dynamic_cast<FunctionType *>( objDecl->type ) ) { // function type?
     1080                if ( FunctionType * funtype = dynamic_cast<FunctionType *>( objDecl->type ) ) { // function type?
    10811081                        // replace the current object declaration with a function declaration
    10821082                        FunctionDecl * newDecl = new FunctionDecl( objDecl->name, objDecl->get_storageClasses(), objDecl->linkage, funtype, 0, objDecl->attributes, objDecl->get_funcSpec() );
     
    11041104        void ReplaceTypedef::addImplicitTypedef( AggDecl * aggDecl ) {
    11051105                if ( typedefNames.count( aggDecl->get_name() ) == 0 ) {
    1106                         Type *type = nullptr;
     1106                        Type * type = nullptr;
    11071107                        if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( aggDecl ) ) {
    11081108                                type = new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() );
     
    11301130                GuardScope( typedefNames );
    11311131                GuardScope( typedeclNames );
    1132                 mutateAll( aggr->parameters, *visitor );
     1132                mutateAll( aggr->parameters, * visitor );
    11331133
    11341134                // unroll mutateAll for aggr->members so that implicit typedefs for nested types are added to the aggregate body.
     
    11371137
    11381138                        try {
    1139                                 *i = maybeMutate( *i, *visitor );
     1139                                * i = maybeMutate( * i, * visitor );
    11401140                        } catch ( SemanticErrorException &e ) {
    11411141                                errors.append( e );
     
    12171217                        for ( size_t i = 0; paramIter != params->end(); ++paramIter, ++i ) {
    12181218                                if ( i < args.size() ) {
    1219                                         TypeExpr * expr = strict_dynamic_cast< TypeExpr * >( *std::next( args.begin(), i ) );
    1220                                         sub.add( (*paramIter)->get_name(), expr->get_type()->clone() );
     1219                                        TypeExpr * expr = strict_dynamic_cast< TypeExpr * >( * std::next( args.begin(), i ) );
     1220                                        sub.add( (* paramIter)->get_name(), expr->get_type()->clone() );
    12211221                                } else if ( i == args.size() ) {
    1222                                         Type * defaultType = (*paramIter)->get_init();
     1222                                        Type * defaultType = (* paramIter)->get_init();
    12231223                                        if ( defaultType ) {
    12241224                                                args.push_back( new TypeExpr( defaultType->clone() ) );
    1225                                                 sub.add( (*paramIter)->get_name(), defaultType->clone() );
     1225                                                sub.add( (* paramIter)->get_name(), defaultType->clone() );
    12261226                                        }
    12271227                                }
     
    12421242        }
    12431243
    1244         void CompoundLiteral::premutate( ObjectDecl *objectDecl ) {
     1244        void CompoundLiteral::premutate( ObjectDecl * objectDecl ) {
    12451245                storageClasses = objectDecl->get_storageClasses();
    12461246        }
    12471247
    1248         Expression *CompoundLiteral::postmutate( CompoundLiteralExpr *compLitExpr ) {
     1248        Expression * CompoundLiteral::postmutate( CompoundLiteralExpr * compLitExpr ) {
    12491249                // transform [storage_class] ... (struct S){ 3, ... };
    12501250                // into [storage_class] struct S temp =  { 3, ... };
    12511251                static UniqueName indexName( "_compLit" );
    12521252
    1253                 ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageClasses, LinkageSpec::C, nullptr, compLitExpr->get_result(), compLitExpr->get_initializer() );
     1253                ObjectDecl * tempvar = new ObjectDecl( indexName.newName(), storageClasses, LinkageSpec::C, nullptr, compLitExpr->get_result(), compLitExpr->get_initializer() );
    12541254                compLitExpr->set_result( nullptr );
    12551255                compLitExpr->set_initializer( nullptr );
     
    12891289                        TupleType * tupleType = strict_dynamic_cast< TupleType * >( ResolvExpr::extractResultType( ftype ) );
    12901290                        // ensure return value is not destructed by explicitly creating an empty ListInit node wherein maybeConstruct is false.
    1291                         ObjectDecl * newRet = new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, tupleType, new ListInit( std::list<Initializer*>(), noDesignators, false ) );
     1291                        ObjectDecl * newRet = new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, tupleType, new ListInit( std::list<Initializer *>(), noDesignators, false ) );
    12921292                        deleteAll( retVals );
    12931293                        retVals.clear();
     
    13021302
    13031303        void FixObjectType::previsit( ObjectDecl * objDecl ) {
    1304                 Type *new_type = ResolvExpr::resolveTypeof( objDecl->get_type(), indexer );
     1304                Type * new_type = ResolvExpr::resolveTypeof( objDecl->get_type(), indexer );
    13051305                new_type->get_qualifiers() -= Type::Lvalue; // even if typeof is lvalue, variable can never have lvalue-qualified type
    13061306                objDecl->set_type( new_type );
     
    13081308
    13091309        void FixObjectType::previsit( FunctionDecl * funcDecl ) {
    1310                 Type *new_type = ResolvExpr::resolveTypeof( funcDecl->type, indexer );
     1310                Type * new_type = ResolvExpr::resolveTypeof( funcDecl->type, indexer );
    13111311                new_type->get_qualifiers() -= Type::Lvalue; // even if typeof is lvalue, variable can never have lvalue-qualified type
    13121312                funcDecl->set_type( new_type );
    13131313        }
    13141314
    1315         void FixObjectType::previsit( TypeDecl *typeDecl ) {
     1315        void FixObjectType::previsit( TypeDecl * typeDecl ) {
    13161316                if ( typeDecl->get_base() ) {
    1317                         Type *new_type = ResolvExpr::resolveTypeof( typeDecl->get_base(), indexer );
     1317                        Type * new_type = ResolvExpr::resolveTypeof( typeDecl->get_base(), indexer );
    13181318                        new_type->get_qualifiers() -= Type::Lvalue; // even if typeof is lvalue, variable can never have lvalue-qualified type
    13191319                        typeDecl->set_base( new_type );
     
    13781378
    13791379namespace {
    1380         /// Replaces enum types by int, and function/array types in function parameter and return 
     1380        /// Replaces enum types by int, and function/array types in function parameter and return
    13811381        /// lists by appropriate pointers
    13821382        struct EnumAndPointerDecay_new {
     
    13851385                        for ( unsigned i = 0; i < enumDecl->members.size(); ++i ) {
    13861386                                // build new version of object with EnumConstant
    1387                                 ast::ptr< ast::ObjectDecl > obj = 
     1387                                ast::ptr< ast::ObjectDecl > obj =
    13881388                                        enumDecl->members[i].strict_as< ast::ObjectDecl >();
    1389                                 obj.get_and_mutate()->type = 
     1389                                obj.get_and_mutate()->type =
    13901390                                        new ast::EnumInstType{ enumDecl->name, ast::CV::Const };
    1391                                
     1391
    13921392                                // set into decl
    13931393                                ast::EnumDecl * mut = mutate( enumDecl );
     
    13991399
    14001400                static const ast::FunctionType * fixFunctionList(
    1401                         const ast::FunctionType * func, 
     1401                        const ast::FunctionType * func,
    14021402                        std::vector< ast::ptr< ast::DeclWithType > > ast::FunctionType::* field,
    14031403                        ast::ArgumentFlag isVarArgs = ast::FixedArgs
    14041404                ) {
    1405                         const auto & dwts = func->*field;
     1405                        const auto & dwts = func->* field;
    14061406                        unsigned nvals = dwts.size();
    14071407                        bool hasVoid = false;
     
    14091409                                func = ast::mutate_field_index( func, field, i, fixFunction( dwts[i], hasVoid ) );
    14101410                        }
    1411                        
     1411
    14121412                        // the only case in which "void" is valid is where it is the only one in the list
    14131413                        if ( hasVoid && ( nvals > 1 || isVarArgs ) ) {
    1414                                 SemanticError( 
     1414                                SemanticError(
    14151415                                        dwts.front()->location, func, "invalid type void in function type" );
    14161416                        }
     
    14181418                        // one void is the only thing in the list, remove it
    14191419                        if ( hasVoid ) {
    1420                                 func = ast::mutate_field( 
     1420                                func = ast::mutate_field(
    14211421                                        func, field, std::vector< ast::ptr< ast::DeclWithType > >{} );
    14221422                        }
     
    14321432
    14331433        /// expand assertions from a trait instance, performing appropriate type variable substitutions
    1434         void expandAssertions( 
    1435                 const ast::TraitInstType * inst, std::vector< ast::ptr< ast::DeclWithType > > & out 
     1434        void expandAssertions(
     1435                const ast::TraitInstType * inst, std::vector< ast::ptr< ast::DeclWithType > > & out
    14361436        ) {
    14371437                assertf( inst->base, "Trait instance not linked to base trait: %s", toCString( inst ) );
    14381438
    14391439                // build list of trait members, substituting trait decl parameters for instance parameters
    1440                 ast::TypeSubstitution sub{ 
     1440                ast::TypeSubstitution sub{
    14411441                        inst->base->params.begin(), inst->base->params.end(), inst->params.begin() };
    14421442                // deliberately take ast::ptr by-value to ensure this does not mutate inst->base
     
    14491449
    14501450        /// Associates forward declarations of aggregates with their definitions
    1451         class LinkReferenceToTypes_new final 
    1452         : public ast::WithSymbolTable, public ast::WithGuards, public 
     1451        class LinkReferenceToTypes_new final
     1452        : public ast::WithSymbolTable, public ast::WithGuards, public
    14531453          ast::WithVisitorRef<LinkReferenceToTypes_new>, public ast::WithShortCircuiting {
    1454                
    1455                 // these maps of uses of forward declarations of types need to have the actual type 
    1456                 // declaration switched in *after* they have been traversed. To enable this in the
    1457                 // ast::Pass framework, any node that needs to be so mutated has mutate() called on it 
    1458                 // before it is placed in the map, properly updating its parents in the usual traversal, 
     1454
     1455                // these maps of uses of forward declarations of types need to have the actual type
     1456                // declaration switched in * after * they have been traversed. To enable this in the
     1457                // ast::Pass framework, any node that needs to be so mutated has mutate() called on it
     1458                // before it is placed in the map, properly updating its parents in the usual traversal,
    14591459                // then can have the actual mutation applied later
    14601460                using ForwardEnumsType = std::unordered_multimap< std::string, ast::EnumInstType * >;
    14611461                using ForwardStructsType = std::unordered_multimap< std::string, ast::StructInstType * >;
    14621462                using ForwardUnionsType = std::unordered_multimap< std::string, ast::UnionInstType * >;
    1463                
     1463
    14641464                const CodeLocation & location;
    14651465                const ast::SymbolTable * localSymtab;
    1466                
     1466
    14671467                ForwardEnumsType forwardEnums;
    14681468                ForwardStructsType forwardStructs;
    14691469                ForwardUnionsType forwardUnions;
    14701470
    1471                 /// true if currently in a generic type body, so that type parameter instances can be 
     1471                /// true if currently in a generic type body, so that type parameter instances can be
    14721472                /// renamed appropriately
    14731473                bool inGeneric = false;
     
    14751475        public:
    14761476                /// contstruct using running symbol table
    1477                 LinkReferenceToTypes_new( const CodeLocation & loc ) 
     1477                LinkReferenceToTypes_new( const CodeLocation & loc )
    14781478                : location( loc ), localSymtab( &symtab ) {}
    1479                
     1479
    14801480                /// construct using provided symbol table
    1481                 LinkReferenceToTypes_new( const CodeLocation & loc, const ast::SymbolTable & syms ) 
     1481                LinkReferenceToTypes_new( const CodeLocation & loc, const ast::SymbolTable & syms )
    14821482                : location( loc ), localSymtab( &syms ) {}
    14831483
     
    14851485                        // ensure generic parameter instances are renamed like the base type
    14861486                        if ( inGeneric && typeInst->base ) {
    1487                                 typeInst = ast::mutate_field( 
     1487                                typeInst = ast::mutate_field(
    14881488                                        typeInst, &ast::TypeInstType::name, typeInst->base->name );
    14891489                        }
    14901490
    1491                         if ( 
    1492                                 auto typeDecl = dynamic_cast< const ast::TypeDecl * >( 
    1493                                         localSymtab->lookupType( typeInst->name ) ) 
     1491                        if (
     1492                                auto typeDecl = dynamic_cast< const ast::TypeDecl * >(
     1493                                        localSymtab->lookupType( typeInst->name ) )
    14941494                        ) {
    14951495                                typeInst = ast::mutate_field( typeInst, &ast::TypeInstType::kind, typeDecl->kind );
     
    15171517                        for ( const ast::Expr * param : inst->params ) {
    15181518                                if ( ! dynamic_cast< const ast::TypeExpr * >( param ) ) {
    1519                                         SemanticError( 
     1519                                        SemanticError(
    15201520                                                location, inst, "Expression parameters for generic types are currently "
    15211521                                                "unsupported: " );
     
    15711571                                auto expr = traitInst->params[i].as< ast::TypeExpr >();
    15721572                                if ( ! expr ) {
    1573                                         SemanticError( 
     1573                                        SemanticError(
    15741574                                                traitInst->params[i].get(), "Expression parameters for trait instances "
    15751575                                                "are currently unsupported: " );
     
    15931593                        return traitInst;
    15941594                }
    1595                
     1595
    15961596                void previsit( const ast::QualifiedType * ) { visit_children = false; }
    1597                
     1597
    15981598                const ast::Type * postvisit( const ast::QualifiedType * qualType ) {
    15991599                        // linking only makes sense for the "oldest ancestor" of the qualified type
    1600                         return ast::mutate_field( 
    1601                                 qualType, &ast::QualifiedType::parent, qualType->parent->accept( *visitor ) );
     1600                        return ast::mutate_field(
     1601                                qualType, &ast::QualifiedType::parent, qualType->parent->accept( * visitor ) );
    16021602                }
    16031603
    16041604                const ast::Decl * postvisit( const ast::EnumDecl * enumDecl ) {
    1605                         // visit enum members first so that the types of self-referencing members are updated 
     1605                        // visit enum members first so that the types of self-referencing members are updated
    16061606                        // properly
    16071607                        if ( ! enumDecl->body ) return enumDecl;
     
    16121612                                auto inst = fwds.first;
    16131613                                do {
    1614                                         // forward decl is stored *mutably* in map, can thus be updated
     1614                                        // forward decl is stored * mutably * in map, can thus be updated
    16151615                                        inst->second->base = enumDecl;
    16161616                                } while ( ++inst != fwds.second );
    16171617                                forwardEnums.erase( fwds.first, fwds.second );
    16181618                        }
    1619                        
     1619
    16201620                        // ensure that enumerator initializers are properly set
    16211621                        for ( unsigned i = 0; i < enumDecl->members.size(); ++i ) {
    16221622                                auto field = enumDecl->members[i].strict_as< ast::ObjectDecl >();
    16231623                                if ( field->init ) {
    1624                                         // need to resolve enumerator initializers early so that other passes that 
     1624                                        // need to resolve enumerator initializers early so that other passes that
    16251625                                        // determine if an expression is constexpr have appropriate information
    16261626                                        auto init = field->init.strict_as< ast::SingleInit >();
    1627                                        
    1628                                         enumDecl = ast::mutate_field_index( 
    1629                                                 enumDecl, &ast::EnumDecl::members, i, 
    1630                                                 ast::mutate_field( field, &ast::ObjectDecl::init, 
     1627
     1628                                        enumDecl = ast::mutate_field_index(
     1629                                                enumDecl, &ast::EnumDecl::members, i,
     1630                                                ast::mutate_field( field, &ast::ObjectDecl::init,
    16311631                                                        ast::mutate_field( init, &ast::SingleInit::value,
    1632                                                                 ResolvExpr::findSingleExpression( 
     1632                                                                ResolvExpr::findSingleExpression(
    16331633                                                                        init->value, new ast::BasicType{ ast::BasicType::SignedInt },
    16341634                                                                        symtab ) ) ) );
     
    16391639                }
    16401640
    1641                 /// rename generic type parameters uniquely so that they do not conflict with user defined 
     1641                /// rename generic type parameters uniquely so that they do not conflict with user defined
    16421642                /// function forall parameters, e.g. the T in Box and the T in f, below
    16431643                ///   forall(otype T)
     
    16571657                                const ast::TypeDecl * td = aggr->params[i];
    16581658
    1659                                 aggr = ast::mutate_field_index( 
    1660                                         aggr, &AggrDecl::params, i, 
     1659                                aggr = ast::mutate_field_index(
     1660                                        aggr, &AggrDecl::params, i,
    16611661                                        ast::mutate_field( td, &ast::TypeDecl::name, "__" + td->name + "_generic_" ) );
    16621662                        }
     
    16691669
    16701670                void postvisit( const ast::StructDecl * structDecl ) {
    1671                         // visit struct members first so that the types of self-referencing members are 
     1671                        // visit struct members first so that the types of self-referencing members are
    16721672                        // updated properly
    16731673                        if ( ! structDecl->body ) return;
     
    16781678                                auto inst = fwds.first;
    16791679                                do {
    1680                                         // forward decl is stored *mutably* in map, can thus be updated
     1680                                        // forward decl is stored * mutably * in map, can thus be updated
    16811681                                        inst->second->base = structDecl;
    16821682                                } while ( ++inst != fwds.second );
     
    16901690
    16911691                void postvisit( const ast::UnionDecl * unionDecl ) {
    1692                         // visit union members first so that the types of self-referencing members are updated 
     1692                        // visit union members first so that the types of self-referencing members are updated
    16931693                        // properly
    16941694                        if ( ! unionDecl->body ) return;
     
    16991699                                auto inst = fwds.first;
    17001700                                do {
    1701                                         // forward decl is stored *mutably* in map, can thus be updated
     1701                                        // forward decl is stored * mutably * in map, can thus be updated
    17021702                                        inst->second->base = unionDecl;
    17031703                                } while ( ++inst != fwds.second );
     
    17121712                                        "number of parameters: %zd", traitDecl->params.size() );
    17131713
    1714                                 traitDecl = ast::mutate_field_index( 
    1715                                         traitDecl, &ast::TraitDecl::params, 0, 
    1716                                         ast::mutate_field( 
     1714                                traitDecl = ast::mutate_field_index(
     1715                                        traitDecl, &ast::TraitDecl::params, 0,
     1716                                        ast::mutate_field(
    17171717                                                traitDecl->params.front().get(), &ast::TypeDecl::sized, true ) );
    17181718                        }
     
    17371737                                traitDecl = mut;
    17381738                        }
    1739                        
     1739
    17401740                        return traitDecl;
    17411741                }
    17421742        };
    17431743
    1744         /// Replaces array and function types in forall lists by appropriate pointer type and assigns 
     1744        /// Replaces array and function types in forall lists by appropriate pointer type and assigns
    17451745        /// each object and function declaration a unique ID
    17461746        class ForallPointerDecay_new {
     
    17511751                const ast::ObjectDecl * previsit( const ast::ObjectDecl * obj ) {
    17521752                        // ensure that operator names only apply to functions or function pointers
    1753                         if ( 
    1754                                 CodeGen::isOperator( obj->name ) 
     1753                        if (
     1754                                CodeGen::isOperator( obj->name )
    17551755                                && ! dynamic_cast< const ast::FunctionType * >( obj->type->stripDeclarator() )
    17561756                        ) {
     
    17761776                /// Fix up assertions -- flattens assertion lists, removing all trait instances
    17771777                template< typename node_t, typename parent_t >
    1778                 static const node_t * forallFixer( 
    1779                         const CodeLocation & loc, const node_t * node, 
     1778                static const node_t * forallFixer(
     1779                        const CodeLocation & loc, const node_t * node,
    17801780                        ast::ParameterizedType::ForallList parent_t::* forallField
    17811781                ) {
    1782                         for ( unsigned i = 0; i < (node->*forallField).size(); ++i ) {
    1783                                 const ast::TypeDecl * type = (node->*forallField)[i];
     1782                        for ( unsigned i = 0; i < (node->* forallField).size(); ++i ) {
     1783                                const ast::TypeDecl * type = (node->* forallField)[i];
    17841784                                if ( type->assertions.empty() ) continue;
    17851785
     
    17891789                                // expand trait instances into their members
    17901790                                for ( const ast::DeclWithType * assn : type->assertions ) {
    1791                                         auto traitInst = 
    1792                                                 dynamic_cast< const ast::TraitInstType * >( assn->get_type() ); 
     1791                                        auto traitInst =
     1792                                                dynamic_cast< const ast::TraitInstType * >( assn->get_type() );
    17931793                                        if ( traitInst ) {
    17941794                                                // expand trait instance to all its members
     
    18311831} // anonymous namespace
    18321832
    1833 const ast::Type * validateType( 
     1833const ast::Type * validateType(
    18341834                const CodeLocation & loc, const ast::Type * type, const ast::SymbolTable & symtab ) {
    18351835        ast::Pass< EnumAndPointerDecay_new > epc;
  • src/SymTab/Validate.h

    r1f1c102 rf53acdf8  
    1919#include <list>  // for list
    2020
    21 class CodeLocation;
    22 class Declaration;
    23 class Type;
     21struct CodeLocation;
     22class  Declaration;
     23class  Type;
    2424
    2525namespace ast {
     
    3535        void validateType( Type *type, const Indexer *indexer );
    3636
    37         const ast::Type * validateType( 
     37        const ast::Type * validateType(
    3838                const CodeLocation & loc, const ast::Type * type, const ast::SymbolTable & symtab );
    3939} // namespace SymTab
Note: See TracChangeset for help on using the changeset viewer.