5 // Jb Evain (jbevain@gmail.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 namespace Mono.Cecil {
32 using System.Collections;
34 using Mono.Cecil.Metadata;
36 sealed class TableComparers {
38 internal sealed class TypeDef : IComparer {
40 public static readonly TypeDef Instance = new TypeDef ();
42 public int Compare (object x, object y)
44 TypeDefinition a = x as TypeDefinition;
45 TypeDefinition b = y as TypeDefinition;
47 if (a == null || b == null)
48 throw new ReflectionException ("TypeDefComparer can only compare TypeDefinition");
50 if (a.Name == Constants.ModuleType && b.Name == Constants.ModuleType)
52 else if (a.Name == Constants.ModuleType)
54 else if (b.Name == Constants.ModuleType)
57 return Comparer.Default.Compare (a.FullName, b.FullName);
61 internal sealed class TypeRef : IComparer {
63 public static readonly TypeRef Instance = new TypeRef ();
65 public int Compare (object x, object y)
67 TypeReference a = x as TypeReference;
68 TypeReference b = y as TypeReference;
70 if (a == null || b == null)
71 throw new ReflectionException ("TypeRefComparer can only compare TypeReference");
73 if (b.DeclaringType == a)
75 else if (a.DeclaringType == b)
78 return Comparer.Default.Compare (a.FullName, b.FullName);
82 internal sealed class NestedClass : IComparer {
84 public static readonly NestedClass Instance = new NestedClass ();
86 public int Compare (object x, object y)
88 NestedClassRow a = x as NestedClassRow;
89 NestedClassRow b = y as NestedClassRow;
91 return Comparer.Default.Compare (a.NestedClass, b.NestedClass);
95 internal sealed class Constant : IComparer {
97 public static readonly Constant Instance = new Constant ();
99 public int Compare (object x, object y)
101 ConstantRow a = x as ConstantRow;
102 ConstantRow b = y as ConstantRow;
104 return Comparer.Default.Compare (
105 Utilities.CompressMetadataToken (CodedIndex.HasConstant, a.Parent),
106 Utilities.CompressMetadataToken (CodedIndex.HasConstant, b.Parent));
111 internal sealed class InterfaceImpl : IComparer {
113 public static readonly InterfaceImpl Instance = new InterfaceImpl ();
115 public int Compare (object x, object y)
117 InterfaceImplRow a = x as InterfaceImplRow;
118 InterfaceImplRow b = y as InterfaceImplRow;
120 int klass = Comparer.Default.Compare (a.Class, b.Class);
123 return Comparer.Default.Compare (
124 Utilities.CompressMetadataToken (CodedIndex.TypeDefOrRef, a.Interface),
125 Utilities.CompressMetadataToken (CodedIndex.TypeDefOrRef, b.Interface));
131 internal sealed class MethodSem : IComparer {
133 public static readonly MethodSem Instance = new MethodSem ();
135 public int Compare (object x, object y)
137 MethodSemanticsRow a = x as MethodSemanticsRow;
138 MethodSemanticsRow b = y as MethodSemanticsRow;
140 return Comparer.Default.Compare (
141 Utilities.CompressMetadataToken (CodedIndex.HasSemantics, a.Association),
142 Utilities.CompressMetadataToken (CodedIndex.HasSemantics, b.Association));
146 internal sealed class CustomAttribute : IComparer {
148 public static readonly CustomAttribute Instance = new CustomAttribute ();
150 public int Compare (object x, object y)
152 CustomAttributeRow a = x as CustomAttributeRow;
153 CustomAttributeRow b = y as CustomAttributeRow;
155 return Comparer.Default.Compare (
156 Utilities.CompressMetadataToken (CodedIndex.HasCustomAttribute, a.Parent),
157 Utilities.CompressMetadataToken (CodedIndex.HasCustomAttribute, b.Parent));
161 internal sealed class SecurityDeclaration : IComparer {
163 public static readonly SecurityDeclaration Instance = new SecurityDeclaration ();
165 public int Compare (object x, object y)
167 DeclSecurityRow a = x as DeclSecurityRow;
168 DeclSecurityRow b = y as DeclSecurityRow;
170 return Comparer.Default.Compare (
171 Utilities.CompressMetadataToken (CodedIndex.HasDeclSecurity, a.Parent),
172 Utilities.CompressMetadataToken (CodedIndex.HasDeclSecurity, b.Parent));
176 internal sealed class Override : IComparer {
178 public static readonly Override Instance = new Override ();
180 public int Compare (object x, object y)
182 MethodImplRow a = x as MethodImplRow;
183 MethodImplRow b = y as MethodImplRow;
185 return Comparer.Default.Compare (a.Class, b.Class);
189 internal sealed class PInvoke : IComparer {
191 public static readonly PInvoke Instance = new PInvoke ();
193 public int Compare (object x, object y)
195 ImplMapRow a = x as ImplMapRow;
196 ImplMapRow b = y as ImplMapRow;
198 return Comparer.Default.Compare (a.MemberForwarded.RID, b.MemberForwarded.RID);
202 internal sealed class FieldRVA : IComparer {
204 public static readonly FieldRVA Instance = new FieldRVA ();
206 public int Compare (object x, object y)
208 FieldRVARow a = x as FieldRVARow;
209 FieldRVARow b = y as FieldRVARow;
211 return Comparer.Default.Compare (a.Field, b.Field);
215 internal sealed class FieldLayout : IComparer {
217 public static readonly FieldLayout Instance = new FieldLayout ();
219 public int Compare (object x, object y)
221 FieldLayoutRow a = x as FieldLayoutRow;
222 FieldLayoutRow b = y as FieldLayoutRow;
224 return Comparer.Default.Compare (a.Field, b.Field);
228 internal sealed class FieldMarshal : IComparer {
230 public static readonly FieldMarshal Instance = new FieldMarshal ();
232 public int Compare (object x, object y)
234 FieldMarshalRow a = x as FieldMarshalRow;
235 FieldMarshalRow b = y as FieldMarshalRow;
237 return Comparer.Default.Compare (
238 Utilities.CompressMetadataToken (CodedIndex.HasFieldMarshal, a.Parent),
239 Utilities.CompressMetadataToken (CodedIndex.HasFieldMarshal, b.Parent));
243 internal sealed class TypeLayout : IComparer {
245 public static readonly TypeLayout Instance = new TypeLayout ();
247 public int Compare (object x, object y)
249 ClassLayoutRow a = x as ClassLayoutRow;
250 ClassLayoutRow b = y as ClassLayoutRow;
252 return Comparer.Default.Compare (a.Parent, b.Parent);
256 internal sealed class GenericParam : IComparer {
258 public static readonly GenericParam Instance = new GenericParam ();
260 public int Compare (object x, object y)
262 GenericParameter a = x as GenericParameter;
263 GenericParameter b = y as GenericParameter;
265 int token = Comparer.Default.Compare (
266 Utilities.CompressMetadataToken (CodedIndex.TypeOrMethodDef, a.Owner.MetadataToken),
267 Utilities.CompressMetadataToken (CodedIndex.TypeOrMethodDef, b.Owner.MetadataToken));
270 return Comparer.Default.Compare (a.Position, b.Position);