In ilasm/tests:
[mono.git] / mcs / ilasm / codegen / GenericParamRef.cs
1 //
2 // Mono.ILASM.GenericParamRef
3 //
4 // Author(s):
5 //  Jackson Harper (Jackson@LatitudeGeo.com)
6 //  Ankit Jain  (JAnkit@novell.com)
7 //
8 // (C) 2003 Jackson Harper, All rights reserved
9 // (C) 2006 Novell, Inc (http://www.novell.com)
10 //
11
12
13 using System;
14 using System.Collections;
15
16 namespace Mono.ILASM {
17
18         public interface IGenericTypeRef : IClassRef {
19                 /* Used to resolve any gen params in arguments, constraints etc */
20                 void Resolve (GenericParameters type_gen_params, GenericParameters method_gen_params);
21                 
22                 /* Only resolves, does not add it to the TypeSpec
23                    table */
24                 void ResolveNoTypeSpec (CodeGen code_gen);
25         }
26
27         public class GenericParamRef : ModifiableType, IGenericTypeRef {
28
29                 /* Might get modified */
30                 private PEAPI.Type type;
31                 /* Unmodified GenParam */
32                 private PEAPI.GenParam param;
33                 private string full_name;
34                 private string sig_mod;
35                 private bool is_resolved;
36                 private bool is_added; /* Added to TypeSpec table ? */
37
38                 public GenericParamRef (PEAPI.GenParam gen_param, string full_name)
39                         : this (gen_param, full_name, null)
40                 {
41                 }
42
43                 public GenericParamRef (PEAPI.GenParam gen_param, string full_name, ArrayList conv_list)
44                 {
45                         this.type = gen_param;
46                         this.param = gen_param;
47                         this.full_name = full_name;
48                         sig_mod = String.Empty;
49                         is_resolved = false;
50                         is_added = false;
51                         if (conv_list != null)
52                                 ConversionList = conv_list;
53                 }
54
55                 public string FullName {
56                         get { 
57                                 return (param.Type == PEAPI.GenParamType.Var ? "!" : "!!") 
58                                         + param.Index + sig_mod;
59                         }
60                 }
61
62                 public override string SigMod {
63                         get { return sig_mod; }
64                         set { sig_mod = value; }
65                 }
66
67                 public PEAPI.Type PeapiType {
68                         get { return type; }
69                 }
70
71                 public PEAPI.Class PeapiClass {
72                         get { return (PEAPI.Class) type; }
73                 }
74
75                 public void MakeValueClass ()
76                 {
77                         throw new Exception ("Not supported");
78                 }
79
80                 public IClassRef Clone ()
81                 {
82                         return new GenericParamRef (param, full_name, (ArrayList) ConversionList.Clone ());
83                 }
84
85                 public GenericTypeInst GetGenericTypeInst (GenericArguments gen_args)
86                 {
87                         throw new Exception ("Not supported");
88                 }
89                 
90                 public PEAPI.Type ResolveInstance (CodeGen code_gen, GenericArguments gen_args)
91                 {
92                         throw new Exception ("Not supported");
93                 }
94
95                 public void ResolveNoTypeSpec (CodeGen code_gen)
96                 {
97                         if (is_resolved)
98                                 return;
99                         
100                         type = Modify (code_gen, type);
101                         is_resolved = true;
102                 }
103
104                 public void Resolve (CodeGen code_gen)
105                 {
106                         ResolveNoTypeSpec (code_gen);
107                         if (is_added)
108                                 return;
109
110                         code_gen.PEFile.AddGenericParam (param);
111                         is_added = true;
112                 }
113                 
114                 public void Resolve (GenericParameters type_gen_params, GenericParameters method_gen_params)
115                 {
116                         if (param.Name == "") {
117                                 /* Name wasn't specified */
118                                 return;
119                         }
120
121                         if (param.Type == PEAPI.GenParamType.MVar && method_gen_params != null)
122                                 param.Index = method_gen_params.GetGenericParamNum (param.Name); 
123                         else if (type_gen_params != null)
124                                 param.Index = type_gen_params.GetGenericParamNum (param.Name);
125
126                         if (param.Index < 0)
127                                 /* TODO: Report error */
128                                 throw new Exception (String.Format ("Invalid {0}type parameter '{1}'", 
129                                                         (param.Type == PEAPI.GenParamType.MVar ? "method " : ""),
130                                                          param.Name));
131                 }
132
133                 public IMethodRef GetMethodRef (ITypeRef ret_type, PEAPI.CallConv call_conv,
134                                 string name, ITypeRef[] param, int gen_param_count)
135                 {
136                         return new TypeSpecMethodRef (this, ret_type, call_conv, name, param, gen_param_count);
137                 }
138
139                 public IFieldRef GetFieldRef (ITypeRef ret_type, string name)
140                 {
141                         return new TypeSpecFieldRef (this, ret_type, name);
142                 }
143         }
144
145 }
146