In ilasm/codegen:
[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 : ModifiableType, IClassRef {
19
20
21                 private Location location;
22                 private string full_name;
23                 private string sig_mod;
24                 private PEAPI.Type type;
25                 private bool is_valuetype;
26                 private Hashtable genericinst_table;
27                 private Hashtable p_genericinst_table;
28
29                 private bool is_resolved;
30
31                 public static readonly TypeRef Ellipsis = new TypeRef ("ELLIPSIS", false, null);
32                 public static readonly TypeRef Any = new TypeRef ("any", false, null);
33
34                 public TypeRef (string full_name, bool is_valuetype, Location location)
35                         : this (full_name, is_valuetype, location, null, null)
36                 {
37                 }
38
39                 public TypeRef (string full_name, bool is_valuetype, Location location, ArrayList conv_list, string sig_mod)
40                 {
41                         this.full_name = full_name;
42                         this.location = location;
43                         this.is_valuetype = is_valuetype;
44                         this.sig_mod = sig_mod;
45                         if (conv_list != null)
46                                 ConversionList = conv_list;
47                         is_resolved = false;
48                 }
49                 
50                 public string FullName {
51                         get { return full_name + sig_mod; }
52                 }
53
54                 public override string SigMod {
55                         get { return sig_mod; }
56                         set { sig_mod = value; }
57                 }
58
59                 public PEAPI.Type PeapiType {
60                         get { return type; }
61                 }
62
63                 public PEAPI.Class PeapiClass {
64                         get { return type as PEAPI.Class; }
65                 }
66
67                 public IClassRef Clone ()
68                 {
69                         return new TypeRef (full_name, is_valuetype, location, (ArrayList) ConversionList.Clone (), sig_mod);
70                 }
71
72                 public bool IsResolved {
73                         get { return is_resolved; }
74                 }
75
76                 public void MakeValueClass ()
77                 {
78                         is_valuetype = true;
79                 }
80
81                 public GenericTypeInst GetGenericTypeInst (GenericArguments gen_args)
82                 {
83                         string sig = gen_args.ToString ();
84                         GenericTypeInst gtri = null;
85
86                         if (genericinst_table == null)
87                                 genericinst_table = new Hashtable ();
88                         else
89                                 gtri = genericinst_table [sig] as GenericTypeInst;
90
91                         if (gtri == null) {
92                                 gtri = new GenericTypeInst (this, gen_args, is_valuetype);
93                                 genericinst_table [sig] = gtri;
94                         }
95
96                         return gtri;
97                 }
98
99                 public PEAPI.Type ResolveInstance (CodeGen code_gen, GenericArguments gen_args)
100                 {
101                         PEAPI.GenericTypeInst gtri = null;
102                         string sig = gen_args.ToString ();
103
104                         if (p_genericinst_table == null)
105                                 p_genericinst_table = new Hashtable ();
106                         else
107                                 gtri = p_genericinst_table [sig] as PEAPI.GenericTypeInst;
108
109                         if (gtri == null) {
110                                 if (!IsResolved)
111                                         Resolve (code_gen);
112
113                                 gtri = new PEAPI.GenericTypeInst (PeapiType, gen_args.Resolve (code_gen));
114                                 p_genericinst_table [sig] = gtri;
115                         }
116                         
117                         return gtri;
118                 }
119
120                 public  IMethodRef GetMethodRef (ITypeRef ret_type,
121                         PEAPI.CallConv call_conv, string name, ITypeRef[] param, int gen_param_count)
122                 {
123                         return new MethodRef (this, call_conv, ret_type, name, param, gen_param_count);
124                 }
125
126                 public IFieldRef GetFieldRef (ITypeRef ret_type, string name)
127                 {
128                         return new FieldRef (this, ret_type, name);
129                 }
130
131                 public void Resolve (CodeGen code_gen)
132                 {
133                         if (is_resolved)
134                                 return;
135
136                         PEAPI.Type base_type;
137
138                         base_type = code_gen.TypeManager.GetPeapiType (full_name);
139
140                         if (base_type == null) {
141                                 code_gen.Report.Error ("Reference to undefined class '" +
142                                                        FullName + "'");
143                                 return;
144                         }
145                         type = Modify (code_gen, base_type);
146
147                         is_resolved = true;
148                 }
149
150                 public IClassRef AsClassRef (CodeGen code_gen)
151                 {
152                         return this;
153                 }
154
155                 public override string ToString ()
156                 {
157                         return FullName;
158                 }
159
160         }
161
162 }
163