| 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 | //
|
|---|
| 7 | // RemoveInit.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Rob Schluntz
|
|---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
|---|
| 11 | // Last Modified By : Rob Schluntz
|
|---|
| 12 | // Last Modified On : Tue Feb 09 15:12:29 2016
|
|---|
| 13 | // Update Count : 166
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include <stack>
|
|---|
| 17 | #include <list>
|
|---|
| 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"
|
|---|
| 25 | #include "SymTab/Autogen.h"
|
|---|
| 26 | #include "GenPoly/PolyMutator.h"
|
|---|
| 27 |
|
|---|
| 28 | namespace InitTweak {
|
|---|
| 29 | namespace {
|
|---|
| 30 | const std::list<Label> noLabels;
|
|---|
| 31 | const std::list<Expression *> noDesignators;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | class RemoveInit : public GenPoly::PolyMutator {
|
|---|
| 35 | public:
|
|---|
| 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 |
|
|---|
| 43 | RemoveInit();
|
|---|
| 44 | virtual ObjectDecl * mutate( ObjectDecl *objDecl );
|
|---|
| 45 | virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
|
|---|
| 46 |
|
|---|
| 47 | virtual Statement * mutate( ReturnStmt * returnStmt );
|
|---|
| 48 |
|
|---|
| 49 | protected:
|
|---|
| 50 | std::list<DeclarationWithType*> returnVals;
|
|---|
| 51 | UniqueName tempNamer;
|
|---|
| 52 | std::string funcName;
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | class CtorDtor : public GenPoly::PolyMutator {
|
|---|
| 56 | public:
|
|---|
| 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 );
|
|---|
| 62 |
|
|---|
| 63 | CtorDtor() : inFunction( false ) {}
|
|---|
| 64 |
|
|---|
| 65 | virtual ObjectDecl * mutate( ObjectDecl * );
|
|---|
| 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 );
|
|---|
| 73 |
|
|---|
| 74 | virtual CompoundStmt * mutate( CompoundStmt * compoundStmt );
|
|---|
| 75 |
|
|---|
| 76 | protected:
|
|---|
| 77 | bool inFunction;
|
|---|
| 78 |
|
|---|
| 79 | // to be added before block ends - use push_front so order is correct
|
|---|
| 80 | std::list< Statement * > destructorStmts;
|
|---|
| 81 | };
|
|---|
| 82 |
|
|---|
| 83 | void tweak( std::list< Declaration * > & translationUnit ) {
|
|---|
| 84 | RemoveInit::removeInitializers( translationUnit );
|
|---|
| 85 | CtorDtor::generateCtorDtor( translationUnit );
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | void RemoveInit::removeInitializers( std::list< Declaration * > & translationUnit ) {
|
|---|
| 89 | RemoveInit remover;
|
|---|
| 90 | mutateAll( translationUnit, remover );
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | RemoveInit::RemoveInit() : tempNamer( "_retVal" ) {}
|
|---|
| 94 |
|
|---|
| 95 | // in the case where an object has an initializer and a polymorphic type, insert an assignment immediately after the
|
|---|
| 96 | // declaration. This will (seemingly) cause the later phases to do the right thing with the assignment
|
|---|
| 97 | ObjectDecl *RemoveInit::mutate( ObjectDecl *objDecl ) {
|
|---|
| 98 | if (objDecl->get_init() && dynamic_cast<TypeInstType*>(objDecl->get_type())) {
|
|---|
| 99 | if (SingleInit * single = dynamic_cast<SingleInit*>(objDecl->get_init())) {
|
|---|
| 100 | // xxx this can be more complicated - consider ListInit
|
|---|
| 101 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?{}" ) );
|
|---|
| 102 | assign->get_args().push_back( new AddressExpr (new NameExpr( objDecl->get_name() ) ) );
|
|---|
| 103 | assign->get_args().push_back( single->get_value()->clone() );
|
|---|
| 104 | stmtsToAddAfter.push_back(new ExprStmt(noLabels, assign));
|
|---|
| 105 | } // if
|
|---|
| 106 | } // if
|
|---|
| 107 | return objDecl;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | Statement *RemoveInit::mutate( ReturnStmt *returnStmt ) {
|
|---|
| 111 | // update for multiple return values
|
|---|
| 112 | assert( returnVals.size() == 0 || returnVals.size() == 1 );
|
|---|
| 113 | // hands off if the function returns an lvalue - we don't want to allocate a temporary if a variable's address
|
|---|
| 114 | // is being returned
|
|---|
| 115 | if ( returnStmt->get_expr() && returnVals.size() == 1 && funcName != "?=?" && ! returnVals.front()->get_type()->get_isLvalue() ) {
|
|---|
| 116 | // ensure return value is not destructed by explicitly creating
|
|---|
| 117 | // an empty SingleInit node wherein maybeConstruct is false
|
|---|
| 118 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, returnVals.front()->get_type()->clone(), new ListInit( std::list<Initializer*>(), noDesignators, false ) );
|
|---|
| 119 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
|
|---|
| 120 |
|
|---|
| 121 | // and explicitly create the constructor expression separately
|
|---|
| 122 | UntypedExpr *construct = new UntypedExpr( new NameExpr( "?{}" ) );
|
|---|
| 123 | construct->get_args().push_back( new AddressExpr (new NameExpr( newObj->get_name() ) ) );
|
|---|
| 124 | construct->get_args().push_back( returnStmt->get_expr() );
|
|---|
| 125 | stmtsToAdd.push_back(new ExprStmt(noLabels, construct));
|
|---|
| 126 |
|
|---|
| 127 | returnStmt->set_expr( new VariableExpr( newObj ) );
|
|---|
| 128 | } // if
|
|---|
| 129 | return returnStmt;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | DeclarationWithType* RemoveInit::mutate( FunctionDecl *functionDecl ) {
|
|---|
| 133 | std::list<DeclarationWithType*> oldReturnVals = returnVals;
|
|---|
| 134 | std::string oldFuncName = funcName;
|
|---|
| 135 |
|
|---|
| 136 | FunctionType * type = functionDecl->get_functionType();
|
|---|
| 137 | returnVals = type->get_returnVals();
|
|---|
| 138 | funcName = functionDecl->get_name();
|
|---|
| 139 | DeclarationWithType * decl = Mutator::mutate( functionDecl );
|
|---|
| 140 | returnVals = oldReturnVals;
|
|---|
| 141 | funcName = oldFuncName;
|
|---|
| 142 | return decl;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 | void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) {
|
|---|
| 147 | CtorDtor ctordtor;
|
|---|
| 148 | mutateAll( translationUnit, ctordtor );
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | namespace {
|
|---|
| 152 | bool tryConstruct( ObjectDecl * objDecl ) {
|
|---|
| 153 | // xxx - handle designations
|
|---|
| 154 | return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) &&
|
|---|
| 155 | (objDecl->get_init() == NULL ||
|
|---|
| 156 | ( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() ));
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | Expression * makeCtorDtorExpr( std::string name, ObjectDecl * objDecl, std::list< Expression * > args ) {
|
|---|
| 160 | UntypedExpr * expr = new UntypedExpr( new NameExpr( name ) );
|
|---|
| 161 | expr->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
|
|---|
| 162 | expr->get_args().splice( expr->get_args().end(), args );
|
|---|
| 163 | return expr;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | class InitExpander : public Visitor {
|
|---|
| 167 | public:
|
|---|
| 168 | InitExpander() {}
|
|---|
| 169 | // ~InitExpander() {}
|
|---|
| 170 | virtual void visit( SingleInit * singleInit );
|
|---|
| 171 | virtual void visit( ListInit * listInit );
|
|---|
| 172 | std::list< Expression * > argList;
|
|---|
| 173 | };
|
|---|
| 174 |
|
|---|
| 175 | void InitExpander::visit( SingleInit * singleInit ) {
|
|---|
| 176 | argList.push_back( singleInit->get_value()->clone() );
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | void InitExpander::visit( ListInit * listInit ) {
|
|---|
| 180 | // xxx - for now, assume no nested list inits
|
|---|
| 181 | std::list<Initializer*>::iterator it = listInit->begin_initializers();
|
|---|
| 182 | for ( ; it != listInit->end_initializers(); ++it ) {
|
|---|
| 183 | (*it)->accept( *this );
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | std::list< Expression * > makeInitList( Initializer * init ) {
|
|---|
| 188 | InitExpander expander;
|
|---|
| 189 | maybeAccept( init, expander );
|
|---|
| 190 | return expander.argList;
|
|---|
| 191 | }
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | ObjectDecl * CtorDtor::mutate( ObjectDecl * objDecl ) {
|
|---|
| 195 | // hands off if designated or if @=
|
|---|
| 196 | if ( tryConstruct( objDecl ) ) {
|
|---|
| 197 | if ( inFunction ) {
|
|---|
| 198 | if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->get_type() ) ) {
|
|---|
| 199 | // call into makeArrayFunction from validate.cc to generate calls to ctor/dtor for each element of array
|
|---|
| 200 | // TODO: walk initializer and generate appropriate copy ctor if element has initializer
|
|---|
| 201 | SymTab::makeArrayFunction( NULL, new VariableExpr( objDecl ), at, "?{}", back_inserter( stmtsToAddAfter ) );
|
|---|
| 202 | SymTab::makeArrayFunction( NULL, new VariableExpr( objDecl ), at, "^?{}", front_inserter( destructorStmts ), false );
|
|---|
| 203 | } else {
|
|---|
| 204 | // it's sufficient to attempt to call the ctor/dtor for the given object and its initializer
|
|---|
| 205 | Expression * ctor = makeCtorDtorExpr( "?{}", objDecl, makeInitList( objDecl->get_init() ) );
|
|---|
| 206 | Expression * dtor = makeCtorDtorExpr( "^?{}", objDecl, std::list< Expression * >() );
|
|---|
| 207 |
|
|---|
| 208 | // need to remember init expression, in case no ctors exist
|
|---|
| 209 | // if ctor does exist, want to use ctor expression instead of init
|
|---|
| 210 | // push this decision to the resolver
|
|---|
| 211 | objDecl->set_init( new ConstructorInit( ctor, objDecl->get_init() ) );
|
|---|
| 212 | destructorStmts.push_front( new ExprStmt( noLabels, dtor ) );
|
|---|
| 213 | }
|
|---|
| 214 | } else {
|
|---|
| 215 | // xxx - find a way to construct/destruct globals
|
|---|
| 216 | // hack: implicit "static" initialization routine for each struct type? or something similar?
|
|---|
| 217 | // --ties into module system
|
|---|
| 218 | // this can be done by mangling main and replacing it with our own main which calls each
|
|---|
| 219 | // module initialization routine in some decided order (order given in link command?)
|
|---|
| 220 | // and finally calls mangled main
|
|---|
| 221 | }
|
|---|
| 222 | }
|
|---|
| 223 | return objDecl;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) {
|
|---|
| 227 | // parameters should not be constructed and destructed, so don't mutate FunctionType
|
|---|
| 228 | bool oldInFunc = inFunction;
|
|---|
| 229 | mutateAll( functionDecl->get_oldDecls(), *this );
|
|---|
| 230 | inFunction = true;
|
|---|
| 231 | functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
|
|---|
| 232 | inFunction = oldInFunc;
|
|---|
| 233 | return functionDecl;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | CompoundStmt * CtorDtor::mutate( CompoundStmt * compoundStmt ) {
|
|---|
| 237 | // don't want to dump all destructors when block is left,
|
|---|
| 238 | // just the destructors associated with variables defined in this block
|
|---|
| 239 | std::list< Statement * > oldDestructorStmts = destructorStmts;
|
|---|
| 240 | destructorStmts = std::list<Statement *>();
|
|---|
| 241 |
|
|---|
| 242 | CompoundStmt * ret = PolyMutator::mutate( compoundStmt );
|
|---|
| 243 | std::list< Statement * > &statements = ret->get_kids();
|
|---|
| 244 | if ( ! destructorStmts.empty() ) {
|
|---|
| 245 | // TODO: adding to the end of a block isn't sufficient, since
|
|---|
| 246 | // return/break/goto should trigger destructor when block is left.
|
|---|
| 247 | statements.splice( statements.end(), destructorStmts );
|
|---|
| 248 | } // if
|
|---|
| 249 |
|
|---|
| 250 | destructorStmts = oldDestructorStmts;
|
|---|
| 251 | return ret;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | // should not traverse into any of these declarations to find objects
|
|---|
| 255 | // that need to be constructed or destructed
|
|---|
| 256 | Declaration* CtorDtor::mutate( StructDecl *aggregateDecl ) { return aggregateDecl; }
|
|---|
| 257 | Declaration* CtorDtor::mutate( UnionDecl *aggregateDecl ) { return aggregateDecl; }
|
|---|
| 258 | Declaration* CtorDtor::mutate( EnumDecl *aggregateDecl ) { return aggregateDecl; }
|
|---|
| 259 | Declaration* CtorDtor::mutate( ContextDecl *aggregateDecl ) { return aggregateDecl; }
|
|---|
| 260 | TypeDecl* CtorDtor::mutate( TypeDecl *typeDecl ) { return typeDecl; }
|
|---|
| 261 | Declaration* CtorDtor::mutate( TypedefDecl *typeDecl ) { return typeDecl; }
|
|---|
| 262 |
|
|---|
| 263 | } // namespace InitTweak
|
|---|
| 264 |
|
|---|
| 265 | // Local Variables: //
|
|---|
| 266 | // tab-width: 4 //
|
|---|
| 267 | // mode: c++ //
|
|---|
| 268 | // compile-command: "make install" //
|
|---|
| 269 | // End: //
|
|---|