girparser: Add common parse_symbol_from_string method
[vala-lang.git] / vala / valadatatype.vala
blobd13e5e09cf4f9897488cefb3a28ed89c0f5a6ef2
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 if (type_argument_list != null && type_argument_list.size > 0) {
110 foreach (DataType type_arg in type_argument_list) {
111 type_arg.accept (visitor);
115 visitor.visit_data_type (this);
119 * Returns the name and qualifiers of this type as it is used in C code.
121 * @return the type string to be used in C code
123 public virtual string? get_cname () {
124 // raise error
125 Report.error (source_reference, "unresolved type reference");
126 return null;
129 public virtual string get_cdeclarator_suffix () {
130 return "";
134 * Returns the name and qualifiers of this type as it is used in C code
135 * in a const declaration.
137 * @return the type string to be used in C code const declarations
139 public string get_const_cname () {
140 string ptr;
141 TypeSymbol t;
142 // FIXME: workaround to make constant arrays possible
143 if (this is ArrayType) {
144 t = ((ArrayType) this).element_type.data_type;
145 } else {
146 t = data_type;
148 if (!t.is_reference_type ()) {
149 ptr = "";
150 } else {
151 ptr = "*";
154 return "const %s%s".printf (t.get_cname (), ptr);
158 * Returns the C name of this data type in lower case. Words are
159 * separated by underscores.
161 * @param infix a string to be placed between namespace and data type
162 * name or null
163 * @return the lower case name to be used in C code
165 public virtual string? get_lower_case_cname (string? infix = null) {
166 return data_type.get_lower_case_cname (infix);
169 public override string to_string () {
170 return to_qualified_string (null);
173 public virtual string to_qualified_string (Scope? scope = null) {
174 string s;
176 if (data_type != null) {
177 Symbol global_symbol = data_type;
178 while (global_symbol.parent_symbol.name != null) {
179 global_symbol = global_symbol.parent_symbol;
182 Symbol sym = null;
183 Scope parent_scope = scope;
184 while (sym == null && parent_scope != null) {
185 sym = parent_scope.lookup (global_symbol.name);
186 parent_scope = parent_scope.parent_scope;
189 if (sym != null && global_symbol != sym) {
190 s = "global::" + data_type.get_full_name ();;
191 } else {
192 s = data_type.get_full_name ();
194 } else {
195 s = "null";
198 var type_args = get_type_arguments ();
199 if (type_args.size > 0) {
200 s += "<";
201 bool first = true;
202 foreach (DataType type_arg in type_args) {
203 if (!first) {
204 s += ",";
205 } else {
206 first = false;
208 if (!type_arg.value_owned) {
209 s += "weak ";
211 s += type_arg.to_qualified_string (scope);
213 s += ">";
215 if (nullable) {
216 s += "?";
219 return s;
223 * Creates a shallow copy of this type reference.
225 * @return copy of this type reference
227 public abstract DataType copy ();
230 * Checks two type references for equality. May only be used with
231 * resolved type references.
233 * @param type2 a type reference
234 * @return true if this type reference is equal to type2, false
235 * otherwise
237 public virtual bool equals (DataType type2) {
238 if (type2.is_disposable () != is_disposable ()) {
239 return false;
241 if (type2.nullable != nullable) {
242 return false;
244 if (type2.data_type != data_type) {
245 return false;
247 if (type2.type_parameter != null || type_parameter != null) {
248 if (type2.type_parameter == null || type_parameter == null) {
249 return false;
251 if (!type2.type_parameter.equals (type_parameter)) {
252 return false;
255 if (type2.floating_reference != floating_reference) {
256 return false;
259 return true;
263 * Checks whether this type reference is at least as strict as the
264 * specified type reference type2.
266 * @param type2 a type reference
267 * @return true if this type reference is stricter or equal
269 public virtual bool stricter (DataType type2) {
270 if (type2.is_disposable () != is_disposable ()) {
271 return false;
274 if (!type2.nullable && nullable) {
275 return false;
278 /* temporarily ignore type parameters */
279 if (type_parameter != null || type2.type_parameter != null) {
280 return true;
283 if (type2.data_type != data_type) {
284 // FIXME: allow this type reference to refer to a
285 // subtype of the type type2 is referring to
286 return false;
289 if (type2.floating_reference != floating_reference) {
290 return false;
293 return true;
296 public override void replace_type (DataType old_type, DataType new_type) {
297 if (type_argument_list != null) {
298 for (int i = 0; i < type_argument_list.size; i++) {
299 if (type_argument_list[i] == old_type) {
300 type_argument_list[i] = new_type;
301 return;
307 public virtual bool compatible (DataType target_type) {
308 if (CodeContext.get ().experimental_non_null && nullable && !target_type.nullable) {
309 return false;
312 if (target_type.get_type_id () == "G_TYPE_VALUE" && get_type_id () != null) {
313 // allow implicit conversion to GValue
314 return true;
317 if (target_type.get_type_id () == "G_TYPE_VARIANT") {
318 // allow implicit conversion to GVariant
319 return true;
322 if (this is ValueType && target_type.data_type != null && target_type.data_type.get_full_name () == "Dova.Value") {
323 // allow implicit conversion to Dova.Value
324 return true;
327 if (target_type is DelegateType && this is DelegateType) {
328 return ((DelegateType) target_type).delegate_symbol == ((DelegateType) this).delegate_symbol;
331 if (target_type is PointerType) {
332 /* any reference or array type or pointer type can be cast to a generic pointer */
333 if (type_parameter != null ||
334 (data_type != null && (
335 data_type.is_reference_type () ||
336 this is DelegateType))) {
337 return true;
340 return false;
343 /* temporarily ignore type parameters */
344 if (target_type.type_parameter != null) {
345 return true;
348 if (this is ArrayType != target_type is ArrayType) {
349 return false;
352 if (data_type is Enum && target_type.data_type is Struct && ((Struct) target_type.data_type).is_integer_type ()) {
353 return true;
356 if (data_type == target_type.data_type) {
357 // check compatibility of generic type arguments
358 if (type_argument_list != null
359 && type_argument_list.size > 0
360 && type_argument_list.size == target_type.get_type_arguments ().size) {
361 for (int i = 0; i < type_argument_list.size; i++) {
362 var type_arg = type_argument_list[i];
363 var target_type_arg = target_type.get_type_arguments ()[i];
364 // mutable generic types require type argument equality,
365 // not just one way compatibility
366 // as we do not currently have immutable generic container types,
367 // the additional check would be very inconvenient, so we
368 // skip the additional check for now
369 if (!type_arg.compatible (target_type_arg)) {
370 return false;
374 return true;
377 if (data_type is Struct && target_type.data_type is Struct) {
378 var expr_struct = (Struct) data_type;
379 var expect_struct = (Struct) target_type.data_type;
381 /* integer types may be implicitly cast to floating point types */
382 if (expr_struct.is_integer_type () && expect_struct.is_floating_type ()) {
383 return true;
386 if ((expr_struct.is_integer_type () && expect_struct.is_integer_type ()) ||
387 (expr_struct.is_floating_type () && expect_struct.is_floating_type ())) {
388 if (expr_struct.get_rank () <= expect_struct.get_rank ()) {
389 return true;
394 if (data_type != null && target_type.data_type != null && data_type.is_subtype_of (target_type.data_type)) {
395 return true;
398 return false;
402 * Returns whether instances of this type are invokable.
404 * @return true if invokable, false otherwise
406 public virtual bool is_invokable () {
407 return false;
411 * Returns the return type of this invokable.
413 * @return return type
415 public virtual DataType? get_return_type () {
416 return null;
420 * Returns copy of the list of invocation parameters.
422 * @return parameter list
424 public virtual List<Parameter>? get_parameters () {
425 return null;
428 public virtual bool is_reference_type_or_type_parameter () {
429 return (data_type != null &&
430 data_type.is_reference_type ()) ||
431 type_parameter != null;
434 public virtual bool is_array () {
435 return false;
438 // check whether this type is at least as accessible as the specified symbol
439 public virtual bool is_accessible (Symbol sym) {
440 if (data_type != null) {
441 return data_type.is_accessible (sym);
443 return true;
446 public virtual Symbol? get_member (string member_name) {
447 if (data_type != null) {
448 return SemanticAnalyzer.symbol_lookup_inherited (data_type, member_name);
450 return null;
453 public virtual Symbol? get_pointer_member (string member_name) {
454 return null;
458 * Checks whether this data type references a real struct. A real struct
459 * is a struct which is not a simple (fundamental) type.
461 public virtual bool is_real_struct_type () {
462 var s = data_type as Struct;
463 if (s != null && !s.is_simple_type ()) {
464 return true;
466 return false;
469 public bool is_real_non_null_struct_type () {
470 return is_real_struct_type () && !nullable;
473 public virtual string? get_type_id () {
474 if (data_type != null) {
475 return data_type.get_type_id ();
476 } else {
477 return null;
482 * Returns whether the value needs to be disposed, i.e. whether
483 * allocated memory or other resources need to be released when
484 * the value is no longer needed.
486 public virtual bool is_disposable () {
487 if (!value_owned) {
488 return false;
491 if (is_reference_type_or_type_parameter ()) {
492 return true;
494 return false;
497 public virtual DataType get_actual_type (DataType? derived_instance_type, MemberAccess? method_access, CodeNode node_reference) {
498 if (derived_instance_type == null && method_access == null) {
499 return this;
502 DataType result = this;
504 if (result is GenericType) {
505 result = SemanticAnalyzer.get_actual_type (derived_instance_type, method_access, (GenericType) result, node_reference);
506 // don't try to resolve type arguments of returned actual type
507 // they can never be resolved and are not related to the instance type
508 } else if (result.type_argument_list != null) {
509 // recursely get actual types for type arguments
510 result = result.copy ();
511 for (int i = 0; i < result.type_argument_list.size; i++) {
512 result.type_argument_list[i] = result.type_argument_list[i].get_actual_type (derived_instance_type, method_access, node_reference);
516 return result;
519 public bool is_weak () {
520 if (this.value_owned) {
521 return false;
522 } else if (this is VoidType || this is PointerType) {
523 return false;
524 } else if (this is ValueType) {
525 if (this.nullable) {
526 // nullable structs are heap allocated
527 return true;
530 // TODO return true for structs with destroy
531 return false;
534 return true;