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