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