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 | // SymbolTable.cpp -- |
---|
8 | // |
---|
9 | // Author : Aaron B. Moss |
---|
10 | // Created On : Wed May 29 11:00:00 2019 |
---|
11 | // Last Modified By : Aaron B. Moss |
---|
12 | // Last Modified On : Wed May 29 11:00:00 2019 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #include "SymbolTable.hpp" |
---|
17 | |
---|
18 | #include <cassert> |
---|
19 | |
---|
20 | #include "Decl.hpp" |
---|
21 | #include "Expr.hpp" |
---|
22 | #include "Type.hpp" |
---|
23 | #include "CodeGen/OperatorTable.h" // for isCtorDtorAssign |
---|
24 | #include "Common/SemanticError.h" |
---|
25 | #include "Common/Stats/Counter.h" |
---|
26 | #include "GenPoly/GenPoly.h" |
---|
27 | #include "InitTweak/InitTweak.h" |
---|
28 | #include "ResolvExpr/Cost.h" |
---|
29 | #include "ResolvExpr/typeops.h" |
---|
30 | #include "SymTab/Mangler.h" |
---|
31 | |
---|
32 | namespace ast { |
---|
33 | |
---|
34 | // Statistics block |
---|
35 | namespace { |
---|
36 | static inline auto stats() { |
---|
37 | using namespace Stats::Counters; |
---|
38 | static auto group = build<CounterGroup>("Indexers"); |
---|
39 | static struct { |
---|
40 | SimpleCounter * count; |
---|
41 | AverageCounter<double> * size; |
---|
42 | SimpleCounter * new_scopes; |
---|
43 | SimpleCounter * lazy_scopes; |
---|
44 | AverageCounter<double> * avg_scope_depth; |
---|
45 | MaxCounter<size_t> * max_scope_depth; |
---|
46 | SimpleCounter * add_calls; |
---|
47 | SimpleCounter * lookup_calls; |
---|
48 | SimpleCounter * map_lookups; |
---|
49 | SimpleCounter * map_mutations; |
---|
50 | } ret = { |
---|
51 | .count = build<SimpleCounter>("Count", group), |
---|
52 | .size = build<AverageCounter<double>>("Average Size", group), |
---|
53 | .new_scopes = build<SimpleCounter>("Scopes", group), |
---|
54 | .lazy_scopes = build<SimpleCounter>("Lazy Scopes", group), |
---|
55 | .avg_scope_depth = build<AverageCounter<double>>("Average Scope", group), |
---|
56 | .max_scope_depth = build<MaxCounter<size_t>>("Max Scope", group), |
---|
57 | .add_calls = build<SimpleCounter>("Add Calls", group), |
---|
58 | .lookup_calls = build<SimpleCounter>("Lookup Calls", group), |
---|
59 | .map_lookups = build<SimpleCounter>("Map Lookups", group), |
---|
60 | .map_mutations = build<SimpleCounter>("Map Mutations", group) |
---|
61 | }; |
---|
62 | return ret; |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | Expr * SymbolTable::IdData::combine( const CodeLocation & loc, ResolvExpr::Cost & cost ) const { |
---|
67 | Expr * ret = ( baseExpr ) ? |
---|
68 | (Expr *)new MemberExpr{ loc, id, referenceToRvalueConversion( baseExpr, cost ) } : |
---|
69 | (Expr *)new VariableExpr{ loc, id }; |
---|
70 | if ( deleter ) { ret = new DeletedExpr{ loc, ret, deleter }; } |
---|
71 | return ret; |
---|
72 | } |
---|
73 | |
---|
74 | SymbolTable::SymbolTable() |
---|
75 | : idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable(), |
---|
76 | prevScope(), scope( 0 ), repScope( 0 ) { ++*stats().count; } |
---|
77 | |
---|
78 | SymbolTable::~SymbolTable() { stats().size->push( idTable ? idTable->size() : 0 ); } |
---|
79 | |
---|
80 | void SymbolTable::enterScope() { |
---|
81 | ++scope; |
---|
82 | |
---|
83 | ++*stats().new_scopes; |
---|
84 | stats().avg_scope_depth->push( scope ); |
---|
85 | stats().max_scope_depth->push( scope ); |
---|
86 | } |
---|
87 | |
---|
88 | void SymbolTable::leaveScope() { |
---|
89 | if ( repScope == scope ) { |
---|
90 | Ptr prev = prevScope; // make sure prevScope stays live |
---|
91 | *this = std::move(*prevScope); // replace with previous scope |
---|
92 | } |
---|
93 | |
---|
94 | --scope; |
---|
95 | } |
---|
96 | |
---|
97 | std::vector<SymbolTable::IdData> SymbolTable::lookupId( const std::string &id ) const { |
---|
98 | ++*stats().lookup_calls; |
---|
99 | if ( ! idTable ) return {}; |
---|
100 | |
---|
101 | ++*stats().map_lookups; |
---|
102 | auto decls = idTable->find( id ); |
---|
103 | if ( decls == idTable->end() ) return {}; |
---|
104 | |
---|
105 | std::vector<IdData> out; |
---|
106 | for ( auto decl : *(decls->second) ) { |
---|
107 | out.push_back( decl.second ); |
---|
108 | } |
---|
109 | return out; |
---|
110 | } |
---|
111 | |
---|
112 | const NamedTypeDecl * SymbolTable::lookupType( const std::string &id ) const { |
---|
113 | ++*stats().lookup_calls; |
---|
114 | if ( ! typeTable ) return nullptr; |
---|
115 | ++*stats().map_lookups; |
---|
116 | auto it = typeTable->find( id ); |
---|
117 | return it == typeTable->end() ? nullptr : it->second.decl; |
---|
118 | } |
---|
119 | |
---|
120 | const StructDecl * SymbolTable::lookupStruct( const std::string &id ) const { |
---|
121 | ++*stats().lookup_calls; |
---|
122 | if ( ! structTable ) return nullptr; |
---|
123 | ++*stats().map_lookups; |
---|
124 | auto it = structTable->find( id ); |
---|
125 | return it == structTable->end() ? nullptr : it->second.decl; |
---|
126 | } |
---|
127 | |
---|
128 | const EnumDecl * SymbolTable::lookupEnum( const std::string &id ) const { |
---|
129 | ++*stats().lookup_calls; |
---|
130 | if ( ! enumTable ) return nullptr; |
---|
131 | ++*stats().map_lookups; |
---|
132 | auto it = enumTable->find( id ); |
---|
133 | return it == enumTable->end() ? nullptr : it->second.decl; |
---|
134 | } |
---|
135 | |
---|
136 | const UnionDecl * SymbolTable::lookupUnion( const std::string &id ) const { |
---|
137 | ++*stats().lookup_calls; |
---|
138 | if ( ! unionTable ) return nullptr; |
---|
139 | ++*stats().map_lookups; |
---|
140 | auto it = unionTable->find( id ); |
---|
141 | return it == unionTable->end() ? nullptr : it->second.decl; |
---|
142 | } |
---|
143 | |
---|
144 | const TraitDecl * SymbolTable::lookupTrait( const std::string &id ) const { |
---|
145 | ++*stats().lookup_calls; |
---|
146 | if ( ! traitTable ) return nullptr; |
---|
147 | ++*stats().map_lookups; |
---|
148 | auto it = traitTable->find( id ); |
---|
149 | return it == traitTable->end() ? nullptr : it->second.decl; |
---|
150 | } |
---|
151 | |
---|
152 | const NamedTypeDecl * SymbolTable::globalLookupType( const std::string &id ) const { |
---|
153 | return atScope( 0 )->lookupType( id ); |
---|
154 | } |
---|
155 | |
---|
156 | const StructDecl * SymbolTable::globalLookupStruct( const std::string &id ) const { |
---|
157 | return atScope( 0 )->lookupStruct( id ); |
---|
158 | } |
---|
159 | |
---|
160 | const UnionDecl * SymbolTable::globalLookupUnion( const std::string &id ) const { |
---|
161 | return atScope( 0 )->lookupUnion( id ); |
---|
162 | } |
---|
163 | |
---|
164 | const EnumDecl * SymbolTable::globalLookupEnum( const std::string &id ) const { |
---|
165 | return atScope( 0 )->lookupEnum( id ); |
---|
166 | } |
---|
167 | |
---|
168 | void SymbolTable::addId( const DeclWithType * decl, const Expr * baseExpr ) { |
---|
169 | // default handling of conflicts is to raise an error |
---|
170 | addId( decl, OnConflict::error(), baseExpr, decl->isDeleted ? decl : nullptr ); |
---|
171 | } |
---|
172 | |
---|
173 | void SymbolTable::addDeletedId( const DeclWithType * decl, const Decl * deleter ) { |
---|
174 | // default handling of conflicts is to raise an error |
---|
175 | addId( decl, OnConflict::error(), nullptr, deleter ); |
---|
176 | } |
---|
177 | |
---|
178 | namespace { |
---|
179 | /// true if redeclaration conflict between two types |
---|
180 | bool addedTypeConflicts( const NamedTypeDecl * existing, const NamedTypeDecl * added ) { |
---|
181 | if ( existing->base == nullptr ) { |
---|
182 | return false; |
---|
183 | } else if ( added->base == nullptr ) { |
---|
184 | return true; |
---|
185 | } else { |
---|
186 | // typedef redeclarations are errors only if types are different |
---|
187 | if ( ! ResolvExpr::typesCompatible( existing->base, added->base, SymbolTable{} ) ) { |
---|
188 | SemanticError( added->location, "redeclaration of " + added->name ); |
---|
189 | } |
---|
190 | } |
---|
191 | // does not need to be added to the table if both existing and added have a base that are |
---|
192 | // the same |
---|
193 | return true; |
---|
194 | } |
---|
195 | |
---|
196 | /// true if redeclaration conflict between two aggregate declarations |
---|
197 | bool addedDeclConflicts( const AggregateDecl * existing, const AggregateDecl * added ) { |
---|
198 | if ( ! existing->body ) { |
---|
199 | return false; |
---|
200 | } else if ( added->body ) { |
---|
201 | SemanticError( added, "redeclaration of " ); |
---|
202 | } |
---|
203 | return true; |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | void SymbolTable::addType( const NamedTypeDecl * decl ) { |
---|
208 | ++*stats().add_calls; |
---|
209 | const std::string &id = decl->name; |
---|
210 | |
---|
211 | if ( ! typeTable ) { |
---|
212 | typeTable = TypeTable::new_ptr(); |
---|
213 | } else { |
---|
214 | ++*stats().map_lookups; |
---|
215 | auto existing = typeTable->find( id ); |
---|
216 | if ( existing != typeTable->end() |
---|
217 | && existing->second.scope == scope |
---|
218 | && addedTypeConflicts( existing->second.decl, decl ) ) return; |
---|
219 | } |
---|
220 | |
---|
221 | lazyInitScope(); |
---|
222 | ++*stats().map_mutations; |
---|
223 | typeTable = typeTable->set( id, scoped<NamedTypeDecl>{ decl, scope } ); |
---|
224 | } |
---|
225 | |
---|
226 | void SymbolTable::addStruct( const std::string &id ) { |
---|
227 | addStruct( new StructDecl( CodeLocation{}, id ) ); |
---|
228 | } |
---|
229 | |
---|
230 | void SymbolTable::addStruct( const StructDecl * decl ) { |
---|
231 | ++*stats().add_calls; |
---|
232 | const std::string &id = decl->name; |
---|
233 | |
---|
234 | if ( ! structTable ) { |
---|
235 | structTable = StructTable::new_ptr(); |
---|
236 | } else { |
---|
237 | ++*stats().map_lookups; |
---|
238 | auto existing = structTable->find( id ); |
---|
239 | if ( existing != structTable->end() |
---|
240 | && existing->second.scope == scope |
---|
241 | && addedDeclConflicts( existing->second.decl, decl ) ) return; |
---|
242 | } |
---|
243 | |
---|
244 | lazyInitScope(); |
---|
245 | ++*stats().map_mutations; |
---|
246 | structTable = structTable->set( id, scoped<StructDecl>{ decl, scope } ); |
---|
247 | } |
---|
248 | |
---|
249 | void SymbolTable::addEnum( const EnumDecl *decl ) { |
---|
250 | ++*stats().add_calls; |
---|
251 | const std::string &id = decl->name; |
---|
252 | |
---|
253 | if ( ! enumTable ) { |
---|
254 | enumTable = EnumTable::new_ptr(); |
---|
255 | } else { |
---|
256 | ++*stats().map_lookups; |
---|
257 | auto existing = enumTable->find( id ); |
---|
258 | if ( existing != enumTable->end() |
---|
259 | && existing->second.scope == scope |
---|
260 | && addedDeclConflicts( existing->second.decl, decl ) ) return; |
---|
261 | } |
---|
262 | |
---|
263 | lazyInitScope(); |
---|
264 | ++*stats().map_mutations; |
---|
265 | enumTable = enumTable->set( id, scoped<EnumDecl>{ decl, scope } ); |
---|
266 | } |
---|
267 | |
---|
268 | void SymbolTable::addUnion( const std::string &id ) { |
---|
269 | addUnion( new UnionDecl( CodeLocation{}, id ) ); |
---|
270 | } |
---|
271 | |
---|
272 | void SymbolTable::addUnion( const UnionDecl * decl ) { |
---|
273 | ++*stats().add_calls; |
---|
274 | const std::string &id = decl->name; |
---|
275 | |
---|
276 | if ( ! unionTable ) { |
---|
277 | unionTable = UnionTable::new_ptr(); |
---|
278 | } else { |
---|
279 | ++*stats().map_lookups; |
---|
280 | auto existing = unionTable->find( id ); |
---|
281 | if ( existing != unionTable->end() |
---|
282 | && existing->second.scope == scope |
---|
283 | && addedDeclConflicts( existing->second.decl, decl ) ) return; |
---|
284 | } |
---|
285 | |
---|
286 | lazyInitScope(); |
---|
287 | ++*stats().map_mutations; |
---|
288 | unionTable = unionTable->set( id, scoped<UnionDecl>{ decl, scope } ); |
---|
289 | } |
---|
290 | |
---|
291 | void SymbolTable::addTrait( const TraitDecl * decl ) { |
---|
292 | ++*stats().add_calls; |
---|
293 | const std::string &id = decl->name; |
---|
294 | |
---|
295 | if ( ! traitTable ) { |
---|
296 | traitTable = TraitTable::new_ptr(); |
---|
297 | } else { |
---|
298 | ++*stats().map_lookups; |
---|
299 | auto existing = traitTable->find( id ); |
---|
300 | if ( existing != traitTable->end() |
---|
301 | && existing->second.scope == scope |
---|
302 | && addedDeclConflicts( existing->second.decl, decl ) ) return; |
---|
303 | } |
---|
304 | |
---|
305 | lazyInitScope(); |
---|
306 | ++*stats().map_mutations; |
---|
307 | traitTable = traitTable->set( id, scoped<TraitDecl>{ decl, scope } ); |
---|
308 | } |
---|
309 | |
---|
310 | |
---|
311 | void SymbolTable::addWith( const std::vector< ptr<Expr> > & withExprs, const Decl * withStmt ) { |
---|
312 | for ( const Expr * expr : withExprs ) { |
---|
313 | if ( ! expr->result ) continue; |
---|
314 | const Type * resTy = expr->result->stripReferences(); |
---|
315 | auto aggrType = dynamic_cast< const BaseInstType * >( resTy ); |
---|
316 | assertf( aggrType, "WithStmt expr has non-aggregate type: %s", |
---|
317 | toString( expr->result ).c_str() ); |
---|
318 | const AggregateDecl * aggr = aggrType->aggr(); |
---|
319 | assertf( aggr, "WithStmt has null aggregate from type: %s", |
---|
320 | toString( expr->result ).c_str() ); |
---|
321 | |
---|
322 | addMembers( aggr, expr, OnConflict::deleteWith( withStmt ) ); |
---|
323 | } |
---|
324 | } |
---|
325 | |
---|
326 | void SymbolTable::addIds( const std::vector< ptr<DeclWithType> > & decls ) { |
---|
327 | for ( const DeclWithType * decl : decls ) { addId( decl ); } |
---|
328 | } |
---|
329 | |
---|
330 | void SymbolTable::addTypes( const std::vector< ptr<TypeDecl> > & tds ) { |
---|
331 | for ( const TypeDecl * td : tds ) { |
---|
332 | addType( td ); |
---|
333 | addIds( td->assertions ); |
---|
334 | } |
---|
335 | } |
---|
336 | |
---|
337 | |
---|
338 | void SymbolTable::addFunction( const FunctionDecl * func ) { |
---|
339 | addTypes( func->type->forall ); |
---|
340 | addIds( func->returns ); |
---|
341 | addIds( func->params ); |
---|
342 | } |
---|
343 | |
---|
344 | |
---|
345 | void SymbolTable::lazyInitScope() { |
---|
346 | // do nothing if already in represented scope |
---|
347 | if ( repScope == scope ) return; |
---|
348 | |
---|
349 | ++*stats().lazy_scopes; |
---|
350 | // create rollback |
---|
351 | prevScope = std::make_shared<SymbolTable>( *this ); |
---|
352 | // update repScope |
---|
353 | repScope = scope; |
---|
354 | } |
---|
355 | |
---|
356 | const ast::SymbolTable * SymbolTable::atScope( unsigned long target ) const { |
---|
357 | // by lazy construction, final symtab in list has repScope 0, cannot be > target |
---|
358 | // otherwise, will find first scope representing the target |
---|
359 | const SymbolTable * symtab = this; |
---|
360 | while ( symtab->repScope > target ) { |
---|
361 | symtab = symtab->prevScope.get(); |
---|
362 | } |
---|
363 | return symtab; |
---|
364 | } |
---|
365 | |
---|
366 | namespace { |
---|
367 | /// gets the base type of the first parameter; decl must be a ctor/dtor/assignment function |
---|
368 | std::string getOtypeKey( const FunctionDecl * function ) { |
---|
369 | const auto & params = function->type->params; |
---|
370 | assert( ! params.empty() ); |
---|
371 | // use base type of pointer, so that qualifiers on the pointer type aren't considered. |
---|
372 | const Type * base = InitTweak::getPointerBase( params.front() ); |
---|
373 | assert( base ); |
---|
374 | return Mangle::mangle( base ); |
---|
375 | } |
---|
376 | |
---|
377 | /// gets the declaration for the function acting on a type specified by otype key, |
---|
378 | /// nullptr if none such |
---|
379 | const FunctionDecl * getFunctionForOtype( |
---|
380 | const DeclWithType * decl, const std::string & otypeKey ) { |
---|
381 | auto func = dynamic_cast< const FunctionDecl * >( decl ); |
---|
382 | if ( ! func || otypeKey != getOtypeKey( func ) ) return nullptr; |
---|
383 | return func; |
---|
384 | } |
---|
385 | } |
---|
386 | |
---|
387 | bool SymbolTable::removeSpecialOverrides( |
---|
388 | SymbolTable::IdData & data, SymbolTable::MangleTable::Ptr & mangleTable ) { |
---|
389 | // if a type contains user defined ctor/dtor/assign, then special rules trigger, which |
---|
390 | // determine the set of ctor/dtor/assign that can be used by the requester. In particular, |
---|
391 | // if the user defines a default ctor, then the generated default ctor is unavailable, |
---|
392 | // likewise for copy ctor and dtor. If the user defines any ctor/dtor, then no generated |
---|
393 | // field ctors are available. If the user defines any ctor then the generated default ctor |
---|
394 | // is unavailable (intrinsic default ctor must be overridden exactly). If the user defines |
---|
395 | // anything that looks like a copy constructor, then the generated copy constructor is |
---|
396 | // unavailable, and likewise for the assignment operator. |
---|
397 | |
---|
398 | // only relevant on function declarations |
---|
399 | const FunctionDecl * function = data.id.as< FunctionDecl >(); |
---|
400 | if ( ! function ) return true; |
---|
401 | // only need to perform this check for constructors, destructors, and assignment functions |
---|
402 | if ( ! CodeGen::isCtorDtorAssign( data.id->name ) ) return true; |
---|
403 | |
---|
404 | // set up information for this type |
---|
405 | bool dataIsUserDefinedFunc = ! function->linkage.is_overrideable; |
---|
406 | bool dataIsCopyFunc = InitTweak::isCopyFunction( function ); |
---|
407 | std::string dataOtypeKey = getOtypeKey( function ); |
---|
408 | |
---|
409 | if ( dataIsUserDefinedFunc && dataIsCopyFunc ) { |
---|
410 | // this is a user-defined copy function |
---|
411 | // if this is the first such, delete/remove non-user-defined overloads as needed |
---|
412 | std::vector< std::string > removed; |
---|
413 | std::vector< MangleTable::value_type > deleted; |
---|
414 | bool alreadyUserDefinedFunc = false; |
---|
415 | |
---|
416 | for ( const auto& entry : *mangleTable ) { |
---|
417 | // skip decls that aren't functions or are for the wrong type |
---|
418 | const FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey ); |
---|
419 | if ( ! decl ) continue; |
---|
420 | |
---|
421 | bool isCopyFunc = InitTweak::isCopyFunction( decl ); |
---|
422 | if ( ! decl->linkage.is_overrideable ) { |
---|
423 | // matching user-defined function |
---|
424 | if ( isCopyFunc ) { |
---|
425 | // mutation already performed, return early |
---|
426 | return true; |
---|
427 | } else { |
---|
428 | // note that non-copy deletions already performed |
---|
429 | alreadyUserDefinedFunc = true; |
---|
430 | } |
---|
431 | } else { |
---|
432 | // non-user-defined function; mark for deletion/removal as appropriate |
---|
433 | if ( isCopyFunc ) { |
---|
434 | removed.push_back( entry.first ); |
---|
435 | } else if ( ! alreadyUserDefinedFunc ) { |
---|
436 | deleted.push_back( entry ); |
---|
437 | } |
---|
438 | } |
---|
439 | } |
---|
440 | |
---|
441 | // perform removals from mangle table, and deletions if necessary |
---|
442 | for ( const auto& key : removed ) { |
---|
443 | ++*stats().map_mutations; |
---|
444 | mangleTable = mangleTable->erase( key ); |
---|
445 | } |
---|
446 | if ( ! alreadyUserDefinedFunc ) for ( const auto& entry : deleted ) { |
---|
447 | ++*stats().map_mutations; |
---|
448 | mangleTable = mangleTable->set( entry.first, IdData{ entry.second, function } ); |
---|
449 | } |
---|
450 | } else if ( dataIsUserDefinedFunc ) { |
---|
451 | // this is a user-defined non-copy function |
---|
452 | // if this is the first user-defined function, delete non-user-defined overloads |
---|
453 | std::vector< MangleTable::value_type > deleted; |
---|
454 | |
---|
455 | for ( const auto& entry : *mangleTable ) { |
---|
456 | // skip decls that aren't functions or are for the wrong type |
---|
457 | const FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey ); |
---|
458 | if ( ! decl ) continue; |
---|
459 | |
---|
460 | // exit early if already a matching user-defined function; |
---|
461 | // earlier function will have mutated table |
---|
462 | if ( ! decl->linkage.is_overrideable ) return true; |
---|
463 | |
---|
464 | // skip mutating intrinsic functions |
---|
465 | if ( decl->linkage == Linkage::Intrinsic ) continue; |
---|
466 | |
---|
467 | // user-defined non-copy functions do not override copy functions |
---|
468 | if ( InitTweak::isCopyFunction( decl ) ) continue; |
---|
469 | |
---|
470 | // this function to be deleted after mangleTable iteration is complete |
---|
471 | deleted.push_back( entry ); |
---|
472 | } |
---|
473 | |
---|
474 | // mark deletions to update mangle table |
---|
475 | // this needs to be a separate loop because of iterator invalidation |
---|
476 | for ( const auto& entry : deleted ) { |
---|
477 | ++*stats().map_mutations; |
---|
478 | mangleTable = mangleTable->set( entry.first, IdData{ entry.second, function } ); |
---|
479 | } |
---|
480 | } else if ( function->linkage != Linkage::Intrinsic ) { |
---|
481 | // this is an overridable generated function |
---|
482 | // if there already exists a matching user-defined function, delete this appropriately |
---|
483 | for ( const auto& entry : *mangleTable ) { |
---|
484 | // skip decls that aren't functions or are for the wrong type |
---|
485 | const FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey ); |
---|
486 | if ( ! decl ) continue; |
---|
487 | |
---|
488 | // skip non-user-defined functions |
---|
489 | if ( decl->linkage.is_overrideable ) continue; |
---|
490 | |
---|
491 | if ( dataIsCopyFunc ) { |
---|
492 | // remove current function if exists a user-defined copy function |
---|
493 | // since the signatures for copy functions don't need to match exactly, using |
---|
494 | // a delete statement is the wrong approach |
---|
495 | if ( InitTweak::isCopyFunction( decl ) ) return false; |
---|
496 | } else { |
---|
497 | // mark current function deleted by first user-defined function found |
---|
498 | data.deleter = decl; |
---|
499 | return true; |
---|
500 | } |
---|
501 | } |
---|
502 | } |
---|
503 | |
---|
504 | // nothing (more) to fix, return true |
---|
505 | return true; |
---|
506 | } |
---|
507 | |
---|
508 | namespace { |
---|
509 | /// true iff the declaration represents a function |
---|
510 | bool isFunction( const DeclWithType * decl ) { |
---|
511 | return GenPoly::getFunctionType( decl->get_type() ); |
---|
512 | } |
---|
513 | |
---|
514 | bool isObject( const DeclWithType * decl ) { return ! isFunction( decl ); } |
---|
515 | |
---|
516 | /// true if the declaration represents a definition instead of a forward decl |
---|
517 | bool isDefinition( const DeclWithType * decl ) { |
---|
518 | if ( auto func = dynamic_cast< const FunctionDecl * >( decl ) ) { |
---|
519 | // a function is a definition if it has a body |
---|
520 | return func->stmts; |
---|
521 | } else { |
---|
522 | // an object is a definition if it is not marked extern |
---|
523 | return ! decl->storage.is_extern; |
---|
524 | } |
---|
525 | } |
---|
526 | } |
---|
527 | |
---|
528 | bool SymbolTable::addedIdConflicts( |
---|
529 | const SymbolTable::IdData & existing, const DeclWithType * added, |
---|
530 | SymbolTable::OnConflict handleConflicts, const Decl * deleter ) { |
---|
531 | // if we're giving the same name mangling to things of different types then there is something |
---|
532 | // wrong |
---|
533 | assert( (isObject( added ) && isObject( existing.id ) ) |
---|
534 | || ( isFunction( added ) && isFunction( existing.id ) ) ); |
---|
535 | |
---|
536 | if ( existing.id->linkage.is_overrideable ) { |
---|
537 | // new definition shadows the autogenerated one, even at the same scope |
---|
538 | return false; |
---|
539 | } else if ( existing.id->linkage.is_mangled |
---|
540 | || ResolvExpr::typesCompatible( |
---|
541 | added->get_type(), existing.id->get_type(), SymbolTable{} ) ) { |
---|
542 | |
---|
543 | // it is a conflict if one declaration is deleted and the other is not |
---|
544 | if ( deleter && ! existing.deleter ) { |
---|
545 | if ( handleConflicts.mode == OnConflict::Error ) { |
---|
546 | SemanticError( added, "deletion of defined identifier " ); |
---|
547 | } |
---|
548 | return true; |
---|
549 | } else if ( ! deleter && existing.deleter ) { |
---|
550 | if ( handleConflicts.mode == OnConflict::Error ) { |
---|
551 | SemanticError( added, "definition of deleted identifier " ); |
---|
552 | } |
---|
553 | return true; |
---|
554 | } |
---|
555 | |
---|
556 | // it is a conflict if both declarations are definitions |
---|
557 | if ( isDefinition( added ) && isDefinition( existing.id ) ) { |
---|
558 | if ( handleConflicts.mode == OnConflict::Error ) { |
---|
559 | SemanticError( added, |
---|
560 | isFunction( added ) ? |
---|
561 | "duplicate function definition for " : |
---|
562 | "duplicate object definition for " ); |
---|
563 | } |
---|
564 | return true; |
---|
565 | } |
---|
566 | } else { |
---|
567 | if ( handleConflicts.mode == OnConflict::Error ) { |
---|
568 | SemanticError( added, "duplicate definition for " ); |
---|
569 | } |
---|
570 | return true; |
---|
571 | } |
---|
572 | |
---|
573 | return true; |
---|
574 | } |
---|
575 | |
---|
576 | void SymbolTable::addId( |
---|
577 | const DeclWithType * decl, SymbolTable::OnConflict handleConflicts, const Expr * baseExpr, |
---|
578 | const Decl * deleter ) { |
---|
579 | ++*stats().add_calls; |
---|
580 | const std::string &name = decl->name; |
---|
581 | if ( name == "" ) return; |
---|
582 | |
---|
583 | std::string mangleName; |
---|
584 | if ( decl->linkage.is_overrideable ) { |
---|
585 | // mangle the name without including the appropriate suffix, so overridable routines |
---|
586 | // are placed into the same "bucket" as their user defined versions. |
---|
587 | mangleName = Mangle::mangle( decl, Mangle::Mode{ Mangle::NoOverrideable } ); |
---|
588 | } else { |
---|
589 | mangleName = Mangle::mangle( decl ); |
---|
590 | } |
---|
591 | |
---|
592 | // this ensures that no two declarations with the same unmangled name at the same scope |
---|
593 | // both have C linkage |
---|
594 | if ( decl->linkage.is_mangled ) { |
---|
595 | // Check that a Cforall declaration doesn't override any C declaration |
---|
596 | if ( hasCompatibleCDecl( name, mangleName ) ) { |
---|
597 | SemanticError( decl, "Cforall declaration hides C function " ); |
---|
598 | } |
---|
599 | } else { |
---|
600 | // NOTE: only correct if name mangling is completely isomorphic to C |
---|
601 | // type-compatibility, which it may not be. |
---|
602 | if ( hasIncompatibleCDecl( name, mangleName ) ) { |
---|
603 | SemanticError( decl, "conflicting overload of C function " ); |
---|
604 | } |
---|
605 | } |
---|
606 | |
---|
607 | // ensure tables exist and add identifier |
---|
608 | MangleTable::Ptr mangleTable; |
---|
609 | if ( ! idTable ) { |
---|
610 | idTable = IdTable::new_ptr(); |
---|
611 | mangleTable = MangleTable::new_ptr(); |
---|
612 | } else { |
---|
613 | ++*stats().map_lookups; |
---|
614 | auto decls = idTable->find( name ); |
---|
615 | if ( decls == idTable->end() ) { |
---|
616 | mangleTable = MangleTable::new_ptr(); |
---|
617 | } else { |
---|
618 | mangleTable = decls->second; |
---|
619 | // skip in-scope repeat declarations of same identifier |
---|
620 | ++*stats().map_lookups; |
---|
621 | auto existing = mangleTable->find( mangleName ); |
---|
622 | if ( existing != mangleTable->end() |
---|
623 | && existing->second.scope == scope |
---|
624 | && existing->second.id ) { |
---|
625 | if ( addedIdConflicts( existing->second, decl, handleConflicts, deleter ) ) { |
---|
626 | if ( handleConflicts.mode == OnConflict::Delete ) { |
---|
627 | // set delete expression for conflicting identifier |
---|
628 | lazyInitScope(); |
---|
629 | *stats().map_mutations += 2; |
---|
630 | idTable = idTable->set( |
---|
631 | name, |
---|
632 | mangleTable->set( |
---|
633 | mangleName, |
---|
634 | IdData{ existing->second, handleConflicts.deleter } ) ); |
---|
635 | } |
---|
636 | return; |
---|
637 | } |
---|
638 | } |
---|
639 | } |
---|
640 | } |
---|
641 | |
---|
642 | // add/overwrite with new identifier |
---|
643 | lazyInitScope(); |
---|
644 | IdData data{ decl, baseExpr, deleter, scope }; |
---|
645 | // Ensure that auto-generated ctor/dtor/assignment are deleted if necessary |
---|
646 | if ( ! removeSpecialOverrides( data, mangleTable ) ) return; |
---|
647 | *stats().map_mutations += 2; |
---|
648 | idTable = idTable->set( name, mangleTable->set( mangleName, std::move(data) ) ); |
---|
649 | } |
---|
650 | |
---|
651 | void SymbolTable::addMembers( |
---|
652 | const AggregateDecl * aggr, const Expr * expr, SymbolTable::OnConflict handleConflicts ) { |
---|
653 | for ( const Decl * decl : aggr->members ) { |
---|
654 | if ( auto dwt = dynamic_cast< const DeclWithType * >( decl ) ) { |
---|
655 | addId( dwt, handleConflicts, expr ); |
---|
656 | if ( dwt->name == "" ) { |
---|
657 | const Type * t = dwt->get_type()->stripReferences(); |
---|
658 | if ( auto rty = dynamic_cast<const BaseInstType *>( t ) ) { |
---|
659 | if ( ! dynamic_cast<const StructInstType *>(rty) |
---|
660 | && ! dynamic_cast<const UnionInstType *>(rty) ) continue; |
---|
661 | ResolvExpr::Cost cost = ResolvExpr::Cost::zero; |
---|
662 | const Expr * base = ResolvExpr::referenceToRvalueConversion( expr, cost ); |
---|
663 | addMembers( |
---|
664 | rty->aggr(), new MemberExpr{ base->location, dwt, base }, handleConflicts ); |
---|
665 | } |
---|
666 | } |
---|
667 | } |
---|
668 | } |
---|
669 | } |
---|
670 | |
---|
671 | bool SymbolTable::hasCompatibleCDecl( const std::string &id, const std::string &mangleName ) const { |
---|
672 | if ( ! idTable ) return false; |
---|
673 | |
---|
674 | ++*stats().map_lookups; |
---|
675 | auto decls = idTable->find( id ); |
---|
676 | if ( decls == idTable->end() ) return false; |
---|
677 | |
---|
678 | for ( auto decl : *(decls->second) ) { |
---|
679 | // skip other scopes (hidden by this decl) |
---|
680 | if ( decl.second.scope != scope ) continue; |
---|
681 | // check for C decl with compatible type (by mangleName) |
---|
682 | if ( ! decl.second.id->linkage.is_mangled && decl.first == mangleName ) return true; |
---|
683 | } |
---|
684 | |
---|
685 | return false; |
---|
686 | } |
---|
687 | |
---|
688 | bool SymbolTable::hasIncompatibleCDecl( const std::string &id, const std::string &mangleName ) const { |
---|
689 | if ( ! idTable ) return false; |
---|
690 | |
---|
691 | ++*stats().map_lookups; |
---|
692 | auto decls = idTable->find( id ); |
---|
693 | if ( decls == idTable->end() ) return false; |
---|
694 | |
---|
695 | for ( auto decl : *(decls->second) ) { |
---|
696 | // skip other scopes (hidden by this decl) |
---|
697 | if ( decl.second.scope != scope ) continue; |
---|
698 | // check for C decl with incompatible type (by manglename) |
---|
699 | if ( ! decl.second.id->linkage.is_mangled && decl.first != mangleName ) return true; |
---|
700 | } |
---|
701 | |
---|
702 | return false; |
---|
703 | } |
---|
704 | |
---|
705 | } |
---|
706 | |
---|
707 | // Local Variables: // |
---|
708 | // tab-width: 4 // |
---|
709 | // mode: c++ // |
---|
710 | // compile-command: "make install" // |
---|
711 | // End: // |
---|