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 | NamedTypeDecl *Indexer::globalLookupType( const std::string &id ) const { |
---|
275 | return lookupTypeAtScope( id, 0 ); |
---|
276 | } |
---|
277 | |
---|
278 | StructDecl *Indexer::globalLookupStruct( const std::string &id ) const { |
---|
279 | return lookupStructAtScope( id, 0 ); |
---|
280 | } |
---|
281 | |
---|
282 | UnionDecl *Indexer::globalLookupUnion( const std::string &id ) const { |
---|
283 | return lookupUnionAtScope( id, 0 ); |
---|
284 | } |
---|
285 | |
---|
286 | EnumDecl *Indexer::globalLookupEnum( const std::string &id ) const { |
---|
287 | return lookupEnumAtScope( id, 0 ); |
---|
288 | } |
---|
289 | |
---|
290 | EnumDecl *Indexer::lookupEnum( const std::string &id ) const { |
---|
291 | if ( ! tables ) return 0; |
---|
292 | |
---|
293 | EnumTable::const_iterator ret = tables->enumTable.find( id ); |
---|
294 | return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnum( id ); |
---|
295 | } |
---|
296 | |
---|
297 | UnionDecl *Indexer::lookupUnion( const std::string &id ) const { |
---|
298 | if ( ! tables ) return 0; |
---|
299 | |
---|
300 | UnionTable::const_iterator ret = tables->unionTable.find( id ); |
---|
301 | return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnion( id ); |
---|
302 | } |
---|
303 | |
---|
304 | TraitDecl *Indexer::lookupTrait( const std::string &id ) const { |
---|
305 | if ( ! tables ) return 0; |
---|
306 | |
---|
307 | TraitTable::const_iterator ret = tables->traitTable.find( id ); |
---|
308 | return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTrait( id ); |
---|
309 | } |
---|
310 | |
---|
311 | const Indexer::IdData * Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) const { |
---|
312 | if ( ! tables ) return nullptr; |
---|
313 | if ( tables->scope < scope ) return nullptr; |
---|
314 | |
---|
315 | IdTable::const_iterator decls = tables->idTable.find( id ); |
---|
316 | if ( decls != tables->idTable.end() ) { |
---|
317 | const MangleTable &mangleTable = decls->second; |
---|
318 | MangleTable::const_iterator decl = mangleTable.find( mangleName ); |
---|
319 | if ( decl != mangleTable.end() ) return &decl->second; |
---|
320 | } |
---|
321 | |
---|
322 | return tables->base.lookupIdAtScope( id, mangleName, scope ); |
---|
323 | } |
---|
324 | |
---|
325 | Indexer::IdData * Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) { |
---|
326 | return const_cast<IdData *>(const_cast<const Indexer *>(this)->lookupIdAtScope( id, mangleName, scope )); |
---|
327 | } |
---|
328 | |
---|
329 | bool Indexer::hasIncompatibleCDecl( 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 those with a compatible type (by mangleName) |
---|
338 | if ( ! LinkageSpec::isMangled( decl->second.id->get_linkage() ) && decl->first != mangleName ) return true; |
---|
339 | } |
---|
340 | } |
---|
341 | |
---|
342 | return tables->base.hasIncompatibleCDecl( id, mangleName, scope ); |
---|
343 | } |
---|
344 | |
---|
345 | bool Indexer::hasCompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const { |
---|
346 | if ( ! tables ) return false; |
---|
347 | if ( tables->scope < scope ) return false; |
---|
348 | |
---|
349 | IdTable::const_iterator decls = tables->idTable.find( id ); |
---|
350 | if ( decls != tables->idTable.end() ) { |
---|
351 | const MangleTable &mangleTable = decls->second; |
---|
352 | for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { |
---|
353 | // check for C decls with the same name, skipping |
---|
354 | // those with an incompatible type (by mangleName) |
---|
355 | if ( ! LinkageSpec::isMangled( decl->second.id->get_linkage() ) && decl->first == mangleName ) return true; |
---|
356 | } |
---|
357 | } |
---|
358 | |
---|
359 | return tables->base.hasCompatibleCDecl( id, mangleName, scope ); |
---|
360 | } |
---|
361 | |
---|
362 | NamedTypeDecl *Indexer::lookupTypeAtScope( const std::string &id, unsigned long scope ) const { |
---|
363 | if ( ! tables ) return 0; |
---|
364 | if ( tables->scope < scope ) return 0; |
---|
365 | if ( tables->scope > scope ) return tables->base.lookupTypeAtScope( id, scope ); |
---|
366 | |
---|
367 | TypeTable::const_iterator ret = tables->typeTable.find( id ); |
---|
368 | return ret != tables->typeTable.end() ? ret->second : tables->base.lookupTypeAtScope( id, scope ); |
---|
369 | } |
---|
370 | |
---|
371 | StructDecl *Indexer::lookupStructAtScope( const std::string &id, unsigned long scope ) const { |
---|
372 | if ( ! tables ) return 0; |
---|
373 | if ( tables->scope < scope ) return 0; |
---|
374 | if ( tables->scope > scope ) return tables->base.lookupStructAtScope( id, scope ); |
---|
375 | |
---|
376 | StructTable::const_iterator ret = tables->structTable.find( id ); |
---|
377 | return ret != tables->structTable.end() ? ret->second : tables->base.lookupStructAtScope( id, scope ); |
---|
378 | } |
---|
379 | |
---|
380 | EnumDecl *Indexer::lookupEnumAtScope( const std::string &id, unsigned long scope ) const { |
---|
381 | if ( ! tables ) return 0; |
---|
382 | if ( tables->scope < scope ) return 0; |
---|
383 | if ( tables->scope > scope ) return tables->base.lookupEnumAtScope( id, scope ); |
---|
384 | |
---|
385 | EnumTable::const_iterator ret = tables->enumTable.find( id ); |
---|
386 | return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnumAtScope( id, scope ); |
---|
387 | } |
---|
388 | |
---|
389 | UnionDecl *Indexer::lookupUnionAtScope( const std::string &id, unsigned long scope ) const { |
---|
390 | if ( ! tables ) return 0; |
---|
391 | if ( tables->scope < scope ) return 0; |
---|
392 | if ( tables->scope > scope ) return tables->base.lookupUnionAtScope( id, scope ); |
---|
393 | |
---|
394 | UnionTable::const_iterator ret = tables->unionTable.find( id ); |
---|
395 | return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnionAtScope( id, scope ); |
---|
396 | } |
---|
397 | |
---|
398 | TraitDecl *Indexer::lookupTraitAtScope( const std::string &id, unsigned long scope ) const { |
---|
399 | if ( ! tables ) return 0; |
---|
400 | if ( tables->scope < scope ) return 0; |
---|
401 | if ( tables->scope > scope ) return tables->base.lookupTraitAtScope( id, scope ); |
---|
402 | |
---|
403 | TraitTable::const_iterator ret = tables->traitTable.find( id ); |
---|
404 | return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTraitAtScope( id, scope ); |
---|
405 | } |
---|
406 | |
---|
407 | bool isFunction( DeclarationWithType * decl ) { |
---|
408 | return GenPoly::getFunctionType( decl->get_type() ); |
---|
409 | } |
---|
410 | |
---|
411 | bool isObject( DeclarationWithType * decl ) { |
---|
412 | return ! isFunction( decl ); |
---|
413 | } |
---|
414 | |
---|
415 | bool isDefinition( DeclarationWithType * decl ) { |
---|
416 | if ( FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl ) ) { |
---|
417 | // a function is a definition if it has a body |
---|
418 | return func->statements; |
---|
419 | } else { |
---|
420 | // an object is a definition if it is not marked extern. |
---|
421 | // both objects must be marked extern |
---|
422 | return ! decl->get_storageClasses().is_extern; |
---|
423 | } |
---|
424 | } |
---|
425 | |
---|
426 | bool addedIdConflicts( Indexer::IdData & existing, DeclarationWithType *added, BaseSyntaxNode * deleteStmt, Indexer::ConflictFunction handleConflicts ) { |
---|
427 | // if we're giving the same name mangling to things of different types then there is something wrong |
---|
428 | assert( (isObject( added ) && isObject( existing.id ) ) |
---|
429 | || ( isFunction( added ) && isFunction( existing.id ) ) ); |
---|
430 | |
---|
431 | if ( LinkageSpec::isOverridable( existing.id->get_linkage() ) ) { |
---|
432 | // new definition shadows the autogenerated one, even at the same scope |
---|
433 | return false; |
---|
434 | } else if ( LinkageSpec::isMangled( added->get_linkage() ) || ResolvExpr::typesCompatible( added->get_type(), existing.id->get_type(), Indexer() ) ) { |
---|
435 | |
---|
436 | // it is a conflict if one declaration is deleted and the other is not |
---|
437 | if ( deleteStmt && ! existing.deleteStmt ) { |
---|
438 | return handleConflicts( existing, "deletion of defined identifier " ); |
---|
439 | } else if ( ! deleteStmt && existing.deleteStmt ) { |
---|
440 | return handleConflicts( existing, "definition of deleted identifier " ); |
---|
441 | } |
---|
442 | |
---|
443 | if ( isDefinition( added ) && isDefinition( existing.id ) ) { |
---|
444 | if ( isFunction( added ) ) { |
---|
445 | return handleConflicts( existing, "duplicate function definition for " ); |
---|
446 | } else { |
---|
447 | return handleConflicts( existing, "duplicate object definition for " ); |
---|
448 | } // if |
---|
449 | } // if |
---|
450 | } else { |
---|
451 | return handleConflicts( existing, "duplicate definition for " ); |
---|
452 | } // if |
---|
453 | |
---|
454 | return true; |
---|
455 | } |
---|
456 | |
---|
457 | void Indexer::addId( DeclarationWithType *decl, ConflictFunction handleConflicts, Expression * baseExpr, BaseSyntaxNode * deleteStmt ) { |
---|
458 | if ( decl->name == "" ) return; |
---|
459 | debugPrint( "Adding Id " << decl->name << std::endl ); |
---|
460 | makeWritable(); |
---|
461 | |
---|
462 | const std::string &name = decl->name; |
---|
463 | std::string mangleName; |
---|
464 | if ( LinkageSpec::isOverridable( decl->linkage ) ) { |
---|
465 | // mangle the name without including the appropriate suffix, so overridable routines are placed into the |
---|
466 | // same "bucket" as their user defined versions. |
---|
467 | mangleName = Mangler::mangle( decl, false ); |
---|
468 | } else { |
---|
469 | mangleName = Mangler::mangle( decl ); |
---|
470 | } // if |
---|
471 | |
---|
472 | // this ensures that no two declarations with the same unmangled name at the same scope both have C linkage |
---|
473 | if ( ! LinkageSpec::isMangled( decl->linkage ) ) { |
---|
474 | // NOTE this is broken in Richard's original code in such a way that it never triggers (it |
---|
475 | // doesn't check decls that have the same manglename, and all C-linkage decls are defined to |
---|
476 | // have their name as their manglename, hence the error can never trigger). |
---|
477 | // The code here is closer to correct, but name mangling would have to be completely |
---|
478 | // isomorphic to C type-compatibility, which it may not be. |
---|
479 | if ( hasIncompatibleCDecl( name, mangleName, scope ) ) { |
---|
480 | SemanticError( decl, "conflicting overload of C function " ); |
---|
481 | } |
---|
482 | } else { |
---|
483 | // Check that a Cforall declaration doesn't override any C declaration |
---|
484 | if ( hasCompatibleCDecl( name, mangleName, scope ) ) { |
---|
485 | SemanticError( decl, "Cforall declaration hides C function " ); |
---|
486 | } |
---|
487 | } |
---|
488 | |
---|
489 | // Skip repeat declarations of the same identifier |
---|
490 | IdData * existing = lookupIdAtScope( name, mangleName, scope ); |
---|
491 | if ( existing && existing->id && addedIdConflicts( *existing, decl, deleteStmt, handleConflicts ) ) return; |
---|
492 | |
---|
493 | // add to indexer |
---|
494 | tables->idTable[ name ][ mangleName ] = IdData{ decl, baseExpr, deleteStmt }; |
---|
495 | ++tables->size; |
---|
496 | } |
---|
497 | |
---|
498 | void Indexer::addId( DeclarationWithType * decl, Expression * baseExpr ) { |
---|
499 | // default handling of conflicts is to raise an error |
---|
500 | addId( decl, [decl](IdData &, const std::string & msg) { SemanticError( decl, msg ); return true; }, baseExpr, decl->isDeleted ? decl : nullptr ); |
---|
501 | } |
---|
502 | |
---|
503 | void Indexer::addDeletedId( DeclarationWithType * decl, BaseSyntaxNode * deleteStmt ) { |
---|
504 | // default handling of conflicts is to raise an error |
---|
505 | addId( decl, [decl](IdData &, const std::string & msg) { SemanticError( decl, msg ); return true; }, nullptr, deleteStmt ); |
---|
506 | } |
---|
507 | |
---|
508 | bool addedTypeConflicts( NamedTypeDecl *existing, NamedTypeDecl *added ) { |
---|
509 | if ( existing->base == nullptr ) { |
---|
510 | return false; |
---|
511 | } else if ( added->base == nullptr ) { |
---|
512 | return true; |
---|
513 | } else { |
---|
514 | assert( existing->base && added->base ); |
---|
515 | // typedef redeclarations are errors only if types are different |
---|
516 | if ( ! ResolvExpr::typesCompatible( existing->base, added->base, Indexer() ) ) { |
---|
517 | SemanticError( added->location, "redeclaration of " + added->name ); |
---|
518 | } |
---|
519 | } |
---|
520 | // does not need to be added to the table if both existing and added have a base that are the same |
---|
521 | return true; |
---|
522 | } |
---|
523 | |
---|
524 | void Indexer::addType( NamedTypeDecl *decl ) { |
---|
525 | debugPrint( "Adding type " << decl->name << std::endl ); |
---|
526 | makeWritable(); |
---|
527 | |
---|
528 | const std::string &id = decl->name; |
---|
529 | TypeTable::iterator existing = tables->typeTable.find( id ); |
---|
530 | if ( existing == tables->typeTable.end() ) { |
---|
531 | NamedTypeDecl *parent = tables->base.lookupTypeAtScope( id, scope ); |
---|
532 | if ( ! parent || ! addedTypeConflicts( parent, decl ) ) { |
---|
533 | tables->typeTable.insert( existing, std::make_pair( id, decl ) ); |
---|
534 | ++tables->size; |
---|
535 | } |
---|
536 | } else { |
---|
537 | if ( ! addedTypeConflicts( existing->second, decl ) ) { |
---|
538 | existing->second = decl; |
---|
539 | } |
---|
540 | } |
---|
541 | } |
---|
542 | |
---|
543 | bool addedDeclConflicts( AggregateDecl *existing, AggregateDecl *added ) { |
---|
544 | if ( ! existing->body ) { |
---|
545 | return false; |
---|
546 | } else if ( added->body ) { |
---|
547 | SemanticError( added, "redeclaration of " ); |
---|
548 | } // if |
---|
549 | return true; |
---|
550 | } |
---|
551 | |
---|
552 | void Indexer::addStruct( const std::string &id ) { |
---|
553 | debugPrint( "Adding fwd decl for struct " << id << std::endl ); |
---|
554 | addStruct( new StructDecl( id ) ); |
---|
555 | } |
---|
556 | |
---|
557 | void Indexer::addStruct( StructDecl *decl ) { |
---|
558 | debugPrint( "Adding struct " << decl->name << std::endl ); |
---|
559 | makeWritable(); |
---|
560 | |
---|
561 | const std::string &id = decl->name; |
---|
562 | StructTable::iterator existing = tables->structTable.find( id ); |
---|
563 | if ( existing == tables->structTable.end() ) { |
---|
564 | StructDecl *parent = tables->base.lookupStructAtScope( id, scope ); |
---|
565 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
566 | tables->structTable.insert( existing, std::make_pair( id, decl ) ); |
---|
567 | ++tables->size; |
---|
568 | } |
---|
569 | } else { |
---|
570 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
571 | existing->second = decl; |
---|
572 | } |
---|
573 | } |
---|
574 | } |
---|
575 | |
---|
576 | void Indexer::addEnum( EnumDecl *decl ) { |
---|
577 | debugPrint( "Adding enum " << decl->name << std::endl ); |
---|
578 | makeWritable(); |
---|
579 | |
---|
580 | const std::string &id = decl->name; |
---|
581 | EnumTable::iterator existing = tables->enumTable.find( id ); |
---|
582 | if ( existing == tables->enumTable.end() ) { |
---|
583 | EnumDecl *parent = tables->base.lookupEnumAtScope( id, scope ); |
---|
584 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
585 | tables->enumTable.insert( existing, std::make_pair( id, decl ) ); |
---|
586 | ++tables->size; |
---|
587 | } |
---|
588 | } else { |
---|
589 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
590 | existing->second = decl; |
---|
591 | } |
---|
592 | } |
---|
593 | } |
---|
594 | |
---|
595 | void Indexer::addUnion( const std::string &id ) { |
---|
596 | debugPrint( "Adding fwd decl for union " << id << std::endl ); |
---|
597 | addUnion( new UnionDecl( id ) ); |
---|
598 | } |
---|
599 | |
---|
600 | void Indexer::addUnion( UnionDecl *decl ) { |
---|
601 | debugPrint( "Adding union " << decl->name << std::endl ); |
---|
602 | makeWritable(); |
---|
603 | |
---|
604 | const std::string &id = decl->name; |
---|
605 | UnionTable::iterator existing = tables->unionTable.find( id ); |
---|
606 | if ( existing == tables->unionTable.end() ) { |
---|
607 | UnionDecl *parent = tables->base.lookupUnionAtScope( id, scope ); |
---|
608 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
609 | tables->unionTable.insert( existing, std::make_pair( id, decl ) ); |
---|
610 | ++tables->size; |
---|
611 | } |
---|
612 | } else { |
---|
613 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
614 | existing->second = decl; |
---|
615 | } |
---|
616 | } |
---|
617 | } |
---|
618 | |
---|
619 | void Indexer::addTrait( TraitDecl *decl ) { |
---|
620 | debugPrint( "Adding trait " << decl->name << std::endl ); |
---|
621 | makeWritable(); |
---|
622 | |
---|
623 | const std::string &id = decl->name; |
---|
624 | TraitTable::iterator existing = tables->traitTable.find( id ); |
---|
625 | if ( existing == tables->traitTable.end() ) { |
---|
626 | TraitDecl *parent = tables->base.lookupTraitAtScope( id, scope ); |
---|
627 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
628 | tables->traitTable.insert( existing, std::make_pair( id, decl ) ); |
---|
629 | ++tables->size; |
---|
630 | } |
---|
631 | } else { |
---|
632 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
633 | existing->second = decl; |
---|
634 | } |
---|
635 | } |
---|
636 | } |
---|
637 | |
---|
638 | void Indexer::addMembers( AggregateDecl * aggr, Expression * expr, ConflictFunction handleConflicts ) { |
---|
639 | for ( Declaration * decl : aggr->members ) { |
---|
640 | if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { |
---|
641 | addId( dwt, handleConflicts, expr ); |
---|
642 | if ( dwt->name == "" ) { |
---|
643 | Type * t = dwt->get_type()->stripReferences(); |
---|
644 | if ( dynamic_cast< StructInstType * >( t ) || dynamic_cast< UnionInstType * >( t ) ) { |
---|
645 | Expression * base = expr->clone(); |
---|
646 | ResolvExpr::Cost cost = ResolvExpr::Cost::zero; // xxx - carry this cost into the indexer as a base cost? |
---|
647 | ResolvExpr::referenceToRvalueConversion( base, cost ); |
---|
648 | addMembers( t->getAggr(), new MemberExpr( dwt, base ), handleConflicts ); |
---|
649 | } |
---|
650 | } |
---|
651 | } |
---|
652 | } |
---|
653 | } |
---|
654 | |
---|
655 | void Indexer::addWith( std::list< Expression * > & withExprs, BaseSyntaxNode * withStmt ) { |
---|
656 | for ( Expression * expr : withExprs ) { |
---|
657 | if ( expr->result ) { |
---|
658 | AggregateDecl * aggr = expr->result->stripReferences()->getAggr(); |
---|
659 | assertf( aggr, "WithStmt expr has non-aggregate type: %s", toString( expr->result ).c_str() ); |
---|
660 | |
---|
661 | addMembers( aggr, expr, [withStmt](IdData & existing, const std::string &) { |
---|
662 | // on conflict, delete the identifier |
---|
663 | existing.deleteStmt = withStmt; |
---|
664 | return true; |
---|
665 | }); |
---|
666 | } |
---|
667 | } |
---|
668 | } |
---|
669 | |
---|
670 | void Indexer::addIds( const std::list< DeclarationWithType * > & decls ) { |
---|
671 | for ( auto d : decls ) { |
---|
672 | addId( d ); |
---|
673 | } |
---|
674 | } |
---|
675 | |
---|
676 | void Indexer::addTypes( const std::list< TypeDecl * > & tds ) { |
---|
677 | for ( auto td : tds ) { |
---|
678 | addType( td ); |
---|
679 | addIds( td->assertions ); |
---|
680 | } |
---|
681 | } |
---|
682 | |
---|
683 | void Indexer::addFunctionType( FunctionType * ftype ) { |
---|
684 | addTypes( ftype->forall ); |
---|
685 | addIds( ftype->returnVals ); |
---|
686 | addIds( ftype->parameters ); |
---|
687 | } |
---|
688 | |
---|
689 | void Indexer::enterScope() { |
---|
690 | ++scope; |
---|
691 | |
---|
692 | if ( doDebug ) { |
---|
693 | std::cerr << "--- Entering scope " << scope << std::endl; |
---|
694 | } |
---|
695 | } |
---|
696 | |
---|
697 | void Indexer::leaveScope() { |
---|
698 | using std::cerr; |
---|
699 | |
---|
700 | assert( scope > 0 && "cannot leave initial scope" ); |
---|
701 | if ( doDebug ) { |
---|
702 | cerr << "--- Leaving scope " << scope << " containing" << std::endl; |
---|
703 | } |
---|
704 | --scope; |
---|
705 | |
---|
706 | while ( tables && tables->scope > scope ) { |
---|
707 | if ( doDebug ) { |
---|
708 | dump( tables->idTable, cerr ); |
---|
709 | dump( tables->typeTable, cerr ); |
---|
710 | dump( tables->structTable, cerr ); |
---|
711 | dump( tables->enumTable, cerr ); |
---|
712 | dump( tables->unionTable, cerr ); |
---|
713 | dump( tables->traitTable, cerr ); |
---|
714 | } |
---|
715 | |
---|
716 | // swap tables for base table until we find one at an appropriate scope |
---|
717 | Indexer::Impl *base = newRef( tables->base.tables ); |
---|
718 | deleteRef( tables ); |
---|
719 | tables = base; |
---|
720 | } |
---|
721 | } |
---|
722 | |
---|
723 | void Indexer::print( std::ostream &os, int indent ) const { |
---|
724 | using std::cerr; |
---|
725 | |
---|
726 | if ( tables ) { |
---|
727 | os << "--- scope " << tables->scope << " ---" << std::endl; |
---|
728 | |
---|
729 | os << "===idTable===" << std::endl; |
---|
730 | dump( tables->idTable, os ); |
---|
731 | os << "===typeTable===" << std::endl; |
---|
732 | dump( tables->typeTable, os ); |
---|
733 | os << "===structTable===" << std::endl; |
---|
734 | dump( tables->structTable, os ); |
---|
735 | os << "===enumTable===" << std::endl; |
---|
736 | dump( tables->enumTable, os ); |
---|
737 | os << "===unionTable===" << std::endl; |
---|
738 | dump( tables->unionTable, os ); |
---|
739 | os << "===contextTable===" << std::endl; |
---|
740 | dump( tables->traitTable, os ); |
---|
741 | |
---|
742 | tables->base.print( os, indent ); |
---|
743 | } else { |
---|
744 | os << "--- end ---" << std::endl; |
---|
745 | } |
---|
746 | |
---|
747 | } |
---|
748 | |
---|
749 | Expression * Indexer::IdData::combine( ResolvExpr::Cost & cost ) const { |
---|
750 | Expression * ret = nullptr; |
---|
751 | if ( baseExpr ) { |
---|
752 | Expression * base = baseExpr->clone(); |
---|
753 | ResolvExpr::referenceToRvalueConversion( base, cost ); |
---|
754 | ret = new MemberExpr( id, base ); |
---|
755 | // xxx - this introduces hidden environments, for now remove them. |
---|
756 | // std::swap( base->env, ret->env ); |
---|
757 | delete base->env; |
---|
758 | base->env = nullptr; |
---|
759 | } else { |
---|
760 | ret = new VariableExpr( id ); |
---|
761 | } |
---|
762 | if ( deleteStmt ) ret = new DeletedExpr( ret, deleteStmt ); |
---|
763 | return ret; |
---|
764 | } |
---|
765 | } // namespace SymTab |
---|
766 | |
---|
767 | // Local Variables: // |
---|
768 | // tab-width: 4 // |
---|
769 | // mode: c++ // |
---|
770 | // compile-command: "make install" // |
---|
771 | // End: // |
---|