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
20 * Jürg Billeter <j@bitron.ch>
26 * Represents a try statement in the source code.
28 public class Vala
.TryStatement
: CodeNode
, Statement
{
30 * Specifies the body of the try statement.
36 _body
.parent_node
= this
;
41 * Specifies the body of the optional finally clause.
43 public Block? finally_body
{
44 get { return _finally_body
; }
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; }
55 private Block _finally_body
;
56 private List
<CatchClause
> catch_clauses
= new ArrayList
<CatchClause
> ();
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) {
68 this
.finally_body
= finally_body
;
69 this
.source_reference
= source_reference
;
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
);
83 * Returns a copy of the list of catch clauses.
85 * @return list of catch clauses
87 public List
<CatchClause
> get_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
) {
114 if (context
.profile
== Profile
.POSIX
) {
115 Report
.error (source_reference
, "`try' is not supported in POSIX profile");
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
);
157 public override void emit (CodeGenerator codegen
) {
158 codegen
.visit_try_statement (this
);