[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 | //
|
---|
[51b73452] | 15 |
|
---|
| 16 | #include <cassert>
|
---|
| 17 |
|
---|
| 18 | #include "Lvalue.h"
|
---|
| 19 |
|
---|
[02ad3f5] | 20 | #include "GenPoly.h"
|
---|
| 21 |
|
---|
[51b73452] | 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"
|
---|
[acd7c5dd] | 29 |
|
---|
[51b73452] | 30 | #include "ResolvExpr/Resolver.h"
|
---|
[acd7c5dd] | 31 | #include "ResolvExpr/TypeEnvironment.h"
|
---|
[51b73452] | 32 | #include "ResolvExpr/typeops.h"
|
---|
[acd7c5dd] | 33 | #include "ResolvExpr/Unify.h"
|
---|
[51b73452] | 34 |
|
---|
[d3b7937] | 35 | #include "Common/UniqueName.h"
|
---|
| 36 | #include "Common/utility.h"
|
---|
[51b73452] | 37 |
|
---|
| 38 | namespace GenPoly {
|
---|
[01aeade] | 39 | namespace {
|
---|
[f8b961b] | 40 | /// Replace uses of lvalue returns with appropriate pointers
|
---|
[01aeade] | 41 | class Pass1 : public Mutator {
|
---|
| 42 | public:
|
---|
| 43 | Pass1();
|
---|
[906e24d] | 44 |
|
---|
[01aeade] | 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 |
|
---|
[f8b961b] | 52 | /// Replace declarations of lvalue returns with appropriate pointers
|
---|
[01aeade] | 53 | class Pass2 : public Visitor {
|
---|
| 54 | public:
|
---|
| 55 | virtual void visit( FunctionType *funType );
|
---|
| 56 | private:
|
---|
| 57 | };
|
---|
[b6fd751] | 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 |
|
---|
[acd7c5dd] | 65 | virtual Expression * mutate( MemberExpr * memExpr );
|
---|
[b6fd751] | 66 | virtual Expression * mutate( AddressExpr * addressExpr );
|
---|
[acd7c5dd] | 67 |
|
---|
| 68 | template<typename Func>
|
---|
| 69 | Expression * applyTransformation( Expression * expr, Expression * arg, Func mkExpr );
|
---|
[b6fd751] | 70 | };
|
---|
[01aeade] | 71 | } // namespace
|
---|
| 72 |
|
---|
| 73 | void convertLvalue( std::list< Declaration* >& translationUnit ) {
|
---|
| 74 | Pass1 p1;
|
---|
| 75 | Pass2 p2;
|
---|
[b6fd751] | 76 | GeneralizedLvalue genLval;
|
---|
[01aeade] | 77 | mutateAll( translationUnit, p1 );
|
---|
| 78 | acceptAll( translationUnit, p2 );
|
---|
[b6fd751] | 79 | mutateAll( translationUnit, genLval );
|
---|
[01aeade] | 80 | }
|
---|
| 81 |
|
---|
[acd7c5dd] | 82 | Expression * generalizedLvalue( Expression * expr ) {
|
---|
| 83 | GeneralizedLvalue genLval;
|
---|
| 84 | return expr->acceptMutator( genLval );
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[01aeade] | 87 | namespace {
|
---|
[02ad3f5] | 88 | Type* isLvalueRet( FunctionType *function ) {
|
---|
| 89 | if ( function->get_returnVals().empty() ) return 0;
|
---|
| 90 | Type *ty = function->get_returnVals().front()->get_type();
|
---|
[615a096] | 91 | return ty->get_lvalue() ? ty : 0;
|
---|
[01aeade] | 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 |
|
---|
[906e24d] | 123 | PointerType *pointer = safe_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() );
|
---|
| 124 | FunctionType *function = safe_dynamic_cast< FunctionType* >( pointer->get_base() );
|
---|
[01aeade] | 125 |
|
---|
[02ad3f5] | 126 | Type *funType = isLvalueRet( function );
|
---|
| 127 | if ( funType && ! isIntrinsicApp( appExpr ) ) {
|
---|
| 128 | Expression *expr = appExpr;
|
---|
[906e24d] | 129 | Type *appType = appExpr->get_result();
|
---|
[baba5d8] | 130 | if ( isPolyType( funType ) && ! isPolyType( appType ) ) {
|
---|
[02ad3f5] | 131 | // make sure cast for polymorphic type is inside dereference
|
---|
| 132 | expr = new CastExpr( appExpr, new PointerType( Type::Qualifiers(), appType->clone() ) );
|
---|
[baba5d8] | 133 | }
|
---|
[01aeade] | 134 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
|
---|
[906e24d] | 135 | deref->set_result( appType->clone() );
|
---|
| 136 | appExpr->set_result( new PointerType( Type::Qualifiers(), appType ) );
|
---|
[02ad3f5] | 137 | deref->get_args().push_back( expr );
|
---|
[01aeade] | 138 | return deref;
|
---|
| 139 | } else {
|
---|
| 140 | return appExpr;
|
---|
| 141 | } // if
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | Statement * Pass1::mutate(ReturnStmt *retStmt) {
|
---|
| 145 | if ( retval && retStmt->get_expr() ) {
|
---|
[615a096] | 146 | if ( retStmt->get_expr()->get_result()->get_lvalue() ) {
|
---|
[cf16f94] | 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
|
---|
[01aeade] | 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
|
---|
[906e24d] | 173 |
|
---|
[01aeade] | 174 | Visitor::visit( funType );
|
---|
| 175 | }
|
---|
[b6fd751] | 176 |
|
---|
[acd7c5dd] | 177 | template<typename Func>
|
---|
| 178 | Expression * GeneralizedLvalue::applyTransformation( Expression * expr, Expression * arg, Func mkExpr ) {
|
---|
| 179 | if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( arg ) ) {
|
---|
[b6fd751] | 180 | Expression * arg1 = commaExpr->get_arg1()->clone();
|
---|
| 181 | Expression * arg2 = commaExpr->get_arg2()->clone();
|
---|
[acd7c5dd] | 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 ) ) {
|
---|
[b6fd751] | 188 | Expression * arg1 = condExpr->get_arg1()->clone();
|
---|
| 189 | Expression * arg2 = condExpr->get_arg2()->clone();
|
---|
| 190 | Expression * arg3 = condExpr->get_arg3()->clone();
|
---|
[acd7c5dd] | 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 );
|
---|
[b6fd751] | 205 | }
|
---|
[acd7c5dd] | 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 ); } );
|
---|
[b6fd751] | 217 | }
|
---|
[01aeade] | 218 | } // namespace
|
---|
[51b73452] | 219 | } // namespace GenPoly
|
---|
[01aeade] | 220 |
|
---|
[51587aa] | 221 | // Local Variables: //
|
---|
| 222 | // tab-width: 4 //
|
---|
| 223 | // mode: c++ //
|
---|
| 224 | // compile-command: "make install" //
|
---|
| 225 | // End: //
|
---|