source: src/SymTab/Indexer.cc @ d16d159

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

Move addIds and addTypes to Indexer

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