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
21 * Jürg Billeter <j@bitron.ch>
22 * Raffaele Sandrini <raffaele@sandrini.ch>
28 * A reference to a data type. This is used to specify static types of
31 public abstract class Vala
.DataType
: CodeNode
{
33 * Specifies that the expression or variable owns the value.
35 public bool value_owned
{ get; set; }
38 * Specifies that the expression may be null.
40 public bool nullable
{ get; set; }
43 * The referred data type.
45 public weak TypeSymbol data_type
{ get; set; }
48 * The referred generic type parameter.
50 public TypeParameter type_parameter
{ get; set; }
53 * Specifies that the expression transfers a floating reference.
55 public bool floating_reference
{ get; set; }
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
;
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
;
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
;
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 () {
117 Report
.error (source_reference
, "unresolved type reference");
121 public virtual string get_cdeclarator_suffix () {
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 () {
134 // FIXME: workaround to make constant arrays possible
135 if (this is ArrayType
) {
136 t
= ((ArrayType
) this
).element_type
.data_type
;
140 if (!t
.is_reference_type ()) {
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
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) {
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
;
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 ();;
184 s
= data_type
.get_full_name ();
190 var type_args
= get_type_arguments ();
191 if (type_args
.size
> 0) {
194 foreach (DataType type_arg
in type_args
) {
200 if (!type_arg
.value_owned
) {
203 s
+= type_arg
.to_qualified_string (scope
);
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
229 public virtual bool equals (DataType type2
) {
230 if (type2
.value_owned
!= value_owned
) {
233 if (type2
.nullable
!= nullable
) {
236 if (type2
.data_type
!= data_type
) {
239 if (type2
.type_parameter
!= null || type_parameter
!= null) {
240 if (type2
.type_parameter
== null || type_parameter
== null) {
243 if (!type2
.type_parameter
.equals (type_parameter
)) {
247 if (type2
.floating_reference
!= floating_reference
) {
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
) {
266 if (!type2
.nullable
&& nullable
) {
270 /* temporarily ignore type parameters */
271 if (type_parameter
!= null || type2
.type_parameter
!= null) {
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
281 if (type2
.floating_reference
!= floating_reference
) {
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
;
299 public virtual bool compatible (DataType target_type
) {
300 if (CodeContext
.get ().experimental_non_null
&& nullable
&& !target_type
.nullable
) {
304 if (target_type
.get_type_id () == "G_TYPE_VALUE" && get_type_id () != null) {
305 // allow implicit conversion to GValue
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
))) {
325 /* temporarily ignore type parameters */
326 if (target_type
.type_parameter
!= null) {
330 if (this is ArrayType
!= target_type is ArrayType
) {
334 if (data_type is Enum
&& target_type
.data_type is Struct
&& ((Struct
) target_type
.data_type
).is_integer_type ()) {
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
)) {
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 ()) {
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 ()) {
376 if (data_type
!= null && target_type
.data_type
!= null && data_type
.is_subtype_of (target_type
.data_type
)) {
384 * Returns whether instances of this type are invokable.
386 * @return true if invokable, false otherwise
388 public virtual bool is_invokable () {
393 * Returns the return type of this invokable.
395 * @return return type
397 public virtual DataType?
get_return_type () {
402 * Returns copy of the list of invocation parameters.
404 * @return parameter list
406 public virtual List
<FormalParameter
>?
get_parameters () {
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 () {
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
);
433 public virtual Symbol?
get_member (string member_name
) {
434 if (data_type
!= null) {
435 return SemanticAnalyzer
.symbol_lookup_inherited (data_type
, member_name
);
440 public virtual Symbol?
get_pointer_member (string member_name
) {
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 ()) {
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 ();
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 ();
485 sig
= sig
.printf (element_sig
);
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 () {
504 if (is_reference_type_or_type_parameter ()) {
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) {
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
);