source: src/GenPoly/Lvalue.cc@ b1e63ac5

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 b1e63ac5 was b1e63ac5, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'master' into references

  • Property mode set to 100644
File size: 7.6 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 "ResolvExpr/Resolver.h"
30#include "ResolvExpr/typeops.h"
31
32#include "Common/UniqueName.h"
33#include "Common/utility.h"
34#include "InitTweak/InitTweak.h"
35
36namespace GenPoly {
37 namespace {
38 /// Replace uses of lvalue returns with appropriate pointers
39 class Pass1 : public Mutator {
40 public:
41 typedef Mutator Parent;
42 Pass1();
43
44 // xxx - should this happen to every expression with reference result type? probably just appexpr and varexpr?
45 virtual Type *mutate( ReferenceType * refType );
46 virtual Expression *mutate( VariableExpr *varExpr );
47 virtual Expression *mutate( ApplicationExpr *appExpr );
48 virtual Statement *mutate( ReturnStmt *appExpr );
49 virtual DeclarationWithType *mutate( FunctionDecl *funDecl );
50 private:
51 DeclarationWithType* retval;
52 };
53
54 /// Replace declarations of lvalue returns with appropriate pointers
55 class Pass2 : public Visitor {
56 public:
57 virtual void visit( FunctionType *funType );
58 private:
59 };
60
61 /// GCC-like Generalized Lvalues (which have since been removed from GCC)
62 /// https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Lvalues.html#Lvalues
63 /// Replaces &(a,b) with (a, &b), &(a ? b : c) with (a ? &b : &c)
64 class GeneralizedLvalue : public Mutator {
65 typedef Mutator Parent;
66
67 virtual Expression * mutate( AddressExpr * addressExpr );
68 };
69 } // namespace
70
71 void convertLvalue( std::list< Declaration* >& translationUnit ) {
72 Pass1 p1;
73 Pass2 p2;
74 GeneralizedLvalue genLval;
75 mutateAll( translationUnit, p1 );
76 acceptAll( translationUnit, p2 );
77 mutateAll( translationUnit, genLval );
78 }
79
80 namespace {
81 Type* isLvalueRet( FunctionType *function ) {
82 if ( function->get_returnVals().empty() ) return 0;
83 Type *ty = function->get_returnVals().front()->get_type();
84 return ty->get_lvalue() ? ty : 0;
85 }
86
87 bool isIntrinsicApp( ApplicationExpr *appExpr ) {
88 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( appExpr->get_function() ) ) {
89 return varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic;
90 } else {
91 return false;
92 } // if
93 }
94
95 Pass1::Pass1() {
96 }
97
98 DeclarationWithType * Pass1::mutate( FunctionDecl *funcDecl ) {
99 if ( funcDecl->get_statements() ) {
100 DeclarationWithType* oldRetval = retval;
101 retval = 0;
102 if ( ! LinkageSpec::isBuiltin( funcDecl->get_linkage() ) && isLvalueRet( funcDecl->get_functionType() ) ) {
103 retval = funcDecl->get_functionType()->get_returnVals().front();
104 }
105 // fix expressions and return statements in this function
106 funcDecl->set_statements( funcDecl->get_statements()->acceptMutator( *this ) );
107 retval = oldRetval;
108 } // if
109 return funcDecl;
110 }
111
112 Type * Pass1::mutate( ReferenceType * refType ) {
113 Type * base = refType->get_base();
114 refType->set_base( nullptr );
115 delete refType;
116 return new PointerType( Type::Qualifiers(), base );
117 }
118
119 Expression * Pass1::mutate( VariableExpr *varExpr ) {
120 if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( varExpr->get_result() ) ) {
121 varExpr->set_result( refType->acceptMutator( *this ) );
122 return UntypedExpr::createDeref( varExpr );
123 }
124 return Parent::mutate( varExpr );
125 }
126
127 Expression * Pass1::mutate( ApplicationExpr *appExpr ) {
128 appExpr->get_function()->acceptMutator( *this );
129 mutateAll( appExpr->get_args(), *this );
130
131 PointerType *pointer = safe_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() );
132 FunctionType *function = safe_dynamic_cast< FunctionType* >( pointer->get_base() );
133
134 Type *funType = isLvalueRet( function );
135 if ( funType && ! isIntrinsicApp( appExpr ) ) {
136 Expression *expr = appExpr;
137 Type *appType = appExpr->get_result();
138 if ( isPolyType( funType ) && ! isPolyType( appType ) ) {
139 // make sure cast for polymorphic type is inside dereference
140 expr = new CastExpr( appExpr, new PointerType( Type::Qualifiers(), appType->clone() ) );
141 }
142 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
143 deref->set_result( appType->clone() );
144 appExpr->set_result( new PointerType( Type::Qualifiers(), appType ) );
145 deref->get_args().push_back( expr );
146 return deref;
147 } else {
148 return appExpr;
149 } // if
150 }
151
152 Statement * Pass1::mutate(ReturnStmt *retStmt) {
153 if ( retval && retStmt->get_expr() ) {
154 if ( retStmt->get_expr()->get_result()->get_lvalue() ) {
155 // ***** Code Removal ***** because casts may be stripped already
156
157 // strip casts because not allowed to take address of cast
158 // while ( CastExpr *castExpr = dynamic_cast< CastExpr* >( retStmt->get_expr() ) ) {
159 // retStmt->set_expr( castExpr->get_arg() );
160 // retStmt->get_expr()->set_env( castExpr->get_env() );
161 // castExpr->set_env( 0 );
162 // castExpr->set_arg( 0 );
163 // delete castExpr;
164 // } // while
165 retStmt->set_expr( new AddressExpr( retStmt->get_expr()->acceptMutator( *this ) ) );
166 } else {
167 throw SemanticError( "Attempt to return non-lvalue from an lvalue-qualified function" );
168 } // if
169 } // if
170 return retStmt;
171 }
172
173 void Pass2::visit( FunctionType *funType ) {
174 std::string typeName;
175 if ( isLvalueRet( funType ) ) {
176 DeclarationWithType *retParm = funType->get_returnVals().front();
177
178 // make a new parameter that is a pointer to the type of the old return value
179 retParm->set_type( new PointerType( Type::Qualifiers(), retParm->get_type() ) );
180 } // if
181
182 Visitor::visit( funType );
183 }
184
185 bool isDeref( Expression * expr ) {
186 if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * >( expr ) ) {
187 return InitTweak::getFunctionName( untyped ) == "*?";
188 } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) {
189 return InitTweak::getFunctionName( appExpr ) == "*?";
190 } else {
191 return false;
192 }
193 }
194
195 Expression * GeneralizedLvalue::mutate( AddressExpr * addrExpr ) {
196 addrExpr = safe_dynamic_cast< AddressExpr * >( Parent::mutate( addrExpr ) );
197 if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( addrExpr->get_arg() ) ) {
198 Expression * arg1 = commaExpr->get_arg1()->clone();
199 Expression * arg2 = commaExpr->get_arg2()->clone();
200 delete addrExpr;
201 return new CommaExpr( arg1, new AddressExpr( arg2 ) );
202 } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( addrExpr->get_arg() ) ) {
203 Expression * arg1 = condExpr->get_arg1()->clone();
204 Expression * arg2 = condExpr->get_arg2()->clone();
205 Expression * arg3 = condExpr->get_arg3()->clone();
206 delete addrExpr;
207 return new ConditionalExpr( arg1, new AddressExpr( arg2 ), new AddressExpr( arg3 ) );
208 } else if ( isDeref( addrExpr->get_arg() ) ) {
209 // xxx - this doesn't belong here -- move it somewhere else
210 Expression *& arg = InitTweak::getCallArg( addrExpr->get_arg(), 0 );
211 Expression * inner = arg;
212 arg = nullptr;
213 delete addrExpr;
214 return inner;
215 }
216 return addrExpr;
217 }
218 } // namespace
219} // namespace GenPoly
220
221// Local Variables: //
222// tab-width: 4 //
223// mode: c++ //
224// compile-command: "make install" //
225// End: //
Note: See TracBrowser for help on using the repository browser.