source: src/SymTab/Indexer.cc@ bed4c63e

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since bed4c63e was bed4c63e, checked in by Aaron Moss <a3moss@…>, 10 years ago

Fold IdTable into indexer

  • Property mode set to 100644
File size: 26.4 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 : Wed Mar 2 17:31:29 2016
13// Update Count : 11
14//
15
16#include "Indexer.h"
17
18#include <string>
19#include <typeinfo>
20#include <unordered_map>
21#include <utility>
22
23#include "Mangler.h"
24
25#include "Common/utility.h"
26
27#include "ResolvExpr/typeops.h"
28
29#include "SynTree/Declaration.h"
30#include "SynTree/Type.h"
31#include "SynTree/Expression.h"
32#include "SynTree/Initializer.h"
33#include "SynTree/Statement.h"
34
35#define debugPrint(x) if ( doDebug ) { std::cout << x; }
36
37namespace SymTab {
38 template< typename Container, typename VisitorType >
39 inline void acceptAllNewScope( Container &container, VisitorType &visitor ) {
40 visitor.enterScope();
41 acceptAll( container, visitor );
42 visitor.leaveScope();
43 }
44
45 typedef std::unordered_map< std::string, DeclarationWithType* > MangleTable;
46 typedef std::unordered_map< std::string, MangleTable > IdTable;
47 typedef std::unordered_map< std::string, NamedTypeDecl* > TypeTable;
48 typedef std::unordered_map< std::string, StructDecl* > StructTable;
49 typedef std::unordered_map< std::string, EnumDecl* > EnumTable;
50 typedef std::unordered_map< std::string, UnionDecl* > UnionTable;
51 typedef std::unordered_map< std::string, TraitDecl* > TraitTable;
52
53 void dump( const IdTable &table, std::ostream &os ) {
54 for ( IdTable::const_iterator id = table.begin(); id != table.end(); ++id ) {
55 for ( MangleTable::const_iterator mangle = id->second.begin(); mangle != id->second.end(); ++mangle ) {
56 os << mangle->second << std::endl;
57 }
58 }
59 }
60
61 template< typename Decl >
62 void dump( const std::unordered_map< std::string, Decl* > &table, std::ostream &os ) {
63 for ( typename std::unordered_map< std::string, Decl* >::const_iterator it = table.begin(); it != table.end(); ++it ) {
64 os << it->second << std::endl;
65 } // for
66 }
67
68 struct Indexer::Impl {
69 Impl( unsigned long _scope ) : refCount(1), scope( _scope ), size( 0 ), base(),
70 idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {}
71 Impl( unsigned long _scope, Indexer &&_base ) : refCount(1), scope( _scope ), size( 0 ), base( _base ),
72 idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {}
73 unsigned long refCount; ///< Number of references to these tables
74 unsigned long scope; ///< Scope these tables are associated with
75 unsigned long size; ///< Number of elements stored in this table
76 const Indexer base; ///< Base indexer this extends
77
78 IdTable idTable; ///< Identifier namespace
79 TypeTable typeTable; ///< Type namespace
80 StructTable structTable; ///< Struct namespace
81 EnumTable enumTable; ///< Enum namespace
82 UnionTable unionTable; ///< Union namespace
83 TraitTable traitTable; ///< Trait namespace
84 };
85
86 Indexer::Impl *Indexer::newRef( Indexer::Impl *toClone ) {
87 if ( ! toClone ) return 0;
88
89 // shorten the search chain by skipping empty links
90 Indexer::Impl *ret = toClone->size == 0 ? toClone->base.tables : toClone;
91 if ( ret ) { ++ret->refCount; }
92
93 return ret;
94 }
95
96 void Indexer::deleteRef( Indexer::Impl *toFree ) {
97 if ( ! toFree ) return;
98
99 if ( --toFree->refCount == 0 ) delete toFree;
100 }
101
102 void Indexer::makeWritable() {
103 if ( ! tables ) {
104 // create indexer if not yet set
105 tables = new Indexer::Impl( scope );
106 } else if ( tables->refCount > 1 || tables->scope != scope ) {
107 // make this indexer the base of a fresh indexer at the current scope
108 tables = new Indexer::Impl( scope, std::move( *this ) );
109 }
110 }
111
112 Indexer::Indexer( bool _doDebug ) : tables( 0 ), scope( 0 ), doDebug( _doDebug ) {}
113
114 Indexer::Indexer( const Indexer &that ) : tables( newRef( that.tables ) ), scope( that.scope ), doDebug( that.doDebug ) {}
115
116 Indexer::Indexer( Indexer &&that ) : tables( that.tables ), scope( that.scope ), doDebug( that.doDebug ) {
117 that.tables = 0;
118 }
119
120 Indexer::~Indexer() {
121 deleteRef( tables );
122 }
123
124 Indexer& Indexer::operator= ( const Indexer &that ) {
125 deleteRef( tables );
126
127 tables = newRef( that.tables );
128 scope = that.scope;
129 doDebug = that.doDebug;
130
131 return *this;
132 }
133
134 Indexer& Indexer::operator= ( Indexer &&that ) {
135 deleteRef( tables );
136
137 tables = that.tables;
138 scope = that.scope;
139 doDebug = that.doDebug;
140
141 that.tables = 0;
142
143 return *this;
144 }
145
146 void Indexer::visit( ObjectDecl *objectDecl ) {
147 enterScope();
148 maybeAccept( objectDecl->get_type(), *this );
149 leaveScope();
150 maybeAccept( objectDecl->get_init(), *this );
151 maybeAccept( objectDecl->get_bitfieldWidth(), *this );
152 if ( objectDecl->get_name() != "" ) {
153 debugPrint( "Adding object " << objectDecl->get_name() << std::endl );
154 addId( objectDecl );
155 } // if
156 }
157
158 void Indexer::visit( FunctionDecl *functionDecl ) {
159 if ( functionDecl->get_name() == "" ) return;
160 debugPrint( "Adding function " << functionDecl->get_name() << std::endl );
161 addId( functionDecl );
162 enterScope();
163 maybeAccept( functionDecl->get_functionType(), *this );
164 acceptAll( functionDecl->get_oldDecls(), *this );
165 maybeAccept( functionDecl->get_statements(), *this );
166 leaveScope();
167 }
168
169
170// A NOTE ON THE ORDER OF TRAVERSAL
171//
172// Types and typedefs have their base types visited before they are added to the type table. This is ok, since there is
173// no such thing as a recursive type or typedef.
174//
175// typedef struct { T *x; } T; // never allowed
176//
177// for structs/unions, it is possible to have recursion, so the decl should be added as if it's incomplete to begin, the
178// members are traversed, and then the complete type should be added (assuming the type is completed by this particular
179// declaration).
180//
181// struct T { struct T *x; }; // allowed
182//
183// It is important to add the complete type to the symbol table *after* the members/base has been traversed, since that
184// traversal may modify the definition of the type and these modifications should be visible when the symbol table is
185// queried later in this pass.
186//
187// TODO: figure out whether recursive contexts are sensible/possible/reasonable.
188
189
190 void Indexer::visit( TypeDecl *typeDecl ) {
191 // see A NOTE ON THE ORDER OF TRAVERSAL, above
192 // note that assertions come after the type is added to the symtab, since they are not part of the type proper
193 // and may depend on the type itself
194 enterScope();
195 acceptAll( typeDecl->get_parameters(), *this );
196 maybeAccept( typeDecl->get_base(), *this );
197 leaveScope();
198 debugPrint( "Adding type " << typeDecl->get_name() << std::endl );
199 addType( typeDecl );
200 acceptAll( typeDecl->get_assertions(), *this );
201 }
202
203 void Indexer::visit( TypedefDecl *typeDecl ) {
204 enterScope();
205 acceptAll( typeDecl->get_parameters(), *this );
206 maybeAccept( typeDecl->get_base(), *this );
207 leaveScope();
208 debugPrint( "Adding typedef " << typeDecl->get_name() << std::endl );
209 addType( typeDecl );
210 }
211
212 void Indexer::visit( StructDecl *aggregateDecl ) {
213 // make up a forward declaration and add it before processing the members
214 StructDecl fwdDecl( aggregateDecl->get_name() );
215 cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() );
216 debugPrint( "Adding fwd decl for struct " << fwdDecl.get_name() << std::endl );
217 addStruct( &fwdDecl );
218
219 enterScope();
220 acceptAll( aggregateDecl->get_parameters(), *this );
221 acceptAll( aggregateDecl->get_members(), *this );
222 leaveScope();
223
224 debugPrint( "Adding struct " << aggregateDecl->get_name() << std::endl );
225 // this addition replaces the forward declaration
226 addStruct( aggregateDecl );
227 }
228
229 void Indexer::visit( UnionDecl *aggregateDecl ) {
230 // make up a forward declaration and add it before processing the members
231 UnionDecl fwdDecl( aggregateDecl->get_name() );
232 cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() );
233 debugPrint( "Adding fwd decl for union " << fwdDecl.get_name() << std::endl );
234 addUnion( &fwdDecl );
235
236 enterScope();
237 acceptAll( aggregateDecl->get_parameters(), *this );
238 acceptAll( aggregateDecl->get_members(), *this );
239 leaveScope();
240
241 debugPrint( "Adding union " << aggregateDecl->get_name() << std::endl );
242 addUnion( aggregateDecl );
243 }
244
245 void Indexer::visit( EnumDecl *aggregateDecl ) {
246 debugPrint( "Adding enum " << aggregateDecl->get_name() << std::endl );
247 addEnum( aggregateDecl );
248 // unlike structs, contexts, and unions, enums inject their members into the global scope
249 acceptAll( aggregateDecl->get_members(), *this );
250 }
251
252 void Indexer::visit( TraitDecl *aggregateDecl ) {
253 enterScope();
254 acceptAll( aggregateDecl->get_parameters(), *this );
255 acceptAll( aggregateDecl->get_members(), *this );
256 leaveScope();
257
258 debugPrint( "Adding context " << aggregateDecl->get_name() << std::endl );
259 addTrait( aggregateDecl );
260 }
261
262 void Indexer::visit( CompoundStmt *compoundStmt ) {
263 enterScope();
264 acceptAll( compoundStmt->get_kids(), *this );
265 leaveScope();
266 }
267
268
269 void Indexer::visit( ApplicationExpr *applicationExpr ) {
270 acceptAllNewScope( applicationExpr->get_results(), *this );
271 maybeAccept( applicationExpr->get_function(), *this );
272 acceptAll( applicationExpr->get_args(), *this );
273 }
274
275 void Indexer::visit( UntypedExpr *untypedExpr ) {
276 acceptAllNewScope( untypedExpr->get_results(), *this );
277 acceptAll( untypedExpr->get_args(), *this );
278 }
279
280 void Indexer::visit( NameExpr *nameExpr ) {
281 acceptAllNewScope( nameExpr->get_results(), *this );
282 }
283
284 void Indexer::visit( AddressExpr *addressExpr ) {
285 acceptAllNewScope( addressExpr->get_results(), *this );
286 maybeAccept( addressExpr->get_arg(), *this );
287 }
288
289 void Indexer::visit( LabelAddressExpr *labAddressExpr ) {
290 acceptAllNewScope( labAddressExpr->get_results(), *this );
291 maybeAccept( labAddressExpr->get_arg(), *this );
292 }
293
294 void Indexer::visit( CastExpr *castExpr ) {
295 acceptAllNewScope( castExpr->get_results(), *this );
296 maybeAccept( castExpr->get_arg(), *this );
297 }
298
299 void Indexer::visit( UntypedMemberExpr *memberExpr ) {
300 acceptAllNewScope( memberExpr->get_results(), *this );
301 maybeAccept( memberExpr->get_aggregate(), *this );
302 }
303
304 void Indexer::visit( MemberExpr *memberExpr ) {
305 acceptAllNewScope( memberExpr->get_results(), *this );
306 maybeAccept( memberExpr->get_aggregate(), *this );
307 }
308
309 void Indexer::visit( VariableExpr *variableExpr ) {
310 acceptAllNewScope( variableExpr->get_results(), *this );
311 }
312
313 void Indexer::visit( ConstantExpr *constantExpr ) {
314 acceptAllNewScope( constantExpr->get_results(), *this );
315 maybeAccept( constantExpr->get_constant(), *this );
316 }
317
318 void Indexer::visit( SizeofExpr *sizeofExpr ) {
319 acceptAllNewScope( sizeofExpr->get_results(), *this );
320 if ( sizeofExpr->get_isType() ) {
321 maybeAccept( sizeofExpr->get_type(), *this );
322 } else {
323 maybeAccept( sizeofExpr->get_expr(), *this );
324 }
325 }
326
327 void Indexer::visit( AlignofExpr *alignofExpr ) {
328 acceptAllNewScope( alignofExpr->get_results(), *this );
329 if ( alignofExpr->get_isType() ) {
330 maybeAccept( alignofExpr->get_type(), *this );
331 } else {
332 maybeAccept( alignofExpr->get_expr(), *this );
333 }
334 }
335
336 void Indexer::visit( UntypedOffsetofExpr *offsetofExpr ) {
337 acceptAllNewScope( offsetofExpr->get_results(), *this );
338 maybeAccept( offsetofExpr->get_type(), *this );
339 }
340
341 void Indexer::visit( OffsetofExpr *offsetofExpr ) {
342 acceptAllNewScope( offsetofExpr->get_results(), *this );
343 maybeAccept( offsetofExpr->get_type(), *this );
344 maybeAccept( offsetofExpr->get_member(), *this );
345 }
346
347 void Indexer::visit( AttrExpr *attrExpr ) {
348 acceptAllNewScope( attrExpr->get_results(), *this );
349 if ( attrExpr->get_isType() ) {
350 maybeAccept( attrExpr->get_type(), *this );
351 } else {
352 maybeAccept( attrExpr->get_expr(), *this );
353 }
354 }
355
356 void Indexer::visit( LogicalExpr *logicalExpr ) {
357 acceptAllNewScope( logicalExpr->get_results(), *this );
358 maybeAccept( logicalExpr->get_arg1(), *this );
359 maybeAccept( logicalExpr->get_arg2(), *this );
360 }
361
362 void Indexer::visit( ConditionalExpr *conditionalExpr ) {
363 acceptAllNewScope( conditionalExpr->get_results(), *this );
364 maybeAccept( conditionalExpr->get_arg1(), *this );
365 maybeAccept( conditionalExpr->get_arg2(), *this );
366 maybeAccept( conditionalExpr->get_arg3(), *this );
367 }
368
369 void Indexer::visit( CommaExpr *commaExpr ) {
370 acceptAllNewScope( commaExpr->get_results(), *this );
371 maybeAccept( commaExpr->get_arg1(), *this );
372 maybeAccept( commaExpr->get_arg2(), *this );
373 }
374
375 void Indexer::visit( TupleExpr *tupleExpr ) {
376 acceptAllNewScope( tupleExpr->get_results(), *this );
377 acceptAll( tupleExpr->get_exprs(), *this );
378 }
379
380 void Indexer::visit( SolvedTupleExpr *tupleExpr ) {
381 acceptAllNewScope( tupleExpr->get_results(), *this );
382 acceptAll( tupleExpr->get_exprs(), *this );
383 }
384
385 void Indexer::visit( TypeExpr *typeExpr ) {
386 acceptAllNewScope( typeExpr->get_results(), *this );
387 maybeAccept( typeExpr->get_type(), *this );
388 }
389
390 void Indexer::visit( AsmExpr *asmExpr ) {
391 maybeAccept( asmExpr->get_inout(), *this );
392 maybeAccept( asmExpr->get_constraint(), *this );
393 maybeAccept( asmExpr->get_operand(), *this );
394 }
395
396 void Indexer::visit( UntypedValofExpr *valofExpr ) {
397 acceptAllNewScope( valofExpr->get_results(), *this );
398 maybeAccept( valofExpr->get_body(), *this );
399 }
400
401
402 void Indexer::visit( TraitInstType *contextInst ) {
403 acceptAll( contextInst->get_parameters(), *this );
404 acceptAll( contextInst->get_members(), *this );
405 }
406
407 void Indexer::visit( StructInstType *structInst ) {
408 if ( ! lookupStruct( structInst->get_name() ) ) {
409 debugPrint( "Adding struct " << structInst->get_name() << " from implicit forward declaration" << std::endl );
410 addStruct( structInst->get_name() );
411 }
412 enterScope();
413 acceptAll( structInst->get_parameters(), *this );
414 leaveScope();
415 }
416
417 void Indexer::visit( UnionInstType *unionInst ) {
418 if ( ! lookupUnion( unionInst->get_name() ) ) {
419 debugPrint( "Adding union " << unionInst->get_name() << " from implicit forward declaration" << std::endl );
420 addUnion( unionInst->get_name() );
421 }
422 enterScope();
423 acceptAll( unionInst->get_parameters(), *this );
424 leaveScope();
425 }
426
427 void Indexer::visit( ForStmt *forStmt ) {
428 // for statements introduce a level of scope
429 enterScope();
430 Visitor::visit( forStmt );
431 leaveScope();
432 }
433
434
435
436 void Indexer::lookupId( const std::string &id, std::list< DeclarationWithType* > &out ) const {
437 if ( ! tables ) return;
438
439 IdTable::const_iterator decls = tables->idTable.find( id );
440 if ( decls != tables->idTable.end() ) {
441 const MangleTable &mangleTable = decls->second;
442 for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) {
443 out.push_back( decl->second );
444 }
445 }
446
447 // get declarations from base indexers
448 tables->base.lookupId( id, out );
449 }
450
451 NamedTypeDecl *Indexer::lookupType( const std::string &id ) const {
452 if ( ! tables ) return 0;
453
454 TypeTable::const_iterator ret = tables->typeTable.find( id );
455 return ret != tables->typeTable.end() ? ret->second : tables->base.lookupType( id );
456 }
457
458 StructDecl *Indexer::lookupStruct( const std::string &id ) const {
459 if ( ! tables ) return 0;
460
461 StructTable::const_iterator ret = tables->structTable.find( id );
462 return ret != tables->structTable.end() ? ret->second : tables->base.lookupStruct( id );
463 }
464
465 EnumDecl *Indexer::lookupEnum( const std::string &id ) const {
466 if ( ! tables ) return 0;
467
468 EnumTable::const_iterator ret = tables->enumTable.find( id );
469 return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnum( id );
470 }
471
472 UnionDecl *Indexer::lookupUnion( const std::string &id ) const {
473 if ( ! tables ) return 0;
474
475 UnionTable::const_iterator ret = tables->unionTable.find( id );
476 return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnion( id );
477 }
478
479 TraitDecl *Indexer::lookupTrait( const std::string &id ) const {
480 if ( ! tables ) return 0;
481
482 TraitTable::const_iterator ret = tables->traitTable.find( id );
483 return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTrait( id );
484 }
485
486 DeclarationWithType *Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) const {
487 if ( ! tables ) return 0;
488 if ( tables->scope < scope ) return 0;
489
490 IdTable::const_iterator decls = tables->idTable.find( id );
491 if ( decls != tables->idTable.end() ) {
492 const MangleTable &mangleTable = decls->second;
493 MangleTable::const_iterator decl = mangleTable.find( mangleName );
494 if ( decl != mangleTable.end() ) return decl->second;
495 }
496
497 return tables->base.lookupIdAtScope( id, mangleName, scope );
498 }
499
500 bool Indexer::hasCDeclWithName( const std::string &id ) const {
501 if ( ! tables ) return false;
502
503 IdTable::const_iterator decls = tables->idTable.find( id );
504 if ( decls != tables->idTable.end() ) {
505 const MangleTable &mangleTable = decls->second;
506 for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) {
507 if ( decl->second->get_linkage() == LinkageSpec::C ) return true;
508 }
509 }
510
511 return tables->base.hasCDeclWithName( id );
512 }
513
514 NamedTypeDecl *Indexer::lookupTypeAtScope( const std::string &id, unsigned long scope ) const {
515 if ( ! tables ) return 0;
516 if ( tables->scope < scope ) return 0;
517
518 TypeTable::const_iterator ret = tables->typeTable.find( id );
519 return ret != tables->typeTable.end() ? ret->second : tables->base.lookupTypeAtScope( id, scope );
520 }
521
522 StructDecl *Indexer::lookupStructAtScope( const std::string &id, unsigned long scope ) const {
523 if ( ! tables ) return 0;
524 if ( tables->scope < scope ) return 0;
525
526 StructTable::const_iterator ret = tables->structTable.find( id );
527 return ret != tables->structTable.end() ? ret->second : tables->base.lookupStructAtScope( id, scope );
528 }
529
530 EnumDecl *Indexer::lookupEnumAtScope( const std::string &id, unsigned long scope ) const {
531 if ( ! tables ) return 0;
532 if ( tables->scope < scope ) return 0;
533
534 EnumTable::const_iterator ret = tables->enumTable.find( id );
535 return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnumAtScope( id, scope );
536 }
537
538 UnionDecl *Indexer::lookupUnionAtScope( const std::string &id, unsigned long scope ) const {
539 if ( ! tables ) return 0;
540 if ( tables->scope < scope ) return 0;
541
542 UnionTable::const_iterator ret = tables->unionTable.find( id );
543 return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnionAtScope( id, scope );
544 }
545
546 TraitDecl *Indexer::lookupTraitAtScope( const std::string &id, unsigned long scope ) const {
547 if ( ! tables ) return 0;
548 if ( tables->scope < scope ) return 0;
549
550 TraitTable::const_iterator ret = tables->traitTable.find( id );
551 return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTraitAtScope( id, scope );
552 }
553
554 bool addedIdConflicts( DeclarationWithType *existing, DeclarationWithType *added ) {
555 // if we're giving the same name mangling to things of different types then there is something wrong
556 assert( (dynamic_cast<ObjectDecl*>( added ) && dynamic_cast<ObjectDecl*>( existing ) )
557 || (dynamic_cast<FunctionDecl*>( added ) && dynamic_cast<FunctionDecl*>( existing ) ) );
558
559 if ( LinkageSpec::isOverridable( existing->get_linkage() ) ) {
560 // new definition shadows the autogenerated one, even at the same scope
561 return false;
562 } else if ( added->get_linkage() != LinkageSpec::C || ResolvExpr::typesCompatible( added->get_type(), existing->get_type(), Indexer() ) ) {
563 // typesCompatible doesn't really do the right thing here. When checking compatibility of function types,
564 // we should ignore outermost pointer qualifiers, except _Atomic?
565 FunctionDecl *newentry = dynamic_cast< FunctionDecl* >( added );
566 FunctionDecl *oldentry = dynamic_cast< FunctionDecl* >( existing );
567 if ( newentry && oldentry ) {
568 if ( newentry->get_statements() && oldentry->get_statements() ) {
569 throw SemanticError( "duplicate function definition for ", added );
570 } // if
571 } else {
572 // two objects with the same mangled name defined in the same scope.
573 // both objects must be marked extern or both must be intrinsic for this to be okay
574 // xxx - perhaps it's actually if either is intrinsic then this is okay?
575 // might also need to be same storage class?
576 ObjectDecl *newobj = dynamic_cast< ObjectDecl* >( added );
577 ObjectDecl *oldobj = dynamic_cast< ObjectDecl* >( existing );
578 if ( newobj->get_storageClass() != DeclarationNode::Extern && oldobj->get_storageClass() != DeclarationNode::Extern ) {
579 throw SemanticError( "duplicate object definition for ", added );
580 } // if
581 } // if
582 } else {
583 throw SemanticError( "duplicate definition for ", added );
584 } // if
585
586 return true;
587 }
588
589 void Indexer::addId( DeclarationWithType *decl ) {
590 makeWritable();
591
592 const std::string &name = decl->get_name();
593 std::string mangleName;
594 if ( decl->get_linkage() == LinkageSpec::C ) {
595 mangleName = name;
596 } else if ( LinkageSpec::isOverridable( decl->get_linkage() ) ) {
597 // mangle the name without including the appropriate suffix, so overridable routines are placed into the
598 // same "bucket" as their user defined versions.
599 mangleName = Mangler::mangle( decl, false );
600 } else {
601 mangleName = Mangler::mangle( decl );
602 } // if
603
604 DeclarationWithType *existing = lookupIdAtScope( name, mangleName, scope );
605 if ( ! existing || ! addedIdConflicts( existing, decl ) ) {
606 // this ensures that no two declarations with the same unmangled name both have C linkage
607 if ( decl->get_linkage() == LinkageSpec::C && hasCDeclWithName( name ) ) {
608 throw SemanticError( "invalid overload of C function ", decl );
609 }
610
611 tables->idTable[ name ][ mangleName ] = decl;
612 ++tables->size;
613 }
614 }
615
616 bool addedTypeConflicts( NamedTypeDecl *existing, NamedTypeDecl *added ) {
617 if ( existing->get_base() == 0 ) {
618 return false;
619 } else if ( added->get_base() == 0 ) {
620 return true;
621 } else {
622 throw SemanticError( "redeclaration of ", added );
623 }
624 }
625
626 void Indexer::addType( NamedTypeDecl *decl ) {
627 makeWritable();
628
629 const std::string &id = decl->get_name();
630 TypeTable::iterator existing = tables->typeTable.find( id );
631 if ( existing == tables->typeTable.end() ) {
632 NamedTypeDecl *parent = tables->base.lookupTypeAtScope( id, scope );
633 if ( ! parent || ! addedTypeConflicts( parent, decl ) ) {
634 tables->typeTable.insert( existing, std::make_pair( id, decl ) );
635 ++tables->size;
636 }
637 } else {
638 if ( ! addedTypeConflicts( existing->second, decl ) ) {
639 existing->second = decl;
640 }
641 }
642 }
643
644 bool addedDeclConflicts( AggregateDecl *existing, AggregateDecl *added ) {
645 if ( existing->get_members().empty() ) {
646 return false;
647 } else if ( ! added->get_members().empty() ) {
648 throw SemanticError( "redeclaration of ", added );
649 } // if
650 return true;
651 }
652
653 void Indexer::addStruct( const std::string &id ) {
654 addStruct( new StructDecl( id ) );
655 }
656
657 void Indexer::addStruct( StructDecl *decl ) {
658 makeWritable();
659
660 const std::string &id = decl->get_name();
661 StructTable::iterator existing = tables->structTable.find( id );
662 if ( existing == tables->structTable.end() ) {
663 StructDecl *parent = tables->base.lookupStructAtScope( id, scope );
664 if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
665 tables->structTable.insert( existing, std::make_pair( id, decl ) );
666 ++tables->size;
667 }
668 } else {
669 if ( ! addedDeclConflicts( existing->second, decl ) ) {
670 existing->second = decl;
671 }
672 }
673 }
674
675 void Indexer::addEnum( EnumDecl *decl ) {
676 makeWritable();
677
678 const std::string &id = decl->get_name();
679 EnumTable::iterator existing = tables->enumTable.find( id );
680 if ( existing == tables->enumTable.end() ) {
681 EnumDecl *parent = tables->base.lookupEnumAtScope( id, scope );
682 if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
683 tables->enumTable.insert( existing, std::make_pair( id, decl ) );
684 ++tables->size;
685 }
686 } else {
687 if ( ! addedDeclConflicts( existing->second, decl ) ) {
688 existing->second = decl;
689 }
690 }
691 }
692
693 void Indexer::addUnion( const std::string &id ) {
694 addUnion( new UnionDecl( id ) );
695 }
696
697 void Indexer::addUnion( UnionDecl *decl ) {
698 makeWritable();
699
700 const std::string &id = decl->get_name();
701 UnionTable::iterator existing = tables->unionTable.find( id );
702 if ( existing == tables->unionTable.end() ) {
703 UnionDecl *parent = tables->base.lookupUnionAtScope( id, scope );
704 if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
705 tables->unionTable.insert( existing, std::make_pair( id, decl ) );
706 ++tables->size;
707 }
708 } else {
709 if ( ! addedDeclConflicts( existing->second, decl ) ) {
710 existing->second = decl;
711 }
712 }
713 }
714
715 void Indexer::addTrait( TraitDecl *decl ) {
716 makeWritable();
717
718 const std::string &id = decl->get_name();
719 TraitTable::iterator existing = tables->traitTable.find( id );
720 if ( existing == tables->traitTable.end() ) {
721 TraitDecl *parent = tables->base.lookupTraitAtScope( id, scope );
722 if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
723 tables->traitTable.insert( existing, std::make_pair( id, decl ) );
724 ++tables->size;
725 }
726 } else {
727 if ( ! addedDeclConflicts( existing->second, decl ) ) {
728 existing->second = decl;
729 }
730 }
731 }
732
733 void Indexer::enterScope() {
734 ++scope;
735
736 if ( doDebug ) {
737 std::cout << "--- Entering scope " << scope << std::endl;
738 }
739 }
740
741 void Indexer::leaveScope() {
742 using std::cout;
743
744 assert( scope > 0 && "cannot leave initial scope" );
745 --scope;
746
747 while ( tables && tables->scope > scope ) {
748 if ( doDebug ) {
749 cout << "--- Leaving scope " << tables->scope << " containing" << std::endl;
750 dump( tables->idTable, cout );
751 dump( tables->typeTable, cout );
752 dump( tables->structTable, cout );
753 dump( tables->enumTable, cout );
754 dump( tables->unionTable, cout );
755 dump( tables->traitTable, cout );
756 }
757
758 // swap tables for base table until we find one at an appropriate scope
759 Indexer::Impl *base = newRef( tables->base.tables );
760 deleteRef( tables );
761 tables = base;
762 }
763 }
764
765 void Indexer::print( std::ostream &os, int indent ) const {
766 using std::cerr;
767
768 cerr << "===idTable===" << std::endl;
769 if ( tables ) dump( tables->idTable, os );
770 cerr << "===typeTable===" << std::endl;
771 if ( tables ) dump( tables->typeTable, os );
772 cerr << "===structTable===" << std::endl;
773 if ( tables ) dump( tables->structTable, os );
774 cerr << "===enumTable===" << std::endl;
775 if ( tables ) dump( tables->enumTable, os );
776 cerr << "===unionTable===" << std::endl;
777 if ( tables ) dump( tables->unionTable, os );
778 cerr << "===contextTable===" << std::endl;
779 if ( tables ) dump( tables->traitTable, os );
780 }
781} // namespace SymTab
782
783// Local Variables: //
784// tab-width: 4 //
785// mode: c++ //
786// compile-command: "make install" //
787// End: //
Note: See TracBrowser for help on using the repository browser.