-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathTypeModifierCollection.cs
More file actions
189 lines (160 loc) · 4.51 KB
/
TypeModifierCollection.cs
File metadata and controls
189 lines (160 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* Date: 19.11.2014, Time: 14:55 */
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using IllidanS4.SharpUtils.Reflection.Emit;
namespace IllidanS4.SharpUtils.Reflection
{
public class TypeModifierCollection : ReadOnlyCollection<TypeModifier>, ISignatureElement
{
public static readonly TypeModifierCollection Empty = new TypeModifierCollection(new CustomTypeModifier[0]);
public TypeModifierCollection(IList<TypeModifier> modifiers) : base(modifiers.ToArray())
{
}
public TypeModifierCollection(Type[] required, Type[] optional) : this(GetModifiers(required, optional))
{
}
public CallingConvention CallingConvention{
get{
if(HasModifier(CustomTypeModifier.CallConvCdecl)) return CallingConvention.Cdecl;
if(HasModifier(CustomTypeModifier.CallConvFastcall)) return CallingConvention.FastCall;
if(HasModifier(CustomTypeModifier.CallConvStdcall)) return CallingConvention.StdCall;
if(HasModifier(CustomTypeModifier.CallConvThiscall)) return CallingConvention.ThisCall;
return 0;
}
}
public bool CompilerMarshalOverride{
get{
return HasModifier(CustomTypeModifier.CompilerMarshalOverride);
}
}
public bool IsBoxed{
get{
return HasModifier(CustomTypeModifier.IsBoxed);
}
}
public bool IsByValue{
get{
return HasModifier(CustomTypeModifier.IsByValue);
}
}
public bool IsConst{
get{
return HasModifier(CustomTypeModifier.IsConst);
}
}
public bool IsCopyConstructed{
get{
return HasModifier(CustomTypeModifier.IsCopyConstructed);
}
}
public bool IsExplicitlyDereferenced{
get{
return HasModifier(CustomTypeModifier.IsExplicitlyDereferenced);
}
}
public bool IsImplicitlyDereferenced{
get{
return HasModifier(CustomTypeModifier.IsImplicitlyDereferenced);
}
}
public bool IsJitIntrinsic{
get{
return HasModifier(CustomTypeModifier.IsJitIntrinsic);
}
}
public bool IsLong{
get{
return HasModifier(CustomTypeModifier.IsLong);
}
}
public bool IsPinned{
get{
return HasModifier(CustomTypeModifier.IsPinned);
}
}
public bool IsSignUnspecifiedByte{
get{
return HasModifier(CustomTypeModifier.IsSignUnspecifiedByte);
}
}
public bool IsUdtReturn{
get{
return HasModifier(CustomTypeModifier.IsUdtReturn);
}
}
public bool IsVolatile{
get{
return HasModifier(CustomTypeModifier.IsVolatile);
}
}
public Type BoxedType{
get{
if(IsBoxed)
{
foreach(CustomTypeModifier modifier in this)
{
if(modifier.ModifierType.IsValueType) return modifier.ModifierType;
}
}
throw new Exception("No boxed type is defined.");
}
}
public IEnumerable<CustomTypeModifier> EnumerateCustomModifiers()
{
return this.Select(m => m as CustomTypeModifier).Where(m => m != null);
}
public IEnumerable<Type> EnumerateOptionalCustomModifiers()
{
return EnumerateCustomModifiers().Where(m => m.IsOptional).Select(m => m.ModifierType);
}
public IEnumerable<Type> EnumerateRequiredCustomModifiers()
{
return EnumerateCustomModifiers().Where(m => m.IsRequired).Select(m => m.ModifierType);
}
public bool HasModifier(Type modifierType)
{
return EnumerateCustomModifiers().Any(m => m.ModifierType == modifierType);
}
public bool HasModifier(Type modifierType, bool required)
{
return EnumerateCustomModifiers().Any(m => m.ModifierType == modifierType && m.IsRequired == required);
}
public bool HasModifier(TypeModifier modifier)
{
return this.Any(m => m.Equals(modifier));
}
public bool? GetModifier(Type modifierType)
{
foreach(CustomTypeModifier modifier in this)
{
if(modifier.ModifierType == modifierType) return modifier.IsRequired;
}
return null;
}
private static CustomTypeModifier[] GetModifiers(Type[] required, Type[] optional)
{
CustomTypeModifier[] modifiers = new CustomTypeModifier[required.Length+optional.Length];
for(int i = 0; i < required.Length; i++)
{
modifiers[i] = new CustomTypeModifier(required[i], true);
}
for(int i = 0; i < optional.Length; i++)
{
modifiers[i+required.Length] = new CustomTypeModifier(optional[i], false);
}
return modifiers;
}
void ISignatureElement.AddSignature(SignatureHelper signature)
{
foreach(var modifier in this)
{
signature.AddElement(modifier);
}
}
}
}