source: src/InitTweak/FixInit.cc@ 5b2f5bb

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 5b2f5bb was 5b2f5bb, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

choose destructor at at object declaration (CtorInit), overhaul and simplification of resolver code for CtorInit

  • Property mode set to 100644
File size: 4.0 KB
Line 
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
12// Last Modified On : Wed Mar 30 14:34:46 2016
13// Update Count : 30
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
27namespace InitTweak {
28 namespace {
29 const std::list<Label> noLabels;
30 }
31
32 class FixInit : public GenPoly::PolyMutator {
33 public:
34 static void fixInitializers( std::list< Declaration * > &translationUnit );
35
36 virtual ObjectDecl * mutate( ObjectDecl *objDecl );
37
38 virtual CompoundStmt * mutate( CompoundStmt * compoundStmt );
39
40 private:
41 std::list< Statement * > dtorStmts;
42 };
43
44 void fix( std::list< Declaration * > & translationUnit ) {
45 FixInit::fixInitializers( translationUnit );
46 }
47
48 void FixInit::fixInitializers( std::list< Declaration * > & translationUnit ) {
49 FixInit fixer;
50 mutateAll( translationUnit, fixer );
51 }
52
53 ObjectDecl *FixInit::mutate( ObjectDecl *objDecl ) {
54 if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) {
55 // a decision should have been made by the resolver, so ctor and init are not both non-NULL
56 assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() );
57 if ( Statement * ctor = ctorInit->get_ctor() ) {
58 stmtsToAddAfter.push_back( ctor );
59 dtorStmts.push_front( ctorInit->get_dtor() );
60 objDecl->set_init( NULL );
61 ctorInit->set_ctor( NULL );
62 ctorInit->set_dtor( NULL ); // xxx - only destruct when constructing? Probably not?
63 } else if ( Initializer * init = ctorInit->get_init() ) {
64 objDecl->set_init( init );
65 ctorInit->set_init( NULL );
66 } else {
67 // no constructor and no initializer, which is okay
68 objDecl->set_init( NULL );
69 }
70 delete ctorInit;
71 }
72 return objDecl;
73 }
74
75 CompoundStmt * FixInit::mutate( CompoundStmt * compoundStmt ) {
76 // mutate statements - this will also populate dtorStmts list
77 // don't want to dump all destructors when block is left,
78 // just the destructors associated with variables defined in this block
79 std::list< Statement * > oldDestructorStmts = dtorStmts;
80 dtorStmts = std::list<Statement *>();
81
82 compoundStmt = PolyMutator::mutate( compoundStmt );
83 std::list< Statement * > & statements = compoundStmt->get_kids();
84 for ( std::list< Statement * >::iterator it = dtorStmts.begin(); it != dtorStmts.end(); ++it ) {
85 // remove if instrinsic destructor statement
86 // xxx - test user manually calling intrinsic functions - what happens?
87 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( *it ) ) {
88 ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( exprStmt->get_expr() );
89 assert( appExpr );
90 VariableExpr * function = dynamic_cast< VariableExpr * >( appExpr->get_function() );
91 assert( function );
92 // check for Intrinsic only - don't want to remove all overridable dtors because autogenerated dtor
93 // will call all member dtors, and some members may have a user defined dtor.
94 if ( function->get_var()->get_linkage() == LinkageSpec::Intrinsic ) {
95 // don't ned to call intrinsic dtor, because it does nothing
96 delete *it;
97 } else {
98 // non-intrinsic dtors must be called
99 statements.push_back( *it );
100 }
101 } else {
102 // could also be a compound statement with a loop, in the case of an array
103 statements.push_back( *it );
104 }
105 }
106 // TODO: adding to the end of a block isn't sufficient, since
107 // return/break/goto should trigger destructor when block is left.
108 dtorStmts = oldDestructorStmts;
109 return compoundStmt;
110 }
111
112} // namespace InitTweak
113
114// Local Variables: //
115// tab-width: 4 //
116// mode: c++ //
117// compile-command: "make install" //
118// End: //
Note: See TracBrowser for help on using the repository browser.