gtk+-4.0: Update to 3.93.0+f4c1a404
[vala-gnome.git] / vala / valatypecheck.vala
blob4a49f5bac4172ab0cb87dd03e2ac5dbf4ad36516
1 /* valatypecheck.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>
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 {
33 get { return _expression; }
34 set {
35 _expression = value;
36 _expression.parent_node = this;
40 /**
41 * The type to be matched against.
43 public DataType type_reference {
44 get { return _data_type; }
45 set {
46 _data_type = value;
47 _data_type.parent_node = this;
51 Expression _expression;
52 private DataType _data_type;
54 /**
55 * Creates a new type check expression.
57 * @param expr an expression
58 * @param type a data type
59 * @param source reference to source code
60 * @return newly created type check expression
61 */
62 public TypeCheck (Expression expr, DataType type, SourceReference source) {
63 expression = expr;
64 type_reference = type;
65 source_reference = source;
68 public override void accept (CodeVisitor visitor) {
69 visitor.visit_type_check (this);
71 visitor.visit_expression (this);
74 public override void accept_children (CodeVisitor visitor) {
75 expression.accept (visitor);
77 type_reference.accept (visitor);
80 public override bool is_pure () {
81 return expression.is_pure ();
84 public override void replace_type (DataType old_type, DataType new_type) {
85 if (type_reference == old_type) {
86 type_reference = new_type;
90 public override void replace_expression (Expression old_node, Expression new_node) {
91 if (expression == old_node) {
92 expression = new_node;
96 public override bool check (CodeContext context) {
97 if (checked) {
98 return !error;
101 checked = true;
103 expression.check (context);
105 type_reference.check (context);
107 if (expression.value_type == null) {
108 Report.error (expression.source_reference, "invalid left operand");
109 error = true;
110 return false;
113 if (type_reference.data_type == null) {
114 /* if type resolving didn't succeed, skip this check */
115 error = true;
116 return false;
119 if (type_reference.get_type_arguments ().size > 0) {
120 Report.warning (_data_type.source_reference, "Type argument list has no effect");
123 value_type = context.analyzer.bool_type;
125 return !error;
128 public override void emit (CodeGenerator codegen) {
129 expression.emit (codegen);
131 codegen.visit_type_check (this);
133 codegen.visit_expression (this);