source: src/InitTweak/InitTweak.cc@ 6cf27a07

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay 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 6cf27a07 was 6cf27a07, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

reorganize global init so that it is simpler and generates less unnecessary code

  • Property mode set to 100644
File size: 7.9 KB
Line 
1#include "InitTweak.h"
2#include "SynTree/Visitor.h"
3#include "SynTree/Statement.h"
4#include "SynTree/Initializer.h"
5#include "SynTree/Expression.h"
6#include "GenPoly/GenPoly.h"
7
8namespace InitTweak {
9 namespace {
10 class HasDesignations : public Visitor {
11 public:
12 bool hasDesignations = false;
13 template<typename Init>
14 void handleInit( Init * init ) {
15 if ( ! init->get_designators().empty() ) hasDesignations = true;
16 else Visitor::visit( init );
17 }
18 virtual void visit( SingleInit * singleInit ) { handleInit( singleInit); }
19 virtual void visit( ListInit * listInit ) { handleInit( listInit); }
20 };
21
22 class InitExpander : public Visitor {
23 public:
24 InitExpander() {}
25 virtual void visit( SingleInit * singleInit );
26 virtual void visit( ListInit * listInit );
27 std::list< Expression * > argList;
28 };
29
30 void InitExpander::visit( SingleInit * singleInit ) {
31 argList.push_back( singleInit->get_value()->clone() );
32 }
33
34 void InitExpander::visit( ListInit * listInit ) {
35 // xxx - for now, assume no nested list inits
36 std::list<Initializer*>::iterator it = listInit->begin_initializers();
37 for ( ; it != listInit->end_initializers(); ++it ) {
38 (*it)->accept( *this );
39 }
40 }
41 }
42
43 std::list< Expression * > makeInitList( Initializer * init ) {
44 InitExpander expander;
45 maybeAccept( init, expander );
46 return expander.argList;
47 }
48
49 bool isDesignated( Initializer * init ) {
50 HasDesignations finder;
51 maybeAccept( init, finder );
52 return finder.hasDesignations;
53 }
54
55 bool tryConstruct( ObjectDecl * objDecl ) {
56 return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) &&
57 (objDecl->get_init() == NULL ||
58 ( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() )) &&
59 ! isDesignated( objDecl->get_init() )
60 && objDecl->get_storageClass() != DeclarationNode::Extern;
61 }
62
63 Expression * getCtorDtorCall( Statement * stmt ) {
64 if ( stmt == NULL ) return NULL;
65 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
66 return exprStmt->get_expr();
67 } else if ( CompoundStmt * compoundStmt = dynamic_cast< CompoundStmt * >( stmt ) ) {
68 // could also be a compound statement with a loop, in the case of an array
69 if( compoundStmt->get_kids().size() == 2 ) {
70 // loop variable and loop
71 ForStmt * forStmt = dynamic_cast< ForStmt * >( compoundStmt->get_kids().back() );
72 assert( forStmt && forStmt->get_body() );
73 return getCtorDtorCall( forStmt->get_body() );
74 } else if ( compoundStmt->get_kids().size() == 1 ) {
75 // should be the call statement, but in any case there's only one option
76 return getCtorDtorCall( compoundStmt->get_kids().front() );
77 } else {
78 assert( false && "too many statements in compoundStmt for getCtorDtorCall" );
79 }
80 } if ( ImplicitCtorDtorStmt * impCtorDtorStmt = dynamic_cast< ImplicitCtorDtorStmt * > ( stmt ) ) {
81 return getCtorDtorCall( impCtorDtorStmt->get_callStmt() );
82 } else {
83 // should never get here
84 assert( false && "encountered unknown call statement" );
85 }
86 }
87
88 bool isInstrinsicSingleArgCallStmt( Statement * stmt ) {
89 Expression * callExpr = getCtorDtorCall( stmt );
90 if ( ! callExpr ) return false;
91 ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr );
92 assert( appExpr );
93 VariableExpr * function = dynamic_cast< VariableExpr * >( appExpr->get_function() );
94 assert( function );
95 // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor
96 // will call all member dtors, and some members may have a user defined dtor.
97 FunctionType * funcType = GenPoly::getFunctionType( function->get_var()->get_type() );
98 assert( funcType );
99 return function->get_var()->get_linkage() == LinkageSpec::Intrinsic && funcType->get_parameters().size() == 1;
100 }
101
102 namespace {
103 template<typename CallExpr>
104 Expression *& callArg( CallExpr * callExpr, unsigned int pos ) {
105 if ( pos >= callExpr->get_args().size() ) assert( false && "asking for argument that doesn't exist. Return NULL/throw exception?" );
106 for ( Expression *& arg : callExpr->get_args() ) {
107 if ( pos == 0 ) return arg;
108 pos--;
109 }
110 assert( false );
111 }
112 }
113
114 Expression *& getCallArg( Expression * callExpr, unsigned int pos ) {
115 if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr ) ) {
116 return callArg( appExpr, pos );
117 } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( callExpr ) ) {
118 return callArg( untypedExpr, pos );
119 } else {
120 assert( false && "Unexpected expression type passed to getCallArg" );
121 }
122 }
123
124 namespace {
125 std::string funcName( Expression * func ) {
126 if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) {
127 return nameExpr->get_name();
128 } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( func ) ) {
129 return varExpr->get_var()->get_name();
130 } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( func ) ) {
131 return funcName( castExpr->get_arg() );
132 } else {
133 assert( false && "Unexpected expression type being called as a function in call expression" );
134 }
135 }
136 }
137
138 std::string getFunctionName( Expression * expr ) {
139 if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) {
140 return funcName( appExpr->get_function() );
141 } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) {
142 return funcName( untypedExpr->get_function() );
143 } else {
144 std::cerr << expr << std::endl;
145 assert( false && "Unexpected expression type passed to getFunctionName" );
146 }
147 }
148
149 Type * getPointerBase( Type * type ) {
150 if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) {
151 return ptrType->get_base();
152 } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) {
153 return arrayType->get_base();
154 } else {
155 return NULL;
156 }
157 }
158
159 Type * isPointerType( Type * type ) {
160 if ( getPointerBase( type ) ) return type;
161 else return NULL;
162 }
163
164 class ConstExprChecker : public Visitor {
165 public:
166 ConstExprChecker() : isConstExpr( true ) {}
167
168 virtual void visit( ApplicationExpr *applicationExpr ) { isConstExpr = false; }
169 virtual void visit( UntypedExpr *untypedExpr ) { isConstExpr = false; }
170 virtual void visit( NameExpr *nameExpr ) { isConstExpr = false; }
171 virtual void visit( CastExpr *castExpr ) { isConstExpr = false; }
172 virtual void visit( LabelAddressExpr *labAddressExpr ) { isConstExpr = false; }
173 virtual void visit( UntypedMemberExpr *memberExpr ) { isConstExpr = false; }
174 virtual void visit( MemberExpr *memberExpr ) { isConstExpr = false; }
175 virtual void visit( VariableExpr *variableExpr ) { isConstExpr = false; }
176 virtual void visit( ConstantExpr *constantExpr ) { /* bottom out */ }
177 // these might be okay?
178 // virtual void visit( SizeofExpr *sizeofExpr );
179 // virtual void visit( AlignofExpr *alignofExpr );
180 // virtual void visit( UntypedOffsetofExpr *offsetofExpr );
181 // virtual void visit( OffsetofExpr *offsetofExpr );
182 // virtual void visit( OffsetPackExpr *offsetPackExpr );
183 // virtual void visit( AttrExpr *attrExpr );
184 // virtual void visit( CommaExpr *commaExpr );
185 // virtual void visit( LogicalExpr *logicalExpr );
186 // virtual void visit( ConditionalExpr *conditionalExpr );
187 virtual void visit( TupleExpr *tupleExpr ) { isConstExpr = false; }
188 virtual void visit( SolvedTupleExpr *tupleExpr ) { isConstExpr = false; }
189 virtual void visit( TypeExpr *typeExpr ) { isConstExpr = false; }
190 virtual void visit( AsmExpr *asmExpr ) { isConstExpr = false; }
191 virtual void visit( UntypedValofExpr *valofExpr ) { isConstExpr = false; }
192 virtual void visit( CompoundLiteralExpr *compLitExpr ) { isConstExpr = false; }
193
194 bool isConstExpr;
195 };
196
197 bool isConstExpr( Expression * expr ) {
198 if ( expr ) {
199 ConstExprChecker checker;
200 expr->accept( checker );
201 return checker.isConstExpr;
202 }
203 return true;
204 }
205
206 bool isConstExpr( Initializer * init ) {
207 if ( init ) {
208 ConstExprChecker checker;
209 init->accept( checker );
210 return checker.isConstExpr;
211 } // if
212 // for all intents and purposes, no initializer means const expr
213 return true;
214 }
215
216}
Note: See TracBrowser for help on using the repository browser.