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