source: src/SymTab/Indexer.cc@ 0c286cf

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 0c286cf was d5556a3, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

change rework TupleAssignExpr and StmtExpr

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