Do not free values returned via g_object_get prematurely, require
[vala-lang.git] / vala / valacastexpression.vala
blob4f94832ce54ef6dd59f3e0e7dda7e78e2bf8bc47
1 /* valacastexpression.vala
3 * Copyright (C) 2006-2008 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>
23 using Gee;
25 /**
26 * Represents a type cast in the source code.
28 public class Vala.CastExpression : Expression {
29 /**
30 * The expression to be cast.
32 public Expression inner {
33 get {
34 return _inner;
36 set {
37 _inner = value;
38 _inner.parent_node = this;
42 /**
43 * The target type.
45 public DataType type_reference {
46 get { return _data_type; }
47 set {
48 _data_type = value;
49 _data_type.parent_node = this;
53 /**
54 * Checked casts return NULL instead of raising an error.
56 public bool is_silent_cast { get; set; }
58 private Expression _inner;
60 private DataType _data_type;
62 /**
63 * Creates a new cast expression.
65 * @param inner expression to be cast
66 * @param type target type
67 * @return newly created cast expression
69 public CastExpression (Expression inner, DataType type_reference, SourceReference source_reference, bool is_silent_cast) {
70 this.type_reference = type_reference;
71 this.source_reference = source_reference;
72 this.is_silent_cast = is_silent_cast;
73 this.inner = inner;
76 public override void accept (CodeVisitor visitor) {
77 inner.accept (visitor);
78 type_reference.accept (visitor);
80 visitor.visit_cast_expression (this);
82 visitor.visit_expression (this);
85 public override void replace_expression (Expression old_node, Expression new_node) {
86 if (inner == old_node) {
87 inner = new_node;
91 public override bool is_pure () {
92 return inner.is_pure ();
95 public override void replace_type (DataType old_type, DataType new_type) {
96 if (type_reference == old_type) {
97 type_reference = new_type;
101 public override bool check (SemanticAnalyzer analyzer) {
102 if (checked) {
103 return !error;
106 checked = true;
108 if (!inner.check (analyzer)) {
109 error = true;
110 return false;
113 type_reference.check (analyzer);
115 // FIXME: check whether cast is allowed
117 analyzer.current_source_file.add_type_dependency (type_reference, SourceFileDependencyType.SOURCE);
119 value_type = type_reference;
120 value_type.value_owned = inner.value_type.value_owned;
122 inner.target_type = inner.value_type.copy ();
124 return !error;
127 public override void get_defined_variables (Collection<LocalVariable> collection) {
128 inner.get_defined_variables (collection);
131 public override void get_used_variables (Collection<LocalVariable> collection) {
132 inner.get_used_variables (collection);