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