source: src/SymTab/Indexer.cc @ 96812c0

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 96812c0 was 96812c0, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Minor cleanup

  • Property mode set to 100644
File size: 27.7 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
[4e06c1e]11// Last Modified By : Peter A. Buhr
[6d49ea3]12// Last Modified On : Thu Aug 17 16:08:40 2017
13// Update Count     : 20
[0dd3a2f]14//
15
[e8032b0]16#include "Indexer.h"
17
[e3e16bc]18#include <cassert>                 // for assert, strict_dynamic_cast
[30f9072]19#include <iostream>                // for operator<<, basic_ostream, ostream
20#include <string>                  // for string, operator<<, operator!=
21#include <unordered_map>           // for operator!=, unordered_map<>::const...
22#include <unordered_set>           // for unordered_set
23#include <utility>                 // for pair, make_pair, move
24
[9236060]25#include "CodeGen/OperatorTable.h" // for isCtorDtor, isCtorDtorAssign
[30f9072]26#include "Common/SemanticError.h"  // for SemanticError
27#include "Common/utility.h"        // for cloneAll
[3f024c9]28#include "GenPoly/GenPoly.h"
[30f9072]29#include "InitTweak/InitTweak.h"   // for isConstructor, isCopyFunction, isC...
30#include "Mangler.h"               // for Mangler
31#include "Parser/LinkageSpec.h"    // for isMangled, isOverridable, Spec
32#include "ResolvExpr/typeops.h"    // for typesCompatible
33#include "SynTree/Constant.h"      // for Constant
34#include "SynTree/Declaration.h"   // for DeclarationWithType, FunctionDecl
35#include "SynTree/Expression.h"    // for Expression, ImplicitCopyCtorExpr
36#include "SynTree/Initializer.h"   // for Initializer
37#include "SynTree/Statement.h"     // for CompoundStmt, Statement, ForStmt (...
38#include "SynTree/Type.h"          // for Type, StructInstType, UnionInstType
[1ba88a0]39
[522363e]40#define debugPrint(x) if ( doDebug ) { std::cerr << x; }
[51b7345]41
42namespace SymTab {
[a40d503]43        std::ostream & operator<<( std::ostream & out, const Indexer::IdData & data ) {
44                return out << "(" << data.id << "," << data.baseExpr << ")";
45        }
46
47        typedef std::unordered_map< std::string, Indexer::IdData > MangleTable;
[bed4c63e]48        typedef std::unordered_map< std::string, MangleTable > IdTable;
[52c2a72]49        typedef std::unordered_map< std::string, NamedTypeDecl* > TypeTable;
50        typedef std::unordered_map< std::string, StructDecl* > StructTable;
51        typedef std::unordered_map< std::string, EnumDecl* > EnumTable;
52        typedef std::unordered_map< std::string, UnionDecl* > UnionTable;
53        typedef std::unordered_map< std::string, TraitDecl* > TraitTable;
54
[bed4c63e]55        void dump( const IdTable &table, std::ostream &os ) {
56                for ( IdTable::const_iterator id = table.begin(); id != table.end(); ++id ) {
57                        for ( MangleTable::const_iterator mangle = id->second.begin(); mangle != id->second.end(); ++mangle ) {
58                                os << mangle->second << std::endl;
59                        }
60                }
61        }
[743fbda]62
[52c2a72]63        template< typename Decl >
64        void dump( const std::unordered_map< std::string, Decl* > &table, std::ostream &os ) {
65                for ( typename std::unordered_map< std::string, Decl* >::const_iterator it = table.begin(); it != table.end(); ++it ) {
66                        os << it->second << std::endl;
67                } // for
68        }
[743fbda]69
[e8032b0]70        struct Indexer::Impl {
[52c2a72]71                Impl( unsigned long _scope ) : refCount(1), scope( _scope ), size( 0 ), base(),
[e8032b0]72                                idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {}
[52c2a72]73                Impl( unsigned long _scope, Indexer &&_base ) : refCount(1), scope( _scope ), size( 0 ), base( _base ),
[e8032b0]74                                idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {}
75                unsigned long refCount;   ///< Number of references to these tables
[52c2a72]76                unsigned long scope;      ///< Scope these tables are associated with
[e8032b0]77                unsigned long size;       ///< Number of elements stored in this table
78                const Indexer base;       ///< Base indexer this extends
[743fbda]79
[e8032b0]80                IdTable idTable;          ///< Identifier namespace
81                TypeTable typeTable;      ///< Type namespace
82                StructTable structTable;  ///< Struct namespace
83                EnumTable enumTable;      ///< Enum namespace
84                UnionTable unionTable;    ///< Union namespace
85                TraitTable traitTable;    ///< Trait namespace
86        };
87
88        Indexer::Impl *Indexer::newRef( Indexer::Impl *toClone ) {
89                if ( ! toClone ) return 0;
90
91                // shorten the search chain by skipping empty links
92                Indexer::Impl *ret = toClone->size == 0 ? toClone->base.tables : toClone;
93                if ( ret ) { ++ret->refCount; }
94
95                return ret;
96        }
97
98        void Indexer::deleteRef( Indexer::Impl *toFree ) {
99                if ( ! toFree ) return;
100
101                if ( --toFree->refCount == 0 ) delete toFree;
102        }
103
[a40d503]104        void Indexer::removeSpecialOverrides( const std::string &id, std::list< IdData > & out ) const {
[ee1635c8]105                // only need to perform this step for constructors, destructors, and assignment functions
[bff227f]106                if ( ! CodeGen::isCtorDtorAssign( id ) ) return;
[1ba88a0]107
108                // helpful data structure
109                struct ValueType {
110                        struct DeclBall {
[a40d503]111                                IdData decl;
[1ba88a0]112                                bool isUserDefinedFunc; // properties for this particular decl
[235114f]113                                bool isDefaultCtor;
114                                bool isDtor;
[1ba88a0]115                                bool isCopyFunc;
116                        };
117                        // properties for this type
[ff03f5c]118                        bool existsUserDefinedFunc = false;    // any user-defined function found
119                        bool existsUserDefinedCtor = false;    // any user-defined constructor found
120                        bool existsUserDefinedDtor = false;    // any user-defined destructor found
121                        bool existsUserDefinedCopyFunc = false;    // user-defined copy ctor found
122                        bool existsUserDefinedDefaultCtor = false; // user-defined default ctor found
[1ba88a0]123                        std::list< DeclBall > decls;
124
125                        // another FunctionDecl for the current type was found - determine
126                        // if it has special properties and update data structure accordingly
[a40d503]127                        ValueType & operator+=( IdData data ) {
128                                DeclarationWithType * function = data.id;
[1ba88a0]129                                bool isUserDefinedFunc = ! LinkageSpec::isOverridable( function->get_linkage() );
[235114f]130                                bool isDefaultCtor = InitTweak::isDefaultConstructor( function );
131                                bool isDtor = InitTweak::isDestructor( function );
[ee1635c8]132                                bool isCopyFunc = InitTweak::isCopyFunction( function, function->get_name() );
[a40d503]133                                decls.push_back( DeclBall{ data, isUserDefinedFunc, isDefaultCtor, isDtor, isCopyFunc } );
[ff03f5c]134                                existsUserDefinedFunc = existsUserDefinedFunc || isUserDefinedFunc;
[bff227f]135                                existsUserDefinedCtor = existsUserDefinedCtor || (isUserDefinedFunc && CodeGen::isConstructor( function->get_name() ) );
[ff03f5c]136                                existsUserDefinedDtor = existsUserDefinedDtor || (isUserDefinedFunc && isDtor);
137                                existsUserDefinedCopyFunc = existsUserDefinedCopyFunc || (isUserDefinedFunc && isCopyFunc);
138                                existsUserDefinedDefaultCtor = existsUserDefinedDefaultCtor || (isUserDefinedFunc && isDefaultCtor);
[1ba88a0]139                                return *this;
140                        }
141                }; // ValueType
142
[a40d503]143                std::list< IdData > copy;
[1ba88a0]144                copy.splice( copy.end(), out );
145
146                // organize discovered declarations by type
147                std::unordered_map< std::string, ValueType > funcMap;
[a40d503]148                for ( auto decl : copy ) {
149                        if ( FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl.id ) ) {
[96812c0]150                                std::list< DeclarationWithType * > & params = function->type->parameters;
[1ba88a0]151                                assert( ! params.empty() );
[24670d2]152                                // use base type of pointer, so that qualifiers on the pointer type aren't considered.
[ce8c12f]153                                Type * base = InitTweak::getPointerBase( params.front()->get_type() );
154                                assert( base );
[a40d503]155                                funcMap[ Mangler::mangle( base ) ] += decl;
[1ba88a0]156                        } else {
157                                out.push_back( decl );
158                        }
159                }
160
[ff03f5c]161                // if a type contains user defined ctor/dtor/assign, then special rules trigger, which determine
162                // the set of ctor/dtor/assign that are seen by the requester. In particular, if the user defines
[1ba88a0]163                // a default ctor, then the generated default ctor should never be seen, likewise for copy ctor
164                // and dtor. If the user defines any ctor/dtor, then no generated field ctors should be seen.
[ff03f5c]165                // If the user defines any ctor then the generated default ctor should not be seen (intrinsic default
166                // ctor must be overridden exactly).
[1ba88a0]167                for ( std::pair< const std::string, ValueType > & pair : funcMap ) {
168                        ValueType & val = pair.second;
169                        for ( ValueType::DeclBall ball : val.decls ) {
[ff03f5c]170                                bool noUserDefinedFunc = ! val.existsUserDefinedFunc;
171                                bool isUserDefinedFunc = ball.isUserDefinedFunc;
[96812c0]172                                bool isAcceptableDefaultCtor = (! val.existsUserDefinedCtor || (! val.existsUserDefinedDefaultCtor && ball.decl.id->linkage == LinkageSpec::Intrinsic)) && ball.isDefaultCtor; // allow default constructors only when no user-defined constructors exist, except in the case of intrinsics, which require exact overrides
[ff03f5c]173                                bool isAcceptableCopyFunc = ! val.existsUserDefinedCopyFunc && ball.isCopyFunc; // handles copy ctor and assignment operator
174                                bool isAcceptableDtor = ! val.existsUserDefinedDtor && ball.isDtor;
175                                if ( noUserDefinedFunc || isUserDefinedFunc || isAcceptableDefaultCtor || isAcceptableCopyFunc || isAcceptableDtor ) {
[1ba88a0]176                                        // decl conforms to the rules described above, so it should be seen by the requester
177                                        out.push_back( ball.decl );
178                                }
179                        }
180                }
181        }
182
[e8032b0]183        void Indexer::makeWritable() {
184                if ( ! tables ) {
[52c2a72]185                        // create indexer if not yet set
186                        tables = new Indexer::Impl( scope );
187                } else if ( tables->refCount > 1 || tables->scope != scope ) {
188                        // make this indexer the base of a fresh indexer at the current scope
189                        tables = new Indexer::Impl( scope, std::move( *this ) );
[e8032b0]190                }
191        }
192
[8b11840]193        Indexer::Indexer() : tables( 0 ), scope( 0 ) {}
[e8032b0]194
[8b11840]195        Indexer::Indexer( const Indexer &that ) : doDebug( that.doDebug ), tables( newRef( that.tables ) ), scope( that.scope ) {}
[e8032b0]196
[8b11840]197        Indexer::Indexer( Indexer &&that ) : doDebug( that.doDebug ), tables( that.tables ), scope( that.scope ) {
[e8032b0]198                that.tables = 0;
199        }
200
201        Indexer::~Indexer() {
202                deleteRef( tables );
203        }
204
205        Indexer& Indexer::operator= ( const Indexer &that ) {
206                deleteRef( tables );
207
208                tables = newRef( that.tables );
[52c2a72]209                scope = that.scope;
[e8032b0]210                doDebug = that.doDebug;
211
212                return *this;
213        }
214
215        Indexer& Indexer::operator= ( Indexer &&that ) {
216                deleteRef( tables );
217
218                tables = that.tables;
[52c2a72]219                scope = that.scope;
[e8032b0]220                doDebug = that.doDebug;
[17cd4eb]221
[e8032b0]222                that.tables = 0;
223
224                return *this;
225        }
[17cd4eb]226
[a40d503]227        void Indexer::lookupId( const std::string &id, std::list< IdData > &out ) const {
[5447e09]228                std::unordered_set< std::string > foundMangleNames;
[743fbda]229
[5447e09]230                Indexer::Impl *searchTables = tables;
231                while ( searchTables ) {
232
233                        IdTable::const_iterator decls = searchTables->idTable.find( id );
234                        if ( decls != searchTables->idTable.end() ) {
235                                const MangleTable &mangleTable = decls->second;
236                                for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) {
237                                        // mark the mangled name as found, skipping this insertion if a declaration for that name has already been found
238                                        if ( foundMangleNames.insert( decl->first ).second == false ) continue;
[743fbda]239
[5447e09]240                                        out.push_back( decl->second );
241                                }
[bed4c63e]242                        }
[743fbda]243
[5447e09]244                        // get declarations from base indexers
245                        searchTables = searchTables->base.tables;
[bed4c63e]246                }
[1ba88a0]247
248                // some special functions, e.g. constructors and destructors
249                // remove autogenerated functions when they are defined so that
250                // they can never be matched
251                removeSpecialOverrides( id, out );
[a08ba92]252        }
[bdd516a]253
[a08ba92]254        NamedTypeDecl *Indexer::lookupType( const std::string &id ) const {
[e8032b0]255                if ( ! tables ) return 0;
256
[52c2a72]257                TypeTable::const_iterator ret = tables->typeTable.find( id );
258                return ret != tables->typeTable.end() ? ret->second : tables->base.lookupType( id );
[a08ba92]259        }
[17cd4eb]260
[a08ba92]261        StructDecl *Indexer::lookupStruct( const std::string &id ) const {
[e8032b0]262                if ( ! tables ) return 0;
263
[52c2a72]264                StructTable::const_iterator ret = tables->structTable.find( id );
265                return ret != tables->structTable.end() ? ret->second : tables->base.lookupStruct( id );
[a08ba92]266        }
[17cd4eb]267
[a08ba92]268        EnumDecl *Indexer::lookupEnum( const std::string &id ) const {
[e8032b0]269                if ( ! tables ) return 0;
270
[52c2a72]271                EnumTable::const_iterator ret = tables->enumTable.find( id );
272                return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnum( id );
[a08ba92]273        }
[17cd4eb]274
[a08ba92]275        UnionDecl *Indexer::lookupUnion( const std::string &id ) const {
[e8032b0]276                if ( ! tables ) return 0;
277
[52c2a72]278                UnionTable::const_iterator ret = tables->unionTable.find( id );
279                return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnion( id );
[e8032b0]280        }
281
282        TraitDecl *Indexer::lookupTrait( const std::string &id ) const {
283                if ( ! tables ) return 0;
284
[52c2a72]285                TraitTable::const_iterator ret = tables->traitTable.find( id );
286                return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTrait( id );
287        }
288
[0ac366b]289        const Indexer::IdData * Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) const {
290                if ( ! tables ) return nullptr;
291                if ( tables->scope < scope ) return nullptr;
[bed4c63e]292
293                IdTable::const_iterator decls = tables->idTable.find( id );
294                if ( decls != tables->idTable.end() ) {
295                        const MangleTable &mangleTable = decls->second;
296                        MangleTable::const_iterator decl = mangleTable.find( mangleName );
[0ac366b]297                        if ( decl != mangleTable.end() ) return &decl->second;
[bed4c63e]298                }
299
300                return tables->base.lookupIdAtScope( id, mangleName, scope );
301        }
302
[0ac366b]303        Indexer::IdData * Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) {
304                return const_cast<IdData *>(const_cast<const Indexer *>(this)->lookupIdAtScope( id, mangleName, scope ));
305        }
306
[09d1ad0]307        bool Indexer::hasIncompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const {
[bed4c63e]308                if ( ! tables ) return false;
[09d1ad0]309                if ( tables->scope < scope ) return false;
[bed4c63e]310
311                IdTable::const_iterator decls = tables->idTable.find( id );
312                if ( decls != tables->idTable.end() ) {
313                        const MangleTable &mangleTable = decls->second;
314                        for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) {
[4e06c1e]315                                // check for C decls with the same name, skipping those with a compatible type (by mangleName)
[a40d503]316                                if ( ! LinkageSpec::isMangled( decl->second.id->get_linkage() ) && decl->first != mangleName ) return true;
[bed4c63e]317                        }
318                }
319
[09d1ad0]320                return tables->base.hasIncompatibleCDecl( id, mangleName, scope );
[bed4c63e]321        }
[743fbda]322
[8884112]323        bool Indexer::hasCompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const {
324                if ( ! tables ) return false;
325                if ( tables->scope < scope ) return false;
326
327                IdTable::const_iterator decls = tables->idTable.find( id );
328                if ( decls != tables->idTable.end() ) {
329                        const MangleTable &mangleTable = decls->second;
330                        for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) {
331                                // check for C decls with the same name, skipping
332                                // those with an incompatible type (by mangleName)
[a40d503]333                                if ( ! LinkageSpec::isMangled( decl->second.id->get_linkage() ) && decl->first == mangleName ) return true;
[8884112]334                        }
335                }
336
337                return tables->base.hasCompatibleCDecl( id, mangleName, scope );
338        }
339
[52c2a72]340        NamedTypeDecl *Indexer::lookupTypeAtScope( const std::string &id, unsigned long scope ) const {
341                if ( ! tables ) return 0;
342                if ( tables->scope < scope ) return 0;
343
344                TypeTable::const_iterator ret = tables->typeTable.find( id );
345                return ret != tables->typeTable.end() ? ret->second : tables->base.lookupTypeAtScope( id, scope );
346        }
[743fbda]347
[52c2a72]348        StructDecl *Indexer::lookupStructAtScope( const std::string &id, unsigned long scope ) const {
349                if ( ! tables ) return 0;
350                if ( tables->scope < scope ) return 0;
351
352                StructTable::const_iterator ret = tables->structTable.find( id );
353                return ret != tables->structTable.end() ? ret->second : tables->base.lookupStructAtScope( id, scope );
354        }
[743fbda]355
[52c2a72]356        EnumDecl *Indexer::lookupEnumAtScope( const std::string &id, unsigned long scope ) const {
357                if ( ! tables ) return 0;
358                if ( tables->scope < scope ) return 0;
359
360                EnumTable::const_iterator ret = tables->enumTable.find( id );
361                return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnumAtScope( id, scope );
362        }
[743fbda]363
[52c2a72]364        UnionDecl *Indexer::lookupUnionAtScope( const std::string &id, unsigned long scope ) const {
365                if ( ! tables ) return 0;
366                if ( tables->scope < scope ) return 0;
367
368                UnionTable::const_iterator ret = tables->unionTable.find( id );
369                return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnionAtScope( id, scope );
370        }
[743fbda]371
[52c2a72]372        TraitDecl *Indexer::lookupTraitAtScope( const std::string &id, unsigned long scope ) const {
373                if ( ! tables ) return 0;
374                if ( tables->scope < scope ) return 0;
375
376                TraitTable::const_iterator ret = tables->traitTable.find( id );
377                return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTraitAtScope( id, scope );
[a08ba92]378        }
[17cd4eb]379
[3f024c9]380        bool isFunction( DeclarationWithType * decl ) {
381                return GenPoly::getFunctionType( decl->get_type() );
382        }
383
384        bool isObject( DeclarationWithType * decl ) {
385                return ! isFunction( decl );
386        }
387
388        bool isDefinition( DeclarationWithType * decl ) {
389                if ( FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl ) ) {
390                        // a function is a definition if it has a body
391                        return func->statements;
392                } else {
393                        // an object is a definition if it is not marked extern.
394                        // both objects must be marked extern
395                        return ! decl->get_storageClasses().is_extern;
396                }
397        }
398
[0ac366b]399        bool addedIdConflicts( Indexer::IdData & existing, DeclarationWithType *added, BaseSyntaxNode * deleteStmt, Indexer::ConflictFunction handleConflicts ) {
[bed4c63e]400                // if we're giving the same name mangling to things of different types then there is something wrong
[3f024c9]401                assert( (isObject( added ) && isObject( existing.id ) )
402                        || ( isFunction( added ) && isFunction( existing.id ) ) );
[bed4c63e]403
[0ac366b]404                if ( LinkageSpec::isOverridable( existing.id->get_linkage() ) ) {
[bed4c63e]405                        // new definition shadows the autogenerated one, even at the same scope
406                        return false;
[0ac366b]407                } else if ( LinkageSpec::isMangled( added->get_linkage() ) || ResolvExpr::typesCompatible( added->get_type(), existing.id->get_type(), Indexer() ) ) {
408
409                        // it is a conflict if one declaration is deleted and the other is not
410                        if ( deleteStmt && ! existing.deleteStmt ) {
411                                return handleConflicts( existing, "deletion of defined identifier " );
412                        } else if ( ! deleteStmt && existing.deleteStmt ) {
413                                return handleConflicts( existing, "definition of deleted identifier " );
414                        }
415
[3f024c9]416                        if ( isDefinition( added ) && isDefinition( existing.id ) ) {
417                                if ( isFunction( added ) ) {
[0ac366b]418                                        return handleConflicts( existing, "duplicate function definition for " );
[3f024c9]419                                } else {
[0ac366b]420                                        return handleConflicts( existing, "duplicate object definition for " );
[bed4c63e]421                                } // if
422                        } // if
423                } else {
[0ac366b]424                        return handleConflicts( existing, "duplicate definition for " );
[bed4c63e]425                } // if
426
427                return true;
428        }
[743fbda]429
[0ac366b]430        void Indexer::addId( DeclarationWithType *decl, ConflictFunction handleConflicts, Expression * baseExpr, BaseSyntaxNode * deleteStmt ) {
[2cb70aa]431                if ( decl->name == "" ) return;
[33a25f9]432                debugPrint( "Adding Id " << decl->name << std::endl );
[e8032b0]433                makeWritable();
[bed4c63e]434
[6fc5c14]435                const std::string &name = decl->name;
[bed4c63e]436                std::string mangleName;
[6fc5c14]437                if ( LinkageSpec::isOverridable( decl->linkage ) ) {
[bed4c63e]438                        // mangle the name without including the appropriate suffix, so overridable routines are placed into the
439                        // same "bucket" as their user defined versions.
440                        mangleName = Mangler::mangle( decl, false );
441                } else {
442                        mangleName = Mangler::mangle( decl );
443                } // if
444
[8884112]445                // this ensures that no two declarations with the same unmangled name at the same scope both have C linkage
[6fc5c14]446                if ( ! LinkageSpec::isMangled( decl->linkage ) ) {
[8884112]447                        // NOTE this is broken in Richard's original code in such a way that it never triggers (it
448                        // doesn't check decls that have the same manglename, and all C-linkage decls are defined to
449                        // have their name as their manglename, hence the error can never trigger).
450                        // The code here is closer to correct, but name mangling would have to be completely
451                        // isomorphic to C type-compatibility, which it may not be.
452                        if ( hasIncompatibleCDecl( name, mangleName, scope ) ) {
[a16764a6]453                                SemanticError( decl, "conflicting overload of C function " );
[8884112]454                        }
455                } else {
[490ff5c3]456                        // Check that a Cforall declaration doesn't override any C declaration
[8884112]457                        if ( hasCompatibleCDecl( name, mangleName, scope ) ) {
[a16764a6]458                                SemanticError( decl, "Cforall declaration hides C function " );
[8884112]459                        }
[bed4c63e]460                }
[8884112]461
462                // Skip repeat declarations of the same identifier
[0ac366b]463                IdData * existing = lookupIdAtScope( name, mangleName, scope );
464                if ( existing && existing->id && addedIdConflicts( *existing, decl, deleteStmt, handleConflicts ) ) return;
[8884112]465
466                // add to indexer
[78d69da7]467                tables->idTable[ name ][ mangleName ] = IdData{ decl, baseExpr, deleteStmt };
[8884112]468                ++tables->size;
[e8032b0]469        }
[52c2a72]470
[0ac366b]471        void Indexer::addId( DeclarationWithType * decl, Expression * baseExpr ) {
472                // default handling of conflicts is to raise an error
[3ed994e]473                addId( decl, [decl](IdData &, const std::string & msg) { SemanticError( decl, msg ); return true; }, baseExpr, decl->isDeleted ? decl : nullptr );
[0ac366b]474        }
475
476        void Indexer::addDeletedId( DeclarationWithType * decl, BaseSyntaxNode * deleteStmt ) {
477                // default handling of conflicts is to raise an error
[a16764a6]478                addId( decl, [decl](IdData &, const std::string & msg) { SemanticError( decl, msg ); return true; }, nullptr, deleteStmt );
[0ac366b]479        }
480
[52c2a72]481        bool addedTypeConflicts( NamedTypeDecl *existing, NamedTypeDecl *added ) {
482                if ( existing->get_base() == 0 ) {
483                        return false;
484                } else if ( added->get_base() == 0 ) {
485                        return true;
486                } else {
[a16764a6]487                        SemanticError( added, "redeclaration of " );
[52c2a72]488                }
489        }
[743fbda]490
[e8032b0]491        void Indexer::addType( NamedTypeDecl *decl ) {
[33a25f9]492                debugPrint( "Adding type " << decl->name << std::endl );
[e8032b0]493                makeWritable();
[52c2a72]494
495                const std::string &id = decl->get_name();
496                TypeTable::iterator existing = tables->typeTable.find( id );
497                if ( existing == tables->typeTable.end() ) {
498                        NamedTypeDecl *parent = tables->base.lookupTypeAtScope( id, scope );
499                        if ( ! parent || ! addedTypeConflicts( parent, decl ) ) {
500                                tables->typeTable.insert( existing, std::make_pair( id, decl ) );
501                                ++tables->size;
502                        }
503                } else {
504                        if ( ! addedTypeConflicts( existing->second, decl ) ) {
505                                existing->second = decl;
506                        }
507                }
508        }
509
510        bool addedDeclConflicts( AggregateDecl *existing, AggregateDecl *added ) {
[b2da0574]511                if ( ! existing->body ) {
[52c2a72]512                        return false;
[b2da0574]513                } else if ( added->body ) {
[a16764a6]514                        SemanticError( added, "redeclaration of " );
[52c2a72]515                } // if
516                return true;
[e8032b0]517        }
518
519        void Indexer::addStruct( const std::string &id ) {
[33a25f9]520                debugPrint( "Adding fwd decl for struct " << id << std::endl );
[52c2a72]521                addStruct( new StructDecl( id ) );
[e8032b0]522        }
[743fbda]523
[e8032b0]524        void Indexer::addStruct( StructDecl *decl ) {
[33a25f9]525                debugPrint( "Adding struct " << decl->name << std::endl );
[e8032b0]526                makeWritable();
[52c2a72]527
528                const std::string &id = decl->get_name();
529                StructTable::iterator existing = tables->structTable.find( id );
530                if ( existing == tables->structTable.end() ) {
531                        StructDecl *parent = tables->base.lookupStructAtScope( id, scope );
532                        if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
533                                tables->structTable.insert( existing, std::make_pair( id, decl ) );
534                                ++tables->size;
535                        }
536                } else {
537                        if ( ! addedDeclConflicts( existing->second, decl ) ) {
538                                existing->second = decl;
539                        }
540                }
[e8032b0]541        }
[743fbda]542
[e8032b0]543        void Indexer::addEnum( EnumDecl *decl ) {
[33a25f9]544                debugPrint( "Adding enum " << decl->name << std::endl );
[e8032b0]545                makeWritable();
[52c2a72]546
547                const std::string &id = decl->get_name();
548                EnumTable::iterator existing = tables->enumTable.find( id );
549                if ( existing == tables->enumTable.end() ) {
550                        EnumDecl *parent = tables->base.lookupEnumAtScope( id, scope );
551                        if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
552                                tables->enumTable.insert( existing, std::make_pair( id, decl ) );
553                                ++tables->size;
554                        }
555                } else {
556                        if ( ! addedDeclConflicts( existing->second, decl ) ) {
557                                existing->second = decl;
558                        }
559                }
[e8032b0]560        }
561
562        void Indexer::addUnion( const std::string &id ) {
[33a25f9]563                debugPrint( "Adding fwd decl for union " << id << std::endl );
[52c2a72]564                addUnion( new UnionDecl( id ) );
[e8032b0]565        }
[743fbda]566
[e8032b0]567        void Indexer::addUnion( UnionDecl *decl ) {
[33a25f9]568                debugPrint( "Adding union " << decl->name << std::endl );
[e8032b0]569                makeWritable();
[52c2a72]570
571                const std::string &id = decl->get_name();
572                UnionTable::iterator existing = tables->unionTable.find( id );
573                if ( existing == tables->unionTable.end() ) {
574                        UnionDecl *parent = tables->base.lookupUnionAtScope( id, scope );
575                        if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
576                                tables->unionTable.insert( existing, std::make_pair( id, decl ) );
577                                ++tables->size;
578                        }
579                } else {
580                        if ( ! addedDeclConflicts( existing->second, decl ) ) {
581                                existing->second = decl;
582                        }
583                }
[e8032b0]584        }
[743fbda]585
[e8032b0]586        void Indexer::addTrait( TraitDecl *decl ) {
[33a25f9]587                debugPrint( "Adding trait " << decl->name << std::endl );
[e8032b0]588                makeWritable();
[52c2a72]589
590                const std::string &id = decl->get_name();
591                TraitTable::iterator existing = tables->traitTable.find( id );
592                if ( existing == tables->traitTable.end() ) {
593                        TraitDecl *parent = tables->base.lookupTraitAtScope( id, scope );
594                        if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
595                                tables->traitTable.insert( existing, std::make_pair( id, decl ) );
596                                ++tables->size;
597                        }
598                } else {
599                        if ( ! addedDeclConflicts( existing->second, decl ) ) {
600                                existing->second = decl;
601                        }
602                }
[a08ba92]603        }
[17cd4eb]604
[0ac366b]605        void Indexer::addMembers( AggregateDecl * aggr, Expression * expr, ConflictFunction handleConflicts ) {
[1485c1a]606                for ( Declaration * decl : aggr->members ) {
607                        if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
[0ac366b]608                                addId( dwt, handleConflicts, expr );
[1485c1a]609                                if ( dwt->name == "" ) {
610                                        Type * t = dwt->get_type()->stripReferences();
611                                        if ( dynamic_cast< StructInstType * >( t ) || dynamic_cast< UnionInstType * >( t ) ) {
612                                                Expression * base = expr->clone();
[a181494]613                                                ResolvExpr::Cost cost = ResolvExpr::Cost::zero; // xxx - carry this cost into the indexer as a base cost?
614                                                ResolvExpr::referenceToRvalueConversion( base, cost );
[0ac366b]615                                                addMembers( t->getAggr(), new MemberExpr( dwt, base ), handleConflicts );
[1485c1a]616                                        }
617                                }
618                        }
619                }
620        }
621
[0ac366b]622        void Indexer::addWith( std::list< Expression * > & withExprs, BaseSyntaxNode * withStmt ) {
[4670c79]623                for ( Expression * expr : withExprs ) {
[81644e0]624                        if ( expr->result ) {
[497282e]625                                AggregateDecl * aggr = expr->result->stripReferences()->getAggr();
[81644e0]626                                assertf( aggr, "WithStmt expr has non-aggregate type: %s", toString( expr->result ).c_str() );
627
[0ac366b]628                                addMembers( aggr, expr, [withStmt](IdData & existing, const std::string &) {
629                                        // on conflict, delete the identifier
630                                        existing.deleteStmt = withStmt;
631                                        return true;
632                                });
[81644e0]633                        }
634                }
635        }
636
[5fe35d6]637        void Indexer::addIds( const std::list< DeclarationWithType * > & decls ) {
638                for ( auto d : decls ) {
639                        addId( d );
640                }
641        }
642
643        void Indexer::addTypes( const std::list< TypeDecl * > & tds ) {
644                for ( auto td : tds ) {
645                        addType( td );
646                        addIds( td->assertions );
647                }
648        }
649
650        void Indexer::addFunctionType( FunctionType * ftype ) {
651                addTypes( ftype->forall );
652                addIds( ftype->returnVals );
653                addIds( ftype->parameters );
654        }
655
[a08ba92]656        void Indexer::enterScope() {
[52c2a72]657                ++scope;
[743fbda]658
[0dd3a2f]659                if ( doDebug ) {
[6137fbb]660                        std::cerr << "--- Entering scope " << scope << std::endl;
[0dd3a2f]661                }
[a08ba92]662        }
[17cd4eb]663
[a08ba92]664        void Indexer::leaveScope() {
[6137fbb]665                using std::cerr;
[52c2a72]666
667                assert( scope > 0 && "cannot leave initial scope" );
[6137fbb]668                if ( doDebug ) {
669                        cerr << "--- Leaving scope " << scope << " containing" << std::endl;
670                }
[52c2a72]671                --scope;
672
673                while ( tables && tables->scope > scope ) {
674                        if ( doDebug ) {
[6137fbb]675                                dump( tables->idTable, cerr );
676                                dump( tables->typeTable, cerr );
677                                dump( tables->structTable, cerr );
678                                dump( tables->enumTable, cerr );
679                                dump( tables->unionTable, cerr );
680                                dump( tables->traitTable, cerr );
[52c2a72]681                        }
682
683                        // swap tables for base table until we find one at an appropriate scope
684                        Indexer::Impl *base = newRef( tables->base.tables );
685                        deleteRef( tables );
686                        tables = base;
[0dd3a2f]687                }
[a08ba92]688        }
[17cd4eb]689
[a08ba92]690        void Indexer::print( std::ostream &os, int indent ) const {
[490ff5c3]691                using std::cerr;
[e8032b0]692
[0cb1d61]693                if ( tables ) {
694                        os << "--- scope " << tables->scope << " ---" << std::endl;
695
696                        os << "===idTable===" << std::endl;
697                        dump( tables->idTable, os );
698                        os << "===typeTable===" << std::endl;
699                        dump( tables->typeTable, os );
700                        os << "===structTable===" << std::endl;
701                        dump( tables->structTable, os );
702                        os << "===enumTable===" << std::endl;
703                        dump( tables->enumTable, os );
704                        os << "===unionTable===" << std::endl;
705                        dump( tables->unionTable, os );
706                        os << "===contextTable===" << std::endl;
707                        dump( tables->traitTable, os );
708
709                        tables->base.print( os, indent );
710                } else {
711                        os << "--- end ---" << std::endl;
712                }
[743fbda]713
[a08ba92]714        }
[a40d503]715
[a181494]716        Expression * Indexer::IdData::combine( ResolvExpr::Cost & cost ) const {
[0ac366b]717                Expression * ret = nullptr;
[a40d503]718                if ( baseExpr ) {
719                        Expression * base = baseExpr->clone();
[a181494]720                        ResolvExpr::referenceToRvalueConversion( base, cost );
[0ac366b]721                        ret = new MemberExpr( id, base );
[a40d503]722                        // xxx - this introduces hidden environments, for now remove them.
723                        // std::swap( base->env, ret->env );
724                        delete base->env;
725                        base->env = nullptr;
726                } else {
[0ac366b]727                        ret = new VariableExpr( id );
[a40d503]728                }
[0ac366b]729                if ( deleteStmt ) ret = new DeletedExpr( ret, deleteStmt );
730                return ret;
[a40d503]731        }
[51b7345]732} // namespace SymTab
[0dd3a2f]733
734// Local Variables: //
735// tab-width: 4 //
736// mode: c++ //
737// compile-command: "make install" //
738// End: //
Note: See TracBrowser for help on using the repository browser.