[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
|
---|
[7b3f66b] | 12 | // Last Modified On : Fri May 13 11:44:26 2016
|
---|
[ca1c11f] | 13 | // Update Count : 30
|
---|
[71f4e4f] | 14 | //
|
---|
| 15 |
|
---|
| 16 | #include <stack>
|
---|
| 17 | #include <list>
|
---|
[a0fdbd5] | 18 | #include "FixInit.h"
|
---|
[7b3f66b] | 19 | #include "InitTweak.h"
|
---|
[db4ecc5] | 20 | #include "ResolvExpr/Resolver.h"
|
---|
[845cedc] | 21 | #include "ResolvExpr/typeops.h"
|
---|
[71f4e4f] | 22 | #include "SynTree/Declaration.h"
|
---|
| 23 | #include "SynTree/Type.h"
|
---|
| 24 | #include "SynTree/Expression.h"
|
---|
| 25 | #include "SynTree/Statement.h"
|
---|
| 26 | #include "SynTree/Initializer.h"
|
---|
| 27 | #include "SynTree/Mutator.h"
|
---|
[db4ecc5] | 28 | #include "SymTab/Indexer.h"
|
---|
[71f4e4f] | 29 | #include "GenPoly/PolyMutator.h"
|
---|
| 30 |
|
---|
[845cedc] | 31 | bool ctordtorp = false;
|
---|
| 32 | #define PRINT( text ) if ( ctordtorp ) { text }
|
---|
| 33 |
|
---|
[71f4e4f] | 34 | namespace InitTweak {
|
---|
| 35 | namespace {
|
---|
| 36 | const std::list<Label> noLabels;
|
---|
[e0323a2] | 37 | const std::list<Expression*> noDesignators;
|
---|
[71f4e4f] | 38 | }
|
---|
| 39 |
|
---|
[5382492] | 40 | class InsertImplicitCalls : public GenPoly::PolyMutator {
|
---|
[db4ecc5] | 41 | public:
|
---|
| 42 | /// wrap function application expressions as ImplicitCopyCtorExpr nodes
|
---|
| 43 | /// so that it is easy to identify which function calls need their parameters
|
---|
| 44 | /// to be copy constructed
|
---|
| 45 | static void insert( std::list< Declaration * > & translationUnit );
|
---|
| 46 |
|
---|
| 47 | virtual Expression * mutate( ApplicationExpr * appExpr );
|
---|
| 48 | };
|
---|
| 49 |
|
---|
| 50 | class ResolveCopyCtors : public SymTab::Indexer {
|
---|
| 51 | public:
|
---|
| 52 | /// generate temporary ObjectDecls for each argument and return value of each
|
---|
| 53 | /// ImplicitCopyCtorExpr, generate/resolve copy construction expressions for each,
|
---|
| 54 | /// and generate/resolve destructors for both arguments and return value temporaries
|
---|
| 55 | static void resolveImplicitCalls( std::list< Declaration * > & translationUnit );
|
---|
| 56 |
|
---|
| 57 | virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr );
|
---|
| 58 |
|
---|
| 59 | /// create and resolve ctor/dtor expression: fname(var, [cpArg])
|
---|
| 60 | ApplicationExpr * makeCtorDtor( const std::string & fname, ObjectDecl * var, Expression * cpArg = NULL );
|
---|
[cf18eea] | 61 | /// true if type does not need to be copy constructed to ensure correctness
|
---|
| 62 | bool skipCopyConstruct( Type * );
|
---|
[540de412] | 63 | private:
|
---|
| 64 | TypeSubstitution * env;
|
---|
[db4ecc5] | 65 | };
|
---|
| 66 |
|
---|
[71f4e4f] | 67 | class FixInit : public GenPoly::PolyMutator {
|
---|
| 68 | public:
|
---|
[db4ecc5] | 69 | /// expand each object declaration to use its constructor after it is declared.
|
---|
| 70 | /// insert destructor calls at the appropriate places
|
---|
[71f4e4f] | 71 | static void fixInitializers( std::list< Declaration * > &translationUnit );
|
---|
| 72 |
|
---|
[db4ecc5] | 73 | virtual DeclarationWithType * mutate( ObjectDecl *objDecl );
|
---|
[f1e012b] | 74 |
|
---|
| 75 | virtual CompoundStmt * mutate( CompoundStmt * compoundStmt );
|
---|
[39786813] | 76 | virtual Statement * mutate( ReturnStmt * returnStmt );
|
---|
| 77 | virtual Statement * mutate( BranchStmt * branchStmt );
|
---|
[5b2f5bb] | 78 |
|
---|
| 79 | private:
|
---|
[39786813] | 80 | // stack of list of statements - used to differentiate scopes
|
---|
| 81 | std::list< std::list< Statement * > > dtorStmts;
|
---|
[71f4e4f] | 82 | };
|
---|
| 83 |
|
---|
[db4ecc5] | 84 | class FixCopyCtors : public GenPoly::PolyMutator {
|
---|
| 85 | public:
|
---|
| 86 | /// expand ImplicitCopyCtorExpr nodes into the temporary declarations, copy constructors,
|
---|
| 87 | /// call expression, and destructors
|
---|
| 88 | static void fixCopyCtors( std::list< Declaration * > &translationUnit );
|
---|
| 89 |
|
---|
| 90 | virtual Expression * mutate( ImplicitCopyCtorExpr * impCpCtorExpr );
|
---|
| 91 |
|
---|
| 92 | private:
|
---|
| 93 | // stack of list of statements - used to differentiate scopes
|
---|
| 94 | std::list< std::list< Statement * > > dtorStmts;
|
---|
| 95 | };
|
---|
| 96 |
|
---|
[71f4e4f] | 97 | void fix( std::list< Declaration * > & translationUnit ) {
|
---|
[db4ecc5] | 98 | InsertImplicitCalls::insert( translationUnit );
|
---|
| 99 | ResolveCopyCtors::resolveImplicitCalls( translationUnit );
|
---|
[71f4e4f] | 100 | FixInit::fixInitializers( translationUnit );
|
---|
[db4ecc5] | 101 | // FixCopyCtors must happen after FixInit, so that destructors are placed correctly
|
---|
| 102 | FixCopyCtors::fixCopyCtors( translationUnit );
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | void InsertImplicitCalls::insert( std::list< Declaration * > & translationUnit ) {
|
---|
| 106 | InsertImplicitCalls inserter;
|
---|
| 107 | mutateAll( translationUnit, inserter );
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | void ResolveCopyCtors::resolveImplicitCalls( std::list< Declaration * > & translationUnit ) {
|
---|
| 111 | ResolveCopyCtors resolver;
|
---|
| 112 | acceptAll( translationUnit, resolver );
|
---|
[71f4e4f] | 113 | }
|
---|
| 114 |
|
---|
| 115 | void FixInit::fixInitializers( std::list< Declaration * > & translationUnit ) {
|
---|
| 116 | FixInit fixer;
|
---|
| 117 | mutateAll( translationUnit, fixer );
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[db4ecc5] | 120 | void FixCopyCtors::fixCopyCtors( std::list< Declaration * > & translationUnit ) {
|
---|
| 121 | FixCopyCtors fixer;
|
---|
| 122 | mutateAll( translationUnit, fixer );
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | Expression * InsertImplicitCalls::mutate( ApplicationExpr * appExpr ) {
|
---|
[845cedc] | 126 | appExpr = dynamic_cast< ApplicationExpr * >( Mutator::mutate( appExpr ) );
|
---|
| 127 | assert( appExpr );
|
---|
| 128 |
|
---|
[db4ecc5] | 129 | if ( VariableExpr * function = dynamic_cast< VariableExpr * > ( appExpr->get_function() ) ) {
|
---|
| 130 | if ( function->get_var()->get_linkage() == LinkageSpec::Intrinsic ) {
|
---|
| 131 | // optimization: don't need to copy construct in order to call intrinsic functions
|
---|
| 132 | return appExpr;
|
---|
[9554d9b] | 133 | } else if ( DeclarationWithType * funcDecl = dynamic_cast< DeclarationWithType * > ( function->get_var() ) ) {
|
---|
| 134 | // FunctionType * ftype = funcDecl->get_functionType();
|
---|
| 135 | FunctionType * ftype = dynamic_cast< FunctionType * >( GenPoly::getFunctionType( funcDecl->get_type() ) );
|
---|
| 136 | assert( ftype );
|
---|
[845cedc] | 137 | if ( (funcDecl->get_name() == "?{}" || funcDecl->get_name() == "?=?") && ftype->get_parameters().size() == 2 ) {
|
---|
| 138 | Type * t1 = ftype->get_parameters().front()->get_type();
|
---|
| 139 | Type * t2 = ftype->get_parameters().back()->get_type();
|
---|
| 140 | PointerType * ptrType = dynamic_cast< PointerType * > ( t1 );
|
---|
| 141 | assert( ptrType );
|
---|
[9554d9b] | 142 |
|
---|
[845cedc] | 143 | if ( ResolvExpr::typesCompatible( ptrType->get_base(), t2, SymTab::Indexer() ) ) {
|
---|
| 144 | // optimization: don't need to copy construct in order to call a copy constructor or
|
---|
| 145 | // assignment operator
|
---|
| 146 | return appExpr;
|
---|
| 147 | }
|
---|
| 148 | } else if ( funcDecl->get_name() == "^?{}" ) {
|
---|
| 149 | // correctness: never copy construct arguments to a destructor
|
---|
| 150 | return appExpr;
|
---|
| 151 | }
|
---|
[db4ecc5] | 152 | }
|
---|
| 153 | }
|
---|
[845cedc] | 154 | PRINT( std::cerr << "InsertImplicitCalls: adding a wrapper " << appExpr << std::endl; )
|
---|
| 155 |
|
---|
[db4ecc5] | 156 | // wrap each function call so that it is easy to identify nodes that have to be copy constructed
|
---|
[5382492] | 157 | ImplicitCopyCtorExpr * expr = new ImplicitCopyCtorExpr( appExpr );
|
---|
[540de412] | 158 | // save the type substitution onto the new node so that it is easy to find.
|
---|
| 159 | // Ensure it is not deleted with the ImplicitCopyCtorExpr by removing it before deletion.
|
---|
[5382492] | 160 | // The substitution is needed to obtain the type of temporary variables so that copy constructor
|
---|
| 161 | // calls can be resolved. Normally this is what PolyMutator is for, but the pass that resolves
|
---|
| 162 | // copy constructor calls must be an Indexer. We could alternatively make a PolyIndexer which
|
---|
[540de412] | 163 | // saves the environment, or compute the types of temporaries here, but it's much simpler to
|
---|
[5382492] | 164 | // save the environment here, and more cohesive to compute temporary variables and resolve copy
|
---|
| 165 | // constructor calls together.
|
---|
| 166 | assert( env );
|
---|
[540de412] | 167 | expr->set_env( env );
|
---|
[5382492] | 168 | return expr;
|
---|
[db4ecc5] | 169 | }
|
---|
| 170 |
|
---|
[cf18eea] | 171 | bool ResolveCopyCtors::skipCopyConstruct( Type * type ) {
|
---|
| 172 | return dynamic_cast< VarArgsType * >( type ) || GenPoly::getFunctionType( type );
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[db4ecc5] | 175 | ApplicationExpr * ResolveCopyCtors::makeCtorDtor( const std::string & fname, ObjectDecl * var, Expression * cpArg ) {
|
---|
| 176 | assert( var );
|
---|
| 177 | UntypedExpr * untyped = new UntypedExpr( new NameExpr( fname ) );
|
---|
| 178 | untyped->get_args().push_back( new AddressExpr( new VariableExpr( var ) ) );
|
---|
| 179 | if (cpArg) untyped->get_args().push_back( cpArg );
|
---|
| 180 |
|
---|
| 181 | // resolve copy constructor
|
---|
| 182 | // should only be one alternative for copy ctor and dtor expressions, since
|
---|
| 183 | // all arguments are fixed (VariableExpr and already resolved expression)
|
---|
[845cedc] | 184 | PRINT( std::cerr << "ResolvingCtorDtor " << untyped << std::endl; )
|
---|
[db4ecc5] | 185 | ApplicationExpr * resolved = dynamic_cast< ApplicationExpr * >( ResolvExpr::findVoidExpression( untyped, *this ) );
|
---|
[540de412] | 186 | if ( resolved->get_env() ) {
|
---|
| 187 | env->add( *resolved->get_env() );
|
---|
| 188 | }
|
---|
[db4ecc5] | 189 |
|
---|
| 190 | assert( resolved );
|
---|
| 191 | delete untyped;
|
---|
| 192 | return resolved;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | void ResolveCopyCtors::visit( ImplicitCopyCtorExpr *impCpCtorExpr ) {
|
---|
| 196 | static UniqueName tempNamer("_tmp_cp");
|
---|
| 197 | static UniqueName retNamer("_tmp_cp_ret");
|
---|
| 198 |
|
---|
[845cedc] | 199 | PRINT( std::cerr << "ResolveCopyCtors: " << impCpCtorExpr << std::endl; )
|
---|
| 200 | Visitor::visit( impCpCtorExpr );
|
---|
[540de412] | 201 | env = impCpCtorExpr->get_env(); // xxx - maybe we really should just have a PolyIndexer...
|
---|
[db4ecc5] | 202 |
|
---|
[845cedc] | 203 | ApplicationExpr * appExpr = impCpCtorExpr->get_callExpr();
|
---|
[b617e4b] | 204 |
|
---|
[db4ecc5] | 205 | // take each argument and attempt to copy construct it.
|
---|
| 206 | for ( Expression * & arg : appExpr->get_args() ) {
|
---|
[5382492] | 207 | PRINT( std::cerr << "Type Substitution: " << *impCpCtorExpr->get_env() << std::endl; )
|
---|
[db4ecc5] | 208 | // xxx - need to handle tuple arguments
|
---|
| 209 | assert( ! arg->get_results().empty() );
|
---|
[cf18eea] | 210 | Type * result = arg->get_results().front();
|
---|
| 211 | if ( skipCopyConstruct( result ) ) continue; // skip certain non-copyable types
|
---|
[5382492] | 212 | // type may involve type variables, so apply type substitution to get temporary variable's actual type
|
---|
| 213 | result = result->clone();
|
---|
| 214 | impCpCtorExpr->get_env()->apply( result );
|
---|
| 215 | ObjectDecl * tmp = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, result, 0 );
|
---|
[b617e4b] | 216 | tmp->get_type()->set_isConst( false );
|
---|
[db4ecc5] | 217 |
|
---|
| 218 | // create and resolve copy constructor
|
---|
[845cedc] | 219 | PRINT( std::cerr << "makeCtorDtor for an argument" << std::endl; )
|
---|
[db4ecc5] | 220 | ApplicationExpr * cpCtor = makeCtorDtor( "?{}", tmp, arg );
|
---|
| 221 |
|
---|
| 222 | // if the chosen constructor is intrinsic, the copy is unnecessary, so
|
---|
| 223 | // don't create the temporary and don't call the copy constructor
|
---|
| 224 | VariableExpr * function = dynamic_cast< VariableExpr * >( cpCtor->get_function() );
|
---|
| 225 | assert( function );
|
---|
| 226 | if ( function->get_var()->get_linkage() != LinkageSpec::Intrinsic ) {
|
---|
| 227 | // replace argument to function call with temporary
|
---|
[4ffdd63] | 228 | arg = new CommaExpr( cpCtor, new VariableExpr( tmp ) );
|
---|
[db4ecc5] | 229 | impCpCtorExpr->get_tempDecls().push_back( tmp );
|
---|
| 230 | impCpCtorExpr->get_dtors().push_front( makeCtorDtor( "^?{}", tmp ) );
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | // each return value from the call needs to be connected with an ObjectDecl
|
---|
| 235 | // at the call site, which is initialized with the return value and is destructed
|
---|
| 236 | // later
|
---|
| 237 | // xxx - handle multiple return values
|
---|
[845cedc] | 238 | ApplicationExpr * callExpr = impCpCtorExpr->get_callExpr();
|
---|
[1b31345] | 239 | // xxx - is this right? callExpr may not have the right environment, because it was attached
|
---|
| 240 | // at a higher level. Trying to pass that environment along.
|
---|
| 241 | callExpr->set_env( impCpCtorExpr->get_env()->clone() );
|
---|
[db4ecc5] | 242 | for ( Type * result : appExpr->get_results() ) {
|
---|
[fea7ca7] | 243 | result = result->clone();
|
---|
| 244 | impCpCtorExpr->get_env()->apply( result );
|
---|
| 245 | ObjectDecl * ret = new ObjectDecl( retNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, result, 0 );
|
---|
[b617e4b] | 246 | ret->get_type()->set_isConst( false );
|
---|
[db4ecc5] | 247 | impCpCtorExpr->get_returnDecls().push_back( ret );
|
---|
[845cedc] | 248 | PRINT( std::cerr << "makeCtorDtor for a return" << std::endl; )
|
---|
[db4ecc5] | 249 | impCpCtorExpr->get_dtors().push_front( makeCtorDtor( "^?{}", ret ) );
|
---|
| 250 | }
|
---|
[845cedc] | 251 | PRINT( std::cerr << "after Resolving: " << impCpCtorExpr << std::endl; )
|
---|
[db4ecc5] | 252 | }
|
---|
| 253 |
|
---|
| 254 |
|
---|
| 255 | Expression * FixCopyCtors::mutate( ImplicitCopyCtorExpr * impCpCtorExpr ) {
|
---|
[845cedc] | 256 | PRINT( std::cerr << "FixCopyCtors: " << impCpCtorExpr << std::endl; )
|
---|
| 257 |
|
---|
[db4ecc5] | 258 | impCpCtorExpr = dynamic_cast< ImplicitCopyCtorExpr * >( Mutator::mutate( impCpCtorExpr ) );
|
---|
| 259 | assert( impCpCtorExpr );
|
---|
| 260 |
|
---|
| 261 | std::list< ObjectDecl * > & tempDecls = impCpCtorExpr->get_tempDecls();
|
---|
| 262 | std::list< ObjectDecl * > & returnDecls = impCpCtorExpr->get_returnDecls();
|
---|
| 263 | std::list< Expression * > & dtors = impCpCtorExpr->get_dtors();
|
---|
| 264 |
|
---|
| 265 | // add all temporary declarations and their constructors
|
---|
| 266 | for ( ObjectDecl * obj : tempDecls ) {
|
---|
| 267 | stmtsToAdd.push_back( new DeclStmt( noLabels, obj ) );
|
---|
[4ffdd63] | 268 | }
|
---|
| 269 | for ( ObjectDecl * obj : returnDecls ) {
|
---|
| 270 | stmtsToAdd.push_back( new DeclStmt( noLabels, obj ) );
|
---|
[db4ecc5] | 271 | }
|
---|
| 272 |
|
---|
| 273 | // add destructors after current statement
|
---|
| 274 | for ( Expression * dtor : dtors ) {
|
---|
| 275 | stmtsToAddAfter.push_back( new ExprStmt( noLabels, dtor ) );
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | // xxx - update to work with multiple return values
|
---|
| 279 | ObjectDecl * returnDecl = returnDecls.empty() ? NULL : returnDecls.front();
|
---|
[845cedc] | 280 | Expression * callExpr = impCpCtorExpr->get_callExpr();
|
---|
| 281 |
|
---|
| 282 | PRINT( std::cerr << "Coming out the back..." << impCpCtorExpr << std::endl; )
|
---|
[db4ecc5] | 283 |
|
---|
| 284 | // xxx - some of these aren't necessary, and can be removed once this is stable
|
---|
| 285 | dtors.clear();
|
---|
| 286 | tempDecls.clear();
|
---|
| 287 | returnDecls.clear();
|
---|
[845cedc] | 288 | impCpCtorExpr->set_callExpr( NULL );
|
---|
[540de412] | 289 | impCpCtorExpr->set_env( NULL );
|
---|
[845cedc] | 290 | delete impCpCtorExpr;
|
---|
[db4ecc5] | 291 |
|
---|
| 292 | if ( returnDecl ) {
|
---|
[4ffdd63] | 293 | UntypedExpr * assign = new UntypedExpr( new NameExpr( "?=?" ) );
|
---|
| 294 | assign->get_args().push_back( new VariableExpr( returnDecl ) );
|
---|
| 295 | assign->get_args().push_back( callExpr );
|
---|
| 296 | // know the result type of the assignment is the type of the LHS (minus the pointer), so
|
---|
| 297 | // add that onto the assignment expression so that later steps have the necessary information
|
---|
| 298 | assign->add_result( returnDecl->get_type()->clone() );
|
---|
[fea7ca7] | 299 |
|
---|
| 300 | Expression * retExpr = new CommaExpr( assign, new VariableExpr( returnDecl ) );
|
---|
| 301 | if ( callExpr->get_results().front()->get_isLvalue() ) {
|
---|
| 302 | // lvalue returning functions are funny. Lvalue.cc inserts a *? in front of any
|
---|
| 303 | // lvalue returning non-intrinsic function. Add an AddressExpr to the call to negate
|
---|
| 304 | // the derefence and change the type of the return temporary from T to T* to properly
|
---|
| 305 | // capture the return value. Then dereference the result of the comma expression, since
|
---|
| 306 | // the lvalue returning call was originally wrapped with an AddressExpr.
|
---|
| 307 | // Effectively, this turns
|
---|
| 308 | // lvalue T f();
|
---|
| 309 | // &*f()
|
---|
| 310 | // into
|
---|
| 311 | // T * tmp_cp_retN;
|
---|
| 312 | // tmp_cp_ret_N = &*(tmp_cp_ret_N = &*f(), tmp_cp_ret);
|
---|
| 313 | // which work out in terms of types, but is pretty messy. It would be nice to find a better way.
|
---|
| 314 | assign->get_args().back() = new AddressExpr( assign->get_args().back() );
|
---|
| 315 |
|
---|
| 316 | Type * resultType = returnDecl->get_type()->clone();
|
---|
| 317 | returnDecl->set_type( new PointerType( Type::Qualifiers(), returnDecl->get_type() ) );
|
---|
| 318 | UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );
|
---|
| 319 | deref->get_args().push_back( retExpr );
|
---|
| 320 | deref->add_result( resultType );
|
---|
| 321 | retExpr = deref;
|
---|
| 322 | }
|
---|
[540de412] | 323 | // xxx - might need to set env on retExpr...
|
---|
| 324 | // retExpr->set_env( env->clone() );
|
---|
[fea7ca7] | 325 | return retExpr;
|
---|
[db4ecc5] | 326 | } else {
|
---|
[845cedc] | 327 | return callExpr;
|
---|
[db4ecc5] | 328 | }
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | DeclarationWithType *FixInit::mutate( ObjectDecl *objDecl ) {
|
---|
| 332 | // first recursively handle pieces of ObjectDecl so that they aren't missed by other visitors
|
---|
| 333 | // when the init is removed from the ObjectDecl
|
---|
| 334 | objDecl = dynamic_cast< ObjectDecl * >( Mutator::mutate( objDecl ) );
|
---|
| 335 |
|
---|
[71f4e4f] | 336 | if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) {
|
---|
[f1e012b] | 337 | // a decision should have been made by the resolver, so ctor and init are not both non-NULL
|
---|
[71f4e4f] | 338 | assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() );
|
---|
[5b2f5bb] | 339 | if ( Statement * ctor = ctorInit->get_ctor() ) {
|
---|
[e0323a2] | 340 | if ( objDecl->get_storageClass() == DeclarationNode::Static ) {
|
---|
| 341 | // generate:
|
---|
| 342 | // static bool __objName_uninitialized = true;
|
---|
| 343 | // if (__objName_uninitialized) {
|
---|
| 344 | // __ctor(__objName);
|
---|
| 345 | // void dtor_atexit() {
|
---|
| 346 | // __dtor(__objName);
|
---|
| 347 | // }
|
---|
| 348 | // on_exit(dtorOnExit, &__objName);
|
---|
| 349 | // __objName_uninitialized = false;
|
---|
| 350 | // }
|
---|
| 351 |
|
---|
| 352 | // generate first line
|
---|
| 353 | BasicType * boolType = new BasicType( Type::Qualifiers(), BasicType::Bool );
|
---|
| 354 | SingleInit * boolInitExpr = new SingleInit( new ConstantExpr( Constant( boolType->clone(), "1" ) ), noDesignators );
|
---|
| 355 | ObjectDecl * isUninitializedVar = new ObjectDecl( objDecl->get_mangleName() + "_uninitialized", DeclarationNode::Static, LinkageSpec::Cforall, 0, boolType, boolInitExpr );
|
---|
| 356 | isUninitializedVar->fixUniqueId();
|
---|
| 357 |
|
---|
| 358 | // void dtor_atexit(...) {...}
|
---|
| 359 | FunctionDecl * dtorCaller = new FunctionDecl( objDecl->get_mangleName() + "_dtor_atexit", DeclarationNode::NoStorageClass, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
|
---|
| 360 | dtorCaller->fixUniqueId();
|
---|
| 361 | dtorCaller->get_statements()->get_kids().push_back( ctorInit->get_dtor() );
|
---|
| 362 |
|
---|
| 363 | // on_exit(dtor_atexit);
|
---|
| 364 | UntypedExpr * callAtexit = new UntypedExpr( new NameExpr( "atexit" ) );
|
---|
| 365 | callAtexit->get_args().push_back( new VariableExpr( dtorCaller ) );
|
---|
| 366 |
|
---|
| 367 | // __objName_uninitialized = false;
|
---|
| 368 | UntypedExpr * setTrue = new UntypedExpr( new NameExpr( "?=?" ) );
|
---|
| 369 | setTrue->get_args().push_back( new VariableExpr( isUninitializedVar ) );
|
---|
| 370 | setTrue->get_args().push_back( new ConstantExpr( Constant( boolType->clone(), "0" ) ) );
|
---|
| 371 |
|
---|
| 372 | // generate body of if
|
---|
| 373 | CompoundStmt * initStmts = new CompoundStmt( noLabels );
|
---|
| 374 | std::list< Statement * > & body = initStmts->get_kids();
|
---|
| 375 | body.push_back( ctor );
|
---|
| 376 | body.push_back( new DeclStmt( noLabels, dtorCaller ) );
|
---|
| 377 | body.push_back( new ExprStmt( noLabels, callAtexit ) );
|
---|
| 378 | body.push_back( new ExprStmt( noLabels, setTrue ) );
|
---|
| 379 |
|
---|
| 380 | // put it all together
|
---|
| 381 | IfStmt * ifStmt = new IfStmt( noLabels, new VariableExpr( isUninitializedVar ), initStmts, 0 );
|
---|
| 382 | stmtsToAddAfter.push_back( new DeclStmt( noLabels, isUninitializedVar ) );
|
---|
| 383 | stmtsToAddAfter.push_back( ifStmt );
|
---|
| 384 | } else {
|
---|
| 385 | stmtsToAddAfter.push_back( ctor );
|
---|
[39786813] | 386 | dtorStmts.back().push_front( ctorInit->get_dtor() );
|
---|
[e0323a2] | 387 | }
|
---|
[71f4e4f] | 388 | objDecl->set_init( NULL );
|
---|
| 389 | ctorInit->set_ctor( NULL );
|
---|
[5b2f5bb] | 390 | ctorInit->set_dtor( NULL ); // xxx - only destruct when constructing? Probably not?
|
---|
[71f4e4f] | 391 | } else if ( Initializer * init = ctorInit->get_init() ) {
|
---|
| 392 | objDecl->set_init( init );
|
---|
| 393 | ctorInit->set_init( NULL );
|
---|
| 394 | } else {
|
---|
[f1e012b] | 395 | // no constructor and no initializer, which is okay
|
---|
| 396 | objDecl->set_init( NULL );
|
---|
[71f4e4f] | 397 | }
|
---|
| 398 | delete ctorInit;
|
---|
| 399 | }
|
---|
| 400 | return objDecl;
|
---|
| 401 | }
|
---|
[f1e012b] | 402 |
|
---|
[ec79847] | 403 | namespace {
|
---|
| 404 | template<typename Iterator, typename OutputIterator>
|
---|
| 405 | void insertDtors( Iterator begin, Iterator end, OutputIterator out ) {
|
---|
| 406 | for ( Iterator it = begin ; it != end ; ++it ) {
|
---|
| 407 | // remove if instrinsic destructor statement. Note that this is only called
|
---|
| 408 | // on lists of implicit dtors, so if the user manually calls an intrinsic
|
---|
[7b3f66b] | 409 | // dtor then the call must (and will) still be generated since the argument
|
---|
| 410 | // may contain side effects.
|
---|
[ec79847] | 411 | if ( ! isInstrinsicSingleArgCallStmt( *it ) ) {
|
---|
| 412 | // don't need to call intrinsic dtor, because it does nothing, but
|
---|
[5b2f5bb] | 413 | // non-intrinsic dtors must be called
|
---|
[39786813] | 414 | *out++ = (*it)->clone();
|
---|
[f1e012b] | 415 | }
|
---|
| 416 | }
|
---|
| 417 | }
|
---|
[39786813] | 418 | }
|
---|
| 419 |
|
---|
| 420 | CompoundStmt * FixInit::mutate( CompoundStmt * compoundStmt ) {
|
---|
| 421 | // mutate statements - this will also populate dtorStmts list.
|
---|
| 422 | // don't want to dump all destructors when block is left,
|
---|
| 423 | // just the destructors associated with variables defined in this block,
|
---|
| 424 | // so push a new list to the top of the stack so that we can differentiate scopes
|
---|
| 425 | dtorStmts.push_back( std::list<Statement *>() );
|
---|
| 426 |
|
---|
| 427 | compoundStmt = PolyMutator::mutate( compoundStmt );
|
---|
| 428 | std::list< Statement * > & statements = compoundStmt->get_kids();
|
---|
| 429 |
|
---|
| 430 | insertDtors( dtorStmts.back().begin(), dtorStmts.back().end(), back_inserter( statements ) );
|
---|
| 431 |
|
---|
| 432 | deleteAll( dtorStmts.back() );
|
---|
| 433 | dtorStmts.pop_back();
|
---|
| 434 | return compoundStmt;
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | Statement * FixInit::mutate( ReturnStmt * returnStmt ) {
|
---|
| 438 | for ( std::list< std::list< Statement * > >::reverse_iterator list = dtorStmts.rbegin(); list != dtorStmts.rend(); ++list ) {
|
---|
| 439 | insertDtors( list->begin(), list->end(), back_inserter( stmtsToAdd ) );
|
---|
| 440 | }
|
---|
[db4ecc5] | 441 | return Mutator::mutate( returnStmt );
|
---|
[39786813] | 442 | }
|
---|
| 443 |
|
---|
| 444 | Statement * FixInit::mutate( BranchStmt * branchStmt ) {
|
---|
[5b2f5bb] | 445 | // TODO: adding to the end of a block isn't sufficient, since
|
---|
| 446 | // return/break/goto should trigger destructor when block is left.
|
---|
[39786813] | 447 | switch( branchStmt->get_type() ) {
|
---|
| 448 | case BranchStmt::Continue:
|
---|
| 449 | case BranchStmt::Break:
|
---|
| 450 | insertDtors( dtorStmts.back().begin(), dtorStmts.back().end(), back_inserter( stmtsToAdd ) );
|
---|
| 451 | break;
|
---|
| 452 | case BranchStmt::Goto:
|
---|
| 453 | // xxx
|
---|
| 454 | // if goto leaves a block, generate dtors for every block it leaves
|
---|
| 455 | // if goto is in same block but earlier statement, destruct every object that was defined after the statement
|
---|
| 456 | break;
|
---|
| 457 | default:
|
---|
| 458 | assert( false );
|
---|
| 459 | }
|
---|
[db4ecc5] | 460 | return Mutator::mutate( branchStmt );
|
---|
[f1e012b] | 461 | }
|
---|
| 462 |
|
---|
[39786813] | 463 |
|
---|
[71f4e4f] | 464 | } // namespace InitTweak
|
---|
| 465 |
|
---|
| 466 | // Local Variables: //
|
---|
| 467 | // tab-width: 4 //
|
---|
| 468 | // mode: c++ //
|
---|
| 469 | // compile-command: "make install" //
|
---|
| 470 | // End: //
|
---|