[71f4e4f] | 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 | // FixInit.h -- |
---|
| 8 | // |
---|
| 9 | // Author : Rob Schluntz |
---|
| 10 | // Created On : Wed Jan 13 16:29:30 2016 |
---|
| 11 | // Last Modified By : Rob Schluntz |
---|
[39786813] | 12 | // Last Modified On : Thu Mar 31 13:54:58 2016 |
---|
[ca1c11f] | 13 | // Update Count : 30 |
---|
[71f4e4f] | 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 "GenPoly/PolyMutator.h" |
---|
| 26 | |
---|
| 27 | namespace InitTweak { |
---|
| 28 | namespace { |
---|
| 29 | const std::list<Label> noLabels; |
---|
[e0323a2] | 30 | const std::list<Expression*> noDesignators; |
---|
[71f4e4f] | 31 | } |
---|
| 32 | |
---|
| 33 | class FixInit : public GenPoly::PolyMutator { |
---|
| 34 | public: |
---|
| 35 | static void fixInitializers( std::list< Declaration * > &translationUnit ); |
---|
| 36 | |
---|
| 37 | virtual ObjectDecl * mutate( ObjectDecl *objDecl ); |
---|
[f1e012b] | 38 | |
---|
| 39 | virtual CompoundStmt * mutate( CompoundStmt * compoundStmt ); |
---|
[39786813] | 40 | virtual Statement * mutate( ReturnStmt * returnStmt ); |
---|
| 41 | virtual Statement * mutate( BranchStmt * branchStmt ); |
---|
[5b2f5bb] | 42 | |
---|
| 43 | private: |
---|
[39786813] | 44 | // stack of list of statements - used to differentiate scopes |
---|
| 45 | std::list< std::list< Statement * > > dtorStmts; |
---|
[71f4e4f] | 46 | }; |
---|
| 47 | |
---|
| 48 | void fix( std::list< Declaration * > & translationUnit ) { |
---|
| 49 | FixInit::fixInitializers( translationUnit ); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | void FixInit::fixInitializers( std::list< Declaration * > & translationUnit ) { |
---|
| 53 | FixInit fixer; |
---|
| 54 | mutateAll( translationUnit, fixer ); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | ObjectDecl *FixInit::mutate( ObjectDecl *objDecl ) { |
---|
| 58 | if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) { |
---|
[f1e012b] | 59 | // a decision should have been made by the resolver, so ctor and init are not both non-NULL |
---|
[71f4e4f] | 60 | assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() ); |
---|
[5b2f5bb] | 61 | if ( Statement * ctor = ctorInit->get_ctor() ) { |
---|
[e0323a2] | 62 | if ( objDecl->get_storageClass() == DeclarationNode::Static ) { |
---|
| 63 | // generate: |
---|
| 64 | // static bool __objName_uninitialized = true; |
---|
| 65 | // if (__objName_uninitialized) { |
---|
| 66 | // __ctor(__objName); |
---|
| 67 | // void dtor_atexit() { |
---|
| 68 | // __dtor(__objName); |
---|
| 69 | // } |
---|
| 70 | // on_exit(dtorOnExit, &__objName); |
---|
| 71 | // __objName_uninitialized = false; |
---|
| 72 | // } |
---|
| 73 | |
---|
| 74 | // generate first line |
---|
| 75 | BasicType * boolType = new BasicType( Type::Qualifiers(), BasicType::Bool ); |
---|
| 76 | SingleInit * boolInitExpr = new SingleInit( new ConstantExpr( Constant( boolType->clone(), "1" ) ), noDesignators ); |
---|
| 77 | ObjectDecl * isUninitializedVar = new ObjectDecl( objDecl->get_mangleName() + "_uninitialized", DeclarationNode::Static, LinkageSpec::Cforall, 0, boolType, boolInitExpr ); |
---|
| 78 | isUninitializedVar->fixUniqueId(); |
---|
| 79 | |
---|
| 80 | // void dtor_atexit(...) {...} |
---|
| 81 | FunctionDecl * dtorCaller = new FunctionDecl( objDecl->get_mangleName() + "_dtor_atexit", DeclarationNode::NoStorageClass, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false ); |
---|
| 82 | dtorCaller->fixUniqueId(); |
---|
| 83 | dtorCaller->get_statements()->get_kids().push_back( ctorInit->get_dtor() ); |
---|
| 84 | |
---|
| 85 | // on_exit(dtor_atexit); |
---|
| 86 | UntypedExpr * callAtexit = new UntypedExpr( new NameExpr( "atexit" ) ); |
---|
| 87 | callAtexit->get_args().push_back( new VariableExpr( dtorCaller ) ); |
---|
| 88 | |
---|
| 89 | // __objName_uninitialized = false; |
---|
| 90 | UntypedExpr * setTrue = new UntypedExpr( new NameExpr( "?=?" ) ); |
---|
| 91 | setTrue->get_args().push_back( new VariableExpr( isUninitializedVar ) ); |
---|
| 92 | setTrue->get_args().push_back( new ConstantExpr( Constant( boolType->clone(), "0" ) ) ); |
---|
| 93 | |
---|
| 94 | // generate body of if |
---|
| 95 | CompoundStmt * initStmts = new CompoundStmt( noLabels ); |
---|
| 96 | std::list< Statement * > & body = initStmts->get_kids(); |
---|
| 97 | body.push_back( ctor ); |
---|
| 98 | body.push_back( new DeclStmt( noLabels, dtorCaller ) ); |
---|
| 99 | body.push_back( new ExprStmt( noLabels, callAtexit ) ); |
---|
| 100 | body.push_back( new ExprStmt( noLabels, setTrue ) ); |
---|
| 101 | |
---|
| 102 | // put it all together |
---|
| 103 | IfStmt * ifStmt = new IfStmt( noLabels, new VariableExpr( isUninitializedVar ), initStmts, 0 ); |
---|
| 104 | stmtsToAddAfter.push_back( new DeclStmt( noLabels, isUninitializedVar ) ); |
---|
| 105 | stmtsToAddAfter.push_back( ifStmt ); |
---|
| 106 | } else { |
---|
| 107 | stmtsToAddAfter.push_back( ctor ); |
---|
[39786813] | 108 | dtorStmts.back().push_front( ctorInit->get_dtor() ); |
---|
[e0323a2] | 109 | } |
---|
[71f4e4f] | 110 | objDecl->set_init( NULL ); |
---|
| 111 | ctorInit->set_ctor( NULL ); |
---|
[5b2f5bb] | 112 | ctorInit->set_dtor( NULL ); // xxx - only destruct when constructing? Probably not? |
---|
[71f4e4f] | 113 | } else if ( Initializer * init = ctorInit->get_init() ) { |
---|
| 114 | objDecl->set_init( init ); |
---|
| 115 | ctorInit->set_init( NULL ); |
---|
| 116 | } else { |
---|
[f1e012b] | 117 | // no constructor and no initializer, which is okay |
---|
| 118 | objDecl->set_init( NULL ); |
---|
[71f4e4f] | 119 | } |
---|
| 120 | delete ctorInit; |
---|
| 121 | } |
---|
| 122 | return objDecl; |
---|
| 123 | } |
---|
[f1e012b] | 124 | |
---|
[39786813] | 125 | template<typename Iterator, typename OutputIterator> |
---|
| 126 | void insertDtors( Iterator begin, Iterator end, OutputIterator out ) { |
---|
| 127 | for ( Iterator it = begin ; it != end ; ++it ) { |
---|
[f1e012b] | 128 | // remove if instrinsic destructor statement |
---|
| 129 | // xxx - test user manually calling intrinsic functions - what happens? |
---|
| 130 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( *it ) ) { |
---|
[5b2f5bb] | 131 | ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( exprStmt->get_expr() ); |
---|
| 132 | assert( appExpr ); |
---|
| 133 | VariableExpr * function = dynamic_cast< VariableExpr * >( appExpr->get_function() ); |
---|
| 134 | assert( function ); |
---|
| 135 | // check for Intrinsic only - don't want to remove all overridable dtors because autogenerated dtor |
---|
| 136 | // will call all member dtors, and some members may have a user defined dtor. |
---|
| 137 | if ( function->get_var()->get_linkage() == LinkageSpec::Intrinsic ) { |
---|
[39786813] | 138 | // don't need to call intrinsic dtor, because it does nothing |
---|
[5b2f5bb] | 139 | } else { |
---|
| 140 | // non-intrinsic dtors must be called |
---|
[39786813] | 141 | *out++ = (*it)->clone(); |
---|
[f1e012b] | 142 | } |
---|
[5b2f5bb] | 143 | } else { |
---|
| 144 | // could also be a compound statement with a loop, in the case of an array |
---|
[39786813] | 145 | *out++ = (*it)->clone(); |
---|
[f1e012b] | 146 | } |
---|
| 147 | } |
---|
[39786813] | 148 | } |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | CompoundStmt * FixInit::mutate( CompoundStmt * compoundStmt ) { |
---|
| 152 | // mutate statements - this will also populate dtorStmts list. |
---|
| 153 | // don't want to dump all destructors when block is left, |
---|
| 154 | // just the destructors associated with variables defined in this block, |
---|
| 155 | // so push a new list to the top of the stack so that we can differentiate scopes |
---|
| 156 | dtorStmts.push_back( std::list<Statement *>() ); |
---|
| 157 | |
---|
| 158 | compoundStmt = PolyMutator::mutate( compoundStmt ); |
---|
| 159 | std::list< Statement * > & statements = compoundStmt->get_kids(); |
---|
| 160 | |
---|
| 161 | insertDtors( dtorStmts.back().begin(), dtorStmts.back().end(), back_inserter( statements ) ); |
---|
| 162 | |
---|
| 163 | deleteAll( dtorStmts.back() ); |
---|
| 164 | dtorStmts.pop_back(); |
---|
| 165 | return compoundStmt; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | Statement * FixInit::mutate( ReturnStmt * returnStmt ) { |
---|
| 169 | for ( std::list< std::list< Statement * > >::reverse_iterator list = dtorStmts.rbegin(); list != dtorStmts.rend(); ++list ) { |
---|
| 170 | insertDtors( list->begin(), list->end(), back_inserter( stmtsToAdd ) ); |
---|
| 171 | } |
---|
| 172 | return returnStmt; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | Statement * FixInit::mutate( BranchStmt * branchStmt ) { |
---|
[5b2f5bb] | 176 | // TODO: adding to the end of a block isn't sufficient, since |
---|
| 177 | // return/break/goto should trigger destructor when block is left. |
---|
[39786813] | 178 | switch( branchStmt->get_type() ) { |
---|
| 179 | case BranchStmt::Continue: |
---|
| 180 | case BranchStmt::Break: |
---|
| 181 | insertDtors( dtorStmts.back().begin(), dtorStmts.back().end(), back_inserter( stmtsToAdd ) ); |
---|
| 182 | break; |
---|
| 183 | case BranchStmt::Goto: |
---|
| 184 | // xxx |
---|
| 185 | // if goto leaves a block, generate dtors for every block it leaves |
---|
| 186 | // if goto is in same block but earlier statement, destruct every object that was defined after the statement |
---|
| 187 | break; |
---|
| 188 | default: |
---|
| 189 | assert( false ); |
---|
| 190 | } |
---|
| 191 | return branchStmt; |
---|
[f1e012b] | 192 | } |
---|
| 193 | |
---|
[39786813] | 194 | |
---|
[71f4e4f] | 195 | } // namespace InitTweak |
---|
| 196 | |
---|
| 197 | // Local Variables: // |
---|
| 198 | // tab-width: 4 // |
---|
| 199 | // mode: c++ // |
---|
| 200 | // compile-command: "make install" // |
---|
| 201 | // End: // |
---|