girparser: Add common parse_symbol_from_string method
[vala-lang.git] / vala / valamemberaccess.vala
blobe8983185b433850fd1966b6b9be648042fb1e535
1 /* valamemberaccess.vala
3 * Copyright (C) 2006-2010 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 * Represents an access to a type member in the source code.
28 public class Vala.MemberAccess : Expression {
29 /**
30 * The parent of the member.
32 public Expression? inner {
33 get {
34 return _inner;
36 set {
37 _inner = value;
38 if (_inner != null) {
39 _inner.parent_node = this;
44 /**
45 * The name of the member.
47 public string member_name { get; set; }
49 /**
50 * Pointer member access.
52 public bool pointer_member_access { get; set; }
54 /**
55 * Represents access to an instance member without an actual instance,
56 * e.g. `MyClass.an_instance_method`.
58 public bool prototype_access { get; set; }
60 /**
61 * Specifies whether the member is used for object creation.
63 public bool creation_member { get; set; }
65 /**
66 * Qualified access to global symbol.
68 public bool qualified { get; set; }
70 private Expression? _inner;
71 private List<DataType> type_argument_list = new ArrayList<DataType> ();
73 /**
74 * Creates a new member access expression.
76 * @param inner parent of the member
77 * @param member_name member name
78 * @param source_reference reference to source code
79 * @return newly created member access expression
81 public MemberAccess (Expression? inner, string member_name, SourceReference? source_reference = null) {
82 this.inner = inner;
83 this.member_name = member_name;
84 this.source_reference = source_reference;
87 public MemberAccess.simple (string member_name, SourceReference? source_reference = null) {
88 this.member_name = member_name;
89 this.source_reference = source_reference;
92 public MemberAccess.pointer (Expression inner, string member_name, SourceReference? source_reference = null) {
93 this.inner = inner;
94 this.member_name = member_name;
95 this.source_reference = source_reference;
96 pointer_member_access = true;
99 /**
100 * Appends the specified type as generic type argument.
102 * @param arg a type reference
104 public void add_type_argument (DataType arg) {
105 type_argument_list.add (arg);
106 arg.parent_node = this;
110 * Returns a copy of the list of generic type arguments.
112 * @return type argument list
114 public List<DataType> get_type_arguments () {
115 return type_argument_list;
118 public override void accept (CodeVisitor visitor) {
119 visitor.visit_member_access (this);
121 visitor.visit_expression (this);
124 public override void accept_children (CodeVisitor visitor) {
125 if (inner != null) {
126 inner.accept (visitor);
129 foreach (DataType type_arg in type_argument_list) {
130 type_arg.accept (visitor);
134 public override string to_string () {
135 if (symbol_reference == null || symbol_reference.is_instance_member ()) {
136 if (inner == null) {
137 return member_name;
138 } else {
139 return "%s.%s".printf (inner.to_string (), member_name);
141 } else {
142 // ensure to always use fully-qualified name
143 // to refer to static members
144 return symbol_reference.get_full_name ();
148 public override void replace_expression (Expression old_node, Expression new_node) {
149 if (inner == old_node) {
150 inner = new_node;
154 public override bool is_pure () {
155 // accessing property could have side-effects
156 return (inner == null || inner.is_pure ()) && !(symbol_reference is Property);
159 public override void replace_type (DataType old_type, DataType new_type) {
160 for (int i = 0; i < type_argument_list.size; i++) {
161 if (type_argument_list[i] == old_type) {
162 type_argument_list[i] = new_type;
163 return;
168 public override bool is_constant () {
169 if (symbol_reference is Constant) {
170 return true;
171 } else {
172 return false;
176 public override bool is_non_null () {
177 var c = symbol_reference as Constant;
178 if (c != null) {
179 return !c.type_reference.nullable;
180 } else {
181 return false;
185 public override bool check (CodeContext context) {
186 if (checked) {
187 return !error;
190 checked = true;
192 if (inner != null) {
193 inner.check (context);
196 foreach (DataType type_arg in type_argument_list) {
197 type_arg.check (context);
200 Symbol base_symbol = null;
201 Parameter this_parameter = null;
202 bool may_access_instance_members = false;
203 bool may_access_klass_members = false;
205 symbol_reference = null;
207 if (qualified) {
208 base_symbol = context.analyzer.root_symbol;
209 symbol_reference = context.analyzer.root_symbol.scope.lookup (member_name);
210 } else if (inner == null) {
211 if (member_name == "this") {
212 if (!context.analyzer.is_in_instance_method ()) {
213 error = true;
214 Report.error (source_reference, "This access invalid outside of instance methods");
215 return false;
219 base_symbol = context.analyzer.current_symbol;
221 // track whether method has been found to make sure that access
222 // to instance member is denied from within static lambda expressions
223 bool method_found = false;
225 var sym = context.analyzer.current_symbol;
226 while (sym != null && symbol_reference == null) {
227 if (!method_found) {
228 if (sym is CreationMethod) {
229 var cm = (CreationMethod) sym;
230 this_parameter = cm.this_parameter;
231 may_access_instance_members = true;
232 may_access_klass_members = true;
233 method_found = true;
234 } else if (sym is Property) {
235 var prop = (Property) sym;
236 this_parameter = prop.this_parameter;
237 may_access_instance_members = (prop.binding == MemberBinding.INSTANCE);
238 may_access_klass_members = (prop.binding != MemberBinding.STATIC);
239 method_found = true;
240 } else if (sym is Constructor) {
241 var c = (Constructor) sym;
242 this_parameter = c.this_parameter;
243 may_access_instance_members = (c.binding == MemberBinding.INSTANCE);
244 may_access_klass_members = true;
245 method_found = true;
246 } else if (sym is Destructor) {
247 var d = (Destructor) sym;
248 this_parameter = d.this_parameter;
249 may_access_instance_members = (d.binding == MemberBinding.INSTANCE);
250 may_access_klass_members = true;
251 method_found = true;
252 } else if (sym is Method) {
253 var m = (Method) sym;
254 this_parameter = m.this_parameter;
255 may_access_instance_members = (m.binding == MemberBinding.INSTANCE);
256 may_access_klass_members = (m.binding != MemberBinding.STATIC);
257 method_found = true;
261 symbol_reference = context.analyzer.symbol_lookup_inherited (sym, member_name);
263 if (symbol_reference == null) {
264 if (sym is TypeSymbol) {
265 // do not allow instance access to outer classes
266 this_parameter = null;
267 may_access_instance_members = false;
268 may_access_klass_members = false;
272 sym = sym.parent_symbol;
275 if (symbol_reference == null && source_reference != null) {
276 foreach (UsingDirective ns in source_reference.using_directives) {
277 var local_sym = ns.namespace_symbol.scope.lookup (member_name);
278 if (local_sym != null) {
279 if (symbol_reference != null && symbol_reference != local_sym) {
280 error = true;
281 Report.error (source_reference, "`%s' is an ambiguous reference between `%s' and `%s'".printf (member_name, symbol_reference.get_full_name (), local_sym.get_full_name ()));
282 return false;
284 symbol_reference = local_sym;
288 } else {
289 if (inner.error) {
290 /* if there was an error in the inner expression, skip this check */
291 error = true;
292 return false;
295 if (inner.value_type is PointerType) {
296 var pointer_type = inner.value_type as PointerType;
297 if (pointer_type != null && pointer_type.base_type is ValueType) {
298 // transform foo->bar to (*foo).bar
299 inner = new PointerIndirection (inner, source_reference);
300 inner.check (context);
301 pointer_member_access = false;
305 if (inner is MemberAccess) {
306 var ma = (MemberAccess) inner;
307 if (ma.prototype_access) {
308 error = true;
309 Report.error (source_reference, "Access to instance member `%s' denied".printf (inner.symbol_reference.get_full_name ()));
310 return false;
314 if (inner is MemberAccess || inner is BaseAccess) {
315 base_symbol = inner.symbol_reference;
317 if (symbol_reference == null && (base_symbol is Namespace || base_symbol is TypeSymbol)) {
318 symbol_reference = base_symbol.scope.lookup (member_name);
319 if (inner is BaseAccess) {
320 // inner expression is base access
321 // access to instance members of the base type possible
322 may_access_instance_members = true;
323 may_access_klass_members = true;
328 if (symbol_reference == null && inner.value_type != null) {
329 if (pointer_member_access) {
330 symbol_reference = inner.value_type.get_pointer_member (member_name);
331 } else {
332 if (inner.value_type.data_type != null) {
333 base_symbol = inner.value_type.data_type;
335 symbol_reference = inner.value_type.get_member (member_name);
337 if (symbol_reference != null) {
338 // inner expression is variable, field, or parameter
339 // access to instance members of the corresponding type possible
340 may_access_instance_members = true;
341 may_access_klass_members = true;
345 if (symbol_reference == null && inner.value_type != null && inner.value_type.is_dynamic) {
346 // allow late bound members for dynamic types
347 var dynamic_object_type = (ObjectType) inner.value_type;
348 if (parent_node is MethodCall) {
349 var invoc = (MethodCall) parent_node;
350 if (invoc.call == this) {
351 // dynamic method
352 DataType ret_type;
353 if (invoc.target_type != null) {
354 ret_type = invoc.target_type.copy ();
355 ret_type.value_owned = true;
356 } else if (invoc.parent_node is ExpressionStatement) {
357 ret_type = new VoidType ();
358 } else {
359 // expect dynamic object of the same type
360 ret_type = inner.value_type.copy ();
362 var m = new DynamicMethod (inner.value_type, member_name, ret_type, source_reference);
363 m.invocation = invoc;
364 var err = new ErrorType (null, null);
365 err.dynamic_error = true;
366 m.add_error_type (err);
367 m.access = SymbolAccessibility.PUBLIC;
368 m.add_parameter (new Parameter.with_ellipsis ());
369 dynamic_object_type.type_symbol.scope.add (null, m);
370 symbol_reference = m;
372 } else if (parent_node is Assignment) {
373 var a = (Assignment) parent_node;
374 if (a.left == this
375 && (a.operator == AssignmentOperator.ADD
376 || a.operator == AssignmentOperator.SUB)) {
377 // dynamic signal
378 var s = new DynamicSignal (inner.value_type, member_name, new VoidType (), source_reference);
379 s.handler = a.right;
380 s.access = SymbolAccessibility.PUBLIC;
381 dynamic_object_type.type_symbol.scope.add (null, s);
382 symbol_reference = s;
383 } else if (a.left == this) {
384 // dynamic property assignment
385 var prop = new DynamicProperty (inner.value_type, member_name, source_reference);
386 prop.access = SymbolAccessibility.PUBLIC;
387 prop.set_accessor = new PropertyAccessor (false, true, false, null, null, prop.source_reference);
388 prop.owner = inner.value_type.data_type.scope;
389 dynamic_object_type.type_symbol.scope.add (null, prop);
390 symbol_reference = prop;
392 } else if (parent_node is MemberAccess && inner is MemberAccess && parent_node.parent_node is MethodCall) {
393 var ma = (MemberAccess) parent_node;
394 if (ma.member_name == "connect" || ma.member_name == "connect_after") {
395 // dynamic signal
396 var s = new DynamicSignal (inner.value_type, member_name, new VoidType (), source_reference);
397 var mcall = (MethodCall) parent_node.parent_node;
398 // the first argument is the handler
399 if (mcall.get_argument_list().size > 0) {
400 s.handler = mcall.get_argument_list()[0];
402 s.access = SymbolAccessibility.PUBLIC;
403 dynamic_object_type.type_symbol.scope.add (null, s);
404 symbol_reference = s;
407 if (symbol_reference == null) {
408 // dynamic property read access
409 var prop = new DynamicProperty (inner.value_type, member_name, source_reference);
410 if (target_type != null) {
411 prop.property_type = target_type;
412 } else {
413 // expect dynamic object of the same type
414 prop.property_type = inner.value_type.copy ();
416 prop.access = SymbolAccessibility.PUBLIC;
417 prop.get_accessor = new PropertyAccessor (true, false, false, prop.property_type.copy (), null, prop.source_reference);
418 prop.owner = inner.value_type.data_type.scope;
419 dynamic_object_type.type_symbol.scope.add (null, prop);
420 symbol_reference = prop;
422 if (symbol_reference != null) {
423 may_access_instance_members = true;
424 may_access_klass_members = true;
429 if (symbol_reference == null) {
430 error = true;
432 string base_type_name = "(null)";
433 if (inner != null && inner.value_type != null) {
434 base_type_name = inner.value_type.to_string ();
435 } else if (base_symbol != null) {
436 base_type_name = base_symbol.get_full_name ();
439 Report.error (source_reference, "The name `%s' does not exist in the context of `%s'".printf (member_name, base_type_name));
440 return false;
443 var member = symbol_reference;
444 var access = SymbolAccessibility.PUBLIC;
445 bool instance = false;
446 bool klass = false;
447 bool generics = false;
449 if (!member.check (context)) {
450 return false;
453 if (member is LocalVariable) {
454 var local = (LocalVariable) member;
455 var block = local.parent_symbol as Block;
456 if (block != null && context.analyzer.find_parent_method_or_property_accessor (block) != context.analyzer.current_method_or_property_accessor) {
457 // mark all methods between current method and the captured
458 // block as closures (to support nested closures)
459 Symbol sym = context.analyzer.current_method_or_property_accessor;
460 while (sym != block) {
461 var method = sym as Method;
462 if (method != null) {
463 method.closure = true;
464 // consider captured variables as used
465 // as we require captured variables to be initialized
466 method.add_captured_variable (local);
468 sym = sym.parent_symbol;
471 local.captured = true;
472 block.captured = true;
474 } else if (member is Parameter) {
475 var param = (Parameter) member;
476 var m = param.parent_symbol as Method;
477 if (m != null && m != context.analyzer.current_method_or_property_accessor && param != m.this_parameter) {
478 // mark all methods between current method and the captured
479 // parameter as closures (to support nested closures)
480 Symbol sym = context.analyzer.current_method_or_property_accessor;
481 while (sym != m) {
482 var method = sym as Method;
483 if (method != null) {
484 method.closure = true;
486 sym = sym.parent_symbol;
489 param.captured = true;
490 m.body.captured = true;
492 if (param.direction != ParameterDirection.IN) {
493 error = true;
494 Report.error (source_reference, "Cannot capture reference or output parameter `%s'".printf (param.get_full_name ()));
496 } else {
497 var acc = param.parent_symbol.parent_symbol as PropertyAccessor;
498 if (acc != null && acc != context.analyzer.current_method_or_property_accessor && param != acc.prop.this_parameter) {
499 // mark all methods between current method and the captured
500 // parameter as closures (to support nested closures)
501 Symbol sym = context.analyzer.current_method_or_property_accessor;
502 while (sym != m) {
503 var method = sym as Method;
504 if (method != null) {
505 method.closure = true;
507 sym = sym.parent_symbol;
510 param.captured = true;
511 acc.body.captured = true;
514 } else if (member is Field) {
515 var f = (Field) member;
516 access = f.access;
517 instance = (f.binding == MemberBinding.INSTANCE);
518 klass = (f.binding == MemberBinding.CLASS);
520 // do not allow access to fields of generic types
521 // if instance type does not specify type arguments
522 if (f.variable_type is GenericType) {
523 generics = true;
525 } else if (member is Constant) {
526 var c = (Constant) member;
527 access = c.access;
528 } else if (member is Method) {
529 var m = (Method) member;
530 if (m.is_async_callback) {
531 // ensure to use right callback method for virtual/abstract async methods
532 // and also for lambda expressions within async methods
533 var async_method = context.analyzer.current_async_method;
535 if (async_method != context.analyzer.current_method) {
536 Symbol sym = context.analyzer.current_method;
537 while (sym != async_method) {
538 var method = sym as Method;
539 if (method != null) {
540 method.closure = true;
542 sym = sym.parent_symbol;
544 async_method.body.captured = true;
547 m = async_method.get_callback_method ();
548 symbol_reference = m;
549 member = symbol_reference;
550 } else if (m.base_method != null) {
551 // refer to base method to inherit default arguments
552 m = m.base_method;
554 if (m.signal_reference != null) {
555 // method is class/default handler for a signal
556 // let signal deal with member access
557 symbol_reference = m.signal_reference;
558 } else {
559 symbol_reference = m;
562 member = symbol_reference;
563 } else if (m.base_interface_method != null) {
564 // refer to base method to inherit default arguments
565 m = m.base_interface_method;
566 symbol_reference = m;
567 member = symbol_reference;
569 access = m.access;
570 if (!(m is CreationMethod)) {
571 instance = (m.binding == MemberBinding.INSTANCE);
573 klass = (m.binding == MemberBinding.CLASS);
575 // do not allow access to methods using generic type parameters
576 // if instance type does not specify type arguments
577 foreach (var param in m.get_parameters ()) {
578 var generic_type = param.variable_type as GenericType;
579 if (generic_type != null && generic_type.type_parameter.parent_symbol is TypeSymbol) {
580 generics = true;
581 break;
584 var generic_type = m.return_type as GenericType;
585 if (generic_type != null && generic_type.type_parameter.parent_symbol is TypeSymbol) {
586 generics = true;
588 } else if (member is Property) {
589 var prop = (Property) member;
590 if (!prop.check (context)) {
591 error = true;
592 return false;
594 if (prop.base_property != null) {
595 // refer to base property
596 prop = prop.base_property;
597 symbol_reference = prop;
598 member = symbol_reference;
599 } else if (prop.base_interface_property != null) {
600 // refer to base property
601 prop = prop.base_interface_property;
602 symbol_reference = prop;
603 member = symbol_reference;
605 access = prop.access;
606 if (lvalue) {
607 if (prop.set_accessor == null) {
608 error = true;
609 Report.error (source_reference, "Property `%s' is read-only".printf (prop.get_full_name ()));
610 return false;
612 if (prop.access == SymbolAccessibility.PUBLIC) {
613 access = prop.set_accessor.access;
614 } else if (prop.access == SymbolAccessibility.PROTECTED
615 && prop.set_accessor.access != SymbolAccessibility.PUBLIC) {
616 access = prop.set_accessor.access;
618 } else {
619 if (prop.get_accessor == null) {
620 error = true;
621 Report.error (source_reference, "Property `%s' is write-only".printf (prop.get_full_name ()));
622 return false;
624 if (prop.access == SymbolAccessibility.PUBLIC) {
625 access = prop.get_accessor.access;
626 } else if (prop.access == SymbolAccessibility.PROTECTED
627 && prop.get_accessor.access != SymbolAccessibility.PUBLIC) {
628 access = prop.get_accessor.access;
631 instance = (prop.binding == MemberBinding.INSTANCE);
633 // do not allow access to properties of generic types
634 // if instance type does not specify type arguments
635 if (prop.property_type is GenericType) {
636 generics = true;
638 } else if (member is Signal) {
639 instance = true;
640 access = member.access;
643 member.used = true;
644 member.check_deprecated (source_reference);
646 if (access == SymbolAccessibility.PROTECTED) {
647 var target_type = (TypeSymbol) member.parent_symbol;
649 bool in_subtype = false;
650 for (Symbol this_symbol = context.analyzer.current_symbol; this_symbol != null; this_symbol = this_symbol.parent_symbol) {
651 if (this_symbol == target_type) {
652 // required for interfaces with non-abstract methods
653 // accessing protected interface members
654 in_subtype = true;
655 break;
658 var cl = this_symbol as Class;
659 if (cl != null && cl.is_subtype_of (target_type)) {
660 in_subtype = true;
661 break;
665 if (!in_subtype) {
666 error = true;
667 Report.error (source_reference, "Access to protected member `%s' denied".printf (member.get_full_name ()));
668 return false;
670 } else if (access == SymbolAccessibility.PRIVATE) {
671 var target_type = member.parent_symbol;
673 bool in_target_type = false;
674 for (Symbol this_symbol = context.analyzer.current_symbol; this_symbol != null; this_symbol = this_symbol.parent_symbol) {
675 if (target_type == this_symbol) {
676 in_target_type = true;
677 break;
681 if (!in_target_type) {
682 error = true;
683 Report.error (source_reference, "Access to private member `%s' denied".printf (member.get_full_name ()));
684 return false;
688 if (generics && inner != null) {
689 var instance_type = inner.value_type;
690 var pointer_type = inner.value_type as PointerType;
691 if (pointer_type != null) {
692 instance_type = pointer_type.base_type;
695 // instance type might be a subtype of the parent symbol of the member
696 // that subtype might not be generic, so do not report an error in that case
697 var object_type = instance_type as ObjectType;
698 if (object_type != null && object_type.type_symbol.get_type_parameters ().size > 0 &&
699 instance_type.get_type_arguments ().size == 0) {
700 error = true;
701 Report.error (inner.source_reference, "missing generic type arguments");
702 return false;
706 if ((instance && !may_access_instance_members) ||
707 (klass && !may_access_klass_members)) {
708 prototype_access = true;
710 if (symbol_reference is Method) {
711 // also set static type for prototype access
712 // required when using instance methods as delegates in constants
713 // TODO replace by MethodPrototype
714 value_type = context.analyzer.get_value_type_for_symbol (symbol_reference, lvalue);
715 } else if (symbol_reference is Field) {
716 value_type = new FieldPrototype ((Field) symbol_reference);
717 } else {
718 value_type = new InvalidType ();
721 if (target_type != null) {
722 value_type.value_owned = target_type.value_owned;
724 } else {
725 // implicit this access
726 if (instance && inner == null) {
727 inner = new MemberAccess (null, "this", source_reference);
728 inner.value_type = this_parameter.variable_type.copy ();
729 inner.symbol_reference = this_parameter;
732 if (context.experimental_non_null && instance && inner.value_type.nullable &&
733 !(inner.value_type is PointerType) && !(inner.value_type is GenericType)) {
734 Report.error (source_reference, "Access to instance member `%s' from nullable reference denied".printf (symbol_reference.get_full_name ()));
737 formal_value_type = context.analyzer.get_value_type_for_symbol (symbol_reference, lvalue);
738 if (inner != null && formal_value_type != null) {
739 value_type = formal_value_type.get_actual_type (inner.value_type, null, this);
740 } else {
741 value_type = formal_value_type;
744 if (symbol_reference is Method) {
745 var m = (Method) symbol_reference;
747 if (target_type != null) {
748 value_type.value_owned = target_type.value_owned;
751 Method base_method;
752 if (m.base_method != null) {
753 base_method = m.base_method;
754 } else if (m.base_interface_method != null) {
755 base_method = m.base_interface_method;
756 } else {
757 base_method = m;
760 if (instance && base_method.parent_symbol is TypeSymbol) {
761 inner.target_type = context.analyzer.get_data_type_for_symbol ((TypeSymbol) base_method.parent_symbol);
763 } else if (symbol_reference is Property) {
764 var prop = (Property) symbol_reference;
766 Property base_property;
767 if (prop.base_property != null) {
768 base_property = prop.base_property;
769 } else if (prop.base_interface_property != null) {
770 base_property = prop.base_interface_property;
771 } else {
772 base_property = prop;
775 if (instance && base_property.parent_symbol != null) {
776 inner.target_type = context.analyzer.get_data_type_for_symbol ((TypeSymbol) base_property.parent_symbol);
778 } else if ((symbol_reference is Field
779 || symbol_reference is Signal)
780 && instance && symbol_reference.parent_symbol != null) {
781 inner.target_type = context.analyzer.get_data_type_for_symbol ((TypeSymbol) symbol_reference.parent_symbol);
785 return !error;
788 public override void emit (CodeGenerator codegen) {
789 if (inner != null) {
790 inner.emit (codegen);
793 codegen.visit_member_access (this);
795 codegen.visit_expression (this);
798 public override void get_defined_variables (Collection<LocalVariable> collection) {
799 if (inner != null) {
800 inner.get_defined_variables (collection);
804 public override void get_used_variables (Collection<LocalVariable> collection) {
805 if (inner != null) {
806 inner.get_used_variables (collection);
808 var local = symbol_reference as LocalVariable;
809 if (local != null) {
810 collection.add (local);