| [0dd3a2f] | 1 | // | 
|---|
|  | 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo | 
|---|
|  | 3 | // | 
|---|
|  | 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
|  | 5 | // file "LICENCE" distributed with Cforall. | 
|---|
|  | 6 | // | 
|---|
| [743fbda] | 7 | // Indexer.cc -- | 
|---|
| [0dd3a2f] | 8 | // | 
|---|
|  | 9 | // Author           : Richard C. Bilson | 
|---|
|  | 10 | // Created On       : Sun May 17 21:37:33 2015 | 
|---|
| [4e06c1e] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
| [6d49ea3] | 12 | // Last Modified On : Thu Aug 17 16:08:40 2017 | 
|---|
|  | 13 | // Update Count     : 20 | 
|---|
| [0dd3a2f] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [e8032b0] | 16 | #include "Indexer.h" | 
|---|
|  | 17 |  | 
|---|
| [e3e16bc] | 18 | #include <cassert>                 // for assert, strict_dynamic_cast | 
|---|
| [30f9072] | 19 | #include <iostream>                // for operator<<, basic_ostream, ostream | 
|---|
|  | 20 | #include <string>                  // for string, operator<<, operator!= | 
|---|
|  | 21 | #include <unordered_map>           // for operator!=, unordered_map<>::const... | 
|---|
|  | 22 | #include <unordered_set>           // for unordered_set | 
|---|
|  | 23 | #include <utility>                 // for pair, make_pair, move | 
|---|
|  | 24 |  | 
|---|
| [9236060] | 25 | #include "CodeGen/OperatorTable.h" // for isCtorDtor, isCtorDtorAssign | 
|---|
| [30f9072] | 26 | #include "Common/SemanticError.h"  // for SemanticError | 
|---|
|  | 27 | #include "Common/utility.h"        // for cloneAll | 
|---|
| [3f024c9] | 28 | #include "GenPoly/GenPoly.h" | 
|---|
| [30f9072] | 29 | #include "InitTweak/InitTweak.h"   // for isConstructor, isCopyFunction, isC... | 
|---|
|  | 30 | #include "Mangler.h"               // for Mangler | 
|---|
|  | 31 | #include "Parser/LinkageSpec.h"    // for isMangled, isOverridable, Spec | 
|---|
|  | 32 | #include "ResolvExpr/typeops.h"    // for typesCompatible | 
|---|
|  | 33 | #include "SynTree/Constant.h"      // for Constant | 
|---|
|  | 34 | #include "SynTree/Declaration.h"   // for DeclarationWithType, FunctionDecl | 
|---|
|  | 35 | #include "SynTree/Expression.h"    // for Expression, ImplicitCopyCtorExpr | 
|---|
|  | 36 | #include "SynTree/Initializer.h"   // for Initializer | 
|---|
|  | 37 | #include "SynTree/Statement.h"     // for CompoundStmt, Statement, ForStmt (... | 
|---|
|  | 38 | #include "SynTree/Type.h"          // for Type, StructInstType, UnionInstType | 
|---|
| [1ba88a0] | 39 |  | 
|---|
| [522363e] | 40 | #define debugPrint(x) if ( doDebug ) { std::cerr << x; } | 
|---|
| [51b73452] | 41 |  | 
|---|
|  | 42 | namespace SymTab { | 
|---|
| [a40d503] | 43 | std::ostream & operator<<( std::ostream & out, const Indexer::IdData & data ) { | 
|---|
|  | 44 | return out << "(" << data.id << "," << data.baseExpr << ")"; | 
|---|
|  | 45 | } | 
|---|
|  | 46 |  | 
|---|
|  | 47 | typedef std::unordered_map< std::string, Indexer::IdData > MangleTable; | 
|---|
| [bed4c63e] | 48 | typedef std::unordered_map< std::string, MangleTable > IdTable; | 
|---|
| [52c2a72] | 49 | typedef std::unordered_map< std::string, NamedTypeDecl* > TypeTable; | 
|---|
|  | 50 | typedef std::unordered_map< std::string, StructDecl* > StructTable; | 
|---|
|  | 51 | typedef std::unordered_map< std::string, EnumDecl* > EnumTable; | 
|---|
|  | 52 | typedef std::unordered_map< std::string, UnionDecl* > UnionTable; | 
|---|
|  | 53 | typedef std::unordered_map< std::string, TraitDecl* > TraitTable; | 
|---|
|  | 54 |  | 
|---|
| [bed4c63e] | 55 | void dump( const IdTable &table, std::ostream &os ) { | 
|---|
|  | 56 | for ( IdTable::const_iterator id = table.begin(); id != table.end(); ++id ) { | 
|---|
|  | 57 | for ( MangleTable::const_iterator mangle = id->second.begin(); mangle != id->second.end(); ++mangle ) { | 
|---|
|  | 58 | os << mangle->second << std::endl; | 
|---|
|  | 59 | } | 
|---|
|  | 60 | } | 
|---|
|  | 61 | } | 
|---|
| [743fbda] | 62 |  | 
|---|
| [52c2a72] | 63 | template< typename Decl > | 
|---|
|  | 64 | void dump( const std::unordered_map< std::string, Decl* > &table, std::ostream &os ) { | 
|---|
|  | 65 | for ( typename std::unordered_map< std::string, Decl* >::const_iterator it = table.begin(); it != table.end(); ++it ) { | 
|---|
|  | 66 | os << it->second << std::endl; | 
|---|
|  | 67 | } // for | 
|---|
|  | 68 | } | 
|---|
| [743fbda] | 69 |  | 
|---|
| [e8032b0] | 70 | struct Indexer::Impl { | 
|---|
| [52c2a72] | 71 | Impl( unsigned long _scope ) : refCount(1), scope( _scope ), size( 0 ), base(), | 
|---|
| [e8032b0] | 72 | idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {} | 
|---|
| [52c2a72] | 73 | Impl( unsigned long _scope, Indexer &&_base ) : refCount(1), scope( _scope ), size( 0 ), base( _base ), | 
|---|
| [e8032b0] | 74 | idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {} | 
|---|
|  | 75 | unsigned long refCount;   ///< Number of references to these tables | 
|---|
| [52c2a72] | 76 | unsigned long scope;      ///< Scope these tables are associated with | 
|---|
| [e8032b0] | 77 | unsigned long size;       ///< Number of elements stored in this table | 
|---|
|  | 78 | const Indexer base;       ///< Base indexer this extends | 
|---|
| [743fbda] | 79 |  | 
|---|
| [e8032b0] | 80 | IdTable idTable;          ///< Identifier namespace | 
|---|
|  | 81 | TypeTable typeTable;      ///< Type namespace | 
|---|
|  | 82 | StructTable structTable;  ///< Struct namespace | 
|---|
|  | 83 | EnumTable enumTable;      ///< Enum namespace | 
|---|
|  | 84 | UnionTable unionTable;    ///< Union namespace | 
|---|
|  | 85 | TraitTable traitTable;    ///< Trait namespace | 
|---|
|  | 86 | }; | 
|---|
|  | 87 |  | 
|---|
|  | 88 | Indexer::Impl *Indexer::newRef( Indexer::Impl *toClone ) { | 
|---|
|  | 89 | if ( ! toClone ) return 0; | 
|---|
|  | 90 |  | 
|---|
|  | 91 | // shorten the search chain by skipping empty links | 
|---|
|  | 92 | Indexer::Impl *ret = toClone->size == 0 ? toClone->base.tables : toClone; | 
|---|
|  | 93 | if ( ret ) { ++ret->refCount; } | 
|---|
|  | 94 |  | 
|---|
|  | 95 | return ret; | 
|---|
|  | 96 | } | 
|---|
|  | 97 |  | 
|---|
|  | 98 | void Indexer::deleteRef( Indexer::Impl *toFree ) { | 
|---|
|  | 99 | if ( ! toFree ) return; | 
|---|
|  | 100 |  | 
|---|
|  | 101 | if ( --toFree->refCount == 0 ) delete toFree; | 
|---|
|  | 102 | } | 
|---|
|  | 103 |  | 
|---|
| [a40d503] | 104 | void Indexer::removeSpecialOverrides( const std::string &id, std::list< IdData > & out ) const { | 
|---|
| [ee1635c8] | 105 | // only need to perform this step for constructors, destructors, and assignment functions | 
|---|
| [bff227f] | 106 | if ( ! CodeGen::isCtorDtorAssign( id ) ) return; | 
|---|
| [1ba88a0] | 107 |  | 
|---|
| [0a75b77] | 108 | // helpful data structure to organize properties for a type | 
|---|
| [1ba88a0] | 109 | struct ValueType { | 
|---|
| [0a75b77] | 110 | struct DeclBall { // properties for this particular decl | 
|---|
| [a40d503] | 111 | IdData decl; | 
|---|
| [0a75b77] | 112 | bool isUserDefinedFunc; | 
|---|
| [1ba88a0] | 113 | bool isCopyFunc; | 
|---|
|  | 114 | }; | 
|---|
|  | 115 | // properties for this type | 
|---|
| [ff03f5c] | 116 | bool existsUserDefinedCopyFunc = false;    // user-defined copy ctor found | 
|---|
| [0a75b77] | 117 | BaseSyntaxNode * deleteStmt = nullptr;     // non-null if a user-defined function is found | 
|---|
| [1ba88a0] | 118 | std::list< DeclBall > decls; | 
|---|
|  | 119 |  | 
|---|
|  | 120 | // another FunctionDecl for the current type was found - determine | 
|---|
|  | 121 | // if it has special properties and update data structure accordingly | 
|---|
| [a40d503] | 122 | ValueType & operator+=( IdData data ) { | 
|---|
|  | 123 | DeclarationWithType * function = data.id; | 
|---|
| [0a75b77] | 124 | bool isUserDefinedFunc = ! LinkageSpec::isOverridable( function->linkage ); | 
|---|
|  | 125 | bool isCopyFunc = InitTweak::isCopyFunction( function, function->name ); | 
|---|
|  | 126 | decls.push_back( DeclBall{ data, isUserDefinedFunc, isCopyFunc } ); | 
|---|
| [ff03f5c] | 127 | existsUserDefinedCopyFunc = existsUserDefinedCopyFunc || (isUserDefinedFunc && isCopyFunc); | 
|---|
| [1057e3d] | 128 | if ( isUserDefinedFunc && ! deleteStmt ) { | 
|---|
| [0a75b77] | 129 | // any user-defined function can act as an implicit delete statement for generated constructors. | 
|---|
|  | 130 | // a delete stmt should not act as an implicit delete statement. | 
|---|
|  | 131 | deleteStmt = data.id; | 
|---|
|  | 132 | } | 
|---|
| [1ba88a0] | 133 | return *this; | 
|---|
|  | 134 | } | 
|---|
|  | 135 | }; // ValueType | 
|---|
|  | 136 |  | 
|---|
| [a40d503] | 137 | std::list< IdData > copy; | 
|---|
| [1ba88a0] | 138 | copy.splice( copy.end(), out ); | 
|---|
|  | 139 |  | 
|---|
|  | 140 | // organize discovered declarations by type | 
|---|
|  | 141 | std::unordered_map< std::string, ValueType > funcMap; | 
|---|
| [a40d503] | 142 | for ( auto decl : copy ) { | 
|---|
|  | 143 | if ( FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl.id ) ) { | 
|---|
| [96812c0] | 144 | std::list< DeclarationWithType * > & params = function->type->parameters; | 
|---|
| [1ba88a0] | 145 | assert( ! params.empty() ); | 
|---|
| [24670d2] | 146 | // use base type of pointer, so that qualifiers on the pointer type aren't considered. | 
|---|
| [ce8c12f] | 147 | Type * base = InitTweak::getPointerBase( params.front()->get_type() ); | 
|---|
|  | 148 | assert( base ); | 
|---|
| [a40d503] | 149 | funcMap[ Mangler::mangle( base ) ] += decl; | 
|---|
| [1ba88a0] | 150 | } else { | 
|---|
|  | 151 | out.push_back( decl ); | 
|---|
|  | 152 | } | 
|---|
|  | 153 | } | 
|---|
|  | 154 |  | 
|---|
| [ff03f5c] | 155 | // if a type contains user defined ctor/dtor/assign, then special rules trigger, which determine | 
|---|
| [0a75b77] | 156 | // the set of ctor/dtor/assign that can be used  by the requester. In particular, if the user defines | 
|---|
|  | 157 | // a default ctor, then the generated default ctor is unavailable, likewise for copy ctor | 
|---|
|  | 158 | // and dtor. If the user defines any ctor/dtor, then no generated field ctors are available. | 
|---|
|  | 159 | // If the user defines any ctor then the generated default ctor is unavailable (intrinsic default | 
|---|
|  | 160 | // ctor must be overridden exactly). If the user defines anything that looks like a copy constructor, | 
|---|
|  | 161 | // then the generated copy constructor is unavailable, and likewise for the assignment operator. | 
|---|
| [1ba88a0] | 162 | for ( std::pair< const std::string, ValueType > & pair : funcMap ) { | 
|---|
|  | 163 | ValueType & val = pair.second; | 
|---|
|  | 164 | for ( ValueType::DeclBall ball : val.decls ) { | 
|---|
| [0a75b77] | 165 | bool isNotUserDefinedFunc = ! ball.isUserDefinedFunc && ball.decl.id->linkage != LinkageSpec::Intrinsic; | 
|---|
|  | 166 | bool isCopyFunc = ball.isCopyFunc; | 
|---|
|  | 167 | bool existsUserDefinedCopyFunc = val.existsUserDefinedCopyFunc; | 
|---|
| [1057e3d] | 168 |  | 
|---|
|  | 169 | // only implicitly delete non-user defined functions that are not intrinsic, and are | 
|---|
|  | 170 | // not copy functions (assignment or copy constructor). If a  user-defined copy function exists, | 
|---|
|  | 171 | // do not pass along the non-user-defined copy functions since signatures do not have to match, | 
|---|
|  | 172 | // and the generated functions will often be cheaper. | 
|---|
|  | 173 | if ( isNotUserDefinedFunc ) { | 
|---|
|  | 174 | if ( isCopyFunc ) { | 
|---|
|  | 175 | // Skip over non-user-defined copy functions when there is a user-defined copy function. | 
|---|
|  | 176 | // Since their signatures do not have to be exact, deleting them is the wrong choice. | 
|---|
|  | 177 | if ( existsUserDefinedCopyFunc ) continue; | 
|---|
|  | 178 | } else { | 
|---|
|  | 179 | // delete non-user-defined non-copy functions if applicable. | 
|---|
|  | 180 | // deleteStmt will be non-null only if a user-defined function is found. | 
|---|
|  | 181 | ball.decl.deleteStmt = val.deleteStmt; | 
|---|
|  | 182 | } | 
|---|
| [1ba88a0] | 183 | } | 
|---|
| [0a75b77] | 184 | out.push_back( ball.decl ); | 
|---|
| [1ba88a0] | 185 | } | 
|---|
|  | 186 | } | 
|---|
|  | 187 | } | 
|---|
|  | 188 |  | 
|---|
| [e8032b0] | 189 | void Indexer::makeWritable() { | 
|---|
|  | 190 | if ( ! tables ) { | 
|---|
| [52c2a72] | 191 | // create indexer if not yet set | 
|---|
|  | 192 | tables = new Indexer::Impl( scope ); | 
|---|
|  | 193 | } else if ( tables->refCount > 1 || tables->scope != scope ) { | 
|---|
|  | 194 | // make this indexer the base of a fresh indexer at the current scope | 
|---|
|  | 195 | tables = new Indexer::Impl( scope, std::move( *this ) ); | 
|---|
| [e8032b0] | 196 | } | 
|---|
|  | 197 | } | 
|---|
|  | 198 |  | 
|---|
| [8b11840] | 199 | Indexer::Indexer() : tables( 0 ), scope( 0 ) {} | 
|---|
| [e8032b0] | 200 |  | 
|---|
| [8b11840] | 201 | Indexer::Indexer( const Indexer &that ) : doDebug( that.doDebug ), tables( newRef( that.tables ) ), scope( that.scope ) {} | 
|---|
| [e8032b0] | 202 |  | 
|---|
| [8b11840] | 203 | Indexer::Indexer( Indexer &&that ) : doDebug( that.doDebug ), tables( that.tables ), scope( that.scope ) { | 
|---|
| [e8032b0] | 204 | that.tables = 0; | 
|---|
|  | 205 | } | 
|---|
|  | 206 |  | 
|---|
|  | 207 | Indexer::~Indexer() { | 
|---|
|  | 208 | deleteRef( tables ); | 
|---|
|  | 209 | } | 
|---|
|  | 210 |  | 
|---|
|  | 211 | Indexer& Indexer::operator= ( const Indexer &that ) { | 
|---|
|  | 212 | deleteRef( tables ); | 
|---|
|  | 213 |  | 
|---|
|  | 214 | tables = newRef( that.tables ); | 
|---|
| [52c2a72] | 215 | scope = that.scope; | 
|---|
| [e8032b0] | 216 | doDebug = that.doDebug; | 
|---|
|  | 217 |  | 
|---|
|  | 218 | return *this; | 
|---|
|  | 219 | } | 
|---|
|  | 220 |  | 
|---|
|  | 221 | Indexer& Indexer::operator= ( Indexer &&that ) { | 
|---|
|  | 222 | deleteRef( tables ); | 
|---|
|  | 223 |  | 
|---|
|  | 224 | tables = that.tables; | 
|---|
| [52c2a72] | 225 | scope = that.scope; | 
|---|
| [e8032b0] | 226 | doDebug = that.doDebug; | 
|---|
| [17cd4eb] | 227 |  | 
|---|
| [e8032b0] | 228 | that.tables = 0; | 
|---|
|  | 229 |  | 
|---|
|  | 230 | return *this; | 
|---|
|  | 231 | } | 
|---|
| [17cd4eb] | 232 |  | 
|---|
| [a40d503] | 233 | void Indexer::lookupId( const std::string &id, std::list< IdData > &out ) const { | 
|---|
| [5447e09] | 234 | std::unordered_set< std::string > foundMangleNames; | 
|---|
| [743fbda] | 235 |  | 
|---|
| [5447e09] | 236 | Indexer::Impl *searchTables = tables; | 
|---|
|  | 237 | while ( searchTables ) { | 
|---|
|  | 238 |  | 
|---|
|  | 239 | IdTable::const_iterator decls = searchTables->idTable.find( id ); | 
|---|
|  | 240 | if ( decls != searchTables->idTable.end() ) { | 
|---|
|  | 241 | const MangleTable &mangleTable = decls->second; | 
|---|
|  | 242 | for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { | 
|---|
|  | 243 | // mark the mangled name as found, skipping this insertion if a declaration for that name has already been found | 
|---|
|  | 244 | if ( foundMangleNames.insert( decl->first ).second == false ) continue; | 
|---|
| [743fbda] | 245 |  | 
|---|
| [5447e09] | 246 | out.push_back( decl->second ); | 
|---|
|  | 247 | } | 
|---|
| [bed4c63e] | 248 | } | 
|---|
| [743fbda] | 249 |  | 
|---|
| [5447e09] | 250 | // get declarations from base indexers | 
|---|
|  | 251 | searchTables = searchTables->base.tables; | 
|---|
| [bed4c63e] | 252 | } | 
|---|
| [1ba88a0] | 253 |  | 
|---|
|  | 254 | // some special functions, e.g. constructors and destructors | 
|---|
|  | 255 | // remove autogenerated functions when they are defined so that | 
|---|
|  | 256 | // they can never be matched | 
|---|
|  | 257 | removeSpecialOverrides( id, out ); | 
|---|
| [a08ba92] | 258 | } | 
|---|
| [bdd516a] | 259 |  | 
|---|
| [a08ba92] | 260 | NamedTypeDecl *Indexer::lookupType( const std::string &id ) const { | 
|---|
| [e8032b0] | 261 | if ( ! tables ) return 0; | 
|---|
|  | 262 |  | 
|---|
| [52c2a72] | 263 | TypeTable::const_iterator ret = tables->typeTable.find( id ); | 
|---|
|  | 264 | return ret != tables->typeTable.end() ? ret->second : tables->base.lookupType( id ); | 
|---|
| [a08ba92] | 265 | } | 
|---|
| [17cd4eb] | 266 |  | 
|---|
| [a08ba92] | 267 | StructDecl *Indexer::lookupStruct( const std::string &id ) const { | 
|---|
| [e8032b0] | 268 | if ( ! tables ) return 0; | 
|---|
|  | 269 |  | 
|---|
| [52c2a72] | 270 | StructTable::const_iterator ret = tables->structTable.find( id ); | 
|---|
|  | 271 | return ret != tables->structTable.end() ? ret->second : tables->base.lookupStruct( id ); | 
|---|
| [a08ba92] | 272 | } | 
|---|
| [17cd4eb] | 273 |  | 
|---|
| [ed34540] | 274 | NamedTypeDecl *Indexer::globalLookupType( const std::string &id ) const { | 
|---|
|  | 275 | return lookupTypeAtScope( id, 0 ); | 
|---|
|  | 276 | } | 
|---|
|  | 277 |  | 
|---|
| [9a7a3b6] | 278 | StructDecl *Indexer::globalLookupStruct( const std::string &id ) const { | 
|---|
|  | 279 | return lookupStructAtScope( id, 0 ); | 
|---|
|  | 280 | } | 
|---|
|  | 281 |  | 
|---|
|  | 282 | UnionDecl *Indexer::globalLookupUnion( const std::string &id ) const { | 
|---|
|  | 283 | return lookupUnionAtScope( id, 0 ); | 
|---|
|  | 284 | } | 
|---|
|  | 285 |  | 
|---|
|  | 286 | EnumDecl *Indexer::globalLookupEnum( const std::string &id ) const { | 
|---|
|  | 287 | return lookupEnumAtScope( id, 0 ); | 
|---|
|  | 288 | } | 
|---|
|  | 289 |  | 
|---|
| [a08ba92] | 290 | EnumDecl *Indexer::lookupEnum( const std::string &id ) const { | 
|---|
| [e8032b0] | 291 | if ( ! tables ) return 0; | 
|---|
|  | 292 |  | 
|---|
| [52c2a72] | 293 | EnumTable::const_iterator ret = tables->enumTable.find( id ); | 
|---|
|  | 294 | return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnum( id ); | 
|---|
| [a08ba92] | 295 | } | 
|---|
| [17cd4eb] | 296 |  | 
|---|
| [a08ba92] | 297 | UnionDecl *Indexer::lookupUnion( const std::string &id ) const { | 
|---|
| [e8032b0] | 298 | if ( ! tables ) return 0; | 
|---|
|  | 299 |  | 
|---|
| [52c2a72] | 300 | UnionTable::const_iterator ret = tables->unionTable.find( id ); | 
|---|
|  | 301 | return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnion( id ); | 
|---|
| [e8032b0] | 302 | } | 
|---|
|  | 303 |  | 
|---|
|  | 304 | TraitDecl *Indexer::lookupTrait( const std::string &id ) const { | 
|---|
|  | 305 | if ( ! tables ) return 0; | 
|---|
|  | 306 |  | 
|---|
| [52c2a72] | 307 | TraitTable::const_iterator ret = tables->traitTable.find( id ); | 
|---|
|  | 308 | return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTrait( id ); | 
|---|
|  | 309 | } | 
|---|
|  | 310 |  | 
|---|
| [0ac366b] | 311 | const Indexer::IdData * Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) const { | 
|---|
|  | 312 | if ( ! tables ) return nullptr; | 
|---|
|  | 313 | if ( tables->scope < scope ) return nullptr; | 
|---|
| [bed4c63e] | 314 |  | 
|---|
|  | 315 | IdTable::const_iterator decls = tables->idTable.find( id ); | 
|---|
|  | 316 | if ( decls != tables->idTable.end() ) { | 
|---|
|  | 317 | const MangleTable &mangleTable = decls->second; | 
|---|
|  | 318 | MangleTable::const_iterator decl = mangleTable.find( mangleName ); | 
|---|
| [0ac366b] | 319 | if ( decl != mangleTable.end() ) return &decl->second; | 
|---|
| [bed4c63e] | 320 | } | 
|---|
|  | 321 |  | 
|---|
|  | 322 | return tables->base.lookupIdAtScope( id, mangleName, scope ); | 
|---|
|  | 323 | } | 
|---|
|  | 324 |  | 
|---|
| [0ac366b] | 325 | Indexer::IdData * Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) { | 
|---|
|  | 326 | return const_cast<IdData *>(const_cast<const Indexer *>(this)->lookupIdAtScope( id, mangleName, scope )); | 
|---|
|  | 327 | } | 
|---|
|  | 328 |  | 
|---|
| [09d1ad0] | 329 | bool Indexer::hasIncompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const { | 
|---|
| [bed4c63e] | 330 | if ( ! tables ) return false; | 
|---|
| [09d1ad0] | 331 | if ( tables->scope < scope ) return false; | 
|---|
| [bed4c63e] | 332 |  | 
|---|
|  | 333 | IdTable::const_iterator decls = tables->idTable.find( id ); | 
|---|
|  | 334 | if ( decls != tables->idTable.end() ) { | 
|---|
|  | 335 | const MangleTable &mangleTable = decls->second; | 
|---|
|  | 336 | for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { | 
|---|
| [4e06c1e] | 337 | // check for C decls with the same name, skipping those with a compatible type (by mangleName) | 
|---|
| [a40d503] | 338 | if ( ! LinkageSpec::isMangled( decl->second.id->get_linkage() ) && decl->first != mangleName ) return true; | 
|---|
| [bed4c63e] | 339 | } | 
|---|
|  | 340 | } | 
|---|
|  | 341 |  | 
|---|
| [09d1ad0] | 342 | return tables->base.hasIncompatibleCDecl( id, mangleName, scope ); | 
|---|
| [bed4c63e] | 343 | } | 
|---|
| [743fbda] | 344 |  | 
|---|
| [8884112] | 345 | bool Indexer::hasCompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const { | 
|---|
|  | 346 | if ( ! tables ) return false; | 
|---|
|  | 347 | if ( tables->scope < scope ) return false; | 
|---|
|  | 348 |  | 
|---|
|  | 349 | IdTable::const_iterator decls = tables->idTable.find( id ); | 
|---|
|  | 350 | if ( decls != tables->idTable.end() ) { | 
|---|
|  | 351 | const MangleTable &mangleTable = decls->second; | 
|---|
|  | 352 | for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { | 
|---|
|  | 353 | // check for C decls with the same name, skipping | 
|---|
|  | 354 | // those with an incompatible type (by mangleName) | 
|---|
| [a40d503] | 355 | if ( ! LinkageSpec::isMangled( decl->second.id->get_linkage() ) && decl->first == mangleName ) return true; | 
|---|
| [8884112] | 356 | } | 
|---|
|  | 357 | } | 
|---|
|  | 358 |  | 
|---|
|  | 359 | return tables->base.hasCompatibleCDecl( id, mangleName, scope ); | 
|---|
|  | 360 | } | 
|---|
|  | 361 |  | 
|---|
| [52c2a72] | 362 | NamedTypeDecl *Indexer::lookupTypeAtScope( const std::string &id, unsigned long scope ) const { | 
|---|
|  | 363 | if ( ! tables ) return 0; | 
|---|
|  | 364 | if ( tables->scope < scope ) return 0; | 
|---|
| [9a7a3b6] | 365 | if ( tables->scope > scope ) return tables->base.lookupTypeAtScope( id, scope ); | 
|---|
| [52c2a72] | 366 |  | 
|---|
|  | 367 | TypeTable::const_iterator ret = tables->typeTable.find( id ); | 
|---|
|  | 368 | return ret != tables->typeTable.end() ? ret->second : tables->base.lookupTypeAtScope( id, scope ); | 
|---|
|  | 369 | } | 
|---|
| [743fbda] | 370 |  | 
|---|
| [52c2a72] | 371 | StructDecl *Indexer::lookupStructAtScope( const std::string &id, unsigned long scope ) const { | 
|---|
|  | 372 | if ( ! tables ) return 0; | 
|---|
|  | 373 | if ( tables->scope < scope ) return 0; | 
|---|
| [9a7a3b6] | 374 | if ( tables->scope > scope ) return tables->base.lookupStructAtScope( id, scope ); | 
|---|
| [52c2a72] | 375 |  | 
|---|
|  | 376 | StructTable::const_iterator ret = tables->structTable.find( id ); | 
|---|
|  | 377 | return ret != tables->structTable.end() ? ret->second : tables->base.lookupStructAtScope( id, scope ); | 
|---|
|  | 378 | } | 
|---|
| [743fbda] | 379 |  | 
|---|
| [52c2a72] | 380 | EnumDecl *Indexer::lookupEnumAtScope( const std::string &id, unsigned long scope ) const { | 
|---|
|  | 381 | if ( ! tables ) return 0; | 
|---|
|  | 382 | if ( tables->scope < scope ) return 0; | 
|---|
| [9a7a3b6] | 383 | if ( tables->scope > scope ) return tables->base.lookupEnumAtScope( id, scope ); | 
|---|
| [52c2a72] | 384 |  | 
|---|
|  | 385 | EnumTable::const_iterator ret = tables->enumTable.find( id ); | 
|---|
|  | 386 | return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnumAtScope( id, scope ); | 
|---|
|  | 387 | } | 
|---|
| [743fbda] | 388 |  | 
|---|
| [52c2a72] | 389 | UnionDecl *Indexer::lookupUnionAtScope( const std::string &id, unsigned long scope ) const { | 
|---|
|  | 390 | if ( ! tables ) return 0; | 
|---|
|  | 391 | if ( tables->scope < scope ) return 0; | 
|---|
| [9a7a3b6] | 392 | if ( tables->scope > scope ) return tables->base.lookupUnionAtScope( id, scope ); | 
|---|
| [52c2a72] | 393 |  | 
|---|
|  | 394 | UnionTable::const_iterator ret = tables->unionTable.find( id ); | 
|---|
|  | 395 | return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnionAtScope( id, scope ); | 
|---|
|  | 396 | } | 
|---|
| [743fbda] | 397 |  | 
|---|
| [52c2a72] | 398 | TraitDecl *Indexer::lookupTraitAtScope( const std::string &id, unsigned long scope ) const { | 
|---|
|  | 399 | if ( ! tables ) return 0; | 
|---|
|  | 400 | if ( tables->scope < scope ) return 0; | 
|---|
| [9a7a3b6] | 401 | if ( tables->scope > scope ) return tables->base.lookupTraitAtScope( id, scope ); | 
|---|
| [52c2a72] | 402 |  | 
|---|
|  | 403 | TraitTable::const_iterator ret = tables->traitTable.find( id ); | 
|---|
|  | 404 | return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTraitAtScope( id, scope ); | 
|---|
| [a08ba92] | 405 | } | 
|---|
| [17cd4eb] | 406 |  | 
|---|
| [3f024c9] | 407 | bool isFunction( DeclarationWithType * decl ) { | 
|---|
|  | 408 | return GenPoly::getFunctionType( decl->get_type() ); | 
|---|
|  | 409 | } | 
|---|
|  | 410 |  | 
|---|
|  | 411 | bool isObject( DeclarationWithType * decl ) { | 
|---|
|  | 412 | return ! isFunction( decl ); | 
|---|
|  | 413 | } | 
|---|
|  | 414 |  | 
|---|
|  | 415 | bool isDefinition( DeclarationWithType * decl ) { | 
|---|
|  | 416 | if ( FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl ) ) { | 
|---|
|  | 417 | // a function is a definition if it has a body | 
|---|
|  | 418 | return func->statements; | 
|---|
|  | 419 | } else { | 
|---|
|  | 420 | // an object is a definition if it is not marked extern. | 
|---|
|  | 421 | // both objects must be marked extern | 
|---|
|  | 422 | return ! decl->get_storageClasses().is_extern; | 
|---|
|  | 423 | } | 
|---|
|  | 424 | } | 
|---|
|  | 425 |  | 
|---|
| [0ac366b] | 426 | bool addedIdConflicts( Indexer::IdData & existing, DeclarationWithType *added, BaseSyntaxNode * deleteStmt, Indexer::ConflictFunction handleConflicts ) { | 
|---|
| [bed4c63e] | 427 | // if we're giving the same name mangling to things of different types then there is something wrong | 
|---|
| [3f024c9] | 428 | assert( (isObject( added ) && isObject( existing.id ) ) | 
|---|
|  | 429 | || ( isFunction( added ) && isFunction( existing.id ) ) ); | 
|---|
| [bed4c63e] | 430 |  | 
|---|
| [0ac366b] | 431 | if ( LinkageSpec::isOverridable( existing.id->get_linkage() ) ) { | 
|---|
| [bed4c63e] | 432 | // new definition shadows the autogenerated one, even at the same scope | 
|---|
|  | 433 | return false; | 
|---|
| [0ac366b] | 434 | } else if ( LinkageSpec::isMangled( added->get_linkage() ) || ResolvExpr::typesCompatible( added->get_type(), existing.id->get_type(), Indexer() ) ) { | 
|---|
|  | 435 |  | 
|---|
|  | 436 | // it is a conflict if one declaration is deleted and the other is not | 
|---|
|  | 437 | if ( deleteStmt && ! existing.deleteStmt ) { | 
|---|
|  | 438 | return handleConflicts( existing, "deletion of defined identifier " ); | 
|---|
|  | 439 | } else if ( ! deleteStmt && existing.deleteStmt ) { | 
|---|
|  | 440 | return handleConflicts( existing, "definition of deleted identifier " ); | 
|---|
|  | 441 | } | 
|---|
|  | 442 |  | 
|---|
| [3f024c9] | 443 | if ( isDefinition( added ) && isDefinition( existing.id ) ) { | 
|---|
|  | 444 | if ( isFunction( added ) ) { | 
|---|
| [0ac366b] | 445 | return handleConflicts( existing, "duplicate function definition for " ); | 
|---|
| [3f024c9] | 446 | } else { | 
|---|
| [0ac366b] | 447 | return handleConflicts( existing, "duplicate object definition for " ); | 
|---|
| [bed4c63e] | 448 | } // if | 
|---|
|  | 449 | } // if | 
|---|
|  | 450 | } else { | 
|---|
| [0ac366b] | 451 | return handleConflicts( existing, "duplicate definition for " ); | 
|---|
| [bed4c63e] | 452 | } // if | 
|---|
|  | 453 |  | 
|---|
|  | 454 | return true; | 
|---|
|  | 455 | } | 
|---|
| [743fbda] | 456 |  | 
|---|
| [0ac366b] | 457 | void Indexer::addId( DeclarationWithType *decl, ConflictFunction handleConflicts, Expression * baseExpr, BaseSyntaxNode * deleteStmt ) { | 
|---|
| [2cb70aa] | 458 | if ( decl->name == "" ) return; | 
|---|
| [33a25f9] | 459 | debugPrint( "Adding Id " << decl->name << std::endl ); | 
|---|
| [e8032b0] | 460 | makeWritable(); | 
|---|
| [bed4c63e] | 461 |  | 
|---|
| [6fc5c14] | 462 | const std::string &name = decl->name; | 
|---|
| [bed4c63e] | 463 | std::string mangleName; | 
|---|
| [6fc5c14] | 464 | if ( LinkageSpec::isOverridable( decl->linkage ) ) { | 
|---|
| [bed4c63e] | 465 | // mangle the name without including the appropriate suffix, so overridable routines are placed into the | 
|---|
|  | 466 | // same "bucket" as their user defined versions. | 
|---|
|  | 467 | mangleName = Mangler::mangle( decl, false ); | 
|---|
|  | 468 | } else { | 
|---|
|  | 469 | mangleName = Mangler::mangle( decl ); | 
|---|
|  | 470 | } // if | 
|---|
|  | 471 |  | 
|---|
| [8884112] | 472 | // this ensures that no two declarations with the same unmangled name at the same scope both have C linkage | 
|---|
| [6fc5c14] | 473 | if ( ! LinkageSpec::isMangled( decl->linkage ) ) { | 
|---|
| [8884112] | 474 | // NOTE this is broken in Richard's original code in such a way that it never triggers (it | 
|---|
|  | 475 | // doesn't check decls that have the same manglename, and all C-linkage decls are defined to | 
|---|
|  | 476 | // have their name as their manglename, hence the error can never trigger). | 
|---|
|  | 477 | // The code here is closer to correct, but name mangling would have to be completely | 
|---|
|  | 478 | // isomorphic to C type-compatibility, which it may not be. | 
|---|
|  | 479 | if ( hasIncompatibleCDecl( name, mangleName, scope ) ) { | 
|---|
| [a16764a6] | 480 | SemanticError( decl, "conflicting overload of C function " ); | 
|---|
| [8884112] | 481 | } | 
|---|
|  | 482 | } else { | 
|---|
| [490ff5c3] | 483 | // Check that a Cforall declaration doesn't override any C declaration | 
|---|
| [8884112] | 484 | if ( hasCompatibleCDecl( name, mangleName, scope ) ) { | 
|---|
| [a16764a6] | 485 | SemanticError( decl, "Cforall declaration hides C function " ); | 
|---|
| [8884112] | 486 | } | 
|---|
| [bed4c63e] | 487 | } | 
|---|
| [8884112] | 488 |  | 
|---|
|  | 489 | // Skip repeat declarations of the same identifier | 
|---|
| [0ac366b] | 490 | IdData * existing = lookupIdAtScope( name, mangleName, scope ); | 
|---|
|  | 491 | if ( existing && existing->id && addedIdConflicts( *existing, decl, deleteStmt, handleConflicts ) ) return; | 
|---|
| [8884112] | 492 |  | 
|---|
|  | 493 | // add to indexer | 
|---|
| [78d69da7] | 494 | tables->idTable[ name ][ mangleName ] = IdData{ decl, baseExpr, deleteStmt }; | 
|---|
| [8884112] | 495 | ++tables->size; | 
|---|
| [e8032b0] | 496 | } | 
|---|
| [52c2a72] | 497 |  | 
|---|
| [0ac366b] | 498 | void Indexer::addId( DeclarationWithType * decl, Expression * baseExpr ) { | 
|---|
|  | 499 | // default handling of conflicts is to raise an error | 
|---|
| [3ed994e] | 500 | addId( decl, [decl](IdData &, const std::string & msg) { SemanticError( decl, msg ); return true; }, baseExpr, decl->isDeleted ? decl : nullptr ); | 
|---|
| [0ac366b] | 501 | } | 
|---|
|  | 502 |  | 
|---|
|  | 503 | void Indexer::addDeletedId( DeclarationWithType * decl, BaseSyntaxNode * deleteStmt ) { | 
|---|
|  | 504 | // default handling of conflicts is to raise an error | 
|---|
| [a16764a6] | 505 | addId( decl, [decl](IdData &, const std::string & msg) { SemanticError( decl, msg ); return true; }, nullptr, deleteStmt ); | 
|---|
| [0ac366b] | 506 | } | 
|---|
|  | 507 |  | 
|---|
| [52c2a72] | 508 | bool addedTypeConflicts( NamedTypeDecl *existing, NamedTypeDecl *added ) { | 
|---|
| [ed34540] | 509 | if ( existing->base == nullptr ) { | 
|---|
| [52c2a72] | 510 | return false; | 
|---|
| [ed34540] | 511 | } else if ( added->base == nullptr ) { | 
|---|
| [52c2a72] | 512 | return true; | 
|---|
|  | 513 | } else { | 
|---|
| [ed34540] | 514 | assert( existing->base && added->base ); | 
|---|
|  | 515 | // typedef redeclarations are errors only if types are different | 
|---|
|  | 516 | if ( ! ResolvExpr::typesCompatible( existing->base, added->base, Indexer() ) ) { | 
|---|
|  | 517 | SemanticError( added->location, "redeclaration of " + added->name ); | 
|---|
|  | 518 | } | 
|---|
| [52c2a72] | 519 | } | 
|---|
| [ed34540] | 520 | // does not need to be added to the table if both existing and added have a base that are the same | 
|---|
|  | 521 | return true; | 
|---|
| [52c2a72] | 522 | } | 
|---|
| [743fbda] | 523 |  | 
|---|
| [e8032b0] | 524 | void Indexer::addType( NamedTypeDecl *decl ) { | 
|---|
| [33a25f9] | 525 | debugPrint( "Adding type " << decl->name << std::endl ); | 
|---|
| [e8032b0] | 526 | makeWritable(); | 
|---|
| [52c2a72] | 527 |  | 
|---|
| [589a70b] | 528 | const std::string &id = decl->name; | 
|---|
| [52c2a72] | 529 | TypeTable::iterator existing = tables->typeTable.find( id ); | 
|---|
|  | 530 | if ( existing == tables->typeTable.end() ) { | 
|---|
|  | 531 | NamedTypeDecl *parent = tables->base.lookupTypeAtScope( id, scope ); | 
|---|
|  | 532 | if ( ! parent || ! addedTypeConflicts( parent, decl ) ) { | 
|---|
|  | 533 | tables->typeTable.insert( existing, std::make_pair( id, decl ) ); | 
|---|
|  | 534 | ++tables->size; | 
|---|
|  | 535 | } | 
|---|
|  | 536 | } else { | 
|---|
|  | 537 | if ( ! addedTypeConflicts( existing->second, decl ) ) { | 
|---|
|  | 538 | existing->second = decl; | 
|---|
|  | 539 | } | 
|---|
|  | 540 | } | 
|---|
|  | 541 | } | 
|---|
|  | 542 |  | 
|---|
|  | 543 | bool addedDeclConflicts( AggregateDecl *existing, AggregateDecl *added ) { | 
|---|
| [b2da0574] | 544 | if ( ! existing->body ) { | 
|---|
| [52c2a72] | 545 | return false; | 
|---|
| [b2da0574] | 546 | } else if ( added->body ) { | 
|---|
| [a16764a6] | 547 | SemanticError( added, "redeclaration of " ); | 
|---|
| [52c2a72] | 548 | } // if | 
|---|
|  | 549 | return true; | 
|---|
| [e8032b0] | 550 | } | 
|---|
|  | 551 |  | 
|---|
|  | 552 | void Indexer::addStruct( const std::string &id ) { | 
|---|
| [33a25f9] | 553 | debugPrint( "Adding fwd decl for struct " << id << std::endl ); | 
|---|
| [52c2a72] | 554 | addStruct( new StructDecl( id ) ); | 
|---|
| [e8032b0] | 555 | } | 
|---|
| [743fbda] | 556 |  | 
|---|
| [e8032b0] | 557 | void Indexer::addStruct( StructDecl *decl ) { | 
|---|
| [33a25f9] | 558 | debugPrint( "Adding struct " << decl->name << std::endl ); | 
|---|
| [e8032b0] | 559 | makeWritable(); | 
|---|
| [52c2a72] | 560 |  | 
|---|
| [589a70b] | 561 | const std::string &id = decl->name; | 
|---|
| [52c2a72] | 562 | StructTable::iterator existing = tables->structTable.find( id ); | 
|---|
|  | 563 | if ( existing == tables->structTable.end() ) { | 
|---|
|  | 564 | StructDecl *parent = tables->base.lookupStructAtScope( id, scope ); | 
|---|
|  | 565 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { | 
|---|
|  | 566 | tables->structTable.insert( existing, std::make_pair( id, decl ) ); | 
|---|
|  | 567 | ++tables->size; | 
|---|
|  | 568 | } | 
|---|
|  | 569 | } else { | 
|---|
|  | 570 | if ( ! addedDeclConflicts( existing->second, decl ) ) { | 
|---|
|  | 571 | existing->second = decl; | 
|---|
|  | 572 | } | 
|---|
|  | 573 | } | 
|---|
| [e8032b0] | 574 | } | 
|---|
| [743fbda] | 575 |  | 
|---|
| [e8032b0] | 576 | void Indexer::addEnum( EnumDecl *decl ) { | 
|---|
| [33a25f9] | 577 | debugPrint( "Adding enum " << decl->name << std::endl ); | 
|---|
| [e8032b0] | 578 | makeWritable(); | 
|---|
| [52c2a72] | 579 |  | 
|---|
| [589a70b] | 580 | const std::string &id = decl->name; | 
|---|
| [52c2a72] | 581 | EnumTable::iterator existing = tables->enumTable.find( id ); | 
|---|
|  | 582 | if ( existing == tables->enumTable.end() ) { | 
|---|
|  | 583 | EnumDecl *parent = tables->base.lookupEnumAtScope( id, scope ); | 
|---|
|  | 584 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { | 
|---|
|  | 585 | tables->enumTable.insert( existing, std::make_pair( id, decl ) ); | 
|---|
|  | 586 | ++tables->size; | 
|---|
|  | 587 | } | 
|---|
|  | 588 | } else { | 
|---|
|  | 589 | if ( ! addedDeclConflicts( existing->second, decl ) ) { | 
|---|
|  | 590 | existing->second = decl; | 
|---|
|  | 591 | } | 
|---|
|  | 592 | } | 
|---|
| [e8032b0] | 593 | } | 
|---|
|  | 594 |  | 
|---|
|  | 595 | void Indexer::addUnion( const std::string &id ) { | 
|---|
| [33a25f9] | 596 | debugPrint( "Adding fwd decl for union " << id << std::endl ); | 
|---|
| [52c2a72] | 597 | addUnion( new UnionDecl( id ) ); | 
|---|
| [e8032b0] | 598 | } | 
|---|
| [743fbda] | 599 |  | 
|---|
| [e8032b0] | 600 | void Indexer::addUnion( UnionDecl *decl ) { | 
|---|
| [33a25f9] | 601 | debugPrint( "Adding union " << decl->name << std::endl ); | 
|---|
| [e8032b0] | 602 | makeWritable(); | 
|---|
| [52c2a72] | 603 |  | 
|---|
| [589a70b] | 604 | const std::string &id = decl->name; | 
|---|
| [52c2a72] | 605 | UnionTable::iterator existing = tables->unionTable.find( id ); | 
|---|
|  | 606 | if ( existing == tables->unionTable.end() ) { | 
|---|
|  | 607 | UnionDecl *parent = tables->base.lookupUnionAtScope( id, scope ); | 
|---|
|  | 608 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { | 
|---|
|  | 609 | tables->unionTable.insert( existing, std::make_pair( id, decl ) ); | 
|---|
|  | 610 | ++tables->size; | 
|---|
|  | 611 | } | 
|---|
|  | 612 | } else { | 
|---|
|  | 613 | if ( ! addedDeclConflicts( existing->second, decl ) ) { | 
|---|
|  | 614 | existing->second = decl; | 
|---|
|  | 615 | } | 
|---|
|  | 616 | } | 
|---|
| [e8032b0] | 617 | } | 
|---|
| [743fbda] | 618 |  | 
|---|
| [e8032b0] | 619 | void Indexer::addTrait( TraitDecl *decl ) { | 
|---|
| [33a25f9] | 620 | debugPrint( "Adding trait " << decl->name << std::endl ); | 
|---|
| [e8032b0] | 621 | makeWritable(); | 
|---|
| [52c2a72] | 622 |  | 
|---|
| [589a70b] | 623 | const std::string &id = decl->name; | 
|---|
| [52c2a72] | 624 | TraitTable::iterator existing = tables->traitTable.find( id ); | 
|---|
|  | 625 | if ( existing == tables->traitTable.end() ) { | 
|---|
|  | 626 | TraitDecl *parent = tables->base.lookupTraitAtScope( id, scope ); | 
|---|
|  | 627 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { | 
|---|
|  | 628 | tables->traitTable.insert( existing, std::make_pair( id, decl ) ); | 
|---|
|  | 629 | ++tables->size; | 
|---|
|  | 630 | } | 
|---|
|  | 631 | } else { | 
|---|
|  | 632 | if ( ! addedDeclConflicts( existing->second, decl ) ) { | 
|---|
|  | 633 | existing->second = decl; | 
|---|
|  | 634 | } | 
|---|
|  | 635 | } | 
|---|
| [a08ba92] | 636 | } | 
|---|
| [17cd4eb] | 637 |  | 
|---|
| [0ac366b] | 638 | void Indexer::addMembers( AggregateDecl * aggr, Expression * expr, ConflictFunction handleConflicts ) { | 
|---|
| [1485c1a] | 639 | for ( Declaration * decl : aggr->members ) { | 
|---|
|  | 640 | if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { | 
|---|
| [0ac366b] | 641 | addId( dwt, handleConflicts, expr ); | 
|---|
| [1485c1a] | 642 | if ( dwt->name == "" ) { | 
|---|
|  | 643 | Type * t = dwt->get_type()->stripReferences(); | 
|---|
|  | 644 | if ( dynamic_cast< StructInstType * >( t ) || dynamic_cast< UnionInstType * >( t ) ) { | 
|---|
|  | 645 | Expression * base = expr->clone(); | 
|---|
| [a181494] | 646 | ResolvExpr::Cost cost = ResolvExpr::Cost::zero; // xxx - carry this cost into the indexer as a base cost? | 
|---|
|  | 647 | ResolvExpr::referenceToRvalueConversion( base, cost ); | 
|---|
| [0ac366b] | 648 | addMembers( t->getAggr(), new MemberExpr( dwt, base ), handleConflicts ); | 
|---|
| [1485c1a] | 649 | } | 
|---|
|  | 650 | } | 
|---|
|  | 651 | } | 
|---|
|  | 652 | } | 
|---|
|  | 653 | } | 
|---|
|  | 654 |  | 
|---|
| [0ac366b] | 655 | void Indexer::addWith( std::list< Expression * > & withExprs, BaseSyntaxNode * withStmt ) { | 
|---|
| [4670c79] | 656 | for ( Expression * expr : withExprs ) { | 
|---|
| [81644e0] | 657 | if ( expr->result ) { | 
|---|
| [497282e] | 658 | AggregateDecl * aggr = expr->result->stripReferences()->getAggr(); | 
|---|
| [81644e0] | 659 | assertf( aggr, "WithStmt expr has non-aggregate type: %s", toString( expr->result ).c_str() ); | 
|---|
|  | 660 |  | 
|---|
| [0ac366b] | 661 | addMembers( aggr, expr, [withStmt](IdData & existing, const std::string &) { | 
|---|
|  | 662 | // on conflict, delete the identifier | 
|---|
|  | 663 | existing.deleteStmt = withStmt; | 
|---|
|  | 664 | return true; | 
|---|
|  | 665 | }); | 
|---|
| [81644e0] | 666 | } | 
|---|
|  | 667 | } | 
|---|
|  | 668 | } | 
|---|
|  | 669 |  | 
|---|
| [5fe35d6] | 670 | void Indexer::addIds( const std::list< DeclarationWithType * > & decls ) { | 
|---|
|  | 671 | for ( auto d : decls ) { | 
|---|
|  | 672 | addId( d ); | 
|---|
|  | 673 | } | 
|---|
|  | 674 | } | 
|---|
|  | 675 |  | 
|---|
|  | 676 | void Indexer::addTypes( const std::list< TypeDecl * > & tds ) { | 
|---|
|  | 677 | for ( auto td : tds ) { | 
|---|
|  | 678 | addType( td ); | 
|---|
|  | 679 | addIds( td->assertions ); | 
|---|
|  | 680 | } | 
|---|
|  | 681 | } | 
|---|
|  | 682 |  | 
|---|
|  | 683 | void Indexer::addFunctionType( FunctionType * ftype ) { | 
|---|
|  | 684 | addTypes( ftype->forall ); | 
|---|
|  | 685 | addIds( ftype->returnVals ); | 
|---|
|  | 686 | addIds( ftype->parameters ); | 
|---|
|  | 687 | } | 
|---|
|  | 688 |  | 
|---|
| [a08ba92] | 689 | void Indexer::enterScope() { | 
|---|
| [52c2a72] | 690 | ++scope; | 
|---|
| [743fbda] | 691 |  | 
|---|
| [0dd3a2f] | 692 | if ( doDebug ) { | 
|---|
| [6137fbb] | 693 | std::cerr << "--- Entering scope " << scope << std::endl; | 
|---|
| [0dd3a2f] | 694 | } | 
|---|
| [a08ba92] | 695 | } | 
|---|
| [17cd4eb] | 696 |  | 
|---|
| [a08ba92] | 697 | void Indexer::leaveScope() { | 
|---|
| [6137fbb] | 698 | using std::cerr; | 
|---|
| [52c2a72] | 699 |  | 
|---|
|  | 700 | assert( scope > 0 && "cannot leave initial scope" ); | 
|---|
| [6137fbb] | 701 | if ( doDebug ) { | 
|---|
|  | 702 | cerr << "--- Leaving scope " << scope << " containing" << std::endl; | 
|---|
|  | 703 | } | 
|---|
| [52c2a72] | 704 | --scope; | 
|---|
|  | 705 |  | 
|---|
|  | 706 | while ( tables && tables->scope > scope ) { | 
|---|
|  | 707 | if ( doDebug ) { | 
|---|
| [6137fbb] | 708 | dump( tables->idTable, cerr ); | 
|---|
|  | 709 | dump( tables->typeTable, cerr ); | 
|---|
|  | 710 | dump( tables->structTable, cerr ); | 
|---|
|  | 711 | dump( tables->enumTable, cerr ); | 
|---|
|  | 712 | dump( tables->unionTable, cerr ); | 
|---|
|  | 713 | dump( tables->traitTable, cerr ); | 
|---|
| [52c2a72] | 714 | } | 
|---|
|  | 715 |  | 
|---|
|  | 716 | // swap tables for base table until we find one at an appropriate scope | 
|---|
|  | 717 | Indexer::Impl *base = newRef( tables->base.tables ); | 
|---|
|  | 718 | deleteRef( tables ); | 
|---|
|  | 719 | tables = base; | 
|---|
| [0dd3a2f] | 720 | } | 
|---|
| [a08ba92] | 721 | } | 
|---|
| [17cd4eb] | 722 |  | 
|---|
| [a08ba92] | 723 | void Indexer::print( std::ostream &os, int indent ) const { | 
|---|
| [490ff5c3] | 724 | using std::cerr; | 
|---|
| [e8032b0] | 725 |  | 
|---|
| [0cb1d61] | 726 | if ( tables ) { | 
|---|
|  | 727 | os << "--- scope " << tables->scope << " ---" << std::endl; | 
|---|
|  | 728 |  | 
|---|
|  | 729 | os << "===idTable===" << std::endl; | 
|---|
|  | 730 | dump( tables->idTable, os ); | 
|---|
|  | 731 | os << "===typeTable===" << std::endl; | 
|---|
|  | 732 | dump( tables->typeTable, os ); | 
|---|
|  | 733 | os << "===structTable===" << std::endl; | 
|---|
|  | 734 | dump( tables->structTable, os ); | 
|---|
|  | 735 | os << "===enumTable===" << std::endl; | 
|---|
|  | 736 | dump( tables->enumTable, os ); | 
|---|
|  | 737 | os << "===unionTable===" << std::endl; | 
|---|
|  | 738 | dump( tables->unionTable, os ); | 
|---|
|  | 739 | os << "===contextTable===" << std::endl; | 
|---|
|  | 740 | dump( tables->traitTable, os ); | 
|---|
|  | 741 |  | 
|---|
|  | 742 | tables->base.print( os, indent ); | 
|---|
|  | 743 | } else { | 
|---|
|  | 744 | os << "--- end ---" << std::endl; | 
|---|
|  | 745 | } | 
|---|
| [743fbda] | 746 |  | 
|---|
| [a08ba92] | 747 | } | 
|---|
| [a40d503] | 748 |  | 
|---|
| [a181494] | 749 | Expression * Indexer::IdData::combine( ResolvExpr::Cost & cost ) const { | 
|---|
| [0ac366b] | 750 | Expression * ret = nullptr; | 
|---|
| [a40d503] | 751 | if ( baseExpr ) { | 
|---|
|  | 752 | Expression * base = baseExpr->clone(); | 
|---|
| [a181494] | 753 | ResolvExpr::referenceToRvalueConversion( base, cost ); | 
|---|
| [0ac366b] | 754 | ret = new MemberExpr( id, base ); | 
|---|
| [a40d503] | 755 | // xxx - this introduces hidden environments, for now remove them. | 
|---|
|  | 756 | // std::swap( base->env, ret->env ); | 
|---|
|  | 757 | delete base->env; | 
|---|
|  | 758 | base->env = nullptr; | 
|---|
|  | 759 | } else { | 
|---|
| [0ac366b] | 760 | ret = new VariableExpr( id ); | 
|---|
| [a40d503] | 761 | } | 
|---|
| [0ac366b] | 762 | if ( deleteStmt ) ret = new DeletedExpr( ret, deleteStmt ); | 
|---|
|  | 763 | return ret; | 
|---|
| [a40d503] | 764 | } | 
|---|
| [51b73452] | 765 | } // namespace SymTab | 
|---|
| [0dd3a2f] | 766 |  | 
|---|
|  | 767 | // Local Variables: // | 
|---|
|  | 768 | // tab-width: 4 // | 
|---|
|  | 769 | // mode: c++ // | 
|---|
|  | 770 | // compile-command: "make install" // | 
|---|
|  | 771 | // End: // | 
|---|