gtk+-4.0: Update to 3.93.0+f4c1a404
[vala-gnome.git] / vala / valacatchclause.vala
blob84e4ba3402b3becc620c860c5b947876cd25c4e3
1 /* valacatchclause.vala
3 * Copyright (C) 2007-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 catch clause in a try statement in the source code.
27 public class Vala.CatchClause : CodeNode {
28 /**
29 * Specifies the error type.
31 public DataType? error_type {
32 get { return _data_type; }
33 set {
34 _data_type = value;
35 if (_data_type != null) {
36 _data_type.parent_node = this;
41 /**
42 * Specifies the error variable name.
44 public string? variable_name { get; set; }
46 /**
47 * Specifies the error handler body.
49 public Block body {
50 get { return _body; }
51 set {
52 _body = value;
53 _body.parent_node = this;
57 /**
58 * Specifies the declarator for the generated error variable.
60 public LocalVariable error_variable {
61 get { return _error_variable; }
62 set {
63 _error_variable = value;
64 _error_variable.parent_node = this;
68 /**
69 * Specifies the label used for this catch clause in the C code.
71 public string? clabel_name { get; set; }
73 private DataType _data_type;
75 private Block _body;
76 private LocalVariable _error_variable;
78 /**
79 * Creates a new catch
81 * @param error_type error type
82 * @param variable_name error variable name
83 * @param body error handler body
84 * @param source_reference reference to source code
85 * @return newly created catch clause
87 public CatchClause (DataType? error_type, string? variable_name, Block body, SourceReference? source_reference = null) {
88 this.error_type = error_type;
89 this.variable_name = variable_name;
90 this.body = body;
91 this.source_reference = source_reference;
94 public override void accept (CodeVisitor visitor) {
95 visitor.visit_catch_clause (this);
98 public override void accept_children (CodeVisitor visitor) {
99 if (error_type != null) {
100 error_type.accept (visitor);
103 body.accept (visitor);
106 public override void replace_type (DataType old_type, DataType new_type) {
107 if (error_type == old_type) {
108 error_type = new_type;
112 public override bool check (CodeContext context) {
113 if (checked) {
114 return !error;
117 checked = true;
119 if (error_type != null) {
120 if (!(error_type is ErrorType)) {
121 Report.error (source_reference, "clause must catch a valid error type, found `%s' instead".printf (error_type.to_string ()));
122 error = true;
125 if (variable_name != null) {
126 error_variable = new LocalVariable (error_type.copy (), variable_name);
128 body.scope.add (variable_name, error_variable);
129 body.add_local_variable (error_variable);
131 error_variable.checked = true;
133 } else {
134 // generic catch clause
135 error_type = new ErrorType (null, null, source_reference);
138 error_type.check (context);
139 body.check (context);
141 return !error;
144 public override void emit (CodeGenerator codegen) {
145 if (error_variable != null) {
146 error_variable.active = true;
149 codegen.visit_catch_clause (this);
152 public override void get_defined_variables (Collection<Variable> collection) {
153 if (error_variable != null) {
154 collection.add (error_variable);