source: src/GenPoly/Lvalue.cc @ 08fc48f

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 08fc48f was 08fc48f, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Big header cleaning pass - commit 1

  • Property mode set to 100644
File size: 8.2 KB
RevLine 
[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
[08fc48f]16#include <cassert>                       // for safe_dynamic_cast
17#include <string>                        // for string
[51b7345]18
[08fc48f]19#include "Common/SemanticError.h"        // for SemanticError
20#include "GenPoly.h"                     // for isPolyType
[51b7345]21#include "Lvalue.h"
[08fc48f]22#include "Parser/LinkageSpec.h"          // for Spec, isBuiltin, Intrinsic
23#include "ResolvExpr/TypeEnvironment.h"  // for AssertionSet, OpenVarSet
24#include "ResolvExpr/Unify.h"            // for unify
25#include "SymTab/Indexer.h"              // for Indexer
26#include "SynTree/Declaration.h"         // for Declaration, FunctionDecl
27#include "SynTree/Expression.h"          // for Expression, ConditionalExpr
28#include "SynTree/Mutator.h"             // for mutateAll, Mutator
29#include "SynTree/Statement.h"           // for ReturnStmt, Statement (ptr o...
30#include "SynTree/Type.h"                // for PointerType, Type, FunctionType
31#include "SynTree/Visitor.h"             // for Visitor, acceptAll
[51b7345]32
33namespace GenPoly {
[01aeade]34        namespace {
[f8b961b]35                /// Replace uses of lvalue returns with appropriate pointers
[01aeade]36                class Pass1 : public Mutator {
37                  public:
38                        Pass1();
[906e24d]39
[01aeade]40                        virtual Expression *mutate( ApplicationExpr *appExpr );
41                        virtual Statement *mutate( ReturnStmt *appExpr );
42                        virtual DeclarationWithType *mutate( FunctionDecl *funDecl );
43                  private:
44                        DeclarationWithType* retval;
45                };
46
[f8b961b]47                /// Replace declarations of lvalue returns with appropriate pointers
[01aeade]48                class Pass2 : public Visitor {
49                  public:
50                        virtual void visit( FunctionType *funType );
51                  private:
52                };
[b6fd751]53
54                /// GCC-like Generalized Lvalues (which have since been removed from GCC)
55                /// https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Lvalues.html#Lvalues
56                /// Replaces &(a,b) with (a, &b), &(a ? b : c) with (a ? &b : &c)
57                class GeneralizedLvalue : public Mutator {
58                        typedef Mutator Parent;
59
[acd7c5dd]60                        virtual Expression * mutate( MemberExpr * memExpr );
[b6fd751]61                        virtual Expression * mutate( AddressExpr * addressExpr );
[acd7c5dd]62
63                        template<typename Func>
64                        Expression * applyTransformation( Expression * expr, Expression * arg, Func mkExpr );
[b6fd751]65                };
[01aeade]66        } // namespace
67
68        void convertLvalue( std::list< Declaration* >& translationUnit ) {
69                Pass1 p1;
70                Pass2 p2;
[b6fd751]71                GeneralizedLvalue genLval;
[01aeade]72                mutateAll( translationUnit, p1 );
73                acceptAll( translationUnit, p2 );
[b6fd751]74                mutateAll( translationUnit, genLval );
[01aeade]75        }
76
[acd7c5dd]77        Expression * generalizedLvalue( Expression * expr ) {
78                GeneralizedLvalue genLval;
79                return expr->acceptMutator( genLval );
80        }
81
[01aeade]82        namespace {
[02ad3f5]83                Type* isLvalueRet( FunctionType *function ) {
84                        if ( function->get_returnVals().empty() ) return 0;
85                        Type *ty = function->get_returnVals().front()->get_type();
[615a096]86                        return ty->get_lvalue() ? ty : 0;
[01aeade]87                }
88
89                bool isIntrinsicApp( ApplicationExpr *appExpr ) {
90                        if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( appExpr->get_function() ) ) {
91                                return varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic;
92                        } else {
93                                return false;
94                        } // if
95                }
96
97                Pass1::Pass1() {
98                }
99
100                DeclarationWithType * Pass1::mutate( FunctionDecl *funcDecl ) {
101                        if ( funcDecl->get_statements() ) {
102                                DeclarationWithType* oldRetval = retval;
103                                retval = 0;
104                                if ( ! LinkageSpec::isBuiltin( funcDecl->get_linkage() ) && isLvalueRet( funcDecl->get_functionType() ) ) {
105                                        retval = funcDecl->get_functionType()->get_returnVals().front();
106                                }
107                                // fix expressions and return statements in this function
108                                funcDecl->set_statements( funcDecl->get_statements()->acceptMutator( *this ) );
109                                retval = oldRetval;
110                        } // if
111                        return funcDecl;
112                }
113
114                Expression * Pass1::mutate( ApplicationExpr *appExpr ) {
115                        appExpr->get_function()->acceptMutator( *this );
116                        mutateAll( appExpr->get_args(), *this );
117
[906e24d]118                        PointerType *pointer = safe_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() );
119                        FunctionType *function = safe_dynamic_cast< FunctionType* >( pointer->get_base() );
[01aeade]120
[02ad3f5]121                        Type *funType = isLvalueRet( function );
122                        if ( funType && ! isIntrinsicApp( appExpr ) ) {
123                                Expression *expr = appExpr;
[906e24d]124                                Type *appType = appExpr->get_result();
[baba5d8]125                                if ( isPolyType( funType ) && ! isPolyType( appType ) ) {
[02ad3f5]126                                        // make sure cast for polymorphic type is inside dereference
127                                        expr = new CastExpr( appExpr, new PointerType( Type::Qualifiers(), appType->clone() ) );
[baba5d8]128                                }
[01aeade]129                                UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
[906e24d]130                                deref->set_result( appType->clone() );
131                                appExpr->set_result( new PointerType( Type::Qualifiers(), appType ) );
[02ad3f5]132                                deref->get_args().push_back( expr );
[01aeade]133                                return deref;
134                        } else {
135                                return appExpr;
136                        } // if
137                }
138
139                Statement * Pass1::mutate(ReturnStmt *retStmt) {
140                        if ( retval && retStmt->get_expr() ) {
[615a096]141                                if ( retStmt->get_expr()->get_result()->get_lvalue() ) {
[cf16f94]142                                        // ***** Code Removal ***** because casts may be stripped already
143
144                                        // strip casts because not allowed to take address of cast
145                                        // while ( CastExpr *castExpr = dynamic_cast< CastExpr* >( retStmt->get_expr() ) ) {
146                                        //      retStmt->set_expr( castExpr->get_arg() );
147                                        //      retStmt->get_expr()->set_env( castExpr->get_env() );
148                                        //      castExpr->set_env( 0 );
149                                        //      castExpr->set_arg( 0 );
150                                        //      delete castExpr;
151                                        // } // while
[01aeade]152                                        retStmt->set_expr( new AddressExpr( retStmt->get_expr()->acceptMutator( *this ) ) );
153                                } else {
154                                        throw SemanticError( "Attempt to return non-lvalue from an lvalue-qualified function" );
155                                } // if
156                        } // if
157                        return retStmt;
158                }
159
160                void Pass2::visit( FunctionType *funType ) {
161                        std::string typeName;
162                        if ( isLvalueRet( funType ) ) {
163                                DeclarationWithType *retParm = funType->get_returnVals().front();
164
165                                // make a new parameter that is a pointer to the type of the old return value
166                                retParm->set_type( new PointerType( Type::Qualifiers(), retParm->get_type() ) );
167                        } // if
[906e24d]168
[01aeade]169                        Visitor::visit( funType );
170                }
[b6fd751]171
[acd7c5dd]172                template<typename Func>
173                Expression * GeneralizedLvalue::applyTransformation( Expression * expr, Expression * arg, Func mkExpr ) {
174                        if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( arg ) ) {
[b6fd751]175                                Expression * arg1 = commaExpr->get_arg1()->clone();
176                                Expression * arg2 = commaExpr->get_arg2()->clone();
[acd7c5dd]177                                Expression * ret = new CommaExpr( arg1, mkExpr( arg2 ) );
178                                ret->set_env( expr->get_env() );
179                                expr->set_env( nullptr );
180                                delete expr;
181                                return ret->acceptMutator( *this );
182                        } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( arg ) ) {
[b6fd751]183                                Expression * arg1 = condExpr->get_arg1()->clone();
184                                Expression * arg2 = condExpr->get_arg2()->clone();
185                                Expression * arg3 = condExpr->get_arg3()->clone();
[acd7c5dd]186                                ConditionalExpr * ret = new ConditionalExpr( arg1, mkExpr( arg2 ), mkExpr( arg3 ) );
187                                ret->set_env( expr->get_env() );
188                                expr->set_env( nullptr );
189                                delete expr;
190
191                                // conditional expr type may not be either of the argument types, need to unify
192                                using namespace ResolvExpr;
193                                Type* commonType = nullptr;
194                                TypeEnvironment newEnv;
195                                AssertionSet needAssertions, haveAssertions;
196                                OpenVarSet openVars;
197                                unify( ret->get_arg2()->get_result(), ret->get_arg3()->get_result(), newEnv, needAssertions, haveAssertions, openVars, SymTab::Indexer(), commonType );
198                                ret->set_result( commonType ? commonType : ret->get_arg2()->get_result()->clone() );
199                                return ret->acceptMutator( *this );
[b6fd751]200                        }
[acd7c5dd]201                        return expr;
202                }
203
204                Expression * GeneralizedLvalue::mutate( MemberExpr * memExpr ) {
205                        Parent::mutate( memExpr );
206                        return applyTransformation( memExpr, memExpr->get_aggregate(), [=]( Expression * aggr ) { return new MemberExpr( memExpr->get_member(), aggr ); } );
207                }
208
209                Expression * GeneralizedLvalue::mutate( AddressExpr * addrExpr ) {
210                        addrExpr = safe_dynamic_cast< AddressExpr * >( Parent::mutate( addrExpr ) );
211                        return applyTransformation( addrExpr, addrExpr->get_arg(), []( Expression * arg ) { return new AddressExpr( arg ); } );
[b6fd751]212                }
[01aeade]213        } // namespace
[51b7345]214} // namespace GenPoly
[01aeade]215
[51587aa]216// Local Variables: //
217// tab-width: 4 //
218// mode: c++ //
219// compile-command: "make install" //
220// End: //
Note: See TracBrowser for help on using the repository browser.