[51587aa] | 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 | // |
---|
[11df881] | 7 | // GenInit.cc -- Generate initializers, and other stuff. |
---|
[51587aa] | 8 | // |
---|
[cf16f94] | 9 | // Author : Rob Schluntz |
---|
[51587aa] | 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[a36eb2d] | 11 | // Last Modified By : Andrew Beach |
---|
[148ba7d] | 12 | // Last Modified On : Mon Oct 25 13:53:00 2021 |
---|
| 13 | // Update Count : 186 |
---|
[51587aa] | 14 | // |
---|
[8ca3a72] | 15 | #include "GenInit.h" |
---|
| 16 | |
---|
[2bfc6b2] | 17 | #include <stddef.h> // for NULL |
---|
| 18 | #include <algorithm> // for any_of |
---|
| 19 | #include <cassert> // for assert, strict_dynamic_cast, assertf |
---|
[b8524ca] | 20 | #include <deque> |
---|
[2bfc6b2] | 21 | #include <iterator> // for back_inserter, inserter, back_inse... |
---|
| 22 | #include <list> // for _List_iterator, list |
---|
[8ca3a72] | 23 | |
---|
[b8524ca] | 24 | #include "AST/Decl.hpp" |
---|
| 25 | #include "AST/Init.hpp" |
---|
[a36eb2d] | 26 | #include "AST/Pass.hpp" |
---|
[b8524ca] | 27 | #include "AST/Node.hpp" |
---|
| 28 | #include "AST/Stmt.hpp" |
---|
[16ba4a6f] | 29 | #include "CompilationState.h" |
---|
[8135d4c] | 30 | #include "CodeGen/OperatorTable.h" |
---|
[2bfc6b2] | 31 | #include "Common/SemanticError.h" // for SemanticError |
---|
[9feb34b] | 32 | #include "Common/ToString.hpp" // for toCString |
---|
[2bfc6b2] | 33 | #include "Common/UniqueName.h" // for UniqueName |
---|
| 34 | #include "Common/utility.h" // for ValueGuard, maybeClone |
---|
| 35 | #include "GenPoly/GenPoly.h" // for getFunctionType, isPolyType |
---|
| 36 | #include "GenPoly/ScopedSet.h" // for ScopedSet, ScopedSet<>::const_iter... |
---|
| 37 | #include "InitTweak.h" // for isConstExpr, InitExpander, checkIn... |
---|
[fdd0509] | 38 | #include "ResolvExpr/Resolver.h" |
---|
[fb4dc28] | 39 | #include "SymTab/GenImplicitCall.hpp" // for genImplicitCall |
---|
[2bfc6b2] | 40 | #include "SymTab/Mangler.h" // for Mangler |
---|
| 41 | #include "Tuples/Tuples.h" // for maybeImpure |
---|
| 42 | #include "Validate/FindSpecialDecls.h" // for SizeType |
---|
[42e2ad7] | 43 | |
---|
| 44 | namespace InitTweak { |
---|
[09867ec] | 45 | |
---|
[a36eb2d] | 46 | namespace { |
---|
| 47 | |
---|
[f02f546] | 48 | // Outer pass finds declarations, for their type could wrap a type that needs hoisting |
---|
[0bd3faf] | 49 | struct HoistArrayDimension_NoResolve final : |
---|
[a36eb2d] | 50 | public ast::WithDeclsToAdd<>, public ast::WithShortCircuiting, |
---|
[c600df1] | 51 | public ast::WithGuards, public ast::WithConstTranslationUnit, |
---|
[0bd3faf] | 52 | public ast::WithVisitorRef<HoistArrayDimension_NoResolve>, |
---|
[f02f546] | 53 | public ast::WithSymbolTableX<ast::SymbolTable::ErrorDetection::IgnoreErrors> { |
---|
| 54 | |
---|
| 55 | // Inner pass looks within a type, for a part that depends on an expression |
---|
| 56 | struct HoistDimsFromTypes final : |
---|
| 57 | public ast::WithShortCircuiting, public ast::WithGuards { |
---|
| 58 | |
---|
[0bd3faf] | 59 | HoistArrayDimension_NoResolve * outer; |
---|
| 60 | HoistDimsFromTypes( HoistArrayDimension_NoResolve * outer ) : outer(outer) {} |
---|
[f02f546] | 61 | |
---|
| 62 | // Only intended for visiting through types. |
---|
| 63 | // Tolerate, and short-circuit at, the dimension expression of an array type. |
---|
| 64 | // (We'll operate on the dimension expression of an array type directly |
---|
| 65 | // from the parent type, not by visiting through it) |
---|
| 66 | // Look inside type exprs. |
---|
| 67 | void previsit( const ast::Node * ) { |
---|
| 68 | assert( false && "unsupported node type" ); |
---|
| 69 | }; |
---|
| 70 | const ast::Expr * allowedExpr = nullptr; |
---|
| 71 | void previsit( const ast::Type * ) { |
---|
| 72 | GuardValue( allowedExpr ) = nullptr; |
---|
| 73 | } |
---|
| 74 | void previsit( const ast::ArrayType * t ) { |
---|
| 75 | GuardValue( allowedExpr ) = t->dimension.get(); |
---|
| 76 | } |
---|
| 77 | void previsit( const ast::PointerType * t ) { |
---|
| 78 | GuardValue( allowedExpr ) = t->dimension.get(); |
---|
| 79 | } |
---|
| 80 | void previsit( const ast::TypeofType * t ) { |
---|
| 81 | GuardValue( allowedExpr ) = t->expr.get(); |
---|
| 82 | } |
---|
| 83 | void previsit( const ast::Expr * e ) { |
---|
| 84 | assert( e == allowedExpr && |
---|
| 85 | "only expecting to visit exprs that are dimension exprs or typeof(-) inner exprs" ); |
---|
[a36eb2d] | 86 | |
---|
[f02f546] | 87 | // Skip the tolerated expressions |
---|
| 88 | visit_children = false; |
---|
| 89 | } |
---|
| 90 | void previsit( const ast::TypeExpr * ) {} |
---|
| 91 | |
---|
| 92 | const ast::Type * postvisit( |
---|
| 93 | const ast::ArrayType * arrayType ) { |
---|
| 94 | static UniqueName dimensionName( "_array_dim" ); |
---|
| 95 | |
---|
| 96 | if ( nullptr == arrayType->dimension ) { // if no dimension is given, don't presume to invent one |
---|
| 97 | return arrayType; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | // find size_t; use it as the type for a dim expr |
---|
| 101 | ast::ptr<ast::Type> dimType = outer->transUnit().global.sizeType; |
---|
| 102 | assert( dimType ); |
---|
| 103 | add_qualifiers( dimType, ast::CV::Qualifiers( ast::CV::Const ) ); |
---|
| 104 | |
---|
| 105 | // Special-case handling: leave the user's dimension expression alone |
---|
| 106 | // - requires the user to have followed a careful convention |
---|
| 107 | // - may apply to extremely simple applications, but only as windfall |
---|
| 108 | // - users of advanced applications will be following the convention on purpose |
---|
| 109 | // - CFA maintainers must protect the criteria against leaving too much alone |
---|
| 110 | |
---|
| 111 | // Actual leave-alone cases following are conservative approximations of "cannot vary" |
---|
| 112 | |
---|
| 113 | // Leave alone: literals and enum constants |
---|
| 114 | if ( dynamic_cast< const ast::ConstantExpr * >( arrayType->dimension.get() ) ) { |
---|
| 115 | return arrayType; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | // Leave alone: direct use of an object declared to be const |
---|
| 119 | const ast::NameExpr * dimn = dynamic_cast< const ast::NameExpr * >( arrayType->dimension.get() ); |
---|
| 120 | if ( dimn ) { |
---|
| 121 | std::vector<ast::SymbolTable::IdData> dimnDefs = outer->symtab.lookupId( dimn->name ); |
---|
| 122 | if ( dimnDefs.size() == 1 ) { |
---|
| 123 | const ast::DeclWithType * dimnDef = dimnDefs[0].id.get(); |
---|
| 124 | assert( dimnDef && "symbol table binds a name to nothing" ); |
---|
| 125 | const ast::ObjectDecl * dimOb = dynamic_cast< const ast::ObjectDecl * >( dimnDef ); |
---|
| 126 | if( dimOb ) { |
---|
| 127 | const ast::Type * dimTy = dimOb->type.get(); |
---|
| 128 | assert( dimTy && "object declaration bearing no type" ); |
---|
| 129 | // must not hoist some: size_t |
---|
| 130 | // must hoist all: pointers and references |
---|
| 131 | // the analysis is conservative; BasicType is a simple approximation |
---|
| 132 | if ( dynamic_cast< const ast::BasicType * >( dimTy ) || |
---|
| 133 | dynamic_cast< const ast::SueInstType<ast::EnumDecl> * >( dimTy ) ) { |
---|
| 134 | if ( dimTy->is_const() ) { |
---|
| 135 | // The dimension is certainly re-evaluable, giving the same answer each time. |
---|
| 136 | // Our user might be hoping to write the array type in multiple places, having them unify. |
---|
| 137 | // Leave the type alone. |
---|
| 138 | |
---|
| 139 | // We believe the new criterion leaves less alone than the old criterion. |
---|
| 140 | // Thus, the old criterion should have left the current case alone. |
---|
| 141 | // Catch cases that weren't thought through. |
---|
| 142 | assert( !Tuples::maybeImpure( arrayType->dimension ) ); |
---|
| 143 | |
---|
| 144 | return arrayType; |
---|
| 145 | } |
---|
| 146 | }; |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | // Leave alone: any sizeof expression (answer cannot vary during current lexical scope) |
---|
| 152 | const ast::SizeofExpr * sz = dynamic_cast< const ast::SizeofExpr * >( arrayType->dimension.get() ); |
---|
| 153 | if ( sz ) { |
---|
| 154 | return arrayType; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | // General-case handling: change the array-type's dim expr (hoist the user-given content out of the type) |
---|
| 158 | // - always safe |
---|
| 159 | // - user-unnoticeable in common applications (benign noise in -CFA output) |
---|
| 160 | // - may annoy a responsible user of advanced applications (but they can work around) |
---|
| 161 | // - protects against misusing advanced features |
---|
| 162 | // |
---|
| 163 | // The hoist, by example, is: |
---|
| 164 | // FROM USER: float a[ rand() ]; |
---|
| 165 | // TO GCC: const size_t __len_of_a = rand(); float a[ __len_of_a ]; |
---|
| 166 | |
---|
| 167 | ast::ObjectDecl * arrayDimension = new ast::ObjectDecl( |
---|
| 168 | arrayType->dimension->location, |
---|
| 169 | dimensionName.newName(), |
---|
| 170 | dimType, |
---|
| 171 | new ast::SingleInit( |
---|
| 172 | arrayType->dimension->location, |
---|
| 173 | arrayType->dimension |
---|
| 174 | ) |
---|
| 175 | ); |
---|
| 176 | |
---|
| 177 | ast::ArrayType * mutType = ast::mutate( arrayType ); |
---|
| 178 | mutType->dimension = new ast::VariableExpr( |
---|
| 179 | arrayDimension->location, arrayDimension ); |
---|
| 180 | outer->declsToAddBefore.push_back( arrayDimension ); |
---|
| 181 | |
---|
| 182 | return mutType; |
---|
| 183 | } // postvisit( const ast::ArrayType * ) |
---|
| 184 | }; // struct HoistDimsFromTypes |
---|
[a36eb2d] | 185 | |
---|
| 186 | ast::Storage::Classes storageClasses; |
---|
[f02f546] | 187 | void previsit( |
---|
| 188 | const ast::ObjectDecl * decl ) { |
---|
| 189 | GuardValue( storageClasses ) = decl->storage; |
---|
[a36eb2d] | 190 | } |
---|
| 191 | |
---|
[f02f546] | 192 | const ast::DeclWithType * postvisit( |
---|
| 193 | const ast::ObjectDecl * objectDecl ) { |
---|
[a36eb2d] | 194 | |
---|
[f02f546] | 195 | if ( !isInFunction() || storageClasses.is_static ) { |
---|
| 196 | return objectDecl; |
---|
[a36eb2d] | 197 | } |
---|
| 198 | |
---|
[f02f546] | 199 | const ast::Type * mid = objectDecl->type; |
---|
[a36eb2d] | 200 | |
---|
[f02f546] | 201 | ast::Pass<HoistDimsFromTypes> hoist{this}; |
---|
| 202 | const ast::Type * result = mid->accept( hoist ); |
---|
[a36eb2d] | 203 | |
---|
[f02f546] | 204 | return mutate_field( objectDecl, &ast::ObjectDecl::type, result ); |
---|
[a36eb2d] | 205 | } |
---|
[f02f546] | 206 | }; |
---|
| 207 | |
---|
[0bd3faf] | 208 | struct ReturnFixer final : |
---|
[fc134a48] | 209 | public ast::WithStmtsToAdd<>, ast::WithGuards, ast::WithShortCircuiting { |
---|
[a36eb2d] | 210 | void previsit( const ast::FunctionDecl * decl ); |
---|
| 211 | const ast::ReturnStmt * previsit( const ast::ReturnStmt * stmt ); |
---|
| 212 | private: |
---|
| 213 | const ast::FunctionDecl * funcDecl = nullptr; |
---|
| 214 | }; |
---|
| 215 | |
---|
[0bd3faf] | 216 | void ReturnFixer::previsit( const ast::FunctionDecl * decl ) { |
---|
[fc134a48] | 217 | if (decl->linkage == ast::Linkage::Intrinsic) visit_children = false; |
---|
[148ba7d] | 218 | GuardValue( funcDecl ) = decl; |
---|
[a36eb2d] | 219 | } |
---|
| 220 | |
---|
[0bd3faf] | 221 | const ast::ReturnStmt * ReturnFixer::previsit( |
---|
[a36eb2d] | 222 | const ast::ReturnStmt * stmt ) { |
---|
| 223 | auto & returns = funcDecl->returns; |
---|
| 224 | assert( returns.size() < 2 ); |
---|
| 225 | // Hands off if the function returns a reference. |
---|
| 226 | // Don't allocate a temporary if the address is returned. |
---|
| 227 | if ( stmt->expr && 1 == returns.size() ) { |
---|
| 228 | ast::ptr<ast::DeclWithType> retDecl = returns.front(); |
---|
| 229 | if ( isConstructable( retDecl->get_type() ) ) { |
---|
| 230 | // Explicitly construct the return value using the return |
---|
| 231 | // expression and the retVal object. |
---|
| 232 | assertf( "" != retDecl->name, |
---|
| 233 | "Function %s has unnamed return value.\n", |
---|
| 234 | funcDecl->name.c_str() ); |
---|
| 235 | |
---|
| 236 | auto retVal = retDecl.strict_as<ast::ObjectDecl>(); |
---|
| 237 | if ( auto varExpr = stmt->expr.as<ast::VariableExpr>() ) { |
---|
| 238 | // Check if the return statement is already set up. |
---|
| 239 | if ( varExpr->var == retVal ) return stmt; |
---|
| 240 | } |
---|
[4e2f1b2] | 241 | const ast::Stmt * ctorStmt = genCtorDtor( |
---|
[a36eb2d] | 242 | retVal->location, "?{}", retVal, stmt->expr ); |
---|
| 243 | assertf( ctorStmt, |
---|
[4ec9513] | 244 | "ReturnFixer: genCtorDtor returned nullptr: %s / %s", |
---|
[a36eb2d] | 245 | toString( retVal ).c_str(), |
---|
| 246 | toString( stmt->expr ).c_str() ); |
---|
[4ec9513] | 247 | stmtsToAddBefore.push_back( ctorStmt ); |
---|
[a36eb2d] | 248 | |
---|
| 249 | // Return the retVal object. |
---|
| 250 | ast::ReturnStmt * mutStmt = ast::mutate( stmt ); |
---|
| 251 | mutStmt->expr = new ast::VariableExpr( |
---|
| 252 | stmt->location, retDecl ); |
---|
| 253 | return mutStmt; |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | return stmt; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | } // namespace |
---|
| 260 | |
---|
[8984003] | 261 | void genInit( ast::TranslationUnit & transUnit ) { |
---|
| 262 | ast::Pass<HoistArrayDimension_NoResolve>::run( transUnit ); |
---|
| 263 | ast::Pass<ReturnFixer>::run( transUnit ); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | void fixReturnStatements( ast::TranslationUnit & transUnit ) { |
---|
| 267 | ast::Pass<ReturnFixer>::run( transUnit ); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | bool ManagedTypes::isManaged( const ast::Type * type ) const { |
---|
| 271 | // references are never constructed |
---|
| 272 | if ( dynamic_cast< const ast::ReferenceType * >( type ) ) return false; |
---|
| 273 | if ( auto tupleType = dynamic_cast< const ast::TupleType * > ( type ) ) { |
---|
| 274 | // tuple is also managed if any of its components are managed |
---|
| 275 | for (auto & component : tupleType->types) { |
---|
| 276 | if (isManaged(component)) return true; |
---|
[16ba4a6f] | 277 | } |
---|
| 278 | } |
---|
[8984003] | 279 | // need to clear and reset qualifiers when determining if a type is managed |
---|
| 280 | // ValueGuard< Type::Qualifiers > qualifiers( type->get_qualifiers() ); |
---|
| 281 | auto tmp = shallowCopy(type); |
---|
| 282 | tmp->qualifiers = {}; |
---|
| 283 | // delete tmp at return |
---|
| 284 | ast::ptr<ast::Type> guard = tmp; |
---|
| 285 | // a type is managed if it appears in the map of known managed types, or if it contains any polymorphism (is a type variable or generic type containing a type variable) |
---|
| 286 | return managedTypes.find( Mangle::mangle( tmp, {Mangle::NoOverrideable | Mangle::NoGenericParams | Mangle::Type} ) ) != managedTypes.end() || GenPoly::isPolyType( tmp ); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | bool ManagedTypes::isManaged( const ast::ObjectDecl * objDecl ) const { |
---|
| 290 | const ast::Type * type = objDecl->type; |
---|
| 291 | while ( auto at = dynamic_cast< const ast::ArrayType * >( type ) ) { |
---|
| 292 | // must always construct VLAs with an initializer, since this is an error in C |
---|
| 293 | if ( at->isVarLen && objDecl->init ) return true; |
---|
| 294 | type = at->base; |
---|
[16ba4a6f] | 295 | } |
---|
[8984003] | 296 | return isManaged( type ); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | void ManagedTypes::handleDWT( const ast::DeclWithType * dwt ) { |
---|
| 300 | // if this function is a user-defined constructor or destructor, mark down the type as "managed" |
---|
| 301 | if ( ! dwt->linkage.is_overrideable && CodeGen::isCtorDtor( dwt->name ) ) { |
---|
| 302 | auto & params = GenPoly::getFunctionType( dwt->get_type())->params; |
---|
| 303 | assert( ! params.empty() ); |
---|
| 304 | // Type * type = InitTweak::getPointerBase( params.front() ); |
---|
| 305 | // assert( type ); |
---|
| 306 | managedTypes.insert( Mangle::mangle( params.front(), {Mangle::NoOverrideable | Mangle::NoGenericParams | Mangle::Type} ) ); |
---|
[16ba4a6f] | 307 | } |
---|
[8984003] | 308 | } |
---|
| 309 | |
---|
| 310 | void ManagedTypes::handleStruct( const ast::StructDecl * aggregateDecl ) { |
---|
| 311 | // don't construct members, but need to take note if there is a managed member, |
---|
| 312 | // because that means that this type is also managed |
---|
| 313 | for ( auto & member : aggregateDecl->members ) { |
---|
| 314 | if ( auto field = member.as<ast::ObjectDecl>() ) { |
---|
| 315 | if ( isManaged( field ) ) { |
---|
| 316 | // generic parameters should not play a role in determining whether a generic type is constructed - construct all generic types, so that |
---|
| 317 | // polymorphic constructors make generic types managed types |
---|
| 318 | ast::StructInstType inst( aggregateDecl ); |
---|
| 319 | managedTypes.insert( Mangle::mangle( &inst, {Mangle::NoOverrideable | Mangle::NoGenericParams | Mangle::Type} ) ); |
---|
| 320 | break; |
---|
[16ba4a6f] | 321 | } |
---|
| 322 | } |
---|
| 323 | } |
---|
[8984003] | 324 | } |
---|
[16ba4a6f] | 325 | |
---|
[8984003] | 326 | void ManagedTypes::beginScope() { managedTypes.beginScope(); } |
---|
| 327 | void ManagedTypes::endScope() { managedTypes.endScope(); } |
---|
[16ba4a6f] | 328 | |
---|
[4e2f1b2] | 329 | const ast::Stmt * genCtorDtor( const CodeLocation & loc, const std::string & fname, const ast::ObjectDecl * objDecl, const ast::Expr * arg ) { |
---|
[8984003] | 330 | assertf(objDecl, "genCtorDtor passed null objDecl"); |
---|
| 331 | InitExpander srcParam(arg); |
---|
| 332 | return SymTab::genImplicitCall(srcParam, new ast::VariableExpr(loc, objDecl), loc, fname, objDecl); |
---|
| 333 | } |
---|
[092528b] | 334 | |
---|
[b8524ca] | 335 | ast::ConstructorInit * genCtorInit( const CodeLocation & loc, const ast::ObjectDecl * objDecl ) { |
---|
[c19edd1] | 336 | // call into genImplicitCall from Autogen.h to generate calls to ctor/dtor for each |
---|
[b8524ca] | 337 | // constructable object |
---|
[0bd3faf] | 338 | InitExpander srcParam{ objDecl->init }, nullParam{ (const ast::Init *)nullptr }; |
---|
[16ba4a6f] | 339 | ast::ptr< ast::Expr > dstParam = new ast::VariableExpr(loc, objDecl); |
---|
[c19edd1] | 340 | |
---|
| 341 | ast::ptr< ast::Stmt > ctor = SymTab::genImplicitCall( |
---|
[16ba4a6f] | 342 | srcParam, dstParam, loc, "?{}", objDecl ); |
---|
[c19edd1] | 343 | ast::ptr< ast::Stmt > dtor = SymTab::genImplicitCall( |
---|
| 344 | nullParam, dstParam, loc, "^?{}", objDecl, |
---|
[b8524ca] | 345 | SymTab::LoopBackward ); |
---|
[c19edd1] | 346 | |
---|
[b8524ca] | 347 | // check that either both ctor and dtor are present, or neither |
---|
| 348 | assert( (bool)ctor == (bool)dtor ); |
---|
| 349 | |
---|
| 350 | if ( ctor ) { |
---|
[c19edd1] | 351 | // need to remember init expression, in case no ctors exist. If ctor does exist, want to |
---|
[b8524ca] | 352 | // use ctor expression instead of init. |
---|
[c19edd1] | 353 | ctor.strict_as< ast::ImplicitCtorDtorStmt >(); |
---|
[b8524ca] | 354 | dtor.strict_as< ast::ImplicitCtorDtorStmt >(); |
---|
| 355 | |
---|
| 356 | return new ast::ConstructorInit{ loc, ctor, dtor, objDecl->init }; |
---|
| 357 | } |
---|
| 358 | |
---|
[234b1cb] | 359 | return nullptr; |
---|
| 360 | } |
---|
| 361 | |
---|
[42e2ad7] | 362 | } // namespace InitTweak |
---|
| 363 | |
---|
[51587aa] | 364 | // Local Variables: // |
---|
| 365 | // tab-width: 4 // |
---|
| 366 | // mode: c++ // |
---|
| 367 | // compile-command: "make install" // |
---|
| 368 | // End: // |
---|