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