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