valaaddressofexpression.vala 3.09 KB
Newer Older
1 2
/* valaaddressofexpression.vala
 *
3
 * Copyright (C) 2007-2010  Jürg Billeter
4 5 6 7
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Jürg Billeter <j@bitron.ch>
 */

using GLib;

/**
Didier 'Ptitjes's avatar
Didier 'Ptitjes committed
26
 * Represents an address-of expression in the source code, e.g. `&foo`.
27 28 29 30 31
 */
public class Vala.AddressofExpression : Expression {
	/**
	 * The variable whose address is to be computed.
	 */
32
	public Expression inner {
33 34 35
		get {
			return _inner;
		}
36
		set {
37 38 39 40
			_inner = value;
			_inner.parent_node = this;
		}
	}
41

42
	private Expression _inner;
43 44 45 46 47 48 49

	/**
	 * Creates a new address-of expression.
	 *
	 * @param inner variable whose address is to be computed
	 * @return      newly created address-of expression
	 */
50
	public AddressofExpression (Expression inner, SourceReference? source_reference = null) {
51 52
		this.source_reference = source_reference;
		this.inner = inner;
53
	}
54

55
	public override void accept (CodeVisitor visitor) {
56
		visitor.visit_addressof_expression (this);
57 58

		visitor.visit_expression (this);
59 60
	}

61 62 63 64
	public override void accept_children (CodeVisitor visitor) {
		inner.accept (visitor);
	}

65
	public override void replace_expression (Expression old_node, Expression new_node) {
66
		if (inner == old_node) {
67
			inner = new_node;
68 69
		}
	}
70 71 72 73

	public override bool is_pure () {
		return inner.is_pure ();
	}
74

75 76 77 78
	public override bool is_accessible (Symbol sym) {
		return inner.is_accessible (sym);
	}

79
	public override bool check (CodeContext context) {
80 81 82 83 84 85
		if (checked) {
			return !error;
		}

		checked = true;

86 87
		inner.lvalue = true;

88
		if (!inner.check (context)) {
89
			error = true;
90 91
			return false;
		}
92
		var ea = inner as ElementAccess;
93 94
		if (inner is MemberAccess && inner.symbol_reference is Variable) {
			// address of variable is always possible
95 96 97
		} else if (ea != null &&
		           (ea.container.value_type is ArrayType || ea.container.value_type is PointerType)) {
			// address of element of regular array or pointer is always possible
98
		} else {
99 100 101 102 103 104 105 106 107 108 109 110 111
			error = true;
			Report.error (source_reference, "Address-of operator not supported for this expression");
			return false;
		}

		if (inner.value_type.is_reference_type_or_type_parameter ()) {
			value_type = new PointerType (new PointerType (inner.value_type));
		} else {
			value_type = new PointerType (inner.value_type);
		}

		return !error;
	}
112 113 114 115 116 117 118 119

	public override void emit (CodeGenerator codegen) {
		inner.emit (codegen);

		codegen.visit_addressof_expression (this);

		codegen.visit_expression (this);
	}
120
}