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