glib-2.0: Add float.parse/try_parse()
[vala-gnome.git] / vala / valacatchclause.vala
blobbb8d73cb2035926cc574bd980ee7a4961008b919
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 (context.profile == Profile.POSIX) {
120 Report.error (source_reference, "`catch' is not supported in POSIX profile");
121 error = true;
122 return false;
125 if (error_type != null) {
126 if (!(error_type is ErrorType)) {
127 Report.error (source_reference, "clause must catch a valid error type, found `%s' instead".printf (error_type.to_string ()));
128 error = true;
131 if (variable_name != null) {
132 error_variable = new LocalVariable (error_type.copy (), variable_name);
134 body.scope.add (variable_name, error_variable);
135 body.add_local_variable (error_variable);
137 error_variable.checked = true;
139 } else {
140 // generic catch clause
141 error_type = new ErrorType (null, null, source_reference);
144 error_type.check (context);
145 body.check (context);
147 return !error;
150 public override void emit (CodeGenerator codegen) {
151 if (error_variable != null) {
152 error_variable.active = true;
155 codegen.visit_catch_clause (this);
158 public override void get_defined_variables (Collection<Variable> collection) {
159 if (error_variable != null) {
160 collection.add (error_variable);