IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
GenericTypeParameterBuilder.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2008-2011 Jeroen Frijters
3
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.
7
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:
11
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.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25using System.Reflection.Metadata.Ecma335;
26
29
31{
32
33 internal sealed class GenericTypeParameterBuilder : TypeInfo
34 {
35
36 readonly string name;
37 readonly TypeBuilder type;
38 readonly MethodBuilder method;
39 readonly int paramPseudoIndex;
40 readonly int position;
41 int typeToken;
42 Type baseType;
43 GenericParameterAttributes attr;
44
51 internal GenericTypeParameterBuilder(string name, TypeBuilder type, int position) :
52 this(name, type, null, position, Signature.ELEMENT_TYPE_VAR)
53 {
54
55 }
56
63 internal GenericTypeParameterBuilder(string name, MethodBuilder method, int position) :
64 this(name, null, method, position, Signature.ELEMENT_TYPE_MVAR)
65 {
66
67 }
68
77 GenericTypeParameterBuilder(string name, TypeBuilder type, MethodBuilder method, int position, byte sigElementType) :
78 base(sigElementType)
79 {
80 this.name = name;
81 this.type = type;
82 this.method = method;
83 this.position = position;
84 var rec = new GenericParamTable.Record();
85 rec.Number = (short)position;
86 rec.Flags = 0;
87 rec.Owner = type != null ? type.MetadataToken : method.MetadataToken;
88 rec.Name = ModuleBuilder.GetOrAddString(name);
89 paramPseudoIndex = ModuleBuilder.GenericParamTable.AddRecord(rec);
90 }
91
92 public override string AssemblyQualifiedName
93 {
94 get { return null; }
95 }
96
97 protected override bool IsValueTypeImpl
98 {
99 get { return (this.GenericParameterAttributes & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0; }
100 }
101
102 public override Type BaseType
103 {
104 get { return baseType; }
105 }
106
107 public override Type[] __GetDeclaredInterfaces()
108 {
109 throw new NotImplementedException();
110 }
111
112 public override TypeAttributes Attributes
113 {
114 get { return TypeAttributes.Public; }
115 }
116
117 public override string Namespace
118 {
119 get { return DeclaringType.Namespace; }
120 }
121
122 public override string Name
123 {
124 get { return name; }
125 }
126
127 public override string FullName
128 {
129 get { return null; }
130 }
131
132 public override string ToString()
133 {
134 return this.Name;
135 }
136
137 private ModuleBuilder ModuleBuilder
138 {
139 get { return type != null ? type.ModuleBuilder : method.ModuleBuilder; }
140 }
141
142 public override Module Module
143 {
144 get { return ModuleBuilder; }
145 }
146
147 public override int GenericParameterPosition
148 {
149 get { return position; }
150 }
151
152 public override Type DeclaringType
153 {
154 get { return type; }
155 }
156
157 public override MethodBase DeclaringMethod
158 {
159 get { return method; }
160 }
161
162 public override Type[] GetGenericParameterConstraints()
163 {
164 throw new NotImplementedException();
165 }
166
167 public override CustomModifiers[] __GetGenericParameterConstraintCustomModifiers()
168 {
169 throw new NotImplementedException();
170 }
171
172 public override GenericParameterAttributes GenericParameterAttributes
173 {
174 get
175 {
176 CheckBaked();
177 return attr;
178 }
179 }
180
181 internal override void CheckBaked()
182 {
183 if (type != null)
184 {
185 type.CheckBaked();
186 }
187 else
188 {
189 method.CheckBaked();
190 }
191 }
192
193 private void AddConstraint(Type type)
194 {
195 var rec = new GenericParamConstraintTable.Record();
196 rec.Owner = MetadataTokens.GetToken(MetadataTokens.GenericParameterHandle(paramPseudoIndex));
197 rec.Constraint = ModuleBuilder.GetTypeTokenForMemberRef(type);
198 this.ModuleBuilder.GenericParamConstraint.AddRecord(rec);
199 }
200
201 public void SetBaseTypeConstraint(Type? baseTypeConstraint)
202 {
203 this.baseType = baseTypeConstraint;
204 AddConstraint(baseTypeConstraint);
205 }
206
207 public void SetInterfaceConstraints(params Type[] interfaceConstraints)
208 {
209 foreach (Type type in interfaceConstraints)
210 {
211 AddConstraint(type);
212 }
213 }
214
215 public void SetGenericParameterAttributes(GenericParameterAttributes genericParameterAttributes)
216 {
217 this.attr = genericParameterAttributes;
218 // for now we'll back patch the table
219 this.ModuleBuilder.GenericParamTable.PatchAttribute(paramPseudoIndex, genericParameterAttributes);
220 }
221
222 public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
223 {
224 this.ModuleBuilder.SetCustomAttribute((GenericParamTable.Index << 24) | paramPseudoIndex, customBuilder);
225 }
226
227 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
228 {
229 SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute));
230 }
231
232 public override int MetadataToken
233 {
234 get
235 {
236 CheckBaked();
237 return (GenericParamTable.Index << 24) | paramPseudoIndex;
238 }
239 }
240
241 internal override int GetModuleBuilderToken()
242 {
243 if (typeToken == 0)
244 {
245 var spec = new ByteBuffer(5);
246 Signature.WriteTypeSpec(ModuleBuilder, spec, this);
247 typeToken = MetadataTokens.GetToken(MetadataTokens.TypeSpecificationHandle(ModuleBuilder.TypeSpecTable.AddRecord(ModuleBuilder.GetOrAddBlob(spec.ToArray()))));
248 }
249 return typeToken;
250 }
251
252 internal override Type BindTypeParameters(IGenericBinder binder)
253 {
254 if (type != null)
255 {
256 return binder.BindTypeParameter(this);
257 }
258 else
259 {
260 return binder.BindMethodParameter(this);
261 }
262 }
263
264 internal override int GetCurrentToken()
265 {
266 if (ModuleBuilder.IsSaved)
267 {
268 return (GenericParamTable.Index << 24) | Module.GenericParamTable.GetIndexFixup()[paramPseudoIndex - 1] + 1;
269 }
270 else
271 {
272 return (GenericParamTable.Index << 24) | paramPseudoIndex;
273 }
274 }
275
276 internal override bool IsBaked
277 {
278 get { return ((MemberInfo)type ?? method).IsBaked; }
279 }
280
281 }
282
283}
IKVM.Reflection.Module Module
IKVM.Reflection.Type Type
IKVM.Reflection.ConstructorInfo ConstructorInfo
IKVM.Reflection.MemberInfo MemberInfo
IKVM.Reflection.MethodBase MethodBase
global::java.lang.invoke.LambdaForm.Name Name
Type BindMethodParameter(Type type)
Type BindTypeParameter(Type type)