New test.
[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 BaseTypeRef 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                                 BaseTypeRef 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 BaseTypeRef Type {
69                         get { return type; }
70                 }
71
72                 public void SetOffset (uint val)
73                 {
74                         offset_set = true;
75                         offset = val;
76                 }
77
78                 public void SetValue (PEAPI.Constant constant)
79                 {
80                         value_set = true;
81                         this.constant = constant;
82                 }
83
84                 public void AddDataValue (string at_data_id)
85                 {
86                         this.at_data_id = at_data_id;
87                 }
88
89                 public void AddCustomAttribute (CustomAttr customattr)
90                 {
91                         if (customattr_list == null)
92                                 customattr_list = new ArrayList ();
93
94                         customattr_list.Add (customattr);
95                 }
96                 
97                 public void AddMarshalInfo (PEAPI.NativeType native_type)
98                 {
99                         this.native_type = native_type;        
100                 }
101
102                 public PEAPI.FieldDef Resolve (CodeGen code_gen)
103                 {
104                         if (is_resolved)
105                                 return field_def;
106
107                         type.Resolve (code_gen);
108                         field_def = code_gen.PEFile.AddField (attr, name, type.PeapiType);
109
110                         is_resolved = true;
111
112                         return field_def;
113                 }
114
115                 public PEAPI.FieldDef Resolve (CodeGen code_gen, PEAPI.ClassDef classdef)
116                 {
117                         if (is_resolved)
118                                 return field_def;
119
120                         type.Resolve (code_gen);
121                         field_def = classdef.AddField (attr, name, type.PeapiType);
122
123                         if (customattr_list != null)
124                                 foreach (CustomAttr customattr in customattr_list)
125                                         customattr.AddTo (code_gen, field_def);
126
127                         if (native_type != null)
128                                 field_def.SetMarshalInfo (native_type);
129
130                         is_resolved = true;
131
132                         return field_def;
133                 }
134
135                 /// <summary>
136                 ///  Define a global field
137                 /// </summary>
138                 public void Define (CodeGen code_gen)
139                 {
140                         Resolve (code_gen);
141                         WriteCode (code_gen, field_def);
142                 }
143
144                 /// <summary>
145                 ///  Define a field member of the specified class
146                 /// </summary>
147                 public void Define (CodeGen code_gen, PEAPI.ClassDef class_def)
148                 {
149                         Resolve (code_gen, class_def);
150                         WriteCode (code_gen, field_def);
151                 }
152
153                 protected void WriteCode (CodeGen code_gen, PEAPI.FieldDef field_def)
154                 {
155                         if (offset_set)
156                                 field_def.SetOffset (offset);
157
158                         if (value_set) {
159                                 PEAPI.ByteArrConst dc = constant as PEAPI.ByteArrConst;
160                                 if (dc != null)
161                                         dc.Type = type.PeapiType;
162                                 field_def.AddValue (constant);
163                         }
164
165                         if (at_data_id != null) {
166                                 PEAPI.DataConstant dc = code_gen.GetDataConst (at_data_id);
167                                 field_def.AddDataValue (dc);
168                         }
169                 }
170         }
171
172 }
173