[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 | // |
---|
[a0fdbd5] | 7 | // GenInit.cc -- |
---|
[51587aa] | 8 | // |
---|
[cf16f94] | 9 | // Author : Rob Schluntz |
---|
[51587aa] | 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[1e9d87b] | 11 | // Last Modified By : Rob Schluntz |
---|
[7b3f66b] | 12 | // Last Modified On : Fri May 13 11:37:48 2016 |
---|
[d63eeb0] | 13 | // Update Count : 166 |
---|
[51587aa] | 14 | // |
---|
[a08ba92] | 15 | |
---|
[974906e2] | 16 | #include <stack> |
---|
| 17 | #include <list> |
---|
[a0fdbd5] | 18 | #include "GenInit.h" |
---|
[7b3f66b] | 19 | #include "InitTweak.h" |
---|
[42e2ad7] | 20 | #include "SynTree/Declaration.h" |
---|
| 21 | #include "SynTree/Type.h" |
---|
| 22 | #include "SynTree/Expression.h" |
---|
| 23 | #include "SynTree/Statement.h" |
---|
| 24 | #include "SynTree/Initializer.h" |
---|
| 25 | #include "SynTree/Mutator.h" |
---|
[620cb95] | 26 | #include "SymTab/Autogen.h" |
---|
[1e9d87b] | 27 | #include "GenPoly/PolyMutator.h" |
---|
[42e2ad7] | 28 | |
---|
| 29 | namespace InitTweak { |
---|
[a08ba92] | 30 | namespace { |
---|
| 31 | const std::list<Label> noLabels; |
---|
[5b40f30] | 32 | const std::list<Expression *> noDesignators; |
---|
[a08ba92] | 33 | } |
---|
[1e9d87b] | 34 | |
---|
[a0fdbd5] | 35 | class ReturnFixer : public GenPoly::PolyMutator { |
---|
[cf16f94] | 36 | public: |
---|
[a0fdbd5] | 37 | /// consistently allocates a temporary variable for the return value |
---|
| 38 | /// of a function so that anything which the resolver decides can be constructed |
---|
[02c7d04] | 39 | /// into the return type of a function can be returned. |
---|
[a0fdbd5] | 40 | static void makeReturnTemp( std::list< Declaration * > &translationUnit ); |
---|
[02c7d04] | 41 | |
---|
[a0fdbd5] | 42 | ReturnFixer(); |
---|
[db4ecc5] | 43 | |
---|
[cf16f94] | 44 | virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ); |
---|
| 45 | |
---|
| 46 | virtual Statement * mutate( ReturnStmt * returnStmt ); |
---|
[1e9d87b] | 47 | |
---|
| 48 | protected: |
---|
[cf16f94] | 49 | std::list<DeclarationWithType*> returnVals; |
---|
| 50 | UniqueName tempNamer; |
---|
| 51 | std::string funcName; |
---|
| 52 | }; |
---|
[42e2ad7] | 53 | |
---|
[974906e2] | 54 | class CtorDtor : public GenPoly::PolyMutator { |
---|
| 55 | public: |
---|
[02c7d04] | 56 | /// create constructor and destructor statements for object declarations. |
---|
| 57 | /// Destructors are inserted directly into the code, whereas constructors |
---|
| 58 | /// will be added in after the resolver has run so that the initializer expression |
---|
| 59 | /// is only removed if a constructor is found |
---|
| 60 | static void generateCtorDtor( std::list< Declaration * > &translationUnit ); |
---|
[974906e2] | 61 | |
---|
[f1e012b] | 62 | CtorDtor() : inFunction( false ) {} |
---|
| 63 | |
---|
[db4ecc5] | 64 | virtual DeclarationWithType * mutate( ObjectDecl * ); |
---|
[02c7d04] | 65 | virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ); |
---|
| 66 | virtual Declaration* mutate( StructDecl *aggregateDecl ); |
---|
| 67 | virtual Declaration* mutate( UnionDecl *aggregateDecl ); |
---|
| 68 | virtual Declaration* mutate( EnumDecl *aggregateDecl ); |
---|
[a5a71d0] | 69 | virtual Declaration* mutate( TraitDecl *aggregateDecl ); |
---|
[02c7d04] | 70 | virtual TypeDecl* mutate( TypeDecl *typeDecl ); |
---|
| 71 | virtual Declaration* mutate( TypedefDecl *typeDecl ); |
---|
[974906e2] | 72 | |
---|
[db4ecc5] | 73 | virtual Type * mutate( FunctionType *funcType ); |
---|
| 74 | |
---|
[974906e2] | 75 | protected: |
---|
[f1e012b] | 76 | bool inFunction; |
---|
[974906e2] | 77 | }; |
---|
| 78 | |
---|
[a0fdbd5] | 79 | void genInit( std::list< Declaration * > & translationUnit ) { |
---|
| 80 | ReturnFixer::makeReturnTemp( translationUnit ); |
---|
[02c7d04] | 81 | CtorDtor::generateCtorDtor( translationUnit ); |
---|
| 82 | } |
---|
| 83 | |
---|
[a0fdbd5] | 84 | void ReturnFixer::makeReturnTemp( std::list< Declaration * > & translationUnit ) { |
---|
| 85 | ReturnFixer fixer; |
---|
| 86 | mutateAll( translationUnit, fixer ); |
---|
[a08ba92] | 87 | } |
---|
[42e2ad7] | 88 | |
---|
[a0fdbd5] | 89 | ReturnFixer::ReturnFixer() : tempNamer( "_retVal" ) {} |
---|
[cf16f94] | 90 | |
---|
[a0fdbd5] | 91 | Statement *ReturnFixer::mutate( ReturnStmt *returnStmt ) { |
---|
[cf16f94] | 92 | // update for multiple return values |
---|
| 93 | assert( returnVals.size() == 0 || returnVals.size() == 1 ); |
---|
| 94 | // hands off if the function returns an lvalue - we don't want to allocate a temporary if a variable's address |
---|
| 95 | // is being returned |
---|
[7eabc25] | 96 | if ( returnStmt->get_expr() && returnVals.size() == 1 && funcName != "?=?" && ! returnVals.front()->get_type()->get_isLvalue() ) { |
---|
[5b40f30] | 97 | // ensure return value is not destructed by explicitly creating |
---|
| 98 | // an empty SingleInit node wherein maybeConstruct is false |
---|
| 99 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, returnVals.front()->get_type()->clone(), new ListInit( std::list<Initializer*>(), noDesignators, false ) ); |
---|
[1e9d87b] | 100 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) ); |
---|
| 101 | |
---|
[5b40f30] | 102 | // and explicitly create the constructor expression separately |
---|
| 103 | UntypedExpr *construct = new UntypedExpr( new NameExpr( "?{}" ) ); |
---|
[7eabc25] | 104 | construct->get_args().push_back( new AddressExpr( new VariableExpr( newObj ) ) ); |
---|
[5b40f30] | 105 | construct->get_args().push_back( returnStmt->get_expr() ); |
---|
| 106 | stmtsToAdd.push_back(new ExprStmt(noLabels, construct)); |
---|
[cf16f94] | 107 | |
---|
| 108 | returnStmt->set_expr( new VariableExpr( newObj ) ); |
---|
| 109 | } // if |
---|
| 110 | return returnStmt; |
---|
| 111 | } |
---|
| 112 | |
---|
[a0fdbd5] | 113 | DeclarationWithType* ReturnFixer::mutate( FunctionDecl *functionDecl ) { |
---|
[cf16f94] | 114 | std::list<DeclarationWithType*> oldReturnVals = returnVals; |
---|
| 115 | std::string oldFuncName = funcName; |
---|
[1e9d87b] | 116 | |
---|
[cf16f94] | 117 | FunctionType * type = functionDecl->get_functionType(); |
---|
| 118 | returnVals = type->get_returnVals(); |
---|
| 119 | funcName = functionDecl->get_name(); |
---|
| 120 | DeclarationWithType * decl = Mutator::mutate( functionDecl ); |
---|
| 121 | returnVals = oldReturnVals; |
---|
| 122 | funcName = oldFuncName; |
---|
| 123 | return decl; |
---|
| 124 | } |
---|
[974906e2] | 125 | |
---|
| 126 | |
---|
[02c7d04] | 127 | void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) { |
---|
| 128 | CtorDtor ctordtor; |
---|
| 129 | mutateAll( translationUnit, ctordtor ); |
---|
[974906e2] | 130 | } |
---|
| 131 | |
---|
[02c7d04] | 132 | namespace { |
---|
[71f4e4f] | 133 | Expression * makeCtorDtorExpr( std::string name, ObjectDecl * objDecl, std::list< Expression * > args ) { |
---|
[02c7d04] | 134 | UntypedExpr * expr = new UntypedExpr( new NameExpr( name ) ); |
---|
| 135 | expr->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) ); |
---|
| 136 | expr->get_args().splice( expr->get_args().end(), args ); |
---|
[71f4e4f] | 137 | return expr; |
---|
[02c7d04] | 138 | } |
---|
[974906e2] | 139 | } |
---|
| 140 | |
---|
[db4ecc5] | 141 | DeclarationWithType * CtorDtor::mutate( ObjectDecl * objDecl ) { |
---|
[974906e2] | 142 | // hands off if designated or if @= |
---|
| 143 | if ( tryConstruct( objDecl ) ) { |
---|
[f1e012b] | 144 | if ( inFunction ) { |
---|
[071a31a] | 145 | if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->get_type() ) ) { |
---|
| 146 | // call into makeArrayFunction from validate.cc to generate calls to ctor/dtor for each element of array |
---|
[620cb95] | 147 | // TODO: walk initializer and generate appropriate copy ctor if element has initializer |
---|
[7b3f66b] | 148 | std::list< Expression * > args = makeInitList( objDecl->get_init() ); |
---|
| 149 | if ( args.empty() ) { |
---|
| 150 | std::list< Statement * > ctor; |
---|
| 151 | std::list< Statement * > dtor; |
---|
| 152 | |
---|
| 153 | SymTab::makeArrayFunction( NULL, new VariableExpr( objDecl ), at, "?{}", back_inserter( ctor ) ); |
---|
| 154 | SymTab::makeArrayFunction( NULL, new VariableExpr( objDecl ), at, "^?{}", front_inserter( dtor ), false ); |
---|
| 155 | |
---|
| 156 | // Currently makeArrayFunction produces a single Statement - a CompoundStmt |
---|
| 157 | // which wraps everything that needs to happen. As such, it's technically |
---|
| 158 | // possible to use a Statement ** in the above calls, but this is inherently |
---|
| 159 | // unsafe, so instead we take the slightly less efficient route, but will be |
---|
| 160 | // immediately informed if somehow the above assumption is broken. In this case, |
---|
| 161 | // we could always wrap the list of statements at this point with a CompoundStmt, |
---|
| 162 | // but it seems reasonable at the moment for this to be done by makeArrayFunction |
---|
| 163 | // itself |
---|
| 164 | assert( ctor.size() == 1 ); |
---|
| 165 | assert( dtor.size() == 1 ); |
---|
| 166 | |
---|
| 167 | objDecl->set_init( new ConstructorInit( ctor.front(), dtor.front(), objDecl->get_init() ) ); |
---|
| 168 | } else { |
---|
| 169 | // array came with an initializer list: initialize each element |
---|
| 170 | // may have more initializers than elements in the array - need to check at each index that |
---|
| 171 | // we haven't exceeded size. This requires precomputing the size because it might be a side-effecting |
---|
| 172 | // computation. |
---|
| 173 | // may have fewer initializers than eleemnts in the array - need to default construct |
---|
| 174 | // remaining elements. |
---|
| 175 | // might be able to merge this with the case above. |
---|
| 176 | } |
---|
[071a31a] | 177 | } else { |
---|
| 178 | // it's sufficient to attempt to call the ctor/dtor for the given object and its initializer |
---|
| 179 | Expression * ctor = makeCtorDtorExpr( "?{}", objDecl, makeInitList( objDecl->get_init() ) ); |
---|
| 180 | Expression * dtor = makeCtorDtorExpr( "^?{}", objDecl, std::list< Expression * >() ); |
---|
| 181 | |
---|
| 182 | // need to remember init expression, in case no ctors exist |
---|
| 183 | // if ctor does exist, want to use ctor expression instead of init |
---|
| 184 | // push this decision to the resolver |
---|
[5b2f5bb] | 185 | ExprStmt * ctorStmt = new ExprStmt( noLabels, ctor ); |
---|
| 186 | ExprStmt * dtorStmt = new ExprStmt( noLabels, dtor ); |
---|
| 187 | objDecl->set_init( new ConstructorInit( ctorStmt, dtorStmt, objDecl->get_init() ) ); |
---|
[071a31a] | 188 | } |
---|
[f1e012b] | 189 | } |
---|
[974906e2] | 190 | } |
---|
[db4ecc5] | 191 | return Mutator::mutate( objDecl ); |
---|
[974906e2] | 192 | } |
---|
| 193 | |
---|
[02c7d04] | 194 | DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) { |
---|
| 195 | // parameters should not be constructed and destructed, so don't mutate FunctionType |
---|
[f1e012b] | 196 | bool oldInFunc = inFunction; |
---|
[02c7d04] | 197 | mutateAll( functionDecl->get_oldDecls(), *this ); |
---|
[f1e012b] | 198 | inFunction = true; |
---|
[02c7d04] | 199 | functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) ); |
---|
[f1e012b] | 200 | inFunction = oldInFunc; |
---|
[02c7d04] | 201 | return functionDecl; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | // should not traverse into any of these declarations to find objects |
---|
| 205 | // that need to be constructed or destructed |
---|
| 206 | Declaration* CtorDtor::mutate( StructDecl *aggregateDecl ) { return aggregateDecl; } |
---|
| 207 | Declaration* CtorDtor::mutate( UnionDecl *aggregateDecl ) { return aggregateDecl; } |
---|
| 208 | Declaration* CtorDtor::mutate( EnumDecl *aggregateDecl ) { return aggregateDecl; } |
---|
[a5a71d0] | 209 | Declaration* CtorDtor::mutate( TraitDecl *aggregateDecl ) { return aggregateDecl; } |
---|
[02c7d04] | 210 | TypeDecl* CtorDtor::mutate( TypeDecl *typeDecl ) { return typeDecl; } |
---|
| 211 | Declaration* CtorDtor::mutate( TypedefDecl *typeDecl ) { return typeDecl; } |
---|
[db4ecc5] | 212 | Type* CtorDtor::mutate( FunctionType *funcType ) { return funcType; } |
---|
[974906e2] | 213 | |
---|
[42e2ad7] | 214 | } // namespace InitTweak |
---|
| 215 | |
---|
[51587aa] | 216 | // Local Variables: // |
---|
| 217 | // tab-width: 4 // |
---|
| 218 | // mode: c++ // |
---|
| 219 | // compile-command: "make install" // |
---|
| 220 | // End: // |
---|