| [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 | //
 | 
|---|
| [01aeade] | 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
 | 
|---|
 | 12 | // Last Modified On : Tue May 19 07:41:33 2015
 | 
|---|
 | 13 | // Update Count     : 1
 | 
|---|
| [51587aa] | 14 | //
 | 
|---|
| [51b73452] | 15 | 
 | 
|---|
 | 16 | #include <cassert>
 | 
|---|
 | 17 | 
 | 
|---|
 | 18 | #include "Lvalue.h"
 | 
|---|
 | 19 | 
 | 
|---|
 | 20 | #include "SynTree/Declaration.h"
 | 
|---|
 | 21 | #include "SynTree/Type.h"
 | 
|---|
 | 22 | #include "SynTree/Expression.h"
 | 
|---|
 | 23 | #include "SynTree/Statement.h"
 | 
|---|
 | 24 | #include "SynTree/Visitor.h"
 | 
|---|
 | 25 | #include "SynTree/Mutator.h"
 | 
|---|
 | 26 | #include "SymTab/Indexer.h"
 | 
|---|
 | 27 | #include "ResolvExpr/Resolver.h"
 | 
|---|
 | 28 | #include "ResolvExpr/typeops.h"
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | #include "UniqueName.h"
 | 
|---|
 | 31 | #include "utility.h"
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 | namespace GenPoly {
 | 
|---|
| [01aeade] | 34 |         namespace {
 | 
|---|
 | 35 |                 const std::list<Label> noLabels;
 | 
|---|
| [51b73452] | 36 | 
 | 
|---|
| [01aeade] | 37 |                 class Pass1 : public Mutator {
 | 
|---|
 | 38 |                   public:
 | 
|---|
 | 39 |                         Pass1();
 | 
|---|
| [51b73452] | 40 |   
 | 
|---|
| [01aeade] | 41 |                         virtual Expression *mutate( ApplicationExpr *appExpr );
 | 
|---|
 | 42 |                         virtual Statement *mutate( ReturnStmt *appExpr );
 | 
|---|
 | 43 |                         virtual DeclarationWithType *mutate( FunctionDecl *funDecl );
 | 
|---|
 | 44 |                   private:
 | 
|---|
 | 45 |                         DeclarationWithType* retval;
 | 
|---|
 | 46 |                 };
 | 
|---|
 | 47 | 
 | 
|---|
 | 48 |                 class Pass2 : public Visitor {
 | 
|---|
 | 49 |                   public:
 | 
|---|
 | 50 |                         virtual void visit( FunctionType *funType );
 | 
|---|
 | 51 |                   private:
 | 
|---|
 | 52 |                 };
 | 
|---|
 | 53 |         } // namespace
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 |         void convertLvalue( std::list< Declaration* >& translationUnit ) {
 | 
|---|
 | 56 |                 Pass1 p1;
 | 
|---|
 | 57 |                 Pass2 p2;
 | 
|---|
 | 58 |                 mutateAll( translationUnit, p1 );
 | 
|---|
 | 59 |                 acceptAll( translationUnit, p2 );
 | 
|---|
 | 60 |         }
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 |         namespace {
 | 
|---|
 | 63 |                 bool isLvalueRet( FunctionType *function ) {
 | 
|---|
 | 64 |                         if ( ! function->get_returnVals().empty() ) {
 | 
|---|
 | 65 |                                 return function->get_returnVals().front()->get_type()->get_isLvalue();
 | 
|---|
 | 66 |                         } else {
 | 
|---|
 | 67 |                                 return false;
 | 
|---|
 | 68 |                         } // if
 | 
|---|
 | 69 |                 }
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 |                 bool isIntrinsicApp( ApplicationExpr *appExpr ) {
 | 
|---|
 | 72 |                         if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( appExpr->get_function() ) ) {
 | 
|---|
 | 73 |                                 return varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic;
 | 
|---|
 | 74 |                         } else {
 | 
|---|
 | 75 |                                 return false;
 | 
|---|
 | 76 |                         } // if
 | 
|---|
 | 77 |                 }
 | 
|---|
 | 78 | 
 | 
|---|
 | 79 |                 Pass1::Pass1() {
 | 
|---|
 | 80 |                 }
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 |                 DeclarationWithType * Pass1::mutate( FunctionDecl *funcDecl ) {
 | 
|---|
 | 83 |                         if ( funcDecl->get_statements() ) {
 | 
|---|
 | 84 |                                 DeclarationWithType* oldRetval = retval;
 | 
|---|
 | 85 |                                 retval = 0;
 | 
|---|
 | 86 |                                 if ( ! LinkageSpec::isBuiltin( funcDecl->get_linkage() ) && isLvalueRet( funcDecl->get_functionType() ) ) {
 | 
|---|
 | 87 |                                         retval = funcDecl->get_functionType()->get_returnVals().front();
 | 
|---|
 | 88 |                                 }
 | 
|---|
 | 89 |                                 // fix expressions and return statements in this function
 | 
|---|
 | 90 |                                 funcDecl->set_statements( funcDecl->get_statements()->acceptMutator( *this ) );
 | 
|---|
 | 91 |                                 retval = oldRetval;
 | 
|---|
 | 92 |                         } // if
 | 
|---|
 | 93 |                         return funcDecl;
 | 
|---|
 | 94 |                 }
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 |                 Expression * Pass1::mutate( ApplicationExpr *appExpr ) {
 | 
|---|
 | 97 |                         appExpr->get_function()->acceptMutator( *this );
 | 
|---|
 | 98 |                         mutateAll( appExpr->get_args(), *this );
 | 
|---|
| [51b73452] | 99 |   
 | 
|---|
| [01aeade] | 100 |                         assert( ! appExpr->get_function()->get_results().empty() );
 | 
|---|
 | 101 | 
 | 
|---|
 | 102 |                         PointerType *pointer = dynamic_cast< PointerType* >( appExpr->get_function()->get_results().front() );
 | 
|---|
 | 103 |                         assert( pointer );
 | 
|---|
 | 104 |                         FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() );
 | 
|---|
 | 105 |                         assert( function );
 | 
|---|
 | 106 | 
 | 
|---|
 | 107 |                         std::string typeName;
 | 
|---|
 | 108 |                         if ( isLvalueRet( function ) && ! isIntrinsicApp( appExpr ) ) {
 | 
|---|
 | 109 |                                 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
 | 
|---|
 | 110 |                                 deref->get_results().push_back( appExpr->get_results().front() );
 | 
|---|
 | 111 |                                 appExpr->get_results().front() = new PointerType( Type::Qualifiers(), deref->get_results().front()->clone() );
 | 
|---|
 | 112 |                                 deref->get_args().push_back( appExpr );
 | 
|---|
 | 113 |                                 return deref;
 | 
|---|
 | 114 |                         } else {
 | 
|---|
 | 115 |                                 return appExpr;
 | 
|---|
 | 116 |                         } // if
 | 
|---|
 | 117 |                 }
 | 
|---|
 | 118 | 
 | 
|---|
 | 119 |                 Statement * Pass1::mutate(ReturnStmt *retStmt) {
 | 
|---|
 | 120 |                         if ( retval && retStmt->get_expr() ) {
 | 
|---|
 | 121 |                                 assert( ! retStmt->get_expr()->get_results().empty() );
 | 
|---|
 | 122 |                                 while ( CastExpr *castExpr = dynamic_cast< CastExpr* >( retStmt->get_expr() ) ) {
 | 
|---|
 | 123 |                                         retStmt->set_expr( castExpr->get_arg() );
 | 
|---|
 | 124 |                                         retStmt->get_expr()->set_env( castExpr->get_env() );
 | 
|---|
 | 125 |                                         castExpr->set_env( 0 );
 | 
|---|
 | 126 |                                         castExpr->set_arg( 0 );
 | 
|---|
 | 127 |                                         delete castExpr;
 | 
|---|
 | 128 |                                 } // while
 | 
|---|
 | 129 |                                 if ( retStmt->get_expr()->get_results().front()->get_isLvalue() ) {
 | 
|---|
 | 130 |                                         retStmt->set_expr( new AddressExpr( retStmt->get_expr()->acceptMutator( *this ) ) );
 | 
|---|
 | 131 |                                 } else {
 | 
|---|
 | 132 |                                         throw SemanticError( "Attempt to return non-lvalue from an lvalue-qualified function" );
 | 
|---|
 | 133 |                                 } // if
 | 
|---|
 | 134 |                         } // if
 | 
|---|
 | 135 |                         return retStmt;
 | 
|---|
 | 136 |                 }
 | 
|---|
 | 137 | 
 | 
|---|
 | 138 |                 void Pass2::visit( FunctionType *funType ) {
 | 
|---|
 | 139 |                         std::string typeName;
 | 
|---|
 | 140 |                         if ( isLvalueRet( funType ) ) {
 | 
|---|
 | 141 |                                 DeclarationWithType *retParm = funType->get_returnVals().front();
 | 
|---|
 | 142 | 
 | 
|---|
 | 143 |                                 // make a new parameter that is a pointer to the type of the old return value
 | 
|---|
 | 144 |                                 retParm->set_type( new PointerType( Type::Qualifiers(), retParm->get_type() ) );
 | 
|---|
 | 145 |                         } // if
 | 
|---|
| [51b73452] | 146 |   
 | 
|---|
| [01aeade] | 147 |                         Visitor::visit( funType );
 | 
|---|
 | 148 |                 }
 | 
|---|
 | 149 |         } // namespace
 | 
|---|
| [51b73452] | 150 | } // namespace GenPoly
 | 
|---|
| [01aeade] | 151 | 
 | 
|---|
| [51587aa] | 152 | // Local Variables: //
 | 
|---|
 | 153 | // tab-width: 4 //
 | 
|---|
 | 154 | // mode: c++ //
 | 
|---|
 | 155 | // compile-command: "make install" //
 | 
|---|
 | 156 | // End: //
 | 
|---|