1 | #include <algorithm> // for find, all_of |
---|
2 | #include <cassert> // for assertf, assert, strict_dynamic_cast |
---|
3 | #include <iostream> // for ostream, cerr, endl |
---|
4 | #include <iterator> // for back_insert_iterator, back_inserter |
---|
5 | #include <memory> // for __shared_ptr |
---|
6 | |
---|
7 | #include "Common/PassVisitor.h" |
---|
8 | #include "Common/SemanticError.h" // for SemanticError |
---|
9 | #include "Common/UniqueName.h" // for UniqueName |
---|
10 | #include "Common/utility.h" // for toString, deleteAll, maybeClone |
---|
11 | #include "GenPoly/GenPoly.h" // for getFunctionType |
---|
12 | #include "InitTweak.h" |
---|
13 | #include "Parser/LinkageSpec.h" // for Spec, isBuiltin, Intrinsic |
---|
14 | #include "ResolvExpr/typeops.h" // for typesCompatibleIgnoreQualifiers |
---|
15 | #include "SymTab/Autogen.h" |
---|
16 | #include "SymTab/Indexer.h" // for Indexer |
---|
17 | #include "SynTree/Attribute.h" // for Attribute |
---|
18 | #include "SynTree/Constant.h" // for Constant |
---|
19 | #include "SynTree/Declaration.h" // for ObjectDecl, DeclarationWithType |
---|
20 | #include "SynTree/Expression.h" // for Expression, UntypedExpr, Applicati... |
---|
21 | #include "SynTree/Initializer.h" // for Initializer, ListInit, Designation |
---|
22 | #include "SynTree/Label.h" // for Label |
---|
23 | #include "SynTree/Statement.h" // for CompoundStmt, ExprStmt, BranchStmt |
---|
24 | #include "SynTree/Type.h" // for FunctionType, ArrayType, PointerType |
---|
25 | #include "SynTree/Visitor.h" // for Visitor, maybeAccept |
---|
26 | #include "Tuples/Tuples.h" // for Tuples::isTtype |
---|
27 | |
---|
28 | class UntypedValofExpr; |
---|
29 | |
---|
30 | namespace InitTweak { |
---|
31 | namespace { |
---|
32 | struct HasDesignations : public WithShortCircuiting { |
---|
33 | bool hasDesignations = false; |
---|
34 | |
---|
35 | void previsit( BaseSyntaxNode * ) { |
---|
36 | // short circuit if we already know there are designations |
---|
37 | if ( hasDesignations ) visit_children = false; |
---|
38 | } |
---|
39 | |
---|
40 | void previsit( Designation * des ) { |
---|
41 | // short circuit if we already know there are designations |
---|
42 | if ( hasDesignations ) visit_children = false; |
---|
43 | else if ( ! des->get_designators().empty() ) { |
---|
44 | hasDesignations = true; |
---|
45 | visit_children = false; |
---|
46 | } |
---|
47 | } |
---|
48 | }; |
---|
49 | |
---|
50 | struct InitDepthChecker : public WithGuards { |
---|
51 | bool depthOkay = true; |
---|
52 | Type * type; |
---|
53 | int curDepth = 0, maxDepth = 0; |
---|
54 | InitDepthChecker( Type * type ) : type( type ) { |
---|
55 | Type * t = type; |
---|
56 | while ( ArrayType * at = dynamic_cast< ArrayType * >( t ) ) { |
---|
57 | maxDepth++; |
---|
58 | t = at->get_base(); |
---|
59 | } |
---|
60 | maxDepth++; |
---|
61 | } |
---|
62 | void previsit( ListInit * ) { |
---|
63 | curDepth++; |
---|
64 | GuardAction( [this]() { curDepth--; } ); |
---|
65 | if ( curDepth > maxDepth ) depthOkay = false; |
---|
66 | } |
---|
67 | }; |
---|
68 | |
---|
69 | struct InitFlattener : public WithShortCircuiting { |
---|
70 | void previsit( SingleInit * singleInit ) { |
---|
71 | visit_children = false; |
---|
72 | argList.push_back( singleInit->value->clone() ); |
---|
73 | } |
---|
74 | std::list< Expression * > argList; |
---|
75 | }; |
---|
76 | |
---|
77 | } |
---|
78 | |
---|
79 | std::list< Expression * > makeInitList( Initializer * init ) { |
---|
80 | PassVisitor<InitFlattener> flattener; |
---|
81 | maybeAccept( init, flattener ); |
---|
82 | return flattener.pass.argList; |
---|
83 | } |
---|
84 | |
---|
85 | bool isDesignated( Initializer * init ) { |
---|
86 | PassVisitor<HasDesignations> finder; |
---|
87 | maybeAccept( init, finder ); |
---|
88 | return finder.pass.hasDesignations; |
---|
89 | } |
---|
90 | |
---|
91 | bool checkInitDepth( ObjectDecl * objDecl ) { |
---|
92 | PassVisitor<InitDepthChecker> checker( objDecl->type ); |
---|
93 | maybeAccept( objDecl->init, checker ); |
---|
94 | return checker.pass.depthOkay; |
---|
95 | } |
---|
96 | |
---|
97 | class InitExpander::ExpanderImpl { |
---|
98 | public: |
---|
99 | virtual ~ExpanderImpl() = default; |
---|
100 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) = 0; |
---|
101 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ) = 0; |
---|
102 | }; |
---|
103 | |
---|
104 | class InitImpl : public InitExpander::ExpanderImpl { |
---|
105 | public: |
---|
106 | InitImpl( Initializer * init ) : init( init ) {} |
---|
107 | virtual ~InitImpl() = default; |
---|
108 | |
---|
109 | virtual std::list< Expression * > next( __attribute((unused)) std::list< Expression * > & indices ) { |
---|
110 | // this is wrong, but just a placeholder for now |
---|
111 | // if ( ! flattened ) flatten( indices ); |
---|
112 | // return ! inits.empty() ? makeInitList( inits.front() ) : std::list< Expression * >(); |
---|
113 | return makeInitList( init ); |
---|
114 | } |
---|
115 | |
---|
116 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ); |
---|
117 | private: |
---|
118 | Initializer * init; |
---|
119 | }; |
---|
120 | |
---|
121 | class ExprImpl : public InitExpander::ExpanderImpl { |
---|
122 | public: |
---|
123 | ExprImpl( Expression * expr ) : arg( expr ) {} |
---|
124 | virtual ~ExprImpl() { delete arg; } |
---|
125 | |
---|
126 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) { |
---|
127 | std::list< Expression * > ret; |
---|
128 | Expression * expr = maybeClone( arg ); |
---|
129 | if ( expr ) { |
---|
130 | for ( std::list< Expression * >::reverse_iterator it = indices.rbegin(); it != indices.rend(); ++it ) { |
---|
131 | // go through indices and layer on subscript exprs ?[?] |
---|
132 | ++it; |
---|
133 | UntypedExpr * subscriptExpr = new UntypedExpr( new NameExpr( "?[?]") ); |
---|
134 | subscriptExpr->get_args().push_back( expr ); |
---|
135 | subscriptExpr->get_args().push_back( (*it)->clone() ); |
---|
136 | expr = subscriptExpr; |
---|
137 | } |
---|
138 | ret.push_back( expr ); |
---|
139 | } |
---|
140 | return ret; |
---|
141 | } |
---|
142 | |
---|
143 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ); |
---|
144 | private: |
---|
145 | Expression * arg; |
---|
146 | }; |
---|
147 | |
---|
148 | InitExpander::InitExpander( Initializer * init ) : expander( new InitImpl( init ) ) {} |
---|
149 | |
---|
150 | InitExpander::InitExpander( Expression * expr ) : expander( new ExprImpl( expr ) ) {} |
---|
151 | |
---|
152 | std::list< Expression * > InitExpander::operator*() { |
---|
153 | return cur; |
---|
154 | } |
---|
155 | |
---|
156 | InitExpander & InitExpander::operator++() { |
---|
157 | cur = expander->next( indices ); |
---|
158 | return *this; |
---|
159 | } |
---|
160 | |
---|
161 | // use array indices list to build switch statement |
---|
162 | void InitExpander::addArrayIndex( Expression * index, Expression * dimension ) { |
---|
163 | indices.push_back( index ); |
---|
164 | indices.push_back( dimension ); |
---|
165 | } |
---|
166 | |
---|
167 | void InitExpander::clearArrayIndices() { |
---|
168 | deleteAll( indices ); |
---|
169 | indices.clear(); |
---|
170 | } |
---|
171 | |
---|
172 | bool InitExpander::addReference() { |
---|
173 | bool added = false; |
---|
174 | for ( Expression *& expr : cur ) { |
---|
175 | expr = new AddressExpr( expr ); |
---|
176 | added = true; |
---|
177 | } |
---|
178 | return added; |
---|
179 | } |
---|
180 | |
---|
181 | namespace { |
---|
182 | /// given index i, dimension d, initializer init, and callExpr f, generates |
---|
183 | /// if (i < d) f(..., init) |
---|
184 | /// ++i; |
---|
185 | /// so that only elements within the range of the array are constructed |
---|
186 | template< typename OutIterator > |
---|
187 | void buildCallExpr( UntypedExpr * callExpr, Expression * index, Expression * dimension, Initializer * init, OutIterator out ) { |
---|
188 | UntypedExpr * cond = new UntypedExpr( new NameExpr( "?<?") ); |
---|
189 | cond->get_args().push_back( index->clone() ); |
---|
190 | cond->get_args().push_back( dimension->clone() ); |
---|
191 | |
---|
192 | std::list< Expression * > args = makeInitList( init ); |
---|
193 | callExpr->get_args().splice( callExpr->get_args().end(), args ); |
---|
194 | |
---|
195 | *out++ = new IfStmt( cond, new ExprStmt( callExpr ), nullptr ); |
---|
196 | |
---|
197 | UntypedExpr * increment = new UntypedExpr( new NameExpr( "++?" ) ); |
---|
198 | increment->get_args().push_back( index->clone() ); |
---|
199 | *out++ = new ExprStmt( increment ); |
---|
200 | } |
---|
201 | |
---|
202 | template< typename OutIterator > |
---|
203 | void build( UntypedExpr * callExpr, InitExpander::IndexList::iterator idx, InitExpander::IndexList::iterator idxEnd, Initializer * init, OutIterator out ) { |
---|
204 | if ( idx == idxEnd ) return; |
---|
205 | Expression * index = *idx++; |
---|
206 | assert( idx != idxEnd ); |
---|
207 | Expression * dimension = *idx++; |
---|
208 | |
---|
209 | // xxx - may want to eventually issue a warning here if we can detect |
---|
210 | // that the number of elements exceeds to dimension of the array |
---|
211 | if ( idx == idxEnd ) { |
---|
212 | if ( ListInit * listInit = dynamic_cast< ListInit * >( init ) ) { |
---|
213 | for ( Initializer * init : *listInit ) { |
---|
214 | buildCallExpr( callExpr->clone(), index, dimension, init, out ); |
---|
215 | } |
---|
216 | } else { |
---|
217 | buildCallExpr( callExpr->clone(), index, dimension, init, out ); |
---|
218 | } |
---|
219 | } else { |
---|
220 | std::list< Statement * > branches; |
---|
221 | |
---|
222 | unsigned long cond = 0; |
---|
223 | ListInit * listInit = dynamic_cast< ListInit * >( init ); |
---|
224 | if ( ! listInit ) { |
---|
225 | // xxx - this shouldn't be an error, but need a way to |
---|
226 | // terminate without creating output, so should catch this error |
---|
227 | SemanticError( init->location, "unbalanced list initializers" ); |
---|
228 | } |
---|
229 | |
---|
230 | static UniqueName targetLabel( "L__autogen__" ); |
---|
231 | Label switchLabel( targetLabel.newName(), 0, std::list< Attribute * >{ new Attribute("unused") } ); |
---|
232 | for ( Initializer * init : *listInit ) { |
---|
233 | Expression * condition; |
---|
234 | // check for designations |
---|
235 | // if ( init-> ) { |
---|
236 | condition = new ConstantExpr( Constant::from_ulong( cond ) ); |
---|
237 | ++cond; |
---|
238 | // } else { |
---|
239 | // condition = // ... take designation |
---|
240 | // cond = // ... take designation+1 |
---|
241 | // } |
---|
242 | std::list< Statement * > stmts; |
---|
243 | build( callExpr, idx, idxEnd, init, back_inserter( stmts ) ); |
---|
244 | stmts.push_back( new BranchStmt( switchLabel, BranchStmt::Break ) ); |
---|
245 | CaseStmt * caseStmt = new CaseStmt( condition, stmts ); |
---|
246 | branches.push_back( caseStmt ); |
---|
247 | } |
---|
248 | *out++ = new SwitchStmt( index->clone(), branches ); |
---|
249 | *out++ = new NullStmt( { switchLabel } ); |
---|
250 | } |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | // if array came with an initializer list: initialize each element |
---|
255 | // may have more initializers than elements in the array - need to check at each index that |
---|
256 | // we haven't exceeded size. |
---|
257 | // may have fewer initializers than elements in the array - need to default construct |
---|
258 | // remaining elements. |
---|
259 | // To accomplish this, generate switch statement, consuming all of expander's elements |
---|
260 | Statement * InitImpl::buildListInit( UntypedExpr * dst, std::list< Expression * > & indices ) { |
---|
261 | if ( ! init ) return nullptr; |
---|
262 | CompoundStmt * block = new CompoundStmt(); |
---|
263 | build( dst, indices.begin(), indices.end(), init, back_inserter( block->get_kids() ) ); |
---|
264 | if ( block->get_kids().empty() ) { |
---|
265 | delete block; |
---|
266 | return nullptr; |
---|
267 | } else { |
---|
268 | init = nullptr; // init was consumed in creating the list init |
---|
269 | return block; |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | Statement * ExprImpl::buildListInit( UntypedExpr *, std::list< Expression * > & ) { |
---|
274 | return nullptr; |
---|
275 | } |
---|
276 | |
---|
277 | Statement * InitExpander::buildListInit( UntypedExpr * dst ) { |
---|
278 | return expander->buildListInit( dst, indices ); |
---|
279 | } |
---|
280 | |
---|
281 | Type * getTypeofThis( FunctionType * ftype ) { |
---|
282 | assertf( ftype, "getTypeofThis: nullptr ftype" ); |
---|
283 | ObjectDecl * thisParam = getParamThis( ftype ); |
---|
284 | ReferenceType * refType = strict_dynamic_cast< ReferenceType * >( thisParam->type ); |
---|
285 | return refType->base; |
---|
286 | } |
---|
287 | |
---|
288 | ObjectDecl * getParamThis( FunctionType * ftype ) { |
---|
289 | assertf( ftype, "getParamThis: nullptr ftype" ); |
---|
290 | auto & params = ftype->parameters; |
---|
291 | assertf( ! params.empty(), "getParamThis: ftype with 0 parameters: %s", toString( ftype ).c_str() ); |
---|
292 | return strict_dynamic_cast< ObjectDecl * >( params.front() ); |
---|
293 | } |
---|
294 | |
---|
295 | bool tryConstruct( DeclarationWithType * dwt ) { |
---|
296 | ObjectDecl * objDecl = dynamic_cast< ObjectDecl * >( dwt ); |
---|
297 | if ( ! objDecl ) return false; |
---|
298 | return (objDecl->get_init() == nullptr || |
---|
299 | ( objDecl->get_init() != nullptr && objDecl->get_init()->get_maybeConstructed() )) |
---|
300 | && ! objDecl->get_storageClasses().is_extern |
---|
301 | && isConstructable( objDecl->type ); |
---|
302 | } |
---|
303 | |
---|
304 | bool isConstructable( Type * type ) { |
---|
305 | return ! dynamic_cast< VarArgsType * >( type ) && ! dynamic_cast< ReferenceType * >( type ) && ! dynamic_cast< FunctionType * >( type ) && ! Tuples::isTtype( type ); |
---|
306 | } |
---|
307 | |
---|
308 | struct CallFinder { |
---|
309 | CallFinder( const std::list< std::string > & names ) : names( names ) {} |
---|
310 | |
---|
311 | void postvisit( ApplicationExpr * appExpr ) { |
---|
312 | handleCallExpr( appExpr ); |
---|
313 | } |
---|
314 | |
---|
315 | void postvisit( UntypedExpr * untypedExpr ) { |
---|
316 | handleCallExpr( untypedExpr ); |
---|
317 | } |
---|
318 | |
---|
319 | std::list< Expression * > * matches; |
---|
320 | private: |
---|
321 | const std::list< std::string > names; |
---|
322 | |
---|
323 | template< typename CallExpr > |
---|
324 | void handleCallExpr( CallExpr * expr ) { |
---|
325 | std::string fname = getFunctionName( expr ); |
---|
326 | if ( std::find( names.begin(), names.end(), fname ) != names.end() ) { |
---|
327 | matches->push_back( expr ); |
---|
328 | } |
---|
329 | } |
---|
330 | }; |
---|
331 | |
---|
332 | void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches ) { |
---|
333 | static PassVisitor<CallFinder> finder( std::list< std::string >{ "?{}", "^?{}" } ); |
---|
334 | finder.pass.matches = &matches; |
---|
335 | maybeAccept( stmt, finder ); |
---|
336 | } |
---|
337 | |
---|
338 | Expression * getCtorDtorCall( Statement * stmt ) { |
---|
339 | std::list< Expression * > matches; |
---|
340 | collectCtorDtorCalls( stmt, matches ); |
---|
341 | assert( matches.size() <= 1 ); |
---|
342 | return matches.size() == 1 ? matches.front() : nullptr; |
---|
343 | } |
---|
344 | |
---|
345 | namespace { |
---|
346 | DeclarationWithType * getCalledFunction( Expression * expr ); |
---|
347 | |
---|
348 | template<typename CallExpr> |
---|
349 | DeclarationWithType * handleDerefCalledFunction( CallExpr * expr ) { |
---|
350 | // (*f)(x) => should get "f" |
---|
351 | std::string name = getFunctionName( expr ); |
---|
352 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
353 | assertf( ! expr->get_args().empty(), "Cannot get called function from dereference with no arguments" ); |
---|
354 | return getCalledFunction( expr->get_args().front() ); |
---|
355 | } |
---|
356 | |
---|
357 | DeclarationWithType * getCalledFunction( Expression * expr ) { |
---|
358 | assert( expr ); |
---|
359 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) { |
---|
360 | return varExpr->var; |
---|
361 | } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( expr ) ) { |
---|
362 | return memberExpr->member; |
---|
363 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { |
---|
364 | return getCalledFunction( castExpr->arg ); |
---|
365 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( expr ) ) { |
---|
366 | return handleDerefCalledFunction( untypedExpr ); |
---|
367 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) { |
---|
368 | return handleDerefCalledFunction( appExpr ); |
---|
369 | } else if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) { |
---|
370 | return getCalledFunction( addrExpr->arg ); |
---|
371 | } else if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( expr ) ) { |
---|
372 | return getCalledFunction( commaExpr->arg2 ); |
---|
373 | } |
---|
374 | return nullptr; |
---|
375 | } |
---|
376 | } |
---|
377 | |
---|
378 | DeclarationWithType * getFunction( Expression * expr ) { |
---|
379 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) { |
---|
380 | return getCalledFunction( appExpr->get_function() ); |
---|
381 | } else if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * > ( expr ) ) { |
---|
382 | return getCalledFunction( untyped->get_function() ); |
---|
383 | } |
---|
384 | assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() ); |
---|
385 | } |
---|
386 | |
---|
387 | ApplicationExpr * isIntrinsicCallExpr( Expression * expr ) { |
---|
388 | ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ); |
---|
389 | if ( ! appExpr ) return nullptr; |
---|
390 | DeclarationWithType * function = getCalledFunction( appExpr->get_function() ); |
---|
391 | assertf( function, "getCalledFunction returned nullptr: %s", toString( appExpr->get_function() ).c_str() ); |
---|
392 | // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor |
---|
393 | // will call all member dtors, and some members may have a user defined dtor. |
---|
394 | return function->get_linkage() == LinkageSpec::Intrinsic ? appExpr : nullptr; |
---|
395 | } |
---|
396 | |
---|
397 | namespace { |
---|
398 | template <typename Predicate> |
---|
399 | bool allofCtorDtor( Statement * stmt, const Predicate & pred ) { |
---|
400 | std::list< Expression * > callExprs; |
---|
401 | collectCtorDtorCalls( stmt, callExprs ); |
---|
402 | // if ( callExprs.empty() ) return false; // xxx - do I still need this check? |
---|
403 | return std::all_of( callExprs.begin(), callExprs.end(), pred); |
---|
404 | } |
---|
405 | } |
---|
406 | |
---|
407 | bool isIntrinsicSingleArgCallStmt( Statement * stmt ) { |
---|
408 | return allofCtorDtor( stmt, []( Expression * callExpr ){ |
---|
409 | if ( ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) { |
---|
410 | FunctionType *funcType = GenPoly::getFunctionType( appExpr->function->result ); |
---|
411 | assert( funcType ); |
---|
412 | return funcType->get_parameters().size() == 1; |
---|
413 | } |
---|
414 | return false; |
---|
415 | }); |
---|
416 | } |
---|
417 | |
---|
418 | bool isIntrinsicCallStmt( Statement * stmt ) { |
---|
419 | return allofCtorDtor( stmt, []( Expression * callExpr ) { |
---|
420 | return isIntrinsicCallExpr( callExpr ); |
---|
421 | }); |
---|
422 | } |
---|
423 | |
---|
424 | namespace { |
---|
425 | template<typename CallExpr> |
---|
426 | Expression *& callArg( CallExpr * callExpr, unsigned int pos ) { |
---|
427 | if ( pos >= callExpr->get_args().size() ) assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.", pos, toString( callExpr ).c_str() ); |
---|
428 | for ( Expression *& arg : callExpr->get_args() ) { |
---|
429 | if ( pos == 0 ) return arg; |
---|
430 | pos--; |
---|
431 | } |
---|
432 | assert( false ); |
---|
433 | } |
---|
434 | } |
---|
435 | |
---|
436 | Expression *& getCallArg( Expression * callExpr, unsigned int pos ) { |
---|
437 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr ) ) { |
---|
438 | return callArg( appExpr, pos ); |
---|
439 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( callExpr ) ) { |
---|
440 | return callArg( untypedExpr, pos ); |
---|
441 | } else if ( TupleAssignExpr * tupleExpr = dynamic_cast< TupleAssignExpr * > ( callExpr ) ) { |
---|
442 | std::list< Statement * > & stmts = tupleExpr->get_stmtExpr()->get_statements()->get_kids(); |
---|
443 | assertf( ! stmts.empty(), "TupleAssignExpr somehow has no statements." ); |
---|
444 | ExprStmt * stmt = strict_dynamic_cast< ExprStmt * >( stmts.back() ); |
---|
445 | TupleExpr * tuple = strict_dynamic_cast< TupleExpr * >( stmt->get_expr() ); |
---|
446 | assertf( ! tuple->get_exprs().empty(), "TupleAssignExpr somehow has empty tuple expr." ); |
---|
447 | return getCallArg( tuple->get_exprs().front(), pos ); |
---|
448 | } else if ( ImplicitCopyCtorExpr * copyCtor = dynamic_cast< ImplicitCopyCtorExpr * >( callExpr ) ) { |
---|
449 | return getCallArg( copyCtor->callExpr, pos ); |
---|
450 | } else { |
---|
451 | assertf( false, "Unexpected expression type passed to getCallArg: %s", toString( callExpr ).c_str() ); |
---|
452 | } |
---|
453 | } |
---|
454 | |
---|
455 | namespace { |
---|
456 | std::string funcName( Expression * func ); |
---|
457 | |
---|
458 | template<typename CallExpr> |
---|
459 | std::string handleDerefName( CallExpr * expr ) { |
---|
460 | // (*f)(x) => should get name "f" |
---|
461 | std::string name = getFunctionName( expr ); |
---|
462 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
463 | assertf( ! expr->get_args().empty(), "Cannot get function name from dereference with no arguments" ); |
---|
464 | return funcName( expr->get_args().front() ); |
---|
465 | } |
---|
466 | |
---|
467 | std::string funcName( Expression * func ) { |
---|
468 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) { |
---|
469 | return nameExpr->get_name(); |
---|
470 | } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( func ) ) { |
---|
471 | return varExpr->get_var()->get_name(); |
---|
472 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( func ) ) { |
---|
473 | return funcName( castExpr->get_arg() ); |
---|
474 | } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( func ) ) { |
---|
475 | return memberExpr->get_member()->get_name(); |
---|
476 | } else if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * > ( func ) ) { |
---|
477 | return funcName( memberExpr->get_member() ); |
---|
478 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( func ) ) { |
---|
479 | return handleDerefName( untypedExpr ); |
---|
480 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( func ) ) { |
---|
481 | return handleDerefName( appExpr ); |
---|
482 | } else if ( ConstructorExpr * ctorExpr = dynamic_cast< ConstructorExpr * >( func ) ) { |
---|
483 | return funcName( getCallArg( ctorExpr->get_callExpr(), 0 ) ); |
---|
484 | } else { |
---|
485 | assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() ); |
---|
486 | } |
---|
487 | } |
---|
488 | } |
---|
489 | |
---|
490 | std::string getFunctionName( Expression * expr ) { |
---|
491 | // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and |
---|
492 | // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction |
---|
493 | // can't possibly do anything reasonable. |
---|
494 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) { |
---|
495 | return funcName( appExpr->get_function() ); |
---|
496 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) { |
---|
497 | return funcName( untypedExpr->get_function() ); |
---|
498 | } else { |
---|
499 | std::cerr << expr << std::endl; |
---|
500 | assertf( false, "Unexpected expression type passed to getFunctionName" ); |
---|
501 | } |
---|
502 | } |
---|
503 | |
---|
504 | Type * getPointerBase( Type * type ) { |
---|
505 | if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) { |
---|
506 | return ptrType->get_base(); |
---|
507 | } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) { |
---|
508 | return arrayType->get_base(); |
---|
509 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( type ) ) { |
---|
510 | return refType->get_base(); |
---|
511 | } else { |
---|
512 | return nullptr; |
---|
513 | } |
---|
514 | } |
---|
515 | |
---|
516 | Type * isPointerType( Type * type ) { |
---|
517 | if ( getPointerBase( type ) ) return type; |
---|
518 | else return nullptr; |
---|
519 | } |
---|
520 | |
---|
521 | ApplicationExpr * createBitwiseAssignment( Expression * dst, Expression * src ) { |
---|
522 | static FunctionDecl * assign = nullptr; |
---|
523 | if ( ! assign ) { |
---|
524 | // temporary? Generate a fake assignment operator to represent bitwise assignments. |
---|
525 | // This operator could easily exist as a real function, but it's tricky because nothing should resolve to this function. |
---|
526 | TypeDecl * td = new TypeDecl( "T", noStorageClasses, nullptr, TypeDecl::Dtype, true ); |
---|
527 | assign = new FunctionDecl( "?=?", noStorageClasses, LinkageSpec::Intrinsic, SymTab::genAssignType( new TypeInstType( noQualifiers, td->name, td ) ), nullptr ); |
---|
528 | } |
---|
529 | if ( dynamic_cast< ReferenceType * >( dst->result ) ) { |
---|
530 | for (int depth = dst->result->referenceDepth(); depth > 0; depth--) { |
---|
531 | dst = new AddressExpr( dst ); |
---|
532 | } |
---|
533 | } else { |
---|
534 | dst = new CastExpr( dst, new ReferenceType( noQualifiers, dst->result->clone() ) ); |
---|
535 | } |
---|
536 | if ( dynamic_cast< ReferenceType * >( src->result ) ) { |
---|
537 | for (int depth = src->result->referenceDepth(); depth > 0; depth--) { |
---|
538 | src = new AddressExpr( src ); |
---|
539 | } |
---|
540 | // src = new CastExpr( src, new ReferenceType( noQualifiers, src->result->stripReferences()->clone() ) ); |
---|
541 | } |
---|
542 | return new ApplicationExpr( VariableExpr::functionPointer( assign ), { dst, src } ); |
---|
543 | } |
---|
544 | |
---|
545 | struct ConstExprChecker : public WithShortCircuiting { |
---|
546 | // most expressions are not const expr |
---|
547 | void previsit( Expression * ) { isConstExpr = false; visit_children = false; } |
---|
548 | |
---|
549 | void previsit( AddressExpr *addressExpr ) { |
---|
550 | visit_children = false; |
---|
551 | |
---|
552 | // address of a variable or member expression is constexpr |
---|
553 | Expression * arg = addressExpr->get_arg(); |
---|
554 | if ( ! dynamic_cast< NameExpr * >( arg) && ! dynamic_cast< VariableExpr * >( arg ) && ! dynamic_cast< MemberExpr * >( arg ) && ! dynamic_cast< UntypedMemberExpr * >( arg ) ) isConstExpr = false; |
---|
555 | } |
---|
556 | |
---|
557 | // these expressions may be const expr, depending on their children |
---|
558 | void previsit( SizeofExpr * ) {} |
---|
559 | void previsit( AlignofExpr * ) {} |
---|
560 | void previsit( UntypedOffsetofExpr * ) {} |
---|
561 | void previsit( OffsetofExpr * ) {} |
---|
562 | void previsit( OffsetPackExpr * ) {} |
---|
563 | void previsit( AttrExpr * ) {} |
---|
564 | void previsit( CommaExpr * ) {} |
---|
565 | void previsit( LogicalExpr * ) {} |
---|
566 | void previsit( ConditionalExpr * ) {} |
---|
567 | void previsit( CastExpr * ) {} |
---|
568 | void previsit( ConstantExpr * ) {} |
---|
569 | |
---|
570 | void previsit( VariableExpr * varExpr ) { |
---|
571 | visit_children = false; |
---|
572 | |
---|
573 | if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( varExpr->result ) ) { |
---|
574 | long long int value; |
---|
575 | if ( inst->baseEnum->valueOf( varExpr->var, value ) ) { |
---|
576 | // enumerators are const expr |
---|
577 | return; |
---|
578 | } |
---|
579 | } |
---|
580 | isConstExpr = false; |
---|
581 | } |
---|
582 | |
---|
583 | bool isConstExpr = true; |
---|
584 | }; |
---|
585 | |
---|
586 | bool isConstExpr( Expression * expr ) { |
---|
587 | if ( expr ) { |
---|
588 | PassVisitor<ConstExprChecker> checker; |
---|
589 | expr->accept( checker ); |
---|
590 | return checker.pass.isConstExpr; |
---|
591 | } |
---|
592 | return true; |
---|
593 | } |
---|
594 | |
---|
595 | bool isConstExpr( Initializer * init ) { |
---|
596 | if ( init ) { |
---|
597 | PassVisitor<ConstExprChecker> checker; |
---|
598 | init->accept( checker ); |
---|
599 | return checker.pass.isConstExpr; |
---|
600 | } // if |
---|
601 | // for all intents and purposes, no initializer means const expr |
---|
602 | return true; |
---|
603 | } |
---|
604 | |
---|
605 | bool isConstructor( const std::string & str ) { return str == "?{}"; } |
---|
606 | bool isDestructor( const std::string & str ) { return str == "^?{}"; } |
---|
607 | bool isAssignment( const std::string & str ) { return str == "?=?"; } |
---|
608 | bool isCtorDtor( const std::string & str ) { return isConstructor( str ) || isDestructor( str ); } |
---|
609 | bool isCtorDtorAssign( const std::string & str ) { return isCtorDtor( str ) || isAssignment( str ); } |
---|
610 | |
---|
611 | FunctionDecl * isCopyFunction( Declaration * decl, const std::string & fname ) { |
---|
612 | FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl ); |
---|
613 | if ( ! function ) return nullptr; |
---|
614 | if ( function->name != fname ) return nullptr; |
---|
615 | FunctionType * ftype = function->type; |
---|
616 | if ( ftype->parameters.size() != 2 ) return nullptr; |
---|
617 | |
---|
618 | Type * t1 = getPointerBase( ftype->get_parameters().front()->get_type() ); |
---|
619 | Type * t2 = ftype->parameters.back()->get_type(); |
---|
620 | assert( t1 ); |
---|
621 | |
---|
622 | if ( ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, SymTab::Indexer() ) ) { |
---|
623 | return function; |
---|
624 | } else { |
---|
625 | return nullptr; |
---|
626 | } |
---|
627 | } |
---|
628 | |
---|
629 | FunctionDecl * isAssignment( Declaration * decl ) { |
---|
630 | return isCopyFunction( decl, "?=?" ); |
---|
631 | } |
---|
632 | FunctionDecl * isDestructor( Declaration * decl ) { |
---|
633 | if ( isDestructor( decl->get_name() ) ) { |
---|
634 | return dynamic_cast< FunctionDecl * >( decl ); |
---|
635 | } |
---|
636 | return nullptr; |
---|
637 | } |
---|
638 | FunctionDecl * isDefaultConstructor( Declaration * decl ) { |
---|
639 | if ( isConstructor( decl->name ) ) { |
---|
640 | if ( FunctionDecl * func = dynamic_cast< FunctionDecl * >( decl ) ) { |
---|
641 | if ( func->type->parameters.size() == 1 ) { |
---|
642 | return func; |
---|
643 | } |
---|
644 | } |
---|
645 | } |
---|
646 | return nullptr; |
---|
647 | } |
---|
648 | FunctionDecl * isCopyConstructor( Declaration * decl ) { |
---|
649 | return isCopyFunction( decl, "?{}" ); |
---|
650 | } |
---|
651 | } |
---|