Line data Source code
1 : /* valapointertype.vala
2 : *
3 : * Copyright (C) 2007-2009 Jürg Billeter
4 : *
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.
9 :
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.
14 :
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
18 : *
19 : * Author:
20 : * Jürg Billeter <j@bitron.ch>
21 : */
22 :
23 : using GLib;
24 :
25 : /**
26 : * A pointer type.
27 : */
28 1232158 : public class Vala.PointerType : DataType {
29 : /**
30 : * The base type the pointer is referring to.
31 : */
32 : public DataType base_type {
33 4382483 : get { return _base_type; }
34 1401680 : set {
35 1401680 : _base_type = value;
36 1401680 : _base_type.parent_node = this;
37 : }
38 : }
39 :
40 1230621 : private DataType _base_type;
41 :
42 3691863 : public PointerType (DataType base_type, SourceReference? source_reference = null) {
43 1230621 : this.base_type = base_type;
44 1230621 : nullable = true;
45 1230621 : this.source_reference = source_reference;
46 : }
47 :
48 741 : public override string to_qualified_string (Scope? scope) {
49 741 : return base_type.to_qualified_string (scope) + "*";
50 : }
51 :
52 606742 : public override DataType copy () {
53 606742 : return new PointerType (base_type.copy (), source_reference);
54 : }
55 :
56 119229 : public override bool compatible (DataType target_type) {
57 119229 : if (target_type is PointerType) {
58 96443 : unowned PointerType? tt = target_type as PointerType;
59 :
60 96443 : if (tt.base_type is VoidType || base_type is VoidType) {
61 22806 : return true;
62 : }
63 :
64 : /* dereference only if both types are references or not */
65 73637 : if (base_type.is_reference_type_or_type_parameter () != tt.base_type.is_reference_type_or_type_parameter ()) {
66 12880 : return false;
67 : }
68 :
69 73637 : return base_type.compatible (tt.base_type);
70 : }
71 :
72 22786 : if ((target_type.type_symbol != null && target_type.type_symbol.has_attribute ("PointerType"))) {
73 29881 : return true;
74 : }
75 :
76 : /* temporarily ignore type parameters */
77 22786 : if (target_type is GenericType) {
78 29881 : return true;
79 : }
80 :
81 15711 : if (base_type.is_reference_type_or_type_parameter ()) {
82 : // Object* is compatible with Object if Object is a reference type
83 2831 : return base_type.compatible (target_type);
84 : }
85 :
86 12880 : var context = CodeContext.get ();
87 :
88 12880 : if (context.profile == Profile.GOBJECT && target_type.type_symbol != null && target_type.type_symbol.is_subtype_of (context.analyzer.gvalue_type.type_symbol)) {
89 : // allow implicit conversion to GValue
90 0 : return true;
91 : }
92 :
93 12880 : return false;
94 : }
95 :
96 1 : public override Symbol? get_member (string member_name) {
97 : return null;
98 : }
99 :
100 2 : public override Symbol? get_pointer_member (string member_name) {
101 2 : unowned Symbol? base_symbol = base_type.type_symbol;
102 :
103 2 : if (base_symbol == null) {
104 0 : return null;
105 : }
106 :
107 2 : return SemanticAnalyzer.symbol_lookup_inherited (base_symbol, member_name);
108 : }
109 :
110 474212 : public override bool is_accessible (Symbol sym) {
111 474212 : return base_type.is_accessible (sym);
112 : }
113 :
114 1196584 : public override void accept_children (CodeVisitor visitor) {
115 1196584 : base_type.accept (visitor);
116 : }
117 :
118 9888 : public override bool stricter (DataType type2) {
119 9888 : if (type2 is PointerType) {
120 9887 : return compatible (type2);
121 : }
122 :
123 1 : if (base_type is VoidType) {
124 : // void* can hold reference types
125 1 : return type2 is ReferenceType;
126 : }
127 :
128 0 : return base_type.stricter (type2);
129 : }
130 :
131 171055 : public override void replace_type (DataType old_type, DataType new_type) {
132 171055 : if (base_type == old_type) {
133 171055 : base_type = new_type;
134 : }
135 : }
136 :
137 61871 : public override bool is_disposable () {
138 61871 : return false;
139 : }
140 :
141 101982 : public override DataType get_actual_type (DataType? derived_instance_type, List<DataType>? method_type_arguments, CodeNode? node_reference) {
142 101982 : PointerType result = (PointerType) this.copy ();
143 :
144 101982 : if (derived_instance_type == null && method_type_arguments == null) {
145 : return result;
146 : }
147 :
148 101982 : if (base_type is GenericType || base_type.has_type_arguments ()) {
149 4 : result.base_type = result.base_type.get_actual_type (derived_instance_type, method_type_arguments, node_reference);
150 : }
151 :
152 : return result;
153 : }
154 :
155 1 : public override DataType? infer_type_argument (TypeParameter type_param, DataType value_type) {
156 1 : unowned PointerType? pointer_type = value_type as PointerType;
157 1 : if (pointer_type != null) {
158 1 : return base_type.infer_type_argument (type_param, pointer_type.base_type);
159 : }
160 :
161 1 : return null;
162 : }
163 :
164 1141519 : public override bool check (CodeContext context) {
165 1141519 : error = !base_type.check (context);
166 1141519 : return !error;
167 : }
168 : }
|