[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 |
---|
[07de76b] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Fri Dec 13 23:43:19 2019 |
---|
| 13 | // Update Count : 22 |
---|
[0dd3a2f] | 14 | // |
---|
| 15 | |
---|
[e8032b0] | 16 | #include "Indexer.h" |
---|
| 17 | |
---|
[e3e16bc] | 18 | #include <cassert> // for assert, strict_dynamic_cast |
---|
[30f9072] | 19 | #include <string> // for string, operator<<, operator!= |
---|
[b8665e3] | 20 | #include <memory> // for shared_ptr, make_shared |
---|
[30f9072] | 21 | #include <unordered_map> // for operator!=, unordered_map<>::const... |
---|
| 22 | #include <unordered_set> // for unordered_set |
---|
| 23 | #include <utility> // for pair, make_pair, move |
---|
[42f1279c] | 24 | #include <vector> // for vector |
---|
[30f9072] | 25 | |
---|
[9236060] | 26 | #include "CodeGen/OperatorTable.h" // for isCtorDtor, isCtorDtorAssign |
---|
[30f9072] | 27 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 28 | #include "Common/utility.h" // for cloneAll |
---|
[b8665e3] | 29 | #include "Common/Stats/Counter.h" // for counters |
---|
| 30 | #include "GenPoly/GenPoly.h" // for getFunctionType |
---|
[30f9072] | 31 | #include "InitTweak/InitTweak.h" // for isConstructor, isCopyFunction, isC... |
---|
| 32 | #include "Mangler.h" // for Mangler |
---|
[fed6a0f] | 33 | #include "ResolvExpr/AlternativeFinder.h" // for referenceToRvalueConversion |
---|
[e563edf] | 34 | #include "ResolvExpr/Unify.h" // for typesCompatible |
---|
[07de76b] | 35 | #include "SynTree/LinkageSpec.h" // for isMangled, isOverridable, Spec |
---|
[30f9072] | 36 | #include "SynTree/Constant.h" // for Constant |
---|
| 37 | #include "SynTree/Declaration.h" // for DeclarationWithType, FunctionDecl |
---|
| 38 | #include "SynTree/Expression.h" // for Expression, ImplicitCopyCtorExpr |
---|
| 39 | #include "SynTree/Initializer.h" // for Initializer |
---|
| 40 | #include "SynTree/Statement.h" // for CompoundStmt, Statement, ForStmt (... |
---|
| 41 | #include "SynTree/Type.h" // for Type, StructInstType, UnionInstType |
---|
[1ba88a0] | 42 | |
---|
[51b7345] | 43 | namespace SymTab { |
---|
[79de2210] | 44 | |
---|
| 45 | // Statistics block |
---|
[b419abb] | 46 | namespace { |
---|
| 47 | static inline auto stats() { |
---|
| 48 | using namespace Stats::Counters; |
---|
| 49 | static auto group = build<CounterGroup>("Indexers"); |
---|
| 50 | static struct { |
---|
| 51 | SimpleCounter * count; |
---|
| 52 | AverageCounter<double> * size; |
---|
| 53 | SimpleCounter * new_scopes; |
---|
| 54 | SimpleCounter * lazy_scopes; |
---|
| 55 | AverageCounter<double> * avg_scope_depth; |
---|
| 56 | MaxCounter<size_t> * max_scope_depth; |
---|
| 57 | SimpleCounter * add_calls; |
---|
| 58 | SimpleCounter * lookup_calls; |
---|
| 59 | SimpleCounter * map_lookups; |
---|
| 60 | SimpleCounter * map_mutations; |
---|
| 61 | } ret = { |
---|
| 62 | .count = build<SimpleCounter>("Count", group), |
---|
| 63 | .size = build<AverageCounter<double>>("Average Size", group), |
---|
| 64 | .new_scopes = build<SimpleCounter>("Scopes", group), |
---|
| 65 | .lazy_scopes = build<SimpleCounter>("Lazy Scopes", group), |
---|
| 66 | .avg_scope_depth = build<AverageCounter<double>>("Average Scope", group), |
---|
| 67 | .max_scope_depth = build<MaxCounter<size_t>>("Max Scope", group), |
---|
| 68 | .add_calls = build<SimpleCounter>("Add Calls", group), |
---|
| 69 | .lookup_calls = build<SimpleCounter>("Lookup Calls", group), |
---|
| 70 | .map_lookups = build<SimpleCounter>("Map Lookups", group), |
---|
| 71 | .map_mutations = build<SimpleCounter>("Map Mutations", group) |
---|
| 72 | }; |
---|
| 73 | return ret; |
---|
| 74 | } |
---|
| 75 | } |
---|
[e8032b0] | 76 | |
---|
[6e50a6b] | 77 | Indexer::Indexer( bool trackIdentifiers ) |
---|
[ef5b828] | 78 | : idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable(), |
---|
[6e50a6b] | 79 | prevScope(), scope( 0 ), repScope( 0 ), trackIdentifiers( trackIdentifiers ) { ++* stats().count; } |
---|
[e8032b0] | 80 | |
---|
[b419abb] | 81 | Indexer::~Indexer() { |
---|
| 82 | stats().size->push( idTable ? idTable->size() : 0 ); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | void Indexer::lazyInitScope() { |
---|
| 86 | if ( repScope < scope ) { |
---|
[ef5b828] | 87 | ++* stats().lazy_scopes; |
---|
[b419abb] | 88 | // create rollback |
---|
[ef5b828] | 89 | prevScope = std::make_shared<Indexer>( * this ); |
---|
[b419abb] | 90 | // update repScope |
---|
| 91 | repScope = scope; |
---|
| 92 | } |
---|
| 93 | } |
---|
[e8032b0] | 94 | |
---|
[b8665e3] | 95 | void Indexer::enterScope() { |
---|
| 96 | ++scope; |
---|
[e8032b0] | 97 | |
---|
[ef5b828] | 98 | ++* stats().new_scopes; |
---|
[b419abb] | 99 | stats().avg_scope_depth->push( scope ); |
---|
| 100 | stats().max_scope_depth->push( scope ); |
---|
[e8032b0] | 101 | } |
---|
| 102 | |
---|
[b8665e3] | 103 | void Indexer::leaveScope() { |
---|
[b419abb] | 104 | if ( repScope == scope ) { |
---|
| 105 | Ptr prev = prevScope; // make sure prevScope stays live |
---|
[ef5b828] | 106 | * this = std::move(* prevScope); // replace with previous scope |
---|
[b419abb] | 107 | } |
---|
[e8032b0] | 108 | |
---|
[b419abb] | 109 | --scope; |
---|
[e8032b0] | 110 | } |
---|
[17cd4eb] | 111 | |
---|
[ef5b828] | 112 | void Indexer::lookupId( const std::string & id, std::list< IdData > &out ) const { |
---|
[6e50a6b] | 113 | assert( trackIdentifiers ); |
---|
| 114 | |
---|
[ef5b828] | 115 | ++* stats().lookup_calls; |
---|
[b8665e3] | 116 | if ( ! idTable ) return; |
---|
[743fbda] | 117 | |
---|
[ef5b828] | 118 | ++* stats().map_lookups; |
---|
[b8665e3] | 119 | auto decls = idTable->find( id ); |
---|
| 120 | if ( decls == idTable->end() ) return; |
---|
[1ba88a0] | 121 | |
---|
[b8665e3] | 122 | for ( auto decl : *(decls->second) ) { |
---|
| 123 | out.push_back( decl.second ); |
---|
| 124 | } |
---|
[a08ba92] | 125 | } |
---|
[bdd516a] | 126 | |
---|
[8fd52e90] | 127 | const NamedTypeDecl * Indexer::lookupType( const std::string & id ) const { |
---|
[ef5b828] | 128 | ++* stats().lookup_calls; |
---|
[b8665e3] | 129 | if ( ! typeTable ) return nullptr; |
---|
[ef5b828] | 130 | ++* stats().map_lookups; |
---|
[b8665e3] | 131 | auto it = typeTable->find( id ); |
---|
| 132 | return it == typeTable->end() ? nullptr : it->second.decl; |
---|
[a08ba92] | 133 | } |
---|
[17cd4eb] | 134 | |
---|
[8fd52e90] | 135 | const StructDecl * Indexer::lookupStruct( const std::string & id ) const { |
---|
[ef5b828] | 136 | ++* stats().lookup_calls; |
---|
[b8665e3] | 137 | if ( ! structTable ) return nullptr; |
---|
[ef5b828] | 138 | ++* stats().map_lookups; |
---|
[b8665e3] | 139 | auto it = structTable->find( id ); |
---|
| 140 | return it == structTable->end() ? nullptr : it->second.decl; |
---|
[9a7a3b6] | 141 | } |
---|
| 142 | |
---|
[8fd52e90] | 143 | const EnumDecl * Indexer::lookupEnum( const std::string & id ) const { |
---|
[ef5b828] | 144 | ++* stats().lookup_calls; |
---|
[b8665e3] | 145 | if ( ! enumTable ) return nullptr; |
---|
[ef5b828] | 146 | ++* stats().map_lookups; |
---|
[b8665e3] | 147 | auto it = enumTable->find( id ); |
---|
| 148 | return it == enumTable->end() ? nullptr : it->second.decl; |
---|
[a08ba92] | 149 | } |
---|
[17cd4eb] | 150 | |
---|
[8fd52e90] | 151 | const UnionDecl * Indexer::lookupUnion( const std::string & id ) const { |
---|
[ef5b828] | 152 | ++* stats().lookup_calls; |
---|
[b8665e3] | 153 | if ( ! unionTable ) return nullptr; |
---|
[ef5b828] | 154 | ++* stats().map_lookups; |
---|
[b8665e3] | 155 | auto it = unionTable->find( id ); |
---|
| 156 | return it == unionTable->end() ? nullptr : it->second.decl; |
---|
[e8032b0] | 157 | } |
---|
| 158 | |
---|
[8fd52e90] | 159 | const TraitDecl * Indexer::lookupTrait( const std::string & id ) const { |
---|
[ef5b828] | 160 | ++* stats().lookup_calls; |
---|
[b8665e3] | 161 | if ( ! traitTable ) return nullptr; |
---|
[ef5b828] | 162 | ++* stats().map_lookups; |
---|
[b8665e3] | 163 | auto it = traitTable->find( id ); |
---|
| 164 | return it == traitTable->end() ? nullptr : it->second.decl; |
---|
[52c2a72] | 165 | } |
---|
| 166 | |
---|
[ef5b828] | 167 | const Indexer * Indexer::atScope( unsigned long target ) const { |
---|
[b419abb] | 168 | // by lazy construction, final indexer in list has repScope 0, cannot be > target |
---|
| 169 | // otherwise, will find first scope representing the target |
---|
[ef5b828] | 170 | const Indexer * indexer = this; |
---|
[b419abb] | 171 | while ( indexer->repScope > target ) { |
---|
[b8665e3] | 172 | indexer = indexer->prevScope.get(); |
---|
[8884112] | 173 | } |
---|
[b8665e3] | 174 | return indexer; |
---|
| 175 | } |
---|
| 176 | |
---|
[ef5b828] | 177 | const NamedTypeDecl * Indexer::globalLookupType( const std::string & id ) const { |
---|
[b8665e3] | 178 | return atScope( 0 )->lookupType( id ); |
---|
[52c2a72] | 179 | } |
---|
[743fbda] | 180 | |
---|
[ef5b828] | 181 | const StructDecl * Indexer::globalLookupStruct( const std::string & id ) const { |
---|
[b8665e3] | 182 | return atScope( 0 )->lookupStruct( id ); |
---|
[52c2a72] | 183 | } |
---|
[743fbda] | 184 | |
---|
[ef5b828] | 185 | const UnionDecl * Indexer::globalLookupUnion( const std::string & id ) const { |
---|
[b8665e3] | 186 | return atScope( 0 )->lookupUnion( id ); |
---|
[52c2a72] | 187 | } |
---|
[743fbda] | 188 | |
---|
[ef5b828] | 189 | const EnumDecl * Indexer::globalLookupEnum( const std::string & id ) const { |
---|
[b8665e3] | 190 | return atScope( 0 )->lookupEnum( id ); |
---|
[a08ba92] | 191 | } |
---|
[17cd4eb] | 192 | |
---|
[6f096d2] | 193 | bool isFunction( const DeclarationWithType * decl ) { |
---|
[3f024c9] | 194 | return GenPoly::getFunctionType( decl->get_type() ); |
---|
| 195 | } |
---|
| 196 | |
---|
[6f096d2] | 197 | bool isObject( const DeclarationWithType * decl ) { |
---|
[3f024c9] | 198 | return ! isFunction( decl ); |
---|
| 199 | } |
---|
| 200 | |
---|
[6f096d2] | 201 | bool isDefinition( const DeclarationWithType * decl ) { |
---|
| 202 | if ( const FunctionDecl * func = dynamic_cast< const FunctionDecl * >( decl ) ) { |
---|
[3f024c9] | 203 | // a function is a definition if it has a body |
---|
| 204 | return func->statements; |
---|
| 205 | } else { |
---|
| 206 | // an object is a definition if it is not marked extern. |
---|
| 207 | // both objects must be marked extern |
---|
| 208 | return ! decl->get_storageClasses().is_extern; |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
[ef5b828] | 212 | |
---|
| 213 | bool Indexer::addedIdConflicts( |
---|
[6f096d2] | 214 | const Indexer::IdData & existing, const DeclarationWithType * added, |
---|
[e67991f] | 215 | Indexer::OnConflict handleConflicts, const Declaration * deleteStmt ) { |
---|
[ef5b828] | 216 | // if we're giving the same name mangling to things of different types then there is |
---|
[b419abb] | 217 | // something wrong |
---|
[3f024c9] | 218 | assert( (isObject( added ) && isObject( existing.id ) ) |
---|
| 219 | || ( isFunction( added ) && isFunction( existing.id ) ) ); |
---|
[bed4c63e] | 220 | |
---|
[b8665e3] | 221 | if ( LinkageSpec::isOverridable( existing.id->linkage ) ) { |
---|
[bed4c63e] | 222 | // new definition shadows the autogenerated one, even at the same scope |
---|
| 223 | return false; |
---|
[ef5b828] | 224 | } else if ( LinkageSpec::isMangled( added->linkage ) |
---|
| 225 | || ResolvExpr::typesCompatible( |
---|
[b8665e3] | 226 | added->get_type(), existing.id->get_type(), Indexer() ) ) { |
---|
[0ac366b] | 227 | |
---|
| 228 | // it is a conflict if one declaration is deleted and the other is not |
---|
| 229 | if ( deleteStmt && ! existing.deleteStmt ) { |
---|
[b8665e3] | 230 | if ( handleConflicts.mode == OnConflict::Error ) { |
---|
| 231 | SemanticError( added, "deletion of defined identifier " ); |
---|
| 232 | } |
---|
| 233 | return true; |
---|
[0ac366b] | 234 | } else if ( ! deleteStmt && existing.deleteStmt ) { |
---|
[b8665e3] | 235 | if ( handleConflicts.mode == OnConflict::Error ) { |
---|
| 236 | SemanticError( added, "definition of deleted identifier " ); |
---|
| 237 | } |
---|
| 238 | return true; |
---|
[0ac366b] | 239 | } |
---|
| 240 | |
---|
[3f024c9] | 241 | if ( isDefinition( added ) && isDefinition( existing.id ) ) { |
---|
[b8665e3] | 242 | if ( handleConflicts.mode == OnConflict::Error ) { |
---|
[ef5b828] | 243 | SemanticError( added, |
---|
| 244 | isFunction( added ) ? |
---|
| 245 | "duplicate function definition for " : |
---|
[b8665e3] | 246 | "duplicate object definition for " ); |
---|
| 247 | } |
---|
| 248 | return true; |
---|
[bed4c63e] | 249 | } // if |
---|
| 250 | } else { |
---|
[b8665e3] | 251 | if ( handleConflicts.mode == OnConflict::Error ) { |
---|
| 252 | SemanticError( added, "duplicate definition for " ); |
---|
| 253 | } |
---|
| 254 | return true; |
---|
[bed4c63e] | 255 | } // if |
---|
| 256 | |
---|
| 257 | return true; |
---|
| 258 | } |
---|
[743fbda] | 259 | |
---|
[ef5b828] | 260 | bool Indexer::hasCompatibleCDecl( const std::string & id, const std::string &mangleName ) const { |
---|
[b8665e3] | 261 | if ( ! idTable ) return false; |
---|
| 262 | |
---|
[ef5b828] | 263 | ++* stats().map_lookups; |
---|
[b8665e3] | 264 | auto decls = idTable->find( id ); |
---|
| 265 | if ( decls == idTable->end() ) return false; |
---|
| 266 | |
---|
| 267 | for ( auto decl : *(decls->second) ) { |
---|
| 268 | // skip other scopes (hidden by this decl) |
---|
| 269 | if ( decl.second.scope != scope ) continue; |
---|
| 270 | // check for C decl with compatible type (by mangleName) |
---|
| 271 | if ( ! LinkageSpec::isMangled( decl.second.id->linkage ) && decl.first == mangleName ) { |
---|
| 272 | return true; |
---|
| 273 | } |
---|
| 274 | } |
---|
[ef5b828] | 275 | |
---|
[b8665e3] | 276 | return false; |
---|
| 277 | } |
---|
| 278 | |
---|
[6f096d2] | 279 | bool Indexer::hasIncompatibleCDecl(const std::string & id, const std::string &mangleName ) const { |
---|
[b8665e3] | 280 | if ( ! idTable ) return false; |
---|
| 281 | |
---|
[ef5b828] | 282 | ++* stats().map_lookups; |
---|
[b8665e3] | 283 | auto decls = idTable->find( id ); |
---|
| 284 | if ( decls == idTable->end() ) return false; |
---|
[bed4c63e] | 285 | |
---|
[b8665e3] | 286 | for ( auto decl : *(decls->second) ) { |
---|
| 287 | // skip other scopes (hidden by this decl) |
---|
| 288 | if ( decl.second.scope != scope ) continue; |
---|
| 289 | // check for C decl with incompatible type (by manglename) |
---|
| 290 | if ( ! LinkageSpec::isMangled( decl.second.id->linkage ) && decl.first != mangleName ) { |
---|
| 291 | return true; |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | return false; |
---|
| 296 | } |
---|
| 297 | |
---|
[42f1279c] | 298 | /// gets the base type of the first parameter; decl must be a ctor/dtor/assignment function |
---|
[6f096d2] | 299 | std::string getOtypeKey( const FunctionDecl * function ) { |
---|
[42f1279c] | 300 | auto& params = function->type->parameters; |
---|
| 301 | assert( ! params.empty() ); |
---|
| 302 | // use base type of pointer, so that qualifiers on the pointer type aren't considered. |
---|
[ef5b828] | 303 | Type * base = InitTweak::getPointerBase( params.front()->get_type() ); |
---|
[42f1279c] | 304 | assert( base ); |
---|
| 305 | return Mangler::mangle( base ); |
---|
| 306 | } |
---|
| 307 | |
---|
[ef5b828] | 308 | /// gets the declaration for the function acting on a type specified by otype key, |
---|
[42f1279c] | 309 | /// nullptr if none such |
---|
[6f096d2] | 310 | const FunctionDecl * getFunctionForOtype( const DeclarationWithType * decl, const std::string& otypeKey ) { |
---|
| 311 | const FunctionDecl * func = dynamic_cast< const FunctionDecl * >( decl ); |
---|
[42f1279c] | 312 | if ( ! func || otypeKey != getOtypeKey( func ) ) return nullptr; |
---|
| 313 | return func; |
---|
| 314 | } |
---|
| 315 | |
---|
[6f096d2] | 316 | bool Indexer::removeSpecialOverrides(Indexer::IdData& data, Indexer::MangleTable::Ptr& mangleTable ) { |
---|
[ef5b828] | 317 | // if a type contains user defined ctor/dtor/assign, then special rules trigger, which |
---|
| 318 | // determinethe set of ctor/dtor/assign that can be used by the requester. In particular, |
---|
| 319 | // if the user defines a default ctor, then the generated default ctor is unavailable, |
---|
| 320 | // likewise for copy ctor and dtor. If the user defines any ctor/dtor, then no generated |
---|
| 321 | // field ctors are available. If the user defines any ctor then the generated default ctor |
---|
| 322 | // is unavailable (intrinsic default ctor must be overridden exactly). If the user defines |
---|
| 323 | // anything that looks like a copy constructor, then the generated copy constructor is |
---|
[42f1279c] | 324 | // unavailable, and likewise for the assignment operator. |
---|
| 325 | |
---|
| 326 | // only relevant on function declarations |
---|
[6f096d2] | 327 | const FunctionDecl * function = dynamic_cast< const FunctionDecl * >( data.id ); |
---|
[42f1279c] | 328 | if ( ! function ) return true; |
---|
| 329 | // only need to perform this check for constructors, destructors, and assignment functions |
---|
| 330 | if ( ! CodeGen::isCtorDtorAssign( data.id->name ) ) return true; |
---|
| 331 | |
---|
| 332 | // set up information for this type |
---|
| 333 | bool dataIsUserDefinedFunc = ! LinkageSpec::isOverridable( function->linkage ); |
---|
| 334 | bool dataIsCopyFunc = InitTweak::isCopyFunction( function, function->name ); |
---|
| 335 | std::string dataOtypeKey = getOtypeKey( function ); |
---|
| 336 | |
---|
| 337 | if ( dataIsUserDefinedFunc && dataIsCopyFunc ) { |
---|
| 338 | // this is a user-defined copy function |
---|
| 339 | // if this is the first such, delete/remove non-user-defined overloads as needed |
---|
| 340 | std::vector< std::string > removed; |
---|
| 341 | std::vector< MangleTable::value_type > deleted; |
---|
| 342 | bool alreadyUserDefinedFunc = false; |
---|
[ef5b828] | 343 | |
---|
| 344 | for ( const auto& entry : * mangleTable ) { |
---|
[42f1279c] | 345 | // skip decls that aren't functions or are for the wrong type |
---|
[6f096d2] | 346 | const FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey ); |
---|
[42f1279c] | 347 | if ( ! decl ) continue; |
---|
| 348 | |
---|
| 349 | bool isCopyFunc = InitTweak::isCopyFunction( decl, decl->name ); |
---|
| 350 | if ( ! LinkageSpec::isOverridable( decl->linkage ) ) { |
---|
| 351 | // matching user-defined function |
---|
| 352 | if ( isCopyFunc ) { |
---|
| 353 | // mutation already performed, return early |
---|
| 354 | return true; |
---|
| 355 | } else { |
---|
| 356 | // note that non-copy deletions already performed |
---|
| 357 | alreadyUserDefinedFunc = true; |
---|
| 358 | } |
---|
| 359 | } else { |
---|
| 360 | // non-user-defined function; mark for deletion/removal as appropriate |
---|
| 361 | if ( isCopyFunc ) { |
---|
| 362 | removed.push_back( entry.first ); |
---|
| 363 | } else if ( ! alreadyUserDefinedFunc ) { |
---|
| 364 | deleted.push_back( entry ); |
---|
| 365 | } |
---|
| 366 | } |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | // perform removals from mangle table, and deletions if necessary |
---|
| 370 | for ( const auto& key : removed ) { |
---|
[ef5b828] | 371 | ++* stats().map_mutations; |
---|
[42f1279c] | 372 | mangleTable = mangleTable->erase( key ); |
---|
| 373 | } |
---|
| 374 | if ( ! alreadyUserDefinedFunc ) for ( const auto& entry : deleted ) { |
---|
[ef5b828] | 375 | ++* stats().map_mutations; |
---|
[42f1279c] | 376 | mangleTable = mangleTable->set( entry.first, IdData{ entry.second, function } ); |
---|
| 377 | } |
---|
| 378 | } else if ( dataIsUserDefinedFunc ) { |
---|
| 379 | // this is a user-defined non-copy function |
---|
| 380 | // if this is the first user-defined function, delete non-user-defined overloads |
---|
| 381 | std::vector< MangleTable::value_type > deleted; |
---|
[ef5b828] | 382 | |
---|
| 383 | for ( const auto& entry : * mangleTable ) { |
---|
[42f1279c] | 384 | // skip decls that aren't functions or are for the wrong type |
---|
[6f096d2] | 385 | const FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey ); |
---|
[42f1279c] | 386 | if ( ! decl ) continue; |
---|
| 387 | |
---|
| 388 | // exit early if already a matching user-defined function; |
---|
| 389 | // earlier function will have mutated table |
---|
| 390 | if ( ! LinkageSpec::isOverridable( decl->linkage ) ) return true; |
---|
| 391 | |
---|
| 392 | // skip mutating intrinsic functions |
---|
| 393 | if ( decl->linkage == LinkageSpec::Intrinsic ) continue; |
---|
| 394 | |
---|
| 395 | // user-defined non-copy functions do not override copy functions |
---|
| 396 | if ( InitTweak::isCopyFunction( decl, decl->name ) ) continue; |
---|
| 397 | |
---|
| 398 | // this function to be deleted after mangleTable iteration is complete |
---|
| 399 | deleted.push_back( entry ); |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | // mark deletions to update mangle table |
---|
| 403 | // this needs to be a separate loop because of iterator invalidation |
---|
| 404 | for ( const auto& entry : deleted ) { |
---|
[ef5b828] | 405 | ++* stats().map_mutations; |
---|
[42f1279c] | 406 | mangleTable = mangleTable->set( entry.first, IdData{ entry.second, function } ); |
---|
| 407 | } |
---|
| 408 | } else if ( function->linkage != LinkageSpec::Intrinsic ) { |
---|
| 409 | // this is an overridable generated function |
---|
| 410 | // if there already exists a matching user-defined function, delete this appropriately |
---|
[ef5b828] | 411 | for ( const auto& entry : * mangleTable ) { |
---|
[42f1279c] | 412 | // skip decls that aren't functions or are for the wrong type |
---|
[6f096d2] | 413 | const FunctionDecl * decl = getFunctionForOtype( entry.second.id, dataOtypeKey ); |
---|
[42f1279c] | 414 | if ( ! decl ) continue; |
---|
| 415 | |
---|
| 416 | // skip non-user-defined functions |
---|
| 417 | if ( LinkageSpec::isOverridable( decl->linkage ) ) continue; |
---|
| 418 | |
---|
| 419 | if ( dataIsCopyFunc ) { |
---|
| 420 | // remove current function if exists a user-defined copy function |
---|
[ef5b828] | 421 | // since the signatures for copy functions don't need to match exactly, using |
---|
[42f1279c] | 422 | // a delete statement is the wrong approach |
---|
| 423 | if ( InitTweak::isCopyFunction( decl, decl->name ) ) return false; |
---|
| 424 | } else { |
---|
| 425 | // mark current function deleted by first user-defined function found |
---|
| 426 | data.deleteStmt = decl; |
---|
| 427 | return true; |
---|
| 428 | } |
---|
| 429 | } |
---|
| 430 | } |
---|
[ef5b828] | 431 | |
---|
[42f1279c] | 432 | // nothing (more) to fix, return true |
---|
| 433 | return true; |
---|
| 434 | } |
---|
| 435 | |
---|
[6f096d2] | 436 | void Indexer::addId(const DeclarationWithType * decl, OnConflict handleConflicts, const Expression * baseExpr, |
---|
[e67991f] | 437 | const Declaration * deleteStmt ) { |
---|
[ef5b828] | 438 | ++* stats().add_calls; |
---|
[6e50a6b] | 439 | if ( ! trackIdentifiers ) return; |
---|
[6fc5c14] | 440 | const std::string &name = decl->name; |
---|
[42f1279c] | 441 | if ( name == "" ) return; |
---|
[ef5b828] | 442 | |
---|
[bed4c63e] | 443 | std::string mangleName; |
---|
[6fc5c14] | 444 | if ( LinkageSpec::isOverridable( decl->linkage ) ) { |
---|
[ef5b828] | 445 | // mangle the name without including the appropriate suffix, so overridable routines |
---|
[b8665e3] | 446 | // are placed into the same "bucket" as their user defined versions. |
---|
[bed4c63e] | 447 | mangleName = Mangler::mangle( decl, false ); |
---|
| 448 | } else { |
---|
| 449 | mangleName = Mangler::mangle( decl ); |
---|
| 450 | } // if |
---|
| 451 | |
---|
[ef5b828] | 452 | // this ensures that no two declarations with the same unmangled name at the same scope |
---|
[b8665e3] | 453 | // both have C linkage |
---|
| 454 | if ( LinkageSpec::isMangled( decl->linkage ) ) { |
---|
[490ff5c3] | 455 | // Check that a Cforall declaration doesn't override any C declaration |
---|
[b8665e3] | 456 | if ( hasCompatibleCDecl( name, mangleName ) ) { |
---|
[a16764a6] | 457 | SemanticError( decl, "Cforall declaration hides C function " ); |
---|
[8884112] | 458 | } |
---|
[b8665e3] | 459 | } else { |
---|
[ef5b828] | 460 | // NOTE: only correct if name mangling is completely isomorphic to C |
---|
[b8665e3] | 461 | // type-compatibility, which it may not be. |
---|
| 462 | if ( hasIncompatibleCDecl( name, mangleName ) ) { |
---|
| 463 | SemanticError( decl, "conflicting overload of C function " ); |
---|
| 464 | } |
---|
[bed4c63e] | 465 | } |
---|
[8884112] | 466 | |
---|
[b8665e3] | 467 | // ensure tables exist and add identifier |
---|
| 468 | MangleTable::Ptr mangleTable; |
---|
| 469 | if ( ! idTable ) { |
---|
| 470 | idTable = IdTable::new_ptr(); |
---|
| 471 | mangleTable = MangleTable::new_ptr(); |
---|
| 472 | } else { |
---|
[ef5b828] | 473 | ++* stats().map_lookups; |
---|
[b8665e3] | 474 | auto decls = idTable->find( name ); |
---|
| 475 | if ( decls == idTable->end() ) { |
---|
| 476 | mangleTable = MangleTable::new_ptr(); |
---|
| 477 | } else { |
---|
| 478 | mangleTable = decls->second; |
---|
| 479 | // skip in-scope repeat declarations of same identifier |
---|
[ef5b828] | 480 | ++* stats().map_lookups; |
---|
[b8665e3] | 481 | auto existing = mangleTable->find( mangleName ); |
---|
| 482 | if ( existing != mangleTable->end() |
---|
| 483 | && existing->second.scope == scope |
---|
| 484 | && existing->second.id ) { |
---|
| 485 | if ( addedIdConflicts( existing->second, decl, handleConflicts, deleteStmt ) ) { |
---|
| 486 | if ( handleConflicts.mode == OnConflict::Delete ) { |
---|
| 487 | // set delete expression for conflicting identifier |
---|
[b419abb] | 488 | lazyInitScope(); |
---|
[ef5b828] | 489 | * stats().map_mutations += 2; |
---|
[b8665e3] | 490 | idTable = idTable->set( |
---|
| 491 | name, |
---|
[ef5b828] | 492 | mangleTable->set( |
---|
| 493 | mangleName, |
---|
[b8665e3] | 494 | IdData{ existing->second, handleConflicts.deleteStmt } ) ); |
---|
| 495 | } |
---|
| 496 | return; |
---|
| 497 | } |
---|
| 498 | } |
---|
| 499 | } |
---|
| 500 | } |
---|
[8884112] | 501 | |
---|
[b8665e3] | 502 | // add/overwrite with new identifier |
---|
[b419abb] | 503 | lazyInitScope(); |
---|
[42f1279c] | 504 | IdData data{ decl, baseExpr, deleteStmt, scope }; |
---|
[114bde6] | 505 | // Ensure that auto-generated ctor/dtor/assignment are deleted if necessary |
---|
[42f1279c] | 506 | if ( ! removeSpecialOverrides( data, mangleTable ) ) return; |
---|
[ef5b828] | 507 | * stats().map_mutations += 2; |
---|
[42f1279c] | 508 | idTable = idTable->set( name, mangleTable->set( mangleName, std::move(data) ) ); |
---|
[e8032b0] | 509 | } |
---|
[52c2a72] | 510 | |
---|
[6f096d2] | 511 | void Indexer::addId( const DeclarationWithType * decl, const Expression * baseExpr ) { |
---|
[0ac366b] | 512 | // default handling of conflicts is to raise an error |
---|
[b8665e3] | 513 | addId( decl, OnConflict::error(), baseExpr, decl->isDeleted ? decl : nullptr ); |
---|
[0ac366b] | 514 | } |
---|
| 515 | |
---|
[e67991f] | 516 | void Indexer::addDeletedId( const DeclarationWithType * decl, const Declaration * deleteStmt ) { |
---|
[0ac366b] | 517 | // default handling of conflicts is to raise an error |
---|
[b8665e3] | 518 | addId( decl, OnConflict::error(), nullptr, deleteStmt ); |
---|
[0ac366b] | 519 | } |
---|
| 520 | |
---|
[6f096d2] | 521 | bool addedTypeConflicts( const NamedTypeDecl * existing, const NamedTypeDecl * added ) { |
---|
[ed34540] | 522 | if ( existing->base == nullptr ) { |
---|
[52c2a72] | 523 | return false; |
---|
[ed34540] | 524 | } else if ( added->base == nullptr ) { |
---|
[52c2a72] | 525 | return true; |
---|
| 526 | } else { |
---|
[ed34540] | 527 | assert( existing->base && added->base ); |
---|
| 528 | // typedef redeclarations are errors only if types are different |
---|
| 529 | if ( ! ResolvExpr::typesCompatible( existing->base, added->base, Indexer() ) ) { |
---|
| 530 | SemanticError( added->location, "redeclaration of " + added->name ); |
---|
| 531 | } |
---|
[52c2a72] | 532 | } |
---|
[ef5b828] | 533 | // does not need to be added to the table if both existing and added have a base that are |
---|
[b419abb] | 534 | // the same |
---|
[ed34540] | 535 | return true; |
---|
[52c2a72] | 536 | } |
---|
[743fbda] | 537 | |
---|
[6f096d2] | 538 | void Indexer::addType( const NamedTypeDecl * decl ) { |
---|
[ef5b828] | 539 | ++* stats().add_calls; |
---|
| 540 | const std::string & id = decl->name; |
---|
[b8665e3] | 541 | |
---|
[ef5b828] | 542 | if ( ! typeTable ) { |
---|
[b8665e3] | 543 | typeTable = TypeTable::new_ptr(); |
---|
[52c2a72] | 544 | } else { |
---|
[ef5b828] | 545 | ++* stats().map_lookups; |
---|
[b8665e3] | 546 | auto existing = typeTable->find( id ); |
---|
[ef5b828] | 547 | if ( existing != typeTable->end() |
---|
| 548 | && existing->second.scope == scope |
---|
[b8665e3] | 549 | && addedTypeConflicts( existing->second.decl, decl ) ) return; |
---|
[52c2a72] | 550 | } |
---|
[ef5b828] | 551 | |
---|
[b419abb] | 552 | lazyInitScope(); |
---|
[ef5b828] | 553 | ++* stats().map_mutations; |
---|
[b8665e3] | 554 | typeTable = typeTable->set( id, Scoped<NamedTypeDecl>{ decl, scope } ); |
---|
[52c2a72] | 555 | } |
---|
| 556 | |
---|
[6f096d2] | 557 | bool addedDeclConflicts( const AggregateDecl * existing, const AggregateDecl * added ) { |
---|
[b2da0574] | 558 | if ( ! existing->body ) { |
---|
[52c2a72] | 559 | return false; |
---|
[b2da0574] | 560 | } else if ( added->body ) { |
---|
[a16764a6] | 561 | SemanticError( added, "redeclaration of " ); |
---|
[52c2a72] | 562 | } // if |
---|
| 563 | return true; |
---|
[e8032b0] | 564 | } |
---|
| 565 | |
---|
[ef5b828] | 566 | void Indexer::addStruct( const std::string & id ) { |
---|
[52c2a72] | 567 | addStruct( new StructDecl( id ) ); |
---|
[e8032b0] | 568 | } |
---|
[743fbda] | 569 | |
---|
[6f096d2] | 570 | void Indexer::addStruct( const StructDecl * decl ) { |
---|
[ef5b828] | 571 | ++* stats().add_calls; |
---|
| 572 | const std::string & id = decl->name; |
---|
[b8665e3] | 573 | |
---|
| 574 | if ( ! structTable ) { |
---|
| 575 | structTable = StructTable::new_ptr(); |
---|
[52c2a72] | 576 | } else { |
---|
[ef5b828] | 577 | ++* stats().map_lookups; |
---|
[b8665e3] | 578 | auto existing = structTable->find( id ); |
---|
[ef5b828] | 579 | if ( existing != structTable->end() |
---|
| 580 | && existing->second.scope == scope |
---|
[b8665e3] | 581 | && addedDeclConflicts( existing->second.decl, decl ) ) return; |
---|
[52c2a72] | 582 | } |
---|
[b8665e3] | 583 | |
---|
[b419abb] | 584 | lazyInitScope(); |
---|
[ef5b828] | 585 | ++* stats().map_mutations; |
---|
[b8665e3] | 586 | structTable = structTable->set( id, Scoped<StructDecl>{ decl, scope } ); |
---|
[e8032b0] | 587 | } |
---|
[743fbda] | 588 | |
---|
[6f096d2] | 589 | void Indexer::addEnum( const EnumDecl * decl ) { |
---|
[ef5b828] | 590 | ++* stats().add_calls; |
---|
| 591 | const std::string & id = decl->name; |
---|
[b8665e3] | 592 | |
---|
| 593 | if ( ! enumTable ) { |
---|
| 594 | enumTable = EnumTable::new_ptr(); |
---|
[52c2a72] | 595 | } else { |
---|
[ef5b828] | 596 | ++* stats().map_lookups; |
---|
[b8665e3] | 597 | auto existing = enumTable->find( id ); |
---|
[ef5b828] | 598 | if ( existing != enumTable->end() |
---|
| 599 | && existing->second.scope == scope |
---|
[b8665e3] | 600 | && addedDeclConflicts( existing->second.decl, decl ) ) return; |
---|
[52c2a72] | 601 | } |
---|
[ef5b828] | 602 | |
---|
[b419abb] | 603 | lazyInitScope(); |
---|
[ef5b828] | 604 | ++* stats().map_mutations; |
---|
[b8665e3] | 605 | enumTable = enumTable->set( id, Scoped<EnumDecl>{ decl, scope } ); |
---|
[e8032b0] | 606 | } |
---|
| 607 | |
---|
[ef5b828] | 608 | void Indexer::addUnion( const std::string & id ) { |
---|
[52c2a72] | 609 | addUnion( new UnionDecl( id ) ); |
---|
[e8032b0] | 610 | } |
---|
[743fbda] | 611 | |
---|
[6f096d2] | 612 | void Indexer::addUnion( const UnionDecl * decl ) { |
---|
[ef5b828] | 613 | ++* stats().add_calls; |
---|
| 614 | const std::string & id = decl->name; |
---|
[b8665e3] | 615 | |
---|
| 616 | if ( ! unionTable ) { |
---|
| 617 | unionTable = UnionTable::new_ptr(); |
---|
[52c2a72] | 618 | } else { |
---|
[ef5b828] | 619 | ++* stats().map_lookups; |
---|
[b8665e3] | 620 | auto existing = unionTable->find( id ); |
---|
[ef5b828] | 621 | if ( existing != unionTable->end() |
---|
| 622 | && existing->second.scope == scope |
---|
[b8665e3] | 623 | && addedDeclConflicts( existing->second.decl, decl ) ) return; |
---|
[52c2a72] | 624 | } |
---|
[b8665e3] | 625 | |
---|
[b419abb] | 626 | lazyInitScope(); |
---|
[ef5b828] | 627 | ++* stats().map_mutations; |
---|
[b8665e3] | 628 | unionTable = unionTable->set( id, Scoped<UnionDecl>{ decl, scope } ); |
---|
[e8032b0] | 629 | } |
---|
[743fbda] | 630 | |
---|
[6f096d2] | 631 | void Indexer::addTrait( const TraitDecl * decl ) { |
---|
[ef5b828] | 632 | ++* stats().add_calls; |
---|
| 633 | const std::string & id = decl->name; |
---|
[b8665e3] | 634 | |
---|
| 635 | if ( ! traitTable ) { |
---|
| 636 | traitTable = TraitTable::new_ptr(); |
---|
[52c2a72] | 637 | } else { |
---|
[ef5b828] | 638 | ++* stats().map_lookups; |
---|
[b8665e3] | 639 | auto existing = traitTable->find( id ); |
---|
[ef5b828] | 640 | if ( existing != traitTable->end() |
---|
| 641 | && existing->second.scope == scope |
---|
[b8665e3] | 642 | && addedDeclConflicts( existing->second.decl, decl ) ) return; |
---|
[52c2a72] | 643 | } |
---|
[b8665e3] | 644 | |
---|
[b419abb] | 645 | lazyInitScope(); |
---|
[ef5b828] | 646 | ++* stats().map_mutations; |
---|
[b8665e3] | 647 | traitTable = traitTable->set( id, Scoped<TraitDecl>{ decl, scope } ); |
---|
[a08ba92] | 648 | } |
---|
[17cd4eb] | 649 | |
---|
[6f096d2] | 650 | void Indexer::addMembers( const AggregateDecl * aggr, const Expression * expr, OnConflict handleConflicts ) { |
---|
[1485c1a] | 651 | for ( Declaration * decl : aggr->members ) { |
---|
| 652 | if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { |
---|
[0ac366b] | 653 | addId( dwt, handleConflicts, expr ); |
---|
[1485c1a] | 654 | if ( dwt->name == "" ) { |
---|
[6f096d2] | 655 | const Type * t = dwt->get_type()->stripReferences(); |
---|
| 656 | if ( dynamic_cast<const StructInstType *>( t ) || dynamic_cast<const UnionInstType *>( t ) ) { |
---|
[1485c1a] | 657 | Expression * base = expr->clone(); |
---|
[a181494] | 658 | ResolvExpr::Cost cost = ResolvExpr::Cost::zero; // xxx - carry this cost into the indexer as a base cost? |
---|
| 659 | ResolvExpr::referenceToRvalueConversion( base, cost ); |
---|
[0ac366b] | 660 | addMembers( t->getAggr(), new MemberExpr( dwt, base ), handleConflicts ); |
---|
[1485c1a] | 661 | } |
---|
| 662 | } |
---|
| 663 | } |
---|
| 664 | } |
---|
| 665 | } |
---|
| 666 | |
---|
[e67991f] | 667 | void Indexer::addWith( const std::list< Expression * > & withExprs, const Declaration * withStmt ) { |
---|
[6f096d2] | 668 | for ( const Expression * expr : withExprs ) { |
---|
[81644e0] | 669 | if ( expr->result ) { |
---|
[497282e] | 670 | AggregateDecl * aggr = expr->result->stripReferences()->getAggr(); |
---|
[81644e0] | 671 | assertf( aggr, "WithStmt expr has non-aggregate type: %s", toString( expr->result ).c_str() ); |
---|
| 672 | |
---|
[b8665e3] | 673 | addMembers( aggr, expr, OnConflict::deleteWith( withStmt ) ); |
---|
[81644e0] | 674 | } |
---|
| 675 | } |
---|
| 676 | } |
---|
| 677 | |
---|
[5fe35d6] | 678 | void Indexer::addIds( const std::list< DeclarationWithType * > & decls ) { |
---|
| 679 | for ( auto d : decls ) { |
---|
| 680 | addId( d ); |
---|
| 681 | } |
---|
| 682 | } |
---|
| 683 | |
---|
| 684 | void Indexer::addTypes( const std::list< TypeDecl * > & tds ) { |
---|
| 685 | for ( auto td : tds ) { |
---|
| 686 | addType( td ); |
---|
| 687 | addIds( td->assertions ); |
---|
| 688 | } |
---|
| 689 | } |
---|
| 690 | |
---|
[6f096d2] | 691 | void Indexer::addFunctionType( const FunctionType * ftype ) { |
---|
[5fe35d6] | 692 | addTypes( ftype->forall ); |
---|
| 693 | addIds( ftype->returnVals ); |
---|
| 694 | addIds( ftype->parameters ); |
---|
| 695 | } |
---|
| 696 | |
---|
[a181494] | 697 | Expression * Indexer::IdData::combine( ResolvExpr::Cost & cost ) const { |
---|
[0ac366b] | 698 | Expression * ret = nullptr; |
---|
[a40d503] | 699 | if ( baseExpr ) { |
---|
| 700 | Expression * base = baseExpr->clone(); |
---|
[a181494] | 701 | ResolvExpr::referenceToRvalueConversion( base, cost ); |
---|
[6f096d2] | 702 | ret = new MemberExpr( const_cast<DeclarationWithType *>(id), base ); |
---|
[a40d503] | 703 | // xxx - this introduces hidden environments, for now remove them. |
---|
| 704 | // std::swap( base->env, ret->env ); |
---|
| 705 | delete base->env; |
---|
| 706 | base->env = nullptr; |
---|
| 707 | } else { |
---|
[6f096d2] | 708 | ret = new VariableExpr( const_cast<DeclarationWithType *>(id) ); |
---|
[a40d503] | 709 | } |
---|
[e67991f] | 710 | if ( deleteStmt ) ret = new DeletedExpr( ret, const_cast<Declaration *>(deleteStmt) ); |
---|
[0ac366b] | 711 | return ret; |
---|
[a40d503] | 712 | } |
---|
[51b7345] | 713 | } // namespace SymTab |
---|
[0dd3a2f] | 714 | |
---|
| 715 | // Local Variables: // |
---|
| 716 | // tab-width: 4 // |
---|
| 717 | // mode: c++ // |
---|
| 718 | // compile-command: "make install" // |
---|
| 719 | // End: // |
---|