Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.13/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory-5.5.0/TypeSystem/Implementation/DefaultMemberReference.cs @ 18242

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

#2077: created branch and added first version

File size: 4.9 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.Linq;
22
23namespace ICSharpCode.NRefactory.TypeSystem.Implementation
24{
25  /// <summary>
26  /// References an entity by its type and name.
27  /// This class can be used to refer to all members except for constructors and explicit interface implementations.
28  /// </summary>
29  /// <remarks>
30  /// Resolving a DefaultMemberReference requires a context that provides enough information for resolving the declaring type reference
31  /// and the parameter types references.
32  /// </remarks>
33  [Serializable]
34  public sealed class DefaultMemberReference : IMemberReference, ISupportsInterning
35  {
36    readonly SymbolKind symbolKind;
37    readonly ITypeReference typeReference;
38    readonly string name;
39    readonly int typeParameterCount;
40    readonly IList<ITypeReference> parameterTypes;
41   
42    public DefaultMemberReference(SymbolKind symbolKind, ITypeReference typeReference, string name, int typeParameterCount = 0, IList<ITypeReference> parameterTypes = null)
43    {
44      if (typeReference == null)
45        throw new ArgumentNullException("typeReference");
46      if (name == null)
47        throw new ArgumentNullException("name");
48      if (typeParameterCount != 0 && symbolKind != SymbolKind.Method)
49        throw new ArgumentException("Type parameter count > 0 is only supported for methods.");
50      this.symbolKind = symbolKind;
51      this.typeReference = typeReference;
52      this.name = name;
53      this.typeParameterCount = typeParameterCount;
54      this.parameterTypes = parameterTypes ?? EmptyList<ITypeReference>.Instance;
55    }
56   
57    public ITypeReference DeclaringTypeReference {
58      get { return typeReference; }
59    }
60   
61    public IMember Resolve(ITypeResolveContext context)
62    {
63      IType type = typeReference.Resolve(context);
64      IEnumerable<IMember> members;
65      if (symbolKind == SymbolKind.Accessor) {
66        members = type.GetAccessors(
67          m => m.Name == name && !m.IsExplicitInterfaceImplementation,
68          GetMemberOptions.IgnoreInheritedMembers);
69      } else if (symbolKind == SymbolKind.Method) {
70        members = type.GetMethods(
71          m => m.Name == name && m.SymbolKind == SymbolKind.Method
72          && m.TypeParameters.Count == typeParameterCount && !m.IsExplicitInterfaceImplementation,
73          GetMemberOptions.IgnoreInheritedMembers);
74      } else {
75        members = type.GetMembers(
76          m => m.Name == name && m.SymbolKind == symbolKind && !m.IsExplicitInterfaceImplementation,
77          GetMemberOptions.IgnoreInheritedMembers);
78      }
79      var resolvedParameterTypes = parameterTypes.Resolve(context);
80      foreach (IMember member in members) {
81        IParameterizedMember parameterizedMember = member as IParameterizedMember;
82        if (parameterizedMember == null) {
83          if (parameterTypes.Count == 0)
84            return member;
85        } else if (parameterTypes.Count == parameterizedMember.Parameters.Count) {
86          bool signatureMatches = true;
87          for (int i = 0; i < parameterTypes.Count; i++) {
88            IType type1 = DummyTypeParameter.NormalizeAllTypeParameters(resolvedParameterTypes[i]);
89            IType type2 = DummyTypeParameter.NormalizeAllTypeParameters(parameterizedMember.Parameters[i].Type);
90            if (!type1.Equals(type2)) {
91              signatureMatches = false;
92              break;
93            }
94          }
95          if (signatureMatches)
96            return member;
97        }
98      }
99      return null;
100    }
101   
102    ISymbol ISymbolReference.Resolve(ITypeResolveContext context)
103    {
104      return ((IMemberReference)this).Resolve(context);
105    }
106   
107    int ISupportsInterning.GetHashCodeForInterning()
108    {
109      return (int)symbolKind ^ typeReference.GetHashCode() ^ name.GetHashCode() ^ parameterTypes.GetHashCode();
110    }
111   
112    bool ISupportsInterning.EqualsForInterning(ISupportsInterning other)
113    {
114      DefaultMemberReference o = other as DefaultMemberReference;
115      return o != null && symbolKind == o.symbolKind && typeReference == o.typeReference && name == o.name && parameterTypes == o.parameterTypes;
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.