copying the latest Sys.Web.Services from trunk.
[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
24                 private bool offset_set;
25                 private bool datavalue_set;
26                 private bool value_set;
27
28                 private bool is_resolved;
29
30                 private uint offset;
31                 private PEAPI.Constant constant;
32                 private string at_data_id;
33
34                 public FieldDef (PEAPI.FieldAttr attr, string name,
35                                 ITypeRef type)
36                 {
37                         this.attr = attr;
38                         this.name = name;
39                         this.type = type;
40
41                         offset_set = false;
42                         datavalue_set = false;
43                         value_set = false;
44
45                         at_data_id = null;
46
47                         is_resolved = false;
48                 }
49
50                 public string Name {
51                         get { return name; }
52                 }
53
54                 public PEAPI.FieldDef PeapiFieldDef {
55                         get { return field_def; }
56                 }
57
58                 public bool IsStatic {
59                         get { return (attr & PEAPI.FieldAttr.Static) != 0; }
60                 }
61
62                 public PEAPI.FieldAttr Attributes {
63                         get { return attr; }
64                         set { attr = value; }
65                 }
66
67                 public void SetOffset (uint val)
68                 {
69                         offset_set = true;
70                         offset = val;
71                 }
72
73                 public void SetValue (PEAPI.Constant constant)
74                 {
75                         value_set = true;
76                         this.constant = constant;
77                 }
78
79                 public void AddDataValue (string at_data_id)
80                 {
81                         this.at_data_id = at_data_id;
82                 }
83
84                 public void AddCustomAttribute (CustomAttr customattr)
85                 {
86                         if (customattr_list == null)
87                                 customattr_list = new ArrayList ();
88
89                         customattr_list.Add (customattr);
90                 }
91
92                 public PEAPI.FieldDef Resolve (CodeGen code_gen)
93                 {
94                         if (is_resolved)
95                                 return field_def;
96
97                         type.Resolve (code_gen);
98                         field_def = code_gen.PEFile.AddField (attr, name, type.PeapiType);
99
100                         is_resolved = true;
101
102                         return field_def;
103                 }
104
105                 public PEAPI.FieldDef Resolve (CodeGen code_gen, PEAPI.ClassDef classdef)
106                 {
107                         if (is_resolved)
108                                 return field_def;
109
110                         type.Resolve (code_gen);
111                         field_def = classdef.AddField (attr, name, type.PeapiType);
112
113                         if (customattr_list != null)
114                                 foreach (CustomAttr customattr in customattr_list)
115                                         customattr.AddTo (code_gen, field_def);
116
117                         is_resolved = true;
118
119                         return field_def;
120                 }
121
122                 /// <summary>
123                 ///  Define a global field
124                 /// </summary>
125                 public void Define (CodeGen code_gen)
126                 {
127                         Resolve (code_gen);
128                         WriteCode (code_gen, field_def);
129                 }
130
131                 /// <summary>
132                 ///  Define a field member of the specified class
133                 /// </summary>
134                 public void Define (CodeGen code_gen, PEAPI.ClassDef class_def)
135                 {
136                         Resolve (code_gen, class_def);
137                         WriteCode (code_gen, field_def);
138                 }
139
140                 protected void WriteCode (CodeGen code_gen, PEAPI.FieldDef field_def)
141                 {
142                         if (offset_set)
143                                 field_def.SetOffset (offset);
144
145                         if (value_set) {
146                                 PEAPI.ByteArrConst dc = constant as PEAPI.ByteArrConst;
147                                 if (dc != null)
148                                         dc.Type = type.PeapiType;
149                                 field_def.AddValue (constant);
150                         }
151
152                         if (at_data_id != null) {
153                                 PEAPI.DataConstant dc = code_gen.GetDataConst (at_data_id);
154                                 field_def.AddDataValue (dc);
155                         }
156                 }
157         }
158
159 }
160