Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[mono.git] / mcs / class / IKVM.Reflection / Emit / FieldBuilder.cs
1 /*
2   Copyright (C) 2008 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 */
24 using System;
25 using System.Runtime.CompilerServices;
26 using IKVM.Reflection.Metadata;
27 using IKVM.Reflection.Writer;
28
29 namespace IKVM.Reflection.Emit
30 {
31         public sealed class FieldBuilder : FieldInfo
32         {
33                 private readonly TypeBuilder typeBuilder;
34                 private readonly string name;
35                 private readonly int pseudoToken;
36                 private FieldAttributes attribs;
37                 private readonly int nameIndex;
38                 private readonly int signature;
39                 private readonly FieldSignature fieldSig;
40
41                 internal FieldBuilder(TypeBuilder type, string name, Type fieldType, CustomModifiers customModifiers, FieldAttributes attribs)
42                 {
43                         this.typeBuilder = type;
44                         this.name = name;
45                         this.pseudoToken = type.ModuleBuilder.AllocPseudoToken();
46                         this.nameIndex = type.ModuleBuilder.Strings.Add(name);
47                         this.fieldSig = FieldSignature.Create(fieldType, customModifiers);
48                         ByteBuffer sig = new ByteBuffer(5);
49                         fieldSig.WriteSig(this.typeBuilder.ModuleBuilder, sig);
50                         this.signature = this.typeBuilder.ModuleBuilder.Blobs.Add(sig);
51                         this.attribs = attribs;
52                         this.typeBuilder.ModuleBuilder.Field.AddVirtualRecord();
53                 }
54
55                 public void SetConstant(object defaultValue)
56                 {
57                         attribs |= FieldAttributes.HasDefault;
58                         typeBuilder.ModuleBuilder.AddConstant(pseudoToken, defaultValue);
59                 }
60
61                 public override object GetRawConstantValue()
62                 {
63                         return typeBuilder.Module.Constant.GetRawConstantValue(typeBuilder.Module, this.MetadataToken);
64                 }
65
66                 public void __SetDataAndRVA(byte[] data)
67                 {
68                         SetDataAndRvaImpl(data, typeBuilder.ModuleBuilder.initializedData, 0);
69                 }
70
71                 public void __SetReadOnlyDataAndRVA(byte[] data)
72                 {
73                         SetDataAndRvaImpl(data, typeBuilder.ModuleBuilder.methodBodies, unchecked((int)0x80000000));
74                 }
75
76                 private void SetDataAndRvaImpl(byte[] data, ByteBuffer bb, int readonlyMarker)
77                 {
78                         attribs |= FieldAttributes.HasFieldRVA;
79                         FieldRVATable.Record rec = new FieldRVATable.Record();
80                         bb.Align(8);
81                         rec.RVA = bb.Position + readonlyMarker;
82                         rec.Field = pseudoToken;
83                         typeBuilder.ModuleBuilder.FieldRVA.AddRecord(rec);
84                         bb.Write(data);
85                 }
86
87                 public override void __GetDataFromRVA(byte[] data, int offset, int length)
88                 {
89                         throw new NotImplementedException();
90                 }
91
92                 public override int __FieldRVA
93                 {
94                         get { throw new NotImplementedException(); }
95                 }
96
97                 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
98                 {
99                         SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute));
100                 }
101
102                 public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
103                 {
104                         Universe u = this.Module.universe;
105                         if (customBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_FieldOffsetAttribute)
106                         {
107                                 customBuilder = customBuilder.DecodeBlob(this.Module.Assembly);
108                                 SetOffset((int)customBuilder.GetConstructorArgument(0));
109                         }
110                         else if (customBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_MarshalAsAttribute)
111                         {
112                                 MarshalSpec.SetMarshalAsAttribute(typeBuilder.ModuleBuilder, pseudoToken, customBuilder);
113                                 attribs |= FieldAttributes.HasFieldMarshal;
114                         }
115                         else if (customBuilder.Constructor.DeclaringType == u.System_NonSerializedAttribute)
116                         {
117                                 attribs |= FieldAttributes.NotSerialized;
118                         }
119                         else if (customBuilder.Constructor.DeclaringType == u.System_Runtime_CompilerServices_SpecialNameAttribute)
120                         {
121                                 attribs |= FieldAttributes.SpecialName;
122                         }
123                         else
124                         {
125                                 typeBuilder.ModuleBuilder.SetCustomAttribute(pseudoToken, customBuilder);
126                         }
127                 }
128
129                 public void SetOffset(int iOffset)
130                 {
131                         FieldLayoutTable.Record rec = new FieldLayoutTable.Record();
132                         rec.Offset = iOffset;
133                         rec.Field = pseudoToken;
134                         typeBuilder.ModuleBuilder.FieldLayout.AddRecord(rec);
135                 }
136
137                 public override FieldAttributes Attributes
138                 {
139                         get { return attribs; }
140                 }
141
142                 public override Type DeclaringType
143                 {
144                         get { return typeBuilder.IsModulePseudoType ? null : typeBuilder; }
145                 }
146
147                 public override string Name
148                 {
149                         get { return name; }
150                 }
151
152                 public override int MetadataToken
153                 {
154                         get { return pseudoToken; }
155                 }
156
157                 public override Module Module
158                 {
159                         get { return typeBuilder.Module; }
160                 }
161
162                 public FieldToken GetToken()
163                 {
164                         return new FieldToken(pseudoToken);
165                 }
166
167                 internal void WriteFieldRecords(MetadataWriter mw)
168                 {
169                         mw.Write((short)attribs);
170                         mw.WriteStringIndex(nameIndex);
171                         mw.WriteBlobIndex(signature);
172                 }
173
174                 internal void FixupToken(int token)
175                 {
176                         typeBuilder.ModuleBuilder.RegisterTokenFixup(this.pseudoToken, token);
177                 }
178
179                 internal override FieldSignature FieldSignature
180                 {
181                         get { return fieldSig; }
182                 }
183
184                 internal override int ImportTo(ModuleBuilder other)
185                 {
186                         if (typeBuilder.IsGenericTypeDefinition)
187                         {
188                                 return other.ImportMember(TypeBuilder.GetField(typeBuilder, this));
189                         }
190                         else if (other == typeBuilder.ModuleBuilder)
191                         {
192                                 return pseudoToken;
193                         }
194                         else
195                         {
196                                 return other.ImportMethodOrField(typeBuilder, name, fieldSig);
197                         }
198                 }
199         }
200 }