| [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
 | 
|---|
| [cad355a] | 164 |                                                 assert( ctor.size() == 1 && dynamic_cast< ImplicitCtorDtorStmt * >( ctor.front() ) );
 | 
|---|
 | 165 |                                                 assert( dtor.size() == 1 && dynamic_cast< ImplicitCtorDtorStmt * >( dtor.front() ) );
 | 
|---|
 | 166 |                                                 objDecl->set_init( new ConstructorInit( ctor.front(), dtor.front(), objDecl->get_init() ) );
 | 
|---|
| [7b3f66b] | 167 |                                         } else {
 | 
|---|
 | 168 |                                                 // array came with an initializer list: initialize each element
 | 
|---|
 | 169 |                                                 // may have more initializers than elements in the array - need to check at each index that
 | 
|---|
 | 170 |                                                 // we haven't exceeded size. This requires precomputing the size because it might be a side-effecting
 | 
|---|
 | 171 |                                                 // computation.
 | 
|---|
 | 172 |                                                 // may have fewer initializers than eleemnts in the array - need to default construct
 | 
|---|
 | 173 |                                                 // remaining elements.
 | 
|---|
 | 174 |                                                 // might be able to merge this with the case above.
 | 
|---|
 | 175 |                                         }
 | 
|---|
| [071a31a] | 176 |                                 } else {
 | 
|---|
 | 177 |                                         // it's sufficient to attempt to call the ctor/dtor for the given object and its initializer
 | 
|---|
 | 178 |                                         Expression * ctor = makeCtorDtorExpr( "?{}", objDecl, makeInitList( objDecl->get_init() ) );
 | 
|---|
 | 179 |                                         Expression * dtor = makeCtorDtorExpr( "^?{}", objDecl, std::list< Expression * >() );
 | 
|---|
 | 180 | 
 | 
|---|
 | 181 |                                         // need to remember init expression, in case no ctors exist
 | 
|---|
 | 182 |                                         // if ctor does exist, want to use ctor expression instead of init
 | 
|---|
 | 183 |                                         // push this decision to the resolver
 | 
|---|
| [5b2f5bb] | 184 |                                         ExprStmt * ctorStmt = new ExprStmt( noLabels, ctor );
 | 
|---|
 | 185 |                                         ExprStmt * dtorStmt = new ExprStmt( noLabels, dtor );
 | 
|---|
| [f1b1e4c] | 186 |                                         objDecl->set_init( new ConstructorInit( new ImplicitCtorDtorStmt( ctorStmt ), new ImplicitCtorDtorStmt( dtorStmt ), objDecl->get_init() ) );
 | 
|---|
| [071a31a] | 187 |                                 }
 | 
|---|
| [f1e012b] | 188 |                         }
 | 
|---|
| [974906e2] | 189 |                 }
 | 
|---|
| [db4ecc5] | 190 |                 return Mutator::mutate( objDecl );
 | 
|---|
| [974906e2] | 191 |         }
 | 
|---|
 | 192 | 
 | 
|---|
| [02c7d04] | 193 |         DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) {
 | 
|---|
 | 194 |                 // parameters should not be constructed and destructed, so don't mutate FunctionType
 | 
|---|
| [f1e012b] | 195 |                 bool oldInFunc = inFunction;
 | 
|---|
| [02c7d04] | 196 |                 mutateAll( functionDecl->get_oldDecls(), *this );
 | 
|---|
| [f1e012b] | 197 |                 inFunction = true;
 | 
|---|
| [02c7d04] | 198 |                 functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
 | 
|---|
| [f1e012b] | 199 |                 inFunction = oldInFunc;
 | 
|---|
| [02c7d04] | 200 |                 return functionDecl;
 | 
|---|
 | 201 |         }
 | 
|---|
 | 202 | 
 | 
|---|
 | 203 |         // should not traverse into any of these declarations to find objects
 | 
|---|
 | 204 |         // that need to be constructed or destructed
 | 
|---|
 | 205 |         Declaration* CtorDtor::mutate( StructDecl *aggregateDecl ) { return aggregateDecl; }
 | 
|---|
 | 206 |         Declaration* CtorDtor::mutate( UnionDecl *aggregateDecl ) { return aggregateDecl; }
 | 
|---|
 | 207 |         Declaration* CtorDtor::mutate( EnumDecl *aggregateDecl ) { return aggregateDecl; }
 | 
|---|
| [a5a71d0] | 208 |         Declaration* CtorDtor::mutate( TraitDecl *aggregateDecl ) { return aggregateDecl; }
 | 
|---|
| [02c7d04] | 209 |         TypeDecl* CtorDtor::mutate( TypeDecl *typeDecl ) { return typeDecl; }
 | 
|---|
 | 210 |         Declaration* CtorDtor::mutate( TypedefDecl *typeDecl ) { return typeDecl; }
 | 
|---|
| [db4ecc5] | 211 |         Type* CtorDtor::mutate( FunctionType *funcType ) { return funcType; }
 | 
|---|
| [974906e2] | 212 | 
 | 
|---|
| [42e2ad7] | 213 | } // namespace InitTweak
 | 
|---|
 | 214 | 
 | 
|---|
| [51587aa] | 215 | // Local Variables: //
 | 
|---|
 | 216 | // tab-width: 4 //
 | 
|---|
 | 217 | // mode: c++ //
 | 
|---|
 | 218 | // compile-command: "make install" //
 | 
|---|
 | 219 | // End: //
 | 
|---|