tests: Add "while (false)" test to increase coverage
[vala-gnome.git] / vala / valadatatype.vala
blobf3f9bf92a4fea649d8628a93cae7d23c792cbe01
1 /* valadatatype.vala
3 * Copyright (C) 2006-2010 Jürg Billeter
4 * Copyright (C) 2006-2008 Raffaele Sandrini
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * Author:
21 * Jürg Billeter <j@bitron.ch>
22 * Raffaele Sandrini <raffaele@sandrini.ch>
25 using GLib;
27 /**
28 * A reference to a data type. This is used to specify static types of
29 * expressions.
31 public abstract class Vala.DataType : CodeNode {
32 /**
33 * Specifies that the expression or variable owns the value.
35 public bool value_owned { get; set; }
37 /**
38 * Specifies that the expression may be null.
40 public bool nullable { get; set; }
42 /**
43 * The referred data type.
45 public weak TypeSymbol data_type { get; set; }
47 /**
48 * The referred generic type parameter.
50 public TypeParameter type_parameter { get; set; }
52 /**
53 * Specifies that the expression transfers a floating reference.
55 public bool floating_reference { get; set; }
57 /**
58 * Specifies that the type supports dynamic lookup.
60 public bool is_dynamic { get; set; }
62 private List<DataType> type_argument_list;
63 private static List<DataType> _empty_type_list;
65 /**
66 * Appends the specified type as generic type argument.
68 * @param arg a type reference
70 public void add_type_argument (DataType arg) {
71 if (type_argument_list == null) {
72 type_argument_list = new ArrayList<DataType> ();
74 type_argument_list.add (arg);
75 arg.parent_node = this;
78 /**
79 * Returns a copy of the list of generic type arguments.
81 * @return type argument list
83 public List<DataType> get_type_arguments () {
84 if (type_argument_list != null) {
85 return type_argument_list;
87 if (_empty_type_list == null) {
88 _empty_type_list = new ArrayList<DataType> ();
90 return _empty_type_list;
93 public bool has_type_arguments () {
94 if (type_argument_list == null) {
95 return false;
98 return type_argument_list.size > 0;
102 * Removes all generic type arguments.
104 public void remove_all_type_arguments () {
105 type_argument_list = null;
108 public override void accept (CodeVisitor visitor) {
109 visitor.visit_data_type (this);
112 public override void accept_children (CodeVisitor visitor) {
113 if (type_argument_list != null && type_argument_list.size > 0) {
114 foreach (DataType type_arg in type_argument_list) {
115 type_arg.accept (visitor);
120 public override string to_string () {
121 return to_qualified_string (null);
124 public virtual string to_qualified_string (Scope? scope = null) {
125 // logic temporarily duplicated in DelegateType class
127 string s;
129 if (data_type != null) {
130 Symbol global_symbol = data_type;
131 while (global_symbol.parent_symbol.name != null) {
132 global_symbol = global_symbol.parent_symbol;
135 Symbol sym = null;
136 Scope parent_scope = scope;
137 while (sym == null && parent_scope != null) {
138 sym = parent_scope.lookup (global_symbol.name);
139 parent_scope = parent_scope.parent_scope;
142 if (sym != null && global_symbol != sym) {
143 s = "global::" + data_type.get_full_name ();;
144 } else {
145 s = data_type.get_full_name ();
147 } else {
148 s = "null";
151 var type_args = get_type_arguments ();
152 if (type_args.size > 0) {
153 s += "<";
154 bool first = true;
155 foreach (DataType type_arg in type_args) {
156 if (!first) {
157 s += ",";
158 } else {
159 first = false;
161 if (!type_arg.value_owned) {
162 s += "weak ";
164 s += type_arg.to_qualified_string (scope);
166 s += ">";
168 if (nullable) {
169 s += "?";
172 return s;
176 * Creates a shallow copy of this type reference.
178 * @return copy of this type reference
180 public abstract DataType copy ();
183 * Checks two type references for equality. May only be used with
184 * resolved type references.
186 * @param type2 a type reference
187 * @return true if this type reference is equal to type2, false
188 * otherwise
190 public virtual bool equals (DataType type2) {
191 if (type2.is_disposable () != is_disposable ()) {
192 return false;
194 if (type2.nullable != nullable) {
195 return false;
197 if (type2.data_type != data_type) {
198 return false;
200 if (type2.type_parameter != null || type_parameter != null) {
201 if (type2.type_parameter == null || type_parameter == null) {
202 return false;
204 if (!type2.type_parameter.equals (type_parameter)) {
205 return false;
208 if (type2.floating_reference != floating_reference) {
209 return false;
212 var type_args = get_type_arguments ();
213 var type2_args = type2.get_type_arguments ();
214 if (type2_args.size != type_args.size) {
215 return false;
218 for (int i = 0; i < type_args.size; i++) {
219 if (!type2_args[i].equals (type_args[i]))
220 return false;
223 return true;
227 * Checks whether this type reference is at least as strict as the
228 * specified type reference type2.
230 * @param type2 a type reference
231 * @return true if this type reference is stricter or equal
233 public virtual bool stricter (DataType type2) {
234 if (type2.is_disposable () != is_disposable ()) {
235 return false;
238 if (!type2.nullable && nullable) {
239 return false;
242 /* temporarily ignore type parameters */
243 if (type_parameter != null || type2.type_parameter != null) {
244 return true;
247 if (type2.data_type != data_type) {
248 // FIXME: allow this type reference to refer to a
249 // subtype of the type type2 is referring to
250 return false;
253 if (type2.floating_reference != floating_reference) {
254 return false;
257 return true;
260 public override void replace_type (DataType old_type, DataType new_type) {
261 if (type_argument_list != null) {
262 for (int i = 0; i < type_argument_list.size; i++) {
263 if (type_argument_list[i] == old_type) {
264 type_argument_list[i] = new_type;
265 return;
271 public virtual bool compatible (DataType target_type) {
272 if (CodeContext.get ().experimental_non_null && nullable && !target_type.nullable) {
273 return false;
276 if (target_type.data_type != null) {
277 if (target_type.data_type.is_subtype_of (CodeContext.get ().analyzer.gvalue_type.data_type)) {
278 // allow implicit conversion to GValue
279 return true;
282 if (target_type.data_type.is_subtype_of (CodeContext.get ().analyzer.gvariant_type.data_type)) {
283 // allow implicit conversion to GVariant
284 return true;
288 if (target_type is DelegateType && this is DelegateType) {
289 return ((DelegateType) target_type).delegate_symbol == ((DelegateType) this).delegate_symbol;
292 if (target_type is PointerType) {
293 /* any reference or array type or pointer type can be cast to a generic pointer */
294 if (type_parameter != null ||
295 (data_type != null && (
296 data_type.is_reference_type () ||
297 this is DelegateType))) {
298 return true;
301 return false;
304 /* temporarily ignore type parameters */
305 if (target_type.type_parameter != null) {
306 return true;
309 if (this is ArrayType != target_type is ArrayType) {
310 return false;
313 if (data_type is Enum && target_type.data_type is Struct && ((Struct) target_type.data_type).is_integer_type ()) {
314 return true;
317 if (data_type != null && target_type.data_type != null && data_type.is_subtype_of (target_type.data_type)) {
318 var base_type = SemanticAnalyzer.get_instance_base_type_for_member(this, target_type.data_type, this);
319 // check compatibility of generic type arguments
320 var base_type_args = base_type.get_type_arguments();
321 var target_type_args = target_type.get_type_arguments();
322 if (base_type_args.size == target_type_args.size) {
323 for (int i = 0; i < base_type_args.size; i++) {
324 // mutable generic types require type argument equality,
325 // not just one way compatibility
326 // as we do not currently have immutable generic container types,
327 // the additional check would be very inconvenient, so we
328 // skip the additional check for now
329 if (!base_type_args[i].compatible (target_type_args[i])) {
330 return false;
334 return true;
337 if (data_type is Struct && target_type.data_type is Struct) {
338 var expr_struct = (Struct) data_type;
339 var expect_struct = (Struct) target_type.data_type;
341 /* integer types may be implicitly cast to floating point types */
342 if (expr_struct.is_integer_type () && expect_struct.is_floating_type ()) {
343 return true;
346 if ((expr_struct.is_integer_type () && expect_struct.is_integer_type ()) ||
347 (expr_struct.is_floating_type () && expect_struct.is_floating_type ())) {
348 if (expr_struct.get_rank () <= expect_struct.get_rank ()) {
349 return true;
354 return false;
358 * Returns whether instances of this type are invokable.
360 * @return true if invokable, false otherwise
362 public virtual bool is_invokable () {
363 return false;
367 * Returns the return type of this invokable.
369 * @return return type
371 public virtual DataType? get_return_type () {
372 return null;
376 * Returns copy of the list of invocation parameters.
378 * @return parameter list
380 public virtual List<Parameter>? get_parameters () {
381 return null;
384 public virtual bool is_reference_type_or_type_parameter () {
385 return (data_type != null &&
386 data_type.is_reference_type ()) ||
387 type_parameter != null;
390 public virtual bool is_array () {
391 return false;
394 // check whether this type is at least as accessible as the specified symbol
395 public virtual bool is_accessible (Symbol sym) {
396 foreach (var type_arg in get_type_arguments ()) {
397 if (!type_arg.is_accessible (sym)) {
398 return false;
401 if (data_type != null) {
402 return data_type.is_accessible (sym);
404 return true;
407 public virtual Symbol? get_member (string member_name) {
408 if (data_type != null) {
409 return SemanticAnalyzer.symbol_lookup_inherited (data_type, member_name);
411 return null;
414 public virtual Symbol? get_pointer_member (string member_name) {
415 return null;
419 * Checks whether this data type references a real struct. A real struct
420 * is a struct which is not a simple (fundamental) type.
422 public virtual bool is_real_struct_type () {
423 var s = data_type as Struct;
424 if (s != null && !s.is_simple_type ()) {
425 return true;
427 return false;
430 public bool is_real_non_null_struct_type () {
431 return is_real_struct_type () && !nullable;
435 * Returns whether the value needs to be disposed, i.e. whether
436 * allocated memory or other resources need to be released when
437 * the value is no longer needed.
439 public virtual bool is_disposable () {
440 if (!value_owned) {
441 return false;
444 if (is_reference_type_or_type_parameter ()) {
445 return true;
447 return false;
450 public virtual DataType get_actual_type (DataType? derived_instance_type, List<DataType>? method_type_arguments, CodeNode node_reference) {
451 DataType result = this.copy ();
453 if (derived_instance_type == null && method_type_arguments == null) {
454 return result;
457 if (result is GenericType) {
458 result = SemanticAnalyzer.get_actual_type (derived_instance_type, method_type_arguments, (GenericType) result, node_reference);
459 // don't try to resolve type arguments of returned actual type
460 // they can never be resolved and are not related to the instance type
461 } else if (result.type_argument_list != null) {
462 // recursely get actual types for type arguments
463 for (int i = 0; i < result.type_argument_list.size; i++) {
464 result.type_argument_list[i] = result.type_argument_list[i].get_actual_type (derived_instance_type, method_type_arguments, node_reference);
468 return result;
472 * Search for the type parameter in this formal type and match it in
473 * value_type.
475 public virtual DataType? infer_type_argument (TypeParameter type_param, DataType value_type) {
476 var value_type_arg_it = value_type.get_type_arguments ().iterator ();
477 foreach (var formal_type_arg in this.get_type_arguments ()) {
478 if (value_type_arg_it.next ()) {
479 var inferred_type = formal_type_arg.infer_type_argument (type_param, value_type_arg_it.get ());
480 if (inferred_type != null) {
481 return inferred_type;
486 return null;
489 public bool is_weak () {
490 if (this.value_owned) {
491 return false;
492 } else if (this is VoidType || this is PointerType) {
493 return false;
494 } else if (this is ValueType) {
495 if (this.nullable) {
496 // nullable structs are heap allocated
497 return true;
500 // TODO return true for structs with destroy
501 return false;
504 return true;