* TypeRef.cs: Rewrite - Typerefs are now resolved after parsing.
[mono.git] / mcs / ilasm / codegen / TypeRef.cs
1 //
2 // Mono.ILASM.TypeRef
3 //
4 // Author(s):
5 //  Jackson Harper (Jackson@LatitudeGeo.com)
6 //
7 // (C) 2003 Jackson Harper, All rights reserved
8 //
9
10 using System;
11 using System.Collections;
12
13 namespace Mono.ILASM {
14
15         /// <summary>
16         /// Reference to a type in the module being compiled.
17         /// </summary>
18         public class TypeRef : IClassRef {
19
20                 private enum ConversionMethod {
21                         MakeArray,
22                         MakeBoundArray,
23                         MakeManagedPointer,
24                         MakeUnmanagedPointer,
25                         MakeCustomModified
26                 }
27
28                 private Location location;
29                 private string full_name;
30                 private PEAPI.Type resolved_type;
31                 private ArrayList conversion_list;
32                 private bool is_pinned;
33
34                 private bool is_resolved;
35
36                 public TypeRef (string full_name, Location location)
37                 {
38                         this.full_name = full_name;
39                         this.location = location;
40                         is_pinned = false;
41                         conversion_list = new ArrayList ();
42                         is_resolved = false;
43                 }
44
45                 public string FullName {
46                         get { return full_name; }
47                 }
48
49                 public bool IsPinned {
50                         get { return is_pinned; }
51                 }
52
53                 public PEAPI.Type PeapiType {
54                         get { return resolved_type; }
55                 }
56
57                 public PEAPI.Class PeapiClass {
58                         get { return resolved_type as PEAPI.Class; }
59                 }
60
61                 public bool IsResolved {
62                         get { return is_resolved; }
63                 }
64
65                 public void MakeArray ()
66                 {
67                         conversion_list.Add (ConversionMethod.MakeArray);
68                 }
69
70                 public void MakeBoundArray (ArrayList bounds)
71                 {
72                         conversion_list.Add (ConversionMethod.MakeBoundArray);
73                         conversion_list.Add (bounds);
74                 }
75
76                 public void MakeManagedPointer ()
77                 {
78                         conversion_list.Add (ConversionMethod.MakeManagedPointer);
79                 }
80
81                 public void MakeUnmanagedPointer ()
82                 {
83                         conversion_list.Add (ConversionMethod.MakeUnmanagedPointer);
84                 }
85
86                 public void MakeCustomModified (PEAPI.CustomModifier modifier)
87                 {
88                         conversion_list.Add (ConversionMethod.MakeCustomModified);
89                         conversion_list.Add (modifier);
90                 }
91
92                 public void MakePinned ()
93                 {
94                         is_pinned = true;
95                 }
96
97                 public void Resolve (CodeGen code_gen)
98                 {
99                         if (is_resolved)
100                                 return;
101
102                         PEAPI.Type base_type;
103                         PeapiTypeRef peapi_type;
104                         int count = conversion_list.Count;
105
106                         base_type = code_gen.TypeManager.GetPeapiType (full_name);
107
108                         /// TODO: Proper error message
109                         if (base_type == null) {
110                                 Console.WriteLine ("Type not defined: {0} {1}", full_name, location);
111                                 return;
112                         }
113
114                         peapi_type = new PeapiTypeRef (base_type, full_name);
115
116                         for (int i=0; i<count; i++) {
117                                 switch ((ConversionMethod) conversion_list[i]) {
118                                 case ConversionMethod.MakeArray:
119                                         peapi_type.MakeArray ();
120                                         break;
121                                 case ConversionMethod.MakeBoundArray:
122                                         peapi_type.MakeBoundArray ((ArrayList) conversion_list[++i]);
123                                         break;
124                                 case ConversionMethod.MakeManagedPointer:
125                                         peapi_type.MakeManagedPointer ();
126                                         break;
127                                 case ConversionMethod.MakeUnmanagedPointer:
128                                         peapi_type.MakeUnmanagedPointer ();
129                                         break;
130                                 case ConversionMethod.MakeCustomModified:
131                                         peapi_type.MakeCustomModified ((PEAPI.CustomModifier) conversion_list[++i]);
132                                         break;
133                                 }
134                         }
135
136                         resolved_type = peapi_type.PeapiType;
137
138                         is_resolved = true;
139                 }
140
141                 public override string ToString ()
142                 {
143                         return FullName;
144                 }
145
146         }
147
148 }
149