In ilasm/codegen:
[mono.git] / mcs / ilasm / codegen / GenericParameters.cs
1 //
2 // Mono.ILASM.GenericParameters
3 //
4 // Author(s):
5 //  Ankit Jain  <jankit@novell.com>
6 //
7 // Copyright 2006 Novell, Inc (http://www.novell.com)
8 //
9
10 using System;
11 using System.Collections;
12 using System.Text;
13
14 namespace Mono.ILASM {
15
16         public class GenericParameter {
17                 string id;
18                 int num;
19                 PEAPI.GenericParamAttributes attr;
20                 ArrayList constraintsList;
21                 
22                 public GenericParameter (string id) 
23                         : this (id, 0, null)
24                 {
25                 }
26
27                 public GenericParameter (string id, PEAPI.GenericParamAttributes attr, ArrayList constraints)
28                 {
29                         this.id = id;
30                         this.attr = attr;
31                         num = -1;
32                         constraintsList = null;
33                                 
34                         if (constraints != null)
35                                 foreach (ITypeRef typeref in constraints)
36                                         AddConstraint (typeref);
37                 }
38
39                 public string Id {
40                         get { return id; }
41                 }
42
43                 public int Num {
44                         get { return num; }
45                         set { num = value; }
46                 }
47
48                 public void AddConstraint (ITypeRef constraint)
49                 {
50                         if (constraint == null)
51                                 throw new ArgumentException ("constraint");
52
53                         if (constraintsList == null)
54                                 constraintsList = new ArrayList ();
55
56                         constraintsList.Add (constraint);
57                 }
58
59                 public override string ToString ()
60                 {
61                         return Id;
62                 }
63
64                 public void Resolve (CodeGen code_gen, PEAPI.MethodDef methoddef)
65                 {
66                         PEAPI.GenericParameter gp = methoddef.AddGenericParameter ((short) num, id, attr);
67                         ResolveConstraints (code_gen, gp);
68                 }
69
70                 public void Resolve (CodeGen code_gen, PEAPI.ClassDef classdef)
71                 {
72                         PEAPI.GenericParameter gp = classdef.AddGenericParameter ((short) num, id, attr);
73                         ResolveConstraints (code_gen, gp);
74                 }
75
76                 public void ResolveConstraints (CodeGen code_gen, PEAPI.GenericParameter gp)
77                 {
78                         if (constraintsList == null)
79                                 return;
80
81                         foreach (ITypeRef constraint in constraintsList) {
82                                 constraint.Resolve (code_gen);
83                                 gp.AddConstraint (constraint.PeapiType);
84                         }
85                 }
86
87         }
88
89         public class GenericParameters {
90                 ArrayList param_list;
91                 string param_str;
92
93                 public GenericParameters ()
94                 {
95                         param_list = null;
96                         param_str = null;
97                 }
98
99                 public int Count {
100                         get { return (param_list == null ? 0 : param_list.Count); }
101                 }
102
103                 public GenericParameter this [int index] {
104                         get { return (param_list != null ? (GenericParameter) param_list [index] : null); }
105                         set { Add (value); }
106                 }
107
108                 public void Add (GenericParameter gen_param)
109                 {
110                         if (gen_param == null)
111                                 throw new ArgumentException ("gen_param");
112
113                         if (param_list == null)
114                                 param_list = new ArrayList ();
115                         gen_param.Num = param_list.Count;
116                         param_list.Add (gen_param);
117                         param_str = null;
118                 }
119                 
120                 public int GetGenericParamNum (string id)
121                 {
122                         if (param_list == null)
123                                 //FIXME: Report error
124                                 throw new Exception (String.Format ("Invalid type parameter '{0}'", id));
125
126                         foreach (GenericParameter param in param_list)
127                                 if (param.Id == id)
128                                         return param.Num;
129                         return -1;
130                 }
131
132                 public void Resolve (CodeGen code_gen, PEAPI.ClassDef classdef)
133                 {
134                         foreach (GenericParameter param in param_list)
135                                 param.Resolve (code_gen, classdef);
136                 }
137                 
138                 public void Resolve (CodeGen code_gen, PEAPI.MethodDef methoddef)
139                 {
140                         foreach (GenericParameter param in param_list)
141                                 param.Resolve (code_gen, methoddef);
142                 }
143
144                 private void MakeString ()
145                 {
146                         //Build full_name (foo < , >)
147                         StringBuilder sb = new StringBuilder ();
148                         sb.Append ("<");
149                         foreach (GenericParameter param in param_list)
150                                 sb.AppendFormat ("{0}, ", param);
151                         //Remove the extra ', ' at the end
152                         sb.Length -= 2;
153                         sb.Append (">");
154                         param_str = sb.ToString ();
155                 }
156
157                 public override string ToString ()
158                 {
159                         if (param_str == null)
160                                 MakeString ();
161                         return param_str;
162                 }
163         }
164
165 }