Do not free values returned via g_object_get prematurely, require
[vala-lang.git] / vala / valacatchclause.vala
blob039ea6bc0da8a5d6856699d85d6433ce89ccdf7c
1 /* valacatchvala
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 Gee;
25 /**
26 * Represents a catch clause in a try statement in the source code.
28 public class Vala.CatchClause : CodeNode {
29 /**
30 * Specifies the error type.
32 public DataType? error_type {
33 get { return _data_type; }
34 set {
35 _data_type = value;
36 if (_data_type != null) {
37 _data_type.parent_node = this;
42 /**
43 * Specifies the error variable name.
45 public string? variable_name { get; set; }
47 /**
48 * Specifies the error handler body.
50 public Block body { get; set; }
52 /**
53 * Specifies the declarator for the generated error variable.
55 public LocalVariable error_variable { get; set; }
57 /**
58 * Specifies the label used for this catch clause in the C code.
60 public string? clabel_name { get; set; }
62 private DataType _data_type;
64 /**
65 * Creates a new catch
67 * @param type_reference error type
68 * @param variable_name error variable name
69 * @param body error handler body
70 * @param source_reference reference to source code
71 * @return newly created catch clause
73 public CatchClause (DataType? error_type, string? variable_name, Block body, SourceReference? source_reference = null) {
74 this.error_type = error_type;
75 this.variable_name = variable_name;
76 this.body = body;
77 this.source_reference = source_reference;
80 public override void accept (CodeVisitor visitor) {
81 visitor.visit_catch_clause (this);
84 public override void accept_children (CodeVisitor visitor) {
85 if (error_type != null) {
86 error_type.accept (visitor);
89 body.accept (visitor);
92 public override void replace_type (DataType old_type, DataType new_type) {
93 if (error_type == old_type) {
94 error_type = new_type;
98 public override bool check (SemanticAnalyzer analyzer) {
99 if (checked) {
100 return !error;
103 checked = true;
105 if (error_type != null) {
106 error_variable = new LocalVariable (error_type.copy (), variable_name);
108 body.scope.add (variable_name, error_variable);
109 body.add_local_variable (error_variable);
111 error_variable.checked = true;
112 } else {
113 error_type = new ErrorType (null, null, source_reference);
116 error_type.check (analyzer);
118 analyzer.current_source_file.add_type_dependency (error_type, SourceFileDependencyType.SOURCE);
120 body.check (analyzer);
122 return !error;
125 public override void get_defined_variables (Collection<LocalVariable> collection) {
126 if (error_variable != null) {
127 collection.add (error_variable);