glib-2.0: add g_strsignal binding
[vala-lang.git] / vala / valacastexpression.vala
blob7018992bb6db9fc4fa8d224bb22e1bd03ad84d39
1 /* valacastexpression.vala
3 * Copyright (C) 2006-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
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; set; }
57 public bool is_non_null_cast { get; 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 target type
68 * @return newly created cast expression
70 public CastExpression (Expression inner, DataType type_reference, SourceReference source_reference, bool is_silent_cast) {
71 this.type_reference = type_reference;
72 this.source_reference = source_reference;
73 this.is_silent_cast = is_silent_cast;
74 this.inner = inner;
77 public CastExpression.non_null (Expression inner, SourceReference source_reference) {
78 this.inner = inner;
79 this.is_non_null_cast = true;
80 this.source_reference = source_reference;
83 public override void accept (CodeVisitor visitor) {
84 visitor.visit_cast_expression (this);
86 visitor.visit_expression (this);
89 public override void accept_children (CodeVisitor visitor) {
90 inner.accept (visitor);
91 if (!is_non_null_cast) {
92 type_reference.accept (visitor);
96 public override void replace_expression (Expression old_node, Expression new_node) {
97 if (inner == old_node) {
98 inner = new_node;
102 public override bool is_pure () {
103 return inner.is_pure ();
106 public override void replace_type (DataType old_type, DataType new_type) {
107 if (type_reference == old_type) {
108 type_reference = new_type;
112 public override bool check (SemanticAnalyzer analyzer) {
113 if (checked) {
114 return !error;
117 checked = true;
119 if (!inner.check (analyzer)) {
120 error = true;
121 return false;
124 if (inner.value_type == null) {
125 Report.error (source_reference, "Invalid cast expression");
126 error = true;
127 return false;
130 if (is_non_null_cast) {
131 // (!) non-null cast
132 type_reference = inner.value_type.copy ();
133 type_reference.nullable = false;
136 type_reference.check (analyzer);
138 // FIXME: check whether cast is allowed
140 value_type = type_reference;
141 value_type.value_owned = inner.value_type.value_owned;
143 if (is_silent_cast) {
144 value_type.nullable = true;
147 inner.target_type = inner.value_type.copy ();
149 return !error;
152 public override void emit (CodeGenerator codegen) {
153 inner.emit (codegen);
155 codegen.visit_cast_expression (this);
157 codegen.visit_expression (this);
160 public override void get_defined_variables (Collection<LocalVariable> collection) {
161 inner.get_defined_variables (collection);
164 public override void get_used_variables (Collection<LocalVariable> collection) {
165 inner.get_used_variables (collection);