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