+ //
+ // Extensions for generics
+ //
+ TypeParameter[] type_params;
+ TypeParameter[] type_param_list;
+
+ protected string GetInstantiationName ()
+ {
+ StringBuilder sb = new StringBuilder (Name);
+ sb.Append ("<");
+ for (int i = 0; i < type_param_list.Length; i++) {
+ if (i > 0)
+ sb.Append (",");
+ sb.Append (type_param_list [i].Name);
+ }
+ sb.Append (">");
+ return sb.ToString ();
+ }
+
+ bool check_type_parameter (ArrayList list, int start, string name)
+ {
+ for (int i = 0; i < start; i++) {
+ TypeParameter param = (TypeParameter) list [i];
+
+ if (param.Name != name)
+ continue;
+
+ Report.SymbolRelatedToPreviousError (Parent);
+ // TODO: Location is wrong (parent instead of child)
+ Report.Warning (693, 3, Location,
+ "Type parameter `{0}' has the same name as the type parameter from outer type `{1}'",
+ name, Parent.GetSignatureForError ());
+
+ return false;
+ }
+
+ return true;
+ }
+
+ TypeParameter[] initialize_type_params ()
+ {
+ if (type_param_list != null)
+ return type_param_list;
+
+ DeclSpace the_parent = Parent;
+ if (this is GenericMethod)
+ the_parent = null;
+
+ int start = 0;
+ ArrayList list = new ArrayList ();
+ if (the_parent != null && the_parent.IsGeneric) {
+ // FIXME: move generics info out of DeclSpace
+ TypeParameter[] parent_params = the_parent.PartialContainer.TypeParameters;
+ start = parent_params.Length;
+ list.AddRange (parent_params);
+ }
+
+ int count = type_params != null ? type_params.Length : 0;
+ for (int i = 0; i < count; i++) {
+ TypeParameter param = type_params [i];
+ check_type_parameter (list, start, param.Name);
+ list.Add (param);
+ }
+
+ type_param_list = new TypeParameter [list.Count];
+ list.CopyTo (type_param_list, 0);
+ return type_param_list;
+ }
+
+ public virtual void SetParameterInfo (ArrayList constraints_list)
+ {
+ if (!is_generic) {
+ if (constraints_list != null) {
+ Report.Error (
+ 80, Location, "Constraints are not allowed " +
+ "on non-generic declarations");
+ }
+
+ return;
+ }
+
+ TypeParameterName[] names = MemberName.TypeArguments.GetDeclarations ();
+ type_params = new TypeParameter [names.Length];
+
+ //
+ // Register all the names
+ //
+ for (int i = 0; i < type_params.Length; i++) {
+ TypeParameterName name = names [i];
+
+ Constraints constraints = null;
+ if (constraints_list != null) {
+ int total = constraints_list.Count;
+ for (int ii = 0; ii < total; ++ii) {
+ Constraints constraints_at = (Constraints)constraints_list[ii];
+ // TODO: it is used by iterators only
+ if (constraints_at == null) {
+ constraints_list.RemoveAt (ii);
+ --total;
+ continue;
+ }
+ if (constraints_at.TypeParameter == name.Name) {
+ constraints = constraints_at;
+ constraints_list.RemoveAt(ii);
+ break;
+ }
+ }
+ }
+
+ type_params [i] = new TypeParameter (
+ Parent, this, name.Name, constraints, name.OptAttributes,
+ Location);
+
+ AddToContainer (type_params [i], name.Name);
+ }
+
+ if (constraints_list != null && constraints_list.Count > 0) {
+ foreach (Constraints constraint in constraints_list) {
+ Report.Error(699, constraint.Location, "`{0}': A constraint references nonexistent type parameter `{1}'",
+ GetSignatureForError (), constraint.TypeParameter);
+ }
+ }
+ }
+
+ public TypeParameter[] TypeParameters {
+ get {
+ if (!IsGeneric)
+ throw new InvalidOperationException ();
+ if ((PartialContainer != null) && (PartialContainer != this))
+ return PartialContainer.TypeParameters;
+ if (type_param_list == null)
+ initialize_type_params ();
+
+ return type_param_list;
+ }
+ }
+
+ public TypeParameter[] CurrentTypeParameters {
+ get {
+ if (!IsGeneric)
+ throw new InvalidOperationException ();
+ if ((PartialContainer != null) && (PartialContainer != this))
+ return PartialContainer.CurrentTypeParameters;
+ if (type_params != null)
+ return type_params;
+ else
+ return new TypeParameter [0];
+ }
+ }
+
+ public int CountTypeParameters {
+ get {
+ return count_type_params;
+ }
+ }
+
+ public int CountCurrentTypeParameters {
+ get {
+ return count_current_type_params;
+ }
+ }
+
+ public TypeParameterExpr LookupGeneric (string name, Location loc)
+ {
+ if (!IsGeneric)
+ return null;
+
+ TypeParameter [] current_params;
+ if (this is TypeContainer)
+ current_params = PartialContainer.CurrentTypeParameters;
+ else
+ current_params = CurrentTypeParameters;
+
+ foreach (TypeParameter type_param in current_params) {
+ if (type_param.Name == name)
+ return new TypeParameterExpr (type_param, loc);
+ }
+
+ if (Parent != null)
+ return Parent.LookupGeneric (name, loc);
+
+ return null;
+ }
+
+ // Used for error reporting only
+ public virtual Type LookupAnyGeneric (string typeName)
+ {
+ return NamespaceEntry.NS.LookForAnyGenericType (typeName);
+ }
+