source: src/GenPoly/Lvalue.cc@ e25707d

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since e25707d was b0440b7, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Prevent reference types from being added in Box through createDeref

  • Property mode set to 100644
File size: 14.1 KB
Line 
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>
17
18#include "Lvalue.h"
19
20#include "GenPoly.h"
21
22#include "SynTree/Declaration.h"
23#include "SynTree/Type.h"
24#include "SynTree/Expression.h"
25#include "SynTree/Statement.h"
26#include "SynTree/Visitor.h"
27#include "SynTree/Mutator.h"
28#include "SymTab/Indexer.h"
29#include "SymTab/Autogen.h"
30#include "ResolvExpr/Resolver.h"
31#include "ResolvExpr/typeops.h"
32
33#include "Common/UniqueName.h"
34#include "Common/utility.h"
35#include "InitTweak/InitTweak.h"
36
37#include "Common/PassVisitor.h"
38
39// need to be careful about polymorphic references... e.g. in *? (___operator_deref__A0_1_0_0__Fd0_Pd0_intrinsic___1)
40// the variable is automatically dereferenced and this causes errors dereferencing void*.
41
42#if 0
43#define PRINT(x) x
44#else
45#define PRINT(x)
46#endif
47
48namespace GenPoly {
49 namespace {
50 // TODO: fold this into the general createDeref function??
51 Expression * mkDeref( Expression * arg ) {
52 if ( SymTab::dereferenceOperator ) {
53 VariableExpr * deref = new VariableExpr( SymTab::dereferenceOperator );
54 deref->set_result( new PointerType( Type::Qualifiers(), deref->get_result() ) );
55 Type * base = InitTweak::getPointerBase( arg->get_result() );
56 assertf( base, "expected pointer type in dereference (type was %s)", toString( arg->get_result() ).c_str() );
57 ApplicationExpr * ret = new ApplicationExpr( deref, { arg } );
58 delete ret->get_result();
59 ret->set_result( new ReferenceType( Type::Qualifiers(), base->clone() ) );
60 return ret;
61 } else {
62 return UntypedExpr::createDeref( arg );
63 }
64 }
65
66 struct ReferenceConversions final {
67 Expression * postmutate( CastExpr * castExpr );
68 Expression * postmutate( AddressExpr * addrExpr );
69 };
70
71 /// Intrinsic functions that take reference parameters don't REALLY take reference parameters -- their reference arguments must always be implicitly dereferenced.
72 struct FixIntrinsicArgs final {
73 Expression * postmutate( ApplicationExpr *appExpr );
74 };
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 };
88
89 /// Removes redundant &*/*& pattern that this pass can generate
90 struct CollapseAddrDeref final {
91 Expression * postmutate( AddressExpr * addressExpr );
92 Expression * postmutate( ApplicationExpr * appExpr );
93 };
94 } // namespace
95
96 static bool referencesEliminated = false;
97 // used by UntypedExpr::createDeref to determine whether result type of dereference should be ReferenceType or value type.
98 bool referencesPermissable() {
99 return ! referencesEliminated;
100 }
101
102 void convertLvalue( std::list< Declaration* >& translationUnit ) {
103 std::cerr << "convertLvalue" << std::endl;
104 PassVisitor<ReferenceConversions> refCvt;
105 PassVisitor<ReferenceTypeElimination> elim;
106 PassVisitor<GeneralizedLvalue> genLval;
107 PassVisitor<FixIntrinsicArgs> fixer;
108 PassVisitor<CollapseAddrDeref> collapser;
109 mutateAll( translationUnit, refCvt );
110 mutateAll( translationUnit, fixer );
111 mutateAll( translationUnit, genLval );
112 mutateAll( translationUnit, collapser );
113 mutateAll( translationUnit, elim ); // last because other passes need reference types to work
114
115 // from this point forward, no other pass should create reference types.
116 referencesEliminated = true;
117 }
118
119 namespace {
120 // true for intrinsic function calls that return a reference
121 bool isIntrinsicReference( Expression * expr ) {
122 if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * >( expr ) ) {
123 std::string fname = InitTweak::getFunctionName( untyped );
124 // known intrinsic-reference prelude functions
125 return fname == "*?" || fname == "?[?]";
126 } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) {
127 if ( DeclarationWithType * func = InitTweak::getFunction( appExpr ) ) {
128 // use type of return variable rather than expr result type, since it may have been changed to a pointer type
129 FunctionType * ftype = GenPoly::getFunctionType( func->get_type() );
130 Type * ret = ftype->get_returnVals().empty() ? nullptr : ftype->get_returnVals().front()->get_type();
131 return func->get_linkage() == LinkageSpec::Intrinsic && dynamic_cast<ReferenceType *>( ret );
132 }
133 }
134 return false;
135 }
136
137 // xxx - might need to & every * (or every * that is an arg to non-intrinsic function??)
138 Expression * FixIntrinsicArgs::postmutate( ApplicationExpr * appExpr ) {
139 // intrinsic functions don't really take reference-typed parameters, so they require an implicit dereference on their arguments.
140 if ( DeclarationWithType * function = InitTweak::getFunction( appExpr ) ) {
141 FunctionType * ftype = GenPoly::getFunctionType( function->get_type() );
142 assertf( ftype, "Function declaration does not have function type." );
143 // can be of differing lengths only when function is variadic
144 assertf( ftype->get_parameters().size() == appExpr->get_args().size() || ftype->get_isVarArgs(), "ApplicationExpr args do not match formal parameter type." );
145
146 if ( isIntrinsicReference( appExpr ) ) {
147 // eliminate reference types from intrinsic applications - now they return lvalues
148 appExpr->set_result( appExpr->get_result()->stripReferences() );
149 appExpr->get_result()->set_lvalue( true );
150 }
151
152 unsigned int i = 0;
153 const unsigned int end = ftype->get_parameters().size();
154 for ( auto p : unsafe_group_iterate( appExpr->get_args(), ftype->get_parameters() ) ) {
155 if (i == end) break;
156 Expression *& arg = std::get<0>( p );
157 Type * formal = std::get<1>( p )->get_type();
158 PRINT(
159 std::cerr << "pair<0>: " << arg << std::endl;
160 std::cerr << "pair<1>: " << formal << std::endl;
161 )
162 if ( dynamic_cast<ReferenceType*>( formal ) ) {
163 if ( isIntrinsicReference( arg ) ) {
164 if ( function->get_linkage() != LinkageSpec::Intrinsic ) { // intrinsic functions that turn pointers into references
165 // if argument is dereference or array subscript, the result isn't REALLY a reference, so it's not necessary to fix the argument
166 PRINT( std::cerr << "is intrinsic arg in non-intrinsic call - adding address" << std::endl; )
167 arg = new AddressExpr( arg );
168 }
169 } else if ( function->get_linkage() == LinkageSpec::Intrinsic ) {
170 // if the parameter is a reference, add a dereference to the reference-typed argument.
171 Type * baseType = InitTweak::getPointerBase( arg->get_result() );
172 assertf( baseType, "parameter is reference, arg must be pointer or reference: %s", toString( arg->get_result() ) );
173 PointerType * ptrType = new PointerType( Type::Qualifiers(), baseType->clone() );
174 delete arg->get_result();
175 arg->set_result( ptrType );
176 arg = mkDeref( arg );
177 }
178 }
179 ++i;
180 }
181 }
182 return appExpr;
183 }
184
185 Expression * ReferenceConversions::postmutate( AddressExpr * addrExpr ) {
186 // Inner expression may have been lvalue to reference conversion, which becomes an address expression.
187 // In this case, remove the outer address expression and return the argument.
188 // TODO: It's possible that this might catch too much and require a more sophisticated check.
189 if ( dynamic_cast<AddressExpr*>( addrExpr->get_arg() ) ) {
190 Expression * arg = addrExpr->get_arg();
191 arg->set_env( addrExpr->get_env() );
192 addrExpr->set_arg( nullptr );
193 addrExpr->set_env( nullptr );
194 delete addrExpr;
195 return arg;
196 }
197 return addrExpr;
198 }
199
200 Expression * ReferenceConversions::postmutate( CastExpr * castExpr ) {
201 // xxx - is it possible to convert directly between reference types with a different base? E.g.,
202 // int x;
203 // (double&)x;
204 // At the moment, I am working off of the assumption that this is illegal, thus the cast becomes redundant
205 // after this pass, so trash the cast altogether. If that changes, care must be taken to insert the correct
206 // pointer casts in the right places.
207
208 // conversion to reference type
209 if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( castExpr->get_result() ) ) {
210 (void)refType;
211 if ( ReferenceType * otherRef = dynamic_cast< ReferenceType * >( castExpr->get_arg()->get_result() ) ) {
212 // nothing to do if casting from reference to reference.
213 (void)otherRef;
214 PRINT( std::cerr << "convert reference to reference -- nop" << std::endl; )
215 if ( isIntrinsicReference( castExpr->get_arg() ) ) {
216 Expression * callExpr = castExpr->get_arg();
217 PRINT(
218 std::cerr << "but arg is deref -- &" << std::endl;
219 std::cerr << callExpr << std::endl;
220 )
221 // move environment out to new top-level
222 callExpr->set_env( castExpr->get_env() );
223 castExpr->set_arg( nullptr );
224 castExpr->set_env( nullptr );
225 delete castExpr;
226 return callExpr;
227 }
228 assertf( false, "non-intrinsic reference with cast of reference to reference not yet supported: ", toString( castExpr ) );
229 PRINT( std::cerr << castExpr << std::endl; )
230 return castExpr;
231 } else if ( castExpr->get_arg()->get_result()->get_lvalue() ) {
232 // conversion from lvalue to reference
233 // xxx - keep cast, but turn into pointer cast??
234 // xxx - memory
235 PRINT(
236 std::cerr << "convert lvalue to reference -- &" << std::endl;
237 std::cerr << castExpr->get_arg() << std::endl;
238 )
239 AddressExpr * ret = new AddressExpr( castExpr->get_arg() );
240 if ( refType->get_base()->get_qualifiers() != castExpr->get_arg()->get_result()->get_qualifiers() ) {
241 // must keep cast if cast-to type is different from the actual type
242 castExpr->set_arg( ret );
243
244 return castExpr;
245 }
246 ret->set_env( castExpr->get_env() );
247 castExpr->set_env( nullptr );
248 castExpr->set_arg( nullptr );
249 delete castExpr;
250 return ret;
251 } else {
252 // rvalue to reference conversion -- introduce temporary
253 }
254 assertf( false, "Only conversions to reference from lvalue are currently supported: %s", toString( castExpr ).c_str() );
255 } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( castExpr->get_arg()->get_result() ) ) {
256 (void)refType;
257 // conversion from reference to rvalue
258 PRINT(
259 std::cerr << "convert reference to rvalue -- *" << std::endl;
260 std::cerr << "was = " << castExpr << std::endl;
261 )
262 Expression * ret = castExpr->get_arg();
263 TypeSubstitution * env = castExpr->get_env();
264 castExpr->set_env( nullptr );
265 if ( ! isIntrinsicReference( ret ) ) {
266 // dereference if not already dereferenced
267 ret = mkDeref( ret );
268 }
269 if ( ResolvExpr::typesCompatibleIgnoreQualifiers( castExpr->get_result(), castExpr->get_arg()->get_result()->stripReferences(), SymTab::Indexer() ) ) {
270 // can remove cast if types are compatible, changing expression type to value type
271 ret->set_result( castExpr->get_result()->clone() );
272 castExpr->set_arg( nullptr );
273 delete castExpr;
274 } else {
275 // must keep cast if types are different
276 castExpr->set_arg( ret );
277 ret = castExpr;
278 }
279 ret->set_env( env );
280 PRINT( std::cerr << "now: " << ret << std::endl; )
281 return ret;
282 }
283 return castExpr;
284 }
285
286 Type * ReferenceTypeElimination::postmutate( ReferenceType * refType ) {
287 Type * base = refType->get_base();
288 Type::Qualifiers qualifiers = refType->get_qualifiers();
289 refType->set_base( nullptr );
290 delete refType;
291 return new PointerType( qualifiers, base );
292 }
293
294 Expression * GeneralizedLvalue::postmutate( AddressExpr * addrExpr ) {
295 if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( addrExpr->get_arg() ) ) {
296 Expression * arg1 = commaExpr->get_arg1()->clone();
297 Expression * arg2 = commaExpr->get_arg2()->clone();
298 delete addrExpr;
299 return new CommaExpr( arg1, (new AddressExpr( arg2 ))->acceptMutator( *visitor ) );
300 } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( addrExpr->get_arg() ) ) {
301 Expression * arg1 = condExpr->get_arg1()->clone();
302 Expression * arg2 = condExpr->get_arg2()->clone();
303 Expression * arg3 = condExpr->get_arg3()->clone();
304 delete addrExpr;
305 return new ConditionalExpr( arg1, (new AddressExpr( arg2 ))->acceptMutator( *visitor ), (new AddressExpr( arg3 ))->acceptMutator( *visitor ) );
306 }
307 return addrExpr;
308 }
309
310 Expression * CollapseAddrDeref::postmutate( AddressExpr * addressExpr ) {
311 Expression * arg = addressExpr->get_arg();
312 if ( isIntrinsicReference( arg ) ) {
313 std::string fname = InitTweak::getFunctionName( arg );
314 if ( fname == "*?" ) {
315 Expression *& arg0 = InitTweak::getCallArg( arg, 0 );
316 Expression * ret = arg0;
317 ret->set_env( addressExpr->get_env() );
318 arg0 = nullptr;
319 addressExpr->set_env( nullptr );
320 delete addressExpr;
321 return ret;
322 }
323 }
324 return addressExpr;
325 }
326
327 Expression * CollapseAddrDeref::postmutate( ApplicationExpr * appExpr ) {
328 if ( isIntrinsicReference( appExpr ) ) {
329 std::string fname = InitTweak::getFunctionName( appExpr );
330 if ( fname == "*?" ) {
331 Expression * arg = InitTweak::getCallArg( appExpr, 0 );
332 // xxx - this isn't right, because it can remove casts that should be there...
333 // while ( CastExpr * castExpr = dynamic_cast< CastExpr * >( arg ) ) {
334 // arg = castExpr->get_arg();
335 // }
336 if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( arg ) ) {
337 Expression * ret = addrExpr->get_arg();
338 ret->set_env( appExpr->get_env() );
339 addrExpr->set_arg( nullptr );
340 appExpr->set_env( nullptr );
341 delete appExpr;
342 return ret;
343 }
344 }
345 }
346 return appExpr;
347 }
348 } // namespace
349} // namespace GenPoly
350
351// Local Variables: //
352// tab-width: 4 //
353// mode: c++ //
354// compile-command: "make install" //
355// End: //
Note: See TracBrowser for help on using the repository browser.