merge -r 53370:58178
[mono.git] / mcs / ilasm / codegen / FieldDef.cs
1 //
2 // Mono.ILASM.FieldDef
3 //
4 // Author(s):
5 //  Jackson Harper (Jackson@LatitudeGeo.com)
6 //
7 // (C) 2003 Jackson Harper, All rights reserved
8 //
9
10
11 using System;
12 using System.Collections;
13
14 namespace Mono.ILASM {
15
16         public class FieldDef : ICustomAttrTarget {
17
18                 private string name;
19                 private ITypeRef type;
20                 private PEAPI.FieldAttr attr;
21                 private PEAPI.FieldDef field_def;
22                 private ArrayList customattr_list;
23                 private PEAPI.NativeType native_type;
24
25                 private bool offset_set;
26                 private bool datavalue_set;
27                 private bool value_set;
28
29                 private bool is_resolved;
30
31                 private uint offset;
32                 private PEAPI.Constant constant;
33                 private string at_data_id;
34
35                 public FieldDef (PEAPI.FieldAttr attr, string name,
36                                 ITypeRef type)
37                 {
38                         this.attr = attr;
39                         this.name = name;
40                         this.type = type;
41
42                         offset_set = false;
43                         datavalue_set = false;
44                         value_set = false;
45
46                         at_data_id = null;
47
48                         is_resolved = false;
49                 }
50
51                 public string Name {
52                         get { return name; }
53                 }
54
55                 public PEAPI.FieldDef PeapiFieldDef {
56                         get { return field_def; }
57                 }
58
59                 public bool IsStatic {
60                         get { return (attr & PEAPI.FieldAttr.Static) != 0; }
61                 }
62
63                 public PEAPI.FieldAttr Attributes {
64                         get { return attr; }
65                         set { attr = value; }
66                 }
67
68                 public void SetOffset (uint val)
69                 {
70                         offset_set = true;
71                         offset = val;
72                 }
73
74                 public void SetValue (PEAPI.Constant constant)
75                 {
76                         value_set = true;
77                         this.constant = constant;
78                 }
79
80                 public void AddDataValue (string at_data_id)
81                 {
82                         this.at_data_id = at_data_id;
83                 }
84
85                 public void AddCustomAttribute (CustomAttr customattr)
86                 {
87                         if (customattr_list == null)
88                                 customattr_list = new ArrayList ();
89
90                         customattr_list.Add (customattr);
91                 }
92                 
93                 public void AddMarshalInfo (PEAPI.NativeType native_type)
94                 {
95                         this.native_type = native_type;        
96                 }
97
98                 public PEAPI.FieldDef Resolve (CodeGen code_gen)
99                 {
100                         if (is_resolved)
101                                 return field_def;
102
103                         type.Resolve (code_gen);
104                         field_def = code_gen.PEFile.AddField (attr, name, type.PeapiType);
105
106                         is_resolved = true;
107
108                         return field_def;
109                 }
110
111                 public PEAPI.FieldDef Resolve (CodeGen code_gen, PEAPI.ClassDef classdef)
112                 {
113                         if (is_resolved)
114                                 return field_def;
115
116                         type.Resolve (code_gen);
117                         field_def = classdef.AddField (attr, name, type.PeapiType);
118
119                         if (customattr_list != null)
120                                 foreach (CustomAttr customattr in customattr_list)
121                                         customattr.AddTo (code_gen, field_def);
122
123                         if (native_type != null)
124                                 field_def.SetMarshalInfo (native_type);
125
126                         is_resolved = true;
127
128                         return field_def;
129                 }
130
131                 /// <summary>
132                 ///  Define a global field
133                 /// </summary>
134                 public void Define (CodeGen code_gen)
135                 {
136                         Resolve (code_gen);
137                         WriteCode (code_gen, field_def);
138                 }
139
140                 /// <summary>
141                 ///  Define a field member of the specified class
142                 /// </summary>
143                 public void Define (CodeGen code_gen, PEAPI.ClassDef class_def)
144                 {
145                         Resolve (code_gen, class_def);
146                         WriteCode (code_gen, field_def);
147                 }
148
149                 protected void WriteCode (CodeGen code_gen, PEAPI.FieldDef field_def)
150                 {
151                         if (offset_set)
152                                 field_def.SetOffset (offset);
153
154                         if (value_set) {
155                                 PEAPI.ByteArrConst dc = constant as PEAPI.ByteArrConst;
156                                 if (dc != null)
157                                         dc.Type = type.PeapiType;
158                                 field_def.AddValue (constant);
159                         }
160
161                         if (at_data_id != null) {
162                                 PEAPI.DataConstant dc = code_gen.GetDataConst (at_data_id);
163                                 field_def.AddDataValue (dc);
164                         }
165                 }
166         }
167
168 }
169