source: src/SynTree/AddressExpr.cc @ 8c63bb4

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 8c63bb4 was 734ceb3e, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

print error message rather than fail when taking address of non-lvalue expression [fixes #123]

  • Property mode set to 100644
File size: 2.8 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// AddressExpr.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Sun May 17 23:54:44 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Feb 28 13:13:38 2019
13// Update Count     : 10
14//
15
16#include <ostream>           // for ostream, operator<<, basic_ostream, endl
17#include <string>            // for operator<<, string
18
19#include "Common/utility.h"  // for maybeClone
20#include "Expression.h"      // for AddressExpr, Expression
21#include "Type.h"            // for PointerType, Type, Type::Qualifiers
22
23// Address expressions are typed based on the following inference rules:
24//    E : lvalue T  &..& (n references)
25//   &E :        T *&..& (n references)
26//
27//    E : T  &..&        (m references)
28//   &E : T *&..&        (m-1 references)
29//
30// That is, lvalues becomes
31
32namespace {
33        Type * addrType( Type * type ) {
34                if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( type ) ) {
35                        return new ReferenceType( refType->get_qualifiers(), addrType( refType->base ) );
36                } else {
37                        return new PointerType( Type::Qualifiers(), type->clone() );
38                }
39        }
40}
41
42AddressExpr::AddressExpr( Expression *arg ) : Expression(), arg( arg ) {
43        if ( arg->result ) {
44                if ( arg->result->get_lvalue() ) {
45                        // lvalue, retains all layers of reference and gains a pointer inside the references
46                        set_result( addrType( arg->result ) );
47                } else {
48                        // taking address of non-lvalue -- must be a reference, loses one layer of reference
49                        if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( arg->result ) ) {
50                                set_result( addrType( refType->base ) );
51                        } else {
52                                SemanticError( arg->result, "Attempt to take address of non-lvalue expression: " );
53                        } // if
54                }
55                // result of & is never an lvalue
56                get_result()->set_lvalue( false );
57        }
58}
59
60AddressExpr::AddressExpr( const AddressExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) {
61}
62
63AddressExpr::~AddressExpr() {
64        delete arg;
65}
66
67void AddressExpr::print( std::ostream &os, Indenter indent ) const {
68        os << "Address of:" << std::endl;
69        if ( arg ) {
70                os << indent+1;
71                arg->print( os, indent+1 );
72        } // if
73}
74
75LabelAddressExpr::LabelAddressExpr( const Label &arg ) : arg( arg ) {
76        // label address always has type void *
77        result = new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) );
78}
79LabelAddressExpr::LabelAddressExpr( const LabelAddressExpr & other ) : Expression( other ), arg( other.arg ) {}
80LabelAddressExpr::~LabelAddressExpr() {}
81
82void LabelAddressExpr::print( std::ostream & os, Indenter ) const {
83        os << "Address of label:" << arg;
84}
85
86// Local Variables: //
87// tab-width: 4 //
88// mode: c++ //
89// compile-command: "make install" //
90// End: //
Note: See TracBrowser for help on using the repository browser.