[0dd3a2f] | 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 | // |
---|
[89231bc] | 7 | // AddressExpr.cc -- |
---|
[0dd3a2f] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 23:54:44 2015 |
---|
[734ceb3e] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Thu Feb 28 13:13:38 2019 |
---|
| 13 | // Update Count : 10 |
---|
[0dd3a2f] | 14 | // |
---|
[51b7345] | 15 | |
---|
[ea6332d] | 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 |
---|
[51b7345] | 22 | |
---|
[c5e3208] | 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 | |
---|
| 32 | namespace { |
---|
| 33 | Type * addrType( Type * type ) { |
---|
| 34 | if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( type ) ) { |
---|
[d29fa5f] | 35 | return new ReferenceType( refType->get_qualifiers(), addrType( refType->base ) ); |
---|
[c5e3208] | 36 | } else { |
---|
| 37 | return new PointerType( Type::Qualifiers(), type->clone() ); |
---|
| 38 | } |
---|
| 39 | } |
---|
| 40 | } |
---|
| 41 | |
---|
[bf4b4cf] | 42 | AddressExpr::AddressExpr( Expression *arg ) : Expression(), arg( arg ) { |
---|
[d29fa5f] | 43 | if ( arg->result ) { |
---|
[2d80111] | 44 | if ( arg->get_lvalue() ) { |
---|
[c5e3208] | 45 | // lvalue, retains all layers of reference and gains a pointer inside the references |
---|
[d29fa5f] | 46 | set_result( addrType( arg->result ) ); |
---|
[ce8c12f] | 47 | } else { |
---|
[c5e3208] | 48 | // taking address of non-lvalue -- must be a reference, loses one layer of reference |
---|
[734ceb3e] | 49 | if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( arg->result ) ) { |
---|
| 50 | set_result( addrType( refType->base ) ); |
---|
| 51 | } else { |
---|
[135143b] | 52 | if(!arg->result->location.isSet()) arg->result->location = arg->location; |
---|
[734ceb3e] | 53 | SemanticError( arg->result, "Attempt to take address of non-lvalue expression: " ); |
---|
| 54 | } // if |
---|
[ce8c12f] | 55 | } |
---|
[906e24d] | 56 | } |
---|
[51b7345] | 57 | } |
---|
| 58 | |
---|
[0dd3a2f] | 59 | AddressExpr::AddressExpr( const AddressExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) { |
---|
[51b7345] | 60 | } |
---|
| 61 | |
---|
[0dd3a2f] | 62 | AddressExpr::~AddressExpr() { |
---|
[a08ba92] | 63 | delete arg; |
---|
[51b7345] | 64 | } |
---|
| 65 | |
---|
[50377a4] | 66 | void AddressExpr::print( std::ostream &os, Indenter indent ) const { |
---|
[89231bc] | 67 | os << "Address of:" << std::endl; |
---|
[a08ba92] | 68 | if ( arg ) { |
---|
[50377a4] | 69 | os << indent+1; |
---|
| 70 | arg->print( os, indent+1 ); |
---|
[a08ba92] | 71 | } // if |
---|
[51b7345] | 72 | } |
---|
| 73 | |
---|
[5809461] | 74 | LabelAddressExpr::LabelAddressExpr( const Label &arg ) : arg( arg ) { |
---|
| 75 | // label address always has type void * |
---|
| 76 | result = new PointerType( Type::Qualifiers(), new VoidType( Type::Qualifiers() ) ); |
---|
| 77 | } |
---|
| 78 | LabelAddressExpr::LabelAddressExpr( const LabelAddressExpr & other ) : Expression( other ), arg( other.arg ) {} |
---|
| 79 | LabelAddressExpr::~LabelAddressExpr() {} |
---|
| 80 | |
---|
[50377a4] | 81 | void LabelAddressExpr::print( std::ostream & os, Indenter ) const { |
---|
| 82 | os << "Address of label:" << arg; |
---|
[5809461] | 83 | } |
---|
| 84 | |
---|
[0dd3a2f] | 85 | // Local Variables: // |
---|
| 86 | // tab-width: 4 // |
---|
| 87 | // mode: c++ // |
---|
| 88 | // compile-command: "make install" // |
---|
| 89 | // End: // |
---|