2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / ilasm / codegen / FieldTable.cs
blob0a5aa46d52e96034b6881cf5ac9fca16bf5a7e96
1 //
2 // Mono.ILASM.FieldTable.cs
3 //
4 // Author(s):
5 // Jackson Harper (Jackson@LatitudeGeo.com)
6 //
7 // (C) 2003 Jackson Harper, All rights reserved
8 //
10 using PEAPI;
11 using System;
12 using System.Text;
13 using System.Collections;
15 namespace Mono.ILASM {
17 public class FieldTable {
19 private class FieldTableItem {
21 private static readonly int DefinedFlag = 2;
23 private int flags;
25 public ArrayList LocationList;
26 public FieldDef Field;
28 public FieldTableItem (FieldDef field, Location location)
30 flags = 0;
31 Field = field;
32 LocationList = new ArrayList ();
33 LocationList.Add (location);
36 public bool Defined {
37 get { return ((flags & DefinedFlag) != 0); }
38 set {
39 if (value)
40 flags |= DefinedFlag;
41 else
42 flags ^= DefinedFlag;
47 protected Hashtable table;
48 protected ClassDef parent_class;
50 public FieldTable (ClassDef parent_class)
52 this.parent_class = parent_class;
53 table = new Hashtable ();
56 public Field GetReference (TypeRef type, string name, Location location)
58 FieldTableItem item = table[name] as FieldTableItem;
60 if (item != null) {
61 item.LocationList.Add (location);
62 return item.Field;
65 FieldDef field = parent_class.AddField (name, type.Type);
66 AddReferenced (name, field, location);
68 return field;
71 public FieldDef AddDefinition (FieldAttr field_attr, string name,
72 TypeRef type, Location location)
74 FieldTableItem item = (FieldTableItem) table[name];
76 if (item == null) {
77 FieldDef field = parent_class.AddField (field_attr, name, type.Type);
78 AddDefined (name, field, location);
79 return field;
82 item.Field.AddFieldAttr (field_attr);
83 item.Defined = true;
85 return item.Field;
88 public bool CheckDefined ()
90 foreach (DictionaryEntry dic_entry in table) {
91 FieldTableItem table_item = (FieldTableItem) dic_entry.Value;
92 if (table_item.Defined)
93 continue;
94 Report.Error (String.Format ("Field: {0} is not defined.", dic_entry.Key));
96 return true;
99 protected void AddDefined (string signature, FieldDef field, Location location)
101 if (table.Contains (signature))
102 return;
104 FieldTableItem item = new FieldTableItem (field, location);
105 item.Defined = true;
107 table[signature] = item;
110 protected void AddReferenced (string signature, FieldDef field, Location location)
112 FieldTableItem item = new FieldTableItem (field, location);
114 table[signature] = item;
117 /// <summary>
118 /// If a field is allready defined throw an Error
119 /// </summary>
120 protected void CheckExists (string signature)
122 FieldTableItem item = table[signature] as FieldTableItem;
124 if ((item != null) && (item.Defined))
125 Report.Error ("Field: " + signature + " defined in multiple locations.");