In ilasm/codegen:
[mono.git] / mcs / ilasm / codegen / GenericArguments.cs
1 //
2 // Mono.ILASM.GenericArguments
3 //
4 // Author(s):
5 //  Ankit Jain  <jankit@novell.com>
6 //
7 // Copyright 2005 Novell, Inc (http://www.novell.com)
8 //
9
10 //Note: This is shared by modified types of the same generic instance
11 //      Eg. foo`1<int32> and foo`1<int32> [] would share their GenericArguments
12
13 using System;
14 using System.Collections;
15 using System.Text;
16
17 namespace Mono.ILASM {
18
19         public class GenericArguments {
20                 ArrayList type_list;
21                 string type_str;
22                 ITypeRef [] type_arr;
23
24                 public GenericArguments ()
25                 {
26                         type_list = null;
27                         type_arr = null;
28                         type_str = null;
29                 }
30
31                 public int Count {
32                         get { return type_list.Count; }
33                 }
34
35                 public void Add (ITypeRef type)
36                 {
37                         if (type == null)
38                                 throw new ArgumentException ("type");
39
40                         if (type_list == null)
41                                 type_list = new ArrayList ();
42                         type_list.Add (type);
43                         type_str = null;
44                         type_arr = null;
45                 }
46                 
47                 public ITypeRef [] ToArray ()
48                 {
49                         if (type_list == null)
50                                 return null;
51                         if (type_arr == null)
52                                 type_arr = (ITypeRef []) type_list.ToArray (typeof (ITypeRef));
53
54                         return type_arr;
55                 }
56
57                 public PEAPI.Type [] Resolve (CodeGen code_gen)
58                 {
59                         int i = 0;
60                         PEAPI.Type [] p_type_list = new PEAPI.Type [type_list.Count];
61                         foreach (ITypeRef type in type_list) {
62                                 type.Resolve (code_gen);
63                                 p_type_list [i ++] = type.PeapiType;
64                         }
65
66                         return p_type_list;
67                 }
68
69                 private void MakeString ()
70                 {
71                         //Build full_name (foo < , >)
72                         StringBuilder sb = new StringBuilder ();
73                         sb.Append ("<");
74                         foreach (ITypeRef tr in type_list)
75                                 sb.AppendFormat ("{0}, ", tr.FullName);
76                         //Remove the extra ', ' at the end
77                         sb.Length -= 2;
78                         sb.Append (">");
79                         type_str = sb.ToString ();
80                 }
81
82                 public override string ToString ()
83                 {
84                         if (type_str == null)
85                                 MakeString ();
86                         return type_str;
87                 }
88         }
89
90 }
91