Sync NEWS from 0.40
[vala-gnome.git] / vala / valapointertype.vala
blob6e0d799cf5fa08f73554e0796c08896cc4928e2b
1 /* valapointertype.vala
3 * Copyright (C) 2007-2009 Jürg Billeter
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * A pointer type.
28 public class Vala.PointerType : DataType {
29 /**
30 * The base type the pointer is referring to.
32 public DataType base_type {
33 get { return _base_type; }
34 set {
35 _base_type = value;
36 _base_type.parent_node = this;
40 private DataType _base_type;
42 public PointerType (DataType base_type, SourceReference? source_reference = null) {
43 this.base_type = base_type;
44 nullable = true;
45 this.source_reference = source_reference;
48 public override string to_qualified_string (Scope? scope) {
49 return base_type.to_qualified_string (scope) + "*";
52 public override DataType copy () {
53 return new PointerType (base_type.copy ());
56 public override bool compatible (DataType target_type) {
57 if (target_type is PointerType) {
58 var tt = target_type as PointerType;
60 if (tt.base_type is VoidType || base_type is VoidType) {
61 return true;
64 /* dereference only if both types are references or not */
65 if (base_type.is_reference_type_or_type_parameter () != tt.base_type.is_reference_type_or_type_parameter ()) {
66 return false;
69 return base_type.compatible (tt.base_type);
72 if ((target_type.data_type != null && target_type.data_type.get_attribute ("PointerType") != null)) {
73 return true;
76 /* temporarily ignore type parameters */
77 if (target_type is GenericType) {
78 return true;
81 if (base_type.is_reference_type_or_type_parameter ()) {
82 // Object* is compatible with Object if Object is a reference type
83 return base_type.compatible (target_type);
86 if (CodeContext.get ().profile == Profile.GOBJECT && target_type.data_type != null && target_type.data_type.is_subtype_of (CodeContext.get ().analyzer.gvalue_type.data_type)) {
87 // allow implicit conversion to GValue
88 return true;
91 return false;
94 public override Symbol? get_member (string member_name) {
95 return null;
98 public override Symbol? get_pointer_member (string member_name) {
99 Symbol base_symbol = base_type.data_type;
101 if (base_symbol == null) {
102 return null;
105 return SemanticAnalyzer.symbol_lookup_inherited (base_symbol, member_name);
108 public override bool is_accessible (Symbol sym) {
109 return base_type.is_accessible (sym);
112 public override void accept_children (CodeVisitor visitor) {
113 base_type.accept (visitor);
116 public override void replace_type (DataType old_type, DataType new_type) {
117 if (base_type == old_type) {
118 base_type = new_type;
122 public override bool is_disposable () {
123 return false;
126 public override DataType get_actual_type (DataType? derived_instance_type, List<DataType>? method_type_arguments, CodeNode node_reference) {
127 PointerType result = (PointerType) this.copy ();
129 if (derived_instance_type == null && method_type_arguments == null) {
130 return result;
133 if (base_type is GenericType || base_type.has_type_arguments ()) {
134 result.base_type = result.base_type.get_actual_type (derived_instance_type, method_type_arguments, node_reference);
137 return result;
140 public override DataType? infer_type_argument (TypeParameter type_param, DataType value_type) {
141 var pointer_type = value_type as PointerType;
142 if (pointer_type != null) {
143 return base_type.infer_type_argument (type_param, pointer_type.base_type);
146 return null;
149 public override bool check (CodeContext context) {
150 error = !base_type.check (context);
151 return !error;