2010-04-01 Jb Evain <jbevain@novell.com>
[mcs.git] / ilasm / codegen / GenericParameters.cs
blob9dcb449a448f2b7337a7087d3ffd8a9fc7ae6a7e
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 //
10 using System;
11 using System.Collections;
12 using System.Text;
14 namespace Mono.ILASM {
16 public class GenericParameter : ICustomAttrTarget {
17 string id;
18 int num;
19 PEAPI.GenericParamAttributes attr;
20 ArrayList constraintsList;
21 ArrayList customattrList;
23 public GenericParameter (string id)
24 : this (id, 0, null)
28 public GenericParameter (string id, PEAPI.GenericParamAttributes attr, ArrayList constraints)
30 this.id = id;
31 this.attr = attr;
32 num = -1;
33 constraintsList = null;
34 customattrList = null;
36 if (constraints != null)
37 foreach (BaseTypeRef typeref in constraints)
38 AddConstraint (typeref);
41 public string Id {
42 get { return id; }
45 public int Num {
46 get { return num; }
47 set { num = value; }
50 public void AddConstraint (BaseTypeRef constraint)
52 if (constraint == null)
53 throw new InternalErrorException ();
55 if (constraintsList == null)
56 constraintsList = new ArrayList ();
58 constraintsList.Add (constraint);
61 public override string ToString ()
63 return Id;
66 public void AddCustomAttribute (CustomAttr customattr)
68 if (customattrList == null)
69 customattrList = new ArrayList ();
71 customattrList.Add (customattr);
74 public void Resolve (CodeGen code_gen, PEAPI.MethodDef methoddef)
76 PEAPI.GenericParameter gp = methoddef.AddGenericParameter ((short) num, id, attr);
77 Resolve (code_gen, gp);
80 public void Resolve (CodeGen code_gen, PEAPI.ClassDef classdef)
82 PEAPI.GenericParameter gp = classdef.AddGenericParameter ((short) num, id, attr);
83 Resolve (code_gen, gp);
86 private void Resolve (CodeGen code_gen, PEAPI.GenericParameter gp)
88 ResolveConstraints (code_gen, gp);
89 if (customattrList == null)
90 return;
92 foreach (CustomAttr customattr in customattrList)
93 customattr.AddTo (code_gen, gp);
96 public void ResolveConstraints (GenericParameters type_gen_params, GenericParameters method_gen_params)
98 if (constraintsList == null)
99 return;
101 foreach (BaseTypeRef constraint in constraintsList) {
102 BaseGenericTypeRef gtr = constraint as BaseGenericTypeRef;
103 if (gtr != null)
104 gtr.Resolve (type_gen_params, method_gen_params);
108 private void ResolveConstraints (CodeGen code_gen, PEAPI.GenericParameter gp)
110 if (constraintsList == null)
111 return;
113 foreach (BaseTypeRef constraint in constraintsList) {
114 constraint.Resolve (code_gen);
115 gp.AddConstraint (constraint.PeapiType);
121 public class GenericParameters {
122 ArrayList param_list;
123 string param_str;
125 public GenericParameters ()
127 param_list = null;
128 param_str = null;
131 public int Count {
132 get { return (param_list == null ? 0 : param_list.Count); }
135 public GenericParameter this [int index] {
136 get { return (param_list != null ? (GenericParameter) param_list [index] : null); }
137 set { Add (value); }
140 public void Add (GenericParameter gen_param)
142 if (gen_param == null)
143 throw new InternalErrorException ();
145 if (param_list == null)
146 param_list = new ArrayList ();
147 gen_param.Num = param_list.Count;
148 param_list.Add (gen_param);
149 param_str = null;
152 public GenericParameter GetGenericParam (string id)
154 if (param_list == null)
155 Report.Error ("Invalid type parameter '" + id + "'");
157 foreach (GenericParameter param in param_list)
158 if (param.Id == id)
159 return param;
160 return null;
163 public int GetGenericParamNum (string id)
165 GenericParameter param = GetGenericParam (id);
166 if (param != null)
167 return param.Num;
169 return -1;
172 public void Resolve (CodeGen code_gen, PEAPI.ClassDef classdef)
174 foreach (GenericParameter param in param_list)
175 param.Resolve (code_gen, classdef);
178 public void Resolve (CodeGen code_gen, PEAPI.MethodDef methoddef)
180 foreach (GenericParameter param in param_list)
181 param.Resolve (code_gen, methoddef);
184 public void ResolveConstraints (GenericParameters type_gen_params, GenericParameters method_gen_params)
186 foreach (GenericParameter param in param_list)
187 param.ResolveConstraints (type_gen_params, method_gen_params);
188 param_str = null;
191 private void MakeString ()
193 //Build full_name (foo < , >)
194 StringBuilder sb = new StringBuilder ();
195 sb.Append ("<");
196 foreach (GenericParameter param in param_list)
197 sb.AppendFormat ("{0}, ", param);
198 //Remove the extra ', ' at the end
199 sb.Length -= 2;
200 sb.Append (">");
201 param_str = sb.ToString ();
204 public override string ToString ()
206 if (param_str == null)
207 MakeString ();
208 return param_str;