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