Keep order of parsed members (required by MD) to be more compatible with csc
[mono-project.git] / mcs / mcs / membercache.cs
blob2b4bbf7e7b3bd74890a6ff3e7ff116031a3286aa
1 //
2 // membercache.cs: A container for all member lookups
3 //
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 // Marek Safar (marek.safar@gmail.com)
6 //
7 // Dual licensed under the terms of the MIT X11 or GNU GPL
8 //
9 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
10 // Copyright 2004-2010 Novell, Inc
11 // Copyright 2011 Xamarin Inc
15 using System;
16 using System.Collections.Generic;
18 namespace Mono.CSharp {
20 [Flags]
21 public enum MemberKind
23 Constructor = 1,
24 Event = 1 << 1,
25 Field = 1 << 2,
26 Method = 1 << 3,
27 Property = 1 << 4,
28 Indexer = 1 << 5,
29 Operator = 1 << 6,
30 Destructor = 1 << 7,
32 Class = 1 << 11,
33 Struct = 1 << 12,
34 Delegate = 1 << 13,
35 Enum = 1 << 14,
36 Interface = 1 << 15,
37 TypeParameter = 1 << 16,
39 ArrayType = 1 << 19,
40 PointerType = 1 << 20,
41 InternalCompilerType = 1 << 21,
42 MissingType = 1 << 22,
43 Void = 1 << 23,
44 Namespace = 1 << 24,
46 NestedMask = Class | Struct | Delegate | Enum | Interface,
47 GenericMask = Method | Class | Struct | Delegate | Interface,
48 MaskType = Constructor | Event | Field | Method | Property | Indexer | Operator | Destructor | NestedMask
51 [Flags]
52 public enum BindingRestriction
54 None = 0,
56 // Inspect only queried type members
57 DeclaredOnly = 1 << 1,
59 // Exclude static
60 InstanceOnly = 1 << 2,
62 NoAccessors = 1 << 3,
64 // Member has to be override
65 OverrideOnly = 1 << 4
68 public struct MemberFilter : IEquatable<MemberSpec>
70 public readonly string Name;
71 public readonly MemberKind Kind;
72 public readonly AParametersCollection Parameters;
73 public readonly TypeSpec MemberType;
74 public readonly int Arity; // -1 to ignore the check
76 private MemberFilter (string name, MemberKind kind)
78 Name = name;
79 Kind = kind;
80 Parameters = null;
81 MemberType = null;
82 Arity = -1;
85 public MemberFilter (MethodSpec m)
87 Name = m.Name;
88 Kind = MemberKind.Method;
89 Parameters = m.Parameters;
90 MemberType = m.ReturnType;
91 Arity = m.Arity;
94 public MemberFilter (string name, int arity, MemberKind kind, AParametersCollection param, TypeSpec type)
96 Name = name;
97 Kind = kind;
98 Parameters = param;
99 MemberType = type;
100 this.Arity = arity;
103 public static MemberFilter Constructor (AParametersCollection param)
105 return new MemberFilter (Mono.CSharp.Constructor.ConstructorName, 0, MemberKind.Constructor, param, null);
108 public static MemberFilter Property (string name, TypeSpec type)
110 return new MemberFilter (name, 0, MemberKind.Property, null, type);
113 public static MemberFilter Field (string name, TypeSpec type)
115 return new MemberFilter (name, 0, MemberKind.Field, null, type);
118 public static MemberFilter Method (string name, int arity, AParametersCollection param, TypeSpec type)
120 return new MemberFilter (name, arity, MemberKind.Method, param, type);
123 #region IEquatable<MemberSpec> Members
125 public bool Equals (MemberSpec other)
127 // Is the member of the correct type ?
128 // TODO: Isn't this redundant ?
129 if ((other.Kind & Kind & MemberKind.MaskType) == 0)
130 return false;
132 // Check arity when not disabled
133 if (Arity >= 0 && Arity != other.Arity)
134 return false;
136 if (Parameters != null) {
137 if (other is IParametersMember) {
138 var other_param = ((IParametersMember) other).Parameters;
139 if (!TypeSpecComparer.Override.IsEqual (Parameters, other_param))
140 return false;
141 } else {
142 return false;
146 if (MemberType != null) {
147 if (other is IInterfaceMemberSpec) {
148 var other_type = ((IInterfaceMemberSpec) other).MemberType;
149 if (!TypeSpecComparer.Override.IsEqual (other_type, MemberType))
150 return false;
151 } else {
152 return false;
156 return true;
159 #endregion
163 // The MemberCache is the main members container used by compiler. It contains
164 // all members imported or defined during compilation using on demand filling
165 // process. Inflated containers are also using MemberCache to make inflated
166 // members look like normal definition.
168 // All of the methods are performance and memory sensitive as the MemberCache
169 // is the underlying engine of all member based operations.
171 public class MemberCache
173 [Flags]
174 enum StateFlags
176 HasConversionOperator = 1 << 1,
177 HasUserOperator = 1 << 2
180 readonly Dictionary<string, IList<MemberSpec>> member_hash;
181 Dictionary<string, MemberSpec[]> locase_members;
182 IList<MethodSpec> missing_abstract;
183 StateFlags state; // TODO: Move to TypeSpec or ITypeDefinition
185 public static readonly string IndexerNameAlias = "<this>";
187 public static readonly MemberCache Empty = new MemberCache (0);
189 public MemberCache ()
190 : this (16)
194 public MemberCache (int capacity)
196 member_hash = new Dictionary<string, IList<MemberSpec>> (capacity);
199 public MemberCache (MemberCache cache)
200 : this (cache.member_hash.Count)
202 this.state = cache.state;
206 // Creates a new MemberCache for the given `container'.
208 public MemberCache (TypeContainer container)
209 : this () // TODO: Optimize the size
214 // For cases where we need to union cache members
216 public void AddBaseType (TypeSpec baseType)
218 var cache = baseType.MemberCache;
220 IList<MemberSpec> list;
221 foreach (var entry in cache.member_hash) {
222 if (!member_hash.TryGetValue (entry.Key, out list)) {
223 if (entry.Value.Count == 1) {
224 list = entry.Value;
225 } else {
226 list = new List<MemberSpec> (entry.Value);
229 member_hash.Add (entry.Key, list);
230 continue;
233 foreach (var ce in entry.Value) {
234 if (list.Contains (ce))
235 continue;
237 if (list is MemberSpec[]) {
238 list = new List<MemberSpec> () { list [0] };
239 member_hash[entry.Key] = list;
242 list.Add (ce);
248 // Member-cache does not contain base members but it does
249 // contain all base interface members, so the Lookup code
250 // can use simple inheritance rules.
252 public void AddInterface (TypeSpec iface)
254 var cache = iface.MemberCache;
256 IList<MemberSpec> list;
257 foreach (var entry in cache.member_hash) {
258 if (!member_hash.TryGetValue (entry.Key, out list)) {
259 if (entry.Value.Count == 1) {
260 list = entry.Value;
261 } else {
262 list = new List<MemberSpec> (entry.Value);
265 member_hash.Add (entry.Key, list);
266 continue;
269 foreach (var ce in entry.Value) {
270 if (list.Contains (ce))
271 continue;
273 if (AddInterfaceMember (ce, ref list))
274 member_hash[entry.Key] = list;
278 // Add also all base interfaces
279 if (iface.Interfaces != null) {
280 foreach (var base_iface in iface.Interfaces)
281 AddInterface (base_iface);
285 public void AddMember (InterfaceMemberBase imb, string exlicitName, MemberSpec ms)
287 // Explicit names cannot be looked-up but can be used for
288 // collision checking (no name mangling needed)
289 if (imb.IsExplicitImpl)
290 AddMember (exlicitName, ms, false);
291 else
292 AddMember (ms);
296 // Add non-explicit member to member cache
298 public void AddMember (MemberSpec ms)
300 AddMember (GetLookupName (ms), ms, false);
303 void AddMember (string name, MemberSpec member, bool removeHiddenMembers)
305 if (member.Kind == MemberKind.Operator) {
306 var dt = member.DeclaringType;
309 // Some core types have user operators but they cannot be used like normal
310 // user operators as they are predefined and therefore having different
311 // rules (e.g. binary operators) by not setting the flag we hide them for
312 // user conversions
314 if (!BuiltinTypeSpec.IsPrimitiveType (dt)) {
315 switch (dt.BuiltinType) {
316 case BuiltinTypeSpec.Type.String:
317 case BuiltinTypeSpec.Type.Delegate:
318 case BuiltinTypeSpec.Type.MulticastDelegate:
319 break;
320 default:
321 if (name == Operator.GetMetadataName (Operator.OpType.Implicit) || name == Operator.GetMetadataName (Operator.OpType.Explicit)) {
322 state |= StateFlags.HasConversionOperator;
323 } else {
324 state |= StateFlags.HasUserOperator;
327 break;
332 IList<MemberSpec> list;
333 if (!member_hash.TryGetValue (name, out list)) {
334 member_hash.Add (name, new MemberSpec[] { member });
335 return;
338 if (removeHiddenMembers && member.DeclaringType.IsInterface) {
339 if (AddInterfaceMember (member, ref list))
340 member_hash[name] = list;
341 } else {
342 if (list.Count == 1) {
343 list = new List<MemberSpec> () { list[0] };
344 member_hash[name] = list;
347 list.Add (member);
351 public void AddMemberImported (MemberSpec ms)
353 AddMember (GetLookupName (ms), ms, true);
357 // Ignores any base interface member which can be hidden
358 // by this interface
360 static bool AddInterfaceMember (MemberSpec member, ref IList<MemberSpec> existing)
362 var member_param = member is IParametersMember ? ((IParametersMember) member).Parameters : ParametersCompiled.EmptyReadOnlyParameters;
365 // interface IA : IB { int Prop { set; } }
366 // interface IB { bool Prop { get; } }
368 // IB.Prop is never accessible from IA interface
370 for (int i = 0; i < existing.Count; ++i) {
371 var entry = existing[i];
373 if (entry.Arity != member.Arity)
374 continue;
376 if (entry is IParametersMember) {
377 var entry_param = ((IParametersMember) entry).Parameters;
378 if (!TypeSpecComparer.Override.IsEqual (entry_param, member_param))
379 continue;
382 if (member.DeclaringType.ImplementsInterface (entry.DeclaringType, false)) {
383 if (existing.Count == 1) {
384 existing = new MemberSpec[] { member };
385 return true;
388 existing.RemoveAt (i--);
389 continue;
392 if ((entry.DeclaringType == member.DeclaringType && entry.IsAccessor == member.IsAccessor) ||
393 entry.DeclaringType.ImplementsInterface (member.DeclaringType, false))
394 return false;
397 if (existing.Count == 1) {
398 existing = new List<MemberSpec> () { existing[0], member };
399 return true;
402 existing.Add (member);
403 return false;
406 public static MemberSpec FindMember (TypeSpec container, MemberFilter filter, BindingRestriction restrictions)
408 do {
409 IList<MemberSpec> applicable;
410 if (container.MemberCache.member_hash.TryGetValue (filter.Name, out applicable)) {
411 // Start from the end because interface members are in reverse order
412 for (int i = applicable.Count - 1; i >= 0; i--) {
413 var entry = applicable [i];
415 if ((restrictions & BindingRestriction.InstanceOnly) != 0 && entry.IsStatic)
416 continue;
418 if ((restrictions & BindingRestriction.NoAccessors) != 0 && entry.IsAccessor)
419 continue;
421 if ((restrictions & BindingRestriction.OverrideOnly) != 0 && (entry.Modifiers & Modifiers.OVERRIDE) == 0)
422 continue;
424 if (!filter.Equals (entry))
425 continue;
427 if ((restrictions & BindingRestriction.DeclaredOnly) != 0 && container.IsInterface && entry.DeclaringType != container)
428 continue;
430 return entry;
434 if ((restrictions & BindingRestriction.DeclaredOnly) != 0)
435 break;
437 container = container.BaseType;
438 } while (container != null);
440 return null;
444 // A special method to work with member lookup only. It returns a list of all members named @name
445 // starting from @container. It's very performance sensitive
447 public static IList<MemberSpec> FindMembers (TypeSpec container, string name, bool declaredOnly)
449 IList<MemberSpec> applicable;
451 do {
452 if (container.MemberCache.member_hash.TryGetValue (name, out applicable) || declaredOnly)
453 return applicable;
455 container = container.BaseType;
456 } while (container != null);
458 return null;
462 // Finds the nested type in container
464 public static TypeSpec FindNestedType (TypeSpec container, string name, int arity)
466 IList<MemberSpec> applicable;
467 TypeSpec best_match = null;
468 do {
469 // TODO: Don't know how to handle this yet
470 // When resolving base type of nested type, parent type must have
471 // base type resolved to scan full hierarchy correctly
472 // Similarly MemberCacheTypes will inflate BaseType and Interfaces
473 // based on type definition
474 var tc = container.MemberDefinition as TypeContainer;
475 if (tc != null)
476 tc.DefineContainer ();
478 if (container.MemberCacheTypes.member_hash.TryGetValue (name, out applicable)) {
479 for (int i = applicable.Count - 1; i >= 0; i--) {
480 var entry = applicable[i];
481 if ((entry.Kind & MemberKind.NestedMask) == 0)
482 continue;
484 var ts = (TypeSpec) entry;
485 if (arity == ts.Arity)
486 return ts;
488 if (arity < 0) {
489 if (best_match == null) {
490 best_match = ts;
491 } else if (System.Math.Abs (ts.Arity + arity) < System.Math.Abs (ts.Arity + arity)) {
492 best_match = ts;
498 container = container.BaseType;
499 } while (container != null);
501 return best_match;
505 // Looks for extension methods with defined name and extension type
507 public List<MethodSpec> FindExtensionMethods (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity)
509 IList<MemberSpec> entries;
510 if (!member_hash.TryGetValue (name, out entries))
511 return null;
513 List<MethodSpec> candidates = null;
514 foreach (var entry in entries) {
515 if (entry.Kind != MemberKind.Method || (arity > 0 && entry.Arity != arity))
516 continue;
518 var ms = (MethodSpec) entry;
519 if (!ms.IsExtensionMethod)
520 continue;
522 if (!ms.IsAccessible (invocationContext))
523 continue;
526 // Extension methods cannot be nested hence checking parent is enough
528 if ((ms.DeclaringType.Modifiers & Modifiers.INTERNAL) != 0 && !ms.DeclaringType.MemberDefinition.IsInternalAsPublic (invocationContext.Module.DeclaringAssembly))
529 continue;
531 if (candidates == null)
532 candidates = new List<MethodSpec> ();
533 candidates.Add (ms);
536 return candidates;
540 // Returns base members of @member member if no exact match is found @bestCandidate returns
541 // the best match
543 public static MemberSpec FindBaseMember (MemberCore member, out MemberSpec bestCandidate, ref bool overrides)
545 bestCandidate = null;
546 var container = member.Parent.PartialContainer.Definition;
547 if (!container.IsInterface) {
548 container = container.BaseType;
550 // It can happen for a user definition of System.Object
551 if (container == null)
552 return null;
555 string name = GetLookupName (member);
556 var member_param = member is IParametersMember ? ((IParametersMember) member).Parameters : null;
558 var mkind = GetMemberCoreKind (member);
559 bool member_with_accessors = mkind == MemberKind.Indexer || mkind == MemberKind.Property;
561 IList<MemberSpec> applicable;
562 MemberSpec ambig_candidate = null;
564 do {
565 if (container.MemberCache.member_hash.TryGetValue (name, out applicable)) {
566 for (int i = 0; i < applicable.Count; ++i) {
567 var entry = applicable [i];
569 if ((entry.Modifiers & Modifiers.PRIVATE) != 0)
570 continue;
572 if ((entry.Modifiers & Modifiers.AccessibilityMask) == Modifiers.INTERNAL &&
573 !entry.DeclaringType.MemberDefinition.IsInternalAsPublic (member.Module.DeclaringAssembly))
574 continue;
577 // Isn't the member of same kind ?
579 if ((entry.Kind & ~MemberKind.Destructor & mkind & MemberKind.MaskType) == 0) {
580 // Destructors are ignored as they cannot be overridden by user
581 if ((entry.Kind & MemberKind.Destructor) != 0)
582 continue;
584 // A method with different arity does not hide base member
585 if (mkind != MemberKind.Method && member.MemberName.Arity != entry.Arity)
586 continue;
588 bestCandidate = entry;
589 return null;
593 // Same kind of different arity is valid
595 if (member.MemberName.Arity != entry.Arity) {
596 continue;
599 if ((entry.Kind & mkind & (MemberKind.Method | MemberKind.Indexer)) != 0) {
600 if (entry.IsAccessor != member is AbstractPropertyEventMethod)
601 continue;
603 var pm = entry as IParametersMember;
604 if (!TypeSpecComparer.Override.IsEqual (pm.Parameters, member_param))
605 continue;
609 // Skip override for member with accessors. It may not fully implement the base member
610 // but keep flag we found an implementation in case the base member is abstract
612 if (member_with_accessors && ((entry.Modifiers & (Modifiers.OVERRIDE | Modifiers.SEALED)) == Modifiers.OVERRIDE)) {
614 // Set candidate to override implementation to flag we found an implementation
616 overrides = true;
617 continue;
621 // For members with parameters we can encounter an ambiguous candidates (they match exactly)
622 // because generic type parameters could be inflated into same types
624 if (ambig_candidate == null && (entry.Kind & mkind & (MemberKind.Method | MemberKind.Indexer)) != 0) {
625 bestCandidate = null;
626 ambig_candidate = entry;
627 continue;
630 bestCandidate = ambig_candidate;
631 return entry;
635 if (container.IsInterface || ambig_candidate != null)
636 break;
638 container = container.BaseType;
639 } while (container != null);
641 return ambig_candidate;
645 // Returns inflated version of MemberSpec, it works similarly to
646 // SRE TypeBuilder.GetMethod
648 public static T GetMember<T> (TypeSpec container, T spec) where T : MemberSpec
650 IList<MemberSpec> applicable;
651 if (container.MemberCache.member_hash.TryGetValue (GetLookupName (spec), out applicable)) {
652 for (int i = applicable.Count - 1; i >= 0; i--) {
653 var entry = applicable[i];
654 if (entry.MemberDefinition == spec.MemberDefinition)
655 return (T) entry;
659 throw new InternalErrorException ("Missing member `{0}' on inflated type `{1}'",
660 spec.GetSignatureForError (), container.GetSignatureForError ());
663 static MemberKind GetMemberCoreKind (MemberCore member)
665 if (member is FieldBase)
666 return MemberKind.Field;
667 if (member is Indexer)
668 return MemberKind.Indexer;
669 if (member is Class)
670 return MemberKind.Class;
671 if (member is Struct)
672 return MemberKind.Struct;
673 if (member is Destructor)
674 return MemberKind.Destructor;
675 if (member is Method)
676 return MemberKind.Method;
677 if (member is Property)
678 return MemberKind.Property;
679 if (member is EventField)
680 return MemberKind.Event;
681 if (member is Interface)
682 return MemberKind.Interface;
683 if (member is EventProperty)
684 return MemberKind.Event;
685 if (member is Delegate)
686 return MemberKind.Delegate;
687 if (member is Enum)
688 return MemberKind.Enum;
690 throw new NotImplementedException (member.GetType ().ToString ());
693 public static List<FieldSpec> GetAllFieldsForDefiniteAssignment (TypeSpec container)
695 List<FieldSpec> fields = null;
696 foreach (var entry in container.MemberCache.member_hash) {
697 foreach (var name_entry in entry.Value) {
698 if (name_entry.Kind != MemberKind.Field)
699 continue;
701 if ((name_entry.Modifiers & Modifiers.STATIC) != 0)
702 continue;
705 // Fixed size buffers are not subject to definite assignment checking
707 if (name_entry is FixedFieldSpec || name_entry is ConstSpec)
708 continue;
710 var fs = (FieldSpec) name_entry;
713 // LAMESPEC: Very bizzare hack, definitive assignment is not done
714 // for imported non-public reference fields except array. No idea what the
715 // actual csc rule is
717 if (!fs.IsPublic && container.MemberDefinition.IsImported && (!fs.MemberType.IsArray && TypeSpec.IsReferenceType (fs.MemberType)))
718 continue;
720 if (fields == null)
721 fields = new List<FieldSpec> ();
723 fields.Add (fs);
724 break;
728 return fields ?? new List<FieldSpec> (0);
731 public static IList<MemberSpec> GetCompletitionMembers (IMemberContext ctx, TypeSpec container, string name)
733 var matches = new List<MemberSpec> ();
734 foreach (var entry in container.MemberCache.member_hash) {
735 foreach (var name_entry in entry.Value) {
736 if (name_entry.IsAccessor)
737 continue;
739 if ((name_entry.Kind & (MemberKind.Constructor | MemberKind.Destructor | MemberKind.Operator)) != 0)
740 continue;
742 if (!name_entry.IsAccessible (ctx))
743 continue;
745 if (name == null || name_entry.Name.StartsWith (name)) {
746 matches.Add (name_entry);
751 return matches;
755 // Returns members of @iface only, base members are ignored
757 public static List<MethodSpec> GetInterfaceMethods (TypeSpec iface)
760 // MemberCache flatten interfaces, therefore in cases like this one
762 // interface IA : IB {}
763 // interface IB { void Foo () }
765 // we would return Foo inside IA which is not expected in this case
767 var methods = new List<MethodSpec> ();
768 foreach (var entry in iface.MemberCache.member_hash.Values) {
769 foreach (var name_entry in entry) {
770 if (iface == name_entry.DeclaringType) {
771 if (name_entry.Kind == MemberKind.Method) {
772 methods.Add ((MethodSpec) name_entry);
778 return methods;
782 // Returns all not implememted abstract members inside abstract type
783 // NOTE: Returned list is shared and must not be modified
785 public static IList<MethodSpec> GetNotImplementedAbstractMethods (TypeSpec type)
787 if (type.MemberCache.missing_abstract != null)
788 return type.MemberCache.missing_abstract;
790 var abstract_methods = new List<MethodSpec> ();
791 List<TypeSpec> hierarchy = null;
794 // Stage 1: top-to-bottom scan for abstract members
796 var abstract_type = type;
797 while (true) {
798 foreach (var entry in abstract_type.MemberCache.member_hash) {
799 foreach (var name_entry in entry.Value) {
800 if ((name_entry.Modifiers & Modifiers.ABSTRACT) == 0)
801 continue;
803 if (name_entry.Kind != MemberKind.Method)
804 continue;
806 abstract_methods.Add ((MethodSpec) name_entry);
810 var base_type = abstract_type.BaseType;
811 if (!base_type.IsAbstract)
812 break;
814 if (hierarchy == null)
815 hierarchy = new List<TypeSpec> ();
817 hierarchy.Add (abstract_type);
818 abstract_type = base_type;
821 int not_implemented_count = abstract_methods.Count;
822 if (not_implemented_count == 0 || hierarchy == null) {
823 type.MemberCache.missing_abstract = abstract_methods;
824 return type.MemberCache.missing_abstract;
828 // Stage 2: Remove already implemented methods
830 foreach (var type_up in hierarchy) {
831 var members = type_up.MemberCache.member_hash;
832 if (members.Count == 0)
833 continue;
835 for (int i = 0; i < abstract_methods.Count; ++i) {
836 var candidate = abstract_methods [i];
837 if (candidate == null)
838 continue;
840 IList<MemberSpec> applicable;
841 if (!members.TryGetValue (candidate.Name, out applicable))
842 continue;
844 var filter = new MemberFilter (candidate);
845 foreach (var item in applicable) {
846 if ((item.Modifiers & (Modifiers.OVERRIDE | Modifiers.VIRTUAL)) == 0)
847 continue;
849 if (filter.Equals (item)) {
850 --not_implemented_count;
851 abstract_methods [i] = null;
852 break;
858 if (not_implemented_count == abstract_methods.Count) {
859 type.MemberCache.missing_abstract = abstract_methods;
860 return type.MemberCache.missing_abstract;
863 var not_implemented = new MethodSpec[not_implemented_count];
864 int counter = 0;
865 foreach (var m in abstract_methods) {
866 if (m == null)
867 continue;
869 not_implemented[counter++] = m;
872 type.MemberCache.missing_abstract = not_implemented;
873 return type.MemberCache.missing_abstract;
876 static string GetLookupName (MemberSpec ms)
878 if (ms.Kind == MemberKind.Indexer)
879 return IndexerNameAlias;
881 if (ms.Kind == MemberKind.Constructor) {
882 if (ms.IsStatic)
883 return Constructor.TypeConstructorName;
885 return Constructor.ConstructorName;
888 return ms.Name;
891 static string GetLookupName (MemberCore mc)
893 if (mc is Indexer)
894 return IndexerNameAlias;
896 if (mc is Constructor)
897 return mc.IsStatic ? Constructor.TypeConstructorName : Constructor.ConstructorName;
899 return mc.MemberName.Name;
903 // Returns all operators declared on container and its base types (until declaredOnly is used)
905 public static IList<MemberSpec> GetUserOperator (TypeSpec container, Operator.OpType op, bool declaredOnly)
907 IList<MemberSpec> found = null;
909 IList<MemberSpec> applicable;
910 do {
911 var mc = container.MemberCache;
913 if (((op == Operator.OpType.Implicit || op == Operator.OpType.Explicit) && (mc.state & StateFlags.HasConversionOperator) != 0) ||
914 (mc.state & StateFlags.HasUserOperator) != 0) {
916 if (mc.member_hash.TryGetValue (Operator.GetMetadataName (op), out applicable)) {
917 int i;
918 for (i = 0; i < applicable.Count; ++i) {
919 if (applicable[i].Kind != MemberKind.Operator) {
920 break;
925 // Handles very rare case where a method with same name as operator (op_xxxx) exists
926 // and we have to resize the applicable list
928 if (i != applicable.Count) {
929 for (i = 0; i < applicable.Count; ++i) {
930 if (applicable[i].Kind != MemberKind.Operator) {
931 continue;
934 if (found == null) {
935 found = new List<MemberSpec> ();
936 found.Add (applicable[i]);
937 } else {
938 var prev = found as List<MemberSpec>;
939 if (prev == null) {
940 prev = new List<MemberSpec> (found.Count + 1);
941 prev.AddRange (found);
944 prev.Add (applicable[i]);
947 } else {
948 if (found == null) {
949 found = applicable;
950 } else {
951 var merged = found as List<MemberSpec>;
952 if (merged == null) {
953 merged = new List<MemberSpec> (found.Count + applicable.Count);
954 merged.AddRange (found);
955 found = merged;
958 merged.AddRange (applicable);
964 // BaseType call can be expensive
965 if (declaredOnly)
966 break;
968 container = container.BaseType;
969 } while (container != null);
971 return found;
975 // Inflates all member cache nested types
977 public void InflateTypes (MemberCache inflated_cache, TypeParameterInflator inflator)
979 foreach (var item in member_hash) {
980 IList<MemberSpec> inflated_members = null;
981 for (int i = 0; i < item.Value.Count; ++i ) {
982 var member = item.Value[i];
984 // FIXME: When inflating members refering nested types before they are inflated
985 if (member == null)
986 continue;
988 if ((member.Kind & MemberKind.NestedMask) != 0 &&
989 (member.Modifiers & Modifiers.COMPILER_GENERATED) == 0) {
990 if (inflated_members == null) {
991 inflated_members = new MemberSpec[item.Value.Count];
992 inflated_cache.member_hash.Add (item.Key, inflated_members);
995 inflated_members [i] = member.InflateMember (inflator);
1002 // Inflates all open type members, requires InflateTypes to be called before
1004 public void InflateMembers (MemberCache cacheToInflate, TypeSpec inflatedType, TypeParameterInflator inflator)
1006 var inflated_member_hash = cacheToInflate.member_hash;
1007 Dictionary<MemberSpec, MethodSpec> accessor_relation = null;
1008 List<MemberSpec> accessor_members = null;
1010 // Copy member specific flags when all members were added
1011 cacheToInflate.state = state;
1013 foreach (var item in member_hash) {
1014 var members = item.Value;
1015 IList<MemberSpec> inflated_members = null;
1016 for (int i = 0; i < members.Count; ++i ) {
1017 var member = members[i];
1020 // All nested types have been inflated earlier except for
1021 // compiler types which are created later and could miss InflateTypes
1023 if ((member.Kind & MemberKind.NestedMask) != 0 &&
1024 (member.Modifiers & Modifiers.COMPILER_GENERATED) == 0) {
1025 if (inflated_members == null)
1026 inflated_members = inflated_member_hash[item.Key];
1028 continue;
1032 // Clone the container first
1034 if (inflated_members == null) {
1035 inflated_members = new MemberSpec [item.Value.Count];
1036 inflated_member_hash.Add (item.Key, inflated_members);
1039 var local_inflator = inflator;
1041 if (member.DeclaringType != inflatedType) {
1043 // Don't inflate top-level non-generic interface members
1044 // merged into generic interface
1046 if (!member.DeclaringType.IsGeneric && !member.DeclaringType.IsNested) {
1047 inflated_members [i] = member;
1048 continue;
1052 // Needed when inflating flatten interfaces. It inflates
1053 // container type only, type parameters are already done
1055 // Handles cases like:
1057 // interface I<T> {}
1058 // interface I<U, V> : I<U> {}
1060 // class C: I<int, bool> {}
1062 var inflated_parent = inflator.Inflate (member.DeclaringType);
1063 if (inflated_parent != inflator.TypeInstance)
1064 local_inflator = new TypeParameterInflator (inflator, inflated_parent);
1068 // Inflate every member, its parent is now different
1070 var inflated = member.InflateMember (local_inflator);
1071 inflated_members [i] = inflated;
1073 if (member is PropertySpec || member is EventSpec) {
1074 if (accessor_members == null)
1075 accessor_members = new List<MemberSpec> ();
1077 accessor_members.Add (inflated);
1078 continue;
1081 if (member.IsAccessor) {
1082 if (accessor_relation == null)
1083 accessor_relation = new Dictionary<MemberSpec, MethodSpec> ();
1084 accessor_relation.Add (member, (MethodSpec) inflated);
1089 if (accessor_members != null) {
1090 foreach (var member in accessor_members) {
1091 var prop = member as PropertySpec;
1092 if (prop != null) {
1093 if (prop.Get != null)
1094 prop.Get = accessor_relation[prop.Get];
1095 if (prop.Set != null)
1096 prop.Set = accessor_relation[prop.Set];
1098 continue;
1101 var ev = (EventSpec) member;
1102 ev.AccessorAdd = accessor_relation[ev.AccessorAdd];
1103 ev.AccessorRemove = accessor_relation[ev.AccessorRemove];
1109 // Removes hidden base members of an interface. For compiled interfaces we cannot
1110 // do name filtering during Add (as we do for import) because we need all base
1111 // names to be valid during type definition.
1112 // Add replaces hidden base member with current one which means any name collision
1113 // (CS0108) of non-first name would be unnoticed because the name was replaced
1114 // with the one from compiled type
1116 public void RemoveHiddenMembers (TypeSpec container)
1118 foreach (var entry in member_hash) {
1119 var values = entry.Value;
1121 int container_members_start_at = 0;
1122 while (values[container_members_start_at].DeclaringType != container && ++container_members_start_at < entry.Value.Count);
1124 if (container_members_start_at == 0 || container_members_start_at == values.Count)
1125 continue;
1127 for (int i = 0; i < container_members_start_at; ++i) {
1128 var member = values[i];
1130 if (!container.ImplementsInterface (member.DeclaringType, false))
1131 continue;
1133 var member_param = member is IParametersMember ? ((IParametersMember) member).Parameters : ParametersCompiled.EmptyReadOnlyParameters;
1135 for (int ii = container_members_start_at; ii < values.Count; ++ii) {
1136 var container_entry = values[ii];
1138 if (container_entry.Arity != member.Arity)
1139 continue;
1141 if (container_entry is IParametersMember) {
1142 if (!TypeSpecComparer.Override.IsEqual (((IParametersMember) container_entry).Parameters, member_param))
1143 continue;
1146 values.RemoveAt (i);
1147 --container_members_start_at;
1148 --ii;
1149 --i;
1156 // Checks all appropriate container members for CLS compliance
1158 public void VerifyClsCompliance (TypeSpec container, Report report)
1160 if (locase_members != null)
1161 return;
1163 if (container.BaseType == null) {
1164 locase_members = new Dictionary<string, MemberSpec[]> (member_hash.Count); // StringComparer.OrdinalIgnoreCase);
1165 } else {
1166 container.BaseType.MemberCache.VerifyClsCompliance (container.BaseType, report);
1167 locase_members = new Dictionary<string, MemberSpec[]> (container.BaseType.MemberCache.locase_members); //, StringComparer.OrdinalIgnoreCase);
1170 var is_imported_type = container.MemberDefinition.IsImported;
1171 foreach (var entry in container.MemberCache.member_hash) {
1172 for (int i = 0; i < entry.Value.Count; ++i ) {
1173 var name_entry = entry.Value[i];
1174 if ((name_entry.Modifiers & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
1175 continue;
1177 if ((name_entry.Modifiers & (Modifiers.OVERRIDE | Modifiers.COMPILER_GENERATED)) != 0)
1178 continue;
1180 if ((name_entry.Kind & MemberKind.MaskType) == 0)
1181 continue;
1183 if (name_entry.MemberDefinition.CLSAttributeValue == false)
1184 continue;
1186 IParametersMember p_a = null;
1187 if (!is_imported_type) {
1188 p_a = name_entry as IParametersMember;
1189 if (p_a != null && !name_entry.IsAccessor) {
1190 var p_a_pd = p_a.Parameters;
1192 // Check differing overloads in @container
1194 for (int ii = i + 1; ii < entry.Value.Count; ++ii) {
1195 var checked_entry = entry.Value[ii];
1196 IParametersMember p_b = checked_entry as IParametersMember;
1197 if (p_b == null)
1198 continue;
1200 if (p_a_pd.Count != p_b.Parameters.Count)
1201 continue;
1203 if (checked_entry.IsAccessor)
1204 continue;
1206 var res = ParametersCompiled.IsSameClsSignature (p_a.Parameters, p_b.Parameters);
1207 if (res != 0) {
1208 ReportOverloadedMethodClsDifference (name_entry, checked_entry, res, report);
1214 if (i > 0 || name_entry.Kind == MemberKind.Constructor || name_entry.Kind == MemberKind.Indexer)
1215 continue;
1217 var name_entry_locase = name_entry.Name.ToLowerInvariant ();
1219 MemberSpec[] found;
1220 if (!locase_members.TryGetValue (name_entry_locase, out found)) {
1221 found = new MemberSpec[] { name_entry };
1222 locase_members.Add (name_entry_locase, found);
1223 } else {
1224 bool same_names_only = true;
1225 foreach (var f in found) {
1226 if (f.Name == name_entry.Name) {
1227 if (p_a != null) {
1228 IParametersMember p_b = f as IParametersMember;
1229 if (p_b == null)
1230 continue;
1232 if (p_a.Parameters.Count != p_b.Parameters.Count)
1233 continue;
1235 if (f.IsAccessor)
1236 continue;
1238 var res = ParametersCompiled.IsSameClsSignature (p_a.Parameters, p_b.Parameters);
1239 if (res != 0) {
1240 ReportOverloadedMethodClsDifference (f, name_entry, res, report);
1244 continue;
1247 same_names_only = false;
1248 if (!is_imported_type) {
1249 var last = GetLaterDefinedMember (f, name_entry);
1250 if (last == f.MemberDefinition) {
1251 report.SymbolRelatedToPreviousError (name_entry);
1252 } else {
1253 report.SymbolRelatedToPreviousError (f);
1256 report.Warning (3005, 1, last.Location,
1257 "Identifier `{0}' differing only in case is not CLS-compliant", last.GetSignatureForError ());
1261 if (!same_names_only) {
1262 Array.Resize (ref found, found.Length + 1);
1263 found[found.Length - 1] = name_entry;
1264 locase_members[name_entry_locase] = found;
1272 // Local report helper to issue correctly ordered members stored in hashtable
1274 static MemberCore GetLaterDefinedMember (MemberSpec a, MemberSpec b)
1276 var mc_a = a.MemberDefinition as MemberCore;
1277 var mc_b = b.MemberDefinition as MemberCore;
1278 if (mc_a == null)
1279 return mc_b;
1281 if (mc_b == null)
1282 return mc_a;
1284 if (a.DeclaringType.MemberDefinition != b.DeclaringType.MemberDefinition)
1285 return mc_b;
1287 if (mc_a.Location.File != mc_a.Location.File)
1288 return mc_b;
1290 return mc_b.Location.Row > mc_a.Location.Row ? mc_b : mc_a;
1293 static void ReportOverloadedMethodClsDifference (MemberSpec a, MemberSpec b, int res, Report report)
1295 var last = GetLaterDefinedMember (a, b);
1296 if (last == a.MemberDefinition) {
1297 report.SymbolRelatedToPreviousError (b);
1298 } else {
1299 report.SymbolRelatedToPreviousError (a);
1302 if ((res & 1) != 0) {
1303 report.Warning (3006, 1, last.Location,
1304 "Overloaded method `{0}' differing only in ref or out, or in array rank, is not CLS-compliant",
1305 last.GetSignatureForError ());
1308 if ((res & 2) != 0) {
1309 report.Warning (3007, 1, last.Location,
1310 "Overloaded method `{0}' differing only by unnamed array types is not CLS-compliant",
1311 last.GetSignatureForError ());
1315 public bool CheckExistingMembersOverloads (MemberCore member, AParametersCollection parameters)
1317 var name = GetLookupName (member);
1318 var imb = member as InterfaceMemberBase;
1319 if (imb != null && imb.IsExplicitImpl) {
1320 name = imb.GetFullName (name);
1323 return CheckExistingMembersOverloads (member, name, parameters);
1326 public bool CheckExistingMembersOverloads (MemberCore member, string name, AParametersCollection parameters)
1328 IList<MemberSpec> entries;
1329 if (!member_hash.TryGetValue (name, out entries))
1330 return false;
1332 var Report = member.Compiler.Report;
1334 int method_param_count = parameters.Count;
1335 for (int i = entries.Count - 1; i >= 0; --i) {
1336 var ce = entries[i];
1337 var pm = ce as IParametersMember;
1338 var pd = pm == null ? ParametersCompiled.EmptyReadOnlyParameters : pm.Parameters;
1339 if (pd.Count != method_param_count)
1340 continue;
1342 if (ce.Arity != member.MemberName.Arity)
1343 continue;
1345 // Ignore merged interface members
1346 if (member.Parent.PartialContainer != ce.DeclaringType.MemberDefinition)
1347 continue;
1349 var p_types = pd.Types;
1350 if (method_param_count > 0) {
1351 int ii = method_param_count - 1;
1352 TypeSpec type_a, type_b;
1353 do {
1354 type_a = parameters.Types [ii];
1355 type_b = p_types [ii];
1357 if ((pd.FixedParameters [ii].ModFlags & Parameter.Modifier.ISBYREF) !=
1358 (parameters.FixedParameters [ii].ModFlags & Parameter.Modifier.ISBYREF))
1359 break;
1361 } while (TypeSpecComparer.Override.IsEqual (type_a, type_b) && ii-- != 0);
1363 if (ii >= 0)
1364 continue;
1367 // Operators can differ in return type only
1369 if (member is Operator && ce.Kind == MemberKind.Operator && ((MethodSpec) ce).ReturnType != ((Operator) member).ReturnType)
1370 continue;
1373 // Report difference in parameter modifiers only
1375 if (pd != null && member is MethodCore) {
1376 ii = method_param_count;
1377 while (ii-- != 0 && parameters.FixedParameters[ii].ModFlags == pd.FixedParameters[ii].ModFlags &&
1378 parameters.ExtensionMethodType == pd.ExtensionMethodType) ;
1380 if (ii >= 0) {
1381 var mc = ce as MethodSpec;
1382 member.Compiler.Report.SymbolRelatedToPreviousError (ce);
1383 if ((member.ModFlags & Modifiers.PARTIAL) != 0 && (mc.Modifiers & Modifiers.PARTIAL) != 0) {
1384 if (parameters.HasParams || pd.HasParams) {
1385 Report.Error (758, member.Location,
1386 "A partial method declaration and partial method implementation cannot differ on use of `params' modifier");
1387 } else {
1388 Report.Error (755, member.Location,
1389 "A partial method declaration and partial method implementation must be both an extension method or neither");
1391 } else if (member is Constructor) {
1392 Report.Error (851, member.Location,
1393 "Overloaded contructor `{0}' cannot differ on use of parameter modifiers only",
1394 member.GetSignatureForError ());
1395 } else {
1396 Report.Error (663, member.Location,
1397 "Overloaded method `{0}' cannot differ on use of parameter modifiers only",
1398 member.GetSignatureForError ());
1400 return false;
1405 if ((ce.Kind & MemberKind.Method) != 0) {
1406 Method method_a = member as Method;
1407 Method method_b = ce.MemberDefinition as Method;
1408 if (method_a != null && method_b != null && (method_a.ModFlags & method_b.ModFlags & Modifiers.PARTIAL) != 0) {
1409 const Modifiers partial_modifiers = Modifiers.STATIC | Modifiers.UNSAFE;
1410 if (method_a.IsPartialDefinition == method_b.IsPartialImplementation) {
1411 if ((method_a.ModFlags & partial_modifiers) == (method_b.ModFlags & partial_modifiers) ||
1412 method_a.Parent.IsUnsafe && method_b.Parent.IsUnsafe) {
1413 if (method_a.IsPartialImplementation) {
1414 method_a.SetPartialDefinition (method_b);
1415 if (entries.Count == 1)
1416 member_hash.Remove (name);
1417 else
1418 entries.RemoveAt (i);
1419 } else {
1420 method_b.SetPartialDefinition (method_a);
1421 method_a.caching_flags |= MemberCore.Flags.PartialDefinitionExists;
1423 continue;
1426 if (method_a.IsStatic != method_b.IsStatic) {
1427 Report.SymbolRelatedToPreviousError (ce);
1428 Report.Error (763, member.Location,
1429 "A partial method declaration and partial method implementation must be both `static' or neither");
1432 Report.SymbolRelatedToPreviousError (ce);
1433 Report.Error (764, member.Location,
1434 "A partial method declaration and partial method implementation must be both `unsafe' or neither");
1435 return false;
1438 Report.SymbolRelatedToPreviousError (ce);
1439 if (method_a.IsPartialDefinition) {
1440 Report.Error (756, member.Location, "A partial method `{0}' declaration is already defined",
1441 member.GetSignatureForError ());
1444 Report.Error (757, member.Location, "A partial method `{0}' implementation is already defined",
1445 member.GetSignatureForError ());
1446 return false;
1449 Report.SymbolRelatedToPreviousError (ce);
1451 bool is_reserved_a = member is AbstractPropertyEventMethod || member is Operator;
1452 bool is_reserved_b = ((MethodSpec) ce).IsReservedMethod;
1454 if (is_reserved_a || is_reserved_b) {
1455 Report.Error (82, member.Location, "A member `{0}' is already reserved",
1456 is_reserved_a ?
1457 ce.GetSignatureForError () :
1458 member.GetSignatureForError ());
1459 return false;
1461 } else {
1462 Report.SymbolRelatedToPreviousError (ce);
1465 if (member is Operator && ce.Kind == MemberKind.Operator) {
1466 Report.Error (557, member.Location, "Duplicate user-defined conversion in type `{0}'",
1467 member.Parent.GetSignatureForError ());
1468 return false;
1471 Report.Error (111, member.Location,
1472 "A member `{0}' is already defined. Rename this member or use different parameter types",
1473 member.GetSignatureForError ());
1474 return false;
1477 return true;