linux: Add psiginfo(3)
[vala-gnome.git] / vala / valacastexpression.vala
blobecf093dc571728c0e09405e43443a19a53e5a689
1 /* valacastexpression.vala
3 * Copyright (C) 2006-2011 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
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
24 /**
25 * Represents a type cast in the source code.
27 public class Vala.CastExpression : Expression {
28 /**
29 * The expression to be cast.
31 public Expression inner {
32 get {
33 return _inner;
35 set {
36 _inner = value;
37 _inner.parent_node = this;
41 /**
42 * The target type.
44 public DataType type_reference {
45 get { return _data_type; }
46 set {
47 _data_type = value;
48 _data_type.parent_node = this;
52 /**
53 * Checked casts return NULL instead of raising an error.
55 public bool is_silent_cast { get; private set; }
57 public bool is_non_null_cast { get; private set; }
59 private Expression _inner;
61 private DataType _data_type;
63 /**
64 * Creates a new cast expression.
66 * @param inner expression to be cast
67 * @param type_reference target type
68 * @return newly created cast expression
70 public CastExpression (Expression inner, DataType type_reference, SourceReference source_reference) {
71 this.type_reference = type_reference;
72 this.source_reference = source_reference;
73 this.is_silent_cast = false;
74 this.is_non_null_cast = false;
75 this.inner = inner;
78 public CastExpression.silent (Expression inner, DataType type_reference, SourceReference source_reference) {
79 this.type_reference = type_reference;
80 this.source_reference = source_reference;
81 this.is_silent_cast = true;
82 this.is_non_null_cast = false;
83 this.inner = inner;
86 public CastExpression.non_null (Expression inner, SourceReference source_reference) {
87 this.inner = inner;
88 this.is_non_null_cast = true;
89 this.source_reference = source_reference;
92 public override void accept (CodeVisitor visitor) {
93 visitor.visit_cast_expression (this);
95 visitor.visit_expression (this);
98 public override void accept_children (CodeVisitor visitor) {
99 inner.accept (visitor);
100 if (!is_non_null_cast) {
101 type_reference.accept (visitor);
105 public override void replace_expression (Expression old_node, Expression new_node) {
106 if (inner == old_node) {
107 inner = new_node;
111 public override bool is_pure () {
112 return inner.is_pure ();
115 public override bool is_accessible (Symbol sym) {
116 return inner.is_accessible (sym);
119 public override void replace_type (DataType old_type, DataType new_type) {
120 if (type_reference == old_type) {
121 type_reference = new_type;
125 public override bool check (CodeContext context) {
126 if (checked) {
127 return !error;
130 checked = true;
132 if (!inner.check (context)) {
133 error = true;
134 return false;
137 if (inner.value_type == null) {
138 Report.error (source_reference, "Invalid cast expression");
139 error = true;
140 return false;
143 if (is_non_null_cast) {
144 // (!) non-null cast
145 type_reference = inner.value_type.copy ();
146 type_reference.nullable = false;
149 type_reference.check (context);
151 // FIXME: check whether cast is allowed
153 if (type_reference is DelegateType && inner.value_type is MethodType) {
154 if (target_type != null) {
155 inner.value_type.value_owned = target_type.value_owned;
156 } else {
157 inner.value_type.value_owned = true;
161 value_type = type_reference;
162 value_type.value_owned = inner.value_type.value_owned;
164 if (is_silent_cast) {
165 value_type.nullable = true;
168 if (is_gvariant (context, inner.value_type) && !is_gvariant (context, value_type)) {
169 // GVariant unboxing returns owned value
170 value_type.value_owned = true;
173 inner.target_type = inner.value_type.copy ();
175 return !error;
178 bool is_gvariant (CodeContext context, DataType type) {
179 return type.data_type != null && type.data_type.is_subtype_of (context.analyzer.gvariant_type.data_type);
182 public override void emit (CodeGenerator codegen) {
183 inner.emit (codegen);
185 codegen.visit_cast_expression (this);
187 codegen.visit_expression (this);
190 public override void get_defined_variables (Collection<Variable> collection) {
191 inner.get_defined_variables (collection);
194 public override void get_used_variables (Collection<Variable> collection) {
195 inner.get_used_variables (collection);
198 public override bool is_constant () {
199 return inner.is_constant ();