[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 |
---|
[fbcde64] | 12 | // Last Modified On : Thu Mar 30 16:38:47 2017 |
---|
| 13 | // Update Count : 19 |
---|
[0dd3a2f] | 14 | // |
---|
| 15 | |
---|
[e8032b0] | 16 | #include "Indexer.h" |
---|
| 17 | |
---|
[52c2a72] | 18 | #include <string> |
---|
| 19 | #include <typeinfo> |
---|
| 20 | #include <unordered_map> |
---|
[5447e09] | 21 | #include <unordered_set> |
---|
[52c2a72] | 22 | #include <utility> |
---|
[1ba88a0] | 23 | #include <algorithm> |
---|
[52c2a72] | 24 | |
---|
[bed4c63e] | 25 | #include "Mangler.h" |
---|
| 26 | |
---|
| 27 | #include "Common/utility.h" |
---|
| 28 | |
---|
| 29 | #include "ResolvExpr/typeops.h" |
---|
[e8032b0] | 30 | |
---|
[51b7345] | 31 | #include "SynTree/Declaration.h" |
---|
| 32 | #include "SynTree/Type.h" |
---|
| 33 | #include "SynTree/Expression.h" |
---|
| 34 | #include "SynTree/Initializer.h" |
---|
| 35 | #include "SynTree/Statement.h" |
---|
[e8032b0] | 36 | |
---|
[1ba88a0] | 37 | #include "InitTweak/InitTweak.h" |
---|
| 38 | |
---|
[17cd4eb] | 39 | #define debugPrint(x) if ( doDebug ) { std::cout << x; } |
---|
[51b7345] | 40 | |
---|
| 41 | namespace SymTab { |
---|
[d5556a3] | 42 | struct NewScope { |
---|
| 43 | NewScope( SymTab::Indexer & indexer ) : indexer( indexer ) { indexer.enterScope(); } |
---|
| 44 | ~NewScope() { indexer.leaveScope(); } |
---|
| 45 | SymTab::Indexer & indexer; |
---|
| 46 | }; |
---|
| 47 | |
---|
[906e24d] | 48 | template< typename TreeType, typename VisitorType > |
---|
| 49 | inline void acceptNewScope( TreeType *tree, VisitorType &visitor ) { |
---|
[ae4c85a] | 50 | visitor.enterScope(); |
---|
[906e24d] | 51 | maybeAccept( tree, visitor ); |
---|
[ae4c85a] | 52 | visitor.leaveScope(); |
---|
| 53 | } |
---|
| 54 | |
---|
[bed4c63e] | 55 | typedef std::unordered_map< std::string, DeclarationWithType* > MangleTable; |
---|
| 56 | typedef std::unordered_map< std::string, MangleTable > IdTable; |
---|
[52c2a72] | 57 | typedef std::unordered_map< std::string, NamedTypeDecl* > TypeTable; |
---|
| 58 | typedef std::unordered_map< std::string, StructDecl* > StructTable; |
---|
| 59 | typedef std::unordered_map< std::string, EnumDecl* > EnumTable; |
---|
| 60 | typedef std::unordered_map< std::string, UnionDecl* > UnionTable; |
---|
| 61 | typedef std::unordered_map< std::string, TraitDecl* > TraitTable; |
---|
| 62 | |
---|
[bed4c63e] | 63 | void dump( const IdTable &table, std::ostream &os ) { |
---|
| 64 | for ( IdTable::const_iterator id = table.begin(); id != table.end(); ++id ) { |
---|
| 65 | for ( MangleTable::const_iterator mangle = id->second.begin(); mangle != id->second.end(); ++mangle ) { |
---|
| 66 | os << mangle->second << std::endl; |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | } |
---|
[743fbda] | 70 | |
---|
[52c2a72] | 71 | template< typename Decl > |
---|
| 72 | void dump( const std::unordered_map< std::string, Decl* > &table, std::ostream &os ) { |
---|
| 73 | for ( typename std::unordered_map< std::string, Decl* >::const_iterator it = table.begin(); it != table.end(); ++it ) { |
---|
| 74 | os << it->second << std::endl; |
---|
| 75 | } // for |
---|
| 76 | } |
---|
[743fbda] | 77 | |
---|
[e8032b0] | 78 | struct Indexer::Impl { |
---|
[52c2a72] | 79 | Impl( unsigned long _scope ) : refCount(1), scope( _scope ), size( 0 ), base(), |
---|
[e8032b0] | 80 | idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {} |
---|
[52c2a72] | 81 | Impl( unsigned long _scope, Indexer &&_base ) : refCount(1), scope( _scope ), size( 0 ), base( _base ), |
---|
[e8032b0] | 82 | idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {} |
---|
| 83 | unsigned long refCount; ///< Number of references to these tables |
---|
[52c2a72] | 84 | unsigned long scope; ///< Scope these tables are associated with |
---|
[e8032b0] | 85 | unsigned long size; ///< Number of elements stored in this table |
---|
| 86 | const Indexer base; ///< Base indexer this extends |
---|
[743fbda] | 87 | |
---|
[e8032b0] | 88 | IdTable idTable; ///< Identifier namespace |
---|
| 89 | TypeTable typeTable; ///< Type namespace |
---|
| 90 | StructTable structTable; ///< Struct namespace |
---|
| 91 | EnumTable enumTable; ///< Enum namespace |
---|
| 92 | UnionTable unionTable; ///< Union namespace |
---|
| 93 | TraitTable traitTable; ///< Trait namespace |
---|
| 94 | }; |
---|
| 95 | |
---|
| 96 | Indexer::Impl *Indexer::newRef( Indexer::Impl *toClone ) { |
---|
| 97 | if ( ! toClone ) return 0; |
---|
| 98 | |
---|
| 99 | // shorten the search chain by skipping empty links |
---|
| 100 | Indexer::Impl *ret = toClone->size == 0 ? toClone->base.tables : toClone; |
---|
| 101 | if ( ret ) { ++ret->refCount; } |
---|
| 102 | |
---|
| 103 | return ret; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | void Indexer::deleteRef( Indexer::Impl *toFree ) { |
---|
| 107 | if ( ! toFree ) return; |
---|
| 108 | |
---|
| 109 | if ( --toFree->refCount == 0 ) delete toFree; |
---|
| 110 | } |
---|
| 111 | |
---|
[1ba88a0] | 112 | void Indexer::removeSpecialOverrides( const std::string &id, std::list< DeclarationWithType * > & out ) const { |
---|
[ee1635c8] | 113 | // only need to perform this step for constructors, destructors, and assignment functions |
---|
| 114 | if ( ! InitTweak::isCtorDtorAssign( id ) ) return; |
---|
[1ba88a0] | 115 | |
---|
| 116 | // helpful data structure |
---|
| 117 | struct ValueType { |
---|
| 118 | struct DeclBall { |
---|
| 119 | FunctionDecl * decl; |
---|
| 120 | bool isUserDefinedFunc; // properties for this particular decl |
---|
[235114f] | 121 | bool isDefaultCtor; |
---|
| 122 | bool isDtor; |
---|
[1ba88a0] | 123 | bool isCopyFunc; |
---|
| 124 | }; |
---|
| 125 | // properties for this type |
---|
[ff03f5c] | 126 | bool existsUserDefinedFunc = false; // any user-defined function found |
---|
| 127 | bool existsUserDefinedCtor = false; // any user-defined constructor found |
---|
| 128 | bool existsUserDefinedDtor = false; // any user-defined destructor found |
---|
| 129 | bool existsUserDefinedCopyFunc = false; // user-defined copy ctor found |
---|
| 130 | bool existsUserDefinedDefaultCtor = false; // user-defined default ctor found |
---|
[1ba88a0] | 131 | std::list< DeclBall > decls; |
---|
| 132 | |
---|
| 133 | // another FunctionDecl for the current type was found - determine |
---|
| 134 | // if it has special properties and update data structure accordingly |
---|
| 135 | ValueType & operator+=( FunctionDecl * function ) { |
---|
| 136 | bool isUserDefinedFunc = ! LinkageSpec::isOverridable( function->get_linkage() ); |
---|
[235114f] | 137 | bool isDefaultCtor = InitTweak::isDefaultConstructor( function ); |
---|
| 138 | bool isDtor = InitTweak::isDestructor( function ); |
---|
[ee1635c8] | 139 | bool isCopyFunc = InitTweak::isCopyFunction( function, function->get_name() ); |
---|
[235114f] | 140 | decls.push_back( DeclBall{ function, isUserDefinedFunc, isDefaultCtor, isDtor, isCopyFunc } ); |
---|
[ff03f5c] | 141 | existsUserDefinedFunc = existsUserDefinedFunc || isUserDefinedFunc; |
---|
| 142 | existsUserDefinedCtor = existsUserDefinedCtor || (isUserDefinedFunc && InitTweak::isConstructor( function->get_name() ) ); |
---|
| 143 | existsUserDefinedDtor = existsUserDefinedDtor || (isUserDefinedFunc && isDtor); |
---|
| 144 | existsUserDefinedCopyFunc = existsUserDefinedCopyFunc || (isUserDefinedFunc && isCopyFunc); |
---|
| 145 | existsUserDefinedDefaultCtor = existsUserDefinedDefaultCtor || (isUserDefinedFunc && isDefaultCtor); |
---|
[1ba88a0] | 146 | return *this; |
---|
| 147 | } |
---|
| 148 | }; // ValueType |
---|
| 149 | |
---|
| 150 | std::list< DeclarationWithType * > copy; |
---|
| 151 | copy.splice( copy.end(), out ); |
---|
| 152 | |
---|
| 153 | // organize discovered declarations by type |
---|
| 154 | std::unordered_map< std::string, ValueType > funcMap; |
---|
| 155 | for ( DeclarationWithType * decl : copy ) { |
---|
| 156 | if ( FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl ) ) { |
---|
[8c49c0e] | 157 | std::list< DeclarationWithType * > & params = function->get_functionType()->get_parameters(); |
---|
[1ba88a0] | 158 | assert( ! params.empty() ); |
---|
[24670d2] | 159 | // use base type of pointer, so that qualifiers on the pointer type aren't considered. |
---|
| 160 | Type * base = safe_dynamic_cast< PointerType * >( params.front()->get_type() )->get_base(); |
---|
| 161 | funcMap[ Mangler::mangle( base ) ] += function; |
---|
[1ba88a0] | 162 | } else { |
---|
| 163 | out.push_back( decl ); |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
[ff03f5c] | 167 | // if a type contains user defined ctor/dtor/assign, then special rules trigger, which determine |
---|
| 168 | // the set of ctor/dtor/assign that are seen by the requester. In particular, if the user defines |
---|
[1ba88a0] | 169 | // a default ctor, then the generated default ctor should never be seen, likewise for copy ctor |
---|
| 170 | // and dtor. If the user defines any ctor/dtor, then no generated field ctors should be seen. |
---|
[ff03f5c] | 171 | // If the user defines any ctor then the generated default ctor should not be seen (intrinsic default |
---|
| 172 | // ctor must be overridden exactly). |
---|
[1ba88a0] | 173 | for ( std::pair< const std::string, ValueType > & pair : funcMap ) { |
---|
| 174 | ValueType & val = pair.second; |
---|
| 175 | for ( ValueType::DeclBall ball : val.decls ) { |
---|
[ff03f5c] | 176 | bool noUserDefinedFunc = ! val.existsUserDefinedFunc; |
---|
| 177 | bool isUserDefinedFunc = ball.isUserDefinedFunc; |
---|
| 178 | bool isAcceptableDefaultCtor = (! val.existsUserDefinedCtor || (! val.existsUserDefinedDefaultCtor && ball.decl->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 |
---|
| 179 | bool isAcceptableCopyFunc = ! val.existsUserDefinedCopyFunc && ball.isCopyFunc; // handles copy ctor and assignment operator |
---|
| 180 | bool isAcceptableDtor = ! val.existsUserDefinedDtor && ball.isDtor; |
---|
| 181 | if ( noUserDefinedFunc || isUserDefinedFunc || isAcceptableDefaultCtor || isAcceptableCopyFunc || isAcceptableDtor ) { |
---|
[1ba88a0] | 182 | // decl conforms to the rules described above, so it should be seen by the requester |
---|
| 183 | out.push_back( ball.decl ); |
---|
| 184 | } |
---|
| 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 | |
---|
[52c2a72] | 199 | Indexer::Indexer( bool _doDebug ) : tables( 0 ), scope( 0 ), doDebug( _doDebug ) {} |
---|
[e8032b0] | 200 | |
---|
[52c2a72] | 201 | Indexer::Indexer( const Indexer &that ) : tables( newRef( that.tables ) ), scope( that.scope ), doDebug( that.doDebug ) {} |
---|
[e8032b0] | 202 | |
---|
[52c2a72] | 203 | Indexer::Indexer( Indexer &&that ) : tables( that.tables ), scope( that.scope ), doDebug( that.doDebug ) { |
---|
[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 | |
---|
[a08ba92] | 233 | void Indexer::visit( ObjectDecl *objectDecl ) { |
---|
[ae4c85a] | 234 | enterScope(); |
---|
[0dd3a2f] | 235 | maybeAccept( objectDecl->get_type(), *this ); |
---|
[ae4c85a] | 236 | leaveScope(); |
---|
[0dd3a2f] | 237 | maybeAccept( objectDecl->get_init(), *this ); |
---|
| 238 | maybeAccept( objectDecl->get_bitfieldWidth(), *this ); |
---|
| 239 | if ( objectDecl->get_name() != "" ) { |
---|
| 240 | debugPrint( "Adding object " << objectDecl->get_name() << std::endl ); |
---|
[e8032b0] | 241 | addId( objectDecl ); |
---|
[0dd3a2f] | 242 | } // if |
---|
[a08ba92] | 243 | } |
---|
[17cd4eb] | 244 | |
---|
[a08ba92] | 245 | void Indexer::visit( FunctionDecl *functionDecl ) { |
---|
[0dd3a2f] | 246 | if ( functionDecl->get_name() == "" ) return; |
---|
| 247 | debugPrint( "Adding function " << functionDecl->get_name() << std::endl ); |
---|
[e8032b0] | 248 | addId( functionDecl ); |
---|
[0dd3a2f] | 249 | enterScope(); |
---|
| 250 | maybeAccept( functionDecl->get_functionType(), *this ); |
---|
| 251 | maybeAccept( functionDecl->get_statements(), *this ); |
---|
| 252 | leaveScope(); |
---|
[a08ba92] | 253 | } |
---|
[51b7345] | 254 | |
---|
[44b5ca0] | 255 | |
---|
| 256 | // A NOTE ON THE ORDER OF TRAVERSAL |
---|
| 257 | // |
---|
| 258 | // Types and typedefs have their base types visited before they are added to the type table. This is ok, since there is |
---|
| 259 | // no such thing as a recursive type or typedef. |
---|
| 260 | // |
---|
| 261 | // typedef struct { T *x; } T; // never allowed |
---|
| 262 | // |
---|
| 263 | // for structs/unions, it is possible to have recursion, so the decl should be added as if it's incomplete to begin, the |
---|
| 264 | // members are traversed, and then the complete type should be added (assuming the type is completed by this particular |
---|
| 265 | // declaration). |
---|
| 266 | // |
---|
| 267 | // struct T { struct T *x; }; // allowed |
---|
| 268 | // |
---|
| 269 | // It is important to add the complete type to the symbol table *after* the members/base has been traversed, since that |
---|
| 270 | // traversal may modify the definition of the type and these modifications should be visible when the symbol table is |
---|
| 271 | // queried later in this pass. |
---|
| 272 | // |
---|
| 273 | // TODO: figure out whether recursive contexts are sensible/possible/reasonable. |
---|
| 274 | |
---|
[51b7345] | 275 | |
---|
[a08ba92] | 276 | void Indexer::visit( TypeDecl *typeDecl ) { |
---|
[0dd3a2f] | 277 | // see A NOTE ON THE ORDER OF TRAVERSAL, above |
---|
[44b5ca0] | 278 | // note that assertions come after the type is added to the symtab, since they are not part of the type proper |
---|
| 279 | // and may depend on the type itself |
---|
[0dd3a2f] | 280 | enterScope(); |
---|
| 281 | acceptAll( typeDecl->get_parameters(), *this ); |
---|
| 282 | maybeAccept( typeDecl->get_base(), *this ); |
---|
| 283 | leaveScope(); |
---|
| 284 | debugPrint( "Adding type " << typeDecl->get_name() << std::endl ); |
---|
[e8032b0] | 285 | addType( typeDecl ); |
---|
[0dd3a2f] | 286 | acceptAll( typeDecl->get_assertions(), *this ); |
---|
[67cf18c] | 287 | acceptNewScope( typeDecl->get_init(), *this ); |
---|
[a08ba92] | 288 | } |
---|
[17cd4eb] | 289 | |
---|
[a08ba92] | 290 | void Indexer::visit( TypedefDecl *typeDecl ) { |
---|
[0dd3a2f] | 291 | enterScope(); |
---|
| 292 | acceptAll( typeDecl->get_parameters(), *this ); |
---|
| 293 | maybeAccept( typeDecl->get_base(), *this ); |
---|
| 294 | leaveScope(); |
---|
| 295 | debugPrint( "Adding typedef " << typeDecl->get_name() << std::endl ); |
---|
[e8032b0] | 296 | addType( typeDecl ); |
---|
[a08ba92] | 297 | } |
---|
[17cd4eb] | 298 | |
---|
[a08ba92] | 299 | void Indexer::visit( StructDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 300 | // make up a forward declaration and add it before processing the members |
---|
[743fbda] | 301 | // needs to be on the heap because addStruct saves the pointer |
---|
| 302 | StructDecl &fwdDecl = *new StructDecl( aggregateDecl->get_name() ); |
---|
[0dd3a2f] | 303 | cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() ); |
---|
| 304 | debugPrint( "Adding fwd decl for struct " << fwdDecl.get_name() << std::endl ); |
---|
[e8032b0] | 305 | addStruct( &fwdDecl ); |
---|
[743fbda] | 306 | |
---|
[0dd3a2f] | 307 | enterScope(); |
---|
| 308 | acceptAll( aggregateDecl->get_parameters(), *this ); |
---|
| 309 | acceptAll( aggregateDecl->get_members(), *this ); |
---|
| 310 | leaveScope(); |
---|
[743fbda] | 311 | |
---|
[0dd3a2f] | 312 | debugPrint( "Adding struct " << aggregateDecl->get_name() << std::endl ); |
---|
| 313 | // this addition replaces the forward declaration |
---|
[e8032b0] | 314 | addStruct( aggregateDecl ); |
---|
[a08ba92] | 315 | } |
---|
[17cd4eb] | 316 | |
---|
[a08ba92] | 317 | void Indexer::visit( UnionDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 318 | // make up a forward declaration and add it before processing the members |
---|
| 319 | UnionDecl fwdDecl( aggregateDecl->get_name() ); |
---|
| 320 | cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() ); |
---|
| 321 | debugPrint( "Adding fwd decl for union " << fwdDecl.get_name() << std::endl ); |
---|
[e8032b0] | 322 | addUnion( &fwdDecl ); |
---|
[743fbda] | 323 | |
---|
[0dd3a2f] | 324 | enterScope(); |
---|
| 325 | acceptAll( aggregateDecl->get_parameters(), *this ); |
---|
| 326 | acceptAll( aggregateDecl->get_members(), *this ); |
---|
| 327 | leaveScope(); |
---|
[743fbda] | 328 | |
---|
[0dd3a2f] | 329 | debugPrint( "Adding union " << aggregateDecl->get_name() << std::endl ); |
---|
[e8032b0] | 330 | addUnion( aggregateDecl ); |
---|
[a08ba92] | 331 | } |
---|
[17cd4eb] | 332 | |
---|
[a08ba92] | 333 | void Indexer::visit( EnumDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 334 | debugPrint( "Adding enum " << aggregateDecl->get_name() << std::endl ); |
---|
[e8032b0] | 335 | addEnum( aggregateDecl ); |
---|
[0dd3a2f] | 336 | // unlike structs, contexts, and unions, enums inject their members into the global scope |
---|
| 337 | acceptAll( aggregateDecl->get_members(), *this ); |
---|
[a08ba92] | 338 | } |
---|
[17cd4eb] | 339 | |
---|
[4040425] | 340 | void Indexer::visit( TraitDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 341 | enterScope(); |
---|
| 342 | acceptAll( aggregateDecl->get_parameters(), *this ); |
---|
| 343 | acceptAll( aggregateDecl->get_members(), *this ); |
---|
| 344 | leaveScope(); |
---|
[743fbda] | 345 | |
---|
[0dd3a2f] | 346 | debugPrint( "Adding context " << aggregateDecl->get_name() << std::endl ); |
---|
[e8032b0] | 347 | addTrait( aggregateDecl ); |
---|
[a08ba92] | 348 | } |
---|
[17cd4eb] | 349 | |
---|
[a08ba92] | 350 | void Indexer::visit( CompoundStmt *compoundStmt ) { |
---|
[0dd3a2f] | 351 | enterScope(); |
---|
| 352 | acceptAll( compoundStmt->get_kids(), *this ); |
---|
| 353 | leaveScope(); |
---|
[a08ba92] | 354 | } |
---|
[17cd4eb] | 355 | |
---|
[ae4c85a] | 356 | |
---|
| 357 | void Indexer::visit( ApplicationExpr *applicationExpr ) { |
---|
[906e24d] | 358 | acceptNewScope( applicationExpr->get_result(), *this ); |
---|
[ae4c85a] | 359 | maybeAccept( applicationExpr->get_function(), *this ); |
---|
| 360 | acceptAll( applicationExpr->get_args(), *this ); |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | void Indexer::visit( UntypedExpr *untypedExpr ) { |
---|
[906e24d] | 364 | acceptNewScope( untypedExpr->get_result(), *this ); |
---|
[ae4c85a] | 365 | acceptAll( untypedExpr->get_args(), *this ); |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | void Indexer::visit( NameExpr *nameExpr ) { |
---|
[906e24d] | 369 | acceptNewScope( nameExpr->get_result(), *this ); |
---|
[ae4c85a] | 370 | } |
---|
| 371 | |
---|
| 372 | void Indexer::visit( AddressExpr *addressExpr ) { |
---|
[906e24d] | 373 | acceptNewScope( addressExpr->get_result(), *this ); |
---|
[ae4c85a] | 374 | maybeAccept( addressExpr->get_arg(), *this ); |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | void Indexer::visit( LabelAddressExpr *labAddressExpr ) { |
---|
[906e24d] | 378 | acceptNewScope( labAddressExpr->get_result(), *this ); |
---|
[ae4c85a] | 379 | maybeAccept( labAddressExpr->get_arg(), *this ); |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | void Indexer::visit( CastExpr *castExpr ) { |
---|
[906e24d] | 383 | acceptNewScope( castExpr->get_result(), *this ); |
---|
[ae4c85a] | 384 | maybeAccept( castExpr->get_arg(), *this ); |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | void Indexer::visit( UntypedMemberExpr *memberExpr ) { |
---|
[906e24d] | 388 | acceptNewScope( memberExpr->get_result(), *this ); |
---|
[ae4c85a] | 389 | maybeAccept( memberExpr->get_aggregate(), *this ); |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | void Indexer::visit( MemberExpr *memberExpr ) { |
---|
[906e24d] | 393 | acceptNewScope( memberExpr->get_result(), *this ); |
---|
[ae4c85a] | 394 | maybeAccept( memberExpr->get_aggregate(), *this ); |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | void Indexer::visit( VariableExpr *variableExpr ) { |
---|
[906e24d] | 398 | acceptNewScope( variableExpr->get_result(), *this ); |
---|
[ae4c85a] | 399 | } |
---|
| 400 | |
---|
| 401 | void Indexer::visit( ConstantExpr *constantExpr ) { |
---|
[906e24d] | 402 | acceptNewScope( constantExpr->get_result(), *this ); |
---|
[ae4c85a] | 403 | maybeAccept( constantExpr->get_constant(), *this ); |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | void Indexer::visit( SizeofExpr *sizeofExpr ) { |
---|
[906e24d] | 407 | acceptNewScope( sizeofExpr->get_result(), *this ); |
---|
[ae4c85a] | 408 | if ( sizeofExpr->get_isType() ) { |
---|
| 409 | maybeAccept( sizeofExpr->get_type(), *this ); |
---|
| 410 | } else { |
---|
| 411 | maybeAccept( sizeofExpr->get_expr(), *this ); |
---|
| 412 | } |
---|
| 413 | } |
---|
| 414 | |
---|
[47534159] | 415 | void Indexer::visit( AlignofExpr *alignofExpr ) { |
---|
[906e24d] | 416 | acceptNewScope( alignofExpr->get_result(), *this ); |
---|
[47534159] | 417 | if ( alignofExpr->get_isType() ) { |
---|
| 418 | maybeAccept( alignofExpr->get_type(), *this ); |
---|
| 419 | } else { |
---|
| 420 | maybeAccept( alignofExpr->get_expr(), *this ); |
---|
| 421 | } |
---|
| 422 | } |
---|
| 423 | |
---|
[2a4b088] | 424 | void Indexer::visit( UntypedOffsetofExpr *offsetofExpr ) { |
---|
[906e24d] | 425 | acceptNewScope( offsetofExpr->get_result(), *this ); |
---|
[2a4b088] | 426 | maybeAccept( offsetofExpr->get_type(), *this ); |
---|
| 427 | } |
---|
| 428 | |
---|
[25a054f] | 429 | void Indexer::visit( OffsetofExpr *offsetofExpr ) { |
---|
[906e24d] | 430 | acceptNewScope( offsetofExpr->get_result(), *this ); |
---|
[25a054f] | 431 | maybeAccept( offsetofExpr->get_type(), *this ); |
---|
| 432 | maybeAccept( offsetofExpr->get_member(), *this ); |
---|
| 433 | } |
---|
| 434 | |
---|
[afc1045] | 435 | void Indexer::visit( OffsetPackExpr *offsetPackExpr ) { |
---|
[906e24d] | 436 | acceptNewScope( offsetPackExpr->get_result(), *this ); |
---|
[afc1045] | 437 | maybeAccept( offsetPackExpr->get_type(), *this ); |
---|
| 438 | } |
---|
| 439 | |
---|
[ae4c85a] | 440 | void Indexer::visit( AttrExpr *attrExpr ) { |
---|
[906e24d] | 441 | acceptNewScope( attrExpr->get_result(), *this ); |
---|
[ae4c85a] | 442 | if ( attrExpr->get_isType() ) { |
---|
| 443 | maybeAccept( attrExpr->get_type(), *this ); |
---|
| 444 | } else { |
---|
| 445 | maybeAccept( attrExpr->get_expr(), *this ); |
---|
| 446 | } |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | void Indexer::visit( LogicalExpr *logicalExpr ) { |
---|
[906e24d] | 450 | acceptNewScope( logicalExpr->get_result(), *this ); |
---|
[ae4c85a] | 451 | maybeAccept( logicalExpr->get_arg1(), *this ); |
---|
| 452 | maybeAccept( logicalExpr->get_arg2(), *this ); |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | void Indexer::visit( ConditionalExpr *conditionalExpr ) { |
---|
[906e24d] | 456 | acceptNewScope( conditionalExpr->get_result(), *this ); |
---|
[ae4c85a] | 457 | maybeAccept( conditionalExpr->get_arg1(), *this ); |
---|
| 458 | maybeAccept( conditionalExpr->get_arg2(), *this ); |
---|
| 459 | maybeAccept( conditionalExpr->get_arg3(), *this ); |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | void Indexer::visit( CommaExpr *commaExpr ) { |
---|
[906e24d] | 463 | acceptNewScope( commaExpr->get_result(), *this ); |
---|
[ae4c85a] | 464 | maybeAccept( commaExpr->get_arg1(), *this ); |
---|
| 465 | maybeAccept( commaExpr->get_arg2(), *this ); |
---|
| 466 | } |
---|
| 467 | |
---|
| 468 | void Indexer::visit( TypeExpr *typeExpr ) { |
---|
[906e24d] | 469 | acceptNewScope( typeExpr->get_result(), *this ); |
---|
[ae4c85a] | 470 | maybeAccept( typeExpr->get_type(), *this ); |
---|
| 471 | } |
---|
| 472 | |
---|
| 473 | void Indexer::visit( AsmExpr *asmExpr ) { |
---|
| 474 | maybeAccept( asmExpr->get_inout(), *this ); |
---|
| 475 | maybeAccept( asmExpr->get_constraint(), *this ); |
---|
| 476 | maybeAccept( asmExpr->get_operand(), *this ); |
---|
| 477 | } |
---|
| 478 | |
---|
[907eccb] | 479 | void Indexer::visit( ImplicitCopyCtorExpr *impCpCtorExpr ) { |
---|
| 480 | acceptNewScope( impCpCtorExpr->get_result(), *this ); |
---|
| 481 | maybeAccept( impCpCtorExpr->get_callExpr(), *this ); |
---|
| 482 | acceptAll( impCpCtorExpr->get_tempDecls(), *this ); |
---|
| 483 | acceptAll( impCpCtorExpr->get_returnDecls(), *this ); |
---|
| 484 | acceptAll( impCpCtorExpr->get_dtors(), *this ); |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | void Indexer::visit( ConstructorExpr * ctorExpr ) { |
---|
| 488 | acceptNewScope( ctorExpr->get_result(), *this ); |
---|
| 489 | maybeAccept( ctorExpr->get_callExpr(), *this ); |
---|
| 490 | } |
---|
| 491 | |
---|
| 492 | void Indexer::visit( CompoundLiteralExpr *compLitExpr ) { |
---|
| 493 | acceptNewScope( compLitExpr->get_result(), *this ); |
---|
| 494 | maybeAccept( compLitExpr->get_initializer(), *this ); |
---|
| 495 | } |
---|
| 496 | |
---|
| 497 | void Indexer::visit( RangeExpr *rangeExpr ) { |
---|
| 498 | maybeAccept( rangeExpr->get_low(), *this ); |
---|
| 499 | maybeAccept( rangeExpr->get_high(), *this ); |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | void Indexer::visit( UntypedTupleExpr *tupleExpr ) { |
---|
| 503 | acceptNewScope( tupleExpr->get_result(), *this ); |
---|
| 504 | acceptAll( tupleExpr->get_exprs(), *this ); |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | void Indexer::visit( TupleExpr *tupleExpr ) { |
---|
| 508 | acceptNewScope( tupleExpr->get_result(), *this ); |
---|
| 509 | acceptAll( tupleExpr->get_exprs(), *this ); |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | void Indexer::visit( TupleIndexExpr *tupleExpr ) { |
---|
| 513 | acceptNewScope( tupleExpr->get_result(), *this ); |
---|
| 514 | maybeAccept( tupleExpr->get_tuple(), *this ); |
---|
| 515 | } |
---|
| 516 | |
---|
| 517 | void Indexer::visit( TupleAssignExpr *tupleExpr ) { |
---|
| 518 | acceptNewScope( tupleExpr->get_result(), *this ); |
---|
| 519 | maybeAccept( tupleExpr->get_stmtExpr(), *this ); |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | void Indexer::visit( StmtExpr *stmtExpr ) { |
---|
| 523 | acceptNewScope( stmtExpr->get_result(), *this ); |
---|
| 524 | maybeAccept( stmtExpr->get_statements(), *this ); |
---|
| 525 | acceptAll( stmtExpr->get_returnDecls(), *this ); |
---|
| 526 | acceptAll( stmtExpr->get_dtors(), *this ); |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | void Indexer::visit( UniqueExpr *uniqueExpr ) { |
---|
| 530 | acceptNewScope( uniqueExpr->get_result(), *this ); |
---|
| 531 | maybeAccept( uniqueExpr->get_expr(), *this ); |
---|
| 532 | } |
---|
| 533 | |
---|
[ae4c85a] | 534 | |
---|
[4040425] | 535 | void Indexer::visit( TraitInstType *contextInst ) { |
---|
[0dd3a2f] | 536 | acceptAll( contextInst->get_parameters(), *this ); |
---|
| 537 | acceptAll( contextInst->get_members(), *this ); |
---|
[a08ba92] | 538 | } |
---|
[17cd4eb] | 539 | |
---|
[a08ba92] | 540 | void Indexer::visit( StructInstType *structInst ) { |
---|
[e8032b0] | 541 | if ( ! lookupStruct( structInst->get_name() ) ) { |
---|
[0dd3a2f] | 542 | debugPrint( "Adding struct " << structInst->get_name() << " from implicit forward declaration" << std::endl ); |
---|
[e8032b0] | 543 | addStruct( structInst->get_name() ); |
---|
[0dd3a2f] | 544 | } |
---|
| 545 | enterScope(); |
---|
| 546 | acceptAll( structInst->get_parameters(), *this ); |
---|
| 547 | leaveScope(); |
---|
[a08ba92] | 548 | } |
---|
[17cd4eb] | 549 | |
---|
[a08ba92] | 550 | void Indexer::visit( UnionInstType *unionInst ) { |
---|
[e8032b0] | 551 | if ( ! lookupUnion( unionInst->get_name() ) ) { |
---|
[0dd3a2f] | 552 | debugPrint( "Adding union " << unionInst->get_name() << " from implicit forward declaration" << std::endl ); |
---|
[e8032b0] | 553 | addUnion( unionInst->get_name() ); |
---|
[0dd3a2f] | 554 | } |
---|
| 555 | enterScope(); |
---|
| 556 | acceptAll( unionInst->get_parameters(), *this ); |
---|
| 557 | leaveScope(); |
---|
[a08ba92] | 558 | } |
---|
[17cd4eb] | 559 | |
---|
[a08ba92] | 560 | void Indexer::visit( ForStmt *forStmt ) { |
---|
| 561 | // for statements introduce a level of scope |
---|
| 562 | enterScope(); |
---|
| 563 | Visitor::visit( forStmt ); |
---|
| 564 | leaveScope(); |
---|
| 565 | } |
---|
[d4778a6] | 566 | |
---|
[743fbda] | 567 | |
---|
[d4778a6] | 568 | |
---|
[bed4c63e] | 569 | void Indexer::lookupId( const std::string &id, std::list< DeclarationWithType* > &out ) const { |
---|
[5447e09] | 570 | std::unordered_set< std::string > foundMangleNames; |
---|
[743fbda] | 571 | |
---|
[5447e09] | 572 | Indexer::Impl *searchTables = tables; |
---|
| 573 | while ( searchTables ) { |
---|
| 574 | |
---|
| 575 | IdTable::const_iterator decls = searchTables->idTable.find( id ); |
---|
| 576 | if ( decls != searchTables->idTable.end() ) { |
---|
| 577 | const MangleTable &mangleTable = decls->second; |
---|
| 578 | for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { |
---|
| 579 | // mark the mangled name as found, skipping this insertion if a declaration for that name has already been found |
---|
| 580 | if ( foundMangleNames.insert( decl->first ).second == false ) continue; |
---|
[743fbda] | 581 | |
---|
[5447e09] | 582 | out.push_back( decl->second ); |
---|
| 583 | } |
---|
[bed4c63e] | 584 | } |
---|
[743fbda] | 585 | |
---|
[5447e09] | 586 | // get declarations from base indexers |
---|
| 587 | searchTables = searchTables->base.tables; |
---|
[bed4c63e] | 588 | } |
---|
[1ba88a0] | 589 | |
---|
| 590 | // some special functions, e.g. constructors and destructors |
---|
| 591 | // remove autogenerated functions when they are defined so that |
---|
| 592 | // they can never be matched |
---|
| 593 | removeSpecialOverrides( id, out ); |
---|
[a08ba92] | 594 | } |
---|
[bdd516a] | 595 | |
---|
[a08ba92] | 596 | NamedTypeDecl *Indexer::lookupType( const std::string &id ) const { |
---|
[e8032b0] | 597 | if ( ! tables ) return 0; |
---|
| 598 | |
---|
[52c2a72] | 599 | TypeTable::const_iterator ret = tables->typeTable.find( id ); |
---|
| 600 | return ret != tables->typeTable.end() ? ret->second : tables->base.lookupType( id ); |
---|
[a08ba92] | 601 | } |
---|
[17cd4eb] | 602 | |
---|
[a08ba92] | 603 | StructDecl *Indexer::lookupStruct( const std::string &id ) const { |
---|
[e8032b0] | 604 | if ( ! tables ) return 0; |
---|
| 605 | |
---|
[52c2a72] | 606 | StructTable::const_iterator ret = tables->structTable.find( id ); |
---|
| 607 | return ret != tables->structTable.end() ? ret->second : tables->base.lookupStruct( id ); |
---|
[a08ba92] | 608 | } |
---|
[17cd4eb] | 609 | |
---|
[a08ba92] | 610 | EnumDecl *Indexer::lookupEnum( const std::string &id ) const { |
---|
[e8032b0] | 611 | if ( ! tables ) return 0; |
---|
| 612 | |
---|
[52c2a72] | 613 | EnumTable::const_iterator ret = tables->enumTable.find( id ); |
---|
| 614 | return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnum( id ); |
---|
[a08ba92] | 615 | } |
---|
[17cd4eb] | 616 | |
---|
[a08ba92] | 617 | UnionDecl *Indexer::lookupUnion( const std::string &id ) const { |
---|
[e8032b0] | 618 | if ( ! tables ) return 0; |
---|
| 619 | |
---|
[52c2a72] | 620 | UnionTable::const_iterator ret = tables->unionTable.find( id ); |
---|
| 621 | return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnion( id ); |
---|
[e8032b0] | 622 | } |
---|
| 623 | |
---|
| 624 | TraitDecl *Indexer::lookupTrait( const std::string &id ) const { |
---|
| 625 | if ( ! tables ) return 0; |
---|
| 626 | |
---|
[52c2a72] | 627 | TraitTable::const_iterator ret = tables->traitTable.find( id ); |
---|
| 628 | return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTrait( id ); |
---|
| 629 | } |
---|
| 630 | |
---|
[bed4c63e] | 631 | DeclarationWithType *Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) const { |
---|
| 632 | if ( ! tables ) return 0; |
---|
| 633 | if ( tables->scope < scope ) return 0; |
---|
| 634 | |
---|
| 635 | IdTable::const_iterator decls = tables->idTable.find( id ); |
---|
| 636 | if ( decls != tables->idTable.end() ) { |
---|
| 637 | const MangleTable &mangleTable = decls->second; |
---|
| 638 | MangleTable::const_iterator decl = mangleTable.find( mangleName ); |
---|
| 639 | if ( decl != mangleTable.end() ) return decl->second; |
---|
| 640 | } |
---|
| 641 | |
---|
| 642 | return tables->base.lookupIdAtScope( id, mangleName, scope ); |
---|
| 643 | } |
---|
| 644 | |
---|
[09d1ad0] | 645 | bool Indexer::hasIncompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const { |
---|
[bed4c63e] | 646 | if ( ! tables ) return false; |
---|
[09d1ad0] | 647 | if ( tables->scope < scope ) return false; |
---|
[bed4c63e] | 648 | |
---|
| 649 | IdTable::const_iterator decls = tables->idTable.find( id ); |
---|
| 650 | if ( decls != tables->idTable.end() ) { |
---|
| 651 | const MangleTable &mangleTable = decls->second; |
---|
| 652 | for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { |
---|
[4e06c1e] | 653 | // check for C decls with the same name, skipping those with a compatible type (by mangleName) |
---|
[5447e09] | 654 | if ( decl->second->get_linkage() == LinkageSpec::C && decl->first != mangleName ) return true; |
---|
[bed4c63e] | 655 | } |
---|
| 656 | } |
---|
| 657 | |
---|
[09d1ad0] | 658 | return tables->base.hasIncompatibleCDecl( id, mangleName, scope ); |
---|
[bed4c63e] | 659 | } |
---|
[743fbda] | 660 | |
---|
[8884112] | 661 | bool Indexer::hasCompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const { |
---|
| 662 | if ( ! tables ) return false; |
---|
| 663 | if ( tables->scope < scope ) return false; |
---|
| 664 | |
---|
| 665 | IdTable::const_iterator decls = tables->idTable.find( id ); |
---|
| 666 | if ( decls != tables->idTable.end() ) { |
---|
| 667 | const MangleTable &mangleTable = decls->second; |
---|
| 668 | for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { |
---|
| 669 | // check for C decls with the same name, skipping |
---|
| 670 | // those with an incompatible type (by mangleName) |
---|
| 671 | if ( decl->second->get_linkage() == LinkageSpec::C && decl->first == mangleName ) return true; |
---|
| 672 | } |
---|
| 673 | } |
---|
| 674 | |
---|
| 675 | return tables->base.hasCompatibleCDecl( id, mangleName, scope ); |
---|
| 676 | } |
---|
| 677 | |
---|
[52c2a72] | 678 | NamedTypeDecl *Indexer::lookupTypeAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 679 | if ( ! tables ) return 0; |
---|
| 680 | if ( tables->scope < scope ) return 0; |
---|
| 681 | |
---|
| 682 | TypeTable::const_iterator ret = tables->typeTable.find( id ); |
---|
| 683 | return ret != tables->typeTable.end() ? ret->second : tables->base.lookupTypeAtScope( id, scope ); |
---|
| 684 | } |
---|
[743fbda] | 685 | |
---|
[52c2a72] | 686 | StructDecl *Indexer::lookupStructAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 687 | if ( ! tables ) return 0; |
---|
| 688 | if ( tables->scope < scope ) return 0; |
---|
| 689 | |
---|
| 690 | StructTable::const_iterator ret = tables->structTable.find( id ); |
---|
| 691 | return ret != tables->structTable.end() ? ret->second : tables->base.lookupStructAtScope( id, scope ); |
---|
| 692 | } |
---|
[743fbda] | 693 | |
---|
[52c2a72] | 694 | EnumDecl *Indexer::lookupEnumAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 695 | if ( ! tables ) return 0; |
---|
| 696 | if ( tables->scope < scope ) return 0; |
---|
| 697 | |
---|
| 698 | EnumTable::const_iterator ret = tables->enumTable.find( id ); |
---|
| 699 | return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnumAtScope( id, scope ); |
---|
| 700 | } |
---|
[743fbda] | 701 | |
---|
[52c2a72] | 702 | UnionDecl *Indexer::lookupUnionAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 703 | if ( ! tables ) return 0; |
---|
| 704 | if ( tables->scope < scope ) return 0; |
---|
| 705 | |
---|
| 706 | UnionTable::const_iterator ret = tables->unionTable.find( id ); |
---|
| 707 | return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnionAtScope( id, scope ); |
---|
| 708 | } |
---|
[743fbda] | 709 | |
---|
[52c2a72] | 710 | TraitDecl *Indexer::lookupTraitAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 711 | if ( ! tables ) return 0; |
---|
| 712 | if ( tables->scope < scope ) return 0; |
---|
| 713 | |
---|
| 714 | TraitTable::const_iterator ret = tables->traitTable.find( id ); |
---|
| 715 | return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTraitAtScope( id, scope ); |
---|
[a08ba92] | 716 | } |
---|
[17cd4eb] | 717 | |
---|
[bed4c63e] | 718 | bool addedIdConflicts( DeclarationWithType *existing, DeclarationWithType *added ) { |
---|
| 719 | // if we're giving the same name mangling to things of different types then there is something wrong |
---|
| 720 | assert( (dynamic_cast<ObjectDecl*>( added ) && dynamic_cast<ObjectDecl*>( existing ) ) |
---|
| 721 | || (dynamic_cast<FunctionDecl*>( added ) && dynamic_cast<FunctionDecl*>( existing ) ) ); |
---|
| 722 | |
---|
| 723 | if ( LinkageSpec::isOverridable( existing->get_linkage() ) ) { |
---|
| 724 | // new definition shadows the autogenerated one, even at the same scope |
---|
| 725 | return false; |
---|
| 726 | } else if ( added->get_linkage() != LinkageSpec::C || ResolvExpr::typesCompatible( added->get_type(), existing->get_type(), Indexer() ) ) { |
---|
| 727 | // typesCompatible doesn't really do the right thing here. When checking compatibility of function types, |
---|
| 728 | // we should ignore outermost pointer qualifiers, except _Atomic? |
---|
| 729 | FunctionDecl *newentry = dynamic_cast< FunctionDecl* >( added ); |
---|
| 730 | FunctionDecl *oldentry = dynamic_cast< FunctionDecl* >( existing ); |
---|
| 731 | if ( newentry && oldentry ) { |
---|
| 732 | if ( newentry->get_statements() && oldentry->get_statements() ) { |
---|
| 733 | throw SemanticError( "duplicate function definition for ", added ); |
---|
| 734 | } // if |
---|
| 735 | } else { |
---|
| 736 | // two objects with the same mangled name defined in the same scope. |
---|
| 737 | // both objects must be marked extern or both must be intrinsic for this to be okay |
---|
| 738 | // xxx - perhaps it's actually if either is intrinsic then this is okay? |
---|
| 739 | // might also need to be same storage class? |
---|
| 740 | ObjectDecl *newobj = dynamic_cast< ObjectDecl* >( added ); |
---|
| 741 | ObjectDecl *oldobj = dynamic_cast< ObjectDecl* >( existing ); |
---|
[08d5507b] | 742 | if ( ! newobj->get_storageClasses().is_extern && ! oldobj->get_storageClasses().is_extern ) { |
---|
[bed4c63e] | 743 | throw SemanticError( "duplicate object definition for ", added ); |
---|
| 744 | } // if |
---|
| 745 | } // if |
---|
| 746 | } else { |
---|
| 747 | throw SemanticError( "duplicate definition for ", added ); |
---|
| 748 | } // if |
---|
| 749 | |
---|
| 750 | return true; |
---|
| 751 | } |
---|
[743fbda] | 752 | |
---|
[e8032b0] | 753 | void Indexer::addId( DeclarationWithType *decl ) { |
---|
| 754 | makeWritable(); |
---|
[bed4c63e] | 755 | |
---|
| 756 | const std::string &name = decl->get_name(); |
---|
| 757 | std::string mangleName; |
---|
[6a57da5] | 758 | if ( LinkageSpec::isOverridable( decl->get_linkage() ) ) { |
---|
[bed4c63e] | 759 | // mangle the name without including the appropriate suffix, so overridable routines are placed into the |
---|
| 760 | // same "bucket" as their user defined versions. |
---|
| 761 | mangleName = Mangler::mangle( decl, false ); |
---|
| 762 | } else { |
---|
| 763 | mangleName = Mangler::mangle( decl ); |
---|
| 764 | } // if |
---|
| 765 | |
---|
[8884112] | 766 | // this ensures that no two declarations with the same unmangled name at the same scope both have C linkage |
---|
| 767 | if ( decl->get_linkage() == LinkageSpec::C ) { |
---|
| 768 | // NOTE this is broken in Richard's original code in such a way that it never triggers (it |
---|
| 769 | // doesn't check decls that have the same manglename, and all C-linkage decls are defined to |
---|
| 770 | // have their name as their manglename, hence the error can never trigger). |
---|
| 771 | // The code here is closer to correct, but name mangling would have to be completely |
---|
| 772 | // isomorphic to C type-compatibility, which it may not be. |
---|
| 773 | if ( hasIncompatibleCDecl( name, mangleName, scope ) ) { |
---|
| 774 | throw SemanticError( "conflicting overload of C function ", decl ); |
---|
| 775 | } |
---|
| 776 | } else { |
---|
| 777 | // Check that a Cforall declaration doesn't overload any C declaration |
---|
| 778 | if ( hasCompatibleCDecl( name, mangleName, scope ) ) { |
---|
| 779 | throw SemanticError( "Cforall declaration hides C function ", decl ); |
---|
| 780 | } |
---|
[bed4c63e] | 781 | } |
---|
[8884112] | 782 | |
---|
| 783 | // Skip repeat declarations of the same identifier |
---|
| 784 | DeclarationWithType *existing = lookupIdAtScope( name, mangleName, scope ); |
---|
| 785 | if ( existing && addedIdConflicts( existing, decl ) ) return; |
---|
| 786 | |
---|
| 787 | // add to indexer |
---|
| 788 | tables->idTable[ name ][ mangleName ] = decl; |
---|
| 789 | ++tables->size; |
---|
[e8032b0] | 790 | } |
---|
[52c2a72] | 791 | |
---|
| 792 | bool addedTypeConflicts( NamedTypeDecl *existing, NamedTypeDecl *added ) { |
---|
| 793 | if ( existing->get_base() == 0 ) { |
---|
| 794 | return false; |
---|
| 795 | } else if ( added->get_base() == 0 ) { |
---|
| 796 | return true; |
---|
| 797 | } else { |
---|
| 798 | throw SemanticError( "redeclaration of ", added ); |
---|
| 799 | } |
---|
| 800 | } |
---|
[743fbda] | 801 | |
---|
[e8032b0] | 802 | void Indexer::addType( NamedTypeDecl *decl ) { |
---|
| 803 | makeWritable(); |
---|
[52c2a72] | 804 | |
---|
| 805 | const std::string &id = decl->get_name(); |
---|
| 806 | TypeTable::iterator existing = tables->typeTable.find( id ); |
---|
| 807 | if ( existing == tables->typeTable.end() ) { |
---|
| 808 | NamedTypeDecl *parent = tables->base.lookupTypeAtScope( id, scope ); |
---|
| 809 | if ( ! parent || ! addedTypeConflicts( parent, decl ) ) { |
---|
| 810 | tables->typeTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 811 | ++tables->size; |
---|
| 812 | } |
---|
| 813 | } else { |
---|
| 814 | if ( ! addedTypeConflicts( existing->second, decl ) ) { |
---|
| 815 | existing->second = decl; |
---|
| 816 | } |
---|
| 817 | } |
---|
| 818 | } |
---|
| 819 | |
---|
| 820 | bool addedDeclConflicts( AggregateDecl *existing, AggregateDecl *added ) { |
---|
| 821 | if ( existing->get_members().empty() ) { |
---|
| 822 | return false; |
---|
| 823 | } else if ( ! added->get_members().empty() ) { |
---|
| 824 | throw SemanticError( "redeclaration of ", added ); |
---|
| 825 | } // if |
---|
| 826 | return true; |
---|
[e8032b0] | 827 | } |
---|
| 828 | |
---|
| 829 | void Indexer::addStruct( const std::string &id ) { |
---|
[52c2a72] | 830 | addStruct( new StructDecl( id ) ); |
---|
[e8032b0] | 831 | } |
---|
[743fbda] | 832 | |
---|
[e8032b0] | 833 | void Indexer::addStruct( StructDecl *decl ) { |
---|
| 834 | makeWritable(); |
---|
[52c2a72] | 835 | |
---|
| 836 | const std::string &id = decl->get_name(); |
---|
| 837 | StructTable::iterator existing = tables->structTable.find( id ); |
---|
| 838 | if ( existing == tables->structTable.end() ) { |
---|
| 839 | StructDecl *parent = tables->base.lookupStructAtScope( id, scope ); |
---|
| 840 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
| 841 | tables->structTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 842 | ++tables->size; |
---|
| 843 | } |
---|
| 844 | } else { |
---|
| 845 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
| 846 | existing->second = decl; |
---|
| 847 | } |
---|
| 848 | } |
---|
[e8032b0] | 849 | } |
---|
[743fbda] | 850 | |
---|
[e8032b0] | 851 | void Indexer::addEnum( EnumDecl *decl ) { |
---|
| 852 | makeWritable(); |
---|
[52c2a72] | 853 | |
---|
| 854 | const std::string &id = decl->get_name(); |
---|
| 855 | EnumTable::iterator existing = tables->enumTable.find( id ); |
---|
| 856 | if ( existing == tables->enumTable.end() ) { |
---|
| 857 | EnumDecl *parent = tables->base.lookupEnumAtScope( id, scope ); |
---|
| 858 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
| 859 | tables->enumTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 860 | ++tables->size; |
---|
| 861 | } |
---|
| 862 | } else { |
---|
| 863 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
| 864 | existing->second = decl; |
---|
| 865 | } |
---|
| 866 | } |
---|
[e8032b0] | 867 | } |
---|
| 868 | |
---|
| 869 | void Indexer::addUnion( const std::string &id ) { |
---|
[52c2a72] | 870 | addUnion( new UnionDecl( id ) ); |
---|
[e8032b0] | 871 | } |
---|
[743fbda] | 872 | |
---|
[e8032b0] | 873 | void Indexer::addUnion( UnionDecl *decl ) { |
---|
| 874 | makeWritable(); |
---|
[52c2a72] | 875 | |
---|
| 876 | const std::string &id = decl->get_name(); |
---|
| 877 | UnionTable::iterator existing = tables->unionTable.find( id ); |
---|
| 878 | if ( existing == tables->unionTable.end() ) { |
---|
| 879 | UnionDecl *parent = tables->base.lookupUnionAtScope( id, scope ); |
---|
| 880 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
| 881 | tables->unionTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 882 | ++tables->size; |
---|
| 883 | } |
---|
| 884 | } else { |
---|
| 885 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
| 886 | existing->second = decl; |
---|
| 887 | } |
---|
| 888 | } |
---|
[e8032b0] | 889 | } |
---|
[743fbda] | 890 | |
---|
[e8032b0] | 891 | void Indexer::addTrait( TraitDecl *decl ) { |
---|
| 892 | makeWritable(); |
---|
[52c2a72] | 893 | |
---|
| 894 | const std::string &id = decl->get_name(); |
---|
| 895 | TraitTable::iterator existing = tables->traitTable.find( id ); |
---|
| 896 | if ( existing == tables->traitTable.end() ) { |
---|
| 897 | TraitDecl *parent = tables->base.lookupTraitAtScope( id, scope ); |
---|
| 898 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
| 899 | tables->traitTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 900 | ++tables->size; |
---|
| 901 | } |
---|
| 902 | } else { |
---|
| 903 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
| 904 | existing->second = decl; |
---|
| 905 | } |
---|
| 906 | } |
---|
[a08ba92] | 907 | } |
---|
[17cd4eb] | 908 | |
---|
[a08ba92] | 909 | void Indexer::enterScope() { |
---|
[52c2a72] | 910 | ++scope; |
---|
[743fbda] | 911 | |
---|
[0dd3a2f] | 912 | if ( doDebug ) { |
---|
[52c2a72] | 913 | std::cout << "--- Entering scope " << scope << std::endl; |
---|
[0dd3a2f] | 914 | } |
---|
[a08ba92] | 915 | } |
---|
[17cd4eb] | 916 | |
---|
[a08ba92] | 917 | void Indexer::leaveScope() { |
---|
[0dd3a2f] | 918 | using std::cout; |
---|
[52c2a72] | 919 | |
---|
| 920 | assert( scope > 0 && "cannot leave initial scope" ); |
---|
| 921 | --scope; |
---|
| 922 | |
---|
| 923 | while ( tables && tables->scope > scope ) { |
---|
| 924 | if ( doDebug ) { |
---|
| 925 | cout << "--- Leaving scope " << tables->scope << " containing" << std::endl; |
---|
[bed4c63e] | 926 | dump( tables->idTable, cout ); |
---|
[52c2a72] | 927 | dump( tables->typeTable, cout ); |
---|
| 928 | dump( tables->structTable, cout ); |
---|
| 929 | dump( tables->enumTable, cout ); |
---|
| 930 | dump( tables->unionTable, cout ); |
---|
| 931 | dump( tables->traitTable, cout ); |
---|
| 932 | } |
---|
| 933 | |
---|
| 934 | // swap tables for base table until we find one at an appropriate scope |
---|
| 935 | Indexer::Impl *base = newRef( tables->base.tables ); |
---|
| 936 | deleteRef( tables ); |
---|
| 937 | tables = base; |
---|
[0dd3a2f] | 938 | } |
---|
[a08ba92] | 939 | } |
---|
[17cd4eb] | 940 | |
---|
[a08ba92] | 941 | void Indexer::print( std::ostream &os, int indent ) const { |
---|
| 942 | using std::cerr; |
---|
[e8032b0] | 943 | |
---|
[0cb1d61] | 944 | if ( tables ) { |
---|
| 945 | os << "--- scope " << tables->scope << " ---" << std::endl; |
---|
| 946 | |
---|
| 947 | os << "===idTable===" << std::endl; |
---|
| 948 | dump( tables->idTable, os ); |
---|
| 949 | os << "===typeTable===" << std::endl; |
---|
| 950 | dump( tables->typeTable, os ); |
---|
| 951 | os << "===structTable===" << std::endl; |
---|
| 952 | dump( tables->structTable, os ); |
---|
| 953 | os << "===enumTable===" << std::endl; |
---|
| 954 | dump( tables->enumTable, os ); |
---|
| 955 | os << "===unionTable===" << std::endl; |
---|
| 956 | dump( tables->unionTable, os ); |
---|
| 957 | os << "===contextTable===" << std::endl; |
---|
| 958 | dump( tables->traitTable, os ); |
---|
| 959 | |
---|
| 960 | tables->base.print( os, indent ); |
---|
| 961 | } else { |
---|
| 962 | os << "--- end ---" << std::endl; |
---|
| 963 | } |
---|
[743fbda] | 964 | |
---|
[a08ba92] | 965 | } |
---|
[51b7345] | 966 | } // namespace SymTab |
---|
[0dd3a2f] | 967 | |
---|
| 968 | // Local Variables: // |
---|
| 969 | // tab-width: 4 // |
---|
| 970 | // mode: c++ // |
---|
| 971 | // compile-command: "make install" // |
---|
| 972 | // End: // |
---|