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