source: src/SymTab/Indexer.cc@ 6fa409e

new-env
Last change on this file since 6fa409e was 1057e3d, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Fix removeSpecialOverrides to delete default constructor and field constructors, and omit copy constructor and assignment operator as necessary

  • Property mode set to 100644
File size: 27.6 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
[4e06c1e]11// Last Modified By : Peter A. Buhr
[6d49ea3]12// Last Modified On : Thu Aug 17 16:08:40 2017
13// Update Count : 20
[0dd3a2f]14//
15
[e8032b0]16#include "Indexer.h"
17
[e3e16bc]18#include <cassert> // for assert, strict_dynamic_cast
[30f9072]19#include <iostream> // for operator<<, basic_ostream, ostream
20#include <string> // for string, operator<<, operator!=
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
[9236060]25#include "CodeGen/OperatorTable.h" // for isCtorDtor, isCtorDtorAssign
[30f9072]26#include "Common/SemanticError.h" // for SemanticError
27#include "Common/utility.h" // for cloneAll
[3f024c9]28#include "GenPoly/GenPoly.h"
[30f9072]29#include "InitTweak/InitTweak.h" // for isConstructor, isCopyFunction, isC...
30#include "Mangler.h" // for Mangler
31#include "Parser/LinkageSpec.h" // for isMangled, isOverridable, Spec
32#include "ResolvExpr/typeops.h" // for typesCompatible
33#include "SynTree/Constant.h" // for Constant
34#include "SynTree/Declaration.h" // for DeclarationWithType, FunctionDecl
35#include "SynTree/Expression.h" // for Expression, ImplicitCopyCtorExpr
36#include "SynTree/Initializer.h" // for Initializer
37#include "SynTree/Statement.h" // for CompoundStmt, Statement, ForStmt (...
38#include "SynTree/Type.h" // for Type, StructInstType, UnionInstType
[1ba88a0]39
[522363e]40#define debugPrint(x) if ( doDebug ) { std::cerr << x; }
[51b73452]41
42namespace SymTab {
[a40d503]43 std::ostream & operator<<( std::ostream & out, const Indexer::IdData & data ) {
44 return out << "(" << data.id << "," << data.baseExpr << ")";
45 }
46
47 typedef std::unordered_map< std::string, Indexer::IdData > MangleTable;
[bed4c63e]48 typedef std::unordered_map< std::string, MangleTable > IdTable;
[52c2a72]49 typedef std::unordered_map< std::string, NamedTypeDecl* > TypeTable;
50 typedef std::unordered_map< std::string, StructDecl* > StructTable;
51 typedef std::unordered_map< std::string, EnumDecl* > EnumTable;
52 typedef std::unordered_map< std::string, UnionDecl* > UnionTable;
53 typedef std::unordered_map< std::string, TraitDecl* > TraitTable;
54
[bed4c63e]55 void dump( const IdTable &table, std::ostream &os ) {
56 for ( IdTable::const_iterator id = table.begin(); id != table.end(); ++id ) {
57 for ( MangleTable::const_iterator mangle = id->second.begin(); mangle != id->second.end(); ++mangle ) {
58 os << mangle->second << std::endl;
59 }
60 }
61 }
[743fbda]62
[52c2a72]63 template< typename Decl >
64 void dump( const std::unordered_map< std::string, Decl* > &table, std::ostream &os ) {
65 for ( typename std::unordered_map< std::string, Decl* >::const_iterator it = table.begin(); it != table.end(); ++it ) {
66 os << it->second << std::endl;
67 } // for
68 }
[743fbda]69
[e8032b0]70 struct Indexer::Impl {
[52c2a72]71 Impl( unsigned long _scope ) : refCount(1), scope( _scope ), size( 0 ), base(),
[e8032b0]72 idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {}
[52c2a72]73 Impl( unsigned long _scope, Indexer &&_base ) : refCount(1), scope( _scope ), size( 0 ), base( _base ),
[e8032b0]74 idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {}
75 unsigned long refCount; ///< Number of references to these tables
[52c2a72]76 unsigned long scope; ///< Scope these tables are associated with
[e8032b0]77 unsigned long size; ///< Number of elements stored in this table
78 const Indexer base; ///< Base indexer this extends
[743fbda]79
[e8032b0]80 IdTable idTable; ///< Identifier namespace
81 TypeTable typeTable; ///< Type namespace
82 StructTable structTable; ///< Struct namespace
83 EnumTable enumTable; ///< Enum namespace
84 UnionTable unionTable; ///< Union namespace
85 TraitTable traitTable; ///< Trait namespace
86 };
87
88 Indexer::Impl *Indexer::newRef( Indexer::Impl *toClone ) {
89 if ( ! toClone ) return 0;
90
91 // shorten the search chain by skipping empty links
92 Indexer::Impl *ret = toClone->size == 0 ? toClone->base.tables : toClone;
93 if ( ret ) { ++ret->refCount; }
94
95 return ret;
96 }
97
98 void Indexer::deleteRef( Indexer::Impl *toFree ) {
99 if ( ! toFree ) return;
100
101 if ( --toFree->refCount == 0 ) delete toFree;
102 }
103
[a40d503]104 void Indexer::removeSpecialOverrides( const std::string &id, std::list< IdData > & out ) const {
[ee1635c8]105 // only need to perform this step for constructors, destructors, and assignment functions
[bff227f]106 if ( ! CodeGen::isCtorDtorAssign( id ) ) return;
[1ba88a0]107
[0a75b77]108 // helpful data structure to organize properties for a type
[1ba88a0]109 struct ValueType {
[0a75b77]110 struct DeclBall { // properties for this particular decl
[a40d503]111 IdData decl;
[0a75b77]112 bool isUserDefinedFunc;
[1ba88a0]113 bool isCopyFunc;
114 };
115 // properties for this type
[ff03f5c]116 bool existsUserDefinedCopyFunc = false; // user-defined copy ctor found
[0a75b77]117 BaseSyntaxNode * deleteStmt = nullptr; // non-null if a user-defined function is found
[1ba88a0]118 std::list< DeclBall > decls;
119
120 // another FunctionDecl for the current type was found - determine
121 // if it has special properties and update data structure accordingly
[a40d503]122 ValueType & operator+=( IdData data ) {
123 DeclarationWithType * function = data.id;
[0a75b77]124 bool isUserDefinedFunc = ! LinkageSpec::isOverridable( function->linkage );
125 bool isCopyFunc = InitTweak::isCopyFunction( function, function->name );
126 decls.push_back( DeclBall{ data, isUserDefinedFunc, isCopyFunc } );
[ff03f5c]127 existsUserDefinedCopyFunc = existsUserDefinedCopyFunc || (isUserDefinedFunc && isCopyFunc);
[1057e3d]128 if ( isUserDefinedFunc && ! deleteStmt ) {
[0a75b77]129 // any user-defined function can act as an implicit delete statement for generated constructors.
130 // a delete stmt should not act as an implicit delete statement.
131 deleteStmt = data.id;
132 }
[1ba88a0]133 return *this;
134 }
135 }; // ValueType
136
[a40d503]137 std::list< IdData > copy;
[1ba88a0]138 copy.splice( copy.end(), out );
139
140 // organize discovered declarations by type
141 std::unordered_map< std::string, ValueType > funcMap;
[a40d503]142 for ( auto decl : copy ) {
143 if ( FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl.id ) ) {
[96812c0]144 std::list< DeclarationWithType * > & params = function->type->parameters;
[1ba88a0]145 assert( ! params.empty() );
[24670d2]146 // use base type of pointer, so that qualifiers on the pointer type aren't considered.
[ce8c12f]147 Type * base = InitTweak::getPointerBase( params.front()->get_type() );
148 assert( base );
[a40d503]149 funcMap[ Mangler::mangle( base ) ] += decl;
[1ba88a0]150 } else {
151 out.push_back( decl );
152 }
153 }
154
[ff03f5c]155 // if a type contains user defined ctor/dtor/assign, then special rules trigger, which determine
[0a75b77]156 // the set of ctor/dtor/assign that can be used by the requester. In particular, if the user defines
157 // a default ctor, then the generated default ctor is unavailable, likewise for copy ctor
158 // and dtor. If the user defines any ctor/dtor, then no generated field ctors are available.
159 // If the user defines any ctor then the generated default ctor is unavailable (intrinsic default
160 // ctor must be overridden exactly). If the user defines anything that looks like a copy constructor,
161 // then the generated copy constructor is unavailable, and likewise for the assignment operator.
[1ba88a0]162 for ( std::pair< const std::string, ValueType > & pair : funcMap ) {
163 ValueType & val = pair.second;
164 for ( ValueType::DeclBall ball : val.decls ) {
[0a75b77]165 bool isNotUserDefinedFunc = ! ball.isUserDefinedFunc && ball.decl.id->linkage != LinkageSpec::Intrinsic;
166 bool isCopyFunc = ball.isCopyFunc;
167 bool existsUserDefinedCopyFunc = val.existsUserDefinedCopyFunc;
[1057e3d]168
169 // only implicitly delete non-user defined functions that are not intrinsic, and are
170 // not copy functions (assignment or copy constructor). If a user-defined copy function exists,
171 // do not pass along the non-user-defined copy functions since signatures do not have to match,
172 // and the generated functions will often be cheaper.
173 if ( isNotUserDefinedFunc ) {
174 if ( isCopyFunc ) {
175 // Skip over non-user-defined copy functions when there is a user-defined copy function.
176 // Since their signatures do not have to be exact, deleting them is the wrong choice.
177 if ( existsUserDefinedCopyFunc ) continue;
178 } else {
179 // delete non-user-defined non-copy functions if applicable.
180 // deleteStmt will be non-null only if a user-defined function is found.
181 ball.decl.deleteStmt = val.deleteStmt;
182 }
[1ba88a0]183 }
[0a75b77]184 out.push_back( ball.decl );
[1ba88a0]185 }
186 }
187 }
188
[e8032b0]189 void Indexer::makeWritable() {
190 if ( ! tables ) {
[52c2a72]191 // create indexer if not yet set
192 tables = new Indexer::Impl( scope );
193 } else if ( tables->refCount > 1 || tables->scope != scope ) {
194 // make this indexer the base of a fresh indexer at the current scope
195 tables = new Indexer::Impl( scope, std::move( *this ) );
[e8032b0]196 }
197 }
198
[8b11840]199 Indexer::Indexer() : tables( 0 ), scope( 0 ) {}
[e8032b0]200
[8b11840]201 Indexer::Indexer( const Indexer &that ) : doDebug( that.doDebug ), tables( newRef( that.tables ) ), scope( that.scope ) {}
[e8032b0]202
[8b11840]203 Indexer::Indexer( Indexer &&that ) : doDebug( that.doDebug ), tables( that.tables ), scope( that.scope ) {
[e8032b0]204 that.tables = 0;
205 }
206
207 Indexer::~Indexer() {
208 deleteRef( tables );
209 }
210
211 Indexer& Indexer::operator= ( const Indexer &that ) {
212 deleteRef( tables );
213
214 tables = newRef( that.tables );
[52c2a72]215 scope = that.scope;
[e8032b0]216 doDebug = that.doDebug;
217
218 return *this;
219 }
220
221 Indexer& Indexer::operator= ( Indexer &&that ) {
222 deleteRef( tables );
223
224 tables = that.tables;
[52c2a72]225 scope = that.scope;
[e8032b0]226 doDebug = that.doDebug;
[17cd4eb]227
[e8032b0]228 that.tables = 0;
229
230 return *this;
231 }
[17cd4eb]232
[a40d503]233 void Indexer::lookupId( const std::string &id, std::list< IdData > &out ) const {
[5447e09]234 std::unordered_set< std::string > foundMangleNames;
[743fbda]235
[5447e09]236 Indexer::Impl *searchTables = tables;
237 while ( searchTables ) {
238
239 IdTable::const_iterator decls = searchTables->idTable.find( id );
240 if ( decls != searchTables->idTable.end() ) {
241 const MangleTable &mangleTable = decls->second;
242 for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) {
243 // mark the mangled name as found, skipping this insertion if a declaration for that name has already been found
244 if ( foundMangleNames.insert( decl->first ).second == false ) continue;
[743fbda]245
[5447e09]246 out.push_back( decl->second );
247 }
[bed4c63e]248 }
[743fbda]249
[5447e09]250 // get declarations from base indexers
251 searchTables = searchTables->base.tables;
[bed4c63e]252 }
[1ba88a0]253
254 // some special functions, e.g. constructors and destructors
255 // remove autogenerated functions when they are defined so that
256 // they can never be matched
257 removeSpecialOverrides( id, out );
[a08ba92]258 }
[bdd516a]259
[a08ba92]260 NamedTypeDecl *Indexer::lookupType( const std::string &id ) const {
[e8032b0]261 if ( ! tables ) return 0;
262
[52c2a72]263 TypeTable::const_iterator ret = tables->typeTable.find( id );
264 return ret != tables->typeTable.end() ? ret->second : tables->base.lookupType( id );
[a08ba92]265 }
[17cd4eb]266
[a08ba92]267 StructDecl *Indexer::lookupStruct( const std::string &id ) const {
[e8032b0]268 if ( ! tables ) return 0;
269
[52c2a72]270 StructTable::const_iterator ret = tables->structTable.find( id );
271 return ret != tables->structTable.end() ? ret->second : tables->base.lookupStruct( id );
[a08ba92]272 }
[17cd4eb]273
[a08ba92]274 EnumDecl *Indexer::lookupEnum( const std::string &id ) const {
[e8032b0]275 if ( ! tables ) return 0;
276
[52c2a72]277 EnumTable::const_iterator ret = tables->enumTable.find( id );
278 return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnum( id );
[a08ba92]279 }
[17cd4eb]280
[a08ba92]281 UnionDecl *Indexer::lookupUnion( const std::string &id ) const {
[e8032b0]282 if ( ! tables ) return 0;
283
[52c2a72]284 UnionTable::const_iterator ret = tables->unionTable.find( id );
285 return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnion( id );
[e8032b0]286 }
287
288 TraitDecl *Indexer::lookupTrait( const std::string &id ) const {
289 if ( ! tables ) return 0;
290
[52c2a72]291 TraitTable::const_iterator ret = tables->traitTable.find( id );
292 return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTrait( id );
293 }
294
[0ac366b]295 const Indexer::IdData * Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) const {
296 if ( ! tables ) return nullptr;
297 if ( tables->scope < scope ) return nullptr;
[bed4c63e]298
299 IdTable::const_iterator decls = tables->idTable.find( id );
300 if ( decls != tables->idTable.end() ) {
301 const MangleTable &mangleTable = decls->second;
302 MangleTable::const_iterator decl = mangleTable.find( mangleName );
[0ac366b]303 if ( decl != mangleTable.end() ) return &decl->second;
[bed4c63e]304 }
305
306 return tables->base.lookupIdAtScope( id, mangleName, scope );
307 }
308
[0ac366b]309 Indexer::IdData * Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) {
310 return const_cast<IdData *>(const_cast<const Indexer *>(this)->lookupIdAtScope( id, mangleName, scope ));
311 }
312
[09d1ad0]313 bool Indexer::hasIncompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const {
[bed4c63e]314 if ( ! tables ) return false;
[09d1ad0]315 if ( tables->scope < scope ) return false;
[bed4c63e]316
317 IdTable::const_iterator decls = tables->idTable.find( id );
318 if ( decls != tables->idTable.end() ) {
319 const MangleTable &mangleTable = decls->second;
320 for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) {
[4e06c1e]321 // check for C decls with the same name, skipping those with a compatible type (by mangleName)
[a40d503]322 if ( ! LinkageSpec::isMangled( decl->second.id->get_linkage() ) && decl->first != mangleName ) return true;
[bed4c63e]323 }
324 }
325
[09d1ad0]326 return tables->base.hasIncompatibleCDecl( id, mangleName, scope );
[bed4c63e]327 }
[743fbda]328
[8884112]329 bool Indexer::hasCompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const {
330 if ( ! tables ) return false;
331 if ( tables->scope < scope ) return false;
332
333 IdTable::const_iterator decls = tables->idTable.find( id );
334 if ( decls != tables->idTable.end() ) {
335 const MangleTable &mangleTable = decls->second;
336 for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) {
337 // check for C decls with the same name, skipping
338 // those with an incompatible type (by mangleName)
[a40d503]339 if ( ! LinkageSpec::isMangled( decl->second.id->get_linkage() ) && decl->first == mangleName ) return true;
[8884112]340 }
341 }
342
343 return tables->base.hasCompatibleCDecl( id, mangleName, scope );
344 }
345
[52c2a72]346 NamedTypeDecl *Indexer::lookupTypeAtScope( const std::string &id, unsigned long scope ) const {
347 if ( ! tables ) return 0;
348 if ( tables->scope < scope ) return 0;
349
350 TypeTable::const_iterator ret = tables->typeTable.find( id );
351 return ret != tables->typeTable.end() ? ret->second : tables->base.lookupTypeAtScope( id, scope );
352 }
[743fbda]353
[52c2a72]354 StructDecl *Indexer::lookupStructAtScope( const std::string &id, unsigned long scope ) const {
355 if ( ! tables ) return 0;
356 if ( tables->scope < scope ) return 0;
357
358 StructTable::const_iterator ret = tables->structTable.find( id );
359 return ret != tables->structTable.end() ? ret->second : tables->base.lookupStructAtScope( id, scope );
360 }
[743fbda]361
[52c2a72]362 EnumDecl *Indexer::lookupEnumAtScope( const std::string &id, unsigned long scope ) const {
363 if ( ! tables ) return 0;
364 if ( tables->scope < scope ) return 0;
365
366 EnumTable::const_iterator ret = tables->enumTable.find( id );
367 return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnumAtScope( id, scope );
368 }
[743fbda]369
[52c2a72]370 UnionDecl *Indexer::lookupUnionAtScope( const std::string &id, unsigned long scope ) const {
371 if ( ! tables ) return 0;
372 if ( tables->scope < scope ) return 0;
373
374 UnionTable::const_iterator ret = tables->unionTable.find( id );
375 return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnionAtScope( id, scope );
376 }
[743fbda]377
[52c2a72]378 TraitDecl *Indexer::lookupTraitAtScope( const std::string &id, unsigned long scope ) const {
379 if ( ! tables ) return 0;
380 if ( tables->scope < scope ) return 0;
381
382 TraitTable::const_iterator ret = tables->traitTable.find( id );
383 return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTraitAtScope( id, scope );
[a08ba92]384 }
[17cd4eb]385
[3f024c9]386 bool isFunction( DeclarationWithType * decl ) {
387 return GenPoly::getFunctionType( decl->get_type() );
388 }
389
390 bool isObject( DeclarationWithType * decl ) {
391 return ! isFunction( decl );
392 }
393
394 bool isDefinition( DeclarationWithType * decl ) {
395 if ( FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl ) ) {
396 // a function is a definition if it has a body
397 return func->statements;
398 } else {
399 // an object is a definition if it is not marked extern.
400 // both objects must be marked extern
401 return ! decl->get_storageClasses().is_extern;
402 }
403 }
404
[0ac366b]405 bool addedIdConflicts( Indexer::IdData & existing, DeclarationWithType *added, BaseSyntaxNode * deleteStmt, Indexer::ConflictFunction handleConflicts ) {
[bed4c63e]406 // if we're giving the same name mangling to things of different types then there is something wrong
[3f024c9]407 assert( (isObject( added ) && isObject( existing.id ) )
408 || ( isFunction( added ) && isFunction( existing.id ) ) );
[bed4c63e]409
[0ac366b]410 if ( LinkageSpec::isOverridable( existing.id->get_linkage() ) ) {
[bed4c63e]411 // new definition shadows the autogenerated one, even at the same scope
412 return false;
[0ac366b]413 } else if ( LinkageSpec::isMangled( added->get_linkage() ) || ResolvExpr::typesCompatible( added->get_type(), existing.id->get_type(), Indexer() ) ) {
414
415 // it is a conflict if one declaration is deleted and the other is not
416 if ( deleteStmt && ! existing.deleteStmt ) {
417 return handleConflicts( existing, "deletion of defined identifier " );
418 } else if ( ! deleteStmt && existing.deleteStmt ) {
419 return handleConflicts( existing, "definition of deleted identifier " );
420 }
421
[3f024c9]422 if ( isDefinition( added ) && isDefinition( existing.id ) ) {
423 if ( isFunction( added ) ) {
[0ac366b]424 return handleConflicts( existing, "duplicate function definition for " );
[3f024c9]425 } else {
[0ac366b]426 return handleConflicts( existing, "duplicate object definition for " );
[bed4c63e]427 } // if
428 } // if
429 } else {
[0ac366b]430 return handleConflicts( existing, "duplicate definition for " );
[bed4c63e]431 } // if
432
433 return true;
434 }
[743fbda]435
[0ac366b]436 void Indexer::addId( DeclarationWithType *decl, ConflictFunction handleConflicts, Expression * baseExpr, BaseSyntaxNode * deleteStmt ) {
[2cb70aa]437 if ( decl->name == "" ) return;
[33a25f9]438 debugPrint( "Adding Id " << decl->name << std::endl );
[e8032b0]439 makeWritable();
[bed4c63e]440
[6fc5c14]441 const std::string &name = decl->name;
[bed4c63e]442 std::string mangleName;
[6fc5c14]443 if ( LinkageSpec::isOverridable( decl->linkage ) ) {
[bed4c63e]444 // mangle the name without including the appropriate suffix, so overridable routines are placed into the
445 // same "bucket" as their user defined versions.
446 mangleName = Mangler::mangle( decl, false );
447 } else {
448 mangleName = Mangler::mangle( decl );
449 } // if
450
[8884112]451 // this ensures that no two declarations with the same unmangled name at the same scope both have C linkage
[6fc5c14]452 if ( ! LinkageSpec::isMangled( decl->linkage ) ) {
[8884112]453 // NOTE this is broken in Richard's original code in such a way that it never triggers (it
454 // doesn't check decls that have the same manglename, and all C-linkage decls are defined to
455 // have their name as their manglename, hence the error can never trigger).
456 // The code here is closer to correct, but name mangling would have to be completely
457 // isomorphic to C type-compatibility, which it may not be.
458 if ( hasIncompatibleCDecl( name, mangleName, scope ) ) {
[a16764a6]459 SemanticError( decl, "conflicting overload of C function " );
[8884112]460 }
461 } else {
[490ff5c3]462 // Check that a Cforall declaration doesn't override any C declaration
[8884112]463 if ( hasCompatibleCDecl( name, mangleName, scope ) ) {
[a16764a6]464 SemanticError( decl, "Cforall declaration hides C function " );
[8884112]465 }
[bed4c63e]466 }
[8884112]467
468 // Skip repeat declarations of the same identifier
[0ac366b]469 IdData * existing = lookupIdAtScope( name, mangleName, scope );
470 if ( existing && existing->id && addedIdConflicts( *existing, decl, deleteStmt, handleConflicts ) ) return;
[8884112]471
472 // add to indexer
[78d69da7]473 tables->idTable[ name ][ mangleName ] = IdData{ decl, baseExpr, deleteStmt };
[8884112]474 ++tables->size;
[e8032b0]475 }
[52c2a72]476
[0ac366b]477 void Indexer::addId( DeclarationWithType * decl, Expression * baseExpr ) {
478 // default handling of conflicts is to raise an error
[3ed994e]479 addId( decl, [decl](IdData &, const std::string & msg) { SemanticError( decl, msg ); return true; }, baseExpr, decl->isDeleted ? decl : nullptr );
[0ac366b]480 }
481
482 void Indexer::addDeletedId( DeclarationWithType * decl, BaseSyntaxNode * deleteStmt ) {
483 // default handling of conflicts is to raise an error
[a16764a6]484 addId( decl, [decl](IdData &, const std::string & msg) { SemanticError( decl, msg ); return true; }, nullptr, deleteStmt );
[0ac366b]485 }
486
[52c2a72]487 bool addedTypeConflicts( NamedTypeDecl *existing, NamedTypeDecl *added ) {
488 if ( existing->get_base() == 0 ) {
489 return false;
490 } else if ( added->get_base() == 0 ) {
491 return true;
492 } else {
[a16764a6]493 SemanticError( added, "redeclaration of " );
[52c2a72]494 }
495 }
[743fbda]496
[e8032b0]497 void Indexer::addType( NamedTypeDecl *decl ) {
[33a25f9]498 debugPrint( "Adding type " << decl->name << std::endl );
[e8032b0]499 makeWritable();
[52c2a72]500
501 const std::string &id = decl->get_name();
502 TypeTable::iterator existing = tables->typeTable.find( id );
503 if ( existing == tables->typeTable.end() ) {
504 NamedTypeDecl *parent = tables->base.lookupTypeAtScope( id, scope );
505 if ( ! parent || ! addedTypeConflicts( parent, decl ) ) {
506 tables->typeTable.insert( existing, std::make_pair( id, decl ) );
507 ++tables->size;
508 }
509 } else {
510 if ( ! addedTypeConflicts( existing->second, decl ) ) {
511 existing->second = decl;
512 }
513 }
514 }
515
516 bool addedDeclConflicts( AggregateDecl *existing, AggregateDecl *added ) {
[b2da0574]517 if ( ! existing->body ) {
[52c2a72]518 return false;
[b2da0574]519 } else if ( added->body ) {
[a16764a6]520 SemanticError( added, "redeclaration of " );
[52c2a72]521 } // if
522 return true;
[e8032b0]523 }
524
525 void Indexer::addStruct( const std::string &id ) {
[33a25f9]526 debugPrint( "Adding fwd decl for struct " << id << std::endl );
[52c2a72]527 addStruct( new StructDecl( id ) );
[e8032b0]528 }
[743fbda]529
[e8032b0]530 void Indexer::addStruct( StructDecl *decl ) {
[33a25f9]531 debugPrint( "Adding struct " << decl->name << std::endl );
[e8032b0]532 makeWritable();
[52c2a72]533
534 const std::string &id = decl->get_name();
535 StructTable::iterator existing = tables->structTable.find( id );
536 if ( existing == tables->structTable.end() ) {
537 StructDecl *parent = tables->base.lookupStructAtScope( id, scope );
538 if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
539 tables->structTable.insert( existing, std::make_pair( id, decl ) );
540 ++tables->size;
541 }
542 } else {
543 if ( ! addedDeclConflicts( existing->second, decl ) ) {
544 existing->second = decl;
545 }
546 }
[e8032b0]547 }
[743fbda]548
[e8032b0]549 void Indexer::addEnum( EnumDecl *decl ) {
[33a25f9]550 debugPrint( "Adding enum " << decl->name << std::endl );
[e8032b0]551 makeWritable();
[52c2a72]552
553 const std::string &id = decl->get_name();
554 EnumTable::iterator existing = tables->enumTable.find( id );
555 if ( existing == tables->enumTable.end() ) {
556 EnumDecl *parent = tables->base.lookupEnumAtScope( id, scope );
557 if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
558 tables->enumTable.insert( existing, std::make_pair( id, decl ) );
559 ++tables->size;
560 }
561 } else {
562 if ( ! addedDeclConflicts( existing->second, decl ) ) {
563 existing->second = decl;
564 }
565 }
[e8032b0]566 }
567
568 void Indexer::addUnion( const std::string &id ) {
[33a25f9]569 debugPrint( "Adding fwd decl for union " << id << std::endl );
[52c2a72]570 addUnion( new UnionDecl( id ) );
[e8032b0]571 }
[743fbda]572
[e8032b0]573 void Indexer::addUnion( UnionDecl *decl ) {
[33a25f9]574 debugPrint( "Adding union " << decl->name << std::endl );
[e8032b0]575 makeWritable();
[52c2a72]576
577 const std::string &id = decl->get_name();
578 UnionTable::iterator existing = tables->unionTable.find( id );
579 if ( existing == tables->unionTable.end() ) {
580 UnionDecl *parent = tables->base.lookupUnionAtScope( id, scope );
581 if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
582 tables->unionTable.insert( existing, std::make_pair( id, decl ) );
583 ++tables->size;
584 }
585 } else {
586 if ( ! addedDeclConflicts( existing->second, decl ) ) {
587 existing->second = decl;
588 }
589 }
[e8032b0]590 }
[743fbda]591
[e8032b0]592 void Indexer::addTrait( TraitDecl *decl ) {
[33a25f9]593 debugPrint( "Adding trait " << decl->name << std::endl );
[e8032b0]594 makeWritable();
[52c2a72]595
596 const std::string &id = decl->get_name();
597 TraitTable::iterator existing = tables->traitTable.find( id );
598 if ( existing == tables->traitTable.end() ) {
599 TraitDecl *parent = tables->base.lookupTraitAtScope( id, scope );
600 if ( ! parent || ! addedDeclConflicts( parent, decl ) ) {
601 tables->traitTable.insert( existing, std::make_pair( id, decl ) );
602 ++tables->size;
603 }
604 } else {
605 if ( ! addedDeclConflicts( existing->second, decl ) ) {
606 existing->second = decl;
607 }
608 }
[a08ba92]609 }
[17cd4eb]610
[0ac366b]611 void Indexer::addMembers( AggregateDecl * aggr, Expression * expr, ConflictFunction handleConflicts ) {
[1485c1a]612 for ( Declaration * decl : aggr->members ) {
613 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
[0ac366b]614 addId( dwt, handleConflicts, expr );
[1485c1a]615 if ( dwt->name == "" ) {
616 Type * t = dwt->get_type()->stripReferences();
617 if ( dynamic_cast< StructInstType * >( t ) || dynamic_cast< UnionInstType * >( t ) ) {
618 Expression * base = expr->clone();
[a181494]619 ResolvExpr::Cost cost = ResolvExpr::Cost::zero; // xxx - carry this cost into the indexer as a base cost?
620 ResolvExpr::referenceToRvalueConversion( base, cost );
[0ac366b]621 addMembers( t->getAggr(), new MemberExpr( dwt, base ), handleConflicts );
[1485c1a]622 }
623 }
624 }
625 }
626 }
627
[0ac366b]628 void Indexer::addWith( std::list< Expression * > & withExprs, BaseSyntaxNode * withStmt ) {
[4670c79]629 for ( Expression * expr : withExprs ) {
[81644e0]630 if ( expr->result ) {
[497282e]631 AggregateDecl * aggr = expr->result->stripReferences()->getAggr();
[81644e0]632 assertf( aggr, "WithStmt expr has non-aggregate type: %s", toString( expr->result ).c_str() );
633
[0ac366b]634 addMembers( aggr, expr, [withStmt](IdData & existing, const std::string &) {
635 // on conflict, delete the identifier
636 existing.deleteStmt = withStmt;
637 return true;
638 });
[81644e0]639 }
640 }
641 }
642
[5fe35d6]643 void Indexer::addIds( const std::list< DeclarationWithType * > & decls ) {
644 for ( auto d : decls ) {
645 addId( d );
646 }
647 }
648
649 void Indexer::addTypes( const std::list< TypeDecl * > & tds ) {
650 for ( auto td : tds ) {
651 addType( td );
652 addIds( td->assertions );
653 }
654 }
655
656 void Indexer::addFunctionType( FunctionType * ftype ) {
657 addTypes( ftype->forall );
658 addIds( ftype->returnVals );
659 addIds( ftype->parameters );
660 }
661
[a08ba92]662 void Indexer::enterScope() {
[52c2a72]663 ++scope;
[743fbda]664
[0dd3a2f]665 if ( doDebug ) {
[6137fbb]666 std::cerr << "--- Entering scope " << scope << std::endl;
[0dd3a2f]667 }
[a08ba92]668 }
[17cd4eb]669
[a08ba92]670 void Indexer::leaveScope() {
[6137fbb]671 using std::cerr;
[52c2a72]672
673 assert( scope > 0 && "cannot leave initial scope" );
[6137fbb]674 if ( doDebug ) {
675 cerr << "--- Leaving scope " << scope << " containing" << std::endl;
676 }
[52c2a72]677 --scope;
678
679 while ( tables && tables->scope > scope ) {
680 if ( doDebug ) {
[6137fbb]681 dump( tables->idTable, cerr );
682 dump( tables->typeTable, cerr );
683 dump( tables->structTable, cerr );
684 dump( tables->enumTable, cerr );
685 dump( tables->unionTable, cerr );
686 dump( tables->traitTable, cerr );
[52c2a72]687 }
688
689 // swap tables for base table until we find one at an appropriate scope
690 Indexer::Impl *base = newRef( tables->base.tables );
691 deleteRef( tables );
692 tables = base;
[0dd3a2f]693 }
[a08ba92]694 }
[17cd4eb]695
[a08ba92]696 void Indexer::print( std::ostream &os, int indent ) const {
[490ff5c3]697 using std::cerr;
[e8032b0]698
[0cb1d61]699 if ( tables ) {
700 os << "--- scope " << tables->scope << " ---" << std::endl;
701
702 os << "===idTable===" << std::endl;
703 dump( tables->idTable, os );
704 os << "===typeTable===" << std::endl;
705 dump( tables->typeTable, os );
706 os << "===structTable===" << std::endl;
707 dump( tables->structTable, os );
708 os << "===enumTable===" << std::endl;
709 dump( tables->enumTable, os );
710 os << "===unionTable===" << std::endl;
711 dump( tables->unionTable, os );
712 os << "===contextTable===" << std::endl;
713 dump( tables->traitTable, os );
714
715 tables->base.print( os, indent );
716 } else {
717 os << "--- end ---" << std::endl;
718 }
[743fbda]719
[a08ba92]720 }
[a40d503]721
[a181494]722 Expression * Indexer::IdData::combine( ResolvExpr::Cost & cost ) const {
[0ac366b]723 Expression * ret = nullptr;
[a40d503]724 if ( baseExpr ) {
725 Expression * base = baseExpr->clone();
[a181494]726 ResolvExpr::referenceToRvalueConversion( base, cost );
[0ac366b]727 ret = new MemberExpr( id, base );
[a40d503]728 // xxx - this introduces hidden environments, for now remove them.
729 // std::swap( base->env, ret->env );
730 delete base->env;
731 base->env = nullptr;
732 } else {
[0ac366b]733 ret = new VariableExpr( id );
[a40d503]734 }
[0ac366b]735 if ( deleteStmt ) ret = new DeletedExpr( ret, deleteStmt );
736 return ret;
[a40d503]737 }
[51b73452]738} // namespace SymTab
[0dd3a2f]739
740// Local Variables: //
741// tab-width: 4 //
742// mode: c++ //
743// compile-command: "make install" //
744// End: //
Note: See TracBrowser for help on using the repository browser.