source: src/InitTweak/GenInit.cc@ a3f9946

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 a3f9946 was 7b3f66b, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

Don't construct global extern variables, handle global array construction separately, stub for array initialization list with constructors

  • Property mode set to 100644
File size: 8.9 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// GenInit.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 : Fri May 13 11:37:48 2016
13// Update Count : 166
14//
15
16#include <stack>
17#include <list>
18#include "GenInit.h"
19#include "InitTweak.h"
20#include "SynTree/Declaration.h"
21#include "SynTree/Type.h"
22#include "SynTree/Expression.h"
23#include "SynTree/Statement.h"
24#include "SynTree/Initializer.h"
25#include "SynTree/Mutator.h"
26#include "SymTab/Autogen.h"
27#include "GenPoly/PolyMutator.h"
28
29namespace InitTweak {
30 namespace {
31 const std::list<Label> noLabels;
32 const std::list<Expression *> noDesignators;
33 }
34
35 class ReturnFixer : public GenPoly::PolyMutator {
36 public:
37 /// consistently allocates a temporary variable for the return value
38 /// of a function so that anything which the resolver decides can be constructed
39 /// into the return type of a function can be returned.
40 static void makeReturnTemp( std::list< Declaration * > &translationUnit );
41
42 ReturnFixer();
43
44 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
45
46 virtual Statement * mutate( ReturnStmt * returnStmt );
47
48 protected:
49 std::list<DeclarationWithType*> returnVals;
50 UniqueName tempNamer;
51 std::string funcName;
52 };
53
54 class CtorDtor : public GenPoly::PolyMutator {
55 public:
56 /// create constructor and destructor statements for object declarations.
57 /// Destructors are inserted directly into the code, whereas constructors
58 /// will be added in after the resolver has run so that the initializer expression
59 /// is only removed if a constructor is found
60 static void generateCtorDtor( std::list< Declaration * > &translationUnit );
61
62 CtorDtor() : inFunction( false ) {}
63
64 virtual DeclarationWithType * mutate( ObjectDecl * );
65 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
66 virtual Declaration* mutate( StructDecl *aggregateDecl );
67 virtual Declaration* mutate( UnionDecl *aggregateDecl );
68 virtual Declaration* mutate( EnumDecl *aggregateDecl );
69 virtual Declaration* mutate( TraitDecl *aggregateDecl );
70 virtual TypeDecl* mutate( TypeDecl *typeDecl );
71 virtual Declaration* mutate( TypedefDecl *typeDecl );
72
73 virtual Type * mutate( FunctionType *funcType );
74
75 protected:
76 bool inFunction;
77 };
78
79 void genInit( std::list< Declaration * > & translationUnit ) {
80 ReturnFixer::makeReturnTemp( translationUnit );
81 CtorDtor::generateCtorDtor( translationUnit );
82 }
83
84 void ReturnFixer::makeReturnTemp( std::list< Declaration * > & translationUnit ) {
85 ReturnFixer fixer;
86 mutateAll( translationUnit, fixer );
87 }
88
89 ReturnFixer::ReturnFixer() : tempNamer( "_retVal" ) {}
90
91 Statement *ReturnFixer::mutate( ReturnStmt *returnStmt ) {
92 // update for multiple return values
93 assert( returnVals.size() == 0 || returnVals.size() == 1 );
94 // hands off if the function returns an lvalue - we don't want to allocate a temporary if a variable's address
95 // is being returned
96 if ( returnStmt->get_expr() && returnVals.size() == 1 && funcName != "?=?" && ! returnVals.front()->get_type()->get_isLvalue() ) {
97 // ensure return value is not destructed by explicitly creating
98 // an empty SingleInit node wherein maybeConstruct is false
99 ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, returnVals.front()->get_type()->clone(), new ListInit( std::list<Initializer*>(), noDesignators, false ) );
100 stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
101
102 // and explicitly create the constructor expression separately
103 UntypedExpr *construct = new UntypedExpr( new NameExpr( "?{}" ) );
104 construct->get_args().push_back( new AddressExpr( new VariableExpr( newObj ) ) );
105 construct->get_args().push_back( returnStmt->get_expr() );
106 stmtsToAdd.push_back(new ExprStmt(noLabels, construct));
107
108 returnStmt->set_expr( new VariableExpr( newObj ) );
109 } // if
110 return returnStmt;
111 }
112
113 DeclarationWithType* ReturnFixer::mutate( FunctionDecl *functionDecl ) {
114 std::list<DeclarationWithType*> oldReturnVals = returnVals;
115 std::string oldFuncName = funcName;
116
117 FunctionType * type = functionDecl->get_functionType();
118 returnVals = type->get_returnVals();
119 funcName = functionDecl->get_name();
120 DeclarationWithType * decl = Mutator::mutate( functionDecl );
121 returnVals = oldReturnVals;
122 funcName = oldFuncName;
123 return decl;
124 }
125
126
127 void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) {
128 CtorDtor ctordtor;
129 mutateAll( translationUnit, ctordtor );
130 }
131
132 namespace {
133 Expression * makeCtorDtorExpr( std::string name, ObjectDecl * objDecl, std::list< Expression * > args ) {
134 UntypedExpr * expr = new UntypedExpr( new NameExpr( name ) );
135 expr->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
136 expr->get_args().splice( expr->get_args().end(), args );
137 return expr;
138 }
139 }
140
141 DeclarationWithType * CtorDtor::mutate( ObjectDecl * objDecl ) {
142 // hands off if designated or if @=
143 if ( tryConstruct( objDecl ) ) {
144 if ( inFunction ) {
145 if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->get_type() ) ) {
146 // call into makeArrayFunction from validate.cc to generate calls to ctor/dtor for each element of array
147 // TODO: walk initializer and generate appropriate copy ctor if element has initializer
148 std::list< Expression * > args = makeInitList( objDecl->get_init() );
149 if ( args.empty() ) {
150 std::list< Statement * > ctor;
151 std::list< Statement * > dtor;
152
153 SymTab::makeArrayFunction( NULL, new VariableExpr( objDecl ), at, "?{}", back_inserter( ctor ) );
154 SymTab::makeArrayFunction( NULL, new VariableExpr( objDecl ), at, "^?{}", front_inserter( dtor ), false );
155
156 // Currently makeArrayFunction produces a single Statement - a CompoundStmt
157 // which wraps everything that needs to happen. As such, it's technically
158 // possible to use a Statement ** in the above calls, but this is inherently
159 // unsafe, so instead we take the slightly less efficient route, but will be
160 // immediately informed if somehow the above assumption is broken. In this case,
161 // we could always wrap the list of statements at this point with a CompoundStmt,
162 // but it seems reasonable at the moment for this to be done by makeArrayFunction
163 // itself
164 assert( ctor.size() == 1 );
165 assert( dtor.size() == 1 );
166
167 objDecl->set_init( new ConstructorInit( ctor.front(), dtor.front(), objDecl->get_init() ) );
168 } else {
169 // array came with an initializer list: initialize each element
170 // may have more initializers than elements in the array - need to check at each index that
171 // we haven't exceeded size. This requires precomputing the size because it might be a side-effecting
172 // computation.
173 // may have fewer initializers than eleemnts in the array - need to default construct
174 // remaining elements.
175 // might be able to merge this with the case above.
176 }
177 } else {
178 // it's sufficient to attempt to call the ctor/dtor for the given object and its initializer
179 Expression * ctor = makeCtorDtorExpr( "?{}", objDecl, makeInitList( objDecl->get_init() ) );
180 Expression * dtor = makeCtorDtorExpr( "^?{}", objDecl, std::list< Expression * >() );
181
182 // need to remember init expression, in case no ctors exist
183 // if ctor does exist, want to use ctor expression instead of init
184 // push this decision to the resolver
185 ExprStmt * ctorStmt = new ExprStmt( noLabels, ctor );
186 ExprStmt * dtorStmt = new ExprStmt( noLabels, dtor );
187 objDecl->set_init( new ConstructorInit( ctorStmt, dtorStmt, objDecl->get_init() ) );
188 }
189 }
190 }
191 return Mutator::mutate( objDecl );
192 }
193
194 DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) {
195 // parameters should not be constructed and destructed, so don't mutate FunctionType
196 bool oldInFunc = inFunction;
197 mutateAll( functionDecl->get_oldDecls(), *this );
198 inFunction = true;
199 functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
200 inFunction = oldInFunc;
201 return functionDecl;
202 }
203
204 // should not traverse into any of these declarations to find objects
205 // that need to be constructed or destructed
206 Declaration* CtorDtor::mutate( StructDecl *aggregateDecl ) { return aggregateDecl; }
207 Declaration* CtorDtor::mutate( UnionDecl *aggregateDecl ) { return aggregateDecl; }
208 Declaration* CtorDtor::mutate( EnumDecl *aggregateDecl ) { return aggregateDecl; }
209 Declaration* CtorDtor::mutate( TraitDecl *aggregateDecl ) { return aggregateDecl; }
210 TypeDecl* CtorDtor::mutate( TypeDecl *typeDecl ) { return typeDecl; }
211 Declaration* CtorDtor::mutate( TypedefDecl *typeDecl ) { return typeDecl; }
212 Type* CtorDtor::mutate( FunctionType *funcType ) { return funcType; }
213
214} // namespace InitTweak
215
216// Local Variables: //
217// tab-width: 4 //
218// mode: c++ //
219// compile-command: "make install" //
220// End: //
Note: See TracBrowser for help on using the repository browser.