* ExternMethodRef.cs: Accept and add calling conventions. Add
[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                 private bool is_ref;
34                 private bool is_array;
35
36                 private bool is_resolved;
37
38                 public TypeRef (string full_name, Location location)
39                 {
40                         this.full_name = full_name;
41                         this.location = location;
42                         is_pinned = false;
43                         is_ref = false;
44                         is_array = false;
45                         conversion_list = new ArrayList ();
46                         is_resolved = false;
47                 }
48
49                 public string FullName {
50                         get { return full_name; }
51                 }
52
53                 public bool IsPinned {
54                         get { return is_pinned; }
55                 }
56
57                 public bool IsArray {
58                         get { return is_array; }
59                 }
60
61                 public bool IsRef {
62                         get { return is_ref; }
63                 }
64
65                 public PEAPI.Type PeapiType {
66                         get { return resolved_type; }
67                 }
68
69                 public PEAPI.Class PeapiClass {
70                         get { return resolved_type as PEAPI.Class; }
71                 }
72
73                 public bool IsResolved {
74                         get { return is_resolved; }
75                 }
76
77                 public void MakeArray ()
78                 {
79                         conversion_list.Add (ConversionMethod.MakeArray);
80                         is_array = true;
81                 }
82
83                 public void MakeBoundArray (ArrayList bounds)
84                 {
85                         conversion_list.Add (ConversionMethod.MakeBoundArray);
86                         conversion_list.Add (bounds);
87                         is_array = true;
88                 }
89
90                 public void MakeManagedPointer ()
91                 {
92                         conversion_list.Add (ConversionMethod.MakeManagedPointer);
93                         is_ref = true;
94                 }
95
96                 public void MakeUnmanagedPointer ()
97                 {
98                         conversion_list.Add (ConversionMethod.MakeUnmanagedPointer);
99                 }
100
101                 public void MakeCustomModified (PEAPI.CustomModifier modifier)
102                 {
103                         conversion_list.Add (ConversionMethod.MakeCustomModified);
104                         conversion_list.Add (modifier);
105                 }
106
107                 public void MakePinned ()
108                 {
109                         is_pinned = true;
110                 }
111
112                 public  IMethodRef GetMethodRef (ITypeRef ret_type,
113                         PEAPI.CallConv call_conv, string name, ITypeRef[] param)
114                 {
115                         return new MethodRef (this, ret_type, name, param);
116                 }
117
118                 public IFieldRef GetFieldRef (ITypeRef ret_type, string name)
119                 {
120                         return new FieldRef (this, ret_type, name);
121                 }
122
123                 public void Resolve (CodeGen code_gen)
124                 {
125                         if (is_resolved)
126                                 return;
127
128                         PEAPI.Type base_type;
129                         PeapiTypeRef peapi_type;
130                         int count = conversion_list.Count;
131
132                         base_type = code_gen.TypeManager.GetPeapiType (full_name);
133
134                         /// TODO: Proper error message
135                         if (base_type == null) {
136                                 Console.WriteLine ("Type not defined: {0} {1}", full_name, location);
137                                 return;
138                         }
139
140                         peapi_type = new PeapiTypeRef (base_type, full_name);
141
142                         for (int i=0; i<count; i++) {
143                                 switch ((ConversionMethod) conversion_list[i]) {
144                                 case ConversionMethod.MakeArray:
145                                         peapi_type.MakeArray ();
146                                         break;
147                                 case ConversionMethod.MakeBoundArray:
148                                         peapi_type.MakeBoundArray ((ArrayList) conversion_list[++i]);
149                                         break;
150                                 case ConversionMethod.MakeManagedPointer:
151                                         peapi_type.MakeManagedPointer ();
152                                         break;
153                                 case ConversionMethod.MakeUnmanagedPointer:
154                                         peapi_type.MakeUnmanagedPointer ();
155                                         break;
156                                 case ConversionMethod.MakeCustomModified:
157                                         peapi_type.MakeCustomModified ((PEAPI.CustomModifier) conversion_list[++i]);
158                                         break;
159                                 }
160                         }
161
162                         resolved_type = peapi_type.PeapiType;
163
164                         is_resolved = true;
165                 }
166
167                 public IClassRef AsClassRef (CodeGen code_gen)
168                 {
169                         return this;
170                 }
171
172                 public override string ToString ()
173                 {
174                         return FullName;
175                 }
176
177         }
178
179 }
180