Warn when using result variable with incompatible type to prepare possible
[vala-lang.git] / vala / valatypecheck.vala
blob2dee142f174a086a9c4134d6beb2270864b8c2a5
1 /* valatypecheck.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 GLib;
25 /**
26 * Represents a type check (`is') expression in the source code.
28 public class Vala.TypeCheck : Expression {
29 /**
30 * The expression to be checked.
32 public Expression expression { get; set; }
34 /**
35 * The type to be matched against.
37 public DataType type_reference {
38 get { return _data_type; }
39 set {
40 _data_type = value;
41 _data_type.parent_node = this;
45 private DataType _data_type;
47 /**
48 * Creates a new type check expression.
50 * @param expr an expression
51 * @param type a data type
52 * @param source reference to source code
53 * @return newly created type check expression
54 */
55 public TypeCheck (Expression expr, DataType type, SourceReference source) {
56 expression = expr;
57 type_reference = type;
58 source_reference = source;
61 public override void accept (CodeVisitor visitor) {
62 expression.accept (visitor);
64 type_reference.accept (visitor);
66 visitor.visit_type_check (this);
68 visitor.visit_expression (this);
71 public override bool is_pure () {
72 return expression.is_pure ();
75 public override void replace_type (DataType old_type, DataType new_type) {
76 if (type_reference == old_type) {
77 type_reference = new_type;
81 public override bool check (SemanticAnalyzer analyzer) {
82 if (checked) {
83 return !error;
86 checked = true;
88 expression.check (analyzer);
90 type_reference.check (analyzer);
92 if (type_reference.data_type == null) {
93 /* if type resolving didn't succeed, skip this check */
94 error = true;
95 return false;
98 analyzer.current_source_file.add_type_dependency (type_reference, SourceFileDependencyType.SOURCE);
100 value_type = analyzer.bool_type;
102 return !error;