Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.CSharp-5.5.0/Resolver/MemberLookup.cs @ 15724

Last change on this file since 15724 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 28.4 KB
Line 
1// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Collections.Generic;
21using System.Diagnostics;
22using System.Linq;
23
24using ICSharpCode.NRefactory.Semantics;
25using ICSharpCode.NRefactory.TypeSystem;
26
27namespace ICSharpCode.NRefactory.CSharp.Resolver
28{
29  /// <summary>
30  /// Implementation of member lookup (C# 4.0 spec, §7.4).
31  /// </summary>
32  public class MemberLookup
33  {
34    #region Static helper methods
35    /// <summary>
36    /// Gets whether the member is considered to be invocable.
37    /// </summary>
38    public static bool IsInvocable(IMember member)
39    {
40      if (member == null)
41        throw new ArgumentNullException("member");
42      // C# 4.0 spec, §7.4 member lookup
43      if (member is IEvent || member is IMethod)
44        return true;
45      IType returnType = member.ReturnType;
46      return returnType.Kind == TypeKind.Dynamic || returnType.Kind == TypeKind.Delegate;
47    }
48    #endregion
49   
50    readonly ITypeDefinition currentTypeDefinition;
51    readonly IAssembly currentAssembly;
52    readonly bool isInEnumMemberInitializer;
53   
54    public MemberLookup(ITypeDefinition currentTypeDefinition, IAssembly currentAssembly, bool isInEnumMemberInitializer = false)
55    {
56      this.currentTypeDefinition = currentTypeDefinition;
57      this.currentAssembly = currentAssembly;
58      this.isInEnumMemberInitializer = isInEnumMemberInitializer;
59    }
60   
61    #region IsAccessible
62    /// <summary>
63    /// Gets whether access to protected instance members of the target expression is possible.
64    /// </summary>
65    public bool IsProtectedAccessAllowed(ResolveResult targetResolveResult)
66    {
67      return targetResolveResult is ThisResolveResult || IsProtectedAccessAllowed(targetResolveResult.Type);
68    }
69   
70    /// <summary>
71    /// Gets whether access to protected instance members of the target type is possible.
72    /// </summary>
73    /// <remarks>
74    /// This method does not consider the special case of the 'base' reference. If possible, use the
75    /// <c>IsProtectedAccessAllowed(ResolveResult)</c> overload instead.
76    /// </remarks>
77    public bool IsProtectedAccessAllowed(IType targetType)
78    {
79      if (targetType.Kind == TypeKind.TypeParameter)
80        targetType = ((ITypeParameter)targetType).EffectiveBaseClass;
81      ITypeDefinition typeDef = targetType.GetDefinition();
82      if (typeDef == null)
83        return false;
84      for (ITypeDefinition c = currentTypeDefinition; c != null; c = c.DeclaringTypeDefinition) {
85        if (typeDef.IsDerivedFrom(c))
86          return true;
87      }
88      return false;
89    }
90   
91    /// <summary>
92    /// Gets whether <paramref name="entity"/> is accessible in the current class.
93    /// </summary>
94    /// <param name="entity">The entity to test</param>
95    /// <param name="allowProtectedAccess">
96    /// Whether protected access to instance members is allowed.
97    /// True if the type of the reference is derived from the current class.
98    /// Protected static members may be accessible even if false is passed for this parameter.
99    /// </param>
100    public bool IsAccessible(IEntity entity, bool allowProtectedAccess)
101    {
102      if (entity == null)
103        throw new ArgumentNullException("entity");
104      // C# 4.0 spec, §3.5.2 Accessiblity domains
105      switch (entity.Accessibility) {
106        case Accessibility.None:
107          return false;
108        case Accessibility.Private:
109          // check for members of outer classes (private members of outer classes can be accessed)
110          for (var t = currentTypeDefinition; t != null; t = t.DeclaringTypeDefinition) {
111            if (t.Equals(entity.DeclaringTypeDefinition))
112              return true;
113          }
114          return false;
115        case Accessibility.Public:
116          return true;
117        case Accessibility.Protected:
118          return IsProtectedAccessible(allowProtectedAccess, entity);
119        case Accessibility.Internal:
120          return IsInternalAccessible(entity.ParentAssembly);
121        case Accessibility.ProtectedOrInternal:
122          return IsInternalAccessible(entity.ParentAssembly) || IsProtectedAccessible(allowProtectedAccess, entity);
123        case Accessibility.ProtectedAndInternal:
124          return IsInternalAccessible(entity.ParentAssembly) && IsProtectedAccessible(allowProtectedAccess, entity);
125        default:
126          throw new Exception("Invalid value for Accessibility");
127      }
128    }
129   
130    bool IsInternalAccessible(IAssembly assembly)
131    {
132      return assembly != null && currentAssembly != null && assembly.InternalsVisibleTo(currentAssembly);
133    }
134   
135    bool IsProtectedAccessible(bool allowProtectedAccess, IEntity entity)
136    {
137      // For static members and type definitions, we do not require the qualifying reference
138      // to be derived from the current class (allowProtectedAccess).
139      if (entity.IsStatic || entity.SymbolKind == SymbolKind.TypeDefinition)
140        allowProtectedAccess = true;
141     
142      for (var t = currentTypeDefinition; t != null; t = t.DeclaringTypeDefinition) {
143        if (t.Equals(entity.DeclaringTypeDefinition))
144          return true;
145       
146        // PERF: this might hurt performance as this method is called several times (once for each member)
147        // make sure resolving base types is cheap (caches?) or cache within the MemberLookup instance
148        if (allowProtectedAccess && t.IsDerivedFrom(entity.DeclaringTypeDefinition))
149          return true;
150      }
151      return false;
152    }
153    #endregion
154   
155    #region GetAccessibleMembers
156    /// <summary>
157    /// Retrieves all members that are accessible and not hidden (by being overridden or shadowed).
158    /// Returns both members and nested type definitions. Does not include extension methods.
159    /// </summary>
160    public IEnumerable<IEntity> GetAccessibleMembers(ResolveResult targetResolveResult)
161    {
162      if (targetResolveResult == null)
163        throw new ArgumentNullException("targetResolveResult");
164     
165      bool targetIsTypeParameter = targetResolveResult.Type.Kind == TypeKind.TypeParameter;
166      bool allowProtectedAccess = IsProtectedAccessAllowed(targetResolveResult);
167     
168      // maps the member name to the list of lookup groups
169      var lookupGroupDict = new Dictionary<string, List<LookupGroup>>();
170     
171      // This loop will handle base types before derived types.
172      // The loop performs three jobs:
173      // 1) It marks entries in lookup groups from base classes as removed when those members
174      //    are hidden by a derived class.
175      // 2) It adds a new lookup group with the members from a declaring type.
176      // 3) It replaces virtual members with the overridden version, placing the override in the
177      //    lookup group belonging to the base class.
178      foreach (IType type in targetResolveResult.Type.GetNonInterfaceBaseTypes()) {
179       
180        List<IEntity> entities = new List<IEntity>();
181        entities.AddRange(type.GetMembers(options: GetMemberOptions.IgnoreInheritedMembers));
182        if (!targetIsTypeParameter) {
183          var nestedTypes = type.GetNestedTypes(options: GetMemberOptions.IgnoreInheritedMembers | GetMemberOptions.ReturnMemberDefinitions);
184          // GetDefinition() might return null if some IType has a strange implementation of GetNestedTypes.
185          entities.AddRange(nestedTypes.Select(t => t.GetDefinition()).Where(td => td != null));
186        }
187       
188        foreach (var entityGroup in entities.GroupBy(e => e.Name)) {
189         
190          List<LookupGroup> lookupGroups = new List<LookupGroup>();
191          if (!lookupGroupDict.TryGetValue(entityGroup.Key, out lookupGroups))
192            lookupGroupDict.Add(entityGroup.Key, lookupGroups = new List<LookupGroup>());
193         
194          List<IType> newNestedTypes = null;
195          List<IParameterizedMember> newMethods = null;
196          IMember newNonMethod = null;
197         
198          IEnumerable<IType> typeBaseTypes = null;
199         
200          if (!targetIsTypeParameter) {
201            AddNestedTypes(type, entityGroup.OfType<IType>(), 0, lookupGroups, ref typeBaseTypes, ref newNestedTypes);
202          }
203          AddMembers(type, entityGroup.OfType<IMember>(), allowProtectedAccess, lookupGroups, false, ref typeBaseTypes, ref newMethods, ref newNonMethod);
204         
205          if (newNestedTypes != null || newMethods != null || newNonMethod != null)
206            lookupGroups.Add(new LookupGroup(type, newNestedTypes, newMethods, newNonMethod));
207        }
208      }
209     
210      foreach (List<LookupGroup> lookupGroups in lookupGroupDict.Values) {
211        // Remove interface members hidden by class members.
212        if (targetIsTypeParameter) {
213          // This can happen only with type parameters.
214          RemoveInterfaceMembersHiddenByClassMembers(lookupGroups);
215        }
216       
217        // Now report the results:
218        foreach (LookupGroup lookupGroup in lookupGroups) {
219          if (!lookupGroup.MethodsAreHidden) {
220            foreach (IMethod method in lookupGroup.Methods) {
221              yield return method;
222            }
223          }
224          if (!lookupGroup.NonMethodIsHidden) {
225            yield return lookupGroup.NonMethod;
226          }
227          if (lookupGroup.NestedTypes != null) {
228            foreach (IType type in lookupGroup.NestedTypes) {
229              ITypeDefinition typeDef = type.GetDefinition();
230              if (typeDef != null)
231                yield return typeDef;
232            }
233          }
234        }
235      }
236    }
237    #endregion
238   
239    #region class LookupGroup
240    sealed class LookupGroup
241    {
242      public readonly IType DeclaringType;
243     
244      // When a nested type is hidden, it is simply removed from the list.
245      public List<IType> NestedTypes;
246     
247      // When members are hidden, they are merely marked as hidden.
248      // We still need to store the hidden methods so that the 'override' processing can
249      // find them, so that it won't introduce the override as a new method.
250      public readonly List<IParameterizedMember> Methods;
251      public bool MethodsAreHidden;
252     
253      public IMember NonMethod;
254      public bool NonMethodIsHidden;
255     
256      public LookupGroup(IType declaringType, List<IType> nestedTypes, List<IParameterizedMember> methods, IMember nonMethod)
257      {
258        this.DeclaringType = declaringType;
259        this.NestedTypes = nestedTypes;
260        this.Methods = methods;
261        this.NonMethod = nonMethod;
262        this.MethodsAreHidden = (methods == null || methods.Count == 0);
263        this.NonMethodIsHidden = (nonMethod == null);
264      }
265     
266      public bool AllHidden {
267        get {
268          if (NestedTypes != null && NestedTypes.Count > 0)
269            return false;
270          return NonMethodIsHidden && MethodsAreHidden;
271        }
272      }
273    }
274    #endregion
275   
276    #region LookupType
277    public ResolveResult LookupType(IType declaringType, string name, IList<IType> typeArguments, bool parameterizeResultType = true)
278    {
279      if (declaringType == null)
280        throw new ArgumentNullException("declaringType");
281      if (name == null)
282        throw new ArgumentNullException("name");
283      if (typeArguments == null)
284        throw new ArgumentNullException("typeArguments");
285     
286      int typeArgumentCount = typeArguments.Count;
287      Predicate<ITypeDefinition> filter = delegate (ITypeDefinition d) {
288        return InnerTypeParameterCount(d) == typeArgumentCount && d.Name == name && IsAccessible(d, true);
289      };
290     
291      List<LookupGroup> lookupGroups = new List<LookupGroup>();
292      if (declaringType.Kind != TypeKind.TypeParameter) {
293        foreach (IType type in declaringType.GetNonInterfaceBaseTypes()) {
294          List<IType> newNestedTypes = null;
295          IEnumerable<IType> typeBaseTypes = null;
296         
297          IEnumerable<IType> nestedTypes;
298          if (parameterizeResultType) {
299            nestedTypes = type.GetNestedTypes(typeArguments, filter, GetMemberOptions.IgnoreInheritedMembers);
300          } else {
301            nestedTypes = type.GetNestedTypes(filter, GetMemberOptions.IgnoreInheritedMembers | GetMemberOptions.ReturnMemberDefinitions);
302          }
303          AddNestedTypes(type, nestedTypes, typeArgumentCount, lookupGroups, ref typeBaseTypes, ref newNestedTypes);
304         
305          if (newNestedTypes != null)
306            lookupGroups.Add(new LookupGroup(type, newNestedTypes, null, null));
307        }
308      }
309     
310      lookupGroups.RemoveAll(g => g.AllHidden);
311      Debug.Assert(lookupGroups.All(g => g.NestedTypes != null && g.NestedTypes.Count > 0));
312     
313      if (lookupGroups.Count == 0) {
314        return new UnknownMemberResolveResult(declaringType, name, typeArguments);
315      }
316     
317      LookupGroup resultGroup = lookupGroups[lookupGroups.Count - 1];
318     
319      if (resultGroup.NestedTypes.Count > 1 || lookupGroups.Count > 1)
320        return new AmbiguousTypeResolveResult(resultGroup.NestedTypes[0]);
321      else
322        return new TypeResolveResult(resultGroup.NestedTypes[0]);
323    }
324   
325    static int InnerTypeParameterCount(IType type)
326    {
327      // inner types contain the type parameters of outer types. therefore this count has to been adjusted.
328      return type.TypeParameterCount - (type.DeclaringType != null ? type.DeclaringType.TypeParameterCount : 0);
329    }
330    #endregion
331   
332    #region Lookup
333    /// <summary>
334    /// Performs a member lookup.
335    /// </summary>
336    public ResolveResult Lookup(ResolveResult targetResolveResult, string name, IList<IType> typeArguments, bool isInvocation)
337    {
338      if (targetResolveResult == null)
339        throw new ArgumentNullException("targetResolveResult");
340      if (name == null)
341        throw new ArgumentNullException("name");
342      if (typeArguments == null)
343        throw new ArgumentNullException("typeArguments");
344     
345      bool targetIsTypeParameter = targetResolveResult.Type.Kind == TypeKind.TypeParameter;
346     
347      bool allowProtectedAccess = IsProtectedAccessAllowed(targetResolveResult);
348      Predicate<ITypeDefinition> nestedTypeFilter = delegate(ITypeDefinition entity) {
349        return entity.Name == name && IsAccessible(entity, allowProtectedAccess);
350      };
351      Predicate<IUnresolvedMember> memberFilter = delegate(IUnresolvedMember entity) {
352        // NOTE: Atm destructors can be looked up with 'Finalize'
353        return entity.SymbolKind != SymbolKind.Indexer &&
354               entity.SymbolKind != SymbolKind.Operator &&
355               entity.Name == name;
356      };
357     
358      List<LookupGroup> lookupGroups = new List<LookupGroup>();
359      // This loop will handle base types before derived types.
360      // The loop performs three jobs:
361      // 1) It marks entries in lookup groups from base classes as removed when those members
362      //    are hidden by a derived class.
363      // 2) It adds a new lookup group with the members from a declaring type.
364      // 3) It replaces virtual members with the overridden version, placing the override in the
365      //    lookup group belonging to the base class.
366      foreach (IType type in targetResolveResult.Type.GetNonInterfaceBaseTypes()) {
367       
368        List<IType> newNestedTypes = null;
369        List<IParameterizedMember> newMethods = null;
370        IMember newNonMethod = null;
371       
372        IEnumerable<IType> typeBaseTypes = null;
373       
374        if (!isInvocation && !targetIsTypeParameter) {
375          // Consider nested types only if it's not an invocation.
376          // type.GetNestedTypes() is checking the type parameter count for an exact match,
377          // so we don't need to do that in our filter.
378          var nestedTypes = type.GetNestedTypes(typeArguments, nestedTypeFilter, GetMemberOptions.IgnoreInheritedMembers);
379          AddNestedTypes(type, nestedTypes, typeArguments.Count, lookupGroups, ref typeBaseTypes, ref newNestedTypes);
380        }
381       
382        IEnumerable<IMember> members;
383        if (typeArguments.Count == 0) {
384          // Note: IsInvocable-checking cannot be done as part of the filter;
385          // because it must be done after type substitution.
386          members = type.GetMembers(memberFilter, GetMemberOptions.IgnoreInheritedMembers);
387          if (isInvocation)
388            members = members.Where(m => IsInvocable(m));
389        } else {
390          // No need to check for isInvocation/isInvocable here:
391          // we only fetch methods
392          members = type.GetMethods(typeArguments, memberFilter, GetMemberOptions.IgnoreInheritedMembers);
393        }
394        AddMembers(type, members, allowProtectedAccess, lookupGroups, false, ref typeBaseTypes, ref newMethods, ref newNonMethod);
395       
396        if (newNestedTypes != null || newMethods != null || newNonMethod != null)
397          lookupGroups.Add(new LookupGroup(type, newNestedTypes, newMethods, newNonMethod));
398      }
399     
400      // Remove interface members hidden by class members.
401      if (targetIsTypeParameter) {
402        // This can happen only with type parameters.
403        RemoveInterfaceMembersHiddenByClassMembers(lookupGroups);
404      }
405     
406      return CreateResult(targetResolveResult, lookupGroups, name, typeArguments);
407    }
408    #endregion
409   
410    #region Lookup Indexer
411    /// <summary>
412    /// Looks up the indexers on the target type.
413    /// </summary>
414    public IList<MethodListWithDeclaringType> LookupIndexers(ResolveResult targetResolveResult)
415    {
416      if (targetResolveResult == null)
417        throw new ArgumentNullException("targetResolveResult");
418     
419      IType targetType = targetResolveResult.Type;
420      bool allowProtectedAccess = IsProtectedAccessAllowed(targetResolveResult);
421      Predicate<IUnresolvedProperty> filter = p => p.IsIndexer;
422     
423      List<LookupGroup> lookupGroups = new List<LookupGroup>();
424      foreach (IType type in targetType.GetNonInterfaceBaseTypes()) {
425        List<IParameterizedMember> newMethods = null;
426        IMember newNonMethod = null;
427       
428        IEnumerable<IType> typeBaseTypes = null;
429       
430        var members = type.GetProperties(filter, GetMemberOptions.IgnoreInheritedMembers);
431        AddMembers(type, members, allowProtectedAccess, lookupGroups, true, ref typeBaseTypes, ref newMethods, ref newNonMethod);
432       
433        if (newMethods != null || newNonMethod != null)
434          lookupGroups.Add(new LookupGroup(type, null, newMethods, newNonMethod));
435      }
436     
437      // Remove interface members hidden by class members.
438      if (targetType.Kind == TypeKind.TypeParameter) {
439        // This can happen only with type parameters.
440        RemoveInterfaceMembersHiddenByClassMembers(lookupGroups);
441      }
442     
443      // Remove all hidden groups
444      lookupGroups.RemoveAll(g => g.MethodsAreHidden || g.Methods.Count == 0);
445     
446      MethodListWithDeclaringType[] methodLists = new MethodListWithDeclaringType[lookupGroups.Count];
447      for (int i = 0; i < methodLists.Length; i++) {
448        methodLists[i] = new MethodListWithDeclaringType(lookupGroups[i].DeclaringType, lookupGroups[i].Methods);
449      }
450      return methodLists;
451    }
452    #endregion
453   
454    #region AddNestedTypes
455    /// <summary>
456    /// Adds the nested types to 'newNestedTypes' and removes any hidden members from the existing lookup groups.
457    /// </summary>
458    /// <param name="type">Declaring type of the nested types</param>
459    /// <param name="nestedTypes">List of nested types to add.</param>
460    /// <param name="typeArgumentCount">The number of type arguments - used for hiding types from the base class</param>
461    /// <param name="lookupGroups">List of existing lookup groups</param>
462    /// <param name="typeBaseTypes">The base types of 'type' (initialized on demand)</param>
463    /// <param name="newNestedTypes">The target list (created on demand).</param>
464    void AddNestedTypes(IType type, IEnumerable<IType> nestedTypes, int typeArgumentCount,
465                        List<LookupGroup> lookupGroups,
466                        ref IEnumerable<IType> typeBaseTypes,
467                        ref List<IType> newNestedTypes)
468    {
469      foreach (IType nestedType in nestedTypes) {
470        // Remove all non-types declared in a base type of 'type',
471        // and all types with same number of type parameters declared in a base type of 'type'.
472        foreach (var lookupGroup in lookupGroups) {
473          if (lookupGroup.AllHidden)
474            continue; // everything is already hidden
475          if (typeBaseTypes == null)
476            typeBaseTypes = type.GetNonInterfaceBaseTypes();
477         
478          if (typeBaseTypes.Contains(lookupGroup.DeclaringType)) {
479            lookupGroup.MethodsAreHidden = true;
480            lookupGroup.NonMethodIsHidden = true;
481            if (lookupGroup.NestedTypes != null)
482              lookupGroup.NestedTypes.RemoveAll(t => InnerTypeParameterCount(t) == typeArgumentCount);
483          }
484        }
485       
486        // Add the new nested type.
487        if (newNestedTypes == null)
488          newNestedTypes = new List<IType>();
489        newNestedTypes.Add(nestedType);
490      }
491    }
492    #endregion
493   
494    #region AddMembers
495    /// <summary>
496    /// Adds members to 'newMethods'/'newNonMethod'.
497    /// Removes any members in the existing lookup groups that were hidden by added members.
498    /// Substitutes 'virtual' members in the existing lookup groups for added 'override' members.
499    /// </summary>
500    /// <param name="type">Declaring type of the members</param>
501    /// <param name="members">List of members to add.</param>
502    /// <param name="allowProtectedAccess">Whether protected members are accessible</param>
503    /// <param name="lookupGroups">List of existing lookup groups</param>
504    /// <param name="treatAllParameterizedMembersAsMethods">Whether to treat properties as methods</param>
505    /// <param name="typeBaseTypes">The base types of 'type' (initialized on demand)</param>
506    /// <param name="newMethods">The target list for methods (created on demand).</param>
507    /// <param name="newNonMethod">The target variable for non-method members.</param>
508    void AddMembers(IType type, IEnumerable<IMember> members,
509                    bool allowProtectedAccess,
510                    List<LookupGroup> lookupGroups,
511                    bool treatAllParameterizedMembersAsMethods,
512                    ref IEnumerable<IType> typeBaseTypes, ref List<IParameterizedMember> newMethods, ref IMember newNonMethod)
513    {
514      foreach (IMember member in members) {
515        if (!IsAccessible(member, allowProtectedAccess))
516          continue;
517       
518        IParameterizedMember method;
519        if (treatAllParameterizedMembersAsMethods)
520          method = member as IParameterizedMember;
521        else
522          method = member as IMethod;
523       
524        bool replacedVirtualMemberWithOverride = false;
525        if (member.IsOverride) {
526          // Replacing virtual member with override:
527         
528          // Go backwards so that we find the corresponding virtual member
529          // in the most-derived type
530          for (int i = lookupGroups.Count - 1; i >= 0 && !replacedVirtualMemberWithOverride; i--) {
531            if (typeBaseTypes == null)
532              typeBaseTypes = type.GetNonInterfaceBaseTypes();
533           
534            var lookupGroup = lookupGroups[i];
535            if (typeBaseTypes.Contains(lookupGroup.DeclaringType)) {
536              if (method != null) {
537                // Find the matching method, and replace it with the override
538                for (int j = 0; j < lookupGroup.Methods.Count; j++) {
539                  if (SignatureComparer.Ordinal.Equals(method, lookupGroup.Methods[j])) {
540                    lookupGroup.Methods[j] = method;
541                    replacedVirtualMemberWithOverride = true;
542                    break;
543                  }
544                }
545              } else {
546                // If the member type matches, replace it with the override
547                if (lookupGroup.NonMethod != null && lookupGroup.NonMethod.SymbolKind == member.SymbolKind) {
548                  lookupGroup.NonMethod = member;
549                  replacedVirtualMemberWithOverride = true;
550                  break;
551                }
552              }
553            }
554          }
555        }
556        // If the member wasn't an override, or if we didn't find any matching virtual method,
557        // proceed to add the member.
558        if (!replacedVirtualMemberWithOverride) {
559          // Make the member hide other members:
560          foreach (var lookupGroup in lookupGroups) {
561            if (lookupGroup.AllHidden)
562              continue; // everything is already hidden
563            if (typeBaseTypes == null)
564              typeBaseTypes = type.GetNonInterfaceBaseTypes();
565           
566            if (typeBaseTypes.Contains(lookupGroup.DeclaringType)) {
567              // Methods hide all non-methods; Non-methods hide everything
568              lookupGroup.NestedTypes = null;
569              lookupGroup.NonMethodIsHidden = true;
570              if (method == null) { // !(member is IMethod)
571                lookupGroup.MethodsAreHidden = true;
572              }
573            }
574          }
575         
576          // Add the new member
577          if (method != null) {
578            if (newMethods == null)
579              newMethods = new List<IParameterizedMember>();
580            newMethods.Add(method);
581          } else {
582            newNonMethod = member;
583          }
584        }
585      }
586    }
587    #endregion
588   
589    #region RemoveInterfaceMembersHiddenByClassMembers
590    void RemoveInterfaceMembersHiddenByClassMembers(List<LookupGroup> lookupGroups)
591    {
592      foreach (var classLookupGroup in lookupGroups) {
593        if (IsInterfaceOrSystemObject(classLookupGroup.DeclaringType))
594          continue;
595        // The current lookup groups contains class members that might hide interface members
596        bool hasNestedTypes = classLookupGroup.NestedTypes != null && classLookupGroup.NestedTypes.Count > 0;
597        if (hasNestedTypes || !classLookupGroup.NonMethodIsHidden) {
598          // Hide all members from interface types
599          foreach (var interfaceLookupGroup in lookupGroups) {
600            if (IsInterfaceOrSystemObject(interfaceLookupGroup.DeclaringType)) {
601              interfaceLookupGroup.NestedTypes = null;
602              interfaceLookupGroup.NonMethodIsHidden = true;
603              interfaceLookupGroup.MethodsAreHidden = true;
604            }
605          }
606        } else if (!classLookupGroup.MethodsAreHidden) {
607          foreach (var classMethod in classLookupGroup.Methods) {
608            // Hide all non-methods from interface types, and all methods with the same signature
609            // as a method in this class type.
610            foreach (var interfaceLookupGroup in lookupGroups) {
611              if (IsInterfaceOrSystemObject(interfaceLookupGroup.DeclaringType)) {
612                interfaceLookupGroup.NestedTypes = null;
613                interfaceLookupGroup.NonMethodIsHidden = true;
614                if (interfaceLookupGroup.Methods != null && !interfaceLookupGroup.MethodsAreHidden) {
615                  // The mapping of virtual to overridden methods is already done,
616                  // so we can simply remove the methods from the collection
617                  interfaceLookupGroup.Methods.RemoveAll(
618                    m => SignatureComparer.Ordinal.Equals(classMethod, m));
619                }
620              }
621            }
622          }
623        }
624      }
625    }
626   
627    static bool IsInterfaceOrSystemObject(IType type)
628    {
629      // return true if type is an interface or System.Object
630      if (type.Kind == TypeKind.Interface)
631        return true;
632      ITypeDefinition d = type.GetDefinition();
633      return d != null && d.KnownTypeCode == KnownTypeCode.Object;
634    }
635    #endregion
636   
637    #region CreateResult
638    ResolveResult CreateResult(ResolveResult targetResolveResult, List<LookupGroup> lookupGroups, string name, IList<IType> typeArguments)
639    {
640      // Remove all hidden groups
641      lookupGroups.RemoveAll(g => g.AllHidden);
642     
643      if (lookupGroups.Count == 0) {
644        // No members found
645        return new UnknownMemberResolveResult(targetResolveResult.Type, name, typeArguments);
646      }
647     
648      if (lookupGroups.Any(g => !g.MethodsAreHidden && g.Methods.Count > 0)) {
649        // If there are methods, make a MethodGroupResolveResult.
650        // Note that a conflict between a member and a method (possible with multiple interface inheritance)
651        // is only a warning, not an error, and the C# compiler will prefer the method group.
652        List<MethodListWithDeclaringType> methodLists = new List<MethodListWithDeclaringType>();
653        foreach (var lookupGroup in lookupGroups) {
654          if (!lookupGroup.MethodsAreHidden && lookupGroup.Methods.Count > 0) {
655            var methodListWithDeclType = new MethodListWithDeclaringType(lookupGroup.DeclaringType);
656            foreach (var method in lookupGroup.Methods) {
657              methodListWithDeclType.Add((IMethod)method);
658            }
659            methodLists.Add(methodListWithDeclType);
660          }
661        }
662       
663        return new MethodGroupResolveResult(targetResolveResult, name, methodLists, typeArguments);
664      }
665     
666      // If there are ambiguities, report the most-derived result (last group)
667      LookupGroup resultGroup = lookupGroups[lookupGroups.Count - 1];
668      if (resultGroup.NestedTypes != null && resultGroup.NestedTypes.Count > 0) {
669        if (resultGroup.NestedTypes.Count > 1 || !resultGroup.NonMethodIsHidden || lookupGroups.Count > 1)
670          return new AmbiguousTypeResolveResult(resultGroup.NestedTypes[0]);
671        else
672          return new TypeResolveResult(resultGroup.NestedTypes[0]);
673      }
674     
675      if (resultGroup.NonMethod.IsStatic && targetResolveResult is ThisResolveResult) {
676        targetResolveResult = new TypeResolveResult(targetResolveResult.Type);
677      }
678     
679      if (lookupGroups.Count > 1) {
680        return new AmbiguousMemberResolveResult(targetResolveResult, resultGroup.NonMethod);
681      } else {
682        if (isInEnumMemberInitializer) {
683          IField field = resultGroup.NonMethod as IField;
684          if (field != null && field.DeclaringTypeDefinition != null && field.DeclaringTypeDefinition.Kind == TypeKind.Enum) {
685            return new MemberResolveResult(
686              targetResolveResult, field,
687              field.DeclaringTypeDefinition.EnumUnderlyingType,
688              field.IsConst, field.ConstantValue);
689          }
690        }
691        return new MemberResolveResult(targetResolveResult, resultGroup.NonMethod);
692      }
693    }
694    #endregion
695  }
696}
Note: See TracBrowser for help on using the repository browser.