summaryrefslogtreecommitdiff
path: root/valadoc/symbolresolver.vala
blob: 20021c5fa59f4eae67a5f90ea8f41d7ebdce1d48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* symbolresolver.vala
 *
 * Copyright (C) 2011  Florian Brosch
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Florian Brosch <flo.brosch@gmail.com>
 */


using Valadoc.Api;

public class Valadoc.SymbolResolver : Visitor {
	private Vala.HashMap<Vala.Symbol, Symbol> symbol_map;
	private Valadoc.Api.Class glib_error;
	private Api.Tree root;

	public SymbolResolver (TreeBuilder builder) {
		this.symbol_map = builder.get_symbol_map ();
		this.glib_error = builder.get_glib_error ();
	}

	public Symbol? resolve (Vala.Symbol symbol) {
		return symbol_map.get (symbol);
	}

	private void resolve_thrown_list (Symbol symbol, Vala.Symbol vala_symbol) {
		var error_types = new Vala.ArrayList<Vala.DataType> ();
		vala_symbol.get_error_types (error_types);
		foreach (Vala.DataType type in error_types) {
			unowned Vala.ErrorDomain? vala_edom = ((Vala.ErrorType) type).error_domain;
			Symbol? edom = symbol_map.get (vala_edom);
			symbol.add_child (edom ?? glib_error);
		}
	}

	private void resolve_array_type_references (Api.Array ptr) {
		Api.Item data_type = ptr.data_type;
		if (data_type == null) {
			// void
		} else if (data_type is Api.Array) {
			resolve_array_type_references ((Api.Array) data_type);
		} else if (data_type is Pointer) {
			resolve_pointer_type_references ((Api.Pointer) data_type);
		} else {
			resolve_type_reference ((TypeReference) data_type);
		}
	}

	private void resolve_pointer_type_references (Pointer ptr) {
		Api.Item type = ptr.data_type;
		if (type == null) {
			// void
		} else if (type is Api.Array) {
			resolve_array_type_references ((Api.Array) type);
		} else if (type is Pointer) {
			resolve_pointer_type_references ((Pointer) type);
		} else {
			resolve_type_reference ((TypeReference) type);
		}
	}

	private void resolve_type_reference (TypeReference reference) {
		Vala.DataType vtyperef = (Vala.DataType) reference.data;
		if (vtyperef is Vala.ErrorType) {
			Vala.ErrorDomain verrdom = ((Vala.ErrorType) vtyperef).error_domain;
			if (verrdom != null) {
				reference.data_type = resolve (verrdom);
			} else {
				reference.data_type = glib_error;
			}
		} else if (vtyperef is Vala.DelegateType) {
			reference.data_type = resolve (((Vala.DelegateType) vtyperef).delegate_symbol);
		} else if (vtyperef is Vala.GenericType) {
			reference.data_type = resolve (((Vala.GenericType) vtyperef).type_parameter);
		} else if (vtyperef.type_symbol != null) {
			reference.data_type = resolve (vtyperef.type_symbol);
		}

		// Type parameters:
		foreach (TypeReference type_param_ref in reference.get_type_arguments ()) {
			resolve_type_reference (type_param_ref);
		}

		if (reference.data_type is Pointer) {
			resolve_pointer_type_references ((Pointer)reference.data_type);
		} else if (reference.data_type is Api.Array) {
			resolve_array_type_references ((Api.Array)reference.data_type);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_tree (Api.Tree item) {
		this.root = item;
		item.accept_children (this);
		this.root = null;
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_package (Package item) {
		item.accept_all_children (this, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_namespace (Namespace item) {
		item.accept_all_children (this, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_interface (Interface item) {
		Vala.Collection<TypeReference> interfaces = item.get_implemented_interface_list ();
		foreach (var type_ref in interfaces) {
			resolve_type_reference (type_ref);
		}

		if (item.base_type != null) {
			resolve_type_reference (item.base_type);
		}

		item.accept_all_children (this, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_class (Class item) {
		Vala.Collection<TypeReference> interfaces = item.get_implemented_interface_list ();
		foreach (TypeReference type_ref in interfaces) {
			resolve_type_reference (type_ref);
		}

		if (item.base_type != null)	{
			resolve_type_reference (item.base_type);
		}

		item.accept_all_children (this, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_struct (Struct item) {
		if (item.base_type != null) {
			resolve_type_reference (item.base_type);
		}

		item.accept_all_children (this, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_property (Property item) {
		Vala.Property vala_property = item.data as Vala.Property;
		Vala.Property? base_vala_property = null;

		if (vala_property.base_property != null) {
			base_vala_property = vala_property.base_property;
		} else if (vala_property.base_interface_property != null) {
			base_vala_property = vala_property.base_interface_property;
		}
		if (base_vala_property == vala_property && vala_property.base_interface_property != null) {
			base_vala_property = vala_property.base_interface_property;
		}
		if (base_vala_property != null) {
			item.base_property = (Property?) resolve (base_vala_property);
		}

		resolve_type_reference (item.property_type);

		item.accept_all_children (this, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_field (Field item) {
		resolve_type_reference (item.field_type);

		item.accept_all_children (this, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_constant (Constant item) {
		resolve_type_reference (item.constant_type);

		item.accept_all_children (this, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_delegate (Delegate item) {
		Vala.Delegate vala_delegate = item.data as Vala.Delegate;

		resolve_type_reference (item.return_type);

		resolve_thrown_list (item, vala_delegate);

		item.accept_all_children (this, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_signal (Api.Signal item) {
		resolve_type_reference (item.return_type);

		item.accept_all_children (this, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_method (Method item) {
		Vala.Method vala_method = item.data as Vala.Method;
		Vala.Method? base_vala_method = null;
		if (vala_method.base_method != null) {
			base_vala_method = vala_method.base_method;
		} else if (vala_method.base_interface_method != null) {
			base_vala_method = vala_method.base_interface_method;
		}
		if (base_vala_method == vala_method && vala_method.base_interface_method != null) {
			base_vala_method = vala_method.base_interface_method;
		}
		if (base_vala_method != null) {
			item.base_method = (Method?) resolve (base_vala_method);
		}

		resolve_thrown_list (item, vala_method);

		resolve_type_reference (item.return_type);

		item.accept_all_children (this, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_type_parameter (TypeParameter item) {
		item.accept_all_children (this, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_formal_parameter (Api.Parameter item) {
		if (item.ellipsis) {
			return;
		}

		if (((Vala.Parameter) item.data).initializer != null) {
			SignatureBuilder signature = new SignatureBuilder ();
			InitializerBuilder ibuilder = new InitializerBuilder (signature, symbol_map);
			((Vala.Parameter) item.data).initializer.accept (ibuilder);
			item.default_value = signature.get ();
		}

		resolve_type_reference (item.parameter_type);
		item.accept_all_children (this, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_error_domain (ErrorDomain item) {
		item.accept_all_children (this, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_error_code (ErrorCode item) {
		item.accept_all_children (this, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_enum (Enum item) {
		item.accept_all_children (this, false);
	}

	/**
	 * {@inheritDoc}
	 */
	public override void visit_enum_value (Api.EnumValue item) {

		if (((Vala.EnumValue) item.data).value != null) {
			SignatureBuilder signature = new SignatureBuilder ();
			InitializerBuilder ibuilder = new InitializerBuilder (signature, symbol_map);
			((Vala.EnumValue) item.data).value.accept (ibuilder);
			item.default_value = signature.get ();
		}

		item.accept_all_children (this, false);
	}
}