source: src/SymTab/Indexer.cc @ c8e4d2f8

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since c8e4d2f8 was 114bde6, checked in by Aaron Moss <a3moss@…>, 5 years ago

Trim old version of removeSpecialOverrides

  • Property mode set to 100644
File size: 24.2 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
[b8665e3]76        Indexer::Indexer() 
77        : idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable(), 
[b419abb]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 ) {
86                        ++*stats().lazy_scopes;
87                        // create rollback
88                        prevScope = std::make_shared<Indexer>( *this );
89                        // update repScope
90                        repScope = scope;
91                }
92        }
[e8032b0]93
[b8665e3]94        void Indexer::enterScope() {
95                ++scope;
[e8032b0]96
[b419abb]97                ++*stats().new_scopes;
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
105                        *this = std::move(*prevScope);  // replace with previous scope
106                }
[e8032b0]107
[b419abb]108                --scope;
[e8032b0]109        }
[17cd4eb]110
[a40d503]111        void Indexer::lookupId( const std::string &id, std::list< IdData > &out ) const {
[b419abb]112                ++*stats().lookup_calls;
[b8665e3]113                if ( ! idTable ) return;
[743fbda]114
[b419abb]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
[a08ba92]124        NamedTypeDecl *Indexer::lookupType( const std::string &id ) const {
[b419abb]125                ++*stats().lookup_calls;
[b8665e3]126                if ( ! typeTable ) return nullptr;
[b419abb]127                ++*stats().map_lookups;
[b8665e3]128                auto it = typeTable->find( id );
129                return it == typeTable->end() ? nullptr : it->second.decl;
[a08ba92]130        }
[17cd4eb]131
[a08ba92]132        StructDecl *Indexer::lookupStruct( const std::string &id ) const {
[b419abb]133                ++*stats().lookup_calls;
[b8665e3]134                if ( ! structTable ) return nullptr;
[b419abb]135                ++*stats().map_lookups;
[b8665e3]136                auto it = structTable->find( id );
137                return it == structTable->end() ? nullptr : it->second.decl;
[9a7a3b6]138        }
139
[a08ba92]140        EnumDecl *Indexer::lookupEnum( const std::string &id ) const {
[b419abb]141                ++*stats().lookup_calls;
[b8665e3]142                if ( ! enumTable ) return nullptr;
[b419abb]143                ++*stats().map_lookups;
[b8665e3]144                auto it = enumTable->find( id );
145                return it == enumTable->end() ? nullptr : it->second.decl;
[a08ba92]146        }
[17cd4eb]147
[a08ba92]148        UnionDecl *Indexer::lookupUnion( const std::string &id ) const {
[b419abb]149                ++*stats().lookup_calls;
[b8665e3]150                if ( ! unionTable ) return nullptr;
[b419abb]151                ++*stats().map_lookups;
[b8665e3]152                auto it = unionTable->find( id );
153                return it == unionTable->end() ? nullptr : it->second.decl;
[e8032b0]154        }
155
156        TraitDecl *Indexer::lookupTrait( const std::string &id ) const {
[b419abb]157                ++*stats().lookup_calls;
[b8665e3]158                if ( ! traitTable ) return nullptr;
[b419abb]159                ++*stats().map_lookups;
[b8665e3]160                auto it = traitTable->find( id );
161                return it == traitTable->end() ? nullptr : it->second.decl;
[52c2a72]162        }
163
[b419abb]164        const Indexer* Indexer::atScope( unsigned long target ) const {
165                // by lazy construction, final indexer in list has repScope 0, cannot be > target
166                // otherwise, will find first scope representing the target
[b8665e3]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
174        NamedTypeDecl *Indexer::globalLookupType( const std::string &id ) const {
175                return atScope( 0 )->lookupType( id );
[52c2a72]176        }
[743fbda]177
[b8665e3]178        StructDecl *Indexer::globalLookupStruct( const std::string &id ) const {
179                return atScope( 0 )->lookupStruct( id );
[52c2a72]180        }
[743fbda]181
[b8665e3]182        UnionDecl *Indexer::globalLookupUnion( const std::string &id ) const {
183                return atScope( 0 )->lookupUnion( id );
[52c2a72]184        }
[743fbda]185
[b8665e3]186        EnumDecl *Indexer::globalLookupEnum( const std::string &id ) const {
187                return atScope( 0 )->lookupEnum( id );
[a08ba92]188        }
[17cd4eb]189
[3f024c9]190        bool isFunction( DeclarationWithType * decl ) {
191                return GenPoly::getFunctionType( decl->get_type() );
192        }
193
194        bool isObject( DeclarationWithType * decl ) {
195                return ! isFunction( decl );
196        }
197
198        bool isDefinition( DeclarationWithType * decl ) {
199                if ( FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl ) ) {
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
[b8665e3]209       
210        bool Indexer::addedIdConflicts( 
211                        const Indexer::IdData & existing, DeclarationWithType *added, 
212                        Indexer::OnConflict handleConflicts, BaseSyntaxNode * deleteStmt ) {
[b419abb]213                // if we're giving the same name mangling to things of different types then there is
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;
[b8665e3]221                } else if ( LinkageSpec::isMangled( added->linkage ) 
222                                || ResolvExpr::typesCompatible( 
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 ) {
240                                        SemanticError( added, 
241                                                isFunction( added ) ? 
242                                                        "duplicate function definition for " : 
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
[b8665e3]257        bool Indexer::hasCompatibleCDecl( const std::string &id, const std::string &mangleName ) const {
258                if ( ! idTable ) return false;
259
[b419abb]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                }
272               
273                return false;
274        }
275
276        bool Indexer::hasIncompatibleCDecl( 
277                        const std::string &id, const std::string &mangleName ) const {
278                if ( ! idTable ) return false;
279
[b419abb]280                ++*stats().map_lookups;
[b8665e3]281                auto decls = idTable->find( id );
282                if ( decls == idTable->end() ) return false;
[bed4c63e]283
[b8665e3]284                for ( auto decl : *(decls->second) ) {
285                        // skip other scopes (hidden by this decl)
286                        if ( decl.second.scope != scope ) continue;
287                        // check for C decl with incompatible type (by manglename)
288                        if ( ! LinkageSpec::isMangled( decl.second.id->linkage ) && decl.first != mangleName ) {
289                                return true;
290                        }
291                }
292
293                return false;
294        }
295
[42f1279c]296        /// gets the base type of the first parameter; decl must be a ctor/dtor/assignment function
297        std::string getOtypeKey( FunctionDecl* function ) {
298                auto& params = function->type->parameters;
299                assert( ! params.empty() );
300                // use base type of pointer, so that qualifiers on the pointer type aren't considered.
301                Type* base = InitTweak::getPointerBase( params.front()->get_type() );
302                assert( base );
303                return Mangler::mangle( base );
304        }
305
306        /// gets the declaration for the function acting on a type specified by otype key,
307        /// nullptr if none such
308        FunctionDecl * getFunctionForOtype( DeclarationWithType * decl, const std::string& otypeKey ) {
309                FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl );
310                if ( ! func || otypeKey != getOtypeKey( func ) ) return nullptr;
311                return func;
312        }
313
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
323                // unavailable, and likewise for the assignment operator.
324
325                // only relevant on function declarations
326                FunctionDecl * function = dynamic_cast< FunctionDecl * >( data.id );
327                if ( ! function ) return true;
328                // only need to perform this check for constructors, destructors, and assignment functions
329                if ( ! CodeGen::isCtorDtorAssign( data.id->name ) ) return true;
330
331                // set up information for this type
332                bool dataIsUserDefinedFunc = ! LinkageSpec::isOverridable( function->linkage );
333                bool dataIsCopyFunc = InitTweak::isCopyFunction( function, function->name );
334                std::string dataOtypeKey = getOtypeKey( function );
335
336                if ( dataIsUserDefinedFunc && dataIsCopyFunc ) {
337                        // this is a user-defined copy function
338                        // if this is the first such, delete/remove non-user-defined overloads as needed
339                        std::vector< std::string > removed;
340                        std::vector< MangleTable::value_type > deleted;
341                        bool alreadyUserDefinedFunc = false;
342                       
343                        for ( const auto& entry : *mangleTable ) {
344                                // skip decls that aren't functions or are for the wrong type
345                                FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey );
346                                if ( ! decl ) continue;
347
348                                bool isCopyFunc = InitTweak::isCopyFunction( decl, decl->name );
349                                if ( ! LinkageSpec::isOverridable( decl->linkage ) ) {
350                                        // matching user-defined function
351                                        if ( isCopyFunc ) {
352                                                // mutation already performed, return early
353                                                return true;
354                                        } else {
355                                                // note that non-copy deletions already performed
356                                                alreadyUserDefinedFunc = true;
357                                        }
358                                } else {
359                                        // non-user-defined function; mark for deletion/removal as appropriate
360                                        if ( isCopyFunc ) {
361                                                removed.push_back( entry.first );
362                                        } else if ( ! alreadyUserDefinedFunc ) {
363                                                deleted.push_back( entry );
364                                        }
365                                }
366                        }
367
368                        // perform removals from mangle table, and deletions if necessary
369                        for ( const auto& key : removed ) {
[114bde6]370                                ++*stats().map_mutations;
[42f1279c]371                                mangleTable = mangleTable->erase( key );
372                        }
373                        if ( ! alreadyUserDefinedFunc ) for ( const auto& entry : deleted ) {
[114bde6]374                                ++*stats().map_mutations;
[42f1279c]375                                mangleTable = mangleTable->set( entry.first, IdData{ entry.second, function } );
376                        }
377                } else if ( dataIsUserDefinedFunc ) {
378                        // this is a user-defined non-copy function
379                        // if this is the first user-defined function, delete non-user-defined overloads
380                        std::vector< MangleTable::value_type > deleted;
381                       
382                        for ( const auto& entry : *mangleTable ) {
383                                // skip decls that aren't functions or are for the wrong type
384                                FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey );
385                                if ( ! decl ) continue;
386
387                                // exit early if already a matching user-defined function;
388                                // earlier function will have mutated table
389                                if ( ! LinkageSpec::isOverridable( decl->linkage ) ) return true;
390
391                                // skip mutating intrinsic functions
392                                if ( decl->linkage == LinkageSpec::Intrinsic ) continue;
393
394                                // user-defined non-copy functions do not override copy functions
395                                if ( InitTweak::isCopyFunction( decl, decl->name ) ) continue;
396
397                                // this function to be deleted after mangleTable iteration is complete
398                                deleted.push_back( entry );
399                        }
400
401                        // mark deletions to update mangle table
402                        // this needs to be a separate loop because of iterator invalidation
403                        for ( const auto& entry : deleted ) {
[114bde6]404                                ++*stats().map_mutations;
[42f1279c]405                                mangleTable = mangleTable->set( entry.first, IdData{ entry.second, function } );
406                        }
407                } else if ( function->linkage != LinkageSpec::Intrinsic ) {
408                        // this is an overridable generated function
409                        // if there already exists a matching user-defined function, delete this appropriately
410                        for ( const auto& entry : *mangleTable ) {
411                                // skip decls that aren't functions or are for the wrong type
412                                FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey );
413                                if ( ! decl ) continue;
414
415                                // skip non-user-defined functions
416                                if ( LinkageSpec::isOverridable( decl->linkage ) ) continue;
417
418                                if ( dataIsCopyFunc ) {
419                                        // remove current function if exists a user-defined copy function
420                                        // since the signatures for copy functions don't need to match exactly, using
421                                        // a delete statement is the wrong approach
422                                        if ( InitTweak::isCopyFunction( decl, decl->name ) ) return false;
423                                } else {
424                                        // mark current function deleted by first user-defined function found
425                                        data.deleteStmt = decl;
426                                        return true;
427                                }
428                        }
429                }
430               
431                // nothing (more) to fix, return true
432                return true;
433        }
434
[b8665e3]435        void Indexer::addId( 
436                        DeclarationWithType *decl, OnConflict handleConflicts, Expression * baseExpr, 
437                        BaseSyntaxNode * deleteStmt ) {
[b419abb]438                ++*stats().add_calls;
[6fc5c14]439                const std::string &name = decl->name;
[42f1279c]440                if ( name == "" ) return;
441               
[bed4c63e]442                std::string mangleName;
[6fc5c14]443                if ( LinkageSpec::isOverridable( decl->linkage ) ) {
[b8665e3]444                        // mangle the name without including the appropriate suffix, so overridable routines
445                        // are placed into the same "bucket" as their user defined versions.
[bed4c63e]446                        mangleName = Mangler::mangle( decl, false );
447                } else {
448                        mangleName = Mangler::mangle( decl );
449                } // if
450
[b8665e3]451                // this ensures that no two declarations with the same unmangled name at the same scope
452                // both have C linkage
453                if ( LinkageSpec::isMangled( decl->linkage ) ) {
[490ff5c3]454                        // Check that a Cforall declaration doesn't override any C declaration
[b8665e3]455                        if ( hasCompatibleCDecl( name, mangleName ) ) {
[a16764a6]456                                SemanticError( decl, "Cforall declaration hides C function " );
[8884112]457                        }
[b8665e3]458                } else {
459                        // NOTE: only correct if name mangling is completely isomorphic to C
460                        // type-compatibility, which it may not be.
461                        if ( hasIncompatibleCDecl( name, mangleName ) ) {
462                                SemanticError( decl, "conflicting overload of C function " );
463                        }
[bed4c63e]464                }
[8884112]465
[b8665e3]466                // ensure tables exist and add identifier
467                MangleTable::Ptr mangleTable;
468                if ( ! idTable ) {
469                        idTable = IdTable::new_ptr();
470                        mangleTable = MangleTable::new_ptr();
471                } else {
[b419abb]472                        ++*stats().map_lookups;
[b8665e3]473                        auto decls = idTable->find( name );
474                        if ( decls == idTable->end() ) {
475                                mangleTable = MangleTable::new_ptr();
476                        } else {
477                                mangleTable = decls->second;
478                                // skip in-scope repeat declarations of same identifier
[b419abb]479                                ++*stats().map_lookups;
[b8665e3]480                                auto existing = mangleTable->find( mangleName );
481                                if ( existing != mangleTable->end()
482                                                && existing->second.scope == scope
483                                                && existing->second.id ) {
484                                        if ( addedIdConflicts( existing->second, decl, handleConflicts, deleteStmt ) ) {
485                                                if ( handleConflicts.mode == OnConflict::Delete ) {
486                                                        // set delete expression for conflicting identifier
[b419abb]487                                                        lazyInitScope();
488                                                        *stats().map_mutations += 2;
[b8665e3]489                                                        idTable = idTable->set(
490                                                                name,
491                                                                mangleTable->set( 
492                                                                        mangleName, 
493                                                                        IdData{ existing->second, handleConflicts.deleteStmt } ) );
494                                                }
495                                                return;
496                                        }
497                                }
498                        }
499                }
[8884112]500
[b8665e3]501                // add/overwrite with new identifier
[b419abb]502                lazyInitScope();
[42f1279c]503                IdData data{ decl, baseExpr, deleteStmt, scope };
[114bde6]504                // Ensure that auto-generated ctor/dtor/assignment are deleted if necessary
[42f1279c]505                if ( ! removeSpecialOverrides( data, mangleTable ) ) return;
[b419abb]506                *stats().map_mutations += 2;
[42f1279c]507                idTable = idTable->set( name, mangleTable->set( mangleName, std::move(data) ) );
[e8032b0]508        }
[52c2a72]509
[0ac366b]510        void Indexer::addId( DeclarationWithType * decl, Expression * baseExpr ) {
511                // default handling of conflicts is to raise an error
[b8665e3]512                addId( decl, OnConflict::error(), baseExpr, decl->isDeleted ? decl : nullptr );
[0ac366b]513        }
514
515        void Indexer::addDeletedId( DeclarationWithType * decl, BaseSyntaxNode * deleteStmt ) {
516                // default handling of conflicts is to raise an error
[b8665e3]517                addId( decl, OnConflict::error(), nullptr, deleteStmt );
[0ac366b]518        }
519
[52c2a72]520        bool addedTypeConflicts( NamedTypeDecl *existing, NamedTypeDecl *added ) {
[ed34540]521                if ( existing->base == nullptr ) {
[52c2a72]522                        return false;
[ed34540]523                } else if ( added->base == nullptr ) {
[52c2a72]524                        return true;
525                } else {
[ed34540]526                        assert( existing->base && added->base );
527                        // typedef redeclarations are errors only if types are different
528                        if ( ! ResolvExpr::typesCompatible( existing->base, added->base, Indexer() ) ) {
529                                SemanticError( added->location, "redeclaration of " + added->name );
530                        }
[52c2a72]531                }
[b419abb]532                // does not need to be added to the table if both existing and added have a base that are
533                // the same
[ed34540]534                return true;
[52c2a72]535        }
[743fbda]536
[e8032b0]537        void Indexer::addType( NamedTypeDecl *decl ) {
[b419abb]538                ++*stats().add_calls;
[589a70b]539                const std::string &id = decl->name;
[b8665e3]540
541                if ( ! typeTable ) { 
542                        typeTable = TypeTable::new_ptr();
[52c2a72]543                } else {
[b419abb]544                        ++*stats().map_lookups;
[b8665e3]545                        auto existing = typeTable->find( id );
546                        if ( existing != typeTable->end() 
547                                && existing->second.scope == scope
548                                && addedTypeConflicts( existing->second.decl, decl ) ) return;
[52c2a72]549                }
[b8665e3]550               
[b419abb]551                lazyInitScope();
552                ++*stats().map_mutations;
[b8665e3]553                typeTable = typeTable->set( id, Scoped<NamedTypeDecl>{ decl, scope } );
[52c2a72]554        }
555
556        bool addedDeclConflicts( AggregateDecl *existing, AggregateDecl *added ) {
[b2da0574]557                if ( ! existing->body ) {
[52c2a72]558                        return false;
[b2da0574]559                } else if ( added->body ) {
[a16764a6]560                        SemanticError( added, "redeclaration of " );
[52c2a72]561                } // if
562                return true;
[e8032b0]563        }
564
565        void Indexer::addStruct( const std::string &id ) {
[52c2a72]566                addStruct( new StructDecl( id ) );
[e8032b0]567        }
[743fbda]568
[e8032b0]569        void Indexer::addStruct( StructDecl *decl ) {
[b419abb]570                ++*stats().add_calls;
[589a70b]571                const std::string &id = decl->name;
[b8665e3]572
573                if ( ! structTable ) {
574                        structTable = StructTable::new_ptr();
[52c2a72]575                } else {
[b419abb]576                        ++*stats().map_lookups;
[b8665e3]577                        auto existing = structTable->find( id );
578                        if ( existing != structTable->end() 
579                                && existing->second.scope == scope
580                                && addedDeclConflicts( existing->second.decl, decl ) ) return;
[52c2a72]581                }
[b8665e3]582
[b419abb]583                lazyInitScope();
584                ++*stats().map_mutations;
[b8665e3]585                structTable = structTable->set( id, Scoped<StructDecl>{ decl, scope } );
[e8032b0]586        }
[743fbda]587
[e8032b0]588        void Indexer::addEnum( EnumDecl *decl ) {
[b419abb]589                ++*stats().add_calls;
[589a70b]590                const std::string &id = decl->name;
[b8665e3]591
592                if ( ! enumTable ) {
593                        enumTable = EnumTable::new_ptr();
[52c2a72]594                } else {
[b419abb]595                        ++*stats().map_lookups;
[b8665e3]596                        auto existing = enumTable->find( id );
597                        if ( existing != enumTable->end() 
598                                && existing->second.scope == scope
599                                && addedDeclConflicts( existing->second.decl, decl ) ) return;
[52c2a72]600                }
[b8665e3]601               
[b419abb]602                lazyInitScope();
603                ++*stats().map_mutations;
[b8665e3]604                enumTable = enumTable->set( id, Scoped<EnumDecl>{ decl, scope } );
[e8032b0]605        }
606
607        void Indexer::addUnion( const std::string &id ) {
[52c2a72]608                addUnion( new UnionDecl( id ) );
[e8032b0]609        }
[743fbda]610
[e8032b0]611        void Indexer::addUnion( UnionDecl *decl ) {
[b419abb]612                ++*stats().add_calls;
[589a70b]613                const std::string &id = decl->name;
[b8665e3]614
615                if ( ! unionTable ) {
616                        unionTable = UnionTable::new_ptr();
[52c2a72]617                } else {
[b419abb]618                        ++*stats().map_lookups;
[b8665e3]619                        auto existing = unionTable->find( id );
620                        if ( existing != unionTable->end() 
621                                && existing->second.scope == scope
622                                && addedDeclConflicts( existing->second.decl, decl ) ) return;
[52c2a72]623                }
[b8665e3]624
[b419abb]625                lazyInitScope();
626                ++*stats().map_mutations;
[b8665e3]627                unionTable = unionTable->set( id, Scoped<UnionDecl>{ decl, scope } );
[e8032b0]628        }
[743fbda]629
[e8032b0]630        void Indexer::addTrait( TraitDecl *decl ) {
[b419abb]631                ++*stats().add_calls;
[589a70b]632                const std::string &id = decl->name;
[b8665e3]633
634                if ( ! traitTable ) {
635                        traitTable = TraitTable::new_ptr();
[52c2a72]636                } else {
[b419abb]637                        ++*stats().map_lookups;
[b8665e3]638                        auto existing = traitTable->find( id );
639                        if ( existing != traitTable->end() 
640                                && existing->second.scope == scope
641                                && addedDeclConflicts( existing->second.decl, decl ) ) return;
[52c2a72]642                }
[b8665e3]643
[b419abb]644                lazyInitScope();
645                ++*stats().map_mutations;
[b8665e3]646                traitTable = traitTable->set( id, Scoped<TraitDecl>{ decl, scope } );
[a08ba92]647        }
[17cd4eb]648
[b8665e3]649        void Indexer::addMembers( AggregateDecl * aggr, Expression * expr, 
650                        OnConflict handleConflicts ) {
[1485c1a]651                for ( Declaration * decl : aggr->members ) {
652                        if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
[0ac366b]653                                addId( dwt, handleConflicts, expr );
[1485c1a]654                                if ( dwt->name == "" ) {
655                                        Type * t = dwt->get_type()->stripReferences();
[b8665e3]656                                        if ( dynamic_cast<StructInstType*>( t ) || dynamic_cast<UnionInstType*>( t ) ) {
[1485c1a]657                                                Expression * base = expr->clone();
[a181494]658                                                ResolvExpr::Cost cost = ResolvExpr::Cost::zero; // xxx - carry this cost into the indexer as a base cost?
659                                                ResolvExpr::referenceToRvalueConversion( base, cost );
[0ac366b]660                                                addMembers( t->getAggr(), new MemberExpr( dwt, base ), handleConflicts );
[1485c1a]661                                        }
662                                }
663                        }
664                }
665        }
666
[0ac366b]667        void Indexer::addWith( std::list< Expression * > & withExprs, BaseSyntaxNode * withStmt ) {
[4670c79]668                for ( Expression * expr : withExprs ) {
[81644e0]669                        if ( expr->result ) {
[497282e]670                                AggregateDecl * aggr = expr->result->stripReferences()->getAggr();
[81644e0]671                                assertf( aggr, "WithStmt expr has non-aggregate type: %s", toString( expr->result ).c_str() );
672
[b8665e3]673                                addMembers( aggr, expr, OnConflict::deleteWith( withStmt ) );
[81644e0]674                        }
675                }
676        }
677
[5fe35d6]678        void Indexer::addIds( const std::list< DeclarationWithType * > & decls ) {
679                for ( auto d : decls ) {
680                        addId( d );
681                }
682        }
683
684        void Indexer::addTypes( const std::list< TypeDecl * > & tds ) {
685                for ( auto td : tds ) {
686                        addType( td );
687                        addIds( td->assertions );
688                }
689        }
690
691        void Indexer::addFunctionType( FunctionType * ftype ) {
692                addTypes( ftype->forall );
693                addIds( ftype->returnVals );
694                addIds( ftype->parameters );
695        }
696
[a181494]697        Expression * Indexer::IdData::combine( ResolvExpr::Cost & cost ) const {
[0ac366b]698                Expression * ret = nullptr;
[a40d503]699                if ( baseExpr ) {
700                        Expression * base = baseExpr->clone();
[a181494]701                        ResolvExpr::referenceToRvalueConversion( base, cost );
[0ac366b]702                        ret = new MemberExpr( id, base );
[a40d503]703                        // xxx - this introduces hidden environments, for now remove them.
704                        // std::swap( base->env, ret->env );
705                        delete base->env;
706                        base->env = nullptr;
707                } else {
[0ac366b]708                        ret = new VariableExpr( id );
[a40d503]709                }
[0ac366b]710                if ( deleteStmt ) ret = new DeletedExpr( ret, deleteStmt );
711                return ret;
[a40d503]712        }
[51b7345]713} // namespace SymTab
[0dd3a2f]714
715// Local Variables: //
716// tab-width: 4 //
717// mode: c++ //
718// compile-command: "make install" //
719// End: //
Note: See TracBrowser for help on using the repository browser.