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