1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/* valacreationmethod.vala
*
* Copyright (C) 2007-2010 Jürg Billeter
* Copyright (C) 2007-2008 Raffaele Sandrini
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Raffaele Sandrini <raffaele@sandrini.ch>
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
/**
* Represents a type creation method.
*/
public class Vala.CreationMethod : Method {
/**
* Specifies the name of the type this creation method belongs to.
*/
public string class_name { get; set; }
/**
* Specifies a custom C return type for that creation method.
* Only the idl parser and the interface writer should use this.
* FIXME: remove this as soon the compiler has a decent attribute
* handling.
*/
public string? custom_return_type_cname { get; set; }
/**
* Specifies whether this constructor chains up to a base
* constructor or a different constructor of the same class.
*/
public bool chain_up { get; set; }
/**
* Creates a new method.
*
* @param name method name
* @param source_reference reference to source code
* @return newly created method
*/
public CreationMethod (string? class_name, string? name, SourceReference? source_reference = null, Comment? comment = null) {
base (name, new VoidType (), source_reference, comment);
this.class_name = class_name;
carray_length_parameter_position = -3;
cdelegate_target_parameter_position = -3;
}
public override void accept (CodeVisitor visitor) {
visitor.visit_creation_method (this);
}
public override void accept_children (CodeVisitor visitor) {
foreach (FormalParameter param in get_parameters()) {
param.accept (visitor);
}
foreach (DataType error_type in get_error_types ()) {
error_type.accept (visitor);
}
foreach (Expression precondition in get_preconditions ()) {
precondition.accept (visitor);
}
foreach (Expression postcondition in get_postconditions ()) {
postcondition.accept (visitor);
}
if (body != null) {
body.accept (visitor);
}
}
public override string get_default_cname () {
var parent = parent_symbol as TypeSymbol;
string infix = "new";
var st = parent as Struct;
if (st != null) {
if (CodeContext.get ().profile == Profile.DOVA) {
if (st.is_boolean_type () || st.is_integer_type () || st.is_floating_type ()) {
// don't use any infix for basic types
if (name == ".new") {
return parent.get_lower_case_cname ();
} else {
return "%s%s".printf (parent.get_lower_case_cprefix (), name);
}
}
}
infix = "init";
}
if (name == ".new") {
return "%s%s".printf (parent.get_lower_case_cprefix (), infix);
} else {
return "%s%s_%s".printf (parent.get_lower_case_cprefix (), infix, name);
}
}
public override string get_real_cname () {
var ccode_attribute = get_attribute ("CCode");
if (ccode_attribute != null && ccode_attribute.has_argument ("construct_function")) {
return ccode_attribute.get_string ("construct_function");
}
return get_default_construct_function ();
}
public string get_default_construct_function () {
var parent = parent_symbol as Class;
if (parent == null || parent.is_compact) {
return get_cname ();
}
string infix = "construct";
if (CodeContext.get ().profile == Profile.DOVA) {
infix = "init";
}
if (name == ".new") {
return "%s%s".printf (parent.get_lower_case_cprefix (), infix);
} else {
return "%s%s_%s".printf (parent.get_lower_case_cprefix (), infix, name);
}
}
public override bool check (SemanticAnalyzer analyzer) {
if (checked) {
return !error;
}
checked = true;
process_attributes ();
if (class_name != null && class_name != parent_symbol.name) {
// class_name is null for constructors generated by GIdlParser
Report.error (source_reference, "missing return type in method `%s.%s´".printf (analyzer.current_symbol.get_full_name (), class_name));
error = true;
return false;
}
var old_source_file = analyzer.current_source_file;
var old_symbol = analyzer.current_symbol;
if (source_reference != null) {
analyzer.current_source_file = source_reference.file;
}
analyzer.current_symbol = this;
foreach (FormalParameter param in get_parameters()) {
param.check (analyzer);
}
foreach (DataType error_type in get_error_types ()) {
error_type.check (analyzer);
}
foreach (Expression precondition in get_preconditions ()) {
precondition.check (analyzer);
}
foreach (Expression postcondition in get_postconditions ()) {
postcondition.check (analyzer);
}
if (body != null) {
body.check (analyzer);
var cl = parent_symbol as Class;
// ensure we chain up to base constructor
if (!chain_up && cl != null && cl.base_class != null) {
if (analyzer.context.profile == Profile.GOBJECT
&& cl.base_class.default_construction_method != null
&& !cl.base_class.default_construction_method.has_construct_function) {
// chain up impossible
} else if (cl.base_class.default_construction_method == null
|| cl.base_class.default_construction_method.access == SymbolAccessibility.PRIVATE) {
Report.error (source_reference, "unable to chain up to private base constructor");
} else if (cl.base_class.default_construction_method.get_required_arguments () > 0) {
Report.error (source_reference, "unable to chain up to base constructor requiring arguments");
} else {
var old_insert_block = analyzer.insert_block;
analyzer.current_symbol = body;
analyzer.insert_block = body;
var stmt = new ExpressionStatement (new MethodCall (new BaseAccess (source_reference), source_reference), source_reference);
body.insert_statement (0, stmt);
stmt.check (analyzer);
analyzer.current_symbol = this;
analyzer.insert_block = old_insert_block;
}
}
}
analyzer.current_source_file = old_source_file;
analyzer.current_symbol = old_symbol;
if (is_abstract || is_virtual || overrides) {
Report.error (source_reference, "The creation method `%s' cannot be marked as override, virtual, or abstract".printf (get_full_name ()));
return false;
}
// check that all errors that can be thrown in the method body are declared
if (body != null) {
foreach (DataType body_error_type in body.get_error_types ()) {
bool can_propagate_error = false;
foreach (DataType method_error_type in get_error_types ()) {
if (body_error_type.compatible (method_error_type)) {
can_propagate_error = true;
}
}
if (!can_propagate_error && !((ErrorType) body_error_type).dynamic_error) {
Report.warning (body_error_type.source_reference, "unhandled error `%s'".printf (body_error_type.to_string()));
}
}
}
return !error;
}
}
|