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 | // InitTweak.cc -- |
---|
8 | // |
---|
9 | // Author : Rob Schluntz |
---|
10 | // Created On : Fri May 13 11:26:36 2016 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Mon Dec 6 13:21:00 2021 |
---|
13 | // Update Count : 20 |
---|
14 | // |
---|
15 | |
---|
16 | #include <algorithm> // for find, all_of |
---|
17 | #include <cassert> // for assertf, assert, strict_dynamic_cast |
---|
18 | #include <iostream> // for ostream, cerr, endl |
---|
19 | #include <iterator> // for back_insert_iterator, back_inserter |
---|
20 | #include <memory> // for __shared_ptr |
---|
21 | #include <vector> |
---|
22 | |
---|
23 | #include "AST/Expr.hpp" |
---|
24 | #include "AST/Init.hpp" |
---|
25 | #include "AST/Node.hpp" |
---|
26 | #include "AST/Pass.hpp" |
---|
27 | #include "AST/Stmt.hpp" |
---|
28 | #include "AST/Type.hpp" |
---|
29 | #include "Common/PassVisitor.h" |
---|
30 | #include "Common/SemanticError.h" // for SemanticError |
---|
31 | #include "Common/UniqueName.h" // for UniqueName |
---|
32 | #include "Common/utility.h" // for toString, deleteAll, maybeClone |
---|
33 | #include "GenPoly/GenPoly.h" // for getFunctionType |
---|
34 | #include "InitTweak.h" |
---|
35 | #include "ResolvExpr/typeops.h" // for typesCompatibleIgnoreQualifiers |
---|
36 | #include "SymTab/Autogen.h" |
---|
37 | #include "SymTab/Indexer.h" // for Indexer |
---|
38 | #include "SynTree/LinkageSpec.h" // for Spec, isBuiltin, Intrinsic |
---|
39 | #include "SynTree/Attribute.h" // for Attribute |
---|
40 | #include "SynTree/Constant.h" // for Constant |
---|
41 | #include "SynTree/Declaration.h" // for ObjectDecl, DeclarationWithType |
---|
42 | #include "SynTree/Expression.h" // for Expression, UntypedExpr, Applicati... |
---|
43 | #include "SynTree/Initializer.h" // for Initializer, ListInit, Designation |
---|
44 | #include "SynTree/Label.h" // for Label |
---|
45 | #include "SynTree/Statement.h" // for CompoundStmt, ExprStmt, BranchStmt |
---|
46 | #include "SynTree/Type.h" // for FunctionType, ArrayType, PointerType |
---|
47 | #include "SynTree/Visitor.h" // for Visitor, maybeAccept |
---|
48 | #include "Tuples/Tuples.h" // for Tuples::isTtype |
---|
49 | |
---|
50 | namespace InitTweak { |
---|
51 | namespace { |
---|
52 | struct HasDesignations : public WithShortCircuiting { |
---|
53 | bool hasDesignations = false; |
---|
54 | |
---|
55 | void previsit( BaseSyntaxNode * ) { |
---|
56 | // short circuit if we already know there are designations |
---|
57 | if ( hasDesignations ) visit_children = false; |
---|
58 | } |
---|
59 | |
---|
60 | void previsit( Designation * des ) { |
---|
61 | // short circuit if we already know there are designations |
---|
62 | if ( hasDesignations ) visit_children = false; |
---|
63 | else if ( ! des->get_designators().empty() ) { |
---|
64 | hasDesignations = true; |
---|
65 | visit_children = false; |
---|
66 | } |
---|
67 | } |
---|
68 | }; |
---|
69 | |
---|
70 | struct InitDepthChecker : public WithGuards { |
---|
71 | bool depthOkay = true; |
---|
72 | Type * type; |
---|
73 | int curDepth = 0, maxDepth = 0; |
---|
74 | InitDepthChecker( Type * type ) : type( type ) { |
---|
75 | Type * t = type; |
---|
76 | while ( ArrayType * at = dynamic_cast< ArrayType * >( t ) ) { |
---|
77 | maxDepth++; |
---|
78 | t = at->get_base(); |
---|
79 | } |
---|
80 | maxDepth++; |
---|
81 | } |
---|
82 | void previsit( ListInit * ) { |
---|
83 | curDepth++; |
---|
84 | GuardAction( [this]() { curDepth--; } ); |
---|
85 | if ( curDepth > maxDepth ) depthOkay = false; |
---|
86 | } |
---|
87 | }; |
---|
88 | |
---|
89 | struct HasDesignations_new : public ast::WithShortCircuiting { |
---|
90 | bool result = false; |
---|
91 | |
---|
92 | void previsit( const ast::Node * ) { |
---|
93 | // short circuit if we already know there are designations |
---|
94 | if ( result ) visit_children = false; |
---|
95 | } |
---|
96 | |
---|
97 | void previsit( const ast::Designation * des ) { |
---|
98 | // short circuit if we already know there are designations |
---|
99 | if ( result ) visit_children = false; |
---|
100 | else if ( ! des->designators.empty() ) { |
---|
101 | result = true; |
---|
102 | visit_children = false; |
---|
103 | } |
---|
104 | } |
---|
105 | }; |
---|
106 | |
---|
107 | struct InitDepthChecker_new : public ast::WithGuards { |
---|
108 | bool result = true; |
---|
109 | const ast::Type * type; |
---|
110 | int curDepth = 0, maxDepth = 0; |
---|
111 | InitDepthChecker_new( const ast::Type * type ) : type( type ) { |
---|
112 | const ast::Type * t = type; |
---|
113 | while ( auto at = dynamic_cast< const ast::ArrayType * >( t ) ) { |
---|
114 | maxDepth++; |
---|
115 | t = at->base; |
---|
116 | } |
---|
117 | maxDepth++; |
---|
118 | } |
---|
119 | void previsit( ListInit * ) { |
---|
120 | curDepth++; |
---|
121 | GuardAction( [this]() { curDepth--; } ); |
---|
122 | if ( curDepth > maxDepth ) result = false; |
---|
123 | } |
---|
124 | }; |
---|
125 | |
---|
126 | struct InitFlattener_old : public WithShortCircuiting { |
---|
127 | void previsit( SingleInit * singleInit ) { |
---|
128 | visit_children = false; |
---|
129 | argList.push_back( singleInit->value->clone() ); |
---|
130 | } |
---|
131 | std::list< Expression * > argList; |
---|
132 | }; |
---|
133 | |
---|
134 | struct InitFlattener_new : public ast::WithShortCircuiting { |
---|
135 | std::vector< ast::ptr< ast::Expr > > argList; |
---|
136 | |
---|
137 | void previsit( const ast::SingleInit * singleInit ) { |
---|
138 | visit_children = false; |
---|
139 | argList.emplace_back( singleInit->value ); |
---|
140 | } |
---|
141 | }; |
---|
142 | |
---|
143 | } // anonymous namespace |
---|
144 | |
---|
145 | std::list< Expression * > makeInitList( Initializer * init ) { |
---|
146 | PassVisitor<InitFlattener_old> flattener; |
---|
147 | maybeAccept( init, flattener ); |
---|
148 | return flattener.pass.argList; |
---|
149 | } |
---|
150 | |
---|
151 | bool isDesignated( Initializer * init ) { |
---|
152 | PassVisitor<HasDesignations> finder; |
---|
153 | maybeAccept( init, finder ); |
---|
154 | return finder.pass.hasDesignations; |
---|
155 | } |
---|
156 | |
---|
157 | bool checkInitDepth( ObjectDecl * objDecl ) { |
---|
158 | PassVisitor<InitDepthChecker> checker( objDecl->type ); |
---|
159 | maybeAccept( objDecl->init, checker ); |
---|
160 | return checker.pass.depthOkay; |
---|
161 | } |
---|
162 | |
---|
163 | bool isDesignated( const ast::Init * init ) { |
---|
164 | ast::Pass<HasDesignations_new> finder; |
---|
165 | maybe_accept( init, finder ); |
---|
166 | return finder.core.result; |
---|
167 | } |
---|
168 | |
---|
169 | bool checkInitDepth( const ast::ObjectDecl * objDecl ) { |
---|
170 | ast::Pass<InitDepthChecker_new> checker( objDecl->type ); |
---|
171 | maybe_accept( objDecl->init.get(), checker ); |
---|
172 | return checker.core.result; |
---|
173 | } |
---|
174 | |
---|
175 | std::vector< ast::ptr< ast::Expr > > makeInitList( const ast::Init * init ) { |
---|
176 | ast::Pass< InitFlattener_new > flattener; |
---|
177 | maybe_accept( init, flattener ); |
---|
178 | return std::move( flattener.core.argList ); |
---|
179 | } |
---|
180 | |
---|
181 | class InitExpander_old::ExpanderImpl { |
---|
182 | public: |
---|
183 | virtual ~ExpanderImpl() = default; |
---|
184 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) = 0; |
---|
185 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ) = 0; |
---|
186 | }; |
---|
187 | |
---|
188 | class InitImpl_old : public InitExpander_old::ExpanderImpl { |
---|
189 | public: |
---|
190 | InitImpl_old( Initializer * init ) : init( init ) {} |
---|
191 | virtual ~InitImpl_old() = default; |
---|
192 | |
---|
193 | virtual std::list< Expression * > next( __attribute((unused)) std::list< Expression * > & indices ) { |
---|
194 | // this is wrong, but just a placeholder for now |
---|
195 | // if ( ! flattened ) flatten( indices ); |
---|
196 | // return ! inits.empty() ? makeInitList( inits.front() ) : std::list< Expression * >(); |
---|
197 | return makeInitList( init ); |
---|
198 | } |
---|
199 | |
---|
200 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ); |
---|
201 | private: |
---|
202 | Initializer * init; |
---|
203 | }; |
---|
204 | |
---|
205 | class ExprImpl_old : public InitExpander_old::ExpanderImpl { |
---|
206 | public: |
---|
207 | ExprImpl_old( Expression * expr ) : arg( expr ) {} |
---|
208 | virtual ~ExprImpl_old() { delete arg; } |
---|
209 | |
---|
210 | virtual std::list< Expression * > next( std::list< Expression * > & indices ) { |
---|
211 | std::list< Expression * > ret; |
---|
212 | Expression * expr = maybeClone( arg ); |
---|
213 | if ( expr ) { |
---|
214 | for ( std::list< Expression * >::reverse_iterator it = indices.rbegin(); it != indices.rend(); ++it ) { |
---|
215 | // go through indices and layer on subscript exprs ?[?] |
---|
216 | ++it; |
---|
217 | UntypedExpr * subscriptExpr = new UntypedExpr( new NameExpr( "?[?]") ); |
---|
218 | subscriptExpr->get_args().push_back( expr ); |
---|
219 | subscriptExpr->get_args().push_back( (*it)->clone() ); |
---|
220 | expr = subscriptExpr; |
---|
221 | } |
---|
222 | ret.push_back( expr ); |
---|
223 | } |
---|
224 | return ret; |
---|
225 | } |
---|
226 | |
---|
227 | virtual Statement * buildListInit( UntypedExpr * callExpr, std::list< Expression * > & indices ); |
---|
228 | private: |
---|
229 | Expression * arg; |
---|
230 | }; |
---|
231 | |
---|
232 | InitExpander_old::InitExpander_old( Initializer * init ) : expander( new InitImpl_old( init ) ) {} |
---|
233 | |
---|
234 | InitExpander_old::InitExpander_old( Expression * expr ) : expander( new ExprImpl_old( expr ) ) {} |
---|
235 | |
---|
236 | std::list< Expression * > InitExpander_old::operator*() { |
---|
237 | return cur; |
---|
238 | } |
---|
239 | |
---|
240 | InitExpander_old & InitExpander_old::operator++() { |
---|
241 | cur = expander->next( indices ); |
---|
242 | return *this; |
---|
243 | } |
---|
244 | |
---|
245 | // use array indices list to build switch statement |
---|
246 | void InitExpander_old::addArrayIndex( Expression * index, Expression * dimension ) { |
---|
247 | indices.push_back( index ); |
---|
248 | indices.push_back( dimension ); |
---|
249 | } |
---|
250 | |
---|
251 | void InitExpander_old::clearArrayIndices() { |
---|
252 | deleteAll( indices ); |
---|
253 | indices.clear(); |
---|
254 | } |
---|
255 | |
---|
256 | bool InitExpander_old::addReference() { |
---|
257 | bool added = false; |
---|
258 | for ( Expression *& expr : cur ) { |
---|
259 | expr = new AddressExpr( expr ); |
---|
260 | added = true; |
---|
261 | } |
---|
262 | return added; |
---|
263 | } |
---|
264 | |
---|
265 | namespace { |
---|
266 | /// given index i, dimension d, initializer init, and callExpr f, generates |
---|
267 | /// if (i < d) f(..., init) |
---|
268 | /// ++i; |
---|
269 | /// so that only elements within the range of the array are constructed |
---|
270 | template< typename OutIterator > |
---|
271 | void buildCallExpr( UntypedExpr * callExpr, Expression * index, Expression * dimension, Initializer * init, OutIterator out ) { |
---|
272 | UntypedExpr * cond = new UntypedExpr( new NameExpr( "?<?") ); |
---|
273 | cond->get_args().push_back( index->clone() ); |
---|
274 | cond->get_args().push_back( dimension->clone() ); |
---|
275 | |
---|
276 | std::list< Expression * > args = makeInitList( init ); |
---|
277 | callExpr->get_args().splice( callExpr->get_args().end(), args ); |
---|
278 | |
---|
279 | *out++ = new IfStmt( cond, new ExprStmt( callExpr ), nullptr ); |
---|
280 | |
---|
281 | UntypedExpr * increment = new UntypedExpr( new NameExpr( "++?" ) ); |
---|
282 | increment->get_args().push_back( index->clone() ); |
---|
283 | *out++ = new ExprStmt( increment ); |
---|
284 | } |
---|
285 | |
---|
286 | template< typename OutIterator > |
---|
287 | void build( UntypedExpr * callExpr, InitExpander_old::IndexList::iterator idx, InitExpander_old::IndexList::iterator idxEnd, Initializer * init, OutIterator out ) { |
---|
288 | if ( idx == idxEnd ) return; |
---|
289 | Expression * index = *idx++; |
---|
290 | assert( idx != idxEnd ); |
---|
291 | Expression * dimension = *idx++; |
---|
292 | |
---|
293 | // xxx - may want to eventually issue a warning here if we can detect |
---|
294 | // that the number of elements exceeds to dimension of the array |
---|
295 | if ( idx == idxEnd ) { |
---|
296 | if ( ListInit * listInit = dynamic_cast< ListInit * >( init ) ) { |
---|
297 | for ( Initializer * init : *listInit ) { |
---|
298 | buildCallExpr( callExpr->clone(), index, dimension, init, out ); |
---|
299 | } |
---|
300 | } else { |
---|
301 | buildCallExpr( callExpr->clone(), index, dimension, init, out ); |
---|
302 | } |
---|
303 | } else { |
---|
304 | std::list< Statement * > branches; |
---|
305 | |
---|
306 | unsigned long cond = 0; |
---|
307 | ListInit * listInit = dynamic_cast< ListInit * >( init ); |
---|
308 | if ( ! listInit ) { |
---|
309 | // xxx - this shouldn't be an error, but need a way to |
---|
310 | // terminate without creating output, so should catch this error |
---|
311 | SemanticError( init->location, "unbalanced list initializers" ); |
---|
312 | } |
---|
313 | |
---|
314 | static UniqueName targetLabel( "L__autogen__" ); |
---|
315 | Label switchLabel( targetLabel.newName(), 0, std::list< Attribute * >{ new Attribute("unused") } ); |
---|
316 | for ( Initializer * init : *listInit ) { |
---|
317 | Expression * condition; |
---|
318 | // check for designations |
---|
319 | // if ( init-> ) { |
---|
320 | condition = new ConstantExpr( Constant::from_ulong( cond ) ); |
---|
321 | ++cond; |
---|
322 | // } else { |
---|
323 | // condition = // ... take designation |
---|
324 | // cond = // ... take designation+1 |
---|
325 | // } |
---|
326 | std::list< Statement * > stmts; |
---|
327 | build( callExpr, idx, idxEnd, init, back_inserter( stmts ) ); |
---|
328 | stmts.push_back( new BranchStmt( switchLabel, BranchStmt::Break ) ); |
---|
329 | CaseStmt * caseStmt = new CaseStmt( condition, stmts ); |
---|
330 | branches.push_back( caseStmt ); |
---|
331 | } |
---|
332 | *out++ = new SwitchStmt( index->clone(), branches ); |
---|
333 | *out++ = new NullStmt( { switchLabel } ); |
---|
334 | } |
---|
335 | } |
---|
336 | } |
---|
337 | |
---|
338 | // if array came with an initializer list: initialize each element |
---|
339 | // may have more initializers than elements in the array - need to check at each index that |
---|
340 | // we haven't exceeded size. |
---|
341 | // may have fewer initializers than elements in the array - need to default construct |
---|
342 | // remaining elements. |
---|
343 | // To accomplish this, generate switch statement, consuming all of expander's elements |
---|
344 | Statement * InitImpl_old::buildListInit( UntypedExpr * dst, std::list< Expression * > & indices ) { |
---|
345 | if ( ! init ) return nullptr; |
---|
346 | CompoundStmt * block = new CompoundStmt(); |
---|
347 | build( dst, indices.begin(), indices.end(), init, back_inserter( block->get_kids() ) ); |
---|
348 | if ( block->get_kids().empty() ) { |
---|
349 | delete block; |
---|
350 | return nullptr; |
---|
351 | } else { |
---|
352 | init = nullptr; // init was consumed in creating the list init |
---|
353 | return block; |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | Statement * ExprImpl_old::buildListInit( UntypedExpr *, std::list< Expression * > & ) { |
---|
358 | return nullptr; |
---|
359 | } |
---|
360 | |
---|
361 | Statement * InitExpander_old::buildListInit( UntypedExpr * dst ) { |
---|
362 | return expander->buildListInit( dst, indices ); |
---|
363 | } |
---|
364 | |
---|
365 | class InitExpander_new::ExpanderImpl { |
---|
366 | public: |
---|
367 | virtual ~ExpanderImpl() = default; |
---|
368 | virtual std::vector< ast::ptr< ast::Expr > > next( IndexList & indices ) = 0; |
---|
369 | virtual ast::ptr< ast::Stmt > buildListInit( |
---|
370 | ast::UntypedExpr * callExpr, IndexList & indices ) = 0; |
---|
371 | }; |
---|
372 | |
---|
373 | namespace { |
---|
374 | template< typename Out > |
---|
375 | void buildCallExpr( |
---|
376 | ast::UntypedExpr * callExpr, const ast::Expr * index, const ast::Expr * dimension, |
---|
377 | const ast::Init * init, Out & out |
---|
378 | ) { |
---|
379 | const CodeLocation & loc = init->location; |
---|
380 | |
---|
381 | auto cond = new ast::UntypedExpr{ |
---|
382 | loc, new ast::NameExpr{ loc, "?<?" }, { index, dimension } }; |
---|
383 | |
---|
384 | std::vector< ast::ptr< ast::Expr > > args = makeInitList( init ); |
---|
385 | splice( callExpr->args, args ); |
---|
386 | |
---|
387 | out.emplace_back( new ast::IfStmt{ loc, cond, new ast::ExprStmt{ loc, callExpr } } ); |
---|
388 | |
---|
389 | out.emplace_back( new ast::ExprStmt{ |
---|
390 | loc, new ast::UntypedExpr{ loc, new ast::NameExpr{ loc, "++?" }, { index } } } ); |
---|
391 | } |
---|
392 | |
---|
393 | template< typename Out > |
---|
394 | void build( |
---|
395 | ast::UntypedExpr * callExpr, const InitExpander_new::IndexList & indices, |
---|
396 | const ast::Init * init, Out & out |
---|
397 | ) { |
---|
398 | if ( indices.empty() ) return; |
---|
399 | |
---|
400 | unsigned idx = 0; |
---|
401 | |
---|
402 | const ast::Expr * index = indices[idx++]; |
---|
403 | assert( idx != indices.size() ); |
---|
404 | const ast::Expr * dimension = indices[idx++]; |
---|
405 | |
---|
406 | if ( idx == indices.size() ) { |
---|
407 | if ( auto listInit = dynamic_cast< const ast::ListInit * >( init ) ) { |
---|
408 | for ( const ast::Init * init : *listInit ) { |
---|
409 | buildCallExpr( shallowCopy(callExpr), index, dimension, init, out ); |
---|
410 | } |
---|
411 | } else { |
---|
412 | buildCallExpr( shallowCopy(callExpr), index, dimension, init, out ); |
---|
413 | } |
---|
414 | } else { |
---|
415 | const CodeLocation & loc = init->location; |
---|
416 | |
---|
417 | unsigned long cond = 0; |
---|
418 | auto listInit = dynamic_cast< const ast::ListInit * >( init ); |
---|
419 | if ( ! listInit ) { SemanticError( loc, "unbalanced list initializers" ); } |
---|
420 | |
---|
421 | static UniqueName targetLabel( "L__autogen__" ); |
---|
422 | ast::Label switchLabel{ |
---|
423 | loc, targetLabel.newName(), { new ast::Attribute{ "unused" } } }; |
---|
424 | |
---|
425 | std::vector< ast::ptr< ast::Stmt > > branches; |
---|
426 | for ( const ast::Init * init : *listInit ) { |
---|
427 | auto condition = ast::ConstantExpr::from_ulong( loc, cond ); |
---|
428 | ++cond; |
---|
429 | |
---|
430 | std::vector< ast::ptr< ast::Stmt > > stmts; |
---|
431 | build( callExpr, indices, init, stmts ); |
---|
432 | stmts.emplace_back( |
---|
433 | new ast::BranchStmt{ loc, ast::BranchStmt::Break, switchLabel } ); |
---|
434 | branches.emplace_back( new ast::CaseStmt{ loc, condition, std::move( stmts ) } ); |
---|
435 | } |
---|
436 | out.emplace_back( new ast::SwitchStmt{ loc, index, std::move( branches ) } ); |
---|
437 | out.emplace_back( new ast::NullStmt{ loc, { switchLabel } } ); |
---|
438 | } |
---|
439 | } |
---|
440 | |
---|
441 | class InitImpl_new final : public InitExpander_new::ExpanderImpl { |
---|
442 | ast::ptr< ast::Init > init; |
---|
443 | public: |
---|
444 | InitImpl_new( const ast::Init * i ) : init( i ) {} |
---|
445 | |
---|
446 | std::vector< ast::ptr< ast::Expr > > next( InitExpander_new::IndexList & ) override { |
---|
447 | return makeInitList( init ); |
---|
448 | } |
---|
449 | |
---|
450 | ast::ptr< ast::Stmt > buildListInit( |
---|
451 | ast::UntypedExpr * callExpr, InitExpander_new::IndexList & indices |
---|
452 | ) override { |
---|
453 | // If array came with an initializer list, initialize each element. We may have more |
---|
454 | // initializers than elements of the array; need to check at each index that we have |
---|
455 | // not exceeded size. We may have fewer initializers than elements in the array; need |
---|
456 | // to default-construct remaining elements. To accomplish this, generate switch |
---|
457 | // statement consuming all of expander's elements |
---|
458 | |
---|
459 | if ( ! init ) return {}; |
---|
460 | |
---|
461 | std::list< ast::ptr< ast::Stmt > > stmts; |
---|
462 | build( callExpr, indices, init, stmts ); |
---|
463 | if ( stmts.empty() ) { |
---|
464 | return {}; |
---|
465 | } else { |
---|
466 | auto block = new ast::CompoundStmt{ init->location, std::move( stmts ) }; |
---|
467 | init = nullptr; // consumed in creating the list init |
---|
468 | return block; |
---|
469 | } |
---|
470 | } |
---|
471 | }; |
---|
472 | |
---|
473 | class ExprImpl_new final : public InitExpander_new::ExpanderImpl { |
---|
474 | ast::ptr< ast::Expr > arg; |
---|
475 | public: |
---|
476 | ExprImpl_new( const ast::Expr * a ) : arg( a ) {} |
---|
477 | |
---|
478 | std::vector< ast::ptr< ast::Expr > > next( |
---|
479 | InitExpander_new::IndexList & indices |
---|
480 | ) override { |
---|
481 | if ( ! arg ) return {}; |
---|
482 | |
---|
483 | const CodeLocation & loc = arg->location; |
---|
484 | const ast::Expr * expr = arg; |
---|
485 | for ( auto it = indices.rbegin(); it != indices.rend(); ++it ) { |
---|
486 | // go through indices and layer on subscript exprs ?[?] |
---|
487 | ++it; |
---|
488 | expr = new ast::UntypedExpr{ |
---|
489 | loc, new ast::NameExpr{ loc, "?[?]" }, { expr, *it } }; |
---|
490 | } |
---|
491 | return { expr }; |
---|
492 | } |
---|
493 | |
---|
494 | ast::ptr< ast::Stmt > buildListInit( |
---|
495 | ast::UntypedExpr *, InitExpander_new::IndexList & |
---|
496 | ) override { |
---|
497 | return {}; |
---|
498 | } |
---|
499 | }; |
---|
500 | } // anonymous namespace |
---|
501 | |
---|
502 | InitExpander_new::InitExpander_new( const ast::Init * init ) |
---|
503 | : expander( new InitImpl_new{ init } ), crnt(), indices() {} |
---|
504 | |
---|
505 | InitExpander_new::InitExpander_new( const ast::Expr * expr ) |
---|
506 | : expander( new ExprImpl_new{ expr } ), crnt(), indices() {} |
---|
507 | |
---|
508 | std::vector< ast::ptr< ast::Expr > > InitExpander_new::operator* () { return crnt; } |
---|
509 | |
---|
510 | InitExpander_new & InitExpander_new::operator++ () { |
---|
511 | crnt = expander->next( indices ); |
---|
512 | return *this; |
---|
513 | } |
---|
514 | |
---|
515 | /// builds statement which has the same semantics as a C-style list initializer (for array |
---|
516 | /// initializers) using callExpr as the base expression to perform initialization |
---|
517 | ast::ptr< ast::Stmt > InitExpander_new::buildListInit( ast::UntypedExpr * callExpr ) { |
---|
518 | return expander->buildListInit( callExpr, indices ); |
---|
519 | } |
---|
520 | |
---|
521 | void InitExpander_new::addArrayIndex( const ast::Expr * index, const ast::Expr * dimension ) { |
---|
522 | indices.emplace_back( index ); |
---|
523 | indices.emplace_back( dimension ); |
---|
524 | } |
---|
525 | |
---|
526 | void InitExpander_new::clearArrayIndices() { indices.clear(); } |
---|
527 | |
---|
528 | bool InitExpander_new::addReference() { |
---|
529 | for ( ast::ptr< ast::Expr > & expr : crnt ) { |
---|
530 | expr = new ast::AddressExpr{ expr }; |
---|
531 | } |
---|
532 | return ! crnt.empty(); |
---|
533 | } |
---|
534 | |
---|
535 | Type * getTypeofThis( FunctionType * ftype ) { |
---|
536 | assertf( ftype, "getTypeofThis: nullptr ftype" ); |
---|
537 | ObjectDecl * thisParam = getParamThis( ftype ); |
---|
538 | ReferenceType * refType = strict_dynamic_cast< ReferenceType * >( thisParam->type ); |
---|
539 | return refType->base; |
---|
540 | } |
---|
541 | |
---|
542 | const ast::Type * getTypeofThis( const ast::FunctionType * ftype ) { |
---|
543 | assertf( ftype, "getTypeofThis: nullptr ftype" ); |
---|
544 | const std::vector<ast::ptr<ast::Type>> & params = ftype->params; |
---|
545 | assertf( !params.empty(), "getTypeofThis: ftype with 0 parameters: %s", |
---|
546 | toString( ftype ).c_str() ); |
---|
547 | const ast::ReferenceType * refType = |
---|
548 | params.front().strict_as<ast::ReferenceType>(); |
---|
549 | return refType->base; |
---|
550 | } |
---|
551 | |
---|
552 | ObjectDecl * getParamThis( FunctionType * ftype ) { |
---|
553 | assertf( ftype, "getParamThis: nullptr ftype" ); |
---|
554 | auto & params = ftype->parameters; |
---|
555 | assertf( ! params.empty(), "getParamThis: ftype with 0 parameters: %s", toString( ftype ).c_str() ); |
---|
556 | return strict_dynamic_cast< ObjectDecl * >( params.front() ); |
---|
557 | } |
---|
558 | |
---|
559 | const ast::ObjectDecl * getParamThis(const ast::FunctionDecl * func) { |
---|
560 | assertf( func, "getParamThis: nullptr ftype" ); |
---|
561 | auto & params = func->params; |
---|
562 | assertf( ! params.empty(), "getParamThis: ftype with 0 parameters: %s", toString( func ).c_str()); |
---|
563 | return params.front().strict_as<ast::ObjectDecl>(); |
---|
564 | } |
---|
565 | |
---|
566 | bool tryConstruct( DeclarationWithType * dwt ) { |
---|
567 | ObjectDecl * objDecl = dynamic_cast< ObjectDecl * >( dwt ); |
---|
568 | if ( ! objDecl ) return false; |
---|
569 | return (objDecl->get_init() == nullptr || |
---|
570 | ( objDecl->get_init() != nullptr && objDecl->get_init()->get_maybeConstructed() )) |
---|
571 | && ! objDecl->get_storageClasses().is_extern |
---|
572 | && isConstructable( objDecl->type ); |
---|
573 | } |
---|
574 | |
---|
575 | bool isConstructable( Type * type ) { |
---|
576 | return ! dynamic_cast< VarArgsType * >( type ) && ! dynamic_cast< ReferenceType * >( type ) && ! dynamic_cast< FunctionType * >( type ) && ! Tuples::isTtype( type ); |
---|
577 | } |
---|
578 | |
---|
579 | bool tryConstruct( const ast::DeclWithType * dwt ) { |
---|
580 | auto objDecl = dynamic_cast< const ast::ObjectDecl * >( dwt ); |
---|
581 | if ( ! objDecl ) return false; |
---|
582 | return (objDecl->init == nullptr || |
---|
583 | ( objDecl->init != nullptr && objDecl->init->maybeConstructed )) |
---|
584 | && ! objDecl->storage.is_extern |
---|
585 | && isConstructable( objDecl->type ); |
---|
586 | } |
---|
587 | |
---|
588 | bool isConstructable( const ast::Type * type ) { |
---|
589 | return ! dynamic_cast< const ast::VarArgsType * >( type ) && ! dynamic_cast< const ast::ReferenceType * >( type ) |
---|
590 | && ! dynamic_cast< const ast::FunctionType * >( type ) && ! Tuples::isTtype( type ); |
---|
591 | } |
---|
592 | |
---|
593 | struct CallFinder_old { |
---|
594 | CallFinder_old( const std::list< std::string > & names ) : names( names ) {} |
---|
595 | |
---|
596 | void postvisit( ApplicationExpr * appExpr ) { |
---|
597 | handleCallExpr( appExpr ); |
---|
598 | } |
---|
599 | |
---|
600 | void postvisit( UntypedExpr * untypedExpr ) { |
---|
601 | handleCallExpr( untypedExpr ); |
---|
602 | } |
---|
603 | |
---|
604 | std::list< Expression * > * matches; |
---|
605 | private: |
---|
606 | const std::list< std::string > names; |
---|
607 | |
---|
608 | template< typename CallExpr > |
---|
609 | void handleCallExpr( CallExpr * expr ) { |
---|
610 | std::string fname = getFunctionName( expr ); |
---|
611 | if ( std::find( names.begin(), names.end(), fname ) != names.end() ) { |
---|
612 | matches->push_back( expr ); |
---|
613 | } |
---|
614 | } |
---|
615 | }; |
---|
616 | |
---|
617 | struct CallFinder_new final { |
---|
618 | std::vector< const ast::Expr * > matches; |
---|
619 | const std::vector< std::string > names; |
---|
620 | |
---|
621 | CallFinder_new( std::vector< std::string > && ns ) : matches(), names( std::move(ns) ) {} |
---|
622 | |
---|
623 | void handleCallExpr( const ast::Expr * expr ) { |
---|
624 | std::string fname = getFunctionName( expr ); |
---|
625 | if ( std::find( names.begin(), names.end(), fname ) != names.end() ) { |
---|
626 | matches.emplace_back( expr ); |
---|
627 | } |
---|
628 | } |
---|
629 | |
---|
630 | void postvisit( const ast::ApplicationExpr * expr ) { handleCallExpr( expr ); } |
---|
631 | void postvisit( const ast::UntypedExpr * expr ) { handleCallExpr( expr ); } |
---|
632 | }; |
---|
633 | |
---|
634 | void collectCtorDtorCalls( Statement * stmt, std::list< Expression * > & matches ) { |
---|
635 | static PassVisitor<CallFinder_old> finder( std::list< std::string >{ "?{}", "^?{}" } ); |
---|
636 | finder.pass.matches = &matches; |
---|
637 | maybeAccept( stmt, finder ); |
---|
638 | } |
---|
639 | |
---|
640 | std::vector< const ast::Expr * > collectCtorDtorCalls( const ast::Stmt * stmt ) { |
---|
641 | ast::Pass< CallFinder_new > finder{ std::vector< std::string >{ "?{}", "^?{}" } }; |
---|
642 | maybe_accept( stmt, finder ); |
---|
643 | return std::move( finder.core.matches ); |
---|
644 | } |
---|
645 | |
---|
646 | Expression * getCtorDtorCall( Statement * stmt ) { |
---|
647 | std::list< Expression * > matches; |
---|
648 | collectCtorDtorCalls( stmt, matches ); |
---|
649 | assertf( matches.size() <= 1, "%zd constructor/destructors found in %s", matches.size(), toString( stmt ).c_str() ); |
---|
650 | return matches.size() == 1 ? matches.front() : nullptr; |
---|
651 | } |
---|
652 | |
---|
653 | namespace { |
---|
654 | DeclarationWithType * getCalledFunction( Expression * expr ); |
---|
655 | const ast::DeclWithType * getCalledFunction( const ast::Expr * expr ); |
---|
656 | |
---|
657 | template<typename CallExpr> |
---|
658 | DeclarationWithType * handleDerefCalledFunction( CallExpr * expr ) { |
---|
659 | // (*f)(x) => should get "f" |
---|
660 | std::string name = getFunctionName( expr ); |
---|
661 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
662 | assertf( ! expr->get_args().empty(), "Cannot get called function from dereference with no arguments" ); |
---|
663 | return getCalledFunction( expr->get_args().front() ); |
---|
664 | } |
---|
665 | |
---|
666 | template<typename CallExpr> |
---|
667 | const ast::DeclWithType * handleDerefCalledFunction( const CallExpr * expr ) { |
---|
668 | // (*f)(x) => should get "f" |
---|
669 | std::string name = getFunctionName( expr ); |
---|
670 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
671 | assertf( ! expr->args.empty(), "Cannot get called function from dereference with no arguments" ); |
---|
672 | return getCalledFunction( expr->args.front() ); |
---|
673 | } |
---|
674 | |
---|
675 | |
---|
676 | DeclarationWithType * getCalledFunction( Expression * expr ) { |
---|
677 | assert( expr ); |
---|
678 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( expr ) ) { |
---|
679 | return varExpr->var; |
---|
680 | } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( expr ) ) { |
---|
681 | return memberExpr->member; |
---|
682 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) { |
---|
683 | return getCalledFunction( castExpr->arg ); |
---|
684 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( expr ) ) { |
---|
685 | return handleDerefCalledFunction( untypedExpr ); |
---|
686 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) { |
---|
687 | return handleDerefCalledFunction( appExpr ); |
---|
688 | } else if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( expr ) ) { |
---|
689 | return getCalledFunction( addrExpr->arg ); |
---|
690 | } else if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( expr ) ) { |
---|
691 | return getCalledFunction( commaExpr->arg2 ); |
---|
692 | } |
---|
693 | return nullptr; |
---|
694 | } |
---|
695 | |
---|
696 | const ast::DeclWithType * getCalledFunction( const ast::Expr * expr ) { |
---|
697 | assert( expr ); |
---|
698 | if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( expr ) ) { |
---|
699 | return varExpr->var; |
---|
700 | } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( expr ) ) { |
---|
701 | return memberExpr->member; |
---|
702 | } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( expr ) ) { |
---|
703 | return getCalledFunction( castExpr->arg ); |
---|
704 | } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( expr ) ) { |
---|
705 | return handleDerefCalledFunction( untypedExpr ); |
---|
706 | } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * > ( expr ) ) { |
---|
707 | return handleDerefCalledFunction( appExpr ); |
---|
708 | } else if ( const ast::AddressExpr * addrExpr = dynamic_cast< const ast::AddressExpr * >( expr ) ) { |
---|
709 | return getCalledFunction( addrExpr->arg ); |
---|
710 | } else if ( const ast::CommaExpr * commaExpr = dynamic_cast< const ast::CommaExpr * >( expr ) ) { |
---|
711 | return getCalledFunction( commaExpr->arg2 ); |
---|
712 | } |
---|
713 | return nullptr; |
---|
714 | } |
---|
715 | |
---|
716 | DeclarationWithType * getFunctionCore( const Expression * expr ) { |
---|
717 | if ( const auto * appExpr = dynamic_cast< const ApplicationExpr * >( expr ) ) { |
---|
718 | return getCalledFunction( appExpr->function ); |
---|
719 | } else if ( const auto * untyped = dynamic_cast< const UntypedExpr * >( expr ) ) { |
---|
720 | return getCalledFunction( untyped->function ); |
---|
721 | } |
---|
722 | assertf( false, "getFunction with unknown expression: %s", toString( expr ).c_str() ); |
---|
723 | } |
---|
724 | } |
---|
725 | |
---|
726 | DeclarationWithType * getFunction( Expression * expr ) { |
---|
727 | return getFunctionCore( expr ); |
---|
728 | } |
---|
729 | |
---|
730 | const DeclarationWithType * getFunction( const Expression * expr ) { |
---|
731 | return getFunctionCore( expr ); |
---|
732 | } |
---|
733 | |
---|
734 | const ast::DeclWithType * getFunction( const ast::Expr * expr ) { |
---|
735 | if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ) ) { |
---|
736 | return getCalledFunction( appExpr->func ); |
---|
737 | } else if ( const ast::UntypedExpr * untyped = dynamic_cast< const ast::UntypedExpr * > ( expr ) ) { |
---|
738 | return getCalledFunction( untyped->func ); |
---|
739 | } |
---|
740 | assertf( false, "getFunction received unknown expression: %s", toString( expr ).c_str() ); |
---|
741 | } |
---|
742 | |
---|
743 | ApplicationExpr * isIntrinsicCallExpr( Expression * expr ) { |
---|
744 | ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ); |
---|
745 | if ( ! appExpr ) return nullptr; |
---|
746 | DeclarationWithType * function = getCalledFunction( appExpr->get_function() ); |
---|
747 | assertf( function, "getCalledFunction returned nullptr: %s", toString( appExpr->get_function() ).c_str() ); |
---|
748 | // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor |
---|
749 | // will call all member dtors, and some members may have a user defined dtor. |
---|
750 | return function->get_linkage() == LinkageSpec::Intrinsic ? appExpr : nullptr; |
---|
751 | } |
---|
752 | |
---|
753 | const ast::ApplicationExpr * isIntrinsicCallExpr( const ast::Expr * expr ) { |
---|
754 | auto appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ); |
---|
755 | if ( ! appExpr ) return nullptr; |
---|
756 | |
---|
757 | const ast::DeclWithType * func = getCalledFunction( appExpr->func ); |
---|
758 | assertf( func, |
---|
759 | "getCalledFunction returned nullptr: %s", toString( appExpr->func ).c_str() ); |
---|
760 | |
---|
761 | // check for Intrinsic only -- don't want to remove all overridable ctor/dtor because |
---|
762 | // autogenerated ctor/dtor will call all member dtors, and some members may have a |
---|
763 | // user-defined dtor |
---|
764 | return func->linkage == ast::Linkage::Intrinsic ? appExpr : nullptr; |
---|
765 | } |
---|
766 | |
---|
767 | namespace { |
---|
768 | template <typename Predicate> |
---|
769 | bool allofCtorDtor( Statement * stmt, const Predicate & pred ) { |
---|
770 | std::list< Expression * > callExprs; |
---|
771 | collectCtorDtorCalls( stmt, callExprs ); |
---|
772 | // if ( callExprs.empty() ) return false; // xxx - do I still need this check? |
---|
773 | return std::all_of( callExprs.begin(), callExprs.end(), pred); |
---|
774 | } |
---|
775 | |
---|
776 | template <typename Predicate> |
---|
777 | bool allofCtorDtor( const ast::Stmt * stmt, const Predicate & pred ) { |
---|
778 | std::vector< const ast::Expr * > callExprs = collectCtorDtorCalls( stmt ); |
---|
779 | return std::all_of( callExprs.begin(), callExprs.end(), pred ); |
---|
780 | } |
---|
781 | } |
---|
782 | |
---|
783 | bool isIntrinsicSingleArgCallStmt( Statement * stmt ) { |
---|
784 | return allofCtorDtor( stmt, []( Expression * callExpr ){ |
---|
785 | if ( ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) { |
---|
786 | FunctionType *funcType = GenPoly::getFunctionType( appExpr->function->result ); |
---|
787 | assert( funcType ); |
---|
788 | return funcType->get_parameters().size() == 1; |
---|
789 | } |
---|
790 | return false; |
---|
791 | }); |
---|
792 | } |
---|
793 | |
---|
794 | bool isIntrinsicSingleArgCallStmt( const ast::Stmt * stmt ) { |
---|
795 | return allofCtorDtor( stmt, []( const ast::Expr * callExpr ){ |
---|
796 | if ( const ast::ApplicationExpr * appExpr = isIntrinsicCallExpr( callExpr ) ) { |
---|
797 | const ast::FunctionType * funcType = |
---|
798 | GenPoly::getFunctionType( appExpr->func->result ); |
---|
799 | assert( funcType ); |
---|
800 | return funcType->params.size() == 1; |
---|
801 | } |
---|
802 | return false; |
---|
803 | }); |
---|
804 | } |
---|
805 | |
---|
806 | bool isIntrinsicCallStmt( Statement * stmt ) { |
---|
807 | return allofCtorDtor( stmt, []( Expression * callExpr ) { |
---|
808 | return isIntrinsicCallExpr( callExpr ); |
---|
809 | }); |
---|
810 | } |
---|
811 | |
---|
812 | namespace { |
---|
813 | template<typename CallExpr> |
---|
814 | Expression *& callArg( CallExpr * callExpr, unsigned int pos ) { |
---|
815 | if ( pos >= callExpr->get_args().size() ) assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.", pos, toString( callExpr ).c_str() ); |
---|
816 | for ( Expression *& arg : callExpr->get_args() ) { |
---|
817 | if ( pos == 0 ) return arg; |
---|
818 | pos--; |
---|
819 | } |
---|
820 | assert( false ); |
---|
821 | } |
---|
822 | |
---|
823 | template<typename CallExpr> |
---|
824 | const ast::Expr * callArg( const CallExpr * call, unsigned int pos ) { |
---|
825 | if( pos >= call->args.size() ) { |
---|
826 | assertf( false, "getCallArg for argument that doesn't exist: (%u); %s.", |
---|
827 | pos, toString( call ).c_str() ); |
---|
828 | } |
---|
829 | for ( const ast::Expr * arg : call->args ) { |
---|
830 | if ( pos == 0 ) return arg; |
---|
831 | --pos; |
---|
832 | } |
---|
833 | assert( false ); |
---|
834 | } |
---|
835 | } |
---|
836 | |
---|
837 | Expression *& getCallArg( Expression * callExpr, unsigned int pos ) { |
---|
838 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr ) ) { |
---|
839 | return callArg( appExpr, pos ); |
---|
840 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( callExpr ) ) { |
---|
841 | return callArg( untypedExpr, pos ); |
---|
842 | } else if ( TupleAssignExpr * tupleExpr = dynamic_cast< TupleAssignExpr * > ( callExpr ) ) { |
---|
843 | std::list< Statement * > & stmts = tupleExpr->get_stmtExpr()->get_statements()->get_kids(); |
---|
844 | assertf( ! stmts.empty(), "TupleAssignExpr somehow has no statements." ); |
---|
845 | ExprStmt * stmt = strict_dynamic_cast< ExprStmt * >( stmts.back() ); |
---|
846 | TupleExpr * tuple = strict_dynamic_cast< TupleExpr * >( stmt->get_expr() ); |
---|
847 | assertf( ! tuple->get_exprs().empty(), "TupleAssignExpr somehow has empty tuple expr." ); |
---|
848 | return getCallArg( tuple->get_exprs().front(), pos ); |
---|
849 | } else if ( ImplicitCopyCtorExpr * copyCtor = dynamic_cast< ImplicitCopyCtorExpr * >( callExpr ) ) { |
---|
850 | return getCallArg( copyCtor->callExpr, pos ); |
---|
851 | } else { |
---|
852 | assertf( false, "Unexpected expression type passed to getCallArg: %s", toString( callExpr ).c_str() ); |
---|
853 | } |
---|
854 | } |
---|
855 | |
---|
856 | const ast::Expr * getCallArg( const ast::Expr * call, unsigned pos ) { |
---|
857 | if ( auto app = dynamic_cast< const ast::ApplicationExpr * >( call ) ) { |
---|
858 | return callArg( app, pos ); |
---|
859 | } else if ( auto untyped = dynamic_cast< const ast::UntypedExpr * >( call ) ) { |
---|
860 | return callArg( untyped, pos ); |
---|
861 | } else if ( auto tupleAssn = dynamic_cast< const ast::TupleAssignExpr * >( call ) ) { |
---|
862 | const std::list<ast::ptr<ast::Stmt>>& stmts = tupleAssn->stmtExpr->stmts->kids; |
---|
863 | assertf( ! stmts.empty(), "TupleAssignExpr missing statements." ); |
---|
864 | auto stmt = strict_dynamic_cast< const ast::ExprStmt * >( stmts.back().get() ); |
---|
865 | auto tuple = strict_dynamic_cast< const ast::TupleExpr * >( stmt->expr.get() ); |
---|
866 | assertf( ! tuple->exprs.empty(), "TupleAssignExpr has empty tuple expr."); |
---|
867 | return getCallArg( tuple->exprs.front(), pos ); |
---|
868 | } else if ( auto ctor = dynamic_cast< const ast::ImplicitCopyCtorExpr * >( call ) ) { |
---|
869 | return getCallArg( ctor->callExpr, pos ); |
---|
870 | } else { |
---|
871 | assertf( false, "Unexpected expression type passed to getCallArg: %s", |
---|
872 | toString( call ).c_str() ); |
---|
873 | } |
---|
874 | } |
---|
875 | |
---|
876 | namespace { |
---|
877 | std::string funcName( Expression * func ); |
---|
878 | std::string funcName( const ast::Expr * func ); |
---|
879 | |
---|
880 | template<typename CallExpr> |
---|
881 | std::string handleDerefName( CallExpr * expr ) { |
---|
882 | // (*f)(x) => should get name "f" |
---|
883 | std::string name = getFunctionName( expr ); |
---|
884 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
885 | assertf( ! expr->get_args().empty(), "Cannot get function name from dereference with no arguments" ); |
---|
886 | return funcName( expr->get_args().front() ); |
---|
887 | } |
---|
888 | |
---|
889 | template<typename CallExpr> |
---|
890 | std::string handleDerefName( const CallExpr * expr ) { |
---|
891 | // (*f)(x) => should get name "f" |
---|
892 | std::string name = getFunctionName( expr ); |
---|
893 | assertf( name == "*?", "Unexpected untyped expression: %s", name.c_str() ); |
---|
894 | assertf( ! expr->args.empty(), "Cannot get function name from dereference with no arguments" ); |
---|
895 | return funcName( expr->args.front() ); |
---|
896 | } |
---|
897 | |
---|
898 | std::string funcName( Expression * func ) { |
---|
899 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) { |
---|
900 | return nameExpr->get_name(); |
---|
901 | } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( func ) ) { |
---|
902 | return varExpr->get_var()->get_name(); |
---|
903 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( func ) ) { |
---|
904 | return funcName( castExpr->get_arg() ); |
---|
905 | } else if ( MemberExpr * memberExpr = dynamic_cast< MemberExpr * >( func ) ) { |
---|
906 | return memberExpr->get_member()->get_name(); |
---|
907 | } else if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * > ( func ) ) { |
---|
908 | return funcName( memberExpr->get_member() ); |
---|
909 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( func ) ) { |
---|
910 | return handleDerefName( untypedExpr ); |
---|
911 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( func ) ) { |
---|
912 | return handleDerefName( appExpr ); |
---|
913 | } else if ( ConstructorExpr * ctorExpr = dynamic_cast< ConstructorExpr * >( func ) ) { |
---|
914 | return funcName( getCallArg( ctorExpr->get_callExpr(), 0 ) ); |
---|
915 | } else { |
---|
916 | assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() ); |
---|
917 | } |
---|
918 | } |
---|
919 | |
---|
920 | std::string funcName( const ast::Expr * func ) { |
---|
921 | if ( const ast::NameExpr * nameExpr = dynamic_cast< const ast::NameExpr * >( func ) ) { |
---|
922 | return nameExpr->name; |
---|
923 | } else if ( const ast::VariableExpr * varExpr = dynamic_cast< const ast::VariableExpr * >( func ) ) { |
---|
924 | return varExpr->var->name; |
---|
925 | } else if ( const ast::CastExpr * castExpr = dynamic_cast< const ast::CastExpr * >( func ) ) { |
---|
926 | return funcName( castExpr->arg ); |
---|
927 | } else if ( const ast::MemberExpr * memberExpr = dynamic_cast< const ast::MemberExpr * >( func ) ) { |
---|
928 | return memberExpr->member->name; |
---|
929 | } else if ( const ast::UntypedMemberExpr * memberExpr = dynamic_cast< const ast::UntypedMemberExpr * > ( func ) ) { |
---|
930 | return funcName( memberExpr->member ); |
---|
931 | } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * >( func ) ) { |
---|
932 | return handleDerefName( untypedExpr ); |
---|
933 | } else if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( func ) ) { |
---|
934 | return handleDerefName( appExpr ); |
---|
935 | } else if ( const ast::ConstructorExpr * ctorExpr = dynamic_cast< const ast::ConstructorExpr * >( func ) ) { |
---|
936 | return funcName( getCallArg( ctorExpr->callExpr, 0 ) ); |
---|
937 | } else { |
---|
938 | assertf( false, "Unexpected expression type being called as a function in call expression: %s", toString( func ).c_str() ); |
---|
939 | } |
---|
940 | } |
---|
941 | } |
---|
942 | |
---|
943 | std::string getFunctionName( Expression * expr ) { |
---|
944 | // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and |
---|
945 | // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction |
---|
946 | // can't possibly do anything reasonable. |
---|
947 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) { |
---|
948 | return funcName( appExpr->get_function() ); |
---|
949 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) { |
---|
950 | return funcName( untypedExpr->get_function() ); |
---|
951 | } else { |
---|
952 | std::cerr << expr << std::endl; |
---|
953 | assertf( false, "Unexpected expression type passed to getFunctionName" ); |
---|
954 | } |
---|
955 | } |
---|
956 | |
---|
957 | std::string getFunctionName( const ast::Expr * expr ) { |
---|
958 | // there's some unforunate overlap here with getCalledFunction. Ideally this would be able to use getCalledFunction and |
---|
959 | // return the name of the DeclarationWithType, but this needs to work for NameExpr and UntypedMemberExpr, where getCalledFunction |
---|
960 | // can't possibly do anything reasonable. |
---|
961 | if ( const ast::ApplicationExpr * appExpr = dynamic_cast< const ast::ApplicationExpr * >( expr ) ) { |
---|
962 | return funcName( appExpr->func ); |
---|
963 | } else if ( const ast::UntypedExpr * untypedExpr = dynamic_cast< const ast::UntypedExpr * > ( expr ) ) { |
---|
964 | return funcName( untypedExpr->func ); |
---|
965 | } else { |
---|
966 | std::cerr << expr << std::endl; |
---|
967 | assertf( false, "Unexpected expression type passed to getFunctionName" ); |
---|
968 | } |
---|
969 | } |
---|
970 | |
---|
971 | Type * getPointerBase( Type * type ) { |
---|
972 | if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) { |
---|
973 | return ptrType->get_base(); |
---|
974 | } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) { |
---|
975 | return arrayType->get_base(); |
---|
976 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( type ) ) { |
---|
977 | return refType->get_base(); |
---|
978 | } else { |
---|
979 | return nullptr; |
---|
980 | } |
---|
981 | } |
---|
982 | const ast::Type* getPointerBase( const ast::Type* t ) { |
---|
983 | if ( const auto * p = dynamic_cast< const ast::PointerType * >( t ) ) { |
---|
984 | return p->base; |
---|
985 | } else if ( const auto * a = dynamic_cast< const ast::ArrayType * >( t ) ) { |
---|
986 | return a->base; |
---|
987 | } else if ( const auto * r = dynamic_cast< const ast::ReferenceType * >( t ) ) { |
---|
988 | return r->base; |
---|
989 | } else return nullptr; |
---|
990 | } |
---|
991 | |
---|
992 | Type * isPointerType( Type * type ) { |
---|
993 | if ( getPointerBase( type ) ) return type; |
---|
994 | else return nullptr; |
---|
995 | } |
---|
996 | |
---|
997 | ApplicationExpr * createBitwiseAssignment( Expression * dst, Expression * src ) { |
---|
998 | static FunctionDecl * assign = nullptr; |
---|
999 | if ( ! assign ) { |
---|
1000 | // temporary? Generate a fake assignment operator to represent bitwise assignments. |
---|
1001 | // This operator could easily exist as a real function, but it's tricky because nothing should resolve to this function. |
---|
1002 | TypeDecl * td = new TypeDecl( "T", noStorageClasses, nullptr, TypeDecl::Dtype, true ); |
---|
1003 | assign = new FunctionDecl( "?=?", noStorageClasses, LinkageSpec::Intrinsic, SymTab::genAssignType( new TypeInstType( noQualifiers, td->name, td ) ), nullptr ); |
---|
1004 | } |
---|
1005 | if ( dynamic_cast< ReferenceType * >( dst->result ) ) { |
---|
1006 | for (int depth = dst->result->referenceDepth(); depth > 0; depth--) { |
---|
1007 | dst = new AddressExpr( dst ); |
---|
1008 | } |
---|
1009 | } else { |
---|
1010 | dst = new CastExpr( dst, new ReferenceType( noQualifiers, dst->result->clone() ) ); |
---|
1011 | } |
---|
1012 | if ( dynamic_cast< ReferenceType * >( src->result ) ) { |
---|
1013 | for (int depth = src->result->referenceDepth(); depth > 0; depth--) { |
---|
1014 | src = new AddressExpr( src ); |
---|
1015 | } |
---|
1016 | // src = new CastExpr( src, new ReferenceType( noQualifiers, src->result->stripReferences()->clone() ) ); |
---|
1017 | } |
---|
1018 | return new ApplicationExpr( VariableExpr::functionPointer( assign ), { dst, src } ); |
---|
1019 | } |
---|
1020 | |
---|
1021 | // looks like some other such codegen uses UntypedExpr and does not create fake function. should revisit afterwards |
---|
1022 | // following passes may accidentally resolve this expression if returned as untyped... |
---|
1023 | ast::Expr * createBitwiseAssignment (const ast::Expr * dst, const ast::Expr * src) { |
---|
1024 | static ast::ptr<ast::FunctionDecl> assign = nullptr; |
---|
1025 | if (!assign) { |
---|
1026 | auto td = new ast::TypeDecl({}, "T", {}, nullptr, ast::TypeDecl::Dtype, true); |
---|
1027 | assign = new ast::FunctionDecl({}, "?=?", {}, |
---|
1028 | { new ast::ObjectDecl({}, "_dst", new ast::ReferenceType(new ast::TypeInstType("T", td))), |
---|
1029 | new ast::ObjectDecl({}, "_src", new ast::TypeInstType("T", td))}, |
---|
1030 | { new ast::ObjectDecl({}, "_ret", new ast::TypeInstType("T", td))}, nullptr, {}, ast::Linkage::Intrinsic); |
---|
1031 | } |
---|
1032 | if (dst->result.as<ast::ReferenceType>()) { |
---|
1033 | for (int depth = dst->result->referenceDepth(); depth > 0; depth--) { |
---|
1034 | dst = new ast::AddressExpr(dst); |
---|
1035 | } |
---|
1036 | } |
---|
1037 | else { |
---|
1038 | dst = new ast::CastExpr(dst, new ast::ReferenceType(dst->result, {})); |
---|
1039 | } |
---|
1040 | if (src->result.as<ast::ReferenceType>()) { |
---|
1041 | for (int depth = src->result->referenceDepth(); depth > 0; depth--) { |
---|
1042 | src = new ast::AddressExpr(src); |
---|
1043 | } |
---|
1044 | } |
---|
1045 | return new ast::ApplicationExpr(dst->location, ast::VariableExpr::functionPointer(dst->location, assign), {dst, src}); |
---|
1046 | } |
---|
1047 | |
---|
1048 | struct ConstExprChecker : public WithShortCircuiting { |
---|
1049 | // most expressions are not const expr |
---|
1050 | void previsit( Expression * ) { isConstExpr = false; visit_children = false; } |
---|
1051 | |
---|
1052 | void previsit( AddressExpr *addressExpr ) { |
---|
1053 | visit_children = false; |
---|
1054 | |
---|
1055 | // address of a variable or member expression is constexpr |
---|
1056 | Expression * arg = addressExpr->get_arg(); |
---|
1057 | if ( ! dynamic_cast< NameExpr * >( arg) && ! dynamic_cast< VariableExpr * >( arg ) && ! dynamic_cast< MemberExpr * >( arg ) && ! dynamic_cast< UntypedMemberExpr * >( arg ) ) isConstExpr = false; |
---|
1058 | } |
---|
1059 | |
---|
1060 | // these expressions may be const expr, depending on their children |
---|
1061 | void previsit( SizeofExpr * ) {} |
---|
1062 | void previsit( AlignofExpr * ) {} |
---|
1063 | void previsit( UntypedOffsetofExpr * ) {} |
---|
1064 | void previsit( OffsetofExpr * ) {} |
---|
1065 | void previsit( OffsetPackExpr * ) {} |
---|
1066 | void previsit( CommaExpr * ) {} |
---|
1067 | void previsit( LogicalExpr * ) {} |
---|
1068 | void previsit( ConditionalExpr * ) {} |
---|
1069 | void previsit( CastExpr * ) {} |
---|
1070 | void previsit( ConstantExpr * ) {} |
---|
1071 | |
---|
1072 | void previsit( VariableExpr * varExpr ) { |
---|
1073 | visit_children = false; |
---|
1074 | |
---|
1075 | if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( varExpr->result ) ) { |
---|
1076 | long long int value; |
---|
1077 | if ( inst->baseEnum->valueOf( varExpr->var, value ) ) { |
---|
1078 | // enumerators are const expr |
---|
1079 | return; |
---|
1080 | } |
---|
1081 | } |
---|
1082 | isConstExpr = false; |
---|
1083 | } |
---|
1084 | |
---|
1085 | bool isConstExpr = true; |
---|
1086 | }; |
---|
1087 | |
---|
1088 | struct ConstExprChecker_new : public ast::WithShortCircuiting { |
---|
1089 | // most expressions are not const expr |
---|
1090 | void previsit( const ast::Expr * ) { result = false; visit_children = false; } |
---|
1091 | |
---|
1092 | void previsit( const ast::AddressExpr *addressExpr ) { |
---|
1093 | visit_children = false; |
---|
1094 | const ast::Expr * arg = addressExpr->arg; |
---|
1095 | |
---|
1096 | // address of a variable or member expression is constexpr |
---|
1097 | if ( ! dynamic_cast< const ast::NameExpr * >( arg ) |
---|
1098 | && ! dynamic_cast< const ast::VariableExpr * >( arg ) |
---|
1099 | && ! dynamic_cast< const ast::MemberExpr * >( arg ) |
---|
1100 | && ! dynamic_cast< const ast::UntypedMemberExpr * >( arg ) ) result = false; |
---|
1101 | } |
---|
1102 | |
---|
1103 | // these expressions may be const expr, depending on their children |
---|
1104 | void previsit( const ast::SizeofExpr * ) {} |
---|
1105 | void previsit( const ast::AlignofExpr * ) {} |
---|
1106 | void previsit( const ast::UntypedOffsetofExpr * ) {} |
---|
1107 | void previsit( const ast::OffsetofExpr * ) {} |
---|
1108 | void previsit( const ast::OffsetPackExpr * ) {} |
---|
1109 | void previsit( const ast::CommaExpr * ) {} |
---|
1110 | void previsit( const ast::LogicalExpr * ) {} |
---|
1111 | void previsit( const ast::ConditionalExpr * ) {} |
---|
1112 | void previsit( const ast::CastExpr * ) {} |
---|
1113 | void previsit( const ast::ConstantExpr * ) {} |
---|
1114 | |
---|
1115 | void previsit( const ast::VariableExpr * varExpr ) { |
---|
1116 | visit_children = false; |
---|
1117 | |
---|
1118 | if ( auto inst = varExpr->result.as<ast::EnumInstType>() ) { |
---|
1119 | long long int value; |
---|
1120 | if ( inst->base->valueOf( varExpr->var, value ) ) { |
---|
1121 | // enumerators are const expr |
---|
1122 | return; |
---|
1123 | } |
---|
1124 | } |
---|
1125 | result = false; |
---|
1126 | } |
---|
1127 | |
---|
1128 | bool result = true; |
---|
1129 | }; |
---|
1130 | |
---|
1131 | bool isConstExpr( Expression * expr ) { |
---|
1132 | if ( expr ) { |
---|
1133 | PassVisitor<ConstExprChecker> checker; |
---|
1134 | expr->accept( checker ); |
---|
1135 | return checker.pass.isConstExpr; |
---|
1136 | } |
---|
1137 | return true; |
---|
1138 | } |
---|
1139 | |
---|
1140 | bool isConstExpr( Initializer * init ) { |
---|
1141 | if ( init ) { |
---|
1142 | PassVisitor<ConstExprChecker> checker; |
---|
1143 | init->accept( checker ); |
---|
1144 | return checker.pass.isConstExpr; |
---|
1145 | } // if |
---|
1146 | // for all intents and purposes, no initializer means const expr |
---|
1147 | return true; |
---|
1148 | } |
---|
1149 | |
---|
1150 | bool isConstExpr( const ast::Expr * expr ) { |
---|
1151 | if ( expr ) { |
---|
1152 | ast::Pass<ConstExprChecker_new> checker; |
---|
1153 | expr->accept( checker ); |
---|
1154 | return checker.core.result; |
---|
1155 | } |
---|
1156 | return true; |
---|
1157 | } |
---|
1158 | |
---|
1159 | bool isConstExpr( const ast::Init * init ) { |
---|
1160 | if ( init ) { |
---|
1161 | ast::Pass<ConstExprChecker_new> checker; |
---|
1162 | init->accept( checker ); |
---|
1163 | return checker.core.result; |
---|
1164 | } // if |
---|
1165 | // for all intents and purposes, no initializer means const expr |
---|
1166 | return true; |
---|
1167 | } |
---|
1168 | |
---|
1169 | bool isConstructor( const std::string & str ) { return str == "?{}"; } |
---|
1170 | bool isDestructor( const std::string & str ) { return str == "^?{}"; } |
---|
1171 | bool isAssignment( const std::string & str ) { return str == "?=?"; } |
---|
1172 | bool isCtorDtor( const std::string & str ) { return isConstructor( str ) || isDestructor( str ); } |
---|
1173 | bool isCtorDtorAssign( const std::string & str ) { return isCtorDtor( str ) || isAssignment( str ); } |
---|
1174 | |
---|
1175 | const FunctionDecl * isCopyFunction( const Declaration * decl, const std::string & fname ) { |
---|
1176 | const FunctionDecl * function = dynamic_cast< const FunctionDecl * >( decl ); |
---|
1177 | if ( ! function ) return nullptr; |
---|
1178 | if ( function->name != fname ) return nullptr; |
---|
1179 | FunctionType * ftype = function->type; |
---|
1180 | if ( ftype->parameters.size() != 2 ) return nullptr; |
---|
1181 | |
---|
1182 | Type * t1 = getPointerBase( ftype->get_parameters().front()->get_type() ); |
---|
1183 | Type * t2 = ftype->parameters.back()->get_type(); |
---|
1184 | assert( t1 ); |
---|
1185 | |
---|
1186 | if ( ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, SymTab::Indexer() ) ) { |
---|
1187 | return function; |
---|
1188 | } else { |
---|
1189 | return nullptr; |
---|
1190 | } |
---|
1191 | } |
---|
1192 | |
---|
1193 | bool isAssignment( const ast::FunctionDecl * decl ) { |
---|
1194 | return isAssignment( decl->name ) && isCopyFunction( decl ); |
---|
1195 | } |
---|
1196 | |
---|
1197 | bool isDestructor( const ast::FunctionDecl * decl ) { |
---|
1198 | return isDestructor( decl->name ); |
---|
1199 | } |
---|
1200 | |
---|
1201 | bool isDefaultConstructor( const ast::FunctionDecl * decl ) { |
---|
1202 | return isConstructor( decl->name ) && 1 == decl->params.size(); |
---|
1203 | } |
---|
1204 | |
---|
1205 | bool isCopyConstructor( const ast::FunctionDecl * decl ) { |
---|
1206 | return isConstructor( decl->name ) && 2 == decl->params.size(); |
---|
1207 | } |
---|
1208 | |
---|
1209 | bool isCopyFunction( const ast::FunctionDecl * decl ) { |
---|
1210 | const ast::FunctionType * ftype = decl->type; |
---|
1211 | if ( ftype->params.size() != 2 ) return false; |
---|
1212 | |
---|
1213 | const ast::Type * t1 = getPointerBase( ftype->params.front() ); |
---|
1214 | if ( ! t1 ) return false; |
---|
1215 | const ast::Type * t2 = ftype->params.back(); |
---|
1216 | |
---|
1217 | return ResolvExpr::typesCompatibleIgnoreQualifiers( t1, t2, ast::SymbolTable{} ); |
---|
1218 | } |
---|
1219 | |
---|
1220 | const FunctionDecl * isAssignment( const Declaration * decl ) { |
---|
1221 | return isCopyFunction( decl, "?=?" ); |
---|
1222 | } |
---|
1223 | const FunctionDecl * isDestructor( const Declaration * decl ) { |
---|
1224 | if ( isDestructor( decl->name ) ) { |
---|
1225 | return dynamic_cast< const FunctionDecl * >( decl ); |
---|
1226 | } |
---|
1227 | return nullptr; |
---|
1228 | } |
---|
1229 | const FunctionDecl * isDefaultConstructor( const Declaration * decl ) { |
---|
1230 | if ( isConstructor( decl->name ) ) { |
---|
1231 | if ( const FunctionDecl * func = dynamic_cast< const FunctionDecl * >( decl ) ) { |
---|
1232 | if ( func->type->parameters.size() == 1 ) { |
---|
1233 | return func; |
---|
1234 | } |
---|
1235 | } |
---|
1236 | } |
---|
1237 | return nullptr; |
---|
1238 | } |
---|
1239 | const FunctionDecl * isCopyConstructor( const Declaration * decl ) { |
---|
1240 | return isCopyFunction( decl, "?{}" ); |
---|
1241 | } |
---|
1242 | |
---|
1243 | void addDataSectonAttribute( ObjectDecl * objDecl ) { |
---|
1244 | objDecl->attributes.push_back(new Attribute("section", { |
---|
1245 | new ConstantExpr( Constant::from_string(".data" |
---|
1246 | #if defined( __x86_64 ) || defined( __i386 ) // assembler comment to prevent assembler warning message |
---|
1247 | "#" |
---|
1248 | #else // defined( __ARM_ARCH ) |
---|
1249 | "//" |
---|
1250 | #endif |
---|
1251 | ))})); |
---|
1252 | } |
---|
1253 | |
---|
1254 | void addDataSectionAttribute( ast::ObjectDecl * objDecl ) { |
---|
1255 | objDecl->attributes.push_back(new ast::Attribute("section", { |
---|
1256 | ast::ConstantExpr::from_string(objDecl->location, ".data" |
---|
1257 | #if defined( __x86_64 ) || defined( __i386 ) // assembler comment to prevent assembler warning message |
---|
1258 | "#" |
---|
1259 | #else // defined( __ARM_ARCH ) |
---|
1260 | "//" |
---|
1261 | #endif |
---|
1262 | )})); |
---|
1263 | } |
---|
1264 | |
---|
1265 | } |
---|