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.cc --
|
---|
8 | //
|
---|
9 | // Author : Richard C. Bilson
|
---|
10 | // Created On : Mon May 18 07:44:20 2015
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Fri Mar 17 09:11:18 2017
|
---|
13 | // Update Count : 5
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <cassert> // for strict_dynamic_cast
|
---|
17 | #include <string> // for string
|
---|
18 |
|
---|
19 | #include "Common/PassVisitor.h"
|
---|
20 | #include "GenPoly.h" // for isPolyType
|
---|
21 | #include "Lvalue.h"
|
---|
22 |
|
---|
23 | #include "Parser/LinkageSpec.h" // for Spec, isBuiltin, Intrinsic
|
---|
24 | #include "ResolvExpr/TypeEnvironment.h" // for AssertionSet, OpenVarSet
|
---|
25 | #include "ResolvExpr/Unify.h" // for unify
|
---|
26 | #include "ResolvExpr/typeops.h"
|
---|
27 | #include "SymTab/Autogen.h"
|
---|
28 | #include "SymTab/Indexer.h" // for Indexer
|
---|
29 | #include "SynTree/Declaration.h" // for Declaration, FunctionDecl
|
---|
30 | #include "SynTree/Expression.h" // for Expression, ConditionalExpr
|
---|
31 | #include "SynTree/Mutator.h" // for mutateAll, Mutator
|
---|
32 | #include "SynTree/Statement.h" // for ReturnStmt, Statement (ptr o...
|
---|
33 | #include "SynTree/Type.h" // for PointerType, Type, FunctionType
|
---|
34 | #include "SynTree/Visitor.h" // for Visitor, acceptAll
|
---|
35 |
|
---|
36 | #if 0
|
---|
37 | #define PRINT(x) x
|
---|
38 | #else
|
---|
39 | #define PRINT(x)
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | namespace GenPoly {
|
---|
43 | namespace {
|
---|
44 | // TODO: fold this into the general createDeref function??
|
---|
45 | Expression * mkDeref( Expression * arg ) {
|
---|
46 | if ( SymTab::dereferenceOperator ) {
|
---|
47 | VariableExpr * deref = new VariableExpr( SymTab::dereferenceOperator );
|
---|
48 | deref->result = new PointerType( Type::Qualifiers(), deref->result );
|
---|
49 | Type * base = InitTweak::getPointerBase( arg->result );
|
---|
50 | assertf( base, "expected pointer type in dereference (type was %s)", toString( arg->result ).c_str() );
|
---|
51 | ApplicationExpr * ret = new ApplicationExpr( deref, { arg } );
|
---|
52 | delete ret->result;
|
---|
53 | ret->result = base->clone();
|
---|
54 | ret->result->set_lvalue( true );
|
---|
55 | return ret;
|
---|
56 | } else {
|
---|
57 | return UntypedExpr::createDeref( arg );
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | struct ReferenceConversions final : public WithStmtsToAdd {
|
---|
62 | Expression * postmutate( CastExpr * castExpr );
|
---|
63 | Expression * postmutate( AddressExpr * addrExpr );
|
---|
64 | };
|
---|
65 |
|
---|
66 | /// Intrinsic functions that take reference parameters don't REALLY take reference parameters -- their reference arguments must always be implicitly dereferenced.
|
---|
67 | struct FixIntrinsicArgs final {
|
---|
68 | Expression * postmutate( ApplicationExpr * appExpr );
|
---|
69 | };
|
---|
70 |
|
---|
71 | struct FixIntrinsicResult final : public WithGuards {
|
---|
72 | Expression * postmutate( ApplicationExpr * appExpr );
|
---|
73 | void premutate( FunctionDecl * funcDecl );
|
---|
74 | bool inIntrinsic = false;
|
---|
75 | };
|
---|
76 |
|
---|
77 | /// Replace reference types with pointer types
|
---|
78 | struct ReferenceTypeElimination final {
|
---|
79 | Type * postmutate( ReferenceType * refType );
|
---|
80 | };
|
---|
81 |
|
---|
82 | /// GCC-like Generalized Lvalues (which have since been removed from GCC)
|
---|
83 | /// https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Lvalues.html#Lvalues
|
---|
84 | /// Replaces &(a,b) with (a, &b), &(a ? b : c) with (a ? &b : &c)
|
---|
85 | struct GeneralizedLvalue final : public WithVisitorRef<GeneralizedLvalue> {
|
---|
86 | Expression * postmutate( AddressExpr * addressExpr );
|
---|
87 | Expression * postmutate( MemberExpr * memExpr );
|
---|
88 |
|
---|
89 | template<typename Func>
|
---|
90 | Expression * applyTransformation( Expression * expr, Expression * arg, Func mkExpr );
|
---|
91 | };
|
---|
92 |
|
---|
93 | /// Removes redundant &*/*& pattern that this pass can generate
|
---|
94 | struct CollapseAddrDeref final {
|
---|
95 | Expression * postmutate( AddressExpr * addressExpr );
|
---|
96 | Expression * postmutate( ApplicationExpr * appExpr );
|
---|
97 | };
|
---|
98 |
|
---|
99 | struct AddrRef final : public WithGuards {
|
---|
100 | void premutate( AddressExpr * addrExpr );
|
---|
101 | Expression * postmutate( AddressExpr * addrExpr );
|
---|
102 | void premutate( Expression * expr );
|
---|
103 |
|
---|
104 | bool first = true;
|
---|
105 | bool current = false;
|
---|
106 | int refDepth = 0;
|
---|
107 | };
|
---|
108 | } // namespace
|
---|
109 |
|
---|
110 | static bool referencesEliminated = false;
|
---|
111 | // used by UntypedExpr::createDeref to determine whether result type of dereference should be ReferenceType or value type.
|
---|
112 | bool referencesPermissable() {
|
---|
113 | return ! referencesEliminated;
|
---|
114 | }
|
---|
115 |
|
---|
116 | void convertLvalue( std::list< Declaration* > & translationUnit ) {
|
---|
117 | PassVisitor<ReferenceConversions> refCvt;
|
---|
118 | PassVisitor<ReferenceTypeElimination> elim;
|
---|
119 | PassVisitor<GeneralizedLvalue> genLval;
|
---|
120 | PassVisitor<FixIntrinsicArgs> fixer;
|
---|
121 | PassVisitor<CollapseAddrDeref> collapser;
|
---|
122 | PassVisitor<AddrRef> addrRef;
|
---|
123 | PassVisitor<FixIntrinsicResult> intrinsicResults;
|
---|
124 | mutateAll( translationUnit, intrinsicResults );
|
---|
125 | mutateAll( translationUnit, addrRef );
|
---|
126 | mutateAll( translationUnit, refCvt );
|
---|
127 | mutateAll( translationUnit, fixer );
|
---|
128 | mutateAll( translationUnit, collapser );
|
---|
129 | mutateAll( translationUnit, genLval );
|
---|
130 | mutateAll( translationUnit, elim ); // last because other passes need reference types to work
|
---|
131 |
|
---|
132 | // from this point forward, no other pass should create reference types.
|
---|
133 | referencesEliminated = true;
|
---|
134 | }
|
---|
135 |
|
---|
136 | Expression * generalizedLvalue( Expression * expr ) {
|
---|
137 | PassVisitor<GeneralizedLvalue> genLval;
|
---|
138 | return expr->acceptMutator( genLval );
|
---|
139 | }
|
---|
140 |
|
---|
141 | namespace {
|
---|
142 | // true for intrinsic function calls that return a reference
|
---|
143 | bool isIntrinsicReference( Expression * expr ) {
|
---|
144 | if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * >( expr ) ) {
|
---|
145 | std::string fname = InitTweak::getFunctionName( untyped );
|
---|
146 | // known intrinsic-reference prelude functions
|
---|
147 | return fname == "*?" || fname == "?[?]";
|
---|
148 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) {
|
---|
149 | if ( DeclarationWithType * func = InitTweak::getFunction( appExpr ) ) {
|
---|
150 | // use type of return variable rather than expr result type, since it may have been changed to a pointer type
|
---|
151 | FunctionType * ftype = GenPoly::getFunctionType( func->get_type() );
|
---|
152 | Type * ret = ftype->returnVals.empty() ? nullptr : ftype->returnVals.front()->get_type();
|
---|
153 | return func->linkage == LinkageSpec::Intrinsic && dynamic_cast<ReferenceType *>( ret );
|
---|
154 | }
|
---|
155 | }
|
---|
156 | return false;
|
---|
157 | }
|
---|
158 |
|
---|
159 | Expression * FixIntrinsicResult::postmutate( ApplicationExpr * appExpr ) {
|
---|
160 | if ( isIntrinsicReference( appExpr ) ) {
|
---|
161 | // eliminate reference types from intrinsic applications - now they return lvalues
|
---|
162 | Type * result = appExpr->result;
|
---|
163 | appExpr->result = result->stripReferences()->clone();
|
---|
164 | appExpr->result->set_lvalue( true );
|
---|
165 | if ( ! inIntrinsic ) {
|
---|
166 | // when not in an intrinsic function, add a cast to
|
---|
167 | // don't add cast when in an intrinsic function, since they already have the cast
|
---|
168 | Expression * ret = new CastExpr( appExpr, result );
|
---|
169 | std::swap( ret->env, appExpr->env );
|
---|
170 | return ret;
|
---|
171 | }
|
---|
172 | delete result;
|
---|
173 | }
|
---|
174 | return appExpr;
|
---|
175 | }
|
---|
176 |
|
---|
177 | void FixIntrinsicResult::premutate( FunctionDecl * funcDecl ) {
|
---|
178 | GuardValue( inIntrinsic );
|
---|
179 | inIntrinsic = funcDecl->linkage == LinkageSpec::Intrinsic;
|
---|
180 | }
|
---|
181 |
|
---|
182 | Expression * FixIntrinsicArgs::postmutate( ApplicationExpr * appExpr ) {
|
---|
183 | // intrinsic functions don't really take reference-typed parameters, so they require an implicit dereference on their arguments.
|
---|
184 | if ( DeclarationWithType * function = InitTweak::getFunction( appExpr ) ) {
|
---|
185 | FunctionType * ftype = GenPoly::getFunctionType( function->get_type() );
|
---|
186 | assertf( ftype, "Function declaration does not have function type." );
|
---|
187 | // can be of differing lengths only when function is variadic
|
---|
188 | assertf( ftype->parameters.size() == appExpr->args.size() || ftype->isVarArgs, "ApplicationExpr args do not match formal parameter type." );
|
---|
189 |
|
---|
190 |
|
---|
191 | unsigned int i = 0;
|
---|
192 | const unsigned int end = ftype->parameters.size();
|
---|
193 | for ( auto p : unsafe_group_iterate( appExpr->args, ftype->parameters ) ) {
|
---|
194 | if (i == end) break;
|
---|
195 | Expression *& arg = std::get<0>( p );
|
---|
196 | Type * formal = std::get<1>( p )->get_type();
|
---|
197 | PRINT(
|
---|
198 | std::cerr << "pair<0>: " << arg << std::endl;
|
---|
199 | std::cerr << "pair<1>: " << formal << std::endl;
|
---|
200 | )
|
---|
201 | if ( dynamic_cast<ReferenceType*>( formal ) ) {
|
---|
202 | if ( isIntrinsicReference( arg ) ) { // do not combine conditions, because that changes the meaning of the else if
|
---|
203 | if ( function->get_linkage() != LinkageSpec::Intrinsic ) { // intrinsic functions that turn pointers into references
|
---|
204 | // if argument is dereference or array subscript, the result isn't REALLY a reference, so it's not necessary to fix the argument
|
---|
205 | PRINT(
|
---|
206 | std::cerr << "===is intrinsic arg in non-intrinsic call - adding address" << std::endl;
|
---|
207 | )
|
---|
208 | arg = new AddressExpr( arg );
|
---|
209 | }
|
---|
210 | } else if ( function->get_linkage() == LinkageSpec::Intrinsic ) {
|
---|
211 | // std::cerr << "===adding deref to arg" << std::endl;
|
---|
212 | // if the parameter is a reference, add a dereference to the reference-typed argument.
|
---|
213 | Type * baseType = InitTweak::getPointerBase( arg->result );
|
---|
214 | assertf( baseType, "parameter is reference, arg must be pointer or reference: %s", toString( arg->result ).c_str() );
|
---|
215 | PointerType * ptrType = new PointerType( Type::Qualifiers(), baseType->clone() );
|
---|
216 | delete arg->result;
|
---|
217 | arg->set_result( ptrType );
|
---|
218 | arg = mkDeref( arg );
|
---|
219 | }
|
---|
220 | }
|
---|
221 | ++i;
|
---|
222 | }
|
---|
223 | }
|
---|
224 | return appExpr;
|
---|
225 | }
|
---|
226 |
|
---|
227 | // idea: &&&E: get outer &, inner &
|
---|
228 | // at inner &, record depth D of reference type
|
---|
229 | // at outer &, add D derefs.
|
---|
230 | void AddrRef::premutate( Expression * ) {
|
---|
231 | GuardValue( current );
|
---|
232 | GuardValue( first );
|
---|
233 | current = false;
|
---|
234 | first = true;
|
---|
235 | }
|
---|
236 |
|
---|
237 | void AddrRef::premutate( AddressExpr * ) {
|
---|
238 | GuardValue( current );
|
---|
239 | GuardValue( first );
|
---|
240 | current = first;
|
---|
241 | first = false;
|
---|
242 | if ( current ) {
|
---|
243 | GuardValue( refDepth );
|
---|
244 | refDepth = 0;
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | Expression * AddrRef::postmutate( AddressExpr * addrExpr ) {
|
---|
249 | if ( refDepth == 0 ) {
|
---|
250 | if ( ! isIntrinsicReference( addrExpr->arg ) ) {
|
---|
251 | // try to avoid ?[?]
|
---|
252 | refDepth = addrExpr->arg->result->referenceDepth();
|
---|
253 | }
|
---|
254 | }
|
---|
255 | if ( current ) {
|
---|
256 | Expression * ret = addrExpr;
|
---|
257 | while ( refDepth ) {
|
---|
258 | ret = mkDeref( ret );
|
---|
259 | refDepth--;
|
---|
260 | }
|
---|
261 | return ret;
|
---|
262 | }
|
---|
263 | return addrExpr;
|
---|
264 | }
|
---|
265 |
|
---|
266 | Expression * ReferenceConversions::postmutate( AddressExpr * addrExpr ) {
|
---|
267 | // Inner expression may have been lvalue to reference conversion, which becomes an address expression.
|
---|
268 | // In this case, remove the outer address expression and return the argument.
|
---|
269 | // TODO: It's possible that this might catch too much and require a more sophisticated check.
|
---|
270 | return addrExpr;
|
---|
271 | }
|
---|
272 |
|
---|
273 | Expression * ReferenceConversions::postmutate( CastExpr * castExpr ) {
|
---|
274 | // xxx - is it possible to convert directly between reference types with a different base? E.g.,
|
---|
275 | // int x;
|
---|
276 | // (double&)x;
|
---|
277 | // At the moment, I am working off of the assumption that this is illegal, thus the cast becomes redundant
|
---|
278 | // after this pass, so trash the cast altogether. If that changes, care must be taken to insert the correct
|
---|
279 | // pointer casts in the right places.
|
---|
280 |
|
---|
281 | // need to reorganize this so that depth difference is the determining factor in what code is run, rather than whether something is reference type or not.
|
---|
282 |
|
---|
283 | Type * destType = castExpr->result;
|
---|
284 | Type * srcType = castExpr->arg->result;
|
---|
285 | int depth1 = destType->referenceDepth();
|
---|
286 | int depth2 = srcType->referenceDepth();
|
---|
287 | int diff = depth1 - depth2;
|
---|
288 |
|
---|
289 | if ( diff > 0 && ! srcType->get_lvalue() ) {
|
---|
290 | // rvalue to reference conversion -- introduce temporary
|
---|
291 | // know that reference depth of cast argument is 0, need to introduce n temporaries for reference depth of n, e.g.
|
---|
292 | // (int &&&)3;
|
---|
293 | // becomes
|
---|
294 | // int __ref_tmp_0 = 3;
|
---|
295 | // int & __ref_tmp_1 = _&_ref_tmp_0;
|
---|
296 | // int && __ref_tmp_2 = &__ref_tmp_1;
|
---|
297 | // &__ref_tmp_2;
|
---|
298 |
|
---|
299 | static UniqueName tempNamer( "__ref_tmp_" );
|
---|
300 | ObjectDecl * temp = ObjectDecl::newObject( tempNamer.newName(), castExpr->arg->result->clone(), new SingleInit( castExpr->arg ) );
|
---|
301 | PRINT( std::cerr << "made temp: " << temp << std::endl; )
|
---|
302 | stmtsToAddBefore.push_back( new DeclStmt( temp ) );
|
---|
303 | for ( int i = 0; i < depth1-1; i++ ) {
|
---|
304 | ObjectDecl * newTemp = ObjectDecl::newObject( tempNamer.newName(), new ReferenceType( Type::Qualifiers(), temp->type->clone() ), new SingleInit( new AddressExpr( new VariableExpr( temp ) ) ) );
|
---|
305 | PRINT( std::cerr << "made temp" << i << ": " << newTemp << std::endl; )
|
---|
306 | stmtsToAddBefore.push_back( new DeclStmt( newTemp ) );
|
---|
307 | temp = newTemp;
|
---|
308 | }
|
---|
309 | // update diff so that remaining code works out correctly
|
---|
310 | castExpr->arg = new VariableExpr( temp );
|
---|
311 | PRINT( std::cerr << "update cast to: " << castExpr << std::endl; )
|
---|
312 | srcType = castExpr->arg->result;
|
---|
313 | depth2 = srcType->referenceDepth();
|
---|
314 | diff = depth1 - depth2;
|
---|
315 | assert( diff == 1 );
|
---|
316 | }
|
---|
317 |
|
---|
318 | PRINT (
|
---|
319 | if ( depth1 || depth2 ) {
|
---|
320 | std::cerr << "destType: " << destType << " / srcType: " << srcType << std::endl;
|
---|
321 | std::cerr << "depth: " << depth1 << " / " << depth2 << std::endl;
|
---|
322 | }
|
---|
323 | )
|
---|
324 | if ( diff > 0 ) {
|
---|
325 | // conversion to type with more depth (e.g. int & -> int &&): add address-of for each level of difference
|
---|
326 | Expression * ret = castExpr->arg;
|
---|
327 | for ( int i = 0; i < diff; ++i ) {
|
---|
328 | ret = new AddressExpr( ret );
|
---|
329 | }
|
---|
330 | if ( srcType->get_lvalue() && srcType->get_qualifiers() != strict_dynamic_cast<ReferenceType *>( destType )->base->get_qualifiers() ) {
|
---|
331 | // must keep cast if cast-to type is different from the actual type
|
---|
332 | castExpr->arg = ret;
|
---|
333 | return castExpr;
|
---|
334 | }
|
---|
335 | ret->env = castExpr->env;
|
---|
336 | delete ret->result;
|
---|
337 | ret->result = castExpr->result;
|
---|
338 | castExpr->env = nullptr;
|
---|
339 | castExpr->arg = nullptr;
|
---|
340 | castExpr->result = nullptr;
|
---|
341 | delete castExpr;
|
---|
342 | return ret;
|
---|
343 | } else if ( diff < 0 ) {
|
---|
344 | // conversion to type with less depth (e.g. int && -> int &): add dereferences for each level of difference
|
---|
345 | diff = -diff; // care only about magnitude now
|
---|
346 | Expression * ret = castExpr->arg;
|
---|
347 | for ( int i = 0; i < diff; ++i ) {
|
---|
348 | ret = mkDeref( ret );
|
---|
349 | }
|
---|
350 | if ( ! ResolvExpr::typesCompatibleIgnoreQualifiers( destType->stripReferences(), srcType->stripReferences(), SymTab::Indexer() ) ) {
|
---|
351 | // must keep cast if types are different
|
---|
352 | castExpr->arg = ret;
|
---|
353 | return castExpr;
|
---|
354 | }
|
---|
355 | ret->env = castExpr->env;
|
---|
356 | delete ret->result;
|
---|
357 | ret->result = castExpr->result;
|
---|
358 | ret->result->set_lvalue( true ); // ensure result is lvalue
|
---|
359 | castExpr->env = nullptr;
|
---|
360 | castExpr->arg = nullptr;
|
---|
361 | castExpr->result = nullptr;
|
---|
362 | delete castExpr;
|
---|
363 | return ret;
|
---|
364 | } else {
|
---|
365 | assert( diff == 0 );
|
---|
366 | // conversion between references of the same depth
|
---|
367 | return castExpr;
|
---|
368 | }
|
---|
369 |
|
---|
370 | // // conversion to reference type
|
---|
371 | // if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( castExpr->result ) ) {
|
---|
372 | // (void)refType;
|
---|
373 | // if ( ReferenceType * otherRef = dynamic_cast< ReferenceType * >( castExpr->arg->result ) ) {
|
---|
374 | // // nothing to do if casting from reference to reference.
|
---|
375 | // (void)otherRef;
|
---|
376 | // PRINT( std::cerr << "convert reference to reference -- nop" << std::endl; )
|
---|
377 | // if ( isIntrinsicReference( castExpr->arg ) ) {
|
---|
378 | // Expression * callExpr = castExpr->arg;
|
---|
379 | // PRINT(
|
---|
380 | // std::cerr << "but arg is deref -- &" << std::endl;
|
---|
381 | // std::cerr << callExpr << std::endl;
|
---|
382 | // )
|
---|
383 | // callExpr = new AddressExpr( callExpr ); // this doesn't work properly for multiple casts
|
---|
384 | // delete callExpr->result;
|
---|
385 | // callExpr->set_result( refType->clone() );
|
---|
386 | // // move environment out to new top-level
|
---|
387 | // callExpr->env = castExpr->env;
|
---|
388 | // castExpr->arg = nullptr;
|
---|
389 | // castExpr->env = nullptr;
|
---|
390 | // delete castExpr;
|
---|
391 | // return callExpr;
|
---|
392 | // }
|
---|
393 | // int depth1 = refType->referenceDepth();
|
---|
394 | // int depth2 = otherRef->referenceDepth();
|
---|
395 | // int diff = depth1-depth2;
|
---|
396 | // if ( diff == 0 ) {
|
---|
397 | // // conversion between references of the same depth
|
---|
398 | // assertf( depth1 == depth2, "non-intrinsic reference with cast of reference to reference not yet supported: %d %d %s", depth1, depth2, toString( castExpr ).c_str() );
|
---|
399 | // PRINT( std::cerr << castExpr << std::endl; )
|
---|
400 | // return castExpr;
|
---|
401 | // } else if ( diff < 0 ) {
|
---|
402 | // // conversion from reference to reference with less depth (e.g. int && -> int &): add dereferences
|
---|
403 | // Expression * ret = castExpr->arg;
|
---|
404 | // for ( int i = 0; i < diff; ++i ) {
|
---|
405 | // ret = mkDeref( ret );
|
---|
406 | // }
|
---|
407 | // ret->env = castExpr->env;
|
---|
408 | // delete ret->result;
|
---|
409 | // ret->result = castExpr->result;
|
---|
410 | // ret->result->set_lvalue( true ); // ensure result is lvalue
|
---|
411 | // castExpr->env = nullptr;
|
---|
412 | // castExpr->arg = nullptr;
|
---|
413 | // castExpr->result = nullptr;
|
---|
414 | // delete castExpr;
|
---|
415 | // return ret;
|
---|
416 | // } else if ( diff > 0 ) {
|
---|
417 | // // conversion from reference to reference with more depth (e.g. int & -> int &&): add address-of
|
---|
418 | // Expression * ret = castExpr->arg;
|
---|
419 | // for ( int i = 0; i < diff; ++i ) {
|
---|
420 | // ret = new AddressExpr( ret );
|
---|
421 | // }
|
---|
422 | // ret->env = castExpr->env;
|
---|
423 | // delete ret->result;
|
---|
424 | // ret->result = castExpr->result;
|
---|
425 | // castExpr->env = nullptr;
|
---|
426 | // castExpr->arg = nullptr;
|
---|
427 | // castExpr->result = nullptr;
|
---|
428 | // delete castExpr;
|
---|
429 | // return ret;
|
---|
430 | // }
|
---|
431 |
|
---|
432 | // assertf( depth1 == depth2, "non-intrinsic reference with cast of reference to reference not yet supported: %d %d %s", depth1, depth2, toString( castExpr ).c_str() );
|
---|
433 | // PRINT( std::cerr << castExpr << std::endl; )
|
---|
434 | // return castExpr;
|
---|
435 | // } else if ( castExpr->arg->result->get_lvalue() ) {
|
---|
436 | // // conversion from lvalue to reference
|
---|
437 | // // xxx - keep cast, but turn into pointer cast??
|
---|
438 | // // xxx - memory
|
---|
439 | // PRINT(
|
---|
440 | // std::cerr << "convert lvalue to reference -- &" << std::endl;
|
---|
441 | // std::cerr << castExpr->arg << std::endl;
|
---|
442 | // )
|
---|
443 | // AddressExpr * ret = new AddressExpr( castExpr->arg );
|
---|
444 | // if ( refType->base->get_qualifiers() != castExpr->arg->result->get_qualifiers() ) {
|
---|
445 | // // must keep cast if cast-to type is different from the actual type
|
---|
446 | // castExpr->arg = ret;
|
---|
447 | // return castExpr;
|
---|
448 | // }
|
---|
449 | // ret->env = castExpr->env;
|
---|
450 | // delete ret->result;
|
---|
451 | // ret->result = castExpr->result;
|
---|
452 | // castExpr->env = nullptr;
|
---|
453 | // castExpr->arg = nullptr;
|
---|
454 | // castExpr->result = nullptr;
|
---|
455 | // delete castExpr;
|
---|
456 | // return ret;
|
---|
457 | // } else {
|
---|
458 | // // rvalue to reference conversion -- introduce temporary
|
---|
459 | // // know that reference depth of cast argument is 0, need to introduce n temporaries for reference depth of n, e.g.
|
---|
460 | // // (int &&&)3;
|
---|
461 | // // becomes
|
---|
462 | // // int __ref_tmp_0 = 3;
|
---|
463 | // // int & __ref_tmp_1 = _&_ref_tmp_0;
|
---|
464 | // // int && __ref_tmp_2 = &__ref_tmp_1;
|
---|
465 | // // &__ref_tmp_2;
|
---|
466 |
|
---|
467 | // static UniqueName tempNamer( "__ref_tmp_" );
|
---|
468 | // ObjectDecl * temp = ObjectDecl::newObject( tempNamer.newName(), castExpr->arg->result->clone(), new SingleInit( castExpr->arg ) );
|
---|
469 | // stmtsToAddBefore.push_back( new DeclStmt( temp ) );
|
---|
470 | // auto depth = castExpr->result->referenceDepth();
|
---|
471 | // for ( int i = 0; i < depth-1; i++ ) {
|
---|
472 | // ObjectDecl * newTemp = ObjectDecl::newObject( tempNamer.newName(), new ReferenceType( Type::Qualifiers(), temp->type->clone() ), new SingleInit( new AddressExpr( new VariableExpr( temp ) ) ) );
|
---|
473 | // stmtsToAddBefore.push_back( new DeclStmt( newTemp ) );
|
---|
474 | // temp = newTemp;
|
---|
475 | // }
|
---|
476 | // Expression * ret = new AddressExpr( new VariableExpr( temp ) );
|
---|
477 | // // for ( int i = 0; i < depth; ++i ) {
|
---|
478 | // // ret = mkDeref( ret );
|
---|
479 | // // }
|
---|
480 | // ret->result = castExpr->result;
|
---|
481 | // ret->result->set_lvalue( true ); // ensure result is lvalue
|
---|
482 | // ret->env = castExpr->env;
|
---|
483 | // castExpr->arg = nullptr;
|
---|
484 | // castExpr->env = nullptr;
|
---|
485 | // castExpr->result = nullptr;
|
---|
486 | // delete castExpr;
|
---|
487 | // return ret;
|
---|
488 | // }
|
---|
489 | // } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( castExpr->arg->result ) ) {
|
---|
490 | // (void)refType;
|
---|
491 | // // conversion from reference to rvalue
|
---|
492 | // PRINT(
|
---|
493 | // std::cerr << "convert reference to rvalue -- *" << std::endl;
|
---|
494 | // std::cerr << "was = " << castExpr << std::endl;
|
---|
495 | // )
|
---|
496 | // Expression * ret = castExpr->arg;
|
---|
497 | // TypeSubstitution * env = castExpr->env;
|
---|
498 | // castExpr->set_env( nullptr );
|
---|
499 | // if ( ! isIntrinsicReference( ret ) ) {
|
---|
500 | // // dereference if not already dereferenced
|
---|
501 | // ret = mkDeref( ret );
|
---|
502 | // }
|
---|
503 | // if ( ResolvExpr::typesCompatibleIgnoreQualifiers( castExpr->result, castExpr->arg->result->stripReferences(), SymTab::Indexer() ) ) {
|
---|
504 | // // can remove cast if types are compatible, changing expression type to value type
|
---|
505 | // ret->result = castExpr->result->clone();
|
---|
506 | // ret->result->set_lvalue( true ); // ensure result is lvalue
|
---|
507 | // castExpr->arg = nullptr;
|
---|
508 | // delete castExpr;
|
---|
509 | // } else {
|
---|
510 | // // must keep cast if types are different
|
---|
511 | // castExpr->arg = ret;
|
---|
512 | // ret = castExpr;
|
---|
513 | // }
|
---|
514 | // ret->set_env( env );
|
---|
515 | // PRINT( std::cerr << "now: " << ret << std::endl; )
|
---|
516 | // return ret;
|
---|
517 | // }
|
---|
518 | // return castExpr;
|
---|
519 | }
|
---|
520 |
|
---|
521 | Type * ReferenceTypeElimination::postmutate( ReferenceType * refType ) {
|
---|
522 | Type * base = refType->base;
|
---|
523 | Type::Qualifiers qualifiers = refType->get_qualifiers();
|
---|
524 | refType->base = nullptr;
|
---|
525 | delete refType;
|
---|
526 | return new PointerType( qualifiers, base );
|
---|
527 | }
|
---|
528 |
|
---|
529 | template<typename Func>
|
---|
530 | Expression * GeneralizedLvalue::applyTransformation( Expression * expr, Expression * arg, Func mkExpr ) {
|
---|
531 | if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( arg ) ) {
|
---|
532 | Expression * arg1 = commaExpr->arg1->clone();
|
---|
533 | Expression * arg2 = commaExpr->arg2->clone();
|
---|
534 | Expression * ret = new CommaExpr( arg1, mkExpr( arg2 )->acceptMutator( *visitor ) );
|
---|
535 | ret->env = expr->env;
|
---|
536 | expr->env = nullptr;
|
---|
537 | delete expr;
|
---|
538 | return ret;
|
---|
539 | } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( arg ) ) {
|
---|
540 | Expression * arg1 = condExpr->arg1->clone();
|
---|
541 | Expression * arg2 = condExpr->arg2->clone();
|
---|
542 | Expression * arg3 = condExpr->arg3->clone();
|
---|
543 | ConditionalExpr * ret = new ConditionalExpr( arg1, mkExpr( arg2 )->acceptMutator( *visitor ), mkExpr( arg3 )->acceptMutator( *visitor ) );
|
---|
544 | ret->env = expr->env;
|
---|
545 | expr->env = nullptr;
|
---|
546 | delete expr;
|
---|
547 |
|
---|
548 | // conditional expr type may not be either of the argument types, need to unify
|
---|
549 | using namespace ResolvExpr;
|
---|
550 | Type* commonType = nullptr;
|
---|
551 | TypeEnvironment newEnv;
|
---|
552 | AssertionSet needAssertions, haveAssertions;
|
---|
553 | OpenVarSet openVars;
|
---|
554 | unify( ret->arg2->result, ret->arg3->result, newEnv, needAssertions, haveAssertions, openVars, SymTab::Indexer(), commonType );
|
---|
555 | ret->result = commonType ? commonType : ret->arg2->result->clone();
|
---|
556 | return ret;
|
---|
557 | }
|
---|
558 | return expr;
|
---|
559 | }
|
---|
560 |
|
---|
561 | Expression * GeneralizedLvalue::postmutate( MemberExpr * memExpr ) {
|
---|
562 | return applyTransformation( memExpr, memExpr->aggregate, [=]( Expression * aggr ) { return new MemberExpr( memExpr->member, aggr ); } );
|
---|
563 | }
|
---|
564 |
|
---|
565 | Expression * GeneralizedLvalue::postmutate( AddressExpr * addrExpr ) {
|
---|
566 | return applyTransformation( addrExpr, addrExpr->arg, []( Expression * arg ) { return new AddressExpr( arg ); } );
|
---|
567 | }
|
---|
568 |
|
---|
569 | Expression * CollapseAddrDeref::postmutate( AddressExpr * addrExpr ) {
|
---|
570 | Expression * arg = addrExpr->arg;
|
---|
571 | if ( isIntrinsicReference( arg ) ) {
|
---|
572 | std::string fname = InitTweak::getFunctionName( arg );
|
---|
573 | if ( fname == "*?" ) {
|
---|
574 | Expression *& arg0 = InitTweak::getCallArg( arg, 0 );
|
---|
575 | Expression * ret = arg0;
|
---|
576 | ret->set_env( addrExpr->env );
|
---|
577 | arg0 = nullptr;
|
---|
578 | addrExpr->env = nullptr;
|
---|
579 | delete addrExpr;
|
---|
580 | return ret;
|
---|
581 | }
|
---|
582 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * > ( arg ) ) {
|
---|
583 | // need to move cast to pointer type out a level since address of pointer
|
---|
584 | // is not valid C code (can be introduced in prior passes, e.g., InstantiateGeneric)
|
---|
585 | if ( InitTweak::getPointerBase( castExpr->result ) ) {
|
---|
586 | addrExpr->arg = castExpr->arg;
|
---|
587 | castExpr->arg = addrExpr;
|
---|
588 | castExpr->result = new PointerType( Type::Qualifiers(), castExpr->result );
|
---|
589 | return castExpr;
|
---|
590 | }
|
---|
591 | }
|
---|
592 | return addrExpr;
|
---|
593 | }
|
---|
594 |
|
---|
595 | Expression * CollapseAddrDeref::postmutate( ApplicationExpr * appExpr ) {
|
---|
596 | if ( isIntrinsicReference( appExpr ) ) {
|
---|
597 | std::string fname = InitTweak::getFunctionName( appExpr );
|
---|
598 | if ( fname == "*?" ) {
|
---|
599 | Expression * arg = InitTweak::getCallArg( appExpr, 0 );
|
---|
600 | // xxx - this isn't right, because it can remove casts that should be there...
|
---|
601 | // while ( CastExpr * castExpr = dynamic_cast< CastExpr * >( arg ) ) {
|
---|
602 | // arg = castExpr->get_arg();
|
---|
603 | // }
|
---|
604 | if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( arg ) ) {
|
---|
605 | Expression * ret = addrExpr->arg;
|
---|
606 | ret->env = appExpr->env;
|
---|
607 | addrExpr->arg = nullptr;
|
---|
608 | appExpr->env = nullptr;
|
---|
609 | delete appExpr;
|
---|
610 | return ret;
|
---|
611 | }
|
---|
612 | }
|
---|
613 | }
|
---|
614 | return appExpr;
|
---|
615 | }
|
---|
616 | } // namespace
|
---|
617 | } // namespace GenPoly
|
---|
618 |
|
---|
619 | // Local Variables: //
|
---|
620 | // tab-width: 4 //
|
---|
621 | // mode: c++ //
|
---|
622 | // compile-command: "make install" //
|
---|
623 | // End: //
|
---|