Merge pull request #5313 from lambdageek/corefx-sre-fixup
[mono.git] / mcs / class / corlib / System.Reflection.Emit / FieldBuilder.cs
1
2 //
3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24
25 //
26 // System.Reflection.Emit/FieldBuilder.cs
27 //
28 // Author:
29 //   Paolo Molaro (lupus@ximian.com)
30 //
31 // (C) 2001-2002 Ximian, Inc.  http://www.ximian.com
32 //
33
34 #if !FULL_AOT_RUNTIME
35 using System;
36 using System.Reflection;
37 using System.Reflection.Emit;
38 using System.Globalization;
39 using System.Runtime.CompilerServices;
40 using System.Runtime.InteropServices;
41
42 namespace System.Reflection.Emit {
43         [ComVisible (true)]
44         [ComDefaultInterface (typeof (_FieldBuilder))]
45         [ClassInterface (ClassInterfaceType.None)]
46         [StructLayout (LayoutKind.Sequential)]
47         public sealed class FieldBuilder : FieldInfo, _FieldBuilder {
48         
49 #pragma warning disable 169, 414
50                 private FieldAttributes attrs;
51                 private Type type;
52                 private String name;
53                 private object def_value;
54                 private int offset;
55                 private int table_idx;
56                 internal TypeBuilder typeb;
57                 private byte[] rva_data;
58                 private CustomAttributeBuilder[] cattrs;
59                 private UnmanagedMarshal marshal_info;
60                 private RuntimeFieldHandle handle;
61                 private Type[] modReq;
62                 private Type[] modOpt;
63 #pragma warning restore 169, 414
64
65                 internal FieldBuilder (TypeBuilder tb, string fieldName, Type type, FieldAttributes attributes, Type[] modReq, Type[] modOpt)
66                 {
67                         if (type == null)
68                                 throw new ArgumentNullException ("type");
69
70                         attrs = attributes;
71                         name = fieldName;
72                         this.type = type;
73                         this.modReq = modReq;
74                         this.modOpt = modOpt;
75                         offset = -1;
76                         typeb = tb;
77                         table_idx = tb.get_next_table_index (this, 0x04, true);
78
79                         ((ModuleBuilder) tb.Module).RegisterToken (this, GetToken ().Token);
80                 }
81
82                 public override FieldAttributes Attributes {
83                         get { return attrs; }
84                 }
85
86                 public override Type DeclaringType {
87                         get { return typeb; }
88                 }
89
90                 public override RuntimeFieldHandle FieldHandle {
91                         get {
92                                 throw CreateNotSupportedException ();
93                         }
94                 }
95
96                 public override Type FieldType {
97                         get { return type; }
98                 }
99
100                 public override string Name {
101                         get { return name; }
102                 }
103
104                 public override Type ReflectedType {
105                         get { return typeb; }
106                 }
107
108                 public override object[] GetCustomAttributes(bool inherit) {
109                         /*
110                          * On MS.NET, this always returns not_supported, but we can't do this
111                          * since there would be no way to obtain custom attributes of 
112                          * dynamically created ctors.
113                          */
114                         if (typeb.is_created)
115                                 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
116                         else
117                                 throw CreateNotSupportedException ();
118                 }
119
120                 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
121                         if (typeb.is_created)
122                                 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
123                         else
124                                 throw CreateNotSupportedException ();
125                 }
126
127                 public FieldToken GetToken() {
128                         return new FieldToken (MetadataToken);
129                 }
130
131                 public override object GetValue(object obj) {
132                         throw CreateNotSupportedException ();
133                 }
134
135                 public override bool IsDefined( Type attributeType, bool inherit) {
136                         throw CreateNotSupportedException ();
137                 }
138
139                 internal override int GetFieldOffset () {
140                         /* FIXME: */
141                         return 0;
142                 }
143
144                 internal void SetRVAData (byte[] data) {
145                         rva_data = (byte[])data.Clone ();
146                 }
147
148                 public void SetConstant( object defaultValue) {
149                         RejectIfCreated ();
150
151                         /*if (defaultValue.GetType() != type)
152                                 throw new ArgumentException ("Constant doesn't match field type");*/
153                         def_value = defaultValue;
154                 }
155
156                 public void SetCustomAttribute (CustomAttributeBuilder customBuilder) {
157                         RejectIfCreated ();
158
159                         if (customBuilder == null)
160                                 throw new ArgumentNullException ("customBuilder");
161
162                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
163                         if (attrname == "System.Runtime.InteropServices.FieldOffsetAttribute") {
164                                 byte[] data = customBuilder.Data;
165                                 offset = (int)data [2];
166                                 offset |= ((int)data [3]) << 8;
167                                 offset |= ((int)data [4]) << 16;
168                                 offset |= ((int)data [5]) << 24;
169                                 return;
170                         } else if (attrname == "System.NonSerializedAttribute") {
171                                 attrs |= FieldAttributes.NotSerialized;
172                                 return;
173                         } else if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
174                                 attrs |= FieldAttributes.SpecialName;
175                                 return;
176                         } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
177                                 attrs |= FieldAttributes.HasFieldMarshal;
178                                 marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, true);
179                                 /* FIXME: check for errors */
180                                 return;
181                         }
182                         if (cattrs != null) {
183                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
184                                 cattrs.CopyTo (new_array, 0);
185                                 new_array [cattrs.Length] = customBuilder;
186                                 cattrs = new_array;
187                         } else {
188                                 cattrs = new CustomAttributeBuilder [1];
189                                 cattrs [0] = customBuilder;
190                         }
191                 }
192
193                 [ComVisible (true)]
194                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
195                         RejectIfCreated ();
196                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
197                 }
198
199                 [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
200                 public void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
201                         RejectIfCreated ();
202                         marshal_info = unmanagedMarshal;
203                         attrs |= FieldAttributes.HasFieldMarshal;
204                 }
205
206                 public void SetOffset( int iOffset) {
207                         RejectIfCreated ();
208                         if (iOffset < 0)
209                                 throw new ArgumentException ("Negative field offset is not allowed");
210                         offset = iOffset;
211                 }
212
213                 public override void SetValue( object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture) {
214                         throw CreateNotSupportedException ();
215                 }
216
217                 private Exception CreateNotSupportedException ()
218                 {
219                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
220                 }
221
222                 private void RejectIfCreated ()
223                 {
224                         if (typeb.is_created)
225                                 throw new InvalidOperationException ("Unable to change after type has been created.");
226                 }
227
228                 internal void ResolveUserTypes () {
229                         type = TypeBuilder.ResolveUserType (type);
230                         TypeBuilder.ResolveUserTypes (modReq);
231                         TypeBuilder.ResolveUserTypes (modOpt);
232                         if (marshal_info != null)
233                                 marshal_info.marshaltyperef = TypeBuilder.ResolveUserType (marshal_info.marshaltyperef);
234                 }
235
236                 internal FieldInfo RuntimeResolve () {
237                         // typeb.CreateType() populates this.handle
238                         var type_handle = new RuntimeTypeHandle (typeb.CreateType () as RuntimeType);
239                         return FieldInfo.GetFieldFromHandle (handle, type_handle);
240                 }
241
242                 public override Module Module {
243                         get {
244                                 return base.Module;
245                         }
246                 }
247
248                 void _FieldBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
249                 {
250                         throw new NotImplementedException ();
251                 }
252
253                 void _FieldBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
254                 {
255                         throw new NotImplementedException ();
256                 }
257
258                 void _FieldBuilder.GetTypeInfoCount (out uint pcTInfo)
259                 {
260                         throw new NotImplementedException ();
261                 }
262
263                 void _FieldBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
264                 {
265                         throw new NotImplementedException ();
266                 }
267         }
268 }
269
270 #endif