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