[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; } |
---|
[51b7345] | 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); |
---|
[0a75b77] | 128 | if ( isUserDefinedFunc && ! data.deleteStmt ) { |
---|
| 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; |
---|
| 168 | // only implicitly delete non-user defined functions that are not intrinsic, and are |
---|
| 169 | // not copy functions (assignment or copy constructor), unless a user-defined copy function exists. |
---|
| 170 | // deleteStmt will be non-null only if a user-defined function is found. |
---|
| 171 | if (isNotUserDefinedFunc && (! isCopyFunc || existsUserDefinedCopyFunc)) { |
---|
| 172 | ball.decl.deleteStmt = val.deleteStmt; |
---|
[1ba88a0] | 173 | } |
---|
[0a75b77] | 174 | out.push_back( ball.decl ); |
---|
[1ba88a0] | 175 | } |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
[e8032b0] | 179 | void Indexer::makeWritable() { |
---|
| 180 | if ( ! tables ) { |
---|
[52c2a72] | 181 | // create indexer if not yet set |
---|
| 182 | tables = new Indexer::Impl( scope ); |
---|
| 183 | } else if ( tables->refCount > 1 || tables->scope != scope ) { |
---|
| 184 | // make this indexer the base of a fresh indexer at the current scope |
---|
| 185 | tables = new Indexer::Impl( scope, std::move( *this ) ); |
---|
[e8032b0] | 186 | } |
---|
| 187 | } |
---|
| 188 | |
---|
[8b11840] | 189 | Indexer::Indexer() : tables( 0 ), scope( 0 ) {} |
---|
[e8032b0] | 190 | |
---|
[8b11840] | 191 | Indexer::Indexer( const Indexer &that ) : doDebug( that.doDebug ), tables( newRef( that.tables ) ), scope( that.scope ) {} |
---|
[e8032b0] | 192 | |
---|
[8b11840] | 193 | Indexer::Indexer( Indexer &&that ) : doDebug( that.doDebug ), tables( that.tables ), scope( that.scope ) { |
---|
[e8032b0] | 194 | that.tables = 0; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | Indexer::~Indexer() { |
---|
| 198 | deleteRef( tables ); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | Indexer& Indexer::operator= ( const Indexer &that ) { |
---|
| 202 | deleteRef( tables ); |
---|
| 203 | |
---|
| 204 | tables = newRef( that.tables ); |
---|
[52c2a72] | 205 | scope = that.scope; |
---|
[e8032b0] | 206 | doDebug = that.doDebug; |
---|
| 207 | |
---|
| 208 | return *this; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | Indexer& Indexer::operator= ( Indexer &&that ) { |
---|
| 212 | deleteRef( tables ); |
---|
| 213 | |
---|
| 214 | tables = that.tables; |
---|
[52c2a72] | 215 | scope = that.scope; |
---|
[e8032b0] | 216 | doDebug = that.doDebug; |
---|
[17cd4eb] | 217 | |
---|
[e8032b0] | 218 | that.tables = 0; |
---|
| 219 | |
---|
| 220 | return *this; |
---|
| 221 | } |
---|
[17cd4eb] | 222 | |
---|
[a40d503] | 223 | void Indexer::lookupId( const std::string &id, std::list< IdData > &out ) const { |
---|
[5447e09] | 224 | std::unordered_set< std::string > foundMangleNames; |
---|
[743fbda] | 225 | |
---|
[5447e09] | 226 | Indexer::Impl *searchTables = tables; |
---|
| 227 | while ( searchTables ) { |
---|
| 228 | |
---|
| 229 | IdTable::const_iterator decls = searchTables->idTable.find( id ); |
---|
| 230 | if ( decls != searchTables->idTable.end() ) { |
---|
| 231 | const MangleTable &mangleTable = decls->second; |
---|
| 232 | for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { |
---|
| 233 | // mark the mangled name as found, skipping this insertion if a declaration for that name has already been found |
---|
| 234 | if ( foundMangleNames.insert( decl->first ).second == false ) continue; |
---|
[743fbda] | 235 | |
---|
[5447e09] | 236 | out.push_back( decl->second ); |
---|
| 237 | } |
---|
[bed4c63e] | 238 | } |
---|
[743fbda] | 239 | |
---|
[5447e09] | 240 | // get declarations from base indexers |
---|
| 241 | searchTables = searchTables->base.tables; |
---|
[bed4c63e] | 242 | } |
---|
[1ba88a0] | 243 | |
---|
| 244 | // some special functions, e.g. constructors and destructors |
---|
| 245 | // remove autogenerated functions when they are defined so that |
---|
| 246 | // they can never be matched |
---|
| 247 | removeSpecialOverrides( id, out ); |
---|
[a08ba92] | 248 | } |
---|
[bdd516a] | 249 | |
---|
[a08ba92] | 250 | NamedTypeDecl *Indexer::lookupType( const std::string &id ) const { |
---|
[e8032b0] | 251 | if ( ! tables ) return 0; |
---|
| 252 | |
---|
[52c2a72] | 253 | TypeTable::const_iterator ret = tables->typeTable.find( id ); |
---|
| 254 | return ret != tables->typeTable.end() ? ret->second : tables->base.lookupType( id ); |
---|
[a08ba92] | 255 | } |
---|
[17cd4eb] | 256 | |
---|
[a08ba92] | 257 | StructDecl *Indexer::lookupStruct( const std::string &id ) const { |
---|
[e8032b0] | 258 | if ( ! tables ) return 0; |
---|
| 259 | |
---|
[52c2a72] | 260 | StructTable::const_iterator ret = tables->structTable.find( id ); |
---|
| 261 | return ret != tables->structTable.end() ? ret->second : tables->base.lookupStruct( id ); |
---|
[a08ba92] | 262 | } |
---|
[17cd4eb] | 263 | |
---|
[a08ba92] | 264 | EnumDecl *Indexer::lookupEnum( const std::string &id ) const { |
---|
[e8032b0] | 265 | if ( ! tables ) return 0; |
---|
| 266 | |
---|
[52c2a72] | 267 | EnumTable::const_iterator ret = tables->enumTable.find( id ); |
---|
| 268 | return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnum( id ); |
---|
[a08ba92] | 269 | } |
---|
[17cd4eb] | 270 | |
---|
[a08ba92] | 271 | UnionDecl *Indexer::lookupUnion( const std::string &id ) const { |
---|
[e8032b0] | 272 | if ( ! tables ) return 0; |
---|
| 273 | |
---|
[52c2a72] | 274 | UnionTable::const_iterator ret = tables->unionTable.find( id ); |
---|
| 275 | return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnion( id ); |
---|
[e8032b0] | 276 | } |
---|
| 277 | |
---|
| 278 | TraitDecl *Indexer::lookupTrait( const std::string &id ) const { |
---|
| 279 | if ( ! tables ) return 0; |
---|
| 280 | |
---|
[52c2a72] | 281 | TraitTable::const_iterator ret = tables->traitTable.find( id ); |
---|
| 282 | return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTrait( id ); |
---|
| 283 | } |
---|
| 284 | |
---|
[0ac366b] | 285 | const Indexer::IdData * Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) const { |
---|
| 286 | if ( ! tables ) return nullptr; |
---|
| 287 | if ( tables->scope < scope ) return nullptr; |
---|
[bed4c63e] | 288 | |
---|
| 289 | IdTable::const_iterator decls = tables->idTable.find( id ); |
---|
| 290 | if ( decls != tables->idTable.end() ) { |
---|
| 291 | const MangleTable &mangleTable = decls->second; |
---|
| 292 | MangleTable::const_iterator decl = mangleTable.find( mangleName ); |
---|
[0ac366b] | 293 | if ( decl != mangleTable.end() ) return &decl->second; |
---|
[bed4c63e] | 294 | } |
---|
| 295 | |
---|
| 296 | return tables->base.lookupIdAtScope( id, mangleName, scope ); |
---|
| 297 | } |
---|
| 298 | |
---|
[0ac366b] | 299 | Indexer::IdData * Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) { |
---|
| 300 | return const_cast<IdData *>(const_cast<const Indexer *>(this)->lookupIdAtScope( id, mangleName, scope )); |
---|
| 301 | } |
---|
| 302 | |
---|
[09d1ad0] | 303 | bool Indexer::hasIncompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const { |
---|
[bed4c63e] | 304 | if ( ! tables ) return false; |
---|
[09d1ad0] | 305 | if ( tables->scope < scope ) return false; |
---|
[bed4c63e] | 306 | |
---|
| 307 | IdTable::const_iterator decls = tables->idTable.find( id ); |
---|
| 308 | if ( decls != tables->idTable.end() ) { |
---|
| 309 | const MangleTable &mangleTable = decls->second; |
---|
| 310 | for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { |
---|
[4e06c1e] | 311 | // check for C decls with the same name, skipping those with a compatible type (by mangleName) |
---|
[a40d503] | 312 | if ( ! LinkageSpec::isMangled( decl->second.id->get_linkage() ) && decl->first != mangleName ) return true; |
---|
[bed4c63e] | 313 | } |
---|
| 314 | } |
---|
| 315 | |
---|
[09d1ad0] | 316 | return tables->base.hasIncompatibleCDecl( id, mangleName, scope ); |
---|
[bed4c63e] | 317 | } |
---|
[743fbda] | 318 | |
---|
[8884112] | 319 | bool Indexer::hasCompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const { |
---|
| 320 | if ( ! tables ) return false; |
---|
| 321 | if ( tables->scope < scope ) return false; |
---|
| 322 | |
---|
| 323 | IdTable::const_iterator decls = tables->idTable.find( id ); |
---|
| 324 | if ( decls != tables->idTable.end() ) { |
---|
| 325 | const MangleTable &mangleTable = decls->second; |
---|
| 326 | for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { |
---|
| 327 | // check for C decls with the same name, skipping |
---|
| 328 | // those with an incompatible type (by mangleName) |
---|
[a40d503] | 329 | if ( ! LinkageSpec::isMangled( decl->second.id->get_linkage() ) && decl->first == mangleName ) return true; |
---|
[8884112] | 330 | } |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | return tables->base.hasCompatibleCDecl( id, mangleName, scope ); |
---|
| 334 | } |
---|
| 335 | |
---|
[52c2a72] | 336 | NamedTypeDecl *Indexer::lookupTypeAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 337 | if ( ! tables ) return 0; |
---|
| 338 | if ( tables->scope < scope ) return 0; |
---|
| 339 | |
---|
| 340 | TypeTable::const_iterator ret = tables->typeTable.find( id ); |
---|
| 341 | return ret != tables->typeTable.end() ? ret->second : tables->base.lookupTypeAtScope( id, scope ); |
---|
| 342 | } |
---|
[743fbda] | 343 | |
---|
[52c2a72] | 344 | StructDecl *Indexer::lookupStructAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 345 | if ( ! tables ) return 0; |
---|
| 346 | if ( tables->scope < scope ) return 0; |
---|
| 347 | |
---|
| 348 | StructTable::const_iterator ret = tables->structTable.find( id ); |
---|
| 349 | return ret != tables->structTable.end() ? ret->second : tables->base.lookupStructAtScope( id, scope ); |
---|
| 350 | } |
---|
[743fbda] | 351 | |
---|
[52c2a72] | 352 | EnumDecl *Indexer::lookupEnumAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 353 | if ( ! tables ) return 0; |
---|
| 354 | if ( tables->scope < scope ) return 0; |
---|
| 355 | |
---|
| 356 | EnumTable::const_iterator ret = tables->enumTable.find( id ); |
---|
| 357 | return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnumAtScope( id, scope ); |
---|
| 358 | } |
---|
[743fbda] | 359 | |
---|
[52c2a72] | 360 | UnionDecl *Indexer::lookupUnionAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 361 | if ( ! tables ) return 0; |
---|
| 362 | if ( tables->scope < scope ) return 0; |
---|
| 363 | |
---|
| 364 | UnionTable::const_iterator ret = tables->unionTable.find( id ); |
---|
| 365 | return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnionAtScope( id, scope ); |
---|
| 366 | } |
---|
[743fbda] | 367 | |
---|
[52c2a72] | 368 | TraitDecl *Indexer::lookupTraitAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 369 | if ( ! tables ) return 0; |
---|
| 370 | if ( tables->scope < scope ) return 0; |
---|
| 371 | |
---|
| 372 | TraitTable::const_iterator ret = tables->traitTable.find( id ); |
---|
| 373 | return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTraitAtScope( id, scope ); |
---|
[a08ba92] | 374 | } |
---|
[17cd4eb] | 375 | |
---|
[3f024c9] | 376 | bool isFunction( DeclarationWithType * decl ) { |
---|
| 377 | return GenPoly::getFunctionType( decl->get_type() ); |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | bool isObject( DeclarationWithType * decl ) { |
---|
| 381 | return ! isFunction( decl ); |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | bool isDefinition( DeclarationWithType * decl ) { |
---|
| 385 | if ( FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl ) ) { |
---|
| 386 | // a function is a definition if it has a body |
---|
| 387 | return func->statements; |
---|
| 388 | } else { |
---|
| 389 | // an object is a definition if it is not marked extern. |
---|
| 390 | // both objects must be marked extern |
---|
| 391 | return ! decl->get_storageClasses().is_extern; |
---|
| 392 | } |
---|
| 393 | } |
---|
| 394 | |
---|
[0ac366b] | 395 | bool addedIdConflicts( Indexer::IdData & existing, DeclarationWithType *added, BaseSyntaxNode * deleteStmt, Indexer::ConflictFunction handleConflicts ) { |
---|
[bed4c63e] | 396 | // if we're giving the same name mangling to things of different types then there is something wrong |
---|
[3f024c9] | 397 | assert( (isObject( added ) && isObject( existing.id ) ) |
---|
| 398 | || ( isFunction( added ) && isFunction( existing.id ) ) ); |
---|
[bed4c63e] | 399 | |
---|
[0ac366b] | 400 | if ( LinkageSpec::isOverridable( existing.id->get_linkage() ) ) { |
---|
[bed4c63e] | 401 | // new definition shadows the autogenerated one, even at the same scope |
---|
| 402 | return false; |
---|
[0ac366b] | 403 | } else if ( LinkageSpec::isMangled( added->get_linkage() ) || ResolvExpr::typesCompatible( added->get_type(), existing.id->get_type(), Indexer() ) ) { |
---|
| 404 | |
---|
| 405 | // it is a conflict if one declaration is deleted and the other is not |
---|
| 406 | if ( deleteStmt && ! existing.deleteStmt ) { |
---|
| 407 | return handleConflicts( existing, "deletion of defined identifier " ); |
---|
| 408 | } else if ( ! deleteStmt && existing.deleteStmt ) { |
---|
| 409 | return handleConflicts( existing, "definition of deleted identifier " ); |
---|
| 410 | } |
---|
| 411 | |
---|
[3f024c9] | 412 | if ( isDefinition( added ) && isDefinition( existing.id ) ) { |
---|
| 413 | if ( isFunction( added ) ) { |
---|
[0ac366b] | 414 | return handleConflicts( existing, "duplicate function definition for " ); |
---|
[3f024c9] | 415 | } else { |
---|
[0ac366b] | 416 | return handleConflicts( existing, "duplicate object definition for " ); |
---|
[bed4c63e] | 417 | } // if |
---|
| 418 | } // if |
---|
| 419 | } else { |
---|
[0ac366b] | 420 | return handleConflicts( existing, "duplicate definition for " ); |
---|
[bed4c63e] | 421 | } // if |
---|
| 422 | |
---|
| 423 | return true; |
---|
| 424 | } |
---|
[743fbda] | 425 | |
---|
[0ac366b] | 426 | void Indexer::addId( DeclarationWithType *decl, ConflictFunction handleConflicts, Expression * baseExpr, BaseSyntaxNode * deleteStmt ) { |
---|
[2cb70aa] | 427 | if ( decl->name == "" ) return; |
---|
[33a25f9] | 428 | debugPrint( "Adding Id " << decl->name << std::endl ); |
---|
[e8032b0] | 429 | makeWritable(); |
---|
[bed4c63e] | 430 | |
---|
[6fc5c14] | 431 | const std::string &name = decl->name; |
---|
[bed4c63e] | 432 | std::string mangleName; |
---|
[6fc5c14] | 433 | if ( LinkageSpec::isOverridable( decl->linkage ) ) { |
---|
[bed4c63e] | 434 | // mangle the name without including the appropriate suffix, so overridable routines are placed into the |
---|
| 435 | // same "bucket" as their user defined versions. |
---|
| 436 | mangleName = Mangler::mangle( decl, false ); |
---|
| 437 | } else { |
---|
| 438 | mangleName = Mangler::mangle( decl ); |
---|
| 439 | } // if |
---|
| 440 | |
---|
[8884112] | 441 | // this ensures that no two declarations with the same unmangled name at the same scope both have C linkage |
---|
[6fc5c14] | 442 | if ( ! LinkageSpec::isMangled( decl->linkage ) ) { |
---|
[8884112] | 443 | // NOTE this is broken in Richard's original code in such a way that it never triggers (it |
---|
| 444 | // doesn't check decls that have the same manglename, and all C-linkage decls are defined to |
---|
| 445 | // have their name as their manglename, hence the error can never trigger). |
---|
| 446 | // The code here is closer to correct, but name mangling would have to be completely |
---|
| 447 | // isomorphic to C type-compatibility, which it may not be. |
---|
| 448 | if ( hasIncompatibleCDecl( name, mangleName, scope ) ) { |
---|
[a16764a6] | 449 | SemanticError( decl, "conflicting overload of C function " ); |
---|
[8884112] | 450 | } |
---|
| 451 | } else { |
---|
[490ff5c3] | 452 | // Check that a Cforall declaration doesn't override any C declaration |
---|
[8884112] | 453 | if ( hasCompatibleCDecl( name, mangleName, scope ) ) { |
---|
[a16764a6] | 454 | SemanticError( decl, "Cforall declaration hides C function " ); |
---|
[8884112] | 455 | } |
---|
[bed4c63e] | 456 | } |
---|
[8884112] | 457 | |
---|
| 458 | // Skip repeat declarations of the same identifier |
---|
[0ac366b] | 459 | IdData * existing = lookupIdAtScope( name, mangleName, scope ); |
---|
| 460 | if ( existing && existing->id && addedIdConflicts( *existing, decl, deleteStmt, handleConflicts ) ) return; |
---|
[8884112] | 461 | |
---|
| 462 | // add to indexer |
---|
[78d69da7] | 463 | tables->idTable[ name ][ mangleName ] = IdData{ decl, baseExpr, deleteStmt }; |
---|
[8884112] | 464 | ++tables->size; |
---|
[e8032b0] | 465 | } |
---|
[52c2a72] | 466 | |
---|
[0ac366b] | 467 | void Indexer::addId( DeclarationWithType * decl, Expression * baseExpr ) { |
---|
| 468 | // default handling of conflicts is to raise an error |
---|
[3ed994e] | 469 | addId( decl, [decl](IdData &, const std::string & msg) { SemanticError( decl, msg ); return true; }, baseExpr, decl->isDeleted ? decl : nullptr ); |
---|
[0ac366b] | 470 | } |
---|
| 471 | |
---|
| 472 | void Indexer::addDeletedId( DeclarationWithType * decl, BaseSyntaxNode * deleteStmt ) { |
---|
| 473 | // default handling of conflicts is to raise an error |
---|
[a16764a6] | 474 | addId( decl, [decl](IdData &, const std::string & msg) { SemanticError( decl, msg ); return true; }, nullptr, deleteStmt ); |
---|
[0ac366b] | 475 | } |
---|
| 476 | |
---|
[52c2a72] | 477 | bool addedTypeConflicts( NamedTypeDecl *existing, NamedTypeDecl *added ) { |
---|
| 478 | if ( existing->get_base() == 0 ) { |
---|
| 479 | return false; |
---|
| 480 | } else if ( added->get_base() == 0 ) { |
---|
| 481 | return true; |
---|
| 482 | } else { |
---|
[a16764a6] | 483 | SemanticError( added, "redeclaration of " ); |
---|
[52c2a72] | 484 | } |
---|
| 485 | } |
---|
[743fbda] | 486 | |
---|
[e8032b0] | 487 | void Indexer::addType( NamedTypeDecl *decl ) { |
---|
[33a25f9] | 488 | debugPrint( "Adding type " << decl->name << std::endl ); |
---|
[e8032b0] | 489 | makeWritable(); |
---|
[52c2a72] | 490 | |
---|
| 491 | const std::string &id = decl->get_name(); |
---|
| 492 | TypeTable::iterator existing = tables->typeTable.find( id ); |
---|
| 493 | if ( existing == tables->typeTable.end() ) { |
---|
| 494 | NamedTypeDecl *parent = tables->base.lookupTypeAtScope( id, scope ); |
---|
| 495 | if ( ! parent || ! addedTypeConflicts( parent, decl ) ) { |
---|
| 496 | tables->typeTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 497 | ++tables->size; |
---|
| 498 | } |
---|
| 499 | } else { |
---|
| 500 | if ( ! addedTypeConflicts( existing->second, decl ) ) { |
---|
| 501 | existing->second = decl; |
---|
| 502 | } |
---|
| 503 | } |
---|
| 504 | } |
---|
| 505 | |
---|
| 506 | bool addedDeclConflicts( AggregateDecl *existing, AggregateDecl *added ) { |
---|
[b2da0574] | 507 | if ( ! existing->body ) { |
---|
[52c2a72] | 508 | return false; |
---|
[b2da0574] | 509 | } else if ( added->body ) { |
---|
[a16764a6] | 510 | SemanticError( added, "redeclaration of " ); |
---|
[52c2a72] | 511 | } // if |
---|
| 512 | return true; |
---|
[e8032b0] | 513 | } |
---|
| 514 | |
---|
| 515 | void Indexer::addStruct( const std::string &id ) { |
---|
[33a25f9] | 516 | debugPrint( "Adding fwd decl for struct " << id << std::endl ); |
---|
[52c2a72] | 517 | addStruct( new StructDecl( id ) ); |
---|
[e8032b0] | 518 | } |
---|
[743fbda] | 519 | |
---|
[e8032b0] | 520 | void Indexer::addStruct( StructDecl *decl ) { |
---|
[33a25f9] | 521 | debugPrint( "Adding struct " << decl->name << std::endl ); |
---|
[e8032b0] | 522 | makeWritable(); |
---|
[52c2a72] | 523 | |
---|
| 524 | const std::string &id = decl->get_name(); |
---|
| 525 | StructTable::iterator existing = tables->structTable.find( id ); |
---|
| 526 | if ( existing == tables->structTable.end() ) { |
---|
| 527 | StructDecl *parent = tables->base.lookupStructAtScope( id, scope ); |
---|
| 528 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
| 529 | tables->structTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 530 | ++tables->size; |
---|
| 531 | } |
---|
| 532 | } else { |
---|
| 533 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
| 534 | existing->second = decl; |
---|
| 535 | } |
---|
| 536 | } |
---|
[e8032b0] | 537 | } |
---|
[743fbda] | 538 | |
---|
[e8032b0] | 539 | void Indexer::addEnum( EnumDecl *decl ) { |
---|
[33a25f9] | 540 | debugPrint( "Adding enum " << decl->name << std::endl ); |
---|
[e8032b0] | 541 | makeWritable(); |
---|
[52c2a72] | 542 | |
---|
| 543 | const std::string &id = decl->get_name(); |
---|
| 544 | EnumTable::iterator existing = tables->enumTable.find( id ); |
---|
| 545 | if ( existing == tables->enumTable.end() ) { |
---|
| 546 | EnumDecl *parent = tables->base.lookupEnumAtScope( id, scope ); |
---|
| 547 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
| 548 | tables->enumTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 549 | ++tables->size; |
---|
| 550 | } |
---|
| 551 | } else { |
---|
| 552 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
| 553 | existing->second = decl; |
---|
| 554 | } |
---|
| 555 | } |
---|
[e8032b0] | 556 | } |
---|
| 557 | |
---|
| 558 | void Indexer::addUnion( const std::string &id ) { |
---|
[33a25f9] | 559 | debugPrint( "Adding fwd decl for union " << id << std::endl ); |
---|
[52c2a72] | 560 | addUnion( new UnionDecl( id ) ); |
---|
[e8032b0] | 561 | } |
---|
[743fbda] | 562 | |
---|
[e8032b0] | 563 | void Indexer::addUnion( UnionDecl *decl ) { |
---|
[33a25f9] | 564 | debugPrint( "Adding union " << decl->name << std::endl ); |
---|
[e8032b0] | 565 | makeWritable(); |
---|
[52c2a72] | 566 | |
---|
| 567 | const std::string &id = decl->get_name(); |
---|
| 568 | UnionTable::iterator existing = tables->unionTable.find( id ); |
---|
| 569 | if ( existing == tables->unionTable.end() ) { |
---|
| 570 | UnionDecl *parent = tables->base.lookupUnionAtScope( id, scope ); |
---|
| 571 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
| 572 | tables->unionTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 573 | ++tables->size; |
---|
| 574 | } |
---|
| 575 | } else { |
---|
| 576 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
| 577 | existing->second = decl; |
---|
| 578 | } |
---|
| 579 | } |
---|
[e8032b0] | 580 | } |
---|
[743fbda] | 581 | |
---|
[e8032b0] | 582 | void Indexer::addTrait( TraitDecl *decl ) { |
---|
[33a25f9] | 583 | debugPrint( "Adding trait " << decl->name << std::endl ); |
---|
[e8032b0] | 584 | makeWritable(); |
---|
[52c2a72] | 585 | |
---|
| 586 | const std::string &id = decl->get_name(); |
---|
| 587 | TraitTable::iterator existing = tables->traitTable.find( id ); |
---|
| 588 | if ( existing == tables->traitTable.end() ) { |
---|
| 589 | TraitDecl *parent = tables->base.lookupTraitAtScope( id, scope ); |
---|
| 590 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
| 591 | tables->traitTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 592 | ++tables->size; |
---|
| 593 | } |
---|
| 594 | } else { |
---|
| 595 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
| 596 | existing->second = decl; |
---|
| 597 | } |
---|
| 598 | } |
---|
[a08ba92] | 599 | } |
---|
[17cd4eb] | 600 | |
---|
[0ac366b] | 601 | void Indexer::addMembers( AggregateDecl * aggr, Expression * expr, ConflictFunction handleConflicts ) { |
---|
[1485c1a] | 602 | for ( Declaration * decl : aggr->members ) { |
---|
| 603 | if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { |
---|
[0ac366b] | 604 | addId( dwt, handleConflicts, expr ); |
---|
[1485c1a] | 605 | if ( dwt->name == "" ) { |
---|
| 606 | Type * t = dwt->get_type()->stripReferences(); |
---|
| 607 | if ( dynamic_cast< StructInstType * >( t ) || dynamic_cast< UnionInstType * >( t ) ) { |
---|
| 608 | Expression * base = expr->clone(); |
---|
[a181494] | 609 | ResolvExpr::Cost cost = ResolvExpr::Cost::zero; // xxx - carry this cost into the indexer as a base cost? |
---|
| 610 | ResolvExpr::referenceToRvalueConversion( base, cost ); |
---|
[0ac366b] | 611 | addMembers( t->getAggr(), new MemberExpr( dwt, base ), handleConflicts ); |
---|
[1485c1a] | 612 | } |
---|
| 613 | } |
---|
| 614 | } |
---|
| 615 | } |
---|
| 616 | } |
---|
| 617 | |
---|
[0ac366b] | 618 | void Indexer::addWith( std::list< Expression * > & withExprs, BaseSyntaxNode * withStmt ) { |
---|
[4670c79] | 619 | for ( Expression * expr : withExprs ) { |
---|
[81644e0] | 620 | if ( expr->result ) { |
---|
[497282e] | 621 | AggregateDecl * aggr = expr->result->stripReferences()->getAggr(); |
---|
[81644e0] | 622 | assertf( aggr, "WithStmt expr has non-aggregate type: %s", toString( expr->result ).c_str() ); |
---|
| 623 | |
---|
[0ac366b] | 624 | addMembers( aggr, expr, [withStmt](IdData & existing, const std::string &) { |
---|
| 625 | // on conflict, delete the identifier |
---|
| 626 | existing.deleteStmt = withStmt; |
---|
| 627 | return true; |
---|
| 628 | }); |
---|
[81644e0] | 629 | } |
---|
| 630 | } |
---|
| 631 | } |
---|
| 632 | |
---|
[5fe35d6] | 633 | void Indexer::addIds( const std::list< DeclarationWithType * > & decls ) { |
---|
| 634 | for ( auto d : decls ) { |
---|
| 635 | addId( d ); |
---|
| 636 | } |
---|
| 637 | } |
---|
| 638 | |
---|
| 639 | void Indexer::addTypes( const std::list< TypeDecl * > & tds ) { |
---|
| 640 | for ( auto td : tds ) { |
---|
| 641 | addType( td ); |
---|
| 642 | addIds( td->assertions ); |
---|
| 643 | } |
---|
| 644 | } |
---|
| 645 | |
---|
| 646 | void Indexer::addFunctionType( FunctionType * ftype ) { |
---|
| 647 | addTypes( ftype->forall ); |
---|
| 648 | addIds( ftype->returnVals ); |
---|
| 649 | addIds( ftype->parameters ); |
---|
| 650 | } |
---|
| 651 | |
---|
[a08ba92] | 652 | void Indexer::enterScope() { |
---|
[52c2a72] | 653 | ++scope; |
---|
[743fbda] | 654 | |
---|
[0dd3a2f] | 655 | if ( doDebug ) { |
---|
[6137fbb] | 656 | std::cerr << "--- Entering scope " << scope << std::endl; |
---|
[0dd3a2f] | 657 | } |
---|
[a08ba92] | 658 | } |
---|
[17cd4eb] | 659 | |
---|
[a08ba92] | 660 | void Indexer::leaveScope() { |
---|
[6137fbb] | 661 | using std::cerr; |
---|
[52c2a72] | 662 | |
---|
| 663 | assert( scope > 0 && "cannot leave initial scope" ); |
---|
[6137fbb] | 664 | if ( doDebug ) { |
---|
| 665 | cerr << "--- Leaving scope " << scope << " containing" << std::endl; |
---|
| 666 | } |
---|
[52c2a72] | 667 | --scope; |
---|
| 668 | |
---|
| 669 | while ( tables && tables->scope > scope ) { |
---|
| 670 | if ( doDebug ) { |
---|
[6137fbb] | 671 | dump( tables->idTable, cerr ); |
---|
| 672 | dump( tables->typeTable, cerr ); |
---|
| 673 | dump( tables->structTable, cerr ); |
---|
| 674 | dump( tables->enumTable, cerr ); |
---|
| 675 | dump( tables->unionTable, cerr ); |
---|
| 676 | dump( tables->traitTable, cerr ); |
---|
[52c2a72] | 677 | } |
---|
| 678 | |
---|
| 679 | // swap tables for base table until we find one at an appropriate scope |
---|
| 680 | Indexer::Impl *base = newRef( tables->base.tables ); |
---|
| 681 | deleteRef( tables ); |
---|
| 682 | tables = base; |
---|
[0dd3a2f] | 683 | } |
---|
[a08ba92] | 684 | } |
---|
[17cd4eb] | 685 | |
---|
[a08ba92] | 686 | void Indexer::print( std::ostream &os, int indent ) const { |
---|
[490ff5c3] | 687 | using std::cerr; |
---|
[e8032b0] | 688 | |
---|
[0cb1d61] | 689 | if ( tables ) { |
---|
| 690 | os << "--- scope " << tables->scope << " ---" << std::endl; |
---|
| 691 | |
---|
| 692 | os << "===idTable===" << std::endl; |
---|
| 693 | dump( tables->idTable, os ); |
---|
| 694 | os << "===typeTable===" << std::endl; |
---|
| 695 | dump( tables->typeTable, os ); |
---|
| 696 | os << "===structTable===" << std::endl; |
---|
| 697 | dump( tables->structTable, os ); |
---|
| 698 | os << "===enumTable===" << std::endl; |
---|
| 699 | dump( tables->enumTable, os ); |
---|
| 700 | os << "===unionTable===" << std::endl; |
---|
| 701 | dump( tables->unionTable, os ); |
---|
| 702 | os << "===contextTable===" << std::endl; |
---|
| 703 | dump( tables->traitTable, os ); |
---|
| 704 | |
---|
| 705 | tables->base.print( os, indent ); |
---|
| 706 | } else { |
---|
| 707 | os << "--- end ---" << std::endl; |
---|
| 708 | } |
---|
[743fbda] | 709 | |
---|
[a08ba92] | 710 | } |
---|
[a40d503] | 711 | |
---|
[a181494] | 712 | Expression * Indexer::IdData::combine( ResolvExpr::Cost & cost ) const { |
---|
[0ac366b] | 713 | Expression * ret = nullptr; |
---|
[a40d503] | 714 | if ( baseExpr ) { |
---|
| 715 | Expression * base = baseExpr->clone(); |
---|
[a181494] | 716 | ResolvExpr::referenceToRvalueConversion( base, cost ); |
---|
[0ac366b] | 717 | ret = new MemberExpr( id, base ); |
---|
[a40d503] | 718 | // xxx - this introduces hidden environments, for now remove them. |
---|
| 719 | // std::swap( base->env, ret->env ); |
---|
| 720 | delete base->env; |
---|
| 721 | base->env = nullptr; |
---|
| 722 | } else { |
---|
[0ac366b] | 723 | ret = new VariableExpr( id ); |
---|
[a40d503] | 724 | } |
---|
[0ac366b] | 725 | if ( deleteStmt ) ret = new DeletedExpr( ret, deleteStmt ); |
---|
| 726 | return ret; |
---|
[a40d503] | 727 | } |
---|
[51b7345] | 728 | } // namespace SymTab |
---|
[0dd3a2f] | 729 | |
---|
| 730 | // Local Variables: // |
---|
| 731 | // tab-width: 4 // |
---|
| 732 | // mode: c++ // |
---|
| 733 | // compile-command: "make install" // |
---|
| 734 | // End: // |
---|