[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/PassVisitor.h" // for PassVisitor, WithGuards, WithShort... |
---|
| 32 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 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" |
---|
[2bfc6b2] | 39 | #include "SymTab/Autogen.h" // for genImplicitCall |
---|
| 40 | #include "SymTab/Mangler.h" // for Mangler |
---|
[07de76b] | 41 | #include "SynTree/LinkageSpec.h" // for isOverridable, C |
---|
[2bfc6b2] | 42 | #include "SynTree/Declaration.h" // for ObjectDecl, DeclarationWithType |
---|
| 43 | #include "SynTree/Expression.h" // for VariableExpr, UntypedExpr, Address... |
---|
| 44 | #include "SynTree/Initializer.h" // for ConstructorInit, SingleInit, Initi... |
---|
| 45 | #include "SynTree/Label.h" // for Label |
---|
| 46 | #include "SynTree/Mutator.h" // for mutateAll |
---|
| 47 | #include "SynTree/Statement.h" // for CompoundStmt, ImplicitCtorDtorStmt |
---|
| 48 | #include "SynTree/Type.h" // for Type, ArrayType, Type::Qualifiers |
---|
| 49 | #include "SynTree/Visitor.h" // for acceptAll, maybeAccept |
---|
| 50 | #include "Tuples/Tuples.h" // for maybeImpure |
---|
| 51 | #include "Validate/FindSpecialDecls.h" // for SizeType |
---|
[42e2ad7] | 52 | |
---|
| 53 | namespace InitTweak { |
---|
[a08ba92] | 54 | namespace { |
---|
| 55 | const std::list<Label> noLabels; |
---|
[5b40f30] | 56 | const std::list<Expression *> noDesignators; |
---|
[a08ba92] | 57 | } |
---|
[1e9d87b] | 58 | |
---|
[d24d4e1] | 59 | struct ReturnFixer : public WithStmtsToAdd, public WithGuards { |
---|
[a0fdbd5] | 60 | /// consistently allocates a temporary variable for the return value |
---|
| 61 | /// of a function so that anything which the resolver decides can be constructed |
---|
[02c7d04] | 62 | /// into the return type of a function can be returned. |
---|
[a0fdbd5] | 63 | static void makeReturnTemp( std::list< Declaration * > &translationUnit ); |
---|
[02c7d04] | 64 | |
---|
[7b6ca2e] | 65 | void premutate( FunctionDecl *functionDecl ); |
---|
| 66 | void premutate( ReturnStmt * returnStmt ); |
---|
[1e9d87b] | 67 | |
---|
| 68 | protected: |
---|
[c6976ba] | 69 | FunctionType * ftype = nullptr; |
---|
[cf16f94] | 70 | std::string funcName; |
---|
| 71 | }; |
---|
[42e2ad7] | 72 | |
---|
[22bc276] | 73 | struct CtorDtor : public WithGuards, public WithShortCircuiting, public WithVisitorRef<CtorDtor> { |
---|
[02c7d04] | 74 | /// create constructor and destructor statements for object declarations. |
---|
[5f98ce5] | 75 | /// the actual call statements will be added in after the resolver has run |
---|
| 76 | /// so that the initializer expression is only removed if a constructor is found |
---|
| 77 | /// and the same destructor call is inserted in all of the appropriate locations. |
---|
[02c7d04] | 78 | static void generateCtorDtor( std::list< Declaration * > &translationUnit ); |
---|
[974906e2] | 79 | |
---|
[d24d4e1] | 80 | void previsit( ObjectDecl * ); |
---|
| 81 | void previsit( FunctionDecl *functionDecl ); |
---|
| 82 | |
---|
[5f98ce5] | 83 | // should not traverse into any of these declarations to find objects |
---|
| 84 | // that need to be constructed or destructed |
---|
[d24d4e1] | 85 | void previsit( StructDecl *aggregateDecl ); |
---|
[aec3e6b] | 86 | void previsit( AggregateDecl * ) { visit_children = false; } |
---|
| 87 | void previsit( NamedTypeDecl * ) { visit_children = false; } |
---|
[22bc276] | 88 | void previsit( FunctionType * ) { visit_children = false; } |
---|
[db4ecc5] | 89 | |
---|
[d24d4e1] | 90 | void previsit( CompoundStmt * compoundStmt ); |
---|
[1ba88a0] | 91 | |
---|
| 92 | private: |
---|
| 93 | // set of mangled type names for which a constructor or destructor exists in the current scope. |
---|
| 94 | // these types require a ConstructorInit node to be generated, anything else is a POD type and thus |
---|
| 95 | // should not have a ConstructorInit generated. |
---|
| 96 | |
---|
[bfd4974] | 97 | ManagedTypes managedTypes; |
---|
[1ba88a0] | 98 | bool inFunction = false; |
---|
[974906e2] | 99 | }; |
---|
| 100 | |
---|
[fdd0509] | 101 | struct HoistArrayDimension final : public WithDeclsToAdd, public WithShortCircuiting, public WithGuards, public WithIndexer { |
---|
[5f98ce5] | 102 | /// hoist dimension from array types in object declaration so that it uses a single |
---|
| 103 | /// const variable of type size_t, so that side effecting array dimensions are only |
---|
| 104 | /// computed once. |
---|
| 105 | static void hoistArrayDimension( std::list< Declaration * > & translationUnit ); |
---|
| 106 | |
---|
[22bc276] | 107 | void premutate( ObjectDecl * objectDecl ); |
---|
| 108 | DeclarationWithType * postmutate( ObjectDecl * objectDecl ); |
---|
| 109 | void premutate( FunctionDecl *functionDecl ); |
---|
[5f98ce5] | 110 | // should not traverse into any of these declarations to find objects |
---|
| 111 | // that need to be constructed or destructed |
---|
[22bc276] | 112 | void premutate( AggregateDecl * ) { visit_children = false; } |
---|
| 113 | void premutate( NamedTypeDecl * ) { visit_children = false; } |
---|
| 114 | void premutate( FunctionType * ) { visit_children = false; } |
---|
[5f98ce5] | 115 | |
---|
[fdd0509] | 116 | // need this so that enumerators are added to the indexer, due to premutate(AggregateDecl *) |
---|
| 117 | void premutate( EnumDecl * ) {} |
---|
| 118 | |
---|
[5f98ce5] | 119 | void hoist( Type * type ); |
---|
| 120 | |
---|
[68fe077a] | 121 | Type::StorageClasses storageClasses; |
---|
[40e636a] | 122 | bool inFunction = false; |
---|
[5f98ce5] | 123 | }; |
---|
| 124 | |
---|
[09867ec] | 125 | struct HoistArrayDimension_NoResolve final : public WithDeclsToAdd, public WithShortCircuiting, public WithGuards { |
---|
| 126 | /// hoist dimension from array types in object declaration so that it uses a single |
---|
| 127 | /// const variable of type size_t, so that side effecting array dimensions are only |
---|
| 128 | /// computed once. |
---|
| 129 | static void hoistArrayDimension( std::list< Declaration * > & translationUnit ); |
---|
| 130 | |
---|
| 131 | void premutate( ObjectDecl * objectDecl ); |
---|
| 132 | DeclarationWithType * postmutate( ObjectDecl * objectDecl ); |
---|
| 133 | void premutate( FunctionDecl *functionDecl ); |
---|
| 134 | // should not traverse into any of these declarations to find objects |
---|
| 135 | // that need to be constructed or destructed |
---|
| 136 | void premutate( AggregateDecl * ) { visit_children = false; } |
---|
| 137 | void premutate( NamedTypeDecl * ) { visit_children = false; } |
---|
| 138 | void premutate( FunctionType * ) { visit_children = false; } |
---|
| 139 | |
---|
| 140 | void hoist( Type * type ); |
---|
| 141 | |
---|
| 142 | Type::StorageClasses storageClasses; |
---|
| 143 | bool inFunction = false; |
---|
| 144 | }; |
---|
| 145 | |
---|
[a0fdbd5] | 146 | void genInit( std::list< Declaration * > & translationUnit ) { |
---|
[09867ec] | 147 | if (!useNewAST) { |
---|
| 148 | HoistArrayDimension::hoistArrayDimension( translationUnit ); |
---|
| 149 | } |
---|
| 150 | else { |
---|
| 151 | HoistArrayDimension_NoResolve::hoistArrayDimension( translationUnit ); |
---|
| 152 | } |
---|
[16ba4a6f] | 153 | fixReturnStatements( translationUnit ); |
---|
| 154 | |
---|
| 155 | if (!useNewAST) { |
---|
| 156 | CtorDtor::generateCtorDtor( translationUnit ); |
---|
| 157 | } |
---|
[02c7d04] | 158 | } |
---|
| 159 | |
---|
[8b11840] | 160 | void fixReturnStatements( std::list< Declaration * > & translationUnit ) { |
---|
[7b6ca2e] | 161 | PassVisitor<ReturnFixer> fixer; |
---|
[a0fdbd5] | 162 | mutateAll( translationUnit, fixer ); |
---|
[a08ba92] | 163 | } |
---|
[42e2ad7] | 164 | |
---|
[7b6ca2e] | 165 | void ReturnFixer::premutate( ReturnStmt *returnStmt ) { |
---|
[65660bd] | 166 | std::list< DeclarationWithType * > & returnVals = ftype->get_returnVals(); |
---|
[cf16f94] | 167 | assert( returnVals.size() == 0 || returnVals.size() == 1 ); |
---|
[c6976ba] | 168 | // hands off if the function returns a reference - we don't want to allocate a temporary if a variable's address |
---|
[cf16f94] | 169 | // is being returned |
---|
[bd7e609] | 170 | if ( returnStmt->expr && returnVals.size() == 1 && isConstructable( returnVals.front()->get_type() ) ) { |
---|
[cce9429] | 171 | // explicitly construct the return value using the return expression and the retVal object |
---|
[18ca28e] | 172 | assertf( returnVals.front()->name != "", "Function %s has unnamed return value\n", funcName.c_str() ); |
---|
[c6976ba] | 173 | |
---|
[bd7e609] | 174 | ObjectDecl * retVal = strict_dynamic_cast< ObjectDecl * >( returnVals.front() ); |
---|
| 175 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( returnStmt->expr ) ) { |
---|
| 176 | // return statement has already been mutated - don't need to do it again |
---|
| 177 | if ( varExpr->var == retVal ) return; |
---|
| 178 | } |
---|
[9fe33947] | 179 | Statement * stmt = genCtorDtor( "?{}", retVal, returnStmt->expr ); |
---|
| 180 | assertf( stmt, "ReturnFixer: genCtorDtor returned nullptr: %s / %s", toString( retVal ).c_str(), toString( returnStmt->expr ).c_str() ); |
---|
| 181 | stmtsToAddBefore.push_back( stmt ); |
---|
[cf16f94] | 182 | |
---|
[cce9429] | 183 | // return the retVal object |
---|
[18ca28e] | 184 | returnStmt->expr = new VariableExpr( returnVals.front() ); |
---|
[cf16f94] | 185 | } // if |
---|
| 186 | } |
---|
| 187 | |
---|
[7b6ca2e] | 188 | void ReturnFixer::premutate( FunctionDecl *functionDecl ) { |
---|
[4eb31f2b] | 189 | GuardValue( ftype ); |
---|
| 190 | GuardValue( funcName ); |
---|
[1e9d87b] | 191 | |
---|
[22bc276] | 192 | ftype = functionDecl->type; |
---|
| 193 | funcName = functionDecl->name; |
---|
[cf16f94] | 194 | } |
---|
[974906e2] | 195 | |
---|
[4d2434a] | 196 | // precompute array dimension expression, because constructor generation may duplicate it, |
---|
| 197 | // which would be incorrect if it is a side-effecting computation. |
---|
[5f98ce5] | 198 | void HoistArrayDimension::hoistArrayDimension( std::list< Declaration * > & translationUnit ) { |
---|
[22bc276] | 199 | PassVisitor<HoistArrayDimension> hoister; |
---|
| 200 | mutateAll( translationUnit, hoister ); |
---|
[5f98ce5] | 201 | } |
---|
| 202 | |
---|
[22bc276] | 203 | void HoistArrayDimension::premutate( ObjectDecl * objectDecl ) { |
---|
| 204 | GuardValue( storageClasses ); |
---|
[a7c90d4] | 205 | storageClasses = objectDecl->get_storageClasses(); |
---|
[22bc276] | 206 | } |
---|
| 207 | |
---|
| 208 | DeclarationWithType * HoistArrayDimension::postmutate( ObjectDecl * objectDecl ) { |
---|
[5f98ce5] | 209 | hoist( objectDecl->get_type() ); |
---|
[22bc276] | 210 | return objectDecl; |
---|
[5f98ce5] | 211 | } |
---|
| 212 | |
---|
| 213 | void HoistArrayDimension::hoist( Type * type ) { |
---|
[40e636a] | 214 | // if in function, generate const size_t var |
---|
[5f98ce5] | 215 | static UniqueName dimensionName( "_array_dim" ); |
---|
[40e636a] | 216 | |
---|
[a7c90d4] | 217 | // C doesn't allow variable sized arrays at global scope or for static variables, so don't hoist dimension. |
---|
[f9cebb5] | 218 | if ( ! inFunction ) return; |
---|
[08d5507b] | 219 | if ( storageClasses.is_static ) return; |
---|
[f9cebb5] | 220 | |
---|
| 221 | if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) { |
---|
[5f98ce5] | 222 | if ( ! arrayType->get_dimension() ) return; // xxx - recursive call to hoist? |
---|
| 223 | |
---|
[fdd0509] | 224 | // need to resolve array dimensions in order to accurately determine if constexpr |
---|
[09867ec] | 225 | ResolvExpr::findSingleExpression( arrayType->dimension, Validate::SizeType->clone(), indexer ); |
---|
| 226 | // array is variable-length when the dimension is not constexpr |
---|
| 227 | arrayType->isVarLen = ! isConstExpr( arrayType->dimension ); |
---|
[300d75b] | 228 | // don't need to hoist dimension if it's definitely pure - only need to if there's potential for side effects. |
---|
[5465377c] | 229 | // xxx - hoisting has no side effects anyways, so don't skip since we delay resolve |
---|
| 230 | // still try to detect constant expressions |
---|
| 231 | if ( ! Tuples::maybeImpure( arrayType->dimension ) ) return; |
---|
[5f98ce5] | 232 | |
---|
[2bfc6b2] | 233 | ObjectDecl * arrayDimension = new ObjectDecl( dimensionName.newName(), storageClasses, LinkageSpec::C, 0, Validate::SizeType->clone(), new SingleInit( arrayType->get_dimension() ) ); |
---|
[615a096] | 234 | arrayDimension->get_type()->set_const( true ); |
---|
[5f98ce5] | 235 | |
---|
| 236 | arrayType->set_dimension( new VariableExpr( arrayDimension ) ); |
---|
[22bc276] | 237 | declsToAddBefore.push_back( arrayDimension ); |
---|
[5f98ce5] | 238 | |
---|
| 239 | hoist( arrayType->get_base() ); |
---|
| 240 | return; |
---|
| 241 | } |
---|
| 242 | } |
---|
[974906e2] | 243 | |
---|
[22bc276] | 244 | void HoistArrayDimension::premutate( FunctionDecl * ) { |
---|
| 245 | GuardValue( inFunction ); |
---|
[fdd0509] | 246 | inFunction = true; |
---|
[40e636a] | 247 | } |
---|
| 248 | |
---|
[09867ec] | 249 | // precompute array dimension expression, because constructor generation may duplicate it, |
---|
| 250 | // which would be incorrect if it is a side-effecting computation. |
---|
| 251 | void HoistArrayDimension_NoResolve::hoistArrayDimension( std::list< Declaration * > & translationUnit ) { |
---|
| 252 | PassVisitor<HoistArrayDimension_NoResolve> hoister; |
---|
| 253 | mutateAll( translationUnit, hoister ); |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | void HoistArrayDimension_NoResolve::premutate( ObjectDecl * objectDecl ) { |
---|
| 257 | GuardValue( storageClasses ); |
---|
| 258 | storageClasses = objectDecl->get_storageClasses(); |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | DeclarationWithType * HoistArrayDimension_NoResolve::postmutate( ObjectDecl * objectDecl ) { |
---|
| 262 | hoist( objectDecl->get_type() ); |
---|
| 263 | return objectDecl; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | void HoistArrayDimension_NoResolve::hoist( Type * type ) { |
---|
| 267 | // if in function, generate const size_t var |
---|
| 268 | static UniqueName dimensionName( "_array_dim" ); |
---|
| 269 | |
---|
| 270 | // C doesn't allow variable sized arrays at global scope or for static variables, so don't hoist dimension. |
---|
| 271 | if ( ! inFunction ) return; |
---|
| 272 | if ( storageClasses.is_static ) return; |
---|
| 273 | |
---|
| 274 | if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) { |
---|
| 275 | if ( ! arrayType->get_dimension() ) return; // xxx - recursive call to hoist? |
---|
| 276 | // don't need to hoist dimension if it's definitely pure - only need to if there's potential for side effects. |
---|
| 277 | // xxx - hoisting has no side effects anyways, so don't skip since we delay resolve |
---|
| 278 | // still try to detect constant expressions |
---|
| 279 | if ( ! Tuples::maybeImpure( arrayType->dimension ) ) return; |
---|
| 280 | |
---|
| 281 | ObjectDecl * arrayDimension = new ObjectDecl( dimensionName.newName(), storageClasses, LinkageSpec::C, 0, Validate::SizeType->clone(), new SingleInit( arrayType->get_dimension() ) ); |
---|
| 282 | arrayDimension->get_type()->set_const( true ); |
---|
| 283 | |
---|
| 284 | arrayType->set_dimension( new VariableExpr( arrayDimension ) ); |
---|
| 285 | declsToAddBefore.push_back( arrayDimension ); |
---|
| 286 | |
---|
| 287 | hoist( arrayType->get_base() ); |
---|
| 288 | return; |
---|
| 289 | } |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | void HoistArrayDimension_NoResolve::premutate( FunctionDecl * ) { |
---|
| 293 | GuardValue( inFunction ); |
---|
| 294 | inFunction = true; |
---|
| 295 | } |
---|
| 296 | |
---|
[a36eb2d] | 297 | namespace { |
---|
| 298 | |
---|
| 299 | # warning Remove the _New suffix after the conversion is complete. |
---|
| 300 | struct HoistArrayDimension_NoResolve_New final : |
---|
| 301 | public ast::WithDeclsToAdd<>, public ast::WithShortCircuiting, |
---|
[c600df1] | 302 | public ast::WithGuards, public ast::WithConstTranslationUnit, |
---|
[a36eb2d] | 303 | public ast::WithVisitorRef<HoistArrayDimension_NoResolve_New> { |
---|
| 304 | void previsit( const ast::ObjectDecl * decl ); |
---|
| 305 | const ast::DeclWithType * postvisit( const ast::ObjectDecl * decl ); |
---|
| 306 | // Do not look for objects inside there declarations (and type). |
---|
| 307 | void previsit( const ast::AggregateDecl * ) { visit_children = false; } |
---|
| 308 | void previsit( const ast::NamedTypeDecl * ) { visit_children = false; } |
---|
| 309 | void previsit( const ast::FunctionType * ) { visit_children = false; } |
---|
| 310 | |
---|
| 311 | const ast::Type * hoist( const ast::Type * type ); |
---|
| 312 | |
---|
| 313 | ast::Storage::Classes storageClasses; |
---|
| 314 | }; |
---|
| 315 | |
---|
| 316 | void HoistArrayDimension_NoResolve_New::previsit( |
---|
| 317 | const ast::ObjectDecl * decl ) { |
---|
[148ba7d] | 318 | GuardValue( storageClasses ) = decl->storage; |
---|
[a36eb2d] | 319 | } |
---|
| 320 | |
---|
| 321 | const ast::DeclWithType * HoistArrayDimension_NoResolve_New::postvisit( |
---|
| 322 | const ast::ObjectDecl * objectDecl ) { |
---|
| 323 | return mutate_field( objectDecl, &ast::ObjectDecl::type, |
---|
| 324 | hoist( objectDecl->type ) ); |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | const ast::Type * HoistArrayDimension_NoResolve_New::hoist( |
---|
| 328 | const ast::Type * type ) { |
---|
| 329 | static UniqueName dimensionName( "_array_dim" ); |
---|
| 330 | |
---|
| 331 | if ( !isInFunction() || storageClasses.is_static ) { |
---|
| 332 | return type; |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | if ( auto arrayType = dynamic_cast< const ast::ArrayType * >( type ) ) { |
---|
| 336 | if ( nullptr == arrayType->dimension ) { |
---|
| 337 | return type; |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | if ( !Tuples::maybeImpure( arrayType->dimension ) ) { |
---|
| 341 | return type; |
---|
| 342 | } |
---|
| 343 | |
---|
[c600df1] | 344 | ast::ptr<ast::Type> dimType = transUnit().global.sizeType; |
---|
[a36eb2d] | 345 | assert( dimType ); |
---|
| 346 | add_qualifiers( dimType, ast::CV::Qualifiers( ast::CV::Const ) ); |
---|
| 347 | |
---|
| 348 | ast::ObjectDecl * arrayDimension = new ast::ObjectDecl( |
---|
| 349 | arrayType->dimension->location, |
---|
| 350 | dimensionName.newName(), |
---|
| 351 | dimType, |
---|
| 352 | new ast::SingleInit( |
---|
| 353 | arrayType->dimension->location, |
---|
| 354 | arrayType->dimension |
---|
| 355 | ) |
---|
| 356 | ); |
---|
| 357 | |
---|
| 358 | ast::ArrayType * mutType = ast::mutate( arrayType ); |
---|
| 359 | mutType->dimension = new ast::VariableExpr( |
---|
| 360 | arrayDimension->location, arrayDimension ); |
---|
| 361 | declsToAddBefore.push_back( arrayDimension ); |
---|
| 362 | |
---|
| 363 | mutType->base = hoist( mutType->base ); |
---|
| 364 | return mutType; |
---|
| 365 | } |
---|
| 366 | return type; |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | struct ReturnFixer_New final : |
---|
[fc134a48] | 370 | public ast::WithStmtsToAdd<>, ast::WithGuards, ast::WithShortCircuiting { |
---|
[a36eb2d] | 371 | void previsit( const ast::FunctionDecl * decl ); |
---|
| 372 | const ast::ReturnStmt * previsit( const ast::ReturnStmt * stmt ); |
---|
| 373 | private: |
---|
| 374 | const ast::FunctionDecl * funcDecl = nullptr; |
---|
| 375 | }; |
---|
| 376 | |
---|
| 377 | void ReturnFixer_New::previsit( const ast::FunctionDecl * decl ) { |
---|
[fc134a48] | 378 | if (decl->linkage == ast::Linkage::Intrinsic) visit_children = false; |
---|
[148ba7d] | 379 | GuardValue( funcDecl ) = decl; |
---|
[a36eb2d] | 380 | } |
---|
| 381 | |
---|
| 382 | const ast::ReturnStmt * ReturnFixer_New::previsit( |
---|
| 383 | const ast::ReturnStmt * stmt ) { |
---|
| 384 | auto & returns = funcDecl->returns; |
---|
| 385 | assert( returns.size() < 2 ); |
---|
| 386 | // Hands off if the function returns a reference. |
---|
| 387 | // Don't allocate a temporary if the address is returned. |
---|
| 388 | if ( stmt->expr && 1 == returns.size() ) { |
---|
| 389 | ast::ptr<ast::DeclWithType> retDecl = returns.front(); |
---|
| 390 | if ( isConstructable( retDecl->get_type() ) ) { |
---|
| 391 | // Explicitly construct the return value using the return |
---|
| 392 | // expression and the retVal object. |
---|
| 393 | assertf( "" != retDecl->name, |
---|
| 394 | "Function %s has unnamed return value.\n", |
---|
| 395 | funcDecl->name.c_str() ); |
---|
| 396 | |
---|
| 397 | auto retVal = retDecl.strict_as<ast::ObjectDecl>(); |
---|
| 398 | if ( auto varExpr = stmt->expr.as<ast::VariableExpr>() ) { |
---|
| 399 | // Check if the return statement is already set up. |
---|
| 400 | if ( varExpr->var == retVal ) return stmt; |
---|
| 401 | } |
---|
| 402 | ast::ptr<ast::Stmt> ctorStmt = genCtorDtor( |
---|
| 403 | retVal->location, "?{}", retVal, stmt->expr ); |
---|
| 404 | assertf( ctorStmt, |
---|
[4ec9513] | 405 | "ReturnFixer: genCtorDtor returned nullptr: %s / %s", |
---|
[a36eb2d] | 406 | toString( retVal ).c_str(), |
---|
| 407 | toString( stmt->expr ).c_str() ); |
---|
[4ec9513] | 408 | stmtsToAddBefore.push_back( ctorStmt ); |
---|
[a36eb2d] | 409 | |
---|
| 410 | // Return the retVal object. |
---|
| 411 | ast::ReturnStmt * mutStmt = ast::mutate( stmt ); |
---|
| 412 | mutStmt->expr = new ast::VariableExpr( |
---|
| 413 | stmt->location, retDecl ); |
---|
| 414 | return mutStmt; |
---|
| 415 | } |
---|
| 416 | } |
---|
| 417 | return stmt; |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | } // namespace |
---|
| 421 | |
---|
| 422 | void genInit( ast::TranslationUnit & transUnit ) { |
---|
| 423 | ast::Pass<HoistArrayDimension_NoResolve_New>::run( transUnit ); |
---|
| 424 | ast::Pass<ReturnFixer_New>::run( transUnit ); |
---|
| 425 | } |
---|
| 426 | |
---|
[4ec9513] | 427 | void fixReturnStatements( ast::TranslationUnit & transUnit ) { |
---|
| 428 | ast::Pass<ReturnFixer_New>::run( transUnit ); |
---|
| 429 | } |
---|
| 430 | |
---|
[02c7d04] | 431 | void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) { |
---|
[d24d4e1] | 432 | PassVisitor<CtorDtor> ctordtor; |
---|
| 433 | acceptAll( translationUnit, ctordtor ); |
---|
[974906e2] | 434 | } |
---|
| 435 | |
---|
[bfd4974] | 436 | bool ManagedTypes::isManaged( Type * type ) const { |
---|
[22bc276] | 437 | // references are never constructed |
---|
[c6976ba] | 438 | if ( dynamic_cast< ReferenceType * >( type ) ) return false; |
---|
[0b465a5] | 439 | // need to clear and reset qualifiers when determining if a type is managed |
---|
| 440 | ValueGuard< Type::Qualifiers > qualifiers( type->get_qualifiers() ); |
---|
| 441 | type->get_qualifiers() = Type::Qualifiers(); |
---|
[ac9ca96] | 442 | if ( TupleType * tupleType = dynamic_cast< TupleType * > ( type ) ) { |
---|
| 443 | // tuple is also managed if any of its components are managed |
---|
[22bc276] | 444 | if ( std::any_of( tupleType->types.begin(), tupleType->types.end(), [&](Type * type) { return isManaged( type ); }) ) { |
---|
[ac9ca96] | 445 | return true; |
---|
| 446 | } |
---|
| 447 | } |
---|
[30b65d8] | 448 | // 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) |
---|
[e35f30a] | 449 | return managedTypes.find( SymTab::Mangler::mangleConcrete( type ) ) != managedTypes.end() || GenPoly::isPolyType( type ); |
---|
[65660bd] | 450 | } |
---|
| 451 | |
---|
[bfd4974] | 452 | bool ManagedTypes::isManaged( ObjectDecl * objDecl ) const { |
---|
[1ba88a0] | 453 | Type * type = objDecl->get_type(); |
---|
| 454 | while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) { |
---|
[be9aa0f] | 455 | // must always construct VLAs with an initializer, since this is an error in C |
---|
| 456 | if ( at->isVarLen && objDecl->init ) return true; |
---|
[1ba88a0] | 457 | type = at->get_base(); |
---|
| 458 | } |
---|
[65660bd] | 459 | return isManaged( type ); |
---|
[1ba88a0] | 460 | } |
---|
| 461 | |
---|
[16ba4a6f] | 462 | // why is this not just on FunctionDecl? |
---|
[bfd4974] | 463 | void ManagedTypes::handleDWT( DeclarationWithType * dwt ) { |
---|
[1ba88a0] | 464 | // if this function is a user-defined constructor or destructor, mark down the type as "managed" |
---|
[bff227f] | 465 | if ( ! LinkageSpec::isOverridable( dwt->get_linkage() ) && CodeGen::isCtorDtor( dwt->get_name() ) ) { |
---|
[1ba88a0] | 466 | std::list< DeclarationWithType * > & params = GenPoly::getFunctionType( dwt->get_type() )->get_parameters(); |
---|
| 467 | assert( ! params.empty() ); |
---|
[ce8c12f] | 468 | Type * type = InitTweak::getPointerBase( params.front()->get_type() ); |
---|
| 469 | assert( type ); |
---|
[e35f30a] | 470 | managedTypes.insert( SymTab::Mangler::mangleConcrete( type ) ); |
---|
[1ba88a0] | 471 | } |
---|
| 472 | } |
---|
| 473 | |
---|
[bfd4974] | 474 | void ManagedTypes::handleStruct( StructDecl * aggregateDecl ) { |
---|
| 475 | // don't construct members, but need to take note if there is a managed member, |
---|
| 476 | // because that means that this type is also managed |
---|
| 477 | for ( Declaration * member : aggregateDecl->get_members() ) { |
---|
| 478 | if ( ObjectDecl * field = dynamic_cast< ObjectDecl * >( member ) ) { |
---|
| 479 | if ( isManaged( field ) ) { |
---|
[e35f30a] | 480 | // generic parameters should not play a role in determining whether a generic type is constructed - construct all generic types, so that |
---|
| 481 | // polymorphic constructors make generic types managed types |
---|
[bfd4974] | 482 | StructInstType inst( Type::Qualifiers(), aggregateDecl ); |
---|
[e35f30a] | 483 | managedTypes.insert( SymTab::Mangler::mangleConcrete( &inst ) ); |
---|
[bfd4974] | 484 | break; |
---|
| 485 | } |
---|
| 486 | } |
---|
| 487 | } |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | void ManagedTypes::beginScope() { managedTypes.beginScope(); } |
---|
| 491 | void ManagedTypes::endScope() { managedTypes.endScope(); } |
---|
| 492 | |
---|
[16ba4a6f] | 493 | bool ManagedTypes_new::isManaged( const ast::Type * type ) const { |
---|
| 494 | // references are never constructed |
---|
| 495 | if ( dynamic_cast< const ast::ReferenceType * >( type ) ) return false; |
---|
| 496 | if ( auto tupleType = dynamic_cast< const ast::TupleType * > ( type ) ) { |
---|
| 497 | // tuple is also managed if any of its components are managed |
---|
| 498 | for (auto & component : tupleType->types) { |
---|
| 499 | if (isManaged(component)) return true; |
---|
| 500 | } |
---|
| 501 | } |
---|
| 502 | // need to clear and reset qualifiers when determining if a type is managed |
---|
| 503 | // ValueGuard< Type::Qualifiers > qualifiers( type->get_qualifiers() ); |
---|
| 504 | auto tmp = shallowCopy(type); |
---|
| 505 | tmp->qualifiers = {}; |
---|
| 506 | // delete tmp at return |
---|
| 507 | ast::ptr<ast::Type> guard = tmp; |
---|
| 508 | // 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) |
---|
| 509 | return managedTypes.find( Mangle::mangle( tmp, {Mangle::NoOverrideable | Mangle::NoGenericParams | Mangle::Type} ) ) != managedTypes.end() || GenPoly::isPolyType( tmp ); |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | bool ManagedTypes_new::isManaged( const ast::ObjectDecl * objDecl ) const { |
---|
| 513 | const ast::Type * type = objDecl->type; |
---|
| 514 | while ( auto at = dynamic_cast< const ast::ArrayType * >( type ) ) { |
---|
| 515 | // must always construct VLAs with an initializer, since this is an error in C |
---|
| 516 | if ( at->isVarLen && objDecl->init ) return true; |
---|
| 517 | type = at->base; |
---|
| 518 | } |
---|
| 519 | return isManaged( type ); |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | void ManagedTypes_new::handleDWT( const ast::DeclWithType * dwt ) { |
---|
| 523 | // if this function is a user-defined constructor or destructor, mark down the type as "managed" |
---|
| 524 | if ( ! dwt->linkage.is_overrideable && CodeGen::isCtorDtor( dwt->name ) ) { |
---|
| 525 | auto & params = GenPoly::getFunctionType( dwt->get_type())->params; |
---|
| 526 | assert( ! params.empty() ); |
---|
| 527 | // Type * type = InitTweak::getPointerBase( params.front() ); |
---|
| 528 | // assert( type ); |
---|
| 529 | managedTypes.insert( Mangle::mangle( params.front(), {Mangle::NoOverrideable | Mangle::NoGenericParams | Mangle::Type} ) ); |
---|
| 530 | } |
---|
| 531 | } |
---|
| 532 | |
---|
| 533 | void ManagedTypes_new::handleStruct( const ast::StructDecl * aggregateDecl ) { |
---|
| 534 | // don't construct members, but need to take note if there is a managed member, |
---|
| 535 | // because that means that this type is also managed |
---|
| 536 | for ( auto & member : aggregateDecl->members ) { |
---|
| 537 | if ( auto field = member.as<ast::ObjectDecl>() ) { |
---|
| 538 | if ( isManaged( field ) ) { |
---|
| 539 | // generic parameters should not play a role in determining whether a generic type is constructed - construct all generic types, so that |
---|
| 540 | // polymorphic constructors make generic types managed types |
---|
| 541 | ast::StructInstType inst( aggregateDecl ); |
---|
| 542 | managedTypes.insert( Mangle::mangle( &inst, {Mangle::NoOverrideable | Mangle::NoGenericParams | Mangle::Type} ) ); |
---|
| 543 | break; |
---|
| 544 | } |
---|
| 545 | } |
---|
| 546 | } |
---|
| 547 | } |
---|
| 548 | |
---|
| 549 | void ManagedTypes_new::beginScope() { managedTypes.beginScope(); } |
---|
| 550 | void ManagedTypes_new::endScope() { managedTypes.endScope(); } |
---|
| 551 | |
---|
[092528b] | 552 | ImplicitCtorDtorStmt * genCtorDtor( const std::string & fname, ObjectDecl * objDecl, Expression * arg ) { |
---|
| 553 | // call into genImplicitCall from Autogen.h to generate calls to ctor/dtor |
---|
| 554 | assertf( objDecl, "genCtorDtor passed null objDecl" ); |
---|
| 555 | std::list< Statement * > stmts; |
---|
[b8524ca] | 556 | InitExpander_old srcParam( maybeClone( arg ) ); |
---|
[092528b] | 557 | SymTab::genImplicitCall( srcParam, new VariableExpr( objDecl ), fname, back_inserter( stmts ), objDecl ); |
---|
| 558 | assert( stmts.size() <= 1 ); |
---|
[e3e16bc] | 559 | return stmts.size() == 1 ? strict_dynamic_cast< ImplicitCtorDtorStmt * >( stmts.front() ) : nullptr; |
---|
[490fb92e] | 560 | |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | ast::ptr<ast::Stmt> genCtorDtor (const CodeLocation & loc, const std::string & fname, const ast::ObjectDecl * objDecl, const ast::Expr * arg) { |
---|
| 564 | assertf(objDecl, "genCtorDtor passed null objDecl"); |
---|
| 565 | InitExpander_new srcParam(arg); |
---|
| 566 | return SymTab::genImplicitCall(srcParam, new ast::VariableExpr(loc, objDecl), loc, fname, objDecl); |
---|
[092528b] | 567 | } |
---|
| 568 | |
---|
[f0121d7] | 569 | ConstructorInit * genCtorInit( ObjectDecl * objDecl ) { |
---|
| 570 | // call into genImplicitCall from Autogen.h to generate calls to ctor/dtor |
---|
| 571 | // for each constructable object |
---|
| 572 | std::list< Statement * > ctor; |
---|
| 573 | std::list< Statement * > dtor; |
---|
| 574 | |
---|
[b8524ca] | 575 | InitExpander_old srcParam( objDecl->get_init() ); |
---|
| 576 | InitExpander_old nullParam( (Initializer *)NULL ); |
---|
[f0121d7] | 577 | SymTab::genImplicitCall( srcParam, new VariableExpr( objDecl ), "?{}", back_inserter( ctor ), objDecl ); |
---|
| 578 | SymTab::genImplicitCall( nullParam, new VariableExpr( objDecl ), "^?{}", front_inserter( dtor ), objDecl, false ); |
---|
| 579 | |
---|
| 580 | // Currently genImplicitCall produces a single Statement - a CompoundStmt |
---|
| 581 | // which wraps everything that needs to happen. As such, it's technically |
---|
| 582 | // possible to use a Statement ** in the above calls, but this is inherently |
---|
| 583 | // unsafe, so instead we take the slightly less efficient route, but will be |
---|
| 584 | // immediately informed if somehow the above assumption is broken. In this case, |
---|
| 585 | // we could always wrap the list of statements at this point with a CompoundStmt, |
---|
| 586 | // but it seems reasonable at the moment for this to be done by genImplicitCall |
---|
| 587 | // itself. It is possible that genImplicitCall produces no statements (e.g. if |
---|
| 588 | // an array type does not have a dimension). In this case, it's fine to ignore |
---|
| 589 | // the object for the purposes of construction. |
---|
| 590 | assert( ctor.size() == dtor.size() && ctor.size() <= 1 ); |
---|
| 591 | if ( ctor.size() == 1 ) { |
---|
| 592 | // need to remember init expression, in case no ctors exist |
---|
| 593 | // if ctor does exist, want to use ctor expression instead of init |
---|
| 594 | // push this decision to the resolver |
---|
| 595 | assert( dynamic_cast< ImplicitCtorDtorStmt * > ( ctor.front() ) && dynamic_cast< ImplicitCtorDtorStmt * > ( dtor.front() ) ); |
---|
| 596 | return new ConstructorInit( ctor.front(), dtor.front(), objDecl->get_init() ); |
---|
| 597 | } |
---|
| 598 | return nullptr; |
---|
| 599 | } |
---|
| 600 | |
---|
[d24d4e1] | 601 | void CtorDtor::previsit( ObjectDecl * objDecl ) { |
---|
[bfd4974] | 602 | managedTypes.handleDWT( objDecl ); |
---|
[1ba88a0] | 603 | // hands off if @=, extern, builtin, etc. |
---|
[a4dd728] | 604 | // even if unmanaged, try to construct global or static if initializer is not constexpr, since this is not legal C |
---|
[bfd4974] | 605 | if ( tryConstruct( objDecl ) && ( managedTypes.isManaged( objDecl ) || ((! inFunction || objDecl->get_storageClasses().is_static ) && ! isConstExpr( objDecl->get_init() ) ) ) ) { |
---|
[1ba88a0] | 606 | // constructed objects cannot be designated |
---|
[a16764a6] | 607 | if ( isDesignated( objDecl->get_init() ) ) SemanticError( objDecl, "Cannot include designations in the initializer for a managed Object. If this is really what you want, then initialize with @=.\n" ); |
---|
[dcd73d1] | 608 | // constructed objects should not have initializers nested too deeply |
---|
[a16764a6] | 609 | if ( ! checkInitDepth( objDecl ) ) SemanticError( objDecl, "Managed object's initializer is too deep " ); |
---|
[1ba88a0] | 610 | |
---|
[f0121d7] | 611 | objDecl->set_init( genCtorInit( objDecl ) ); |
---|
[974906e2] | 612 | } |
---|
| 613 | } |
---|
| 614 | |
---|
[d24d4e1] | 615 | void CtorDtor::previsit( FunctionDecl *functionDecl ) { |
---|
[22bc276] | 616 | visit_children = false; // do not try and construct parameters or forall parameters |
---|
[d24d4e1] | 617 | GuardValue( inFunction ); |
---|
[1ba88a0] | 618 | inFunction = true; |
---|
| 619 | |
---|
[bfd4974] | 620 | managedTypes.handleDWT( functionDecl ); |
---|
[1ba88a0] | 621 | |
---|
[d24d4e1] | 622 | GuardScope( managedTypes ); |
---|
[1ba88a0] | 623 | // go through assertions and recursively add seen ctor/dtors |
---|
[8c49c0e] | 624 | for ( auto & tyDecl : functionDecl->get_functionType()->get_forall() ) { |
---|
[1ba88a0] | 625 | for ( DeclarationWithType *& assertion : tyDecl->get_assertions() ) { |
---|
[bfd4974] | 626 | managedTypes.handleDWT( assertion ); |
---|
[1ba88a0] | 627 | } |
---|
| 628 | } |
---|
| 629 | |
---|
[22bc276] | 630 | maybeAccept( functionDecl->get_statements(), *visitor ); |
---|
[02c7d04] | 631 | } |
---|
[1ba88a0] | 632 | |
---|
[d24d4e1] | 633 | void CtorDtor::previsit( StructDecl *aggregateDecl ) { |
---|
| 634 | visit_children = false; // do not try to construct and destruct aggregate members |
---|
| 635 | |
---|
[bfd4974] | 636 | managedTypes.handleStruct( aggregateDecl ); |
---|
[1ba88a0] | 637 | } |
---|
| 638 | |
---|
[22bc276] | 639 | void CtorDtor::previsit( CompoundStmt * ) { |
---|
[d24d4e1] | 640 | GuardScope( managedTypes ); |
---|
[1ba88a0] | 641 | } |
---|
[234b1cb] | 642 | |
---|
[b8524ca] | 643 | ast::ConstructorInit * genCtorInit( const CodeLocation & loc, const ast::ObjectDecl * objDecl ) { |
---|
[c19edd1] | 644 | // call into genImplicitCall from Autogen.h to generate calls to ctor/dtor for each |
---|
[b8524ca] | 645 | // constructable object |
---|
| 646 | InitExpander_new srcParam{ objDecl->init }, nullParam{ (const ast::Init *)nullptr }; |
---|
[16ba4a6f] | 647 | ast::ptr< ast::Expr > dstParam = new ast::VariableExpr(loc, objDecl); |
---|
[c19edd1] | 648 | |
---|
| 649 | ast::ptr< ast::Stmt > ctor = SymTab::genImplicitCall( |
---|
[16ba4a6f] | 650 | srcParam, dstParam, loc, "?{}", objDecl ); |
---|
[c19edd1] | 651 | ast::ptr< ast::Stmt > dtor = SymTab::genImplicitCall( |
---|
| 652 | nullParam, dstParam, loc, "^?{}", objDecl, |
---|
[b8524ca] | 653 | SymTab::LoopBackward ); |
---|
[c19edd1] | 654 | |
---|
[b8524ca] | 655 | // check that either both ctor and dtor are present, or neither |
---|
| 656 | assert( (bool)ctor == (bool)dtor ); |
---|
| 657 | |
---|
| 658 | if ( ctor ) { |
---|
[c19edd1] | 659 | // need to remember init expression, in case no ctors exist. If ctor does exist, want to |
---|
[b8524ca] | 660 | // use ctor expression instead of init. |
---|
[c19edd1] | 661 | ctor.strict_as< ast::ImplicitCtorDtorStmt >(); |
---|
[b8524ca] | 662 | dtor.strict_as< ast::ImplicitCtorDtorStmt >(); |
---|
| 663 | |
---|
| 664 | return new ast::ConstructorInit{ loc, ctor, dtor, objDecl->init }; |
---|
| 665 | } |
---|
| 666 | |
---|
[234b1cb] | 667 | return nullptr; |
---|
| 668 | } |
---|
| 669 | |
---|
[42e2ad7] | 670 | } // namespace InitTweak |
---|
| 671 | |
---|
[51587aa] | 672 | // Local Variables: // |
---|
| 673 | // tab-width: 4 // |
---|
| 674 | // mode: c++ // |
---|
| 675 | // compile-command: "make install" // |
---|
| 676 | // End: // |
---|