Line data Source code
1 : /* valathrowstatement.vala
2 : *
3 : * Copyright (C) 2007-2010 Jürg Billeter
4 : *
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.
9 :
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.
14 :
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
18 : *
19 : * Author:
20 : * Jürg Billeter <j@bitron.ch>
21 : */
22 :
23 :
24 : /**
25 : * Represents a throw statement in the source code.
26 : */
27 4654 : public class Vala.ThrowStatement : CodeNode, Statement {
28 : /**
29 : * The error expression to throw.
30 : */
31 : public Expression error_expression {
32 21227 : get {
33 21227 : return _error_expression;
34 : }
35 1596 : private set {
36 1596 : _error_expression = value;
37 1596 : if (_error_expression != null) {
38 1596 : _error_expression.parent_node = this;
39 : }
40 : }
41 : }
42 :
43 1596 : private Expression _error_expression;
44 :
45 : /**
46 : * Creates a new throw statement.
47 : *
48 : * @param error_expression the error expression
49 : * @param source_reference reference to source code
50 : * @return newly created throw statement
51 : */
52 4788 : public ThrowStatement (Expression error_expression, SourceReference? source_reference = null) {
53 1596 : this.source_reference = source_reference;
54 1596 : this.error_expression = error_expression;
55 : }
56 :
57 2508 : public override void accept (CodeVisitor visitor) {
58 2508 : visitor.visit_throw_statement (this);
59 : }
60 :
61 1487 : public override void accept_children (CodeVisitor visitor) {
62 1487 : if (error_expression != null) {
63 1487 : error_expression.accept (visitor);
64 :
65 1487 : visitor.visit_end_full_expression (error_expression);
66 : }
67 : }
68 :
69 0 : public override void replace_expression (Expression old_node, Expression new_node) {
70 0 : if (error_expression == old_node) {
71 0 : error_expression = new_node;
72 : }
73 : }
74 :
75 7089 : public override void get_error_types (Collection<DataType> collection, SourceReference? source_reference = null) {
76 3545 : if (error) {
77 : return;
78 : }
79 3544 : if (source_reference == null) {
80 3542 : source_reference = this.source_reference;
81 : }
82 3544 : var error_type = error_expression.value_type.copy ();
83 3544 : error_type.source_reference = source_reference;
84 3544 : collection.add (error_type);
85 : }
86 :
87 1482 : public override bool check (CodeContext context) {
88 1482 : if (checked) {
89 0 : return !error;
90 : }
91 :
92 1482 : checked = true;
93 :
94 1482 : if (context.profile == Profile.POSIX) {
95 0 : Report.error (source_reference, "`throws' is not supported in POSIX profile");
96 0 : error = true;
97 0 : return false;
98 : }
99 :
100 1482 : error_expression.target_type = new ErrorType (null, null, source_reference);
101 1482 : error_expression.target_type.value_owned = true;
102 :
103 1482 : if (error_expression != null) {
104 1482 : if (!error_expression.check (context)) {
105 1 : error = true;
106 1 : return false;
107 : }
108 :
109 1481 : if (error_expression.value_type == null) {
110 0 : Report.error (error_expression.source_reference, "invalid error expression");
111 0 : error = true;
112 0 : return false;
113 : }
114 :
115 1481 : if (context.profile == Profile.GOBJECT && !(error_expression.value_type is ErrorType)) {
116 0 : Report.error (error_expression.source_reference, "`%s' is not an error type", error_expression.value_type.to_string ());
117 0 : error = true;
118 0 : return false;
119 : }
120 : }
121 :
122 1481 : return !error;
123 : }
124 :
125 62 : public override void emit (CodeGenerator codegen) {
126 62 : if (error_expression != null) {
127 62 : error_expression.emit (codegen);
128 :
129 62 : codegen.visit_end_full_expression (error_expression);
130 : }
131 :
132 62 : codegen.visit_throw_statement (this);
133 : }
134 :
135 3063 : public override void get_defined_variables (Collection<Variable> collection) {
136 3063 : error_expression.get_defined_variables (collection);
137 : }
138 :
139 1021 : public override void get_used_variables (Collection<Variable> collection) {
140 1021 : error_expression.get_used_variables (collection);
141 : }
142 : }
|