Debian Codegraph @

external/ikvm/reflect/Emit/FieldBuilder.cs

raw
1 /*
2 Copyright (C) 2008-2012 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 [email protected]
22
23 */
24 using System;
25 using System.Collections.Generic;
26 using System.Runtime.CompilerServices;
27 using IKVM.Reflection.Metadata;
28 using IKVM.Reflection.Writer;
29
30 namespace IKVM.Reflection.Emit
31 {
32 public sealed class FieldBuilder : FieldInfo
33 {
34 private readonly TypeBuilder typeBuilder;
35 private readonly string name;
36 private readonly int pseudoToken;
37 private FieldAttributes attribs;
38 private readonly int nameIndex;
39 private readonly int signature;
40 private readonly FieldSignature fieldSig;
41
42 internal FieldBuilder(TypeBuilder type, string name, Type fieldType, CustomModifiers customModifiers, FieldAttributes attribs)
43 {
44 this.typeBuilder = type;
45 this.name = name;
46 this.pseudoToken = type.ModuleBuilder.AllocPseudoToken();
47 this.nameIndex = type.ModuleBuilder.Strings.Add(name);
48 this.fieldSig = FieldSignature.Create(fieldType, customModifiers);
49 ByteBuffer sig = new ByteBuffer(5);
50 fieldSig.WriteSig(this.typeBuilder.ModuleBuilder, sig);
51 this.signature = this.typeBuilder.ModuleBuilder.Blobs.Add(sig);
52 this.attribs = attribs;
53 this.typeBuilder.ModuleBuilder.Field.AddVirtualRecord();
54 }
55
56 public void SetConstant(object defaultValue)
57 {
58 attribs |= FieldAttributes.HasDefault;
59 typeBuilder.ModuleBuilder.AddConstant(pseudoToken, defaultValue);
60 }
61
62 public override object GetRawConstantValue()
63 {
64 if (!typeBuilder.IsCreated())
65 {
66 // the .NET FieldBuilder doesn't support this method
67 // (since we dont' have a different FieldInfo object after baking, we will support it once we're baked)
68 throw new NotSupportedException();
69 }
70 return typeBuilder.Module.Constant.GetRawConstantValue(typeBuilder.Module, GetCurrentToken());
71 }
72
73 public void __SetDataAndRVA(byte[] data)
74 {
75 SetDataAndRvaImpl(data, typeBuilder.ModuleBuilder.initializedData, 0);
76 }
77
78 public void __SetReadOnlyDataAndRVA(byte[] data)
79 {
80 SetDataAndRvaImpl(data, typeBuilder.ModuleBuilder.methodBodies, unchecked((int)0x80000000));
81 }
82
83 private void SetDataAndRvaImpl(byte[] data, ByteBuffer bb, int readonlyMarker)
84 {
85 attribs |= FieldAttributes.HasFieldRVA;
86 FieldRVATable.Record rec = new FieldRVATable.Record();
87 bb.Align(8);
88 rec.RVA = bb.Position + readonlyMarker;
89 rec.Field = pseudoToken;
90 typeBuilder.ModuleBuilder.FieldRVA.AddRecord(rec);
91 bb.Write(data);
92 }
93
94 public override void __GetDataFromRVA(byte[] data, int offset, int length)
95 {
96 throw new NotImplementedException();
97 }
98
99 public override int __FieldRVA
100 {
101 get { throw new NotImplementedException(); }
102 }
103
104 public override bool __TryGetFieldOffset(out int offset)
105 {
106 int pseudoTokenOrIndex = pseudoToken;
107 if (typeBuilder.ModuleBuilder.IsSaved)
108 {
109 pseudoTokenOrIndex = typeBuilder.ModuleBuilder.ResolvePseudoToken(pseudoToken) & 0xFFFFFF;
110 }
111 foreach (int i in this.Module.FieldLayout.Filter(pseudoTokenOrIndex))
112 {
113 offset = this.Module.FieldLayout.records[i].Offset;
114 return true;
115 }
116 offset = 0;
117 return false;
118 }
119
120 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
121 {
122 SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute));
123 }
124
125 public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
126 {
127 switch (customBuilder.KnownCA)
128 {
129 case KnownCA.FieldOffsetAttribute:
130 SetOffset((int)customBuilder.DecodeBlob(this.Module.Assembly).GetConstructorArgument(0));
131 break;
132 case KnownCA.MarshalAsAttribute:
133 FieldMarshal.SetMarshalAsAttribute(typeBuilder.ModuleBuilder, pseudoToken, customBuilder);
134 attribs |= FieldAttributes.HasFieldMarshal;
135 break;
136 case KnownCA.NonSerializedAttribute:
137 attribs |= FieldAttributes.NotSerialized;
138 break;
139 case KnownCA.SpecialNameAttribute:
140 attribs |= FieldAttributes.SpecialName;
141 break;
142 default:
143 typeBuilder.ModuleBuilder.SetCustomAttribute(pseudoToken, customBuilder);
144 break;
145 }
146 }
147
148 public void SetOffset(int iOffset)
149 {
150 FieldLayoutTable.Record rec = new FieldLayoutTable.Record();
151 rec.Offset = iOffset;
152 rec.Field = pseudoToken;
153 typeBuilder.ModuleBuilder.FieldLayout.AddRecord(rec);
154 }
155
156 public override FieldAttributes Attributes
157 {
158 get { return attribs; }
159 }
160
161 public override Type DeclaringType
162 {
163 get { return typeBuilder.IsModulePseudoType ? null : typeBuilder; }
164 }
165
166 public override string Name
167 {
168 get { return name; }
169 }
170
171 public override int MetadataToken
172 {
173 get { return pseudoToken; }
174 }
175
176 public override Module Module
177 {
178 get { return typeBuilder.Module; }
179 }
180
181 public FieldToken GetToken()
182 {
183 return new FieldToken(pseudoToken);
184 }
185
186 internal void WriteFieldRecords(MetadataWriter mw)
187 {
188 mw.Write((short)attribs);
189 mw.WriteStringIndex(nameIndex);
190 mw.WriteBlobIndex(signature);
191 }
192
193 internal void FixupToken(int token)
194 {
195 typeBuilder.ModuleBuilder.RegisterTokenFixup(this.pseudoToken, token);
196 }
197
198 internal override FieldSignature FieldSignature
199 {
200 get { return fieldSig; }
201 }
202
203 internal override int ImportTo(ModuleBuilder other)
204 {
205 return other.ImportMethodOrField(typeBuilder, name, fieldSig);
206 }
207
208 internal override int GetCurrentToken()
209 {
210 if (typeBuilder.ModuleBuilder.IsSaved)
211 {
212 return typeBuilder.ModuleBuilder.ResolvePseudoToken(pseudoToken);
213 }
214 else
215 {
216 return pseudoToken;
217 }
218 }
219
220 internal override bool IsBaked
221 {
222 get { return typeBuilder.IsBaked; }
223 }
224 }
225 }