source: src/GenPoly/Lvalue.cc @ 47534159

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 47534159 was f8b961b, checked in by Aaron Moss <a3moss@…>, 8 years ago

Documentation improvements

  • Property mode set to 100644
File size: 4.7 KB
Line 
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//
7// Lvalue.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue May 19 07:41:33 2015
13// Update Count     : 1
14//
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
33namespace GenPoly {
34        namespace {
35                const std::list<Label> noLabels;
36
37                /// Replace uses of lvalue returns with appropriate pointers
38                class Pass1 : public Mutator {
39                  public:
40                        Pass1();
41 
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
49                /// Replace declarations of lvalue returns with appropriate pointers
50                class Pass2 : public Visitor {
51                  public:
52                        virtual void visit( FunctionType *funType );
53                  private:
54                };
55        } // namespace
56
57        void convertLvalue( std::list< Declaration* >& translationUnit ) {
58                Pass1 p1;
59                Pass2 p2;
60                mutateAll( translationUnit, p1 );
61                acceptAll( translationUnit, p2 );
62        }
63
64        namespace {
65                bool isLvalueRet( FunctionType *function ) {
66                        if ( ! function->get_returnVals().empty() ) {
67                                return function->get_returnVals().front()->get_type()->get_isLvalue();
68                        } else {
69                                return false;
70                        } // if
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 
102                        assert( ! appExpr->get_function()->get_results().empty() );
103
104                        PointerType *pointer = dynamic_cast< PointerType* >( appExpr->get_function()->get_results().front() );
105                        assert( pointer );
106                        FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() );
107                        assert( function );
108
109                        std::string typeName;
110                        if ( isLvalueRet( function ) && ! isIntrinsicApp( appExpr ) ) {
111                                UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
112                                deref->get_results().push_back( appExpr->get_results().front() );
113                                appExpr->get_results().front() = new PointerType( Type::Qualifiers(), deref->get_results().front()->clone() );
114                                deref->get_args().push_back( appExpr );
115                                return deref;
116                        } else {
117                                return appExpr;
118                        } // if
119                }
120
121                Statement * Pass1::mutate(ReturnStmt *retStmt) {
122                        if ( retval && retStmt->get_expr() ) {
123                                assert( ! retStmt->get_expr()->get_results().empty() );
124                                while ( CastExpr *castExpr = dynamic_cast< CastExpr* >( retStmt->get_expr() ) ) {
125                                        retStmt->set_expr( castExpr->get_arg() );
126                                        retStmt->get_expr()->set_env( castExpr->get_env() );
127                                        castExpr->set_env( 0 );
128                                        castExpr->set_arg( 0 );
129                                        delete castExpr;
130                                } // while
131                                if ( retStmt->get_expr()->get_results().front()->get_isLvalue() ) {
132                                        retStmt->set_expr( new AddressExpr( retStmt->get_expr()->acceptMutator( *this ) ) );
133                                } else {
134                                        throw SemanticError( "Attempt to return non-lvalue from an lvalue-qualified function" );
135                                } // if
136                        } // if
137                        return retStmt;
138                }
139
140                void Pass2::visit( FunctionType *funType ) {
141                        std::string typeName;
142                        if ( isLvalueRet( funType ) ) {
143                                DeclarationWithType *retParm = funType->get_returnVals().front();
144
145                                // make a new parameter that is a pointer to the type of the old return value
146                                retParm->set_type( new PointerType( Type::Qualifiers(), retParm->get_type() ) );
147                        } // if
148 
149                        Visitor::visit( funType );
150                }
151        } // namespace
152} // namespace GenPoly
153
154// Local Variables: //
155// tab-width: 4 //
156// mode: c++ //
157// compile-command: "make install" //
158// End: //
Note: See TracBrowser for help on using the repository browser.