[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 |
---|
| 28 | #include "InitTweak/InitTweak.h" // for isConstructor, isCopyFunction, isC... |
---|
| 29 | #include "Mangler.h" // for Mangler |
---|
| 30 | #include "Parser/LinkageSpec.h" // for isMangled, isOverridable, Spec |
---|
| 31 | #include "ResolvExpr/typeops.h" // for typesCompatible |
---|
| 32 | #include "SynTree/Constant.h" // for Constant |
---|
| 33 | #include "SynTree/Declaration.h" // for DeclarationWithType, FunctionDecl |
---|
| 34 | #include "SynTree/Expression.h" // for Expression, ImplicitCopyCtorExpr |
---|
| 35 | #include "SynTree/Initializer.h" // for Initializer |
---|
| 36 | #include "SynTree/Statement.h" // for CompoundStmt, Statement, ForStmt (... |
---|
| 37 | #include "SynTree/Type.h" // for Type, StructInstType, UnionInstType |
---|
[1ba88a0] | 38 | |
---|
[522363e] | 39 | #define debugPrint(x) if ( doDebug ) { std::cerr << x; } |
---|
[51b7345] | 40 | |
---|
| 41 | namespace SymTab { |
---|
[a40d503] | 42 | std::ostream & operator<<( std::ostream & out, const Indexer::IdData & data ) { |
---|
| 43 | return out << "(" << data.id << "," << data.baseExpr << ")"; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | typedef std::unordered_map< std::string, Indexer::IdData > MangleTable; |
---|
[bed4c63e] | 47 | typedef std::unordered_map< std::string, MangleTable > IdTable; |
---|
[52c2a72] | 48 | typedef std::unordered_map< std::string, NamedTypeDecl* > TypeTable; |
---|
| 49 | typedef std::unordered_map< std::string, StructDecl* > StructTable; |
---|
| 50 | typedef std::unordered_map< std::string, EnumDecl* > EnumTable; |
---|
| 51 | typedef std::unordered_map< std::string, UnionDecl* > UnionTable; |
---|
| 52 | typedef std::unordered_map< std::string, TraitDecl* > TraitTable; |
---|
| 53 | |
---|
[bed4c63e] | 54 | void dump( const IdTable &table, std::ostream &os ) { |
---|
| 55 | for ( IdTable::const_iterator id = table.begin(); id != table.end(); ++id ) { |
---|
| 56 | for ( MangleTable::const_iterator mangle = id->second.begin(); mangle != id->second.end(); ++mangle ) { |
---|
| 57 | os << mangle->second << std::endl; |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | } |
---|
[743fbda] | 61 | |
---|
[52c2a72] | 62 | template< typename Decl > |
---|
| 63 | void dump( const std::unordered_map< std::string, Decl* > &table, std::ostream &os ) { |
---|
| 64 | for ( typename std::unordered_map< std::string, Decl* >::const_iterator it = table.begin(); it != table.end(); ++it ) { |
---|
| 65 | os << it->second << std::endl; |
---|
| 66 | } // for |
---|
| 67 | } |
---|
[743fbda] | 68 | |
---|
[e8032b0] | 69 | struct Indexer::Impl { |
---|
[52c2a72] | 70 | Impl( unsigned long _scope ) : refCount(1), scope( _scope ), size( 0 ), base(), |
---|
[e8032b0] | 71 | idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {} |
---|
[52c2a72] | 72 | Impl( unsigned long _scope, Indexer &&_base ) : refCount(1), scope( _scope ), size( 0 ), base( _base ), |
---|
[e8032b0] | 73 | idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {} |
---|
| 74 | unsigned long refCount; ///< Number of references to these tables |
---|
[52c2a72] | 75 | unsigned long scope; ///< Scope these tables are associated with |
---|
[e8032b0] | 76 | unsigned long size; ///< Number of elements stored in this table |
---|
| 77 | const Indexer base; ///< Base indexer this extends |
---|
[743fbda] | 78 | |
---|
[e8032b0] | 79 | IdTable idTable; ///< Identifier namespace |
---|
| 80 | TypeTable typeTable; ///< Type namespace |
---|
| 81 | StructTable structTable; ///< Struct namespace |
---|
| 82 | EnumTable enumTable; ///< Enum namespace |
---|
| 83 | UnionTable unionTable; ///< Union namespace |
---|
| 84 | TraitTable traitTable; ///< Trait namespace |
---|
| 85 | }; |
---|
| 86 | |
---|
| 87 | Indexer::Impl *Indexer::newRef( Indexer::Impl *toClone ) { |
---|
| 88 | if ( ! toClone ) return 0; |
---|
| 89 | |
---|
| 90 | // shorten the search chain by skipping empty links |
---|
| 91 | Indexer::Impl *ret = toClone->size == 0 ? toClone->base.tables : toClone; |
---|
| 92 | if ( ret ) { ++ret->refCount; } |
---|
| 93 | |
---|
| 94 | return ret; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void Indexer::deleteRef( Indexer::Impl *toFree ) { |
---|
| 98 | if ( ! toFree ) return; |
---|
| 99 | |
---|
| 100 | if ( --toFree->refCount == 0 ) delete toFree; |
---|
| 101 | } |
---|
| 102 | |
---|
[a40d503] | 103 | void Indexer::removeSpecialOverrides( const std::string &id, std::list< IdData > & out ) const { |
---|
[ee1635c8] | 104 | // only need to perform this step for constructors, destructors, and assignment functions |
---|
[bff227f] | 105 | if ( ! CodeGen::isCtorDtorAssign( id ) ) return; |
---|
[1ba88a0] | 106 | |
---|
| 107 | // helpful data structure |
---|
| 108 | struct ValueType { |
---|
| 109 | struct DeclBall { |
---|
[a40d503] | 110 | IdData decl; |
---|
[1ba88a0] | 111 | bool isUserDefinedFunc; // properties for this particular decl |
---|
[235114f] | 112 | bool isDefaultCtor; |
---|
| 113 | bool isDtor; |
---|
[1ba88a0] | 114 | bool isCopyFunc; |
---|
| 115 | }; |
---|
| 116 | // properties for this type |
---|
[ff03f5c] | 117 | bool existsUserDefinedFunc = false; // any user-defined function found |
---|
| 118 | bool existsUserDefinedCtor = false; // any user-defined constructor found |
---|
| 119 | bool existsUserDefinedDtor = false; // any user-defined destructor found |
---|
| 120 | bool existsUserDefinedCopyFunc = false; // user-defined copy ctor found |
---|
| 121 | bool existsUserDefinedDefaultCtor = false; // user-defined default ctor found |
---|
[1ba88a0] | 122 | std::list< DeclBall > decls; |
---|
| 123 | |
---|
| 124 | // another FunctionDecl for the current type was found - determine |
---|
| 125 | // if it has special properties and update data structure accordingly |
---|
[a40d503] | 126 | ValueType & operator+=( IdData data ) { |
---|
| 127 | DeclarationWithType * function = data.id; |
---|
[1ba88a0] | 128 | bool isUserDefinedFunc = ! LinkageSpec::isOverridable( function->get_linkage() ); |
---|
[235114f] | 129 | bool isDefaultCtor = InitTweak::isDefaultConstructor( function ); |
---|
| 130 | bool isDtor = InitTweak::isDestructor( function ); |
---|
[ee1635c8] | 131 | bool isCopyFunc = InitTweak::isCopyFunction( function, function->get_name() ); |
---|
[a40d503] | 132 | decls.push_back( DeclBall{ data, isUserDefinedFunc, isDefaultCtor, isDtor, isCopyFunc } ); |
---|
[ff03f5c] | 133 | existsUserDefinedFunc = existsUserDefinedFunc || isUserDefinedFunc; |
---|
[bff227f] | 134 | existsUserDefinedCtor = existsUserDefinedCtor || (isUserDefinedFunc && CodeGen::isConstructor( function->get_name() ) ); |
---|
[ff03f5c] | 135 | existsUserDefinedDtor = existsUserDefinedDtor || (isUserDefinedFunc && isDtor); |
---|
| 136 | existsUserDefinedCopyFunc = existsUserDefinedCopyFunc || (isUserDefinedFunc && isCopyFunc); |
---|
| 137 | existsUserDefinedDefaultCtor = existsUserDefinedDefaultCtor || (isUserDefinedFunc && isDefaultCtor); |
---|
[1ba88a0] | 138 | return *this; |
---|
| 139 | } |
---|
| 140 | }; // ValueType |
---|
| 141 | |
---|
[a40d503] | 142 | std::list< IdData > copy; |
---|
[1ba88a0] | 143 | copy.splice( copy.end(), out ); |
---|
| 144 | |
---|
| 145 | // organize discovered declarations by type |
---|
| 146 | std::unordered_map< std::string, ValueType > funcMap; |
---|
[a40d503] | 147 | for ( auto decl : copy ) { |
---|
| 148 | if ( FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl.id ) ) { |
---|
[8c49c0e] | 149 | std::list< DeclarationWithType * > & params = function->get_functionType()->get_parameters(); |
---|
[1ba88a0] | 150 | assert( ! params.empty() ); |
---|
[24670d2] | 151 | // use base type of pointer, so that qualifiers on the pointer type aren't considered. |
---|
[ce8c12f] | 152 | Type * base = InitTweak::getPointerBase( params.front()->get_type() ); |
---|
| 153 | assert( base ); |
---|
[a40d503] | 154 | funcMap[ Mangler::mangle( base ) ] += decl; |
---|
[1ba88a0] | 155 | } else { |
---|
| 156 | out.push_back( decl ); |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | |
---|
[ff03f5c] | 160 | // if a type contains user defined ctor/dtor/assign, then special rules trigger, which determine |
---|
| 161 | // the set of ctor/dtor/assign that are seen by the requester. In particular, if the user defines |
---|
[1ba88a0] | 162 | // a default ctor, then the generated default ctor should never be seen, likewise for copy ctor |
---|
| 163 | // and dtor. If the user defines any ctor/dtor, then no generated field ctors should be seen. |
---|
[ff03f5c] | 164 | // If the user defines any ctor then the generated default ctor should not be seen (intrinsic default |
---|
| 165 | // ctor must be overridden exactly). |
---|
[1ba88a0] | 166 | for ( std::pair< const std::string, ValueType > & pair : funcMap ) { |
---|
| 167 | ValueType & val = pair.second; |
---|
| 168 | for ( ValueType::DeclBall ball : val.decls ) { |
---|
[ff03f5c] | 169 | bool noUserDefinedFunc = ! val.existsUserDefinedFunc; |
---|
| 170 | bool isUserDefinedFunc = ball.isUserDefinedFunc; |
---|
[a40d503] | 171 | bool isAcceptableDefaultCtor = (! val.existsUserDefinedCtor || (! val.existsUserDefinedDefaultCtor && ball.decl.id->get_linkage() == LinkageSpec::Intrinsic)) && ball.isDefaultCtor; // allow default constructors only when no user-defined constructors exist, except in the case of intrinsics, which require exact overrides |
---|
[ff03f5c] | 172 | bool isAcceptableCopyFunc = ! val.existsUserDefinedCopyFunc && ball.isCopyFunc; // handles copy ctor and assignment operator |
---|
| 173 | bool isAcceptableDtor = ! val.existsUserDefinedDtor && ball.isDtor; |
---|
| 174 | if ( noUserDefinedFunc || isUserDefinedFunc || isAcceptableDefaultCtor || isAcceptableCopyFunc || isAcceptableDtor ) { |
---|
[1ba88a0] | 175 | // decl conforms to the rules described above, so it should be seen by the requester |
---|
| 176 | out.push_back( ball.decl ); |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | |
---|
[e8032b0] | 182 | void Indexer::makeWritable() { |
---|
| 183 | if ( ! tables ) { |
---|
[52c2a72] | 184 | // create indexer if not yet set |
---|
| 185 | tables = new Indexer::Impl( scope ); |
---|
| 186 | } else if ( tables->refCount > 1 || tables->scope != scope ) { |
---|
| 187 | // make this indexer the base of a fresh indexer at the current scope |
---|
| 188 | tables = new Indexer::Impl( scope, std::move( *this ) ); |
---|
[e8032b0] | 189 | } |
---|
| 190 | } |
---|
| 191 | |
---|
[8b11840] | 192 | Indexer::Indexer() : tables( 0 ), scope( 0 ) {} |
---|
[e8032b0] | 193 | |
---|
[8b11840] | 194 | Indexer::Indexer( const Indexer &that ) : doDebug( that.doDebug ), tables( newRef( that.tables ) ), scope( that.scope ) {} |
---|
[e8032b0] | 195 | |
---|
[8b11840] | 196 | Indexer::Indexer( Indexer &&that ) : doDebug( that.doDebug ), tables( that.tables ), scope( that.scope ) { |
---|
[e8032b0] | 197 | that.tables = 0; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | Indexer::~Indexer() { |
---|
| 201 | deleteRef( tables ); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | Indexer& Indexer::operator= ( const Indexer &that ) { |
---|
| 205 | deleteRef( tables ); |
---|
| 206 | |
---|
| 207 | tables = newRef( that.tables ); |
---|
[52c2a72] | 208 | scope = that.scope; |
---|
[e8032b0] | 209 | doDebug = that.doDebug; |
---|
| 210 | |
---|
| 211 | return *this; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | Indexer& Indexer::operator= ( Indexer &&that ) { |
---|
| 215 | deleteRef( tables ); |
---|
| 216 | |
---|
| 217 | tables = that.tables; |
---|
[52c2a72] | 218 | scope = that.scope; |
---|
[e8032b0] | 219 | doDebug = that.doDebug; |
---|
[17cd4eb] | 220 | |
---|
[e8032b0] | 221 | that.tables = 0; |
---|
| 222 | |
---|
| 223 | return *this; |
---|
| 224 | } |
---|
[17cd4eb] | 225 | |
---|
[a40d503] | 226 | void Indexer::lookupId( const std::string &id, std::list< IdData > &out ) const { |
---|
[5447e09] | 227 | std::unordered_set< std::string > foundMangleNames; |
---|
[743fbda] | 228 | |
---|
[5447e09] | 229 | Indexer::Impl *searchTables = tables; |
---|
| 230 | while ( searchTables ) { |
---|
| 231 | |
---|
| 232 | IdTable::const_iterator decls = searchTables->idTable.find( id ); |
---|
| 233 | if ( decls != searchTables->idTable.end() ) { |
---|
| 234 | const MangleTable &mangleTable = decls->second; |
---|
| 235 | for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { |
---|
| 236 | // mark the mangled name as found, skipping this insertion if a declaration for that name has already been found |
---|
| 237 | if ( foundMangleNames.insert( decl->first ).second == false ) continue; |
---|
[743fbda] | 238 | |
---|
[5447e09] | 239 | out.push_back( decl->second ); |
---|
| 240 | } |
---|
[bed4c63e] | 241 | } |
---|
[743fbda] | 242 | |
---|
[5447e09] | 243 | // get declarations from base indexers |
---|
| 244 | searchTables = searchTables->base.tables; |
---|
[bed4c63e] | 245 | } |
---|
[1ba88a0] | 246 | |
---|
| 247 | // some special functions, e.g. constructors and destructors |
---|
| 248 | // remove autogenerated functions when they are defined so that |
---|
| 249 | // they can never be matched |
---|
| 250 | removeSpecialOverrides( id, out ); |
---|
[a08ba92] | 251 | } |
---|
[bdd516a] | 252 | |
---|
[a08ba92] | 253 | NamedTypeDecl *Indexer::lookupType( const std::string &id ) const { |
---|
[e8032b0] | 254 | if ( ! tables ) return 0; |
---|
| 255 | |
---|
[52c2a72] | 256 | TypeTable::const_iterator ret = tables->typeTable.find( id ); |
---|
| 257 | return ret != tables->typeTable.end() ? ret->second : tables->base.lookupType( id ); |
---|
[a08ba92] | 258 | } |
---|
[17cd4eb] | 259 | |
---|
[a08ba92] | 260 | StructDecl *Indexer::lookupStruct( const std::string &id ) const { |
---|
[e8032b0] | 261 | if ( ! tables ) return 0; |
---|
| 262 | |
---|
[52c2a72] | 263 | StructTable::const_iterator ret = tables->structTable.find( id ); |
---|
| 264 | return ret != tables->structTable.end() ? ret->second : tables->base.lookupStruct( id ); |
---|
[a08ba92] | 265 | } |
---|
[17cd4eb] | 266 | |
---|
[a08ba92] | 267 | EnumDecl *Indexer::lookupEnum( const std::string &id ) const { |
---|
[e8032b0] | 268 | if ( ! tables ) return 0; |
---|
| 269 | |
---|
[52c2a72] | 270 | EnumTable::const_iterator ret = tables->enumTable.find( id ); |
---|
| 271 | return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnum( id ); |
---|
[a08ba92] | 272 | } |
---|
[17cd4eb] | 273 | |
---|
[a08ba92] | 274 | UnionDecl *Indexer::lookupUnion( const std::string &id ) const { |
---|
[e8032b0] | 275 | if ( ! tables ) return 0; |
---|
| 276 | |
---|
[52c2a72] | 277 | UnionTable::const_iterator ret = tables->unionTable.find( id ); |
---|
| 278 | return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnion( id ); |
---|
[e8032b0] | 279 | } |
---|
| 280 | |
---|
| 281 | TraitDecl *Indexer::lookupTrait( const std::string &id ) const { |
---|
| 282 | if ( ! tables ) return 0; |
---|
| 283 | |
---|
[52c2a72] | 284 | TraitTable::const_iterator ret = tables->traitTable.find( id ); |
---|
| 285 | return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTrait( id ); |
---|
| 286 | } |
---|
| 287 | |
---|
[bed4c63e] | 288 | DeclarationWithType *Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) const { |
---|
| 289 | if ( ! tables ) return 0; |
---|
| 290 | if ( tables->scope < scope ) return 0; |
---|
| 291 | |
---|
| 292 | IdTable::const_iterator decls = tables->idTable.find( id ); |
---|
| 293 | if ( decls != tables->idTable.end() ) { |
---|
| 294 | const MangleTable &mangleTable = decls->second; |
---|
| 295 | MangleTable::const_iterator decl = mangleTable.find( mangleName ); |
---|
[a40d503] | 296 | if ( decl != mangleTable.end() ) return decl->second.id; |
---|
[bed4c63e] | 297 | } |
---|
| 298 | |
---|
| 299 | return tables->base.lookupIdAtScope( id, mangleName, scope ); |
---|
| 300 | } |
---|
| 301 | |
---|
[09d1ad0] | 302 | bool Indexer::hasIncompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const { |
---|
[bed4c63e] | 303 | if ( ! tables ) return false; |
---|
[09d1ad0] | 304 | if ( tables->scope < scope ) return false; |
---|
[bed4c63e] | 305 | |
---|
| 306 | IdTable::const_iterator decls = tables->idTable.find( id ); |
---|
| 307 | if ( decls != tables->idTable.end() ) { |
---|
| 308 | const MangleTable &mangleTable = decls->second; |
---|
| 309 | for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { |
---|
[4e06c1e] | 310 | // check for C decls with the same name, skipping those with a compatible type (by mangleName) |
---|
[a40d503] | 311 | if ( ! LinkageSpec::isMangled( decl->second.id->get_linkage() ) && decl->first != mangleName ) return true; |
---|
[bed4c63e] | 312 | } |
---|
| 313 | } |
---|
| 314 | |
---|
[09d1ad0] | 315 | return tables->base.hasIncompatibleCDecl( id, mangleName, scope ); |
---|
[bed4c63e] | 316 | } |
---|
[743fbda] | 317 | |
---|
[8884112] | 318 | bool Indexer::hasCompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const { |
---|
| 319 | if ( ! tables ) return false; |
---|
| 320 | if ( tables->scope < scope ) return false; |
---|
| 321 | |
---|
| 322 | IdTable::const_iterator decls = tables->idTable.find( id ); |
---|
| 323 | if ( decls != tables->idTable.end() ) { |
---|
| 324 | const MangleTable &mangleTable = decls->second; |
---|
| 325 | for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { |
---|
| 326 | // check for C decls with the same name, skipping |
---|
| 327 | // those with an incompatible type (by mangleName) |
---|
[a40d503] | 328 | if ( ! LinkageSpec::isMangled( decl->second.id->get_linkage() ) && decl->first == mangleName ) return true; |
---|
[8884112] | 329 | } |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | return tables->base.hasCompatibleCDecl( id, mangleName, scope ); |
---|
| 333 | } |
---|
| 334 | |
---|
[52c2a72] | 335 | NamedTypeDecl *Indexer::lookupTypeAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 336 | if ( ! tables ) return 0; |
---|
| 337 | if ( tables->scope < scope ) return 0; |
---|
| 338 | |
---|
| 339 | TypeTable::const_iterator ret = tables->typeTable.find( id ); |
---|
| 340 | return ret != tables->typeTable.end() ? ret->second : tables->base.lookupTypeAtScope( id, scope ); |
---|
| 341 | } |
---|
[743fbda] | 342 | |
---|
[52c2a72] | 343 | StructDecl *Indexer::lookupStructAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 344 | if ( ! tables ) return 0; |
---|
| 345 | if ( tables->scope < scope ) return 0; |
---|
| 346 | |
---|
| 347 | StructTable::const_iterator ret = tables->structTable.find( id ); |
---|
| 348 | return ret != tables->structTable.end() ? ret->second : tables->base.lookupStructAtScope( id, scope ); |
---|
| 349 | } |
---|
[743fbda] | 350 | |
---|
[52c2a72] | 351 | EnumDecl *Indexer::lookupEnumAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 352 | if ( ! tables ) return 0; |
---|
| 353 | if ( tables->scope < scope ) return 0; |
---|
| 354 | |
---|
| 355 | EnumTable::const_iterator ret = tables->enumTable.find( id ); |
---|
| 356 | return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnumAtScope( id, scope ); |
---|
| 357 | } |
---|
[743fbda] | 358 | |
---|
[52c2a72] | 359 | UnionDecl *Indexer::lookupUnionAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 360 | if ( ! tables ) return 0; |
---|
| 361 | if ( tables->scope < scope ) return 0; |
---|
| 362 | |
---|
| 363 | UnionTable::const_iterator ret = tables->unionTable.find( id ); |
---|
| 364 | return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnionAtScope( id, scope ); |
---|
| 365 | } |
---|
[743fbda] | 366 | |
---|
[52c2a72] | 367 | TraitDecl *Indexer::lookupTraitAtScope( const std::string &id, unsigned long scope ) const { |
---|
| 368 | if ( ! tables ) return 0; |
---|
| 369 | if ( tables->scope < scope ) return 0; |
---|
| 370 | |
---|
| 371 | TraitTable::const_iterator ret = tables->traitTable.find( id ); |
---|
| 372 | return ret != tables->traitTable.end() ? ret->second : tables->base.lookupTraitAtScope( id, scope ); |
---|
[a08ba92] | 373 | } |
---|
[17cd4eb] | 374 | |
---|
[bed4c63e] | 375 | bool addedIdConflicts( DeclarationWithType *existing, DeclarationWithType *added ) { |
---|
| 376 | // if we're giving the same name mangling to things of different types then there is something wrong |
---|
| 377 | assert( (dynamic_cast<ObjectDecl*>( added ) && dynamic_cast<ObjectDecl*>( existing ) ) |
---|
| 378 | || (dynamic_cast<FunctionDecl*>( added ) && dynamic_cast<FunctionDecl*>( existing ) ) ); |
---|
| 379 | |
---|
| 380 | if ( LinkageSpec::isOverridable( existing->get_linkage() ) ) { |
---|
| 381 | // new definition shadows the autogenerated one, even at the same scope |
---|
| 382 | return false; |
---|
[307a732] | 383 | } else if ( LinkageSpec::isMangled( added->get_linkage() ) || ResolvExpr::typesCompatible( added->get_type(), existing->get_type(), Indexer() ) ) { |
---|
[bed4c63e] | 384 | // typesCompatible doesn't really do the right thing here. When checking compatibility of function types, |
---|
| 385 | // we should ignore outermost pointer qualifiers, except _Atomic? |
---|
| 386 | FunctionDecl *newentry = dynamic_cast< FunctionDecl* >( added ); |
---|
| 387 | FunctionDecl *oldentry = dynamic_cast< FunctionDecl* >( existing ); |
---|
| 388 | if ( newentry && oldentry ) { |
---|
| 389 | if ( newentry->get_statements() && oldentry->get_statements() ) { |
---|
| 390 | throw SemanticError( "duplicate function definition for ", added ); |
---|
| 391 | } // if |
---|
| 392 | } else { |
---|
| 393 | // two objects with the same mangled name defined in the same scope. |
---|
| 394 | // both objects must be marked extern or both must be intrinsic for this to be okay |
---|
| 395 | // xxx - perhaps it's actually if either is intrinsic then this is okay? |
---|
| 396 | // might also need to be same storage class? |
---|
| 397 | ObjectDecl *newobj = dynamic_cast< ObjectDecl* >( added ); |
---|
| 398 | ObjectDecl *oldobj = dynamic_cast< ObjectDecl* >( existing ); |
---|
[08d5507b] | 399 | if ( ! newobj->get_storageClasses().is_extern && ! oldobj->get_storageClasses().is_extern ) { |
---|
[bed4c63e] | 400 | throw SemanticError( "duplicate object definition for ", added ); |
---|
| 401 | } // if |
---|
| 402 | } // if |
---|
| 403 | } else { |
---|
| 404 | throw SemanticError( "duplicate definition for ", added ); |
---|
| 405 | } // if |
---|
| 406 | |
---|
| 407 | return true; |
---|
| 408 | } |
---|
[743fbda] | 409 | |
---|
[a40d503] | 410 | void Indexer::addId( DeclarationWithType *decl, Expression * baseExpr ) { |
---|
[33a25f9] | 411 | debugPrint( "Adding Id " << decl->name << std::endl ); |
---|
[e8032b0] | 412 | makeWritable(); |
---|
[bed4c63e] | 413 | |
---|
[6fc5c14] | 414 | const std::string &name = decl->name; |
---|
[bed4c63e] | 415 | std::string mangleName; |
---|
[6fc5c14] | 416 | if ( LinkageSpec::isOverridable( decl->linkage ) ) { |
---|
[bed4c63e] | 417 | // mangle the name without including the appropriate suffix, so overridable routines are placed into the |
---|
| 418 | // same "bucket" as their user defined versions. |
---|
| 419 | mangleName = Mangler::mangle( decl, false ); |
---|
| 420 | } else { |
---|
| 421 | mangleName = Mangler::mangle( decl ); |
---|
| 422 | } // if |
---|
| 423 | |
---|
[8884112] | 424 | // this ensures that no two declarations with the same unmangled name at the same scope both have C linkage |
---|
[6fc5c14] | 425 | if ( ! LinkageSpec::isMangled( decl->linkage ) ) { |
---|
[8884112] | 426 | // NOTE this is broken in Richard's original code in such a way that it never triggers (it |
---|
| 427 | // doesn't check decls that have the same manglename, and all C-linkage decls are defined to |
---|
| 428 | // have their name as their manglename, hence the error can never trigger). |
---|
| 429 | // The code here is closer to correct, but name mangling would have to be completely |
---|
| 430 | // isomorphic to C type-compatibility, which it may not be. |
---|
| 431 | if ( hasIncompatibleCDecl( name, mangleName, scope ) ) { |
---|
| 432 | throw SemanticError( "conflicting overload of C function ", decl ); |
---|
| 433 | } |
---|
| 434 | } else { |
---|
| 435 | // Check that a Cforall declaration doesn't overload any C declaration |
---|
| 436 | if ( hasCompatibleCDecl( name, mangleName, scope ) ) { |
---|
| 437 | throw SemanticError( "Cforall declaration hides C function ", decl ); |
---|
| 438 | } |
---|
[bed4c63e] | 439 | } |
---|
[8884112] | 440 | |
---|
| 441 | // Skip repeat declarations of the same identifier |
---|
| 442 | DeclarationWithType *existing = lookupIdAtScope( name, mangleName, scope ); |
---|
| 443 | if ( existing && addedIdConflicts( existing, decl ) ) return; |
---|
| 444 | |
---|
| 445 | // add to indexer |
---|
[a40d503] | 446 | tables->idTable[ name ][ mangleName ] = { decl, baseExpr }; |
---|
[8884112] | 447 | ++tables->size; |
---|
[e8032b0] | 448 | } |
---|
[52c2a72] | 449 | |
---|
| 450 | bool addedTypeConflicts( NamedTypeDecl *existing, NamedTypeDecl *added ) { |
---|
| 451 | if ( existing->get_base() == 0 ) { |
---|
| 452 | return false; |
---|
| 453 | } else if ( added->get_base() == 0 ) { |
---|
| 454 | return true; |
---|
| 455 | } else { |
---|
| 456 | throw SemanticError( "redeclaration of ", added ); |
---|
| 457 | } |
---|
| 458 | } |
---|
[743fbda] | 459 | |
---|
[e8032b0] | 460 | void Indexer::addType( NamedTypeDecl *decl ) { |
---|
[33a25f9] | 461 | debugPrint( "Adding type " << decl->name << std::endl ); |
---|
[e8032b0] | 462 | makeWritable(); |
---|
[52c2a72] | 463 | |
---|
| 464 | const std::string &id = decl->get_name(); |
---|
| 465 | TypeTable::iterator existing = tables->typeTable.find( id ); |
---|
| 466 | if ( existing == tables->typeTable.end() ) { |
---|
| 467 | NamedTypeDecl *parent = tables->base.lookupTypeAtScope( id, scope ); |
---|
| 468 | if ( ! parent || ! addedTypeConflicts( parent, decl ) ) { |
---|
| 469 | tables->typeTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 470 | ++tables->size; |
---|
| 471 | } |
---|
| 472 | } else { |
---|
| 473 | if ( ! addedTypeConflicts( existing->second, decl ) ) { |
---|
| 474 | existing->second = decl; |
---|
| 475 | } |
---|
| 476 | } |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | bool addedDeclConflicts( AggregateDecl *existing, AggregateDecl *added ) { |
---|
| 480 | if ( existing->get_members().empty() ) { |
---|
| 481 | return false; |
---|
| 482 | } else if ( ! added->get_members().empty() ) { |
---|
| 483 | throw SemanticError( "redeclaration of ", added ); |
---|
| 484 | } // if |
---|
| 485 | return true; |
---|
[e8032b0] | 486 | } |
---|
| 487 | |
---|
| 488 | void Indexer::addStruct( const std::string &id ) { |
---|
[33a25f9] | 489 | debugPrint( "Adding fwd decl for struct " << id << std::endl ); |
---|
[52c2a72] | 490 | addStruct( new StructDecl( id ) ); |
---|
[e8032b0] | 491 | } |
---|
[743fbda] | 492 | |
---|
[e8032b0] | 493 | void Indexer::addStruct( StructDecl *decl ) { |
---|
[33a25f9] | 494 | debugPrint( "Adding struct " << decl->name << std::endl ); |
---|
[e8032b0] | 495 | makeWritable(); |
---|
[52c2a72] | 496 | |
---|
| 497 | const std::string &id = decl->get_name(); |
---|
| 498 | StructTable::iterator existing = tables->structTable.find( id ); |
---|
| 499 | if ( existing == tables->structTable.end() ) { |
---|
| 500 | StructDecl *parent = tables->base.lookupStructAtScope( id, scope ); |
---|
| 501 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
| 502 | tables->structTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 503 | ++tables->size; |
---|
| 504 | } |
---|
| 505 | } else { |
---|
| 506 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
| 507 | existing->second = decl; |
---|
| 508 | } |
---|
| 509 | } |
---|
[e8032b0] | 510 | } |
---|
[743fbda] | 511 | |
---|
[e8032b0] | 512 | void Indexer::addEnum( EnumDecl *decl ) { |
---|
[33a25f9] | 513 | debugPrint( "Adding enum " << decl->name << std::endl ); |
---|
[e8032b0] | 514 | makeWritable(); |
---|
[52c2a72] | 515 | |
---|
| 516 | const std::string &id = decl->get_name(); |
---|
| 517 | EnumTable::iterator existing = tables->enumTable.find( id ); |
---|
| 518 | if ( existing == tables->enumTable.end() ) { |
---|
| 519 | EnumDecl *parent = tables->base.lookupEnumAtScope( id, scope ); |
---|
| 520 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
| 521 | tables->enumTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 522 | ++tables->size; |
---|
| 523 | } |
---|
| 524 | } else { |
---|
| 525 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
| 526 | existing->second = decl; |
---|
| 527 | } |
---|
| 528 | } |
---|
[e8032b0] | 529 | } |
---|
| 530 | |
---|
| 531 | void Indexer::addUnion( const std::string &id ) { |
---|
[33a25f9] | 532 | debugPrint( "Adding fwd decl for union " << id << std::endl ); |
---|
[52c2a72] | 533 | addUnion( new UnionDecl( id ) ); |
---|
[e8032b0] | 534 | } |
---|
[743fbda] | 535 | |
---|
[e8032b0] | 536 | void Indexer::addUnion( UnionDecl *decl ) { |
---|
[33a25f9] | 537 | debugPrint( "Adding union " << decl->name << std::endl ); |
---|
[e8032b0] | 538 | makeWritable(); |
---|
[52c2a72] | 539 | |
---|
| 540 | const std::string &id = decl->get_name(); |
---|
| 541 | UnionTable::iterator existing = tables->unionTable.find( id ); |
---|
| 542 | if ( existing == tables->unionTable.end() ) { |
---|
| 543 | UnionDecl *parent = tables->base.lookupUnionAtScope( id, scope ); |
---|
| 544 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
| 545 | tables->unionTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 546 | ++tables->size; |
---|
| 547 | } |
---|
| 548 | } else { |
---|
| 549 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
| 550 | existing->second = decl; |
---|
| 551 | } |
---|
| 552 | } |
---|
[e8032b0] | 553 | } |
---|
[743fbda] | 554 | |
---|
[e8032b0] | 555 | void Indexer::addTrait( TraitDecl *decl ) { |
---|
[33a25f9] | 556 | debugPrint( "Adding trait " << decl->name << std::endl ); |
---|
[e8032b0] | 557 | makeWritable(); |
---|
[52c2a72] | 558 | |
---|
| 559 | const std::string &id = decl->get_name(); |
---|
| 560 | TraitTable::iterator existing = tables->traitTable.find( id ); |
---|
| 561 | if ( existing == tables->traitTable.end() ) { |
---|
| 562 | TraitDecl *parent = tables->base.lookupTraitAtScope( id, scope ); |
---|
| 563 | if ( ! parent || ! addedDeclConflicts( parent, decl ) ) { |
---|
| 564 | tables->traitTable.insert( existing, std::make_pair( id, decl ) ); |
---|
| 565 | ++tables->size; |
---|
| 566 | } |
---|
| 567 | } else { |
---|
| 568 | if ( ! addedDeclConflicts( existing->second, decl ) ) { |
---|
| 569 | existing->second = decl; |
---|
| 570 | } |
---|
| 571 | } |
---|
[a08ba92] | 572 | } |
---|
[17cd4eb] | 573 | |
---|
[81644e0] | 574 | void Indexer::addWith( WithStmt * stmt ) { |
---|
| 575 | for ( Expression * expr : stmt->exprs ) { |
---|
| 576 | if ( expr->result ) { |
---|
[497282e] | 577 | AggregateDecl * aggr = expr->result->stripReferences()->getAggr(); |
---|
[81644e0] | 578 | assertf( aggr, "WithStmt expr has non-aggregate type: %s", toString( expr->result ).c_str() ); |
---|
| 579 | |
---|
| 580 | for ( Declaration * decl : aggr->members ) { |
---|
| 581 | if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { |
---|
[a40d503] | 582 | addId( dwt, expr ); |
---|
[81644e0] | 583 | } |
---|
| 584 | } |
---|
| 585 | } |
---|
| 586 | } |
---|
| 587 | } |
---|
| 588 | |
---|
[5fe35d6] | 589 | void Indexer::addIds( const std::list< DeclarationWithType * > & decls ) { |
---|
| 590 | for ( auto d : decls ) { |
---|
| 591 | addId( d ); |
---|
| 592 | } |
---|
| 593 | } |
---|
| 594 | |
---|
| 595 | void Indexer::addTypes( const std::list< TypeDecl * > & tds ) { |
---|
| 596 | for ( auto td : tds ) { |
---|
| 597 | addType( td ); |
---|
| 598 | addIds( td->assertions ); |
---|
| 599 | } |
---|
| 600 | } |
---|
| 601 | |
---|
| 602 | void Indexer::addFunctionType( FunctionType * ftype ) { |
---|
| 603 | addTypes( ftype->forall ); |
---|
| 604 | addIds( ftype->returnVals ); |
---|
| 605 | addIds( ftype->parameters ); |
---|
| 606 | } |
---|
| 607 | |
---|
[a08ba92] | 608 | void Indexer::enterScope() { |
---|
[52c2a72] | 609 | ++scope; |
---|
[743fbda] | 610 | |
---|
[0dd3a2f] | 611 | if ( doDebug ) { |
---|
[6137fbb] | 612 | std::cerr << "--- Entering scope " << scope << std::endl; |
---|
[0dd3a2f] | 613 | } |
---|
[a08ba92] | 614 | } |
---|
[17cd4eb] | 615 | |
---|
[a08ba92] | 616 | void Indexer::leaveScope() { |
---|
[6137fbb] | 617 | using std::cerr; |
---|
[52c2a72] | 618 | |
---|
| 619 | assert( scope > 0 && "cannot leave initial scope" ); |
---|
[6137fbb] | 620 | if ( doDebug ) { |
---|
| 621 | cerr << "--- Leaving scope " << scope << " containing" << std::endl; |
---|
| 622 | } |
---|
[52c2a72] | 623 | --scope; |
---|
| 624 | |
---|
| 625 | while ( tables && tables->scope > scope ) { |
---|
| 626 | if ( doDebug ) { |
---|
[6137fbb] | 627 | dump( tables->idTable, cerr ); |
---|
| 628 | dump( tables->typeTable, cerr ); |
---|
| 629 | dump( tables->structTable, cerr ); |
---|
| 630 | dump( tables->enumTable, cerr ); |
---|
| 631 | dump( tables->unionTable, cerr ); |
---|
| 632 | dump( tables->traitTable, cerr ); |
---|
[52c2a72] | 633 | } |
---|
| 634 | |
---|
| 635 | // swap tables for base table until we find one at an appropriate scope |
---|
| 636 | Indexer::Impl *base = newRef( tables->base.tables ); |
---|
| 637 | deleteRef( tables ); |
---|
| 638 | tables = base; |
---|
[0dd3a2f] | 639 | } |
---|
[a08ba92] | 640 | } |
---|
[17cd4eb] | 641 | |
---|
[a08ba92] | 642 | void Indexer::print( std::ostream &os, int indent ) const { |
---|
| 643 | using std::cerr; |
---|
[e8032b0] | 644 | |
---|
[0cb1d61] | 645 | if ( tables ) { |
---|
| 646 | os << "--- scope " << tables->scope << " ---" << std::endl; |
---|
| 647 | |
---|
| 648 | os << "===idTable===" << std::endl; |
---|
| 649 | dump( tables->idTable, os ); |
---|
| 650 | os << "===typeTable===" << std::endl; |
---|
| 651 | dump( tables->typeTable, os ); |
---|
| 652 | os << "===structTable===" << std::endl; |
---|
| 653 | dump( tables->structTable, os ); |
---|
| 654 | os << "===enumTable===" << std::endl; |
---|
| 655 | dump( tables->enumTable, os ); |
---|
| 656 | os << "===unionTable===" << std::endl; |
---|
| 657 | dump( tables->unionTable, os ); |
---|
| 658 | os << "===contextTable===" << std::endl; |
---|
| 659 | dump( tables->traitTable, os ); |
---|
| 660 | |
---|
| 661 | tables->base.print( os, indent ); |
---|
| 662 | } else { |
---|
| 663 | os << "--- end ---" << std::endl; |
---|
| 664 | } |
---|
[743fbda] | 665 | |
---|
[a08ba92] | 666 | } |
---|
[a40d503] | 667 | |
---|
| 668 | Expression * Indexer::IdData::combine() const { |
---|
| 669 | if ( baseExpr ) { |
---|
| 670 | Expression * base = baseExpr->clone(); |
---|
[ac57659] | 671 | ResolvExpr::referenceToRvalueConversion( base ); |
---|
[a40d503] | 672 | Expression * ret = new MemberExpr( id, base ); |
---|
| 673 | // xxx - this introduces hidden environments, for now remove them. |
---|
| 674 | // std::swap( base->env, ret->env ); |
---|
| 675 | delete base->env; |
---|
| 676 | base->env = nullptr; |
---|
| 677 | return ret; |
---|
| 678 | } else { |
---|
| 679 | return new VariableExpr( id ); |
---|
| 680 | } |
---|
| 681 | } |
---|
[51b7345] | 682 | } // namespace SymTab |
---|
[0dd3a2f] | 683 | |
---|
| 684 | // Local Variables: // |
---|
| 685 | // tab-width: 4 // |
---|
| 686 | // mode: c++ // |
---|
| 687 | // compile-command: "make install" // |
---|
| 688 | // End: // |
---|