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