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