Use strict non-null types with --enable-experimental-non-null
[vala-lang.git] / vala / valadatatype.vala
blobb8b6cebab2b134490376b77fd18c7a2971ecf622
1 /* valadatatype.vala
3 * Copyright (C) 2006-2009 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 ReadOnlyList<DataType> (new ArrayList<DataType> ());
90 return _empty_type_list;
93 /**
94 * Removes all generic type arguments.
96 public void remove_all_type_arguments () {
97 type_argument_list = null;
100 public override void accept (CodeVisitor visitor) {
101 if (type_argument_list != null && type_argument_list.size > 0) {
102 foreach (DataType type_arg in type_argument_list) {
103 type_arg.accept (visitor);
107 visitor.visit_data_type (this);
111 * Returns the name and qualifiers of this type as it is used in C code.
113 * @return the type string to be used in C code
115 public virtual string? get_cname () {
116 // raise error
117 Report.error (source_reference, "unresolved type reference");
118 return null;
121 public virtual string get_cdeclarator_suffix () {
122 return "";
126 * Returns the name and qualifiers of this type as it is used in C code
127 * in a const declaration.
129 * @return the type string to be used in C code const declarations
131 public string get_const_cname () {
132 string ptr;
133 TypeSymbol t;
134 // FIXME: workaround to make constant arrays possible
135 if (this is ArrayType) {
136 t = ((ArrayType) this).element_type.data_type;
137 } else {
138 t = data_type;
140 if (!t.is_reference_type ()) {
141 ptr = "";
142 } else {
143 ptr = "*";
146 return "const %s%s".printf (t.get_cname (), ptr);
150 * Returns the C name of this data type in lower case. Words are
151 * separated by underscores.
153 * @param infix a string to be placed between namespace and data type
154 * name or null
155 * @return the lower case name to be used in C code
157 public virtual string? get_lower_case_cname (string? infix = null) {
158 return data_type.get_lower_case_cname (infix);
161 public override string to_string () {
162 return to_qualified_string (null);
165 public virtual string to_qualified_string (Scope? scope = null) {
166 string s;
168 if (data_type != null) {
169 Symbol global_symbol = data_type;
170 while (global_symbol.parent_symbol.name != null) {
171 global_symbol = global_symbol.parent_symbol;
174 Symbol sym = null;
175 Scope parent_scope = scope;
176 while (sym == null && parent_scope != null) {
177 sym = parent_scope.lookup (global_symbol.name);
178 parent_scope = parent_scope.parent_scope;
181 if (sym != null && global_symbol != sym) {
182 s = "global::" + data_type.get_full_name ();;
183 } else {
184 s = data_type.get_full_name ();
186 } else {
187 s = "null";
190 var type_args = get_type_arguments ();
191 if (type_args.size > 0) {
192 s += "<";
193 bool first = true;
194 foreach (DataType type_arg in type_args) {
195 if (!first) {
196 s += ",";
197 } else {
198 first = false;
200 if (!type_arg.value_owned) {
201 s += "weak ";
203 s += type_arg.to_qualified_string (scope);
205 s += ">";
207 if (nullable) {
208 s += "?";
211 return s;
215 * Creates a shallow copy of this type reference.
217 * @return copy of this type reference
219 public abstract DataType copy ();
222 * Checks two type references for equality. May only be used with
223 * resolved type references.
225 * @param type2 a type reference
226 * @return true if this type reference is equal to type2, false
227 * otherwise
229 public virtual bool equals (DataType type2) {
230 if (type2.value_owned != value_owned) {
231 return false;
233 if (type2.nullable != nullable) {
234 return false;
236 if (type2.data_type != data_type) {
237 return false;
239 if (type2.type_parameter != null || type_parameter != null) {
240 if (type2.type_parameter == null || type_parameter == null) {
241 return false;
243 if (!type2.type_parameter.equals (type_parameter)) {
244 return false;
247 if (type2.floating_reference != floating_reference) {
248 return false;
251 return true;
255 * Checks whether this type reference is at least as strict as the
256 * specified type reference type2.
258 * @param type2 a type reference
259 * @return true if this type reference is stricter or equal
261 public virtual bool stricter (DataType type2) {
262 if (type2.value_owned != value_owned) {
263 return false;
266 if (!type2.nullable && nullable) {
267 return false;
270 /* temporarily ignore type parameters */
271 if (type_parameter != null || type2.type_parameter != null) {
272 return true;
275 if (type2.data_type != data_type) {
276 // FIXME: allow this type reference to refer to a
277 // subtype of the type type2 is referring to
278 return false;
281 if (type2.floating_reference != floating_reference) {
282 return false;
285 return true;
288 public override void replace_type (DataType old_type, DataType new_type) {
289 if (type_argument_list != null) {
290 for (int i = 0; i < type_argument_list.size; i++) {
291 if (type_argument_list[i] == old_type) {
292 type_argument_list[i] = new_type;
293 return;
299 public virtual bool compatible (DataType target_type) {
300 if (CodeContext.get ().experimental_non_null && nullable && !target_type.nullable) {
301 return false;
304 if (target_type.get_type_id () == "G_TYPE_VALUE" && get_type_id () != null) {
305 // allow implicit conversion to GValue
306 return true;
309 if (target_type is DelegateType && this is DelegateType) {
310 return ((DelegateType) target_type).delegate_symbol == ((DelegateType) this).delegate_symbol;
313 if (target_type is PointerType) {
314 /* any reference or array type or pointer type can be cast to a generic pointer */
315 if (type_parameter != null ||
316 (data_type != null && (
317 data_type.is_reference_type () ||
318 this is DelegateType))) {
319 return true;
322 return false;
325 /* temporarily ignore type parameters */
326 if (target_type.type_parameter != null) {
327 return true;
330 if (this is ArrayType != target_type is ArrayType) {
331 return false;
334 if (data_type is Enum && target_type.data_type is Struct && ((Struct) target_type.data_type).is_integer_type ()) {
335 return true;
338 if (data_type == target_type.data_type) {
339 // check compatibility of generic type arguments
340 if (type_argument_list != null
341 && type_argument_list.size > 0
342 && type_argument_list.size == target_type.get_type_arguments ().size) {
343 for (int i = 0; i < type_argument_list.size; i++) {
344 var type_arg = type_argument_list[i];
345 var target_type_arg = target_type.get_type_arguments ()[i];
346 // mutable generic types require type argument equality,
347 // not just one way compatibility
348 // as we do not currently have immutable generic container types,
349 // the additional check would be very inconvenient, so we
350 // skip the additional check for now
351 if (!type_arg.compatible (target_type_arg)) {
352 return false;
356 return true;
359 if (data_type is Struct && target_type.data_type is Struct) {
360 var expr_struct = (Struct) data_type;
361 var expect_struct = (Struct) target_type.data_type;
363 /* integer types may be implicitly cast to floating point types */
364 if (expr_struct.is_integer_type () && expect_struct.is_floating_type ()) {
365 return true;
368 if ((expr_struct.is_integer_type () && expect_struct.is_integer_type ()) ||
369 (expr_struct.is_floating_type () && expect_struct.is_floating_type ())) {
370 if (expr_struct.get_rank () <= expect_struct.get_rank ()) {
371 return true;
376 if (data_type != null && target_type.data_type != null && data_type.is_subtype_of (target_type.data_type)) {
377 return true;
380 return false;
384 * Returns whether instances of this type are invokable.
386 * @return true if invokable, false otherwise
388 public virtual bool is_invokable () {
389 return false;
393 * Returns the return type of this invokable.
395 * @return return type
397 public virtual DataType? get_return_type () {
398 return null;
402 * Returns copy of the list of invocation parameters.
404 * @return parameter list
406 public virtual List<FormalParameter>? get_parameters () {
407 return null;
410 public virtual bool is_reference_type_or_type_parameter () {
411 return (data_type != null &&
412 data_type.is_reference_type ()) ||
413 type_parameter != null;
416 public virtual bool is_array () {
417 return false;
421 * Returns a list of symbols that define this type.
423 * @return symbol list
425 public virtual List<Symbol> get_symbols () {
426 var symbols = new ArrayList<Symbol> ();
427 if (data_type != null) {
428 symbols.add (data_type);
430 return symbols;
433 public virtual Symbol? get_member (string member_name) {
434 if (data_type != null) {
435 return SemanticAnalyzer.symbol_lookup_inherited (data_type, member_name);
437 return null;
440 public virtual Symbol? get_pointer_member (string member_name) {
441 return null;
445 * Checks whether this data type references a real struct. A real struct
446 * is a struct which is not a simple (fundamental) type.
448 public virtual bool is_real_struct_type () {
449 var s = data_type as Struct;
450 if (s != null && !s.is_simple_type ()) {
451 return true;
453 return false;
456 public bool is_real_non_null_struct_type () {
457 return is_real_struct_type () && !nullable;
460 public virtual string? get_type_id () {
461 if (data_type != null) {
462 return data_type.get_type_id ();
463 } else {
464 return null;
469 * Returns type signature as used for GVariant and D-Bus.
471 public virtual string? get_type_signature () {
472 if (data_type != null) {
473 string sig = data_type.get_type_signature ();
475 var type_args = get_type_arguments ();
476 if (sig != null && sig.str ("%s") != null && type_args.size > 0) {
477 string element_sig = "";
478 foreach (DataType type_arg in type_args) {
479 var s = type_arg.get_type_signature ();
480 if (s != null) {
481 element_sig += s;
485 sig = sig.printf (element_sig);
488 return sig;
489 } else {
490 return null;
495 * Returns whether the value needs to be disposed, i.e. whether
496 * allocated memory or other resources need to be released when
497 * the value is no longer needed.
499 public virtual bool is_disposable () {
500 if (!value_owned) {
501 return false;
504 if (is_reference_type_or_type_parameter ()) {
505 return true;
507 return false;
510 public DataType get_actual_type (DataType? derived_instance_type, MemberAccess? method_access, CodeNode node_reference) {
511 if (derived_instance_type == null && method_access == null) {
512 return this;
515 DataType result = this;
517 if (result is GenericType) {
518 result = SemanticAnalyzer.get_actual_type (derived_instance_type, method_access, (GenericType) result, node_reference);
519 // don't try to resolve type arguments of returned actual type
520 // they can never be resolved and are not related to the instance type
521 } else if (result.type_argument_list != null) {
522 // recursely get actual types for type arguments
523 result = result.copy ();
524 for (int i = 0; i < result.type_argument_list.size; i++) {
525 result.type_argument_list[i] = result.type_argument_list[i].get_actual_type (derived_instance_type, method_access, node_reference);
529 return result;