Release 0.7.8
[vala-lang.git] / vala / valatrystatement.vala
blob19be21c116c48388e74cf95cc2bc6d8c31aac58d
1 /* valatrystatement.vala
3 * Copyright (C) 2007-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 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 private Block _body;
53 private Block _finally_body;
54 private List<CatchClause> catch_clauses = new ArrayList<CatchClause> ();
56 /**
57 * Creates a new try statement.
59 * @param body body of the try statement
60 * @param finally_body body of the optional finally clause
61 * @param source_reference reference to source code
62 * @return newly created try statement
64 public TryStatement (Block body, Block? finally_body, SourceReference? source_reference = null) {
65 this.body = body;
66 this.finally_body = finally_body;
67 this.source_reference = source_reference;
70 /**
71 * Appends the specified clause to the list of catch clauses.
73 * @param clause a catch clause
75 public void add_catch_clause (CatchClause clause) {
76 clause.parent_node = this;
77 catch_clauses.add (clause);
80 /**
81 * Returns a copy of the list of catch clauses.
83 * @return list of catch clauses
85 public List<CatchClause> get_catch_clauses () {
86 return new ReadOnlyList<CatchClause> (catch_clauses);
89 public override void accept (CodeVisitor visitor) {
90 visitor.visit_try_statement (this);
93 public override void accept_children (CodeVisitor visitor) {
94 body.accept (visitor);
96 foreach (CatchClause clause in catch_clauses) {
97 clause.accept (visitor);
100 if (finally_body != null) {
101 finally_body.accept (visitor);
105 public override bool check (SemanticAnalyzer analyzer) {
106 if (checked) {
107 return !error;
110 checked = true;
112 body.check (analyzer);
114 var error_types = new ArrayList<DataType> ();
115 foreach (DataType body_error_type in body.get_error_types ()) {
116 error_types.add (body_error_type);
119 var handled_error_types = new ArrayList<DataType> ();
120 foreach (CatchClause clause in catch_clauses) {
121 foreach (DataType body_error_type in error_types) {
122 if (clause.error_type == null || body_error_type.compatible (clause.error_type)) {
123 handled_error_types.add (body_error_type);
126 foreach (DataType handled_error_type in handled_error_types) {
127 error_types.remove (handled_error_type);
129 handled_error_types.clear ();
131 clause.check (analyzer);
132 foreach (DataType body_error_type in clause.body.get_error_types ()) {
133 error_types.add (body_error_type);
137 if (finally_body != null) {
138 finally_body.check (analyzer);
139 foreach (DataType body_error_type in finally_body.get_error_types ()) {
140 error_types.add (body_error_type);
144 add_error_types (error_types);
146 return !error;