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