source: translator/GenPoly/Lvalue.cc @ 51587aa

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 51587aa was 51587aa, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

licencing: fourth groups of files

  • Property mode set to 100644
File size: 4.6 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// XXX.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By :
12// Last Modified On :
13// Update Count     : 0
14//
15/*
16 * This file is part of the Cforall project
17 *
18 * $Id: Lvalue.cc,v 1.5 2005/08/29 20:14:13 rcbilson Exp $
19 *
20 */
21
22#include <cassert>
23
24#include "Lvalue.h"
25
26#include "SynTree/Declaration.h"
27#include "SynTree/Type.h"
28#include "SynTree/Expression.h"
29#include "SynTree/Statement.h"
30#include "SynTree/Visitor.h"
31#include "SynTree/Mutator.h"
32#include "SymTab/Indexer.h"
33#include "ResolvExpr/Resolver.h"
34#include "ResolvExpr/typeops.h"
35
36#include "UniqueName.h"
37#include "utility.h"
38
39
40namespace GenPoly {
41
42namespace {
43
44const std::list<Label> noLabels;
45
46class Pass1 : public Mutator
47{
48public:
49  Pass1();
50 
51  virtual Expression *mutate( ApplicationExpr *appExpr );
52  virtual Statement *mutate( ReturnStmt *appExpr );
53  virtual DeclarationWithType *mutate( FunctionDecl *funDecl );
54
55private:
56  DeclarationWithType* retval;
57};
58
59class Pass2 : public Visitor
60{
61public:
62  virtual void visit( FunctionType *funType );
63
64private:
65};
66
67} // namespace
68
69void
70convertLvalue( std::list< Declaration* >& translationUnit )
71{
72  Pass1 p1;
73  Pass2 p2;
74  mutateAll( translationUnit, p1 );
75  acceptAll( translationUnit, p2 );
76}
77
78namespace {
79
80bool
81isLvalueRet( FunctionType *function )
82{
83  if ( ! function->get_returnVals().empty() ) {
84    return function->get_returnVals().front()->get_type()->get_isLvalue();
85  } else {
86    return false;
87  }
88}
89
90bool
91isIntrinsicApp( ApplicationExpr *appExpr )
92{
93  if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( appExpr->get_function() ) ) {
94    return varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic;
95  } else {
96    return false;
97  }
98}
99
100Pass1::Pass1()
101{
102}
103
104DeclarationWithType* 
105Pass1::mutate( FunctionDecl *funcDecl )
106{
107  if ( funcDecl->get_statements() ) {
108    DeclarationWithType* oldRetval = retval;
109    retval = 0;
110    if ( ! LinkageSpec::isBuiltin( funcDecl->get_linkage() ) && isLvalueRet( funcDecl->get_functionType() ) ) {
111      retval = funcDecl->get_functionType()->get_returnVals().front();
112    }
113    // fix expressions and return statements in this function
114    funcDecl->set_statements( funcDecl->get_statements()->acceptMutator( *this ) );
115    retval = oldRetval;
116  }
117  return funcDecl;
118}
119
120Expression* 
121Pass1::mutate( ApplicationExpr *appExpr )
122{
123  appExpr->get_function()->acceptMutator( *this );
124  mutateAll( appExpr->get_args(), *this );
125 
126  assert( ! appExpr->get_function()->get_results().empty() );
127
128  PointerType *pointer = dynamic_cast< PointerType* >( appExpr->get_function()->get_results().front() );
129  assert( pointer );
130  FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() );
131  assert( function );
132
133  std::string typeName;
134  if ( isLvalueRet( function ) && ! isIntrinsicApp( appExpr ) ) {
135    UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
136    deref->get_results().push_back( appExpr->get_results().front() );
137    appExpr->get_results().front() = new PointerType( Type::Qualifiers(), deref->get_results().front()->clone() );
138    deref->get_args().push_back( appExpr );
139    return deref;
140  } else {
141    return appExpr;
142  }
143}
144
145Statement* 
146Pass1::mutate(ReturnStmt *retStmt)
147{
148  if ( retval && retStmt->get_expr() ) {
149    assert( ! retStmt->get_expr()->get_results().empty() );
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    }
157    if ( retStmt->get_expr()->get_results().front()->get_isLvalue() ) {
158      retStmt->set_expr( new AddressExpr( retStmt->get_expr()->acceptMutator( *this ) ) );
159    } else {
160      throw SemanticError( "Attempt to return non-lvalue from an lvalue-qualified function" );
161    }
162  }
163  return retStmt;
164}
165
166void 
167Pass2::visit( FunctionType *funType )
168{
169  std::string typeName;
170  if ( isLvalueRet( funType ) ) {
171    DeclarationWithType *retParm = funType->get_returnVals().front();
172   
173    // make a new parameter that is a pointer to the type of the old return value
174    retParm->set_type( new PointerType( Type::Qualifiers(), retParm->get_type() ) );
175  }
176 
177  Visitor::visit( funType );
178}
179
180} // namespace
181
182} // namespace GenPoly
183// Local Variables: //
184// tab-width: 4 //
185// mode: c++ //
186// compile-command: "make install" //
187// End: //
Note: See TracBrowser for help on using the repository browser.