do not check order sequence if option /order was not used
[mono.git] / mcs / class / IKVM.Reflection / Reader / Field.cs
1 /*
2   Copyright (C) 2009 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.Collections.Generic;
26 using System.Text;
27 using System.IO;
28 using IKVM.Reflection.Metadata;
29
30 namespace IKVM.Reflection.Reader
31 {
32         sealed class FieldDefImpl : FieldInfo
33         {
34                 private readonly ModuleReader module;
35                 private readonly TypeDefImpl declaringType;
36                 private readonly int index;
37                 private FieldSignature lazyFieldSig;
38
39                 internal FieldDefImpl(ModuleReader module, TypeDefImpl declaringType, int index)
40                 {
41                         this.module = module;
42                         this.declaringType = declaringType;
43                         this.index = index;
44                 }
45
46                 public override FieldAttributes Attributes
47                 {
48                         get { return (FieldAttributes)module.Field.records[index].Flags; }
49                 }
50
51                 public override Type DeclaringType
52                 {
53                         get { return declaringType.IsModulePseudoType ? null : declaringType; }
54                 }
55
56                 public override string Name
57                 {
58                         get { return module.GetString(module.Field.records[index].Name); }
59                 }
60
61                 public override string ToString()
62                 {
63                         return this.FieldType.Name + " " + this.Name;
64                 }
65
66                 public override Module Module
67                 {
68                         get { return module; }
69                 }
70
71                 public override int MetadataToken
72                 {
73                         get { return (FieldTable.Index << 24) + index + 1; }
74                 }
75
76                 public override object GetRawConstantValue()
77                 {
78                         return module.Constant.GetRawConstantValue(module, this.MetadataToken);
79                 }
80
81                 public override void __GetDataFromRVA(byte[] data, int offset, int length)
82                 {
83                         int rva = this.__FieldRVA;
84                         if (rva == 0)
85                         {
86                                 // C++ assemblies can have fields that have an RVA that is zero
87                                 Array.Clear(data, offset, length);
88                                 return;
89                         }
90                         module.__ReadDataFromRVA(rva, data, offset, length);
91                 }
92
93                 public override int __FieldRVA
94                 {
95                         get
96                         {
97                                 foreach (int i in module.FieldRVA.Filter(index + 1))
98                                 {
99                                         return module.FieldRVA.records[i].RVA;
100                                 }
101                                 throw new InvalidOperationException();
102                         }
103                 }
104
105                 public override bool __TryGetFieldOffset(out int offset)
106                 {
107                         foreach (int i in this.Module.FieldLayout.Filter(index + 1))
108                         {
109                                 offset = this.Module.FieldLayout.records[i].Offset;
110                                 return true;
111                         }
112                         offset = 0;
113                         return false;
114                 }
115
116                 internal override FieldSignature FieldSignature
117                 {
118                         get { return lazyFieldSig ?? (lazyFieldSig = FieldSignature.ReadSig(module, module.GetBlob(module.Field.records[index].Signature), declaringType)); }
119                 }
120
121                 internal override int ImportTo(Emit.ModuleBuilder module)
122                 {
123                         return module.ImportMethodOrField(declaringType, this.Name, this.FieldSignature);
124                 }
125
126                 internal override int GetCurrentToken()
127                 {
128                         return this.MetadataToken;
129                 }
130
131                 internal override bool IsBaked
132                 {
133                         get { return true; }
134                 }
135         }
136 }