[cil-strip] Upgrade to latest Mono.Cecil API
[mono.git] / mcs / tools / cil-strip / Mono.Cecil / TableComparers.cs
1 //
2 // TableComparers.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@gmail.com)
6 //
7 // (C) 2005 Jb Evain
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27 //
28
29 namespace Mono.Cecil {
30
31         using System;
32         using System.Collections;
33
34         using Mono.Cecil.Metadata;
35
36         sealed class TableComparers {
37
38                 internal sealed class TypeDef : IComparer {
39
40                         public static readonly TypeDef Instance = new TypeDef ();
41
42                         public int Compare (object x, object y)
43                         {
44                                 TypeDefinition a = x as TypeDefinition;
45                                 TypeDefinition b = y as TypeDefinition;
46
47                                 if (a == null || b == null)
48                                         throw new ReflectionException ("TypeDefComparer can only compare TypeDefinition");
49
50                                 if (a.Name == Constants.ModuleType && b.Name == Constants.ModuleType)
51                                         return 0;
52                                 else if (a.Name == Constants.ModuleType)
53                                         return -1;
54                                 else if (b.Name == Constants.ModuleType)
55                                         return 1;
56
57                                 return Comparer.Default.Compare (a.FullName, b.FullName);
58                         }
59                 }
60
61                 internal sealed class TypeRef : IComparer {
62
63                         public static readonly TypeRef Instance = new TypeRef ();
64
65                         public int Compare (object x, object y)
66                         {
67                                 TypeReference a = x as TypeReference;
68                                 TypeReference b = y as TypeReference;
69
70                                 if (a == null || b == null)
71                                         throw new ReflectionException ("TypeRefComparer can only compare TypeReference");
72
73                                 if (b.DeclaringType == a)
74                                         return -1;
75                                 else if (a.DeclaringType == b)
76                                         return 1;
77
78                                 return Comparer.Default.Compare (a.FullName, b.FullName);
79                         }
80                 }
81
82                 internal sealed class NestedClass : IComparer {
83
84                         public static readonly NestedClass Instance = new NestedClass ();
85
86                         public int Compare (object x, object y)
87                         {
88                                 NestedClassRow a = x as NestedClassRow;
89                                 NestedClassRow b = y as NestedClassRow;
90
91                                 return Comparer.Default.Compare (a.NestedClass, b.NestedClass);
92                         }
93                 }
94
95                 internal sealed class Constant : IComparer {
96
97                         public static readonly Constant Instance = new Constant ();
98
99                         public int Compare (object x, object y)
100                         {
101                                 ConstantRow a = x as ConstantRow;
102                                 ConstantRow b = y as ConstantRow;
103
104                                 return Comparer.Default.Compare (
105                                         Utilities.CompressMetadataToken (CodedIndex.HasConstant, a.Parent),
106                                         Utilities.CompressMetadataToken (CodedIndex.HasConstant, b.Parent));
107                         }
108
109                 }
110
111                 internal sealed class InterfaceImpl : IComparer {
112
113                         public static readonly InterfaceImpl Instance = new InterfaceImpl ();
114
115                         public int Compare (object x, object y)
116                         {
117                                 InterfaceImplRow a = x as InterfaceImplRow;
118                                 InterfaceImplRow b = y as InterfaceImplRow;
119
120                                 int klass = Comparer.Default.Compare (a.Class, b.Class);
121
122                                 if (klass == 0)
123                                         return Comparer.Default.Compare (
124                                                 Utilities.CompressMetadataToken (CodedIndex.TypeDefOrRef, a.Interface),
125                                                 Utilities.CompressMetadataToken (CodedIndex.TypeDefOrRef, b.Interface));
126
127                                 return klass;
128                         }
129                 }
130
131                 internal sealed class MethodSem : IComparer {
132
133                         public static readonly MethodSem Instance = new MethodSem ();
134
135                         public int Compare (object x, object y)
136                         {
137                                 MethodSemanticsRow a = x as MethodSemanticsRow;
138                                 MethodSemanticsRow b = y as MethodSemanticsRow;
139
140                                 return Comparer.Default.Compare (
141                                         Utilities.CompressMetadataToken (CodedIndex.HasSemantics, a.Association),
142                                         Utilities.CompressMetadataToken (CodedIndex.HasSemantics, b.Association));
143                         }
144                 }
145
146                 internal sealed class CustomAttribute : IComparer {
147
148                         public static readonly CustomAttribute Instance = new CustomAttribute ();
149
150                         public int Compare (object x, object y)
151                         {
152                                 CustomAttributeRow a = x as CustomAttributeRow;
153                                 CustomAttributeRow b = y as CustomAttributeRow;
154
155                                 return Comparer.Default.Compare (
156                                         Utilities.CompressMetadataToken (CodedIndex.HasCustomAttribute, a.Parent),
157                                         Utilities.CompressMetadataToken (CodedIndex.HasCustomAttribute, b.Parent));
158                         }
159                 }
160
161                 internal sealed class SecurityDeclaration : IComparer {
162
163                         public static readonly SecurityDeclaration Instance = new SecurityDeclaration ();
164
165                         public int Compare (object x, object y)
166                         {
167                                 DeclSecurityRow a = x as DeclSecurityRow;
168                                 DeclSecurityRow b = y as DeclSecurityRow;
169
170                                 return Comparer.Default.Compare (
171                                         Utilities.CompressMetadataToken (CodedIndex.HasDeclSecurity, a.Parent),
172                                         Utilities.CompressMetadataToken (CodedIndex.HasDeclSecurity, b.Parent));
173                         }
174                 }
175
176                 internal sealed class Override : IComparer {
177
178                         public static readonly Override Instance = new Override ();
179
180                         public int Compare (object x, object y)
181                         {
182                                 MethodImplRow a = x as MethodImplRow;
183                                 MethodImplRow b = y as MethodImplRow;
184
185                                 return Comparer.Default.Compare (a.Class, b.Class);
186                         }
187                 }
188
189                 internal sealed class PInvoke : IComparer {
190
191                         public static readonly PInvoke Instance = new PInvoke ();
192
193                         public int Compare (object x, object y)
194                         {
195                                 ImplMapRow a = x as ImplMapRow;
196                                 ImplMapRow b = y as ImplMapRow;
197
198                                 return Comparer.Default.Compare (a.MemberForwarded.RID, b.MemberForwarded.RID);
199                         }
200                 }
201
202                 internal sealed class FieldRVA : IComparer {
203
204                         public static readonly FieldRVA Instance = new FieldRVA ();
205
206                         public int Compare (object x, object y)
207                         {
208                                 FieldRVARow a = x as FieldRVARow;
209                                 FieldRVARow b = y as FieldRVARow;
210
211                                 return Comparer.Default.Compare (a.Field, b.Field);
212                         }
213                 }
214
215                 internal sealed class FieldLayout : IComparer {
216
217                         public static readonly FieldLayout Instance = new FieldLayout ();
218
219                         public int Compare (object x, object y)
220                         {
221                                 FieldLayoutRow a = x as FieldLayoutRow;
222                                 FieldLayoutRow b = y as FieldLayoutRow;
223
224                                 return Comparer.Default.Compare (a.Field, b.Field);
225                         }
226                 }
227
228                 internal sealed class FieldMarshal : IComparer {
229
230                         public static readonly FieldMarshal Instance = new FieldMarshal ();
231
232                         public int Compare (object x, object y)
233                         {
234                                 FieldMarshalRow a = x as FieldMarshalRow;
235                                 FieldMarshalRow b = y as FieldMarshalRow;
236
237                                 return Comparer.Default.Compare (
238                                         Utilities.CompressMetadataToken (CodedIndex.HasFieldMarshal, a.Parent),
239                                         Utilities.CompressMetadataToken (CodedIndex.HasFieldMarshal, b.Parent));
240                         }
241                 }
242
243                 internal sealed class TypeLayout : IComparer {
244
245                         public static readonly TypeLayout Instance = new TypeLayout ();
246
247                         public int Compare (object x, object y)
248                         {
249                                 ClassLayoutRow a = x as ClassLayoutRow;
250                                 ClassLayoutRow b = y as ClassLayoutRow;
251
252                                 return Comparer.Default.Compare (a.Parent, b.Parent);
253                         }
254                 }
255
256                 internal sealed class GenericParam : IComparer {
257
258                         public static readonly GenericParam Instance = new GenericParam ();
259
260                         public int Compare (object x, object y)
261                         {
262                                 GenericParameter a = x as GenericParameter;
263                                 GenericParameter b = y as GenericParameter;
264
265                                 int token = Comparer.Default.Compare (
266                                         Utilities.CompressMetadataToken (CodedIndex.TypeOrMethodDef, a.Owner.MetadataToken),
267                                         Utilities.CompressMetadataToken (CodedIndex.TypeOrMethodDef, b.Owner.MetadataToken));
268
269                                 if (token == 0)
270                                         return Comparer.Default.Compare (a.Position, b.Position);
271
272                                 return token;
273                         }
274                 }
275         }
276 }