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