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
20 * Jürg Billeter <j@bitron.ch>
25 * Represents a catch clause in a try statement in the source code.
27 public class Vala
.CatchClause
: CodeNode
{
29 * Specifies the error type.
31 public DataType? error_type
{
32 get { return _data_type
; }
35 if (_data_type
!= null) {
36 _data_type
.parent_node
= this
;
42 * Specifies the error variable name.
44 public string? variable_name
{ get; set; }
47 * Specifies the error handler body.
53 _body
.parent_node
= this
;
58 * Specifies the declarator for the generated error variable.
60 public LocalVariable error_variable
{
61 get { return _error_variable
; }
63 _error_variable
= value
;
64 _error_variable
.parent_node
= this
;
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
;
76 private LocalVariable _error_variable
;
81 * @param type_reference 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
;
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
) {
119 if (error_type
!= null) {
120 error_variable
= new
LocalVariable (error_type
.copy (), variable_name
);
122 body
.scope
.add (variable_name
, error_variable
);
123 body
.add_local_variable (error_variable
);
125 error_variable
.checked
= true;
127 // generic catch clause
128 if (context
.profile
== Profile
.GOBJECT
) {
129 error_type
= new
ErrorType (null, null, source_reference
);
131 error_type
= context
.analyzer
.error_type
;
135 error_type
.check (context
);
137 body
.check (context
);
142 public override void emit (CodeGenerator codegen
) {
143 if (error_variable
!= null) {
144 error_variable
.active
= true;
147 codegen
.visit_catch_clause (this
);
150 public override void get_defined_variables (Collection
<LocalVariable
> collection
) {
151 if (error_variable
!= null) {
152 collection
.add (error_variable
);