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 | // Lvalue.cpp -- Clean up lvalues and remove references. |
---|
8 | // |
---|
9 | // Author : Andrew Beach |
---|
10 | // Created On : Thu Sep 15 14:08:00 2022 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Mon Aug 12 18:07:00 2024 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #include "Lvalue.hpp" |
---|
17 | |
---|
18 | #include <set> |
---|
19 | #include <iostream> |
---|
20 | |
---|
21 | #include "AST/Copy.hpp" // for deepCopy |
---|
22 | #include "AST/Expr.hpp" |
---|
23 | #include "AST/Inspect.hpp" |
---|
24 | #include "AST/LinkageSpec.hpp" // for Linkage |
---|
25 | #include "AST/Pass.hpp" |
---|
26 | #include "Common/SemanticError.hpp" // for SemanticWarning |
---|
27 | #include "Common/ToString.hpp" // for toCString |
---|
28 | #include "Common/UniqueName.hpp" // for UniqueName |
---|
29 | #include "GenPoly/GenPoly.hpp" // for genFunctionType |
---|
30 | #include "ResolvExpr/Typeops.hpp" // for typesCompatible |
---|
31 | #include "ResolvExpr/Unify.hpp" // for unify |
---|
32 | |
---|
33 | #if 0 |
---|
34 | #define PRINT(x) x |
---|
35 | #else |
---|
36 | #define PRINT(x) |
---|
37 | #endif |
---|
38 | |
---|
39 | namespace GenPoly { |
---|
40 | |
---|
41 | namespace { |
---|
42 | |
---|
43 | /// Intrinsic functions that return references now instead return lvalues. |
---|
44 | struct FixIntrinsicResults final : public ast::WithGuards { |
---|
45 | enum { |
---|
46 | NoSkip, |
---|
47 | Skip, |
---|
48 | SkipInProgress, |
---|
49 | } skip = NoSkip; |
---|
50 | |
---|
51 | void previsit( ast::AsmExpr const * ) { |
---|
52 | GuardValue( skip ) = Skip; |
---|
53 | } |
---|
54 | void previsit( ast::ApplicationExpr const * ) { |
---|
55 | GuardValue( skip ) = (skip == Skip) ? SkipInProgress : NoSkip; |
---|
56 | } |
---|
57 | |
---|
58 | ast::Expr const * postvisit( ast::ApplicationExpr const * expr ); |
---|
59 | void previsit( ast::FunctionDecl const * decl ); |
---|
60 | bool inIntrinsic = false; |
---|
61 | }; |
---|
62 | |
---|
63 | /// Add de-references around address-of operations on reference types. |
---|
64 | struct AddressRef final : |
---|
65 | public ast::WithConstTranslationUnit, |
---|
66 | public ast::WithGuards, |
---|
67 | public ast::WithShortCircuiting, |
---|
68 | public ast::WithVisitorRef<AddressRef> { |
---|
69 | void previsit( ast::AddressExpr const * expr ); |
---|
70 | ast::Expr const * postvisit( ast::AddressExpr const * expr ); |
---|
71 | void previsit( ast::Expr const * expr ); |
---|
72 | ast::ApplicationExpr const * previsit( ast::ApplicationExpr const * expr ); |
---|
73 | void previsit( ast::SingleInit const * init ); |
---|
74 | |
---|
75 | void handleNonAddr( ast::Expr const * expr ); |
---|
76 | |
---|
77 | bool first = true; |
---|
78 | bool current = false; |
---|
79 | bool addCast = false; |
---|
80 | int refDepth = 0; |
---|
81 | }; |
---|
82 | |
---|
83 | /// Handles casts between references and pointers, |
---|
84 | /// creating temporaries for the conversion. |
---|
85 | struct ReferenceConversions final : |
---|
86 | public ast::WithConstTranslationUnit, |
---|
87 | public ast::WithGuards, public ast::WithStmtsToAdd { |
---|
88 | ast::Expr const * postvisit( ast::CastExpr const * expr ); |
---|
89 | ast::Expr const * postvisit( ast::AddressExpr const * expr ); |
---|
90 | }; |
---|
91 | |
---|
92 | /// Intrinsic functions that take reference parameters don't actually do. |
---|
93 | /// Their reference arguments must be implicity dereferenced. |
---|
94 | /// TODO Also appears to contain redundent code with AddressRef |
---|
95 | struct FixIntrinsicArgs final : |
---|
96 | public ast::WithConstTranslationUnit { |
---|
97 | ast::Expr const * postvisit( ast::ApplicationExpr const * expr ); |
---|
98 | }; |
---|
99 | |
---|
100 | /// Removes redundant &* / *& patterns that may be generated. |
---|
101 | struct CollapseAddressDeref final { |
---|
102 | ast::Expr const * postvisit( ast::AddressExpr const * expr ); |
---|
103 | ast::Expr const * postvisit( ast::ApplicationExpr const * expr ); |
---|
104 | }; |
---|
105 | |
---|
106 | /// GCC-like Generalized Lvalues (which have since been removed from GCC). |
---|
107 | /// https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Lvalues.html#Lvalues |
---|
108 | /// Replaces &(a,b) with (a, &b), &(a ? b : c) with (a ? &b : &c) |
---|
109 | struct GeneralizedLvalue final : |
---|
110 | public ast::WithVisitorRef<GeneralizedLvalue> { |
---|
111 | ast::Expr const * postvisit( ast::AddressExpr const * expr ); |
---|
112 | ast::Expr const * postvisit( ast::MemberExpr const * expr ); |
---|
113 | |
---|
114 | template<typename Node, typename Func> |
---|
115 | ast::Expr const * applyTransformation( |
---|
116 | Node const * expr, ast::ptr<ast::Expr> Node::*field, Func mkExpr ); |
---|
117 | }; |
---|
118 | |
---|
119 | /// Replace all reference types with pointer types. |
---|
120 | struct ReferenceTypeElimination final { |
---|
121 | ast::SizeofExpr const * previsit( ast::SizeofExpr const * expr ); |
---|
122 | ast::AlignofExpr const * previsit( ast::AlignofExpr const * expr ); |
---|
123 | ast::Type const * postvisit( ast::ReferenceType const * type ); |
---|
124 | }; |
---|
125 | |
---|
126 | /// True for intrinsic function calls that return an lvalue in C. |
---|
127 | bool isIntrinsicReference( ast::Expr const * expr ) { |
---|
128 | // The known intrinsic-reference prelude functions. |
---|
129 | static std::set<std::string> const lvalueFunctions = { "*?", "?[?]" }; |
---|
130 | if ( auto untyped = dynamic_cast<ast::UntypedExpr const *>( expr ) ) { |
---|
131 | std::string fname = ast::getFunctionName( untyped ); |
---|
132 | return lvalueFunctions.count( fname ); |
---|
133 | } else if ( auto app = dynamic_cast<ast::ApplicationExpr const *>( expr ) ) { |
---|
134 | if ( auto func = ast::getFunction( app ) ) { |
---|
135 | return func->linkage == ast::Linkage::Intrinsic |
---|
136 | && lvalueFunctions.count( func->name ); |
---|
137 | } |
---|
138 | } |
---|
139 | return false; |
---|
140 | } |
---|
141 | |
---|
142 | // A maybe typed variant of the createDeref function (only UntypedExpr). |
---|
143 | ast::Expr * mkDeref( |
---|
144 | ast::TranslationGlobal const & global, ast::Expr const * arg ) { |
---|
145 | if ( global.dereference ) { |
---|
146 | // Note: Reference depth can be arbitrarily deep here, |
---|
147 | // so peel off the outermost pointer/reference, not just |
---|
148 | // pointer because they are effecitvely equivalent in this pass |
---|
149 | ast::VariableExpr * deref = new ast::VariableExpr( |
---|
150 | arg->location, global.dereference ); |
---|
151 | deref->result = new ast::PointerType( deref->result ); |
---|
152 | ast::Type const * base = ast::getPointerBase( arg->result ); |
---|
153 | assertf( base, "expected pointer type in dereference (type was %s)", toString( arg->result ).c_str() ); |
---|
154 | ast::ApplicationExpr * ret = |
---|
155 | new ast::ApplicationExpr( arg->location, deref, { arg } ); |
---|
156 | ret->result = ast::deepCopy( base ); |
---|
157 | return ret; |
---|
158 | } else { |
---|
159 | return ast::UntypedExpr::createDeref( arg->location, arg ); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | ast::Expr const * FixIntrinsicResults::postvisit( |
---|
164 | ast::ApplicationExpr const * expr ) { |
---|
165 | |
---|
166 | if ( skip == SkipInProgress || !isIntrinsicReference( expr ) ) { |
---|
167 | return expr; |
---|
168 | } |
---|
169 | // Eliminate reference types from intrinsic applications |
---|
170 | // now they return lvalues. |
---|
171 | ast::ptr<ast::ReferenceType> result = |
---|
172 | expr->result.strict_as<ast::ReferenceType>(); |
---|
173 | expr = ast::mutate_field( expr, &ast::ApplicationExpr::result, |
---|
174 | ast::deepCopy( result->base ) ); |
---|
175 | if ( inIntrinsic ) { |
---|
176 | return expr; |
---|
177 | } |
---|
178 | // When not in an intrinsic function, add a cast to don't add cast when |
---|
179 | // in an intrinsic function, since they already have the cast. |
---|
180 | auto * ret = new ast::CastExpr( expr->location, expr, result.get() ); |
---|
181 | ret->env = expr->env; |
---|
182 | return ret; |
---|
183 | } |
---|
184 | |
---|
185 | void FixIntrinsicResults::previsit( ast::FunctionDecl const * decl ) { |
---|
186 | GuardValue( inIntrinsic ) = decl->linkage == ast::Linkage::Intrinsic; |
---|
187 | } |
---|
188 | |
---|
189 | void AddressRef::previsit( ast::AddressExpr const * ) { |
---|
190 | // Is this the first address-of in the chain? |
---|
191 | GuardValue( current ) = first; |
---|
192 | // Later references will not be for next address-of to be first in chain. |
---|
193 | GuardValue( first ) = false; |
---|
194 | // If is the outermost address-of in a chain: |
---|
195 | if ( current ) { |
---|
196 | // Set depth to 0 so that postvisit can |
---|
197 | // find the innermost address-of easily. |
---|
198 | GuardValue( refDepth ) = 0; |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | ast::Expr const * AddressRef::postvisit( ast::AddressExpr const * expr ) { |
---|
203 | PRINT( std::cerr << "addr ref at " << expr << std::endl; ) |
---|
204 | if ( 0 == refDepth ) { |
---|
205 | PRINT( std::cerr << "depth 0, get new depth..." << std::endl; ) |
---|
206 | // Is this the innermost address-of in a chain? record depth D. |
---|
207 | if ( isIntrinsicReference( expr->arg ) ) { |
---|
208 | assertf( false, "AddrRef : address-of should not have intrinsic reference argument: %s", toCString( expr->arg ) ); |
---|
209 | } else { |
---|
210 | // try to avoid ?[?] |
---|
211 | // TODO is this condition still necessary? intrinsicReferences |
---|
212 | // should have a cast around them at this point, so I don't think |
---|
213 | // this condition ever fires. |
---|
214 | refDepth = expr->arg->result->referenceDepth(); |
---|
215 | PRINT( std::cerr << "arg not intrinsic reference, new depth is: " << refDepth << std::endl; ) |
---|
216 | } |
---|
217 | } |
---|
218 | if ( current ) { |
---|
219 | PRINT( std::cerr << "current, depth is: " << refDepth << std::endl; ) |
---|
220 | ast::Expr const * ret = expr; |
---|
221 | while ( refDepth ) { |
---|
222 | // Add one dereference for each address-of in the chain. |
---|
223 | ret = mkDeref( transUnit().global, ret ); |
---|
224 | --refDepth; |
---|
225 | } |
---|
226 | |
---|
227 | // if addrExpr depth is 0, then the result is a pointer because the |
---|
228 | // arg was depth 1 and not lvalue. This means the dereference result |
---|
229 | // is not a reference, is lvalue, and one less pointer depth than the |
---|
230 | // addrExpr. Thus the cast is meaningless. |
---|
231 | // TODO: One thing to double check is whether it is possible for the |
---|
232 | // types to differ outside of the single pointer level (i.e. can the |
---|
233 | // base type of addrExpr differ from the type of addrExpr-arg?). If |
---|
234 | // so then the cast might need to be added, conditional on a more |
---|
235 | // sophisticated check. |
---|
236 | if ( addCast && 0 != expr->result->referenceDepth() ) { |
---|
237 | PRINT( std::cerr << "adding cast to " << expr->result << std::endl; ) |
---|
238 | return new ast::CastExpr( expr->location, |
---|
239 | ret, ast::deepCopy( expr->result ) ); |
---|
240 | } |
---|
241 | return ret; |
---|
242 | } |
---|
243 | PRINT( std::cerr << "not current..." << std::endl; ) |
---|
244 | return expr; |
---|
245 | } |
---|
246 | |
---|
247 | void AddressRef::previsit( ast::Expr const * expr ) { |
---|
248 | handleNonAddr( expr ); |
---|
249 | GuardValue( addCast ) = false; |
---|
250 | } |
---|
251 | |
---|
252 | // So we want to skip traversing to the head? |
---|
253 | ast::ApplicationExpr const * AddressRef::previsit( |
---|
254 | ast::ApplicationExpr const * expr ) { |
---|
255 | visit_children = false; |
---|
256 | GuardValue( addCast ); |
---|
257 | handleNonAddr( expr ); |
---|
258 | auto mutExpr = ast::mutate( expr ); |
---|
259 | for ( ast::ptr<ast::Expr> & arg : mutExpr->args ) { |
---|
260 | addCast = true; |
---|
261 | arg = arg->accept( *visitor ); |
---|
262 | } |
---|
263 | return mutExpr; |
---|
264 | } |
---|
265 | |
---|
266 | void AddressRef::previsit( ast::SingleInit const * ) { |
---|
267 | // Each initialization context with address-of requires a cast. |
---|
268 | GuardValue( addCast ) = true; |
---|
269 | } |
---|
270 | |
---|
271 | // idea: &&&E: get outer &, inner & |
---|
272 | // at inner &, record depth D of reference type of argument of &. |
---|
273 | // at auter &, add D derefs. |
---|
274 | void AddressRef::handleNonAddr( ast::Expr const * ) { |
---|
275 | // non-address-of: reset status variables: |
---|
276 | // * current expr is NOT the first address-of expr in an address-of chain. |
---|
277 | // * next seen address-of expr IS the first in the chain. |
---|
278 | GuardValue( current ) = false; |
---|
279 | GuardValue( first ) = true; |
---|
280 | } |
---|
281 | |
---|
282 | ast::Expr const * ReferenceConversions::postvisit( |
---|
283 | ast::CastExpr const * expr ) { |
---|
284 | // TODO: Is it possible to convert directly between reference types with |
---|
285 | // a different base. e.g. |
---|
286 | // int x; |
---|
287 | // (double&)x; |
---|
288 | // At the moment, I (who?) am working off of the assumption that this is |
---|
289 | // illegal, thus the cast becomes redundant after this pass, so trash the |
---|
290 | // cast altogether. If that changes, care must be taken to insert the |
---|
291 | // correct pointer casts in the right places. |
---|
292 | |
---|
293 | // Note: reference depth difference is the determining factor in what |
---|
294 | // code is run, rather than whether something is reference type or not, |
---|
295 | // since conversion still needs to occur when both types are references |
---|
296 | // that differ in depth. |
---|
297 | ast::Type const * dstType = expr->result.get(); |
---|
298 | ast::Type const * srcType = expr->arg->result.get(); |
---|
299 | assertf( dstType, "Cast to no type in: %s", toCString( expr ) ); |
---|
300 | assertf( srcType, "Cast from no type in: %s", toCString( expr ) ); |
---|
301 | int dstDepth = dstType->referenceDepth(); |
---|
302 | int srcDepth = srcType->referenceDepth(); |
---|
303 | int diff = dstDepth - srcDepth; |
---|
304 | |
---|
305 | if ( 0 < diff && !expr->arg->get_lvalue() ) { |
---|
306 | // rvalue to reference conversion -- introduce temporary |
---|
307 | // know that reference depth of cast argument is 0 |
---|
308 | // (int &&&)3; |
---|
309 | // becomes |
---|
310 | // int __ref_tmp_0 = 3; |
---|
311 | // int & __ref_tmp_1 = &__ref_tmp_0; |
---|
312 | // int && __ref_tmp_2 = &__ref_tmp_1; |
---|
313 | // &__ref_tmp_2; |
---|
314 | // The last & comes from the remaining reference conversion code. |
---|
315 | |
---|
316 | |
---|
317 | // allowing conversion in the rvalue to const ref case |
---|
318 | // use the referenced-to type to create temp variables |
---|
319 | ast::Type const * targetType = dstType; |
---|
320 | for (int i = 0; i < diff; ++i) targetType = (strict_dynamic_cast<ast::ReferenceType const *>(targetType))->base; |
---|
321 | |
---|
322 | if (!targetType->qualifiers.is_const) { |
---|
323 | SemanticWarning( expr->arg->location, |
---|
324 | Warning::RvalueToReferenceConversion, toCString( expr->arg ) ); |
---|
325 | } |
---|
326 | |
---|
327 | static UniqueName tmpNamer( "__ref_tmp_" ); |
---|
328 | ast::ObjectDecl * tmp = new ast::ObjectDecl( expr->arg->location, |
---|
329 | tmpNamer.newName(), |
---|
330 | // ast::deepCopy( expr->arg->result ), |
---|
331 | ast::deepCopy (targetType), |
---|
332 | new ast::SingleInit( expr->arg->location, expr->arg ) ); |
---|
333 | PRINT( std::cerr << "make tmp: " << tmp << std::endl; ) |
---|
334 | stmtsToAddBefore.push_back( new ast::DeclStmt( tmp->location, tmp ) ); |
---|
335 | for ( int i = 0 ; i < dstDepth - 1 ; ++i ) { |
---|
336 | ast::ObjectDecl * newTmp = new ast::ObjectDecl( tmp->location, |
---|
337 | tmpNamer.newName(), |
---|
338 | new ast::ReferenceType( ast::deepCopy( tmp->type ) ), |
---|
339 | new ast::SingleInit( tmp->location, |
---|
340 | new ast::AddressExpr( tmp->location, |
---|
341 | new ast::VariableExpr( tmp->location, tmp ) ) ) ); |
---|
342 | PRINT( std::cerr << "make tmp: " << i << ": " << newTmp << std::endl; ) |
---|
343 | stmtsToAddBefore.push_back( |
---|
344 | new ast::DeclStmt( newTmp->location, newTmp ) ); |
---|
345 | tmp = newTmp; |
---|
346 | } |
---|
347 | // Update diff so that remaining code works out correctly. |
---|
348 | expr = ast::mutate_field( expr, &ast::CastExpr::arg, |
---|
349 | new ast::VariableExpr( tmp->location, tmp ) ); |
---|
350 | PRINT( std::cerr << "update cast to: " << expr << std::endl; ) |
---|
351 | srcType = expr->arg->result; |
---|
352 | srcDepth = srcType->referenceDepth(); |
---|
353 | diff = dstDepth - srcDepth; |
---|
354 | assert( 1 == diff ); |
---|
355 | } |
---|
356 | |
---|
357 | // Handle conversion between different depths. |
---|
358 | PRINT( |
---|
359 | if ( dstDepth || srcDepth ) { |
---|
360 | std::cerr << "dstType: " << dstType << " / srcType: " << srcType << '\n'; |
---|
361 | std::cerr << "depth: " << dstDepth << " / " << srcDepth << std::endl; |
---|
362 | } |
---|
363 | ) |
---|
364 | // Conversion to type with more depth/more references. |
---|
365 | // Add address-of for each level of difference. |
---|
366 | if ( 0 < diff ) { |
---|
367 | ast::Expr * ret = ast::mutate( expr->arg.get() ); |
---|
368 | for ( int i = 0 ; i < diff ; ++i ) { |
---|
369 | ret = new ast::AddressExpr( ret->location, ret ); |
---|
370 | } |
---|
371 | // Must keep cast if types are different. |
---|
372 | if ( !ResolvExpr::typesCompatible( |
---|
373 | srcType, |
---|
374 | strict_dynamic_cast<ast::ReferenceType const *>( dstType )->base ) ) { |
---|
375 | return ast::mutate_field( expr, &ast::CastExpr::arg, ret ); |
---|
376 | } |
---|
377 | ret->env = expr->env; |
---|
378 | ret->result = expr->result; |
---|
379 | return ret; |
---|
380 | // Conversion to type with less depth/fewer references. |
---|
381 | // Add dereferences for each level of difference. |
---|
382 | } else if ( diff < 0 ) { |
---|
383 | ast::Expr * ret = ast::mutate( expr->arg.get() ); |
---|
384 | for ( int i = 0 ; i < -diff ; ++i ) { |
---|
385 | ret = mkDeref( transUnit().global, ret ); |
---|
386 | } |
---|
387 | // Must keep cast if types are different. |
---|
388 | if ( !ResolvExpr::typesCompatible( |
---|
389 | dstType->stripReferences(), |
---|
390 | srcType->stripReferences() ) ) { |
---|
391 | return ast::mutate_field( expr, &ast::CastExpr::arg, ret ); |
---|
392 | } |
---|
393 | ret->env = expr->env; |
---|
394 | ret->result = expr->result; |
---|
395 | // The result must be an lvalue. |
---|
396 | assert( ret->get_lvalue() ); |
---|
397 | return ret; |
---|
398 | // Conversion with the same depth. |
---|
399 | } else { |
---|
400 | assert( 0 == diff ); |
---|
401 | // Must keep cast if types are different. (Or it is explicit.) |
---|
402 | if ( ast::ExplicitCast == expr->isGenerated || |
---|
403 | !ResolvExpr::typesCompatible( |
---|
404 | expr->result, |
---|
405 | expr->arg->result ) ) { |
---|
406 | return expr; |
---|
407 | } |
---|
408 | PRINT( |
---|
409 | std::cerr << "types are compatible, removing cast: " << expr << '\n'; |
---|
410 | std::cerr << "-- " << expr->result << '\n'; |
---|
411 | std::cerr << "-- " << expr->arg->result << std::endl; |
---|
412 | ) |
---|
413 | return ast::mutate_field( expr->arg.get(), |
---|
414 | &ast::Expr::env, expr->env.get() ); |
---|
415 | } |
---|
416 | } |
---|
417 | |
---|
418 | ast::Expr const * ReferenceConversions::postvisit( |
---|
419 | ast::AddressExpr const * expr ) { |
---|
420 | // Inner expression may have been lvalue to reference conversion, which |
---|
421 | // becomes an address expression. In this case, remove the outer address |
---|
422 | // expression and return the argument. |
---|
423 | // TODO: It's possible that this might catch too much and require a more |
---|
424 | // sophisticated check. TODO What check are we talking about here? |
---|
425 | return expr; |
---|
426 | } |
---|
427 | |
---|
428 | ast::Expr const * FixIntrinsicArgs::postvisit( |
---|
429 | ast::ApplicationExpr const * expr ) { |
---|
430 | // Intrinsic functions don't really take reference-typed parameters, |
---|
431 | // so they require an implicit dereference on their arguments. |
---|
432 | auto function = ast::getFunction( expr ); |
---|
433 | if ( function == nullptr ) { |
---|
434 | return expr; |
---|
435 | } |
---|
436 | |
---|
437 | ast::FunctionType const * ftype = GenPoly::getFunctionType( function->get_type() ); |
---|
438 | assertf( ftype, "Function declaration does not have function type." ); |
---|
439 | // Can be of different lengths only when function is variadic. |
---|
440 | assertf( ftype->params.size() == expr->args.size() || ftype->isVarArgs, |
---|
441 | "ApplicationExpr args do not match formal parameter type." ); |
---|
442 | assertf( ftype->params.size() <= expr->args.size(), |
---|
443 | "Cannot have more parameters than arguments." ); |
---|
444 | |
---|
445 | unsigned int i = 0; |
---|
446 | unsigned int const end = ftype->params.size(); |
---|
447 | |
---|
448 | // This is used to make sure we get a zip on shortests. |
---|
449 | if ( end == i ) return expr; |
---|
450 | |
---|
451 | // This mutate could be redundent, but it is simpler this way. |
---|
452 | auto mutExpr = ast::mutate( expr ); |
---|
453 | |
---|
454 | for ( auto pair : unsafe_group_iterate( mutExpr->args, ftype->params ) ) { |
---|
455 | ast::ptr<ast::Expr> & arg = std::get<0>( pair ); |
---|
456 | ast::ptr<ast::Type> const & formal = std::get<1>( pair ); |
---|
457 | PRINT( |
---|
458 | std::cerr << "pair<0>: " << arg.get() << std::endl; |
---|
459 | std::cerr << " -- " << arg->result << std::endl; |
---|
460 | std::cerr << "pair<1>: " << formal << std::endl; |
---|
461 | ) |
---|
462 | //if ( dynamic_cast<ast::ReferenceType const *>( formal.get() ) ) { |
---|
463 | if ( formal.as<ast::ReferenceType>() ) { |
---|
464 | PRINT( std::cerr << "===formal is reference" << std::endl; ) |
---|
465 | // TODO: It's likely that the second condition should be |
---|
466 | // `... && ! isIntrinsicReference( arg )`, but this requires |
---|
467 | // investigation. |
---|
468 | |
---|
469 | if ( ast::Linkage::Intrinsic != function->linkage |
---|
470 | && isIntrinsicReference( arg ) ) { |
---|
471 | // Needed for definition of prelude functions, etc. |
---|
472 | // If argument is dereference or array subscript, the result |
---|
473 | // isn't REALLY a reference, but non-intrinsic functions |
---|
474 | // expect a reference: take address |
---|
475 | |
---|
476 | // TODO: OK, so this should be cut?! |
---|
477 | // NODE: Previously, this condition fixed |
---|
478 | // void f(int *&); |
---|
479 | // int & x = ...; |
---|
480 | // f(&x); |
---|
481 | // But now this is taken care of by a reference cast added by |
---|
482 | // AddressRef. Need to find a new example or remove this |
---|
483 | // branch. |
---|
484 | PRINT( |
---|
485 | std::cerr << "===is intrinsic arg in non-intrinsic call - adding address" << std::endl; |
---|
486 | ) |
---|
487 | arg = new ast::AddressExpr( arg->location, arg ); |
---|
488 | } else if ( ast::Linkage::Intrinsic == function->linkage |
---|
489 | && arg->result->referenceDepth() != 0 ) { |
---|
490 | // Argument is a 'real' reference, but function expects a C |
---|
491 | // lvalue: Add a dereference to the reference-typed argument. |
---|
492 | PRINT( |
---|
493 | std::cerr << "===is non-intrinsic arg in intrinsic call - adding deref to arg" << std::endl; |
---|
494 | ) |
---|
495 | ast::Type const * base = ast::getPointerBase( arg->result ); |
---|
496 | assertf( base, "parameter is reference, arg must be pointer or reference: %s", toString( arg->result ).c_str() ); |
---|
497 | ast::PointerType * ptr = new ast::PointerType( ast::deepCopy( base ) ); |
---|
498 | arg = ast::mutate_field( arg.get(), |
---|
499 | &ast::ApplicationExpr::result, ptr ); |
---|
500 | arg = mkDeref( transUnit().global, arg ); |
---|
501 | } |
---|
502 | } |
---|
503 | ++i; |
---|
504 | if ( end == i ) break; |
---|
505 | } |
---|
506 | return mutExpr; |
---|
507 | } |
---|
508 | |
---|
509 | /// Recursively move an address expression underneath casts. Casts are not |
---|
510 | /// lvalue expressions in C but are sometimes considered as such in Cforall, |
---|
511 | /// (passes like InstantiateGeneric can add them.) - &(int) => (int*)& |
---|
512 | ast::Expr const * moveAddressUnderCast( ast::AddressExpr const * expr ) { |
---|
513 | if ( !dynamic_cast<ast::CastExpr const *>( expr->arg.get() ) ) { |
---|
514 | return expr; |
---|
515 | } |
---|
516 | auto mutExpr = ast::mutate( expr ); |
---|
517 | auto mutCast = strict_dynamic_cast<ast::CastExpr *>( |
---|
518 | ast::mutate( mutExpr->arg.release() ) ); |
---|
519 | mutExpr->arg = mutCast->arg; |
---|
520 | mutCast->arg = moveAddressUnderCast( mutExpr ); |
---|
521 | mutCast->result = new ast::PointerType( mutCast->result ); |
---|
522 | return mutCast; |
---|
523 | } |
---|
524 | |
---|
525 | ast::Expr const * CollapseAddressDeref::postvisit( |
---|
526 | ast::AddressExpr const * expr ) { |
---|
527 | ast::Expr const * arg = expr->arg; |
---|
528 | if ( isIntrinsicReference( arg ) ) { |
---|
529 | std::string fname = ast::getFunctionName( arg ); |
---|
530 | if ( fname == "*?" ) { |
---|
531 | ast::Expr const * arg0 = ast::getCallArg( arg, 0 ); |
---|
532 | ast::Expr * ret = ast::mutate( arg0 ); |
---|
533 | ret->env = expr->env; |
---|
534 | return ret; |
---|
535 | } |
---|
536 | } else { |
---|
537 | return moveAddressUnderCast( expr ); |
---|
538 | } |
---|
539 | return expr; |
---|
540 | } |
---|
541 | |
---|
542 | ast::Expr const * CollapseAddressDeref::postvisit( |
---|
543 | ast::ApplicationExpr const * expr ) { |
---|
544 | if ( isIntrinsicReference( expr ) ) { |
---|
545 | std::string fname = ast::getFunctionName( expr ); |
---|
546 | if ( fname == "*?" ) { |
---|
547 | assert( 1 == expr->args.size() ); |
---|
548 | ast::Expr const * arg = ast::getCallArg( expr, 0 ); |
---|
549 | // xxx - this isn't right, because it can remove casts that |
---|
550 | // should be there... |
---|
551 | // while ( auto cast = dynamic_cast< ast::CastExpr const * >( arg ) ) { |
---|
552 | // arg = cast->arg; |
---|
553 | // } |
---|
554 | if ( auto addr = dynamic_cast<ast::AddressExpr const *>( arg ) ) { |
---|
555 | return ast::mutate_field( addr->arg.get(), |
---|
556 | &ast::Expr::env, expr->env.get() ); |
---|
557 | } |
---|
558 | } |
---|
559 | } |
---|
560 | return expr; |
---|
561 | } |
---|
562 | |
---|
563 | ast::Expr const * GeneralizedLvalue::postvisit( |
---|
564 | ast::AddressExpr const * expr ) { |
---|
565 | return applyTransformation( expr, &ast::AddressExpr::arg, |
---|
566 | []( ast::Expr const * arg ) { |
---|
567 | return new ast::AddressExpr( arg->location, arg ); |
---|
568 | } |
---|
569 | ); |
---|
570 | } |
---|
571 | |
---|
572 | ast::Expr const * GeneralizedLvalue::postvisit( |
---|
573 | ast::MemberExpr const * expr ) { |
---|
574 | return applyTransformation( expr, &ast::MemberExpr::aggregate, |
---|
575 | [expr]( ast::Expr const * aggr ) { |
---|
576 | return new ast::MemberExpr( aggr->location, expr->member, aggr ); |
---|
577 | } |
---|
578 | ); |
---|
579 | } |
---|
580 | |
---|
581 | template<typename Node, typename Func> |
---|
582 | ast::Expr const * GeneralizedLvalue::applyTransformation( |
---|
583 | Node const * expr, ast::ptr<ast::Expr> Node::*field, Func mkExpr ) { |
---|
584 | ast::ptr<ast::Expr> const & arg = expr->*field; |
---|
585 | if ( auto commaArg = arg.as<ast::CommaExpr>() ) { |
---|
586 | ast::Expr const * arg1 = ast::deepCopy( commaArg->arg1 ); |
---|
587 | ast::Expr const * arg2 = ast::deepCopy( commaArg->arg2 ); |
---|
588 | ast::Expr const * ret = new ast::CommaExpr( |
---|
589 | commaArg->location, arg1, mkExpr( arg2 )->accept( *visitor ) ); |
---|
590 | return ret; |
---|
591 | } else if ( auto condArg = arg.as<ast::ConditionalExpr>() ) { |
---|
592 | ast::Expr const * arg1 = ast::deepCopy( condArg->arg1 ); |
---|
593 | ast::Expr const * arg2 = ast::deepCopy( condArg->arg2 ); |
---|
594 | ast::Expr const * arg3 = ast::deepCopy( condArg->arg3 ); |
---|
595 | ast::ConditionalExpr * ret = new ast::ConditionalExpr( |
---|
596 | condArg->location, arg1, mkExpr( arg2 )->accept( *visitor ), |
---|
597 | mkExpr( arg3 )->accept( *visitor ) ); |
---|
598 | |
---|
599 | // Conditional expr type may not be either of the arguments, |
---|
600 | // so unify to get the result. |
---|
601 | // TODO: Maybe I could create a wrapper for this. |
---|
602 | ast::ptr<ast::Type> common = nullptr; |
---|
603 | ast::TypeEnvironment newEnv; |
---|
604 | ast::AssertionSet needAssertions, haveAssertions; |
---|
605 | ast::OpenVarSet openVars; |
---|
606 | ResolvExpr::unify( ret->arg2->result, ret->arg3->result, newEnv, |
---|
607 | needAssertions, haveAssertions, openVars, common ); |
---|
608 | ret->result = common ? common : ast::deepCopy( ret->arg2->result ); |
---|
609 | return ret; |
---|
610 | } |
---|
611 | return expr; |
---|
612 | } |
---|
613 | |
---|
614 | ast::SizeofExpr const * ReferenceTypeElimination::previsit( |
---|
615 | ast::SizeofExpr const * expr ) { |
---|
616 | return ast::mutate_field( expr, &ast::SizeofExpr::type, |
---|
617 | expr->type->stripReferences() ); |
---|
618 | } |
---|
619 | |
---|
620 | ast::AlignofExpr const * ReferenceTypeElimination::previsit( |
---|
621 | ast::AlignofExpr const * expr ) { |
---|
622 | return ast::mutate_field( expr, &ast::AlignofExpr::type, |
---|
623 | expr->type->stripReferences() ); |
---|
624 | } |
---|
625 | |
---|
626 | ast::Type const * ReferenceTypeElimination::postvisit( |
---|
627 | ast::ReferenceType const * type ) { |
---|
628 | return new ast::PointerType( type->base, type->qualifiers ); |
---|
629 | } |
---|
630 | |
---|
631 | } // namespace |
---|
632 | |
---|
633 | // Stored elsewhere (Lvalue2, initially false): |
---|
634 | extern bool referencesEliminated; |
---|
635 | |
---|
636 | void convertLvalue( ast::TranslationUnit & translationUnit ) { |
---|
637 | ast::Pass<FixIntrinsicResults>::run( translationUnit ); |
---|
638 | ast::Pass<AddressRef>::run( translationUnit ); |
---|
639 | ast::Pass<ReferenceConversions>::run( translationUnit ); |
---|
640 | ast::Pass<FixIntrinsicArgs>::run( translationUnit ); |
---|
641 | ast::Pass<CollapseAddressDeref>::run( translationUnit ); |
---|
642 | ast::Pass<GeneralizedLvalue>::run( translationUnit ); |
---|
643 | // Last because other passes need reference types to work. |
---|
644 | ast::Pass<ReferenceTypeElimination>::run( translationUnit ); |
---|
645 | // From this point forward, nothing should create reference types. |
---|
646 | referencesEliminated = true; |
---|
647 | } |
---|
648 | |
---|
649 | ast::Expr const * generalizedLvalue( ast::Expr const * expr ) { |
---|
650 | ast::Pass<GeneralizedLvalue> visitor; |
---|
651 | return expr->accept( visitor ); |
---|
652 | } |
---|
653 | |
---|
654 | } // namespace GenPoly |
---|
655 | |
---|
656 | // Local Variables: // |
---|
657 | // tab-width: 4 // |
---|
658 | // mode: c++ // |
---|
659 | // compile-command: "make install" // |
---|
660 | // End: // |
---|