Release 0.41.92
[vala-gnome.git] / vala / valatrystatement.vala
blob7e82228a5cb46d65b12d82d28a78861fe43caed4
1 /* valatrystatement.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>
23 using GLib;
25 /**
26 * Represents a try statement in the source code.
28 public class Vala.TryStatement : CodeNode, Statement {
29 /**
30 * Specifies the body of the try statement.
32 public Block body {
33 get { return _body; }
34 set {
35 _body = value;
36 _body.parent_node = this;
40 /**
41 * Specifies the body of the optional finally clause.
43 public Block? finally_body {
44 get { return _finally_body; }
45 set {
46 _finally_body = value;
47 if (_finally_body != null)
48 _finally_body.parent_node = this;
52 public bool after_try_block_reachable { get; set; default = true; }
54 private Block _body;
55 private Block _finally_body;
56 private List<CatchClause> catch_clauses = new ArrayList<CatchClause> ();
58 /**
59 * Creates a new try statement.
61 * @param body body of the try statement
62 * @param finally_body body of the optional finally clause
63 * @param source_reference reference to source code
64 * @return newly created try statement
66 public TryStatement (Block body, Block? finally_body, SourceReference? source_reference = null) {
67 this.body = body;
68 this.finally_body = finally_body;
69 this.source_reference = source_reference;
72 /**
73 * Appends the specified clause to the list of catch clauses.
75 * @param clause a catch clause
77 public void add_catch_clause (CatchClause clause) {
78 clause.parent_node = this;
79 catch_clauses.add (clause);
82 /**
83 * Returns a copy of the list of catch clauses.
85 * @return list of catch clauses
87 public List<CatchClause> get_catch_clauses () {
88 return catch_clauses;
91 public override void accept (CodeVisitor visitor) {
92 visitor.visit_try_statement (this);
95 public override void accept_children (CodeVisitor visitor) {
96 body.accept (visitor);
98 foreach (CatchClause clause in catch_clauses) {
99 clause.accept (visitor);
102 if (finally_body != null) {
103 finally_body.accept (visitor);
107 public override bool check (CodeContext context) {
108 if (checked) {
109 return !error;
112 checked = true;
114 if (context.profile == Profile.POSIX) {
115 Report.error (source_reference, "`try' is not supported in POSIX profile");
116 error = true;
117 return false;
120 body.check (context);
122 var error_types = new ArrayList<DataType> ();
123 foreach (DataType body_error_type in body.get_error_types ()) {
124 error_types.add (body_error_type);
127 var handled_error_types = new ArrayList<DataType> ();
128 foreach (CatchClause clause in catch_clauses) {
129 foreach (DataType body_error_type in error_types) {
130 if (clause.error_type == null || body_error_type.compatible (clause.error_type)) {
131 handled_error_types.add (body_error_type);
134 foreach (DataType handled_error_type in handled_error_types) {
135 error_types.remove (handled_error_type);
137 handled_error_types.clear ();
139 clause.check (context);
140 foreach (DataType body_error_type in clause.body.get_error_types ()) {
141 error_types.add (body_error_type);
145 if (finally_body != null) {
146 finally_body.check (context);
147 foreach (DataType body_error_type in finally_body.get_error_types ()) {
148 error_types.add (body_error_type);
152 add_error_types (error_types);
154 return !error;
157 public override void emit (CodeGenerator codegen) {
158 codegen.visit_try_statement (this);