1 | #include <algorithm> |
---|
2 | #include "InitTweak.h" |
---|
3 | #include "SynTree/Visitor.h" |
---|
4 | #include "SynTree/Statement.h" |
---|
5 | #include "SynTree/Initializer.h" |
---|
6 | #include "SynTree/Expression.h" |
---|
7 | #include "GenPoly/GenPoly.h" |
---|
8 | |
---|
9 | namespace InitTweak { |
---|
10 | namespace { |
---|
11 | class HasDesignations : public Visitor { |
---|
12 | public: |
---|
13 | bool hasDesignations = false; |
---|
14 | template<typename Init> |
---|
15 | void handleInit( Init * init ) { |
---|
16 | if ( ! init->get_designators().empty() ) hasDesignations = true; |
---|
17 | else Visitor::visit( init ); |
---|
18 | } |
---|
19 | virtual void visit( SingleInit * singleInit ) { handleInit( singleInit); } |
---|
20 | virtual void visit( ListInit * listInit ) { handleInit( listInit); } |
---|
21 | }; |
---|
22 | |
---|
23 | class InitFlattener : public Visitor { |
---|
24 | public: |
---|
25 | virtual void visit( SingleInit * singleInit ); |
---|
26 | virtual void visit( ListInit * listInit ); |
---|
27 | std::list< Expression * > argList; |
---|
28 | }; |
---|
29 | |
---|
30 | void InitFlattener::visit( SingleInit * singleInit ) { |
---|
31 | argList.push_back( singleInit->get_value()->clone() ); |
---|
32 | } |
---|
33 | |
---|
34 | void InitFlattener::visit( ListInit * listInit ) { |
---|
35 | // flatten nested list inits |
---|
36 | std::list<Initializer*>::iterator it = listInit->begin(); |
---|
37 | for ( ; it != listInit->end(); ++it ) { |
---|
38 | (*it)->accept( *this ); |
---|
39 | } |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | std::list< Expression * > makeInitList( Initializer * init ) { |
---|
44 | InitFlattener flattener; |
---|
45 | maybeAccept( init, flattener ); |
---|
46 | return flattener.argList; |
---|
47 | } |
---|
48 | |
---|
49 | bool isDesignated( Initializer * init ) { |
---|
50 | HasDesignations finder; |
---|
51 | maybeAccept( init, finder ); |
---|
52 | return finder.hasDesignations; |
---|
53 | } |
---|
54 | |
---|
55 | class InitExpander::ExpanderImpl { |
---|
56 | public: |
---|
57 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) = 0; |
---|
58 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ) = 0; |
---|
59 | }; |
---|
60 | |
---|
61 | class InitImpl : public InitExpander::ExpanderImpl { |
---|
62 | public: |
---|
63 | InitImpl( Initializer * init ) : init( init ) {} |
---|
64 | |
---|
65 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) { |
---|
66 | // this is wrong, but just a placeholder for now |
---|
67 | // if ( ! flattened ) flatten( indices ); |
---|
68 | // return ! inits.empty() ? makeInitList( inits.front() ) : std::list< Expression * >(); |
---|
69 | return makeInitList( init ); |
---|
70 | } |
---|
71 | |
---|
72 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ); |
---|
73 | private: |
---|
74 | Initializer * init; |
---|
75 | }; |
---|
76 | |
---|
77 | class ExprImpl : public InitExpander::ExpanderImpl { |
---|
78 | public: |
---|
79 | ExprImpl( Expression * expr ) : arg( expr ) {} |
---|
80 | |
---|
81 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) { |
---|
82 | std::list< Expression * > ret; |
---|
83 | Expression * expr = maybeClone( arg ); |
---|
84 | if ( expr ) { |
---|
85 | for ( std::list< Expression * >::reverse_iterator it = indices.rbegin(); it != indices.rend(); ++it ) { |
---|
86 | // go through indices and layer on subscript exprs ?[?] |
---|
87 | ++it; |
---|
88 | UntypedExpr * subscriptExpr = new UntypedExpr( new NameExpr( "?[?]") ); |
---|
89 | subscriptExpr->get_args().push_back( expr ); |
---|
90 | subscriptExpr->get_args().push_back( (*it)->clone() ); |
---|
91 | expr = subscriptExpr; |
---|
92 | } |
---|
93 | ret.push_back( expr ); |
---|
94 | } |
---|
95 | return ret; |
---|
96 | } |
---|
97 | |
---|
98 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ); |
---|
99 | private: |
---|
100 | Expression * arg; |
---|
101 | }; |
---|
102 | |
---|
103 | InitExpander::InitExpander( Initializer * init ) : expander( new InitImpl( init ) ) {} |
---|
104 | |
---|
105 | InitExpander::InitExpander( Expression * expr ) : expander( new ExprImpl( expr ) ) {} |
---|
106 | |
---|
107 | std::list< Expression * > InitExpander::operator*() { |
---|
108 | return cur; |
---|
109 | } |
---|
110 | |
---|
111 | InitExpander & InitExpander::operator++() { |
---|
112 | cur = expander->next( indices ); |
---|
113 | return *this; |
---|
114 | } |
---|
115 | |
---|
116 | // use array indices list to build switch statement |
---|
117 | void InitExpander::addArrayIndex( Expression * index, Expression * dimension ) { |
---|
118 | indices.push_back( index ); |
---|
119 | indices.push_back( dimension ); |
---|
120 | } |
---|
121 | |
---|
122 | void InitExpander::clearArrayIndices() { |
---|
123 | indices.clear(); |
---|
124 | } |
---|
125 | |
---|
126 | namespace { |
---|
127 | template< typename OutIterator > |
---|
128 | void dothething( UntypedExpr * callExpr, Expression * index, Expression * dimension, Initializer * init, OutIterator out ) { |
---|
129 | UntypedExpr * cond = new UntypedExpr( new NameExpr( "?<?") ); |
---|
130 | cond->get_args().push_back( index->clone() ); |
---|
131 | cond->get_args().push_back( dimension->clone() ); |
---|
132 | |
---|
133 | std::list< Expression * > args = makeInitList( init ); |
---|
134 | callExpr->get_args().splice( callExpr->get_args().end(), args ); |
---|
135 | |
---|
136 | *out++ = new IfStmt( noLabels, cond, new ExprStmt( noLabels, callExpr ), NULL ); |
---|
137 | |
---|
138 | UntypedExpr * increment = new UntypedExpr( new NameExpr( "++?" ) ); |
---|
139 | increment->get_args().push_back( new AddressExpr( index->clone() ) ); |
---|
140 | *out++ = new ExprStmt( noLabels, increment ); |
---|
141 | } |
---|
142 | |
---|
143 | template< typename OutIterator > |
---|
144 | void build( UntypedExpr * callExpr, InitExpander::IndexList::iterator idx, InitExpander::IndexList::iterator idxEnd, Initializer * init, OutIterator out ) { |
---|
145 | if ( idx == idxEnd ) return; |
---|
146 | Expression * index = *idx++; |
---|
147 | assert( idx != idxEnd ); |
---|
148 | Expression * dimension = *idx++; |
---|
149 | |
---|
150 | if ( idx == idxEnd ) { |
---|
151 | if ( ListInit * listInit = dynamic_cast< ListInit * >( init ) ) { |
---|
152 | for ( Initializer * init : *listInit ) { |
---|
153 | dothething( callExpr->clone(), index, dimension, init, out ); |
---|
154 | } |
---|
155 | } else { |
---|
156 | dothething( callExpr->clone(), index, dimension, init, out ); |
---|
157 | } |
---|
158 | } else { |
---|
159 | std::list< Statement * > branches; |
---|
160 | |
---|
161 | unsigned long cond = 0; |
---|
162 | ListInit * listInit = dynamic_cast< ListInit * >( init ); |
---|
163 | if ( ! listInit ) { |
---|
164 | // xxx - this shouldn't be an error, but need a way to |
---|
165 | // terminate without creating output, so should catch this error |
---|
166 | throw SemanticError( "unbalanced list initializers" ); |
---|
167 | } |
---|
168 | for ( Initializer * init : *listInit ) { |
---|
169 | Expression * condition; |
---|
170 | // check for designations |
---|
171 | // if ( init-> ) { |
---|
172 | condition = new ConstantExpr( Constant::from_ulong( cond ) ); |
---|
173 | ++cond; |
---|
174 | // } else { |
---|
175 | // condition = // ... take designation |
---|
176 | // cond = // ... take designation+1 |
---|
177 | // } |
---|
178 | std::list< Statement * > stmts; |
---|
179 | build( callExpr, idx, idxEnd, init, back_inserter( stmts ) ); |
---|
180 | CaseStmt * caseStmt = new CaseStmt( noLabels, condition, stmts ); |
---|
181 | branches.push_back( caseStmt ); |
---|
182 | } |
---|
183 | *out++ = new SwitchStmt( noLabels, index->clone(), branches ); |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | // if array came with an initializer list: initialize each element |
---|
189 | // may have more initializers than elements in the array - need to check at each index that |
---|
190 | // we haven't exceeded size. |
---|
191 | // may have fewer initializers than elements in the array - need to default construct |
---|
192 | // remaining elements. |
---|
193 | // To accomplish this, generate switch statement, consuming all of expander's elements |
---|
194 | Statement * InitImpl::buildListInit( UntypedExpr * dst, std::list< Expression * > & indices ) { |
---|
195 | if ( ! init ) return NULL; |
---|
196 | std::list< Statement * > results; |
---|
197 | build( dst, indices.begin(), indices.end(), init, back_inserter( results ) ); |
---|
198 | assert( results.size() <= 1 ); |
---|
199 | if ( results.empty() ) { |
---|
200 | return NULL; |
---|
201 | } else { |
---|
202 | init = NULL; // init was consumed in creating the list init |
---|
203 | return results.front(); |
---|
204 | } |
---|
205 | return ! results.empty() ? results.front() : NULL; |
---|
206 | } |
---|
207 | |
---|
208 | Statement * ExprImpl::buildListInit( UntypedExpr * dst, std::list< Expression * > & indices ) { |
---|
209 | return NULL; |
---|
210 | } |
---|
211 | |
---|
212 | Statement * InitExpander::buildListInit( UntypedExpr * dst ) { |
---|
213 | return expander->buildListInit( dst, indices ); |
---|
214 | } |
---|
215 | |
---|
216 | bool tryConstruct( ObjectDecl * objDecl ) { |
---|
217 | return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) && |
---|
218 | (objDecl->get_init() == NULL || |
---|
219 | ( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() )) && |
---|
220 | ! isDesignated( objDecl->get_init() ) |
---|
221 | && objDecl->get_storageClass() != DeclarationNode::Extern; |
---|
222 | } |
---|
223 | |
---|
224 | class CallFinder : public Visitor { |
---|
225 | public: |
---|
226 | typedef Visitor Parent; |
---|
227 | CallFinder( const std::list< std::string > & names ) : names( names ) {} |
---|
228 | |
---|
229 | virtual void visit( ApplicationExpr * appExpr ) { |
---|
230 | handleCallExpr( appExpr ); |
---|
231 | } |
---|
232 | |
---|
233 | virtual void visit( UntypedExpr * untypedExpr ) { |
---|
234 | handleCallExpr( untypedExpr ); |
---|
235 | } |
---|
236 | |
---|
237 | std::list< Expression * > * matches; |
---|
238 | private: |
---|
239 | const std::list< std::string > names; |
---|
240 | |
---|
241 | template< typename CallExpr > |
---|
242 | void handleCallExpr( CallExpr * expr ) { |
---|
243 | Parent::visit( expr ); |
---|
244 | std::string fname = getFunctionName( expr ); |
---|
245 | if ( std::find( names.begin(), names.end(), fname ) != names.end() ) { |
---|
246 | matches->push_back( expr ); |
---|
247 | } |
---|
248 | } |
---|
249 | }; |
---|
250 | |
---|
251 | void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches ) { |
---|
252 | static CallFinder finder( std::list< std::string >{ "?{}", "^?{}" } ); |
---|
253 | finder.matches = &matches; |
---|
254 | maybeAccept( stmt, finder ); |
---|
255 | } |
---|
256 | |
---|
257 | Expression * getCtorDtorCall( Statement * stmt ) { |
---|
258 | std::list< Expression * > matches; |
---|
259 | collectCtorDtorCalls( stmt, matches ); |
---|
260 | assert( matches.size() <= 1 ); |
---|
261 | return matches.size() == 1 ? matches.front() : NULL; |
---|
262 | } |
---|
263 | |
---|
264 | namespace { |
---|
265 | VariableExpr * getCalledFunction( ApplicationExpr * appExpr ) { |
---|
266 | assert( appExpr ); |
---|
267 | // xxx - it's possible this can be other things, e.g. MemberExpr, so this is insufficient |
---|
268 | return dynamic_cast< VariableExpr * >( appExpr->get_function() ); |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | ApplicationExpr * isIntrinsicCallExpr( Expression * expr ) { |
---|
273 | ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ); |
---|
274 | if ( ! appExpr ) return NULL; |
---|
275 | VariableExpr * function = getCalledFunction( appExpr ); |
---|
276 | assert( function ); |
---|
277 | // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor |
---|
278 | // will call all member dtors, and some members may have a user defined dtor. |
---|
279 | return function->get_var()->get_linkage() == LinkageSpec::Intrinsic ? appExpr : NULL; |
---|
280 | } |
---|
281 | |
---|
282 | bool isInstrinsicSingleArgCallStmt( Statement * stmt ) { |
---|
283 | std::list< Expression * > callExprs; |
---|
284 | collectCtorDtorCalls( stmt, callExprs ); |
---|
285 | // if ( callExprs.empty() ) return false; // xxx - do I still need this check? |
---|
286 | return std::all_of( callExprs.begin(), callExprs.end(), []( Expression * callExpr ){ |
---|
287 | if ( ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) { |
---|
288 | assert( ! appExpr->get_function()->get_results().empty() ); |
---|
289 | FunctionType *funcType = GenPoly::getFunctionType( appExpr->get_function()->get_results().front() ); |
---|
290 | assert( funcType ); |
---|
291 | return funcType->get_parameters().size() == 1; |
---|
292 | } |
---|
293 | return false; |
---|
294 | }); |
---|
295 | } |
---|
296 | |
---|
297 | namespace { |
---|
298 | template<typename CallExpr> |
---|
299 | Expression *& callArg( CallExpr * callExpr, unsigned int pos ) { |
---|
300 | if ( pos >= callExpr->get_args().size() ) assert( false && "asking for argument that doesn't exist. Return NULL/throw exception?" ); |
---|
301 | for ( Expression *& arg : callExpr->get_args() ) { |
---|
302 | if ( pos == 0 ) return arg; |
---|
303 | pos--; |
---|
304 | } |
---|
305 | assert( false ); |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | Expression *& getCallArg( Expression * callExpr, unsigned int pos ) { |
---|
310 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr ) ) { |
---|
311 | return callArg( appExpr, pos ); |
---|
312 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( callExpr ) ) { |
---|
313 | return callArg( untypedExpr, pos ); |
---|
314 | } else { |
---|
315 | assert( false && "Unexpected expression type passed to getCallArg" ); |
---|
316 | } |
---|
317 | } |
---|
318 | |
---|
319 | namespace { |
---|
320 | std::string funcName( Expression * func ) { |
---|
321 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) { |
---|
322 | return nameExpr->get_name(); |
---|
323 | } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( func ) ) { |
---|
324 | return varExpr->get_var()->get_name(); |
---|
325 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( func ) ) { |
---|
326 | return funcName( castExpr->get_arg() ); |
---|
327 | } else { |
---|
328 | assert( false && "Unexpected expression type being called as a function in call expression" ); |
---|
329 | } |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | std::string getFunctionName( Expression * expr ) { |
---|
334 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) { |
---|
335 | return funcName( appExpr->get_function() ); |
---|
336 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) { |
---|
337 | return funcName( untypedExpr->get_function() ); |
---|
338 | } else { |
---|
339 | std::cerr << expr << std::endl; |
---|
340 | assert( false && "Unexpected expression type passed to getFunctionName" ); |
---|
341 | } |
---|
342 | } |
---|
343 | |
---|
344 | Type * getPointerBase( Type * type ) { |
---|
345 | if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) { |
---|
346 | return ptrType->get_base(); |
---|
347 | } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) { |
---|
348 | return arrayType->get_base(); |
---|
349 | } else { |
---|
350 | return NULL; |
---|
351 | } |
---|
352 | } |
---|
353 | |
---|
354 | Type * isPointerType( Type * type ) { |
---|
355 | if ( getPointerBase( type ) ) return type; |
---|
356 | else return NULL; |
---|
357 | } |
---|
358 | |
---|
359 | class ConstExprChecker : public Visitor { |
---|
360 | public: |
---|
361 | ConstExprChecker() : isConstExpr( true ) {} |
---|
362 | |
---|
363 | virtual void visit( ApplicationExpr *applicationExpr ) { isConstExpr = false; } |
---|
364 | virtual void visit( UntypedExpr *untypedExpr ) { isConstExpr = false; } |
---|
365 | virtual void visit( NameExpr *nameExpr ) { isConstExpr = false; } |
---|
366 | virtual void visit( CastExpr *castExpr ) { isConstExpr = false; } |
---|
367 | virtual void visit( LabelAddressExpr *labAddressExpr ) { isConstExpr = false; } |
---|
368 | virtual void visit( UntypedMemberExpr *memberExpr ) { isConstExpr = false; } |
---|
369 | virtual void visit( MemberExpr *memberExpr ) { isConstExpr = false; } |
---|
370 | virtual void visit( VariableExpr *variableExpr ) { isConstExpr = false; } |
---|
371 | virtual void visit( ConstantExpr *constantExpr ) { /* bottom out */ } |
---|
372 | // these might be okay? |
---|
373 | // virtual void visit( SizeofExpr *sizeofExpr ); |
---|
374 | // virtual void visit( AlignofExpr *alignofExpr ); |
---|
375 | // virtual void visit( UntypedOffsetofExpr *offsetofExpr ); |
---|
376 | // virtual void visit( OffsetofExpr *offsetofExpr ); |
---|
377 | // virtual void visit( OffsetPackExpr *offsetPackExpr ); |
---|
378 | // virtual void visit( AttrExpr *attrExpr ); |
---|
379 | // virtual void visit( CommaExpr *commaExpr ); |
---|
380 | // virtual void visit( LogicalExpr *logicalExpr ); |
---|
381 | // virtual void visit( ConditionalExpr *conditionalExpr ); |
---|
382 | virtual void visit( TupleExpr *tupleExpr ) { isConstExpr = false; } |
---|
383 | virtual void visit( SolvedTupleExpr *tupleExpr ) { isConstExpr = false; } |
---|
384 | virtual void visit( TypeExpr *typeExpr ) { isConstExpr = false; } |
---|
385 | virtual void visit( AsmExpr *asmExpr ) { isConstExpr = false; } |
---|
386 | virtual void visit( UntypedValofExpr *valofExpr ) { isConstExpr = false; } |
---|
387 | virtual void visit( CompoundLiteralExpr *compLitExpr ) { isConstExpr = false; } |
---|
388 | |
---|
389 | bool isConstExpr; |
---|
390 | }; |
---|
391 | |
---|
392 | bool isConstExpr( Expression * expr ) { |
---|
393 | if ( expr ) { |
---|
394 | ConstExprChecker checker; |
---|
395 | expr->accept( checker ); |
---|
396 | return checker.isConstExpr; |
---|
397 | } |
---|
398 | return true; |
---|
399 | } |
---|
400 | |
---|
401 | bool isConstExpr( Initializer * init ) { |
---|
402 | if ( init ) { |
---|
403 | ConstExprChecker checker; |
---|
404 | init->accept( checker ); |
---|
405 | return checker.isConstExpr; |
---|
406 | } // if |
---|
407 | // for all intents and purposes, no initializer means const expr |
---|
408 | return true; |
---|
409 | } |
---|
410 | |
---|
411 | } |
---|