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