source: src/SymTab/Indexer.cc@ 1528a2c

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 1528a2c was 1cb7fab2, checked in by tdelisle <tdelisle@…>, 7 years ago

Added better support for enabling/disabling/compiling-out statistics

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