[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 |
---|
[cf16f94] | 12 | // Last Modified On : Tue Dec 15 15:33:13 2015 |
---|
| 13 | // Update Count : 3 |
---|
[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 { |
---|
| 37 | const std::list<Label> noLabels; |
---|
[51b7345] | 38 | |
---|
[f8b961b] | 39 | /// Replace uses of lvalue returns with appropriate pointers |
---|
[01aeade] | 40 | class Pass1 : public Mutator { |
---|
| 41 | public: |
---|
| 42 | Pass1(); |
---|
[906e24d] | 43 | |
---|
[01aeade] | 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 | |
---|
[f8b961b] | 51 | /// Replace declarations of lvalue returns with appropriate pointers |
---|
[01aeade] | 52 | class Pass2 : public Visitor { |
---|
| 53 | public: |
---|
| 54 | virtual void visit( FunctionType *funType ); |
---|
| 55 | private: |
---|
| 56 | }; |
---|
| 57 | } // namespace |
---|
| 58 | |
---|
| 59 | void convertLvalue( std::list< Declaration* >& translationUnit ) { |
---|
| 60 | Pass1 p1; |
---|
| 61 | Pass2 p2; |
---|
| 62 | mutateAll( translationUnit, p1 ); |
---|
| 63 | acceptAll( translationUnit, p2 ); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | namespace { |
---|
[02ad3f5] | 67 | Type* isLvalueRet( FunctionType *function ) { |
---|
| 68 | if ( function->get_returnVals().empty() ) return 0; |
---|
| 69 | Type *ty = function->get_returnVals().front()->get_type(); |
---|
| 70 | return ty->get_isLvalue() ? ty : 0; |
---|
[01aeade] | 71 | } |
---|
| 72 | |
---|
| 73 | bool isIntrinsicApp( ApplicationExpr *appExpr ) { |
---|
| 74 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( appExpr->get_function() ) ) { |
---|
| 75 | return varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic; |
---|
| 76 | } else { |
---|
| 77 | return false; |
---|
| 78 | } // if |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | Pass1::Pass1() { |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | DeclarationWithType * Pass1::mutate( FunctionDecl *funcDecl ) { |
---|
| 85 | if ( funcDecl->get_statements() ) { |
---|
| 86 | DeclarationWithType* oldRetval = retval; |
---|
| 87 | retval = 0; |
---|
| 88 | if ( ! LinkageSpec::isBuiltin( funcDecl->get_linkage() ) && isLvalueRet( funcDecl->get_functionType() ) ) { |
---|
| 89 | retval = funcDecl->get_functionType()->get_returnVals().front(); |
---|
| 90 | } |
---|
| 91 | // fix expressions and return statements in this function |
---|
| 92 | funcDecl->set_statements( funcDecl->get_statements()->acceptMutator( *this ) ); |
---|
| 93 | retval = oldRetval; |
---|
| 94 | } // if |
---|
| 95 | return funcDecl; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | Expression * Pass1::mutate( ApplicationExpr *appExpr ) { |
---|
| 99 | appExpr->get_function()->acceptMutator( *this ); |
---|
| 100 | mutateAll( appExpr->get_args(), *this ); |
---|
| 101 | |
---|
[906e24d] | 102 | PointerType *pointer = safe_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() ); |
---|
| 103 | FunctionType *function = safe_dynamic_cast< FunctionType* >( pointer->get_base() ); |
---|
[01aeade] | 104 | |
---|
[02ad3f5] | 105 | Type *funType = isLvalueRet( function ); |
---|
| 106 | if ( funType && ! isIntrinsicApp( appExpr ) ) { |
---|
| 107 | Expression *expr = appExpr; |
---|
[906e24d] | 108 | Type *appType = appExpr->get_result(); |
---|
[baba5d8] | 109 | if ( isPolyType( funType ) && ! isPolyType( appType ) ) { |
---|
[02ad3f5] | 110 | // make sure cast for polymorphic type is inside dereference |
---|
| 111 | expr = new CastExpr( appExpr, new PointerType( Type::Qualifiers(), appType->clone() ) ); |
---|
[baba5d8] | 112 | } |
---|
[01aeade] | 113 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
[906e24d] | 114 | deref->set_result( appType->clone() ); |
---|
| 115 | appExpr->set_result( new PointerType( Type::Qualifiers(), appType ) ); |
---|
[02ad3f5] | 116 | deref->get_args().push_back( expr ); |
---|
[01aeade] | 117 | return deref; |
---|
| 118 | } else { |
---|
| 119 | return appExpr; |
---|
| 120 | } // if |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | Statement * Pass1::mutate(ReturnStmt *retStmt) { |
---|
| 124 | if ( retval && retStmt->get_expr() ) { |
---|
[906e24d] | 125 | if ( retStmt->get_expr()->get_result()->get_isLvalue() ) { |
---|
[cf16f94] | 126 | // ***** Code Removal ***** because casts may be stripped already |
---|
| 127 | |
---|
| 128 | // strip casts because not allowed to take address of cast |
---|
| 129 | // while ( CastExpr *castExpr = dynamic_cast< CastExpr* >( retStmt->get_expr() ) ) { |
---|
| 130 | // retStmt->set_expr( castExpr->get_arg() ); |
---|
| 131 | // retStmt->get_expr()->set_env( castExpr->get_env() ); |
---|
| 132 | // castExpr->set_env( 0 ); |
---|
| 133 | // castExpr->set_arg( 0 ); |
---|
| 134 | // delete castExpr; |
---|
| 135 | // } // while |
---|
[01aeade] | 136 | retStmt->set_expr( new AddressExpr( retStmt->get_expr()->acceptMutator( *this ) ) ); |
---|
| 137 | } else { |
---|
| 138 | throw SemanticError( "Attempt to return non-lvalue from an lvalue-qualified function" ); |
---|
| 139 | } // if |
---|
| 140 | } // if |
---|
| 141 | return retStmt; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | void Pass2::visit( FunctionType *funType ) { |
---|
| 145 | std::string typeName; |
---|
| 146 | if ( isLvalueRet( funType ) ) { |
---|
| 147 | DeclarationWithType *retParm = funType->get_returnVals().front(); |
---|
| 148 | |
---|
| 149 | // make a new parameter that is a pointer to the type of the old return value |
---|
| 150 | retParm->set_type( new PointerType( Type::Qualifiers(), retParm->get_type() ) ); |
---|
| 151 | } // if |
---|
[906e24d] | 152 | |
---|
[01aeade] | 153 | Visitor::visit( funType ); |
---|
| 154 | } |
---|
| 155 | } // namespace |
---|
[51b7345] | 156 | } // namespace GenPoly |
---|
[01aeade] | 157 | |
---|
[51587aa] | 158 | // Local Variables: // |
---|
| 159 | // tab-width: 4 // |
---|
| 160 | // mode: c++ // |
---|
| 161 | // compile-command: "make install" // |
---|
| 162 | // End: // |
---|