Release 0.7.8
[vala-lang.git] / vala / valasymbolresolver.vala
blobde3169633a64d4edc7cb87379d44095f7f10ea4e
1 /* valasymbolresolver.vala
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
20 * Author:
21 * Jürg Billeter <j@bitron.ch>
22 * Raffaele Sandrini <raffaele@sandrini.ch>
25 using GLib;
27 /**
28 * Code visitor resolving symbol names.
30 public class Vala.SymbolResolver : CodeVisitor {
31 CodeContext context;
32 Symbol root_symbol;
33 Scope current_scope;
35 /**
36 * Resolve symbol names in the specified code context.
38 * @param context a code context
40 public void resolve (CodeContext context) {
41 this.context = context;
42 root_symbol = context.root;
44 context.root.accept (this);
47 public override void visit_namespace (Namespace ns) {
48 var old_scope = current_scope;
49 current_scope = ns.scope;
51 ns.accept_children (this);
53 current_scope = old_scope;
56 public override void visit_class (Class cl) {
57 current_scope = cl.scope;
59 cl.accept_children (this);
61 foreach (DataType type in cl.get_base_types ()) {
62 if (type.data_type is Class) {
63 if (cl.base_class != null) {
64 cl.error = true;
65 Report.error (type.source_reference, "%s: Classes cannot have multiple base classes (`%s' and `%s')".printf (cl.get_full_name (), cl.base_class.get_full_name (), type.data_type.get_full_name ()));
66 return;
68 cl.base_class = (Class) type.data_type;
69 if (cl.base_class.is_subtype_of (cl)) {
70 cl.error = true;
71 Report.error (type.source_reference, "Base class cycle (`%s' and `%s')".printf (cl.get_full_name (), cl.base_class.get_full_name ()));
72 return;
77 current_scope = current_scope.parent_scope;
80 public override void visit_struct (Struct st) {
81 current_scope = st.scope;
83 st.accept_children (this);
85 if (st.base_type != null) {
86 var base_type = st.base_struct;
87 if (base_type != null) {
88 if (base_type.is_subtype_of (st)) {
89 st.error = true;
90 Report.error (base_type.source_reference, "Base struct cycle (`%s' and `%s')".printf (st.get_full_name (), base_type.get_full_name ()));
91 return;
96 current_scope = current_scope.parent_scope;
99 public override void visit_interface (Interface iface) {
100 current_scope = iface.scope;
102 iface.accept_children (this);
104 foreach (DataType type in iface.get_prerequisites ()) {
105 if (type.data_type != null && type.data_type.is_subtype_of (iface)) {
106 iface.error = true;
107 Report.error (type.source_reference, "Prerequisite cycle (`%s' and `%s')".printf (iface.get_full_name (), type.data_type.get_full_name ()));
108 return;
112 current_scope = current_scope.parent_scope;
115 public override void visit_enum (Enum en) {
116 current_scope = en.scope;
118 en.accept_children (this);
120 current_scope = current_scope.parent_scope;
123 public override void visit_error_domain (ErrorDomain ed) {
124 current_scope = ed.scope;
126 ed.accept_children (this);
128 current_scope = current_scope.parent_scope;
131 public override void visit_delegate (Delegate cb) {
132 current_scope = cb.scope;
134 cb.accept_children (this);
136 current_scope = current_scope.parent_scope;
139 public override void visit_constant (Constant c) {
140 current_scope = c.scope;
142 c.accept_children (this);
145 public override void visit_field (Field f) {
146 current_scope = f.scope;
148 f.accept_children (this);
150 current_scope = current_scope.parent_scope;
153 public override void visit_method (Method m) {
154 current_scope = m.scope;
156 m.accept_children (this);
158 current_scope = current_scope.parent_scope;
161 public override void visit_creation_method (CreationMethod m) {
162 m.accept_children (this);
165 public override void visit_formal_parameter (FormalParameter p) {
166 p.accept_children (this);
169 public override void visit_property (Property prop) {
170 prop.accept_children (this);
173 public override void visit_property_accessor (PropertyAccessor acc) {
174 acc.accept_children (this);
177 public override void visit_signal (Signal sig) {
178 sig.accept_children (this);
181 public override void visit_constructor (Constructor c) {
182 c.accept_children (this);
185 public override void visit_destructor (Destructor d) {
186 d.accept_children (this);
189 public override void visit_block (Block b) {
190 b.accept_children (this);
193 public override void visit_using_directive (UsingDirective ns) {
194 var unresolved_symbol = ns.namespace_symbol as UnresolvedSymbol;
195 if (unresolved_symbol != null) {
196 ns.namespace_symbol = resolve_symbol (unresolved_symbol);
197 if (ns.namespace_symbol == null) {
198 ns.error = true;
199 Report.error (ns.source_reference, "The namespace name `%s' could not be found".printf (unresolved_symbol.to_string ()));
200 return;
205 private Symbol? resolve_symbol (UnresolvedSymbol unresolved_symbol) {
206 if (unresolved_symbol.qualified) {
207 // qualified access to global symbol
208 return root_symbol.scope.lookup (unresolved_symbol.name);
209 } else if (unresolved_symbol.inner == null) {
210 Symbol sym = null;
211 Scope scope = current_scope;
212 while (sym == null && scope != null) {
213 sym = scope.lookup (unresolved_symbol.name);
215 // only look for types and type containers
216 if (!(sym is Namespace || sym is TypeSymbol || sym is TypeParameter)) {
217 sym = null;
220 scope = scope.parent_scope;
222 if (sym == null && unresolved_symbol.source_reference != null) {
223 foreach (UsingDirective ns in unresolved_symbol.source_reference.using_directives) {
224 if (ns.error || ns.namespace_symbol is UnresolvedSymbol) {
225 continue;
228 var local_sym = ns.namespace_symbol.scope.lookup (unresolved_symbol.name);
230 // only look for types and type containers
231 if (!(local_sym is Namespace || local_sym is TypeSymbol || sym is TypeParameter)) {
232 local_sym = null;
235 if (local_sym != null) {
236 if (sym != null && sym != local_sym) {
237 unresolved_symbol.error = true;
238 Report.error (unresolved_symbol.source_reference, "`%s' is an ambiguous reference between `%s' and `%s'".printf (unresolved_symbol.name, sym.get_full_name (), local_sym.get_full_name ()));
239 return null;
241 sym = local_sym;
245 return sym;
246 } else {
247 var parent_symbol = resolve_symbol (unresolved_symbol.inner);
248 if (parent_symbol == null) {
249 unresolved_symbol.error = true;
250 Report.error (unresolved_symbol.inner.source_reference, "The symbol `%s' could not be found".printf (unresolved_symbol.inner.name));
251 return null;
254 return parent_symbol.scope.lookup (unresolved_symbol.name);
258 private DataType resolve_type (UnresolvedType unresolved_type) {
259 DataType type = null;
261 // still required for vapigen
262 if (unresolved_type.unresolved_symbol.name == "void") {
263 return new VoidType ();
266 var sym = resolve_symbol (unresolved_type.unresolved_symbol);
267 if (sym == null) {
268 // don't report same error twice
269 if (!unresolved_type.unresolved_symbol.error) {
270 Report.error (unresolved_type.source_reference, "The type name `%s' could not be found".printf (unresolved_type.unresolved_symbol.to_string ()));
272 return new InvalidType ();
275 if (sym is TypeParameter) {
276 type = new GenericType ((TypeParameter) sym);
277 } else if (sym is TypeSymbol) {
278 if (sym is Delegate) {
279 type = new DelegateType ((Delegate) sym);
280 } else if (sym is Class) {
281 var cl = (Class) sym;
282 if (cl.is_error_base) {
283 type = new ErrorType (null, null, unresolved_type.source_reference);
284 } else {
285 type = new ObjectType (cl);
287 } else if (sym is Interface) {
288 type = new ObjectType ((Interface) sym);
289 } else if (sym is Struct) {
290 var st = (Struct) sym;
291 // attributes are not processed yet, access them directly
292 if (st.get_attribute ("BooleanType") != null) {
293 type = new BooleanType (st);
294 } else if (st.get_attribute ("IntegerType") != null) {
295 type = new IntegerType (st);
296 } else if (st.get_attribute ("FloatingType") != null) {
297 type = new FloatingType (st);
298 } else {
299 type = new StructValueType (st);
301 } else if (sym is Enum) {
302 type = new EnumValueType ((Enum) sym);
303 } else if (sym is ErrorDomain) {
304 type = new ErrorType ((ErrorDomain) sym, null, unresolved_type.source_reference);
305 } else if (sym is ErrorCode) {
306 type = new ErrorType ((ErrorDomain) sym.parent_symbol, (ErrorCode) sym, unresolved_type.source_reference);
307 } else {
308 Report.error (unresolved_type.source_reference, "internal error: `%s' is not a supported type".printf (sym.get_full_name ()));
309 return new InvalidType ();
311 } else {
312 Report.error (unresolved_type.source_reference, "`%s' is not a type".printf (sym.get_full_name ()));
313 return new InvalidType ();
316 type.source_reference = unresolved_type.source_reference;
317 type.value_owned = unresolved_type.value_owned;
319 if (type is GenericType) {
320 // type parameters are always considered nullable
321 // actual type argument may or may not be nullable
322 type.nullable = true;
323 } else {
324 type.nullable = unresolved_type.nullable;
327 type.is_dynamic = unresolved_type.is_dynamic;
328 foreach (DataType type_arg in unresolved_type.get_type_arguments ()) {
329 type.add_type_argument (type_arg);
332 return type;
335 public override void visit_data_type (DataType data_type) {
336 data_type.accept_children (this);
338 if (!(data_type is UnresolvedType)) {
339 return;
342 var unresolved_type = (UnresolvedType) data_type;
344 unresolved_type.parent_node.replace_type (unresolved_type, resolve_type (unresolved_type));
347 public override void visit_declaration_statement (DeclarationStatement stmt) {
348 stmt.accept_children (this);
351 public override void visit_local_variable (LocalVariable local) {
352 local.accept_children (this);
353 if (!context.experimental_non_null) {
354 // local reference variables are considered nullable
355 // except when using experimental non-null enhancements
356 if (local.variable_type is ReferenceType) {
357 var array_type = local.variable_type as ArrayType;
358 if (array_type != null && array_type.fixed_length) {
359 // local fixed length arrays are not nullable
360 } else {
361 local.variable_type.nullable = true;
367 public override void visit_initializer_list (InitializerList list) {
368 list.accept_children (this);
371 public override void visit_expression_statement (ExpressionStatement stmt) {
372 stmt.accept_children (this);
375 public override void visit_if_statement (IfStatement stmt) {
376 stmt.accept_children (this);
379 public override void visit_switch_statement (SwitchStatement stmt) {
380 stmt.accept_children (this);
383 public override void visit_switch_section (SwitchSection section) {
384 section.accept_children (this);
387 public override void visit_switch_label (SwitchLabel label) {
388 label.accept_children (this);
391 public override void visit_loop (Loop stmt) {
392 stmt.accept_children (this);
395 public override void visit_while_statement (WhileStatement stmt) {
396 stmt.accept_children (this);
399 public override void visit_do_statement (DoStatement stmt) {
400 stmt.accept_children (this);
403 public override void visit_for_statement (ForStatement stmt) {
404 stmt.accept_children (this);
407 public override void visit_foreach_statement (ForeachStatement stmt) {
408 stmt.accept_children (this);
411 public override void visit_return_statement (ReturnStatement stmt) {
412 stmt.accept_children (this);
415 public override void visit_yield_statement (YieldStatement stmt) {
416 stmt.accept_children (this);
419 public override void visit_throw_statement (ThrowStatement stmt) {
420 stmt.accept_children (this);
423 public override void visit_try_statement (TryStatement stmt) {
424 stmt.accept_children (this);
427 public override void visit_catch_clause (CatchClause clause) {
428 clause.accept_children (this);
431 public override void visit_array_creation_expression (ArrayCreationExpression e) {
432 e.accept_children (this);
435 public override void visit_member_access (MemberAccess expr) {
436 expr.accept_children (this);
439 public override void visit_method_call (MethodCall expr) {
440 expr.accept_children (this);
443 public override void visit_element_access (ElementAccess expr) {
444 expr.accept_children (this);
447 public override void visit_object_creation_expression (ObjectCreationExpression expr) {
448 expr.accept_children (this);
451 public override void visit_unary_expression (UnaryExpression expr) {
452 expr.accept_children (this);
455 public override void visit_reference_transfer_expression (ReferenceTransferExpression expr) {
456 expr.accept_children (this);
459 public override void visit_binary_expression (BinaryExpression expr) {
460 expr.accept_children (this);
463 public override void visit_lambda_expression (LambdaExpression l) {
464 l.accept_children (this);
467 public override void visit_assignment (Assignment a) {
468 a.accept_children (this);