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