source: src/SymTab/Indexer.cc @ e67991f

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since e67991f was e67991f, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

WithStmt? is now a Declaration

  • Property mode set to 100644
File size: 24.6 KB
RevLine 
[0dd3a2f]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[743fbda]7// Indexer.cc --
[0dd3a2f]8//
9// Author           : Richard C. Bilson
10// Created On       : Sun May 17 21:37:33 2015
[b8665e3]11// Last Modified By : Aaron B. Moss
12// Last Modified On : Fri Mar  8 13:55:00 2019
13// Update Count     : 21
[0dd3a2f]14//
15
[e8032b0]16#include "Indexer.h"
17
[e3e16bc]18#include <cassert>                 // for assert, strict_dynamic_cast
[30f9072]19#include <string>                  // for string, operator<<, operator!=
[b8665e3]20#include <memory>                  // for shared_ptr, make_shared
[30f9072]21#include <unordered_map>           // for operator!=, unordered_map<>::const...
22#include <unordered_set>           // for unordered_set
23#include <utility>                 // for pair, make_pair, move
[42f1279c]24#include <vector>                  // for vector
[30f9072]25
[9236060]26#include "CodeGen/OperatorTable.h" // for isCtorDtor, isCtorDtorAssign
[30f9072]27#include "Common/SemanticError.h"  // for SemanticError
28#include "Common/utility.h"        // for cloneAll
[b8665e3]29#include "Common/Stats/Counter.h"  // for counters
30#include "GenPoly/GenPoly.h"       // for getFunctionType
[30f9072]31#include "InitTweak/InitTweak.h"   // for isConstructor, isCopyFunction, isC...
32#include "Mangler.h"               // for Mangler
33#include "Parser/LinkageSpec.h"    // for isMangled, isOverridable, Spec
34#include "ResolvExpr/typeops.h"    // for typesCompatible
35#include "SynTree/Constant.h"      // for Constant
36#include "SynTree/Declaration.h"   // for DeclarationWithType, FunctionDecl
37#include "SynTree/Expression.h"    // for Expression, ImplicitCopyCtorExpr
38#include "SynTree/Initializer.h"   // for Initializer
39#include "SynTree/Statement.h"     // for CompoundStmt, Statement, ForStmt (...
40#include "SynTree/Type.h"          // for Type, StructInstType, UnionInstType
[1ba88a0]41
[51b7345]42namespace SymTab {
[79de2210]43
44        // Statistics block
[b419abb]45        namespace {
46                static inline auto stats() {
47                        using namespace Stats::Counters;
48                        static auto group   = build<CounterGroup>("Indexers");
49                        static struct {
50                                SimpleCounter * count;
51                                AverageCounter<double> * size;
52                                SimpleCounter * new_scopes;
53                                SimpleCounter * lazy_scopes;
54                                AverageCounter<double> * avg_scope_depth;
55                                MaxCounter<size_t> * max_scope_depth;
56                                SimpleCounter * add_calls;
57                                SimpleCounter * lookup_calls;
58                                SimpleCounter * map_lookups;
59                                SimpleCounter * map_mutations;
60                        } ret = {
61                                .count   = build<SimpleCounter>("Count", group),
62                                .size    = build<AverageCounter<double>>("Average Size", group),
63                                .new_scopes = build<SimpleCounter>("Scopes", group),
64                                .lazy_scopes = build<SimpleCounter>("Lazy Scopes", group),
65                                .avg_scope_depth = build<AverageCounter<double>>("Average Scope", group),
66                                .max_scope_depth = build<MaxCounter<size_t>>("Max Scope", group),
67                                .add_calls = build<SimpleCounter>("Add Calls", group),
68                                .lookup_calls = build<SimpleCounter>("Lookup Calls", group),
69                                .map_lookups = build<SimpleCounter>("Map Lookups", group),
70                                .map_mutations = build<SimpleCounter>("Map Mutations", group)
71                        };
72                        return ret;
73                }
74        }
[e8032b0]75
[ef5b828]76        Indexer::Indexer()
77        : idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable(),
78          prevScope(), scope( 0 ), repScope( 0 ) { ++* stats().count; }
[e8032b0]79
[b419abb]80        Indexer::~Indexer() {
81                stats().size->push( idTable ? idTable->size() : 0 );
82        }
83
84        void Indexer::lazyInitScope() {
85                if ( repScope < scope ) {
[ef5b828]86                        ++* stats().lazy_scopes;
[b419abb]87                        // create rollback
[ef5b828]88                        prevScope = std::make_shared<Indexer>( * this );
[b419abb]89                        // update repScope
90                        repScope = scope;
91                }
92        }
[e8032b0]93
[b8665e3]94        void Indexer::enterScope() {
95                ++scope;
[e8032b0]96
[ef5b828]97                ++* stats().new_scopes;
[b419abb]98                stats().avg_scope_depth->push( scope );
99                stats().max_scope_depth->push( scope );
[e8032b0]100        }
101
[b8665e3]102        void Indexer::leaveScope() {
[b419abb]103                if ( repScope == scope ) {
104                        Ptr prev = prevScope;           // make sure prevScope stays live
[ef5b828]105                        * this = std::move(* prevScope);  // replace with previous scope
[b419abb]106                }
[e8032b0]107
[b419abb]108                --scope;
[e8032b0]109        }
[17cd4eb]110
[ef5b828]111        void Indexer::lookupId( const std::string & id, std::list< IdData > &out ) const {
112                ++* stats().lookup_calls;
[b8665e3]113                if ( ! idTable ) return;
[743fbda]114
[ef5b828]115                ++* stats().map_lookups;
[b8665e3]116                auto decls = idTable->find( id );
117                if ( decls == idTable->end() ) return;
[1ba88a0]118
[b8665e3]119                for ( auto decl : *(decls->second) ) {
120                        out.push_back( decl.second );
121                }
[a08ba92]122        }
[bdd516a]123
[8fd52e90]124        const NamedTypeDecl * Indexer::lookupType( const std::string & id ) const {
[ef5b828]125                ++* stats().lookup_calls;
[b8665e3]126                if ( ! typeTable ) return nullptr;
[ef5b828]127                ++* stats().map_lookups;
[b8665e3]128                auto it = typeTable->find( id );
129                return it == typeTable->end() ? nullptr : it->second.decl;
[a08ba92]130        }
[17cd4eb]131
[8fd52e90]132        const StructDecl * Indexer::lookupStruct( const std::string & id ) const {
[ef5b828]133                ++* stats().lookup_calls;
[b8665e3]134                if ( ! structTable ) return nullptr;
[ef5b828]135                ++* stats().map_lookups;
[b8665e3]136                auto it = structTable->find( id );
137                return it == structTable->end() ? nullptr : it->second.decl;
[9a7a3b6]138        }
139
[8fd52e90]140        const EnumDecl * Indexer::lookupEnum( const std::string & id ) const {
[ef5b828]141                ++* stats().lookup_calls;
[b8665e3]142                if ( ! enumTable ) return nullptr;
[ef5b828]143                ++* stats().map_lookups;
[b8665e3]144                auto it = enumTable->find( id );
145                return it == enumTable->end() ? nullptr : it->second.decl;
[a08ba92]146        }
[17cd4eb]147
[8fd52e90]148        const UnionDecl * Indexer::lookupUnion( const std::string & id ) const {
[ef5b828]149                ++* stats().lookup_calls;
[b8665e3]150                if ( ! unionTable ) return nullptr;
[ef5b828]151                ++* stats().map_lookups;
[b8665e3]152                auto it = unionTable->find( id );
153                return it == unionTable->end() ? nullptr : it->second.decl;
[e8032b0]154        }
155
[8fd52e90]156        const TraitDecl * Indexer::lookupTrait( const std::string & id ) const {
[ef5b828]157                ++* stats().lookup_calls;
[b8665e3]158                if ( ! traitTable ) return nullptr;
[ef5b828]159                ++* stats().map_lookups;
[b8665e3]160                auto it = traitTable->find( id );
161                return it == traitTable->end() ? nullptr : it->second.decl;
[52c2a72]162        }
163
[ef5b828]164        const Indexer * Indexer::atScope( unsigned long target ) const {
[b419abb]165                // by lazy construction, final indexer in list has repScope 0, cannot be > target
166                // otherwise, will find first scope representing the target
[ef5b828]167                const Indexer * indexer = this;
[b419abb]168                while ( indexer->repScope > target ) {
[b8665e3]169                        indexer = indexer->prevScope.get();
[8884112]170                }
[b8665e3]171                return indexer;
172        }
173
[ef5b828]174        const NamedTypeDecl * Indexer::globalLookupType( const std::string & id ) const {
[b8665e3]175                return atScope( 0 )->lookupType( id );
[52c2a72]176        }
[743fbda]177
[ef5b828]178        const StructDecl * Indexer::globalLookupStruct( const std::string & id ) const {
[b8665e3]179                return atScope( 0 )->lookupStruct( id );
[52c2a72]180        }
[743fbda]181
[ef5b828]182        const UnionDecl * Indexer::globalLookupUnion( const std::string & id ) const {
[b8665e3]183                return atScope( 0 )->lookupUnion( id );
[52c2a72]184        }
[743fbda]185
[ef5b828]186        const EnumDecl * Indexer::globalLookupEnum( const std::string & id ) const {
[b8665e3]187                return atScope( 0 )->lookupEnum( id );
[a08ba92]188        }
[17cd4eb]189
[6f096d2]190        bool isFunction( const DeclarationWithType * decl ) {
[3f024c9]191                return GenPoly::getFunctionType( decl->get_type() );
192        }
193
[6f096d2]194        bool isObject( const DeclarationWithType * decl ) {
[3f024c9]195                return ! isFunction( decl );
196        }
197
[6f096d2]198        bool isDefinition( const DeclarationWithType * decl ) {
199                if ( const FunctionDecl * func = dynamic_cast< const FunctionDecl * >( decl ) ) {
[3f024c9]200                        // a function is a definition if it has a body
201                        return func->statements;
202                } else {
203                        // an object is a definition if it is not marked extern.
204                        // both objects must be marked extern
205                        return ! decl->get_storageClasses().is_extern;
206                }
207        }
208
[ef5b828]209
210        bool Indexer::addedIdConflicts(
[6f096d2]211                        const Indexer::IdData & existing, const DeclarationWithType * added,
[e67991f]212                        Indexer::OnConflict handleConflicts, const Declaration * deleteStmt ) {
[ef5b828]213                // if we're giving the same name mangling to things of different types then there is
[b419abb]214                // something wrong
[3f024c9]215                assert( (isObject( added ) && isObject( existing.id ) )
216                        || ( isFunction( added ) && isFunction( existing.id ) ) );
[bed4c63e]217
[b8665e3]218                if ( LinkageSpec::isOverridable( existing.id->linkage ) ) {
[bed4c63e]219                        // new definition shadows the autogenerated one, even at the same scope
220                        return false;
[ef5b828]221                } else if ( LinkageSpec::isMangled( added->linkage )
222                                || ResolvExpr::typesCompatible(
[b8665e3]223                                        added->get_type(), existing.id->get_type(), Indexer() ) ) {
[0ac366b]224
225                        // it is a conflict if one declaration is deleted and the other is not
226                        if ( deleteStmt && ! existing.deleteStmt ) {
[b8665e3]227                                if ( handleConflicts.mode == OnConflict::Error ) {
228                                        SemanticError( added, "deletion of defined identifier " );
229                                }
230                                return true;
[0ac366b]231                        } else if ( ! deleteStmt && existing.deleteStmt ) {
[b8665e3]232                                if ( handleConflicts.mode == OnConflict::Error ) {
233                                        SemanticError( added, "definition of deleted identifier " );
234                                }
235                                return true;
[0ac366b]236                        }
237
[3f024c9]238                        if ( isDefinition( added ) && isDefinition( existing.id ) ) {
[b8665e3]239                                if ( handleConflicts.mode == OnConflict::Error ) {
[ef5b828]240                                        SemanticError( added,
241                                                isFunction( added ) ?
242                                                        "duplicate function definition for " :
[b8665e3]243                                                        "duplicate object definition for " );
244                                }
245                                return true;
[bed4c63e]246                        } // if
247                } else {
[b8665e3]248                        if ( handleConflicts.mode == OnConflict::Error ) {
249                                SemanticError( added, "duplicate definition for " );
250                        }
251                        return true;
[bed4c63e]252                } // if
253
254                return true;
255        }
[743fbda]256
[ef5b828]257        bool Indexer::hasCompatibleCDecl( const std::string & id, const std::string &mangleName ) const {
[b8665e3]258                if ( ! idTable ) return false;
259
[ef5b828]260                ++* stats().map_lookups;
[b8665e3]261                auto decls = idTable->find( id );
262                if ( decls == idTable->end() ) return false;
263
264                for ( auto decl : *(decls->second) ) {
265                        // skip other scopes (hidden by this decl)
266                        if ( decl.second.scope != scope ) continue;
267                        // check for C decl with compatible type (by mangleName)
268                        if ( ! LinkageSpec::isMangled( decl.second.id->linkage ) && decl.first == mangleName ) {
269                                return true;
270                        }
271                }
[ef5b828]272
[b8665e3]273                return false;
274        }
275
[6f096d2]276        bool Indexer::hasIncompatibleCDecl(const std::string & id, const std::string &mangleName ) const {
[b8665e3]277                if ( ! idTable ) return false;
278
[ef5b828]279                ++* stats().map_lookups;
[b8665e3]280                auto decls = idTable->find( id );
281                if ( decls == idTable->end() ) return false;
[bed4c63e]282
[b8665e3]283                for ( auto decl : *(decls->second) ) {
284                        // skip other scopes (hidden by this decl)
285                        if ( decl.second.scope != scope ) continue;
286                        // check for C decl with incompatible type (by manglename)
287                        if ( ! LinkageSpec::isMangled( decl.second.id->linkage ) && decl.first != mangleName ) {
288                                return true;
289                        }
290                }
291
292                return false;
293        }
294
[42f1279c]295        /// gets the base type of the first parameter; decl must be a ctor/dtor/assignment function
[6f096d2]296        std::string getOtypeKey( const FunctionDecl * function ) {
[42f1279c]297                auto& params = function->type->parameters;
298                assert( ! params.empty() );
299                // use base type of pointer, so that qualifiers on the pointer type aren't considered.
[ef5b828]300                Type * base = InitTweak::getPointerBase( params.front()->get_type() );
[42f1279c]301                assert( base );
302                return Mangler::mangle( base );
303        }
304
[ef5b828]305        /// gets the declaration for the function acting on a type specified by otype key,
[42f1279c]306        /// nullptr if none such
[6f096d2]307        const FunctionDecl * getFunctionForOtype( const DeclarationWithType * decl, const std::string& otypeKey ) {
308                const FunctionDecl * func = dynamic_cast< const FunctionDecl * >( decl );
[42f1279c]309                if ( ! func || otypeKey != getOtypeKey( func ) ) return nullptr;
310                return func;
311        }
312
[6f096d2]313        bool Indexer::removeSpecialOverrides(Indexer::IdData& data, Indexer::MangleTable::Ptr& mangleTable ) {
[ef5b828]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
[42f1279c]321                // unavailable, and likewise for the assignment operator.
322
323                // only relevant on function declarations
[6f096d2]324                const FunctionDecl * function = dynamic_cast< const FunctionDecl * >( data.id );
[42f1279c]325                if ( ! function ) return true;
326                // only need to perform this check for constructors, destructors, and assignment functions
327                if ( ! CodeGen::isCtorDtorAssign( data.id->name ) ) return true;
328
329                // set up information for this type
330                bool dataIsUserDefinedFunc = ! LinkageSpec::isOverridable( function->linkage );
331                bool dataIsCopyFunc = InitTweak::isCopyFunction( function, function->name );
332                std::string dataOtypeKey = getOtypeKey( function );
333
334                if ( dataIsUserDefinedFunc && dataIsCopyFunc ) {
335                        // this is a user-defined copy function
336                        // if this is the first such, delete/remove non-user-defined overloads as needed
337                        std::vector< std::string > removed;
338                        std::vector< MangleTable::value_type > deleted;
339                        bool alreadyUserDefinedFunc = false;
[ef5b828]340
341                        for ( const auto& entry : * mangleTable ) {
[42f1279c]342                                // skip decls that aren't functions or are for the wrong type
[6f096d2]343                                const FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey );
[42f1279c]344                                if ( ! decl ) continue;
345
346                                bool isCopyFunc = InitTweak::isCopyFunction( decl, decl->name );
347                                if ( ! LinkageSpec::isOverridable( decl->linkage ) ) {
348                                        // matching user-defined function
349                                        if ( isCopyFunc ) {
350                                                // mutation already performed, return early
351                                                return true;
352                                        } else {
353                                                // note that non-copy deletions already performed
354                                                alreadyUserDefinedFunc = true;
355                                        }
356                                } else {
357                                        // non-user-defined function; mark for deletion/removal as appropriate
358                                        if ( isCopyFunc ) {
359                                                removed.push_back( entry.first );
360                                        } else if ( ! alreadyUserDefinedFunc ) {
361                                                deleted.push_back( entry );
362                                        }
363                                }
364                        }
365
366                        // perform removals from mangle table, and deletions if necessary
367                        for ( const auto& key : removed ) {
[ef5b828]368                                ++* stats().map_mutations;
[42f1279c]369                                mangleTable = mangleTable->erase( key );
370                        }
371                        if ( ! alreadyUserDefinedFunc ) for ( const auto& entry : deleted ) {
[ef5b828]372                                ++* stats().map_mutations;
[42f1279c]373                                mangleTable = mangleTable->set( entry.first, IdData{ entry.second, function } );
374                        }
375                } else if ( dataIsUserDefinedFunc ) {
376                        // this is a user-defined non-copy function
377                        // if this is the first user-defined function, delete non-user-defined overloads
378                        std::vector< MangleTable::value_type > deleted;
[ef5b828]379
380                        for ( const auto& entry : * mangleTable ) {
[42f1279c]381                                // skip decls that aren't functions or are for the wrong type
[6f096d2]382                                const FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey );
[42f1279c]383                                if ( ! decl ) continue;
384
385                                // exit early if already a matching user-defined function;
386                                // earlier function will have mutated table
387                                if ( ! LinkageSpec::isOverridable( decl->linkage ) ) return true;
388
389                                // skip mutating intrinsic functions
390                                if ( decl->linkage == LinkageSpec::Intrinsic ) continue;
391
392                                // user-defined non-copy functions do not override copy functions
393                                if ( InitTweak::isCopyFunction( decl, decl->name ) ) continue;
394
395                                // this function to be deleted after mangleTable iteration is complete
396                                deleted.push_back( entry );
397                        }
398
399                        // mark deletions to update mangle table
400                        // this needs to be a separate loop because of iterator invalidation
401                        for ( const auto& entry : deleted ) {
[ef5b828]402                                ++* stats().map_mutations;
[42f1279c]403                                mangleTable = mangleTable->set( entry.first, IdData{ entry.second, function } );
404                        }
405                } else if ( function->linkage != LinkageSpec::Intrinsic ) {
406                        // this is an overridable generated function
407                        // if there already exists a matching user-defined function, delete this appropriately
[ef5b828]408                        for ( const auto& entry : * mangleTable ) {
[42f1279c]409                                // skip decls that aren't functions or are for the wrong type
[6f096d2]410                                const FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey );
[42f1279c]411                                if ( ! decl ) continue;
412
413                                // skip non-user-defined functions
414                                if ( LinkageSpec::isOverridable( decl->linkage ) ) continue;
415
416                                if ( dataIsCopyFunc ) {
417                                        // remove current function if exists a user-defined copy function
[ef5b828]418                                        // since the signatures for copy functions don't need to match exactly, using
[42f1279c]419                                        // a delete statement is the wrong approach
420                                        if ( InitTweak::isCopyFunction( decl, decl->name ) ) return false;
421                                } else {
422                                        // mark current function deleted by first user-defined function found
423                                        data.deleteStmt = decl;
424                                        return true;
425                                }
426                        }
427                }
[ef5b828]428
[42f1279c]429                // nothing (more) to fix, return true
430                return true;
431        }
432
[6f096d2]433        void Indexer::addId(const DeclarationWithType * decl, OnConflict handleConflicts, const Expression * baseExpr,
[e67991f]434                        const Declaration * deleteStmt ) {
[ef5b828]435                ++* stats().add_calls;
[6fc5c14]436                const std::string &name = decl->name;
[42f1279c]437                if ( name == "" ) return;
[ef5b828]438
[bed4c63e]439                std::string mangleName;
[6fc5c14]440                if ( LinkageSpec::isOverridable( decl->linkage ) ) {
[ef5b828]441                        // mangle the name without including the appropriate suffix, so overridable routines
[b8665e3]442                        // are placed into the same "bucket" as their user defined versions.
[bed4c63e]443                        mangleName = Mangler::mangle( decl, false );
444                } else {
445                        mangleName = Mangler::mangle( decl );
446                } // if
447
[ef5b828]448                // this ensures that no two declarations with the same unmangled name at the same scope
[b8665e3]449                // both have C linkage
450                if ( LinkageSpec::isMangled( decl->linkage ) ) {
[490ff5c3]451                        // Check that a Cforall declaration doesn't override any C declaration
[b8665e3]452                        if ( hasCompatibleCDecl( name, mangleName ) ) {
[a16764a6]453                                SemanticError( decl, "Cforall declaration hides C function " );
[8884112]454                        }
[b8665e3]455                } else {
[ef5b828]456                        // NOTE: only correct if name mangling is completely isomorphic to C
[b8665e3]457                        // type-compatibility, which it may not be.
458                        if ( hasIncompatibleCDecl( name, mangleName ) ) {
459                                SemanticError( decl, "conflicting overload of C function " );
460                        }
[bed4c63e]461                }
[8884112]462
[b8665e3]463                // ensure tables exist and add identifier
464                MangleTable::Ptr mangleTable;
465                if ( ! idTable ) {
466                        idTable = IdTable::new_ptr();
467                        mangleTable = MangleTable::new_ptr();
468                } else {
[ef5b828]469                        ++* stats().map_lookups;
[b8665e3]470                        auto decls = idTable->find( name );
471                        if ( decls == idTable->end() ) {
472                                mangleTable = MangleTable::new_ptr();
473                        } else {
474                                mangleTable = decls->second;
475                                // skip in-scope repeat declarations of same identifier
[ef5b828]476                                ++* stats().map_lookups;
[b8665e3]477                                auto existing = mangleTable->find( mangleName );
478                                if ( existing != mangleTable->end()
479                                                && existing->second.scope == scope
480                                                && existing->second.id ) {
481                                        if ( addedIdConflicts( existing->second, decl, handleConflicts, deleteStmt ) ) {
482                                                if ( handleConflicts.mode == OnConflict::Delete ) {
483                                                        // set delete expression for conflicting identifier
[b419abb]484                                                        lazyInitScope();
[ef5b828]485                                                        * stats().map_mutations += 2;
[b8665e3]486                                                        idTable = idTable->set(
487                                                                name,
[ef5b828]488                                                                mangleTable->set(
489                                                                        mangleName,
[b8665e3]490                                                                        IdData{ existing->second, handleConflicts.deleteStmt } ) );
491                                                }
492                                                return;
493                                        }
494                                }
495                        }
496                }
[8884112]497
[b8665e3]498                // add/overwrite with new identifier
[b419abb]499                lazyInitScope();
[42f1279c]500                IdData data{ decl, baseExpr, deleteStmt, scope };
[114bde6]501                // Ensure that auto-generated ctor/dtor/assignment are deleted if necessary
[42f1279c]502                if ( ! removeSpecialOverrides( data, mangleTable ) ) return;
[ef5b828]503                * stats().map_mutations += 2;
[42f1279c]504                idTable = idTable->set( name, mangleTable->set( mangleName, std::move(data) ) );
[e8032b0]505        }
[52c2a72]506
[6f096d2]507        void Indexer::addId( const DeclarationWithType * decl, const Expression * baseExpr ) {
[0ac366b]508                // default handling of conflicts is to raise an error
[b8665e3]509                addId( decl, OnConflict::error(), baseExpr, decl->isDeleted ? decl : nullptr );
[0ac366b]510        }
511
[e67991f]512        void Indexer::addDeletedId( const DeclarationWithType * decl, const Declaration * deleteStmt ) {
[0ac366b]513                // default handling of conflicts is to raise an error
[b8665e3]514                addId( decl, OnConflict::error(), nullptr, deleteStmt );
[0ac366b]515        }
516
[6f096d2]517        bool addedTypeConflicts( const NamedTypeDecl * existing, const NamedTypeDecl * added ) {
[ed34540]518                if ( existing->base == nullptr ) {
[52c2a72]519                        return false;
[ed34540]520                } else if ( added->base == nullptr ) {
[52c2a72]521                        return true;
522                } else {
[ed34540]523                        assert( existing->base && added->base );
524                        // typedef redeclarations are errors only if types are different
525                        if ( ! ResolvExpr::typesCompatible( existing->base, added->base, Indexer() ) ) {
526                                SemanticError( added->location, "redeclaration of " + added->name );
527                        }
[52c2a72]528                }
[ef5b828]529                // does not need to be added to the table if both existing and added have a base that are
[b419abb]530                // the same
[ed34540]531                return true;
[52c2a72]532        }
[743fbda]533
[6f096d2]534        void Indexer::addType( const NamedTypeDecl * decl ) {
[ef5b828]535                ++* stats().add_calls;
536                const std::string & id = decl->name;
[b8665e3]537
[ef5b828]538                if ( ! typeTable ) {
[b8665e3]539                        typeTable = TypeTable::new_ptr();
[52c2a72]540                } else {
[ef5b828]541                        ++* stats().map_lookups;
[b8665e3]542                        auto existing = typeTable->find( id );
[ef5b828]543                        if ( existing != typeTable->end()
544                                && existing->second.scope == scope
[b8665e3]545                                && addedTypeConflicts( existing->second.decl, decl ) ) return;
[52c2a72]546                }
[ef5b828]547
[b419abb]548                lazyInitScope();
[ef5b828]549                ++* stats().map_mutations;
[b8665e3]550                typeTable = typeTable->set( id, Scoped<NamedTypeDecl>{ decl, scope } );
[52c2a72]551        }
552
[6f096d2]553        bool addedDeclConflicts( const AggregateDecl * existing, const AggregateDecl * added ) {
[b2da0574]554                if ( ! existing->body ) {
[52c2a72]555                        return false;
[b2da0574]556                } else if ( added->body ) {
[a16764a6]557                        SemanticError( added, "redeclaration of " );
[52c2a72]558                } // if
559                return true;
[e8032b0]560        }
561
[ef5b828]562        void Indexer::addStruct( const std::string & id ) {
[52c2a72]563                addStruct( new StructDecl( id ) );
[e8032b0]564        }
[743fbda]565
[6f096d2]566        void Indexer::addStruct( const StructDecl * decl ) {
[ef5b828]567                ++* stats().add_calls;
568                const std::string & id = decl->name;
[b8665e3]569
570                if ( ! structTable ) {
571                        structTable = StructTable::new_ptr();
[52c2a72]572                } else {
[ef5b828]573                        ++* stats().map_lookups;
[b8665e3]574                        auto existing = structTable->find( id );
[ef5b828]575                        if ( existing != structTable->end()
576                                && existing->second.scope == scope
[b8665e3]577                                && addedDeclConflicts( existing->second.decl, decl ) ) return;
[52c2a72]578                }
[b8665e3]579
[b419abb]580                lazyInitScope();
[ef5b828]581                ++* stats().map_mutations;
[b8665e3]582                structTable = structTable->set( id, Scoped<StructDecl>{ decl, scope } );
[e8032b0]583        }
[743fbda]584
[6f096d2]585        void Indexer::addEnum( const EnumDecl * decl ) {
[ef5b828]586                ++* stats().add_calls;
587                const std::string & id = decl->name;
[b8665e3]588
589                if ( ! enumTable ) {
590                        enumTable = EnumTable::new_ptr();
[52c2a72]591                } else {
[ef5b828]592                        ++* stats().map_lookups;
[b8665e3]593                        auto existing = enumTable->find( id );
[ef5b828]594                        if ( existing != enumTable->end()
595                                && existing->second.scope == scope
[b8665e3]596                                && addedDeclConflicts( existing->second.decl, decl ) ) return;
[52c2a72]597                }
[ef5b828]598
[b419abb]599                lazyInitScope();
[ef5b828]600                ++* stats().map_mutations;
[b8665e3]601                enumTable = enumTable->set( id, Scoped<EnumDecl>{ decl, scope } );
[e8032b0]602        }
603
[ef5b828]604        void Indexer::addUnion( const std::string & id ) {
[52c2a72]605                addUnion( new UnionDecl( id ) );
[e8032b0]606        }
[743fbda]607
[6f096d2]608        void Indexer::addUnion( const UnionDecl * decl ) {
[ef5b828]609                ++* stats().add_calls;
610                const std::string & id = decl->name;
[b8665e3]611
612                if ( ! unionTable ) {
613                        unionTable = UnionTable::new_ptr();
[52c2a72]614                } else {
[ef5b828]615                        ++* stats().map_lookups;
[b8665e3]616                        auto existing = unionTable->find( id );
[ef5b828]617                        if ( existing != unionTable->end()
618                                && existing->second.scope == scope
[b8665e3]619                                && addedDeclConflicts( existing->second.decl, decl ) ) return;
[52c2a72]620                }
[b8665e3]621
[b419abb]622                lazyInitScope();
[ef5b828]623                ++* stats().map_mutations;
[b8665e3]624                unionTable = unionTable->set( id, Scoped<UnionDecl>{ decl, scope } );
[e8032b0]625        }
[743fbda]626
[6f096d2]627        void Indexer::addTrait( const TraitDecl * decl ) {
[ef5b828]628                ++* stats().add_calls;
629                const std::string & id = decl->name;
[b8665e3]630
631                if ( ! traitTable ) {
632                        traitTable = TraitTable::new_ptr();
[52c2a72]633                } else {
[ef5b828]634                        ++* stats().map_lookups;
[b8665e3]635                        auto existing = traitTable->find( id );
[ef5b828]636                        if ( existing != traitTable->end()
637                                && existing->second.scope == scope
[b8665e3]638                                && addedDeclConflicts( existing->second.decl, decl ) ) return;
[52c2a72]639                }
[b8665e3]640
[b419abb]641                lazyInitScope();
[ef5b828]642                ++* stats().map_mutations;
[b8665e3]643                traitTable = traitTable->set( id, Scoped<TraitDecl>{ decl, scope } );
[a08ba92]644        }
[17cd4eb]645
[6f096d2]646        void Indexer::addMembers( const AggregateDecl * aggr, const Expression * expr, OnConflict handleConflicts ) {
[1485c1a]647                for ( Declaration * decl : aggr->members ) {
648                        if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
[0ac366b]649                                addId( dwt, handleConflicts, expr );
[1485c1a]650                                if ( dwt->name == "" ) {
[6f096d2]651                                        const Type * t = dwt->get_type()->stripReferences();
652                                        if ( dynamic_cast<const StructInstType *>( t ) || dynamic_cast<const UnionInstType *>( t ) ) {
[1485c1a]653                                                Expression * base = expr->clone();
[a181494]654                                                ResolvExpr::Cost cost = ResolvExpr::Cost::zero; // xxx - carry this cost into the indexer as a base cost?
655                                                ResolvExpr::referenceToRvalueConversion( base, cost );
[0ac366b]656                                                addMembers( t->getAggr(), new MemberExpr( dwt, base ), handleConflicts );
[1485c1a]657                                        }
658                                }
659                        }
660                }
661        }
662
[e67991f]663        void Indexer::addWith( const std::list< Expression * > & withExprs, const Declaration * withStmt ) {
[6f096d2]664                for ( const Expression * expr : withExprs ) {
[81644e0]665                        if ( expr->result ) {
[497282e]666                                AggregateDecl * aggr = expr->result->stripReferences()->getAggr();
[81644e0]667                                assertf( aggr, "WithStmt expr has non-aggregate type: %s", toString( expr->result ).c_str() );
668
[b8665e3]669                                addMembers( aggr, expr, OnConflict::deleteWith( withStmt ) );
[81644e0]670                        }
671                }
672        }
673
[5fe35d6]674        void Indexer::addIds( const std::list< DeclarationWithType * > & decls ) {
675                for ( auto d : decls ) {
676                        addId( d );
677                }
678        }
679
680        void Indexer::addTypes( const std::list< TypeDecl * > & tds ) {
681                for ( auto td : tds ) {
682                        addType( td );
683                        addIds( td->assertions );
684                }
685        }
686
[6f096d2]687        void Indexer::addFunctionType( const FunctionType * ftype ) {
[5fe35d6]688                addTypes( ftype->forall );
689                addIds( ftype->returnVals );
690                addIds( ftype->parameters );
691        }
692
[a181494]693        Expression * Indexer::IdData::combine( ResolvExpr::Cost & cost ) const {
[0ac366b]694                Expression * ret = nullptr;
[a40d503]695                if ( baseExpr ) {
696                        Expression * base = baseExpr->clone();
[a181494]697                        ResolvExpr::referenceToRvalueConversion( base, cost );
[6f096d2]698                        ret = new MemberExpr( const_cast<DeclarationWithType *>(id), base );
[a40d503]699                        // xxx - this introduces hidden environments, for now remove them.
700                        // std::swap( base->env, ret->env );
701                        delete base->env;
702                        base->env = nullptr;
703                } else {
[6f096d2]704                        ret = new VariableExpr( const_cast<DeclarationWithType *>(id) );
[a40d503]705                }
[e67991f]706                if ( deleteStmt ) ret = new DeletedExpr( ret, const_cast<Declaration *>(deleteStmt) );
[0ac366b]707                return ret;
[a40d503]708        }
[51b7345]709} // namespace SymTab
[0dd3a2f]710
711// Local Variables: //
712// tab-width: 4 //
713// mode: c++ //
714// compile-command: "make install" //
715// End: //
Note: See TracBrowser for help on using the repository browser.