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 | |
---|
19 | using System; |
---|
20 | using System.Collections.Generic; |
---|
21 | using System.Threading; |
---|
22 | using ICSharpCode.NRefactory.TypeSystem.Implementation; |
---|
23 | |
---|
24 | namespace ICSharpCode.NRefactory.TypeSystem |
---|
25 | { |
---|
26 | /// <summary> |
---|
27 | /// Compares parameter lists by comparing the types of all parameters. |
---|
28 | /// </summary> |
---|
29 | /// <remarks> |
---|
30 | /// 'ref int' and 'out int' are considered to be equal. |
---|
31 | /// 'object' and 'dynamic' are also equal. |
---|
32 | /// For generic methods, "Method{T}(T a)" and "Method{S}(S b)" are considered equal. |
---|
33 | /// However, "Method(T a)" and "Method(S b)" are not considered equal when the type parameters T and S belong to classes. |
---|
34 | /// </remarks> |
---|
35 | public sealed class ParameterListComparer : IEqualityComparer<IList<IParameter>> |
---|
36 | { |
---|
37 | public static readonly ParameterListComparer Instance = new ParameterListComparer(); |
---|
38 | |
---|
39 | sealed class NormalizeTypeVisitor : TypeVisitor |
---|
40 | { |
---|
41 | public override IType VisitTypeParameter(ITypeParameter type) |
---|
42 | { |
---|
43 | if (type.OwnerType == SymbolKind.Method) { |
---|
44 | return DummyTypeParameter.GetMethodTypeParameter(type.Index); |
---|
45 | } else { |
---|
46 | return base.VisitTypeParameter(type); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | public override IType VisitTypeDefinition(ITypeDefinition type) |
---|
51 | { |
---|
52 | if (type.KnownTypeCode == KnownTypeCode.Object) |
---|
53 | return SpecialType.Dynamic; |
---|
54 | return base.VisitTypeDefinition(type); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | static readonly NormalizeTypeVisitor normalizationVisitor = new NormalizeTypeVisitor(); |
---|
59 | |
---|
60 | /// <summary> |
---|
61 | /// Replaces all occurrences of method type parameters in the given type |
---|
62 | /// by normalized type parameters. This allows comparing parameter types from different |
---|
63 | /// generic methods. |
---|
64 | /// </summary> |
---|
65 | [Obsolete("Use DummyTypeParameter.NormalizeMethodTypeParameters instead if you only need to normalize type parameters. Also, consider if you need to normalize object vs. dynamic as well.")] |
---|
66 | public IType NormalizeMethodTypeParameters(IType type) |
---|
67 | { |
---|
68 | return DummyTypeParameter.NormalizeMethodTypeParameters(type); |
---|
69 | } |
---|
70 | |
---|
71 | public bool Equals(IList<IParameter> x, IList<IParameter> y) |
---|
72 | { |
---|
73 | if (x == y) |
---|
74 | return true; |
---|
75 | if (x == null || y == null || x.Count != y.Count) |
---|
76 | return false; |
---|
77 | for (int i = 0; i < x.Count; i++) { |
---|
78 | var a = x[i]; |
---|
79 | var b = y[i]; |
---|
80 | if (a == null && b == null) |
---|
81 | continue; |
---|
82 | if (a == null || b == null) |
---|
83 | return false; |
---|
84 | |
---|
85 | // We want to consider the parameter lists "Method<T>(T a)" and "Method<S>(S b)" as equal. |
---|
86 | // However, the parameter types are not considered equal, as T is a different type parameter than S. |
---|
87 | // In order to compare the method signatures, we will normalize all method type parameters. |
---|
88 | IType aType = a.Type.AcceptVisitor(normalizationVisitor); |
---|
89 | IType bType = b.Type.AcceptVisitor(normalizationVisitor); |
---|
90 | |
---|
91 | if (!aType.Equals(bType)) |
---|
92 | return false; |
---|
93 | } |
---|
94 | return true; |
---|
95 | } |
---|
96 | |
---|
97 | public int GetHashCode(IList<IParameter> obj) |
---|
98 | { |
---|
99 | int hashCode = obj.Count; |
---|
100 | unchecked { |
---|
101 | foreach (IParameter p in obj) { |
---|
102 | hashCode *= 27; |
---|
103 | IType type = p.Type.AcceptVisitor(normalizationVisitor); |
---|
104 | hashCode += type.GetHashCode(); |
---|
105 | } |
---|
106 | } |
---|
107 | return hashCode; |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | /// <summary> |
---|
112 | /// Compares member signatures. |
---|
113 | /// </summary> |
---|
114 | /// <remarks> |
---|
115 | /// This comparer checks for equal short name, equal type parameter count, and equal parameter types (using ParameterListComparer). |
---|
116 | /// </remarks> |
---|
117 | public sealed class SignatureComparer : IEqualityComparer<IMember> |
---|
118 | { |
---|
119 | StringComparer nameComparer; |
---|
120 | |
---|
121 | public SignatureComparer(StringComparer nameComparer) |
---|
122 | { |
---|
123 | if (nameComparer == null) |
---|
124 | throw new ArgumentNullException("nameComparer"); |
---|
125 | this.nameComparer = nameComparer; |
---|
126 | } |
---|
127 | |
---|
128 | /// <summary> |
---|
129 | /// Gets a signature comparer that uses an ordinal comparison for the member name. |
---|
130 | /// </summary> |
---|
131 | public static readonly SignatureComparer Ordinal = new SignatureComparer(StringComparer.Ordinal); |
---|
132 | |
---|
133 | public bool Equals(IMember x, IMember y) |
---|
134 | { |
---|
135 | if (x == y) |
---|
136 | return true; |
---|
137 | if (x == null || y == null || x.SymbolKind != y.SymbolKind || !nameComparer.Equals(x.Name, y.Name)) |
---|
138 | return false; |
---|
139 | IParameterizedMember px = x as IParameterizedMember; |
---|
140 | IParameterizedMember py = y as IParameterizedMember; |
---|
141 | if (px != null && py != null) { |
---|
142 | IMethod mx = x as IMethod; |
---|
143 | IMethod my = y as IMethod; |
---|
144 | if (mx != null && my != null && mx.TypeParameters.Count != my.TypeParameters.Count) |
---|
145 | return false; |
---|
146 | return ParameterListComparer.Instance.Equals(px.Parameters, py.Parameters); |
---|
147 | } else { |
---|
148 | return true; |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | public int GetHashCode(IMember obj) |
---|
153 | { |
---|
154 | unchecked { |
---|
155 | int hash = (int)obj.SymbolKind * 33 + nameComparer.GetHashCode(obj.Name); |
---|
156 | IParameterizedMember pm = obj as IParameterizedMember; |
---|
157 | if (pm != null) { |
---|
158 | hash *= 27; |
---|
159 | hash += ParameterListComparer.Instance.GetHashCode(pm.Parameters); |
---|
160 | IMethod m = pm as IMethod; |
---|
161 | if (m != null) |
---|
162 | hash += m.TypeParameters.Count; |
---|
163 | } |
---|
164 | return hash; |
---|
165 | } |
---|
166 | } |
---|
167 | } |
---|
168 | } |
---|