1 /* valaaddressofexpression.vala
3 * Copyright (C) 2007-2010 Jürg Billeter
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * Jürg Billeter <j@bitron.ch>
26 * Represents an address-of expression in the source code, e.g. `&foo`.
28 public class Vala
.AddressofExpression
: Expression
{
30 * The variable whose address is to be computed.
32 public Expression inner
{
38 _inner
.parent_node
= this
;
42 private Expression _inner
;
45 * Creates a new address-of expression.
47 * @param inner variable whose address is to be computed
48 * @return newly created address-of expression
50 public AddressofExpression (Expression inner
, SourceReference? source_reference
= null) {
51 this
.source_reference
= source_reference
;
55 public override void accept (CodeVisitor visitor
) {
56 visitor
.visit_addressof_expression (this
);
58 visitor
.visit_expression (this
);
61 public override void accept_children (CodeVisitor visitor
) {
62 inner
.accept (visitor
);
65 public override void replace_expression (Expression old_node
, Expression new_node
) {
66 if (inner
== old_node
) {
71 public override bool is_pure () {
72 return inner
.is_pure ();
75 public override bool check (SemanticAnalyzer analyzer
) {
82 if (!inner
.check (analyzer
)) {
86 var ea
= inner as ElementAccess
;
87 if (inner is MemberAccess
&& inner
.symbol_reference is Variable
) {
88 // address of variable is always possible
89 } else if (ea
!= null &&
90 (ea
.container
.value_type is ArrayType
|| ea
.container
.value_type is PointerType
)) {
91 // address of element of regular array or pointer is always possible
94 Report
.error (source_reference
, "Address-of operator not supported for this expression");
98 if (inner
.value_type
.is_reference_type_or_type_parameter ()) {
99 value_type
= new
PointerType (new
PointerType (inner
.value_type
));
101 value_type
= new
PointerType (inner
.value_type
);
107 public override void emit (CodeGenerator codegen
) {
108 inner
.emit (codegen
);
110 codegen
.visit_addressof_expression (this
);
112 codegen
.visit_expression (this
);