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