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