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