2 // Mono.ILASM.GenericParameters
5 // Ankit Jain <jankit@novell.com>
7 // Copyright 2006 Novell, Inc (http://www.novell.com)
11 using System
.Collections
;
14 namespace Mono
.ILASM
{
16 public class GenericParameter
: ICustomAttrTarget
{
19 PEAPI
.GenericParamAttributes attr
;
20 ArrayList constraintsList
;
21 ArrayList customattrList
;
23 public GenericParameter (string id
)
28 public GenericParameter (string id
, PEAPI
.GenericParamAttributes attr
, ArrayList constraints
)
33 constraintsList
= null;
34 customattrList
= null;
36 if (constraints
!= null)
37 foreach (BaseTypeRef typeref
in constraints
)
38 AddConstraint (typeref
);
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 ()
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)
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)
101 foreach (BaseTypeRef constraint
in constraintsList
) {
102 BaseGenericTypeRef gtr
= constraint
as BaseGenericTypeRef
;
104 gtr
.Resolve (type_gen_params
, method_gen_params
);
108 private void ResolveConstraints (CodeGen code_gen
, PEAPI
.GenericParameter gp
)
110 if (constraintsList
== null)
113 foreach (BaseTypeRef constraint
in constraintsList
) {
114 constraint
.Resolve (code_gen
);
115 gp
.AddConstraint (constraint
.PeapiType
);
121 public class GenericParameters
{
122 ArrayList param_list
;
125 public GenericParameters ()
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); }
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
);
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
)
163 public int GetGenericParamNum (string id
)
165 GenericParameter param
= GetGenericParam (id
);
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
);
191 private void MakeString ()
193 //Build full_name (foo < , >)
194 StringBuilder sb
= new StringBuilder ();
196 foreach (GenericParameter param
in param_list
)
197 sb
.AppendFormat ("{0}, ", param
);
198 //Remove the extra ', ' at the end
201 param_str
= sb
.ToString ();
204 public override string ToString ()
206 if (param_str
== null)