2 Copyright (C) 2011 Jeroen Frijters
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
25 using System.Collections.Generic;
26 using System.Diagnostics;
28 using IKVM.Reflection.Emit;
29 using IKVM.Reflection.Reader;
31 namespace IKVM.Reflection
33 public struct CustomModifiers : IEquatable<CustomModifiers>, IEnumerable<CustomModifiers.Entry>
35 private static readonly Type ModOpt = new MarkerType();
36 private static readonly Type ModReq = new MarkerType();
37 private static readonly Type Initial = ModOpt;
38 private readonly Type[] types;
40 internal CustomModifiers(List<CustomModifiersBuilder.Item> list)
42 bool required = Initial == ModReq;
43 int count = list.Count;
44 foreach (CustomModifiersBuilder.Item item in list)
46 if (item.required != required)
48 required = item.required;
52 types = new Type[count];
53 required = Initial == ModReq;
55 foreach (CustomModifiersBuilder.Item item in list)
57 if (item.required != required)
59 required = item.required;
60 types[index++] = required ? ModReq : ModOpt;
62 types[index++] = item.type;
66 private CustomModifiers(Type[] types)
68 Debug.Assert(types == null || types.Length != 0);
72 public struct Enumerator : IEnumerator<Entry>
74 private readonly Type[] types;
76 private bool required;
78 internal Enumerator(Type[] types)
82 this.required = Initial == ModReq;
85 void System.Collections.IEnumerator.Reset()
88 this.required = Initial == ModReq;
93 get { return new Entry(types[index], required); }
96 public bool MoveNext()
98 if (types == null || index == types.Length)
103 if (index == types.Length)
107 else if (types[index] == ModOpt)
112 else if (types[index] == ModReq)
120 object System.Collections.IEnumerator.Current
122 get { return Current; }
125 void IDisposable.Dispose()
132 private readonly Type type;
133 private readonly bool required;
135 internal Entry(Type type, bool required)
138 this.required = required;
146 public bool IsRequired
148 get { return required; }
152 public Enumerator GetEnumerator()
154 return new Enumerator(types);
157 IEnumerator<Entry> IEnumerable<Entry>.GetEnumerator()
159 return GetEnumerator();
162 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
164 return GetEnumerator();
169 get { return types == null; }
172 public bool Equals(CustomModifiers other)
174 return Util.ArrayEquals(types, other.types);
177 public override bool Equals(object obj)
179 CustomModifiers? other = obj as CustomModifiers?;
180 return other != null && Equals(other.Value);
183 public override int GetHashCode()
185 return Util.GetHashCode(types);
188 public override string ToString()
194 StringBuilder sb = new StringBuilder();
196 foreach (Entry e in this)
198 sb.Append(sep).Append(e.IsRequired ? "modreq(" : "modopt(").Append(e.Type.FullName).Append(')');
201 return sb.ToString();
204 private Type[] GetRequiredOrOptional(bool required)
208 return Type.EmptyTypes;
211 foreach (Entry e in this)
213 if (e.IsRequired == required)
218 Type[] result = new Type[count];
219 foreach (Entry e in this)
221 if (e.IsRequired == required)
223 // FXBUG reflection (and ildasm) return custom modifiers in reverse order
224 // while SRE writes them in the specified order
225 result[--count] = e.Type;
231 internal Type[] GetRequired()
233 return GetRequiredOrOptional(true);
236 internal Type[] GetOptional()
238 return GetRequiredOrOptional(false);
241 internal CustomModifiers Bind(IGenericBinder binder)
247 Type[] result = types;
248 for (int i = 0; i < types.Length; i++)
250 if (types[i] == ModOpt || types[i] == ModReq)
254 Type type = types[i].BindTypeParameters(binder);
255 if (!ReferenceEquals(type, types[i]))
259 result = (Type[])types.Clone();
264 return new CustomModifiers(result);
267 internal static CustomModifiers Read(ModuleReader module, ByteReader br, IGenericContext context)
269 byte b = br.PeekByte();
270 if (!IsCustomModifier(b))
272 return new CustomModifiers();
274 List<Type> list = new List<Type>();
278 Type cmod = br.ReadByte() == Signature.ELEMENT_TYPE_CMOD_REQD ? ModReq : ModOpt;
284 list.Add(Signature.ReadTypeDefOrRefEncoded(module, br, context));
287 while (IsCustomModifier(b));
288 return new CustomModifiers(list.ToArray());
291 internal static void Skip(ByteReader br)
293 byte b = br.PeekByte();
294 while (IsCustomModifier(b))
297 br.ReadCompressedInt();
302 internal static CustomModifiers FromReqOpt(Type[] req, Type[] opt)
304 List<Type> list = null;
305 if (opt != null && opt.Length != 0)
307 list = new List<Type>(opt);
309 if (req != null && req.Length != 0)
313 list = new List<Type>();
320 return new CustomModifiers();
324 return new CustomModifiers(list.ToArray());
328 private static bool IsCustomModifier(byte b)
330 return b == Signature.ELEMENT_TYPE_CMOD_OPT || b == Signature.ELEMENT_TYPE_CMOD_REQD;
333 internal static CustomModifiers Combine(CustomModifiers mods1, CustomModifiers mods2)
339 else if (mods2.IsEmpty)
345 Type[] combo = new Type[mods1.types.Length + mods2.types.Length];
346 Array.Copy(mods1.types, combo, mods1.types.Length);
347 Array.Copy(mods2.types, 0, combo, mods1.types.Length, mods2.types.Length);
348 return new CustomModifiers(combo);