[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 | // |
---|
[1e9d87b] | 7 | // RemoveInit.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 |
---|
[5b2f5bb] | 12 | // Last Modified On : Wed Mar 30 15:45:12 2016 |
---|
[d63eeb0] | 13 | // Update Count : 166 |
---|
[51587aa] | 14 | // |
---|
[a08ba92] | 15 | |
---|
[974906e2] | 16 | #include <stack> |
---|
| 17 | #include <list> |
---|
[42e2ad7] | 18 | #include "RemoveInit.h" |
---|
| 19 | #include "SynTree/Declaration.h" |
---|
| 20 | #include "SynTree/Type.h" |
---|
| 21 | #include "SynTree/Expression.h" |
---|
| 22 | #include "SynTree/Statement.h" |
---|
| 23 | #include "SynTree/Initializer.h" |
---|
| 24 | #include "SynTree/Mutator.h" |
---|
[620cb95] | 25 | #include "SymTab/Autogen.h" |
---|
[1e9d87b] | 26 | #include "GenPoly/PolyMutator.h" |
---|
[42e2ad7] | 27 | |
---|
| 28 | namespace InitTweak { |
---|
[a08ba92] | 29 | namespace { |
---|
| 30 | const std::list<Label> noLabels; |
---|
[5b40f30] | 31 | const std::list<Expression *> noDesignators; |
---|
[a08ba92] | 32 | } |
---|
[1e9d87b] | 33 | |
---|
| 34 | class RemoveInit : public GenPoly::PolyMutator { |
---|
[cf16f94] | 35 | public: |
---|
[02c7d04] | 36 | /// removes and replaces initialization for polymorphic value objects |
---|
| 37 | /// with assignment (TODO: constructor) statements. |
---|
| 38 | /// also consistently allocates a temporary variable for the return value |
---|
| 39 | /// of a function so that anything which the resolver decides can be assigned |
---|
| 40 | /// into the return type of a function can be returned. |
---|
| 41 | static void removeInitializers( std::list< Declaration * > &translationUnit ); |
---|
| 42 | |
---|
[cf16f94] | 43 | RemoveInit(); |
---|
[974906e2] | 44 | virtual ObjectDecl * mutate( ObjectDecl *objDecl ); |
---|
[cf16f94] | 45 | virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ); |
---|
| 46 | |
---|
| 47 | virtual Statement * mutate( ReturnStmt * returnStmt ); |
---|
[1e9d87b] | 48 | |
---|
| 49 | protected: |
---|
[cf16f94] | 50 | std::list<DeclarationWithType*> returnVals; |
---|
| 51 | UniqueName tempNamer; |
---|
| 52 | std::string funcName; |
---|
| 53 | }; |
---|
[42e2ad7] | 54 | |
---|
[974906e2] | 55 | class CtorDtor : public GenPoly::PolyMutator { |
---|
| 56 | public: |
---|
[02c7d04] | 57 | /// create constructor and destructor statements for object declarations. |
---|
| 58 | /// Destructors are inserted directly into the code, whereas constructors |
---|
| 59 | /// will be added in after the resolver has run so that the initializer expression |
---|
| 60 | /// is only removed if a constructor is found |
---|
| 61 | static void generateCtorDtor( std::list< Declaration * > &translationUnit ); |
---|
[974906e2] | 62 | |
---|
[f1e012b] | 63 | CtorDtor() : inFunction( false ) {} |
---|
| 64 | |
---|
[974906e2] | 65 | virtual ObjectDecl * mutate( ObjectDecl * ); |
---|
[02c7d04] | 66 | virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ); |
---|
| 67 | virtual Declaration* mutate( StructDecl *aggregateDecl ); |
---|
| 68 | virtual Declaration* mutate( UnionDecl *aggregateDecl ); |
---|
| 69 | virtual Declaration* mutate( EnumDecl *aggregateDecl ); |
---|
| 70 | virtual Declaration* mutate( ContextDecl *aggregateDecl ); |
---|
| 71 | virtual TypeDecl* mutate( TypeDecl *typeDecl ); |
---|
| 72 | virtual Declaration* mutate( TypedefDecl *typeDecl ); |
---|
[974906e2] | 73 | |
---|
| 74 | protected: |
---|
[f1e012b] | 75 | bool inFunction; |
---|
[974906e2] | 76 | }; |
---|
| 77 | |
---|
[02c7d04] | 78 | void tweak( std::list< Declaration * > & translationUnit ) { |
---|
| 79 | RemoveInit::removeInitializers( translationUnit ); |
---|
| 80 | CtorDtor::generateCtorDtor( translationUnit ); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | void RemoveInit::removeInitializers( std::list< Declaration * > & translationUnit ) { |
---|
[a08ba92] | 84 | RemoveInit remover; |
---|
| 85 | mutateAll( translationUnit, remover ); |
---|
| 86 | } |
---|
[42e2ad7] | 87 | |
---|
[cf16f94] | 88 | RemoveInit::RemoveInit() : tempNamer( "_retVal" ) {} |
---|
[42e2ad7] | 89 | |
---|
[cf16f94] | 90 | // in the case where an object has an initializer and a polymorphic type, insert an assignment immediately after the |
---|
| 91 | // declaration. This will (seemingly) cause the later phases to do the right thing with the assignment |
---|
[a08ba92] | 92 | ObjectDecl *RemoveInit::mutate( ObjectDecl *objDecl ) { |
---|
| 93 | if (objDecl->get_init() && dynamic_cast<TypeInstType*>(objDecl->get_type())) { |
---|
| 94 | if (SingleInit * single = dynamic_cast<SingleInit*>(objDecl->get_init())) { |
---|
[f1e012b] | 95 | // xxx this can be more complicated - consider ListInit |
---|
| 96 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?{}" ) ); |
---|
[a08ba92] | 97 | assign->get_args().push_back( new AddressExpr (new NameExpr( objDecl->get_name() ) ) ); |
---|
| 98 | assign->get_args().push_back( single->get_value()->clone() ); |
---|
| 99 | stmtsToAddAfter.push_back(new ExprStmt(noLabels, assign)); |
---|
| 100 | } // if |
---|
| 101 | } // if |
---|
| 102 | return objDecl; |
---|
| 103 | } |
---|
[cf16f94] | 104 | |
---|
| 105 | Statement *RemoveInit::mutate( ReturnStmt *returnStmt ) { |
---|
| 106 | // update for multiple return values |
---|
| 107 | assert( returnVals.size() == 0 || returnVals.size() == 1 ); |
---|
| 108 | // hands off if the function returns an lvalue - we don't want to allocate a temporary if a variable's address |
---|
| 109 | // is being returned |
---|
| 110 | if ( returnStmt->get_expr() && returnVals.size() == 1 && funcName != "?=?" && ! returnVals.front()->get_type()->get_isLvalue() ) { |
---|
[5b40f30] | 111 | // ensure return value is not destructed by explicitly creating |
---|
| 112 | // an empty SingleInit node wherein maybeConstruct is false |
---|
| 113 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, returnVals.front()->get_type()->clone(), new ListInit( std::list<Initializer*>(), noDesignators, false ) ); |
---|
[1e9d87b] | 114 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) ); |
---|
| 115 | |
---|
[5b40f30] | 116 | // and explicitly create the constructor expression separately |
---|
| 117 | UntypedExpr *construct = new UntypedExpr( new NameExpr( "?{}" ) ); |
---|
| 118 | construct->get_args().push_back( new AddressExpr (new NameExpr( newObj->get_name() ) ) ); |
---|
| 119 | construct->get_args().push_back( returnStmt->get_expr() ); |
---|
| 120 | stmtsToAdd.push_back(new ExprStmt(noLabels, construct)); |
---|
[cf16f94] | 121 | |
---|
| 122 | returnStmt->set_expr( new VariableExpr( newObj ) ); |
---|
| 123 | } // if |
---|
| 124 | return returnStmt; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | DeclarationWithType* RemoveInit::mutate( FunctionDecl *functionDecl ) { |
---|
| 128 | std::list<DeclarationWithType*> oldReturnVals = returnVals; |
---|
| 129 | std::string oldFuncName = funcName; |
---|
[1e9d87b] | 130 | |
---|
[cf16f94] | 131 | FunctionType * type = functionDecl->get_functionType(); |
---|
| 132 | returnVals = type->get_returnVals(); |
---|
| 133 | funcName = functionDecl->get_name(); |
---|
| 134 | DeclarationWithType * decl = Mutator::mutate( functionDecl ); |
---|
| 135 | returnVals = oldReturnVals; |
---|
| 136 | funcName = oldFuncName; |
---|
| 137 | return decl; |
---|
| 138 | } |
---|
[974906e2] | 139 | |
---|
| 140 | |
---|
[02c7d04] | 141 | void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) { |
---|
| 142 | CtorDtor ctordtor; |
---|
| 143 | mutateAll( translationUnit, ctordtor ); |
---|
[974906e2] | 144 | } |
---|
| 145 | |
---|
[02c7d04] | 146 | namespace { |
---|
| 147 | bool tryConstruct( ObjectDecl * objDecl ) { |
---|
| 148 | // xxx - handle designations |
---|
| 149 | return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) && |
---|
| 150 | (objDecl->get_init() == NULL || |
---|
| 151 | ( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() )); |
---|
| 152 | } |
---|
[974906e2] | 153 | |
---|
[71f4e4f] | 154 | Expression * makeCtorDtorExpr( std::string name, ObjectDecl * objDecl, std::list< Expression * > args ) { |
---|
[02c7d04] | 155 | UntypedExpr * expr = new UntypedExpr( new NameExpr( name ) ); |
---|
| 156 | expr->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) ); |
---|
| 157 | expr->get_args().splice( expr->get_args().end(), args ); |
---|
[71f4e4f] | 158 | return expr; |
---|
[02c7d04] | 159 | } |
---|
[974906e2] | 160 | |
---|
[02c7d04] | 161 | class InitExpander : public Visitor { |
---|
| 162 | public: |
---|
| 163 | InitExpander() {} |
---|
| 164 | // ~InitExpander() {} |
---|
| 165 | virtual void visit( SingleInit * singleInit ); |
---|
| 166 | virtual void visit( ListInit * listInit ); |
---|
| 167 | std::list< Expression * > argList; |
---|
| 168 | }; |
---|
| 169 | |
---|
| 170 | void InitExpander::visit( SingleInit * singleInit ) { |
---|
| 171 | argList.push_back( singleInit->get_value()->clone() ); |
---|
| 172 | } |
---|
[974906e2] | 173 | |
---|
[02c7d04] | 174 | void InitExpander::visit( ListInit * listInit ) { |
---|
| 175 | // xxx - for now, assume no nested list inits |
---|
| 176 | std::list<Initializer*>::iterator it = listInit->begin_initializers(); |
---|
| 177 | for ( ; it != listInit->end_initializers(); ++it ) { |
---|
| 178 | (*it)->accept( *this ); |
---|
| 179 | } |
---|
[974906e2] | 180 | } |
---|
| 181 | |
---|
[02c7d04] | 182 | std::list< Expression * > makeInitList( Initializer * init ) { |
---|
[974906e2] | 183 | InitExpander expander; |
---|
[02c7d04] | 184 | maybeAccept( init, expander ); |
---|
| 185 | return expander.argList; |
---|
[974906e2] | 186 | } |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | ObjectDecl * CtorDtor::mutate( ObjectDecl * objDecl ) { |
---|
| 190 | // hands off if designated or if @= |
---|
| 191 | if ( tryConstruct( objDecl ) ) { |
---|
[f1e012b] | 192 | if ( inFunction ) { |
---|
[071a31a] | 193 | if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->get_type() ) ) { |
---|
| 194 | // call into makeArrayFunction from validate.cc to generate calls to ctor/dtor for each element of array |
---|
[620cb95] | 195 | // TODO: walk initializer and generate appropriate copy ctor if element has initializer |
---|
[5b2f5bb] | 196 | std::list< Statement * > ctor; |
---|
| 197 | std::list< Statement * > dtor; |
---|
| 198 | |
---|
| 199 | SymTab::makeArrayFunction( NULL, new VariableExpr( objDecl ), at, "?{}", back_inserter( ctor ) ); |
---|
| 200 | SymTab::makeArrayFunction( NULL, new VariableExpr( objDecl ), at, "^?{}", front_inserter( dtor ), false ); |
---|
| 201 | |
---|
| 202 | // Currently makeArrayFunction produces a single Statement - a CompoundStmt |
---|
| 203 | // which wraps everything that needs to happen. As such, it's technically |
---|
| 204 | // possible to use a Statement ** in the above calls, but this is inherently |
---|
| 205 | // unsafe, so instead we take the slightly less efficient route, but will be |
---|
| 206 | // immediately informed if somehow the above assumption is broken. In this case, |
---|
| 207 | // we could always wrap the list of statements at this point with a CompoundStmt, |
---|
| 208 | // but it seems reasonable at the moment for this to be done by makeArrayFunction |
---|
| 209 | // itself |
---|
| 210 | assert( ctor.size() == 1 ); |
---|
| 211 | assert( dtor.size() == 1 ); |
---|
| 212 | |
---|
| 213 | objDecl->set_init( new ConstructorInit( ctor.front(), dtor.front(), objDecl->get_init() ) ); |
---|
[071a31a] | 214 | } else { |
---|
| 215 | // it's sufficient to attempt to call the ctor/dtor for the given object and its initializer |
---|
| 216 | Expression * ctor = makeCtorDtorExpr( "?{}", objDecl, makeInitList( objDecl->get_init() ) ); |
---|
| 217 | Expression * dtor = makeCtorDtorExpr( "^?{}", objDecl, std::list< Expression * >() ); |
---|
| 218 | |
---|
| 219 | // need to remember init expression, in case no ctors exist |
---|
| 220 | // if ctor does exist, want to use ctor expression instead of init |
---|
| 221 | // push this decision to the resolver |
---|
[5b2f5bb] | 222 | ExprStmt * ctorStmt = new ExprStmt( noLabels, ctor ); |
---|
| 223 | ExprStmt * dtorStmt = new ExprStmt( noLabels, dtor ); |
---|
| 224 | objDecl->set_init( new ConstructorInit( ctorStmt, dtorStmt, objDecl->get_init() ) ); |
---|
[071a31a] | 225 | } |
---|
[f1e012b] | 226 | } else { |
---|
| 227 | // xxx - find a way to construct/destruct globals |
---|
| 228 | // hack: implicit "static" initialization routine for each struct type? or something similar? |
---|
| 229 | // --ties into module system |
---|
[071a31a] | 230 | // this can be done by mangling main and replacing it with our own main which calls each |
---|
| 231 | // module initialization routine in some decided order (order given in link command?) |
---|
| 232 | // and finally calls mangled main |
---|
[f1e012b] | 233 | } |
---|
[974906e2] | 234 | } |
---|
| 235 | return objDecl; |
---|
| 236 | } |
---|
| 237 | |
---|
[02c7d04] | 238 | DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) { |
---|
| 239 | // parameters should not be constructed and destructed, so don't mutate FunctionType |
---|
[f1e012b] | 240 | bool oldInFunc = inFunction; |
---|
[02c7d04] | 241 | mutateAll( functionDecl->get_oldDecls(), *this ); |
---|
[f1e012b] | 242 | inFunction = true; |
---|
[02c7d04] | 243 | functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) ); |
---|
[f1e012b] | 244 | inFunction = oldInFunc; |
---|
[02c7d04] | 245 | return functionDecl; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | // should not traverse into any of these declarations to find objects |
---|
| 249 | // that need to be constructed or destructed |
---|
| 250 | Declaration* CtorDtor::mutate( StructDecl *aggregateDecl ) { return aggregateDecl; } |
---|
| 251 | Declaration* CtorDtor::mutate( UnionDecl *aggregateDecl ) { return aggregateDecl; } |
---|
| 252 | Declaration* CtorDtor::mutate( EnumDecl *aggregateDecl ) { return aggregateDecl; } |
---|
| 253 | Declaration* CtorDtor::mutate( ContextDecl *aggregateDecl ) { return aggregateDecl; } |
---|
| 254 | TypeDecl* CtorDtor::mutate( TypeDecl *typeDecl ) { return typeDecl; } |
---|
| 255 | Declaration* CtorDtor::mutate( TypedefDecl *typeDecl ) { return typeDecl; } |
---|
[974906e2] | 256 | |
---|
[42e2ad7] | 257 | } // namespace InitTweak |
---|
| 258 | |
---|
[51587aa] | 259 | // Local Variables: // |
---|
| 260 | // tab-width: 4 // |
---|
| 261 | // mode: c++ // |
---|
| 262 | // compile-command: "make install" // |
---|
| 263 | // End: // |
---|