source: src/SymTab/Indexer.cc@ fce4e31

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since fce4e31 was ef5b828, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Indexer now has const lookup by default

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