Apply Gert's version of the AutoResetEvent, and merge his test
[mono.git] / mcs / mcs / parameter.cs
1 //
2 // parameter.cs: Parameter definition.
3 //
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 //         Marek Safar (marek.safar@seznam.cz)
6 //
7 // Licensed under the terms of the GNU GPL
8 //
9 // (C) 2001 Ximian, Inc (http://www.ximian.com)
10 //
11 //
12 //
13 using System;
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Collections;
17 using System.Text;
18
19 namespace Mono.CSharp {
20
21         /// <summary>
22         ///   Abstract Base class for parameters of a method.
23         /// </summary>
24         public abstract class ParameterBase : Attributable {
25
26                 protected ParameterBuilder builder;
27
28                 protected ParameterBase (Attributes attrs)
29                         : base (attrs)
30                 {
31                 }
32
33                 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
34                 {
35                         if (a.Type == TypeManager.marshal_as_attr_type) {
36                                 UnmanagedMarshal marshal = a.GetMarshal (this);
37                                 if (marshal != null) {
38                                         builder.SetMarshal (marshal);
39                                 }
40                                 return;
41                         }
42
43                         if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
44                                 a.Error_InvalidSecurityParent ();
45                                 return;
46                         }
47
48                         builder.SetCustomAttribute (cb);
49                 }
50
51                 public override bool IsClsComplianceRequired()
52                 {
53                         return false;
54                 }
55         }
56
57         /// <summary>
58         /// Class for applying custom attributes on the return type
59         /// </summary>
60         public class ReturnParameter : ParameterBase {
61                 public ReturnParameter (MethodBuilder mb, Location location):
62                         base (null)
63                 {
64                         try {
65                                 builder = mb.DefineParameter (0, ParameterAttributes.None, "");                 
66                         }
67                         catch (ArgumentOutOfRangeException) {
68                                 Report.RuntimeMissingSupport (location, "custom attributes on the return type");
69                         }
70                 }
71
72                 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
73                 {
74                         if (a.Type == TypeManager.cls_compliant_attribute_type) {
75                                 Report.Warning (3023, 1, a.Location, "CLSCompliant attribute has no meaning when applied to return types. Try putting it on the method instead");
76                         }
77
78                         // This occurs after Warning -28
79                         if (builder == null)
80                                 return;
81
82                         base.ApplyAttributeBuilder (a, cb);
83                 }
84
85                 public override AttributeTargets AttributeTargets {
86                         get {
87                                 return AttributeTargets.ReturnValue;
88                         }
89                 }
90
91                 public override IResolveContext ResolveContext {
92                         get {
93                                 throw new NotSupportedException ();
94                         }
95                 }
96
97                 /// <summary>
98                 /// Is never called
99                 /// </summary>
100                 public override string[] ValidAttributeTargets {
101                         get {
102                                 return null;
103                         }
104                 }
105         }
106
107         /// <summary>
108         /// Class for applying custom attributes on the implicit parameter type
109         /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
110         /// </summary>
111         /// 
112         // TODO: should use more code from Parameter.ApplyAttributeBuilder
113         public class ImplicitParameter : ParameterBase {
114                 public ImplicitParameter (MethodBuilder mb):
115                         base (null)
116                 {
117                         builder = mb.DefineParameter (1, ParameterAttributes.None, "");                 
118                 }
119
120                 public override AttributeTargets AttributeTargets {
121                         get {
122                                 return AttributeTargets.Parameter;
123                         }
124                 }
125
126                 public override IResolveContext ResolveContext {
127                         get {
128                                 throw new NotSupportedException ();
129                         }
130                 }
131
132                 /// <summary>
133                 /// Is never called
134                 /// </summary>
135                 public override string[] ValidAttributeTargets {
136                         get {
137                                 return null;
138                         }
139                 }
140         }
141
142         public class ImplicitLambdaParameter : Parameter
143         {
144                 public ImplicitLambdaParameter (string name, Location loc)
145                         : base ((Type)null, name, Modifier.NONE, null, loc)
146                 {
147                 }
148         }
149
150         public class ParamsParameter : Parameter {
151                 public ParamsParameter (Expression type, string name, Attributes attrs, Location loc):
152                         base (type, name, Parameter.Modifier.PARAMS, attrs, loc)
153                 {
154                 }
155
156                 public override bool Resolve (IResolveContext ec)
157                 {
158                         if (!base.Resolve (ec))
159                                 return false;
160
161                         if (!parameter_type.IsArray || parameter_type.GetArrayRank () != 1) {
162                                 Report.Error (225, Location, "The params parameter must be a single dimensional array");
163                                 return false;
164                         }
165                         return true;
166                 }
167
168                 public override void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index)
169                 {
170                         base.ApplyAttributes (mb, cb, index);
171
172                         CustomAttributeBuilder a = new CustomAttributeBuilder (
173                                 TypeManager.cons_param_array_attribute, new object[0]);
174                                 
175                         builder.SetCustomAttribute (a);
176                 }
177         }
178
179         public class ArglistParameter : Parameter {
180                 // Doesn't have proper type because it's never chosen for better conversion
181                 public ArglistParameter () :
182                         base (typeof (ArglistParameter), String.Empty, Parameter.Modifier.ARGLIST, null, Location.Null)
183                 {
184                 }
185
186                 public override bool CheckAccessibility (InterfaceMemberBase member)
187                 {
188                         return true;
189                 }
190
191                 public override bool Resolve (IResolveContext ec)
192                 {
193                         return true;
194                 }
195
196                 public override string GetSignatureForError ()
197                 {
198                         return "__arglist";
199                 }
200         }
201
202         /// <summary>
203         ///   Represents a single method parameter
204         /// </summary>
205         public class Parameter : ParameterBase {
206                 [Flags]
207                 public enum Modifier : byte {
208                         NONE    = 0,
209                         REF     = REFMASK | ISBYREF,
210                         OUT     = OUTMASK | ISBYREF,
211                         PARAMS  = 4,
212                         // This is a flag which says that it's either REF or OUT.
213                         ISBYREF = 8,
214                         ARGLIST = 16,
215                         REFMASK = 32,
216                         OUTMASK = 64,
217                         This    = 128
218                 }
219
220                 static string[] attribute_targets = new string [] { "param" };
221
222                 public Expression TypeName;
223                 public Modifier modFlags;
224                 public string Name;
225                 public bool IsCaptured;
226                 protected Type parameter_type;
227                 public readonly Location Location;
228
229                 IResolveContext resolve_context;
230
231                 Variable var;
232                 public Variable Variable {
233                         get { return var; }
234                 }
235
236 #if GMCS_SOURCE
237                 public bool IsTypeParameter;
238                 GenericConstraints constraints;
239 #else
240                 public bool IsTypeParameter {
241                         get {
242                                 return false;
243                         }
244                         set {
245                                 if (value)
246                                         throw new Exception ("You can not se TypeParameter in MCS");
247                         }
248                 }
249 #endif
250                 
251                 public Parameter (Expression type, string name, Modifier mod, Attributes attrs, Location loc)
252                         : this (type.Type, name, mod, attrs, loc)
253                 {
254                         if (type == TypeManager.system_void_expr)
255                                 Report.Error (1536, loc, "Invalid parameter type `void'");
256                         
257                         TypeName = type;
258                 }
259
260                 public Parameter (Type type, string name, Modifier mod, Attributes attrs, Location loc)
261                         : base (attrs)
262                 {
263                         Name = name;
264                         modFlags = mod;
265                         parameter_type = type;
266                         Location = loc;
267                 }
268
269                 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
270                 {
271                         if (a.Type == TypeManager.in_attribute_type && ModFlags == Modifier.OUT) {
272                                 Report.Error (36, a.Location, "An out parameter cannot have the `In' attribute");
273                                 return;
274                         }
275
276                         if (a.Type == TypeManager.param_array_type) {
277                                 Report.Error (674, a.Location, "Do not use `System.ParamArrayAttribute'. Use the `params' keyword instead");
278                                 return;
279                         }
280
281                         if (a.Type == TypeManager.out_attribute_type && (ModFlags & Modifier.REF) == Modifier.REF &&
282                             !OptAttributes.Contains (TypeManager.in_attribute_type)) {
283                                 Report.Error (662, a.Location,
284                                         "Cannot specify only `Out' attribute on a ref parameter. Use both `In' and `Out' attributes or neither");
285                                 return;
286                         }
287
288                         if (a.Type == TypeManager.cls_compliant_attribute_type) {
289                                 Report.Warning (3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
290                         }
291
292                         // TypeManager.default_parameter_value_attribute_type is null if !NET_2_0, or if System.dll is not referenced
293                         if (a.Type == TypeManager.default_parameter_value_attribute_type) {
294                                 object val = a.GetParameterDefaultValue ();
295                                 if (val != null) {
296                                         Type t = val.GetType ();
297                                         if (t.IsArray || TypeManager.IsSubclassOf (t, TypeManager.type_type)) {
298                                                 if (parameter_type == TypeManager.object_type) {
299                                                         if (!t.IsArray)
300                                                                 t = TypeManager.type_type;
301
302                                                         Report.Error (1910, a.Location, "Argument of type `{0}' is not applicable for the DefaultValue attribute",
303                                                                 TypeManager.CSharpName (t));
304                                                 } else {
305                                                         Report.Error (1909, a.Location, "The DefaultValue attribute is not applicable on parameters of type `{0}'",
306                                                                 TypeManager.CSharpName (parameter_type)); ;
307                                                 }
308                                                 return;
309                                         }
310                                 }
311
312                                 if (parameter_type == TypeManager.object_type ||
313                                     (val == null && !TypeManager.IsValueType (parameter_type)) ||
314                                     (val != null && TypeManager.TypeToCoreType (val.GetType ()) == parameter_type))
315                                         builder.SetConstant (val);
316                                 else
317                                         Report.Error (1908, a.Location, "The type of the default value should match the type of the parameter");
318                                 return;
319                         }
320
321                         base.ApplyAttributeBuilder (a, cb);
322                 }
323                 
324                 public virtual bool CheckAccessibility (InterfaceMemberBase member)
325                 {
326                         if (IsTypeParameter)
327                                 return true;
328
329                         return member.ds.AsAccessible (parameter_type, member.ModFlags);
330                 }
331
332                 public override IResolveContext ResolveContext {
333                         get {
334                                 return resolve_context;
335                         }
336                 }
337
338                 // <summary>
339                 //   Resolve is used in method definitions
340                 // </summary>
341                 public virtual bool Resolve (IResolveContext ec)
342                 {
343                         // HACK: to resolve attributes correctly
344                         this.resolve_context = ec;
345
346                         if (parameter_type != null)
347                                 return true;
348
349                         TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec, false);
350                         if (texpr == null)
351                                 return false;
352
353                         parameter_type = texpr.Type;
354
355 #if GMCS_SOURCE
356                         TypeParameterExpr tparam = texpr as TypeParameterExpr;
357                         if (tparam != null) {
358                                 IsTypeParameter = true;
359                                 constraints = tparam.TypeParameter.Constraints;
360                                 return true;
361                         }
362 #endif
363
364                         if ((parameter_type.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
365                                 Report.Error (721, Location, "`{0}': static types cannot be used as parameters", 
366                                         texpr.GetSignatureForError ());
367                                 return false;
368                         }
369
370                         if ((modFlags & Parameter.Modifier.ISBYREF) != 0){
371                                 if (parameter_type == TypeManager.typed_reference_type ||
372                                     parameter_type == TypeManager.arg_iterator_type){
373                                         Report.Error (1601, Location, "Method or delegate parameter cannot be of type `{0}'",
374                                                 GetSignatureForError ());
375                                         return false;
376                                 }
377                         }
378
379                         if ((modFlags & Modifier.This) != 0 && parameter_type.IsPointer) {
380                                 Report.Error (1103, Location, "The type of extension method cannot be `{0}'",
381                                         TypeManager.CSharpName (parameter_type));
382                                 return false;
383                         }
384                         
385                         return true;
386                 }
387
388                 public void ResolveVariable (ToplevelBlock toplevel, int idx)
389                 {
390                         if (toplevel.RootScope != null)
391                                 var = toplevel.RootScope.GetCapturedParameter (this);
392                         if (var == null)
393                                 var = new ParameterVariable (this, idx);
394                 }
395
396                 public Type ExternalType ()
397                 {
398                         if ((modFlags & Parameter.Modifier.ISBYREF) != 0)
399                                 return TypeManager.GetReferenceType (parameter_type);
400                         
401                         return parameter_type;
402                 }
403
404                 public bool HasExtensionMethodModifier {
405                         get { return (modFlags & Modifier.This) != 0; }
406                 }
407
408                 public Modifier ModFlags {
409                         get { return modFlags & ~Modifier.This; }
410                 }
411
412                 public Type ParameterType {
413                         get {
414                                 return parameter_type;
415                         }
416                         set {
417                                 parameter_type = value;
418                                 IsTypeParameter = false;
419                         }
420                 }
421
422 #if GMCS_SOURCE
423                 public GenericConstraints GenericConstraints {
424                         get {
425                                 return constraints;
426                         }
427                 }
428 #endif
429
430                 ParameterAttributes Attributes {
431                         get {
432                                 return (modFlags & Modifier.OUT) == Modifier.OUT ?
433                                         ParameterAttributes.Out : ParameterAttributes.None;
434                         }
435                 }
436
437                 // TODO: should be removed !!!!!!!
438                 public static ParameterAttributes GetParameterAttributes (Modifier mod)
439                 {
440                         int flags = ((int) mod) & ~((int) Parameter.Modifier.ISBYREF);
441                         switch ((Modifier) flags) {
442                         case Modifier.NONE:
443                                 return ParameterAttributes.None;
444                         case Modifier.REF:
445                                 return ParameterAttributes.None;
446                         case Modifier.OUT:
447                                 return ParameterAttributes.Out;
448                         case Modifier.PARAMS:
449                                 return 0;
450                         }
451                                 
452                         return ParameterAttributes.None;
453                 }
454                 
455                 public override AttributeTargets AttributeTargets {
456                         get {
457                                 return AttributeTargets.Parameter;
458                         }
459                 }
460
461                 public virtual string GetSignatureForError ()
462                 {
463                         string type_name;
464                         if (parameter_type != null)
465                                 type_name = TypeManager.CSharpName (parameter_type);
466                         else
467                                 type_name = TypeName.GetSignatureForError ();
468
469                         string mod = GetModifierSignature (modFlags);
470                         if (mod.Length > 0)
471                                 return String.Concat (mod, ' ', type_name);
472
473                         return type_name;
474                 }
475
476                 public static string GetModifierSignature (Modifier mod)
477                 {
478                         switch (mod) {
479                                 case Modifier.OUT:
480                                         return "out";
481                                 case Modifier.PARAMS:
482                                         return "params";
483                                 case Modifier.REF:
484                                         return "ref";
485                                 case Modifier.ARGLIST:
486                                         return "__arglist";
487                                 case Modifier.This:
488                                         return "this";
489                                 default:
490                                         return "";
491                         }
492                 }
493
494                 public void IsClsCompliant ()
495                 {
496                         if (AttributeTester.IsClsCompliant (ExternalType ()))
497                                 return;
498
499                         Report.Error (3001, Location, "Argument type `{0}' is not CLS-compliant", GetSignatureForError ());
500                 }
501
502                 public virtual void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index)
503                 {
504 #if !GMCS_SOURCE || !MS_COMPATIBLE
505                         // TODO: It should use mb.DefineGenericParameters
506                         if (mb == null)
507                                 builder = cb.DefineParameter (index, Attributes, Name);
508                         else 
509                                 builder = mb.DefineParameter (index, Attributes, Name);
510 #endif
511
512                         if (OptAttributes != null)
513                                 OptAttributes.Emit ();
514                 }
515
516                 public override string[] ValidAttributeTargets {
517                         get {
518                                 return attribute_targets;
519                         }
520                 }
521
522                 protected class ParameterVariable : Variable
523                 {
524                         public readonly Parameter Parameter;
525                         public readonly int Idx;
526                         public readonly bool IsRef;
527
528                         public ParameterVariable (Parameter par, int idx)
529                         {
530                                 this.Parameter = par;
531                                 this.Idx = idx;
532                                 this.IsRef = (par.ModFlags & Parameter.Modifier.ISBYREF) != 0;
533                         }
534
535                         public override Type Type {
536                                 get { return Parameter.ParameterType; }
537                         }
538
539                         public override bool HasInstance {
540                                 get { return false; }
541                         }
542
543                         public override bool NeedsTemporary {
544                                 get { return false; }
545                         }
546
547                         public override void EmitInstance (EmitContext ec)
548                         {
549                         }
550
551                         public override void Emit (EmitContext ec)
552                         {
553                                 int arg_idx = Idx;
554                                 if (!ec.MethodIsStatic)
555                                         arg_idx++;
556
557                                 ParameterReference.EmitLdArg (ec.ig, arg_idx);
558                         }
559
560                         public override void EmitAssign (EmitContext ec)
561                         {
562                                 int arg_idx = Idx;
563                                 if (!ec.MethodIsStatic)
564                                         arg_idx++;
565
566                                 if (arg_idx <= 255)
567                                         ec.ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
568                                 else
569                                         ec.ig.Emit (OpCodes.Starg, arg_idx);
570                         }
571
572                         public override void EmitAddressOf (EmitContext ec)
573                         {
574                                 int arg_idx = Idx;
575
576                                 if (!ec.MethodIsStatic)
577                                         arg_idx++;
578
579                                 if (IsRef) {
580                                         if (arg_idx <= 255)
581                                                 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
582                                         else
583                                                 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
584                                 } else {
585                                         if (arg_idx <= 255)
586                                                 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
587                                         else
588                                                 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
589                                 }
590                         }
591                 }
592
593                 public Parameter Clone ()
594                 {
595                         Parameter p = new Parameter (parameter_type, Name, ModFlags, attributes, Location);
596                         p.IsTypeParameter = IsTypeParameter;
597
598                         return p;
599                 }
600         }
601
602         /// <summary>
603         ///   Represents the methods parameters
604         /// </summary>
605         public class Parameters : ParameterData {
606                 // Null object pattern
607                 public Parameter [] FixedParameters;
608                 public readonly bool HasArglist;
609                 Type [] types;
610                 int count;
611
612                 public static readonly Parameters EmptyReadOnlyParameters = new Parameters ();
613                 static readonly Parameter ArgList = new ArglistParameter ();
614
615 #if GMCS_SOURCE
616 //              public readonly TypeParameter[] TypeParameters;
617 #endif
618
619                 private Parameters ()
620                 {
621                         FixedParameters = new Parameter[0];
622                         types = new Type [0];
623                 }
624
625                 private Parameters (Parameter[] parameters, Type[] types)
626                 {
627                         FixedParameters = parameters;
628                         this.types = types;
629                         count = types.Length;
630                 }
631                 
632                 public Parameters (params Parameter[] parameters)
633                 {
634                         if (parameters == null)
635                                 throw new ArgumentException ("Use EmptyReadOnlyPatameters");
636
637                         FixedParameters = parameters;
638                         count = parameters.Length;
639                 }
640
641                 public Parameters (Parameter[] parameters, bool has_arglist):
642                         this (parameters)
643                 {
644                         HasArglist = has_arglist;
645                 }
646                 
647                 public static Parameters CreateFullyResolved (Parameter p)
648                 {
649                         return new Parameters (new Parameter [] { p }, new Type [] { p.ParameterType });
650                 }
651                 
652                 public static Parameters CreateFullyResolved (Parameter[] parameters, Type[] types)
653                 {
654                         return new Parameters (parameters, types);
655                 }
656
657                 /// <summary>
658                 /// Use this method when you merge compiler generated argument with user arguments
659                 /// </summary>
660                 public static Parameters MergeGenerated (Parameters userParams, params Parameter[] compilerParams)
661                 {
662                         Parameter[] all_params = new Parameter [userParams.count + compilerParams.Length];
663                         Type[] all_types = new Type[all_params.Length];
664                         userParams.FixedParameters.CopyTo(all_params, 0);
665                         userParams.Types.CopyTo (all_types, 0);
666
667                         int last_filled = userParams.Count;
668                         foreach (Parameter p in compilerParams) {
669                                 for (int i = 0; i < last_filled; ++i) {
670                                         while (p.Name == all_params [i].Name) {
671                                                 p.Name = '_' + p.Name;
672                                         }
673                                 }
674                                 all_params [last_filled] = p;
675                                 all_types [last_filled] = p.ParameterType;
676                                 ++last_filled;
677                         }
678                         
679                         return new Parameters (all_params, all_types);
680                 }
681
682                 public bool Empty {
683                         get {
684                                 return count == 0;
685                         }
686                 }
687
688                 public int Count {
689                         get {
690                                 return HasArglist ? count + 1 : count;
691                         }
692                 }
693
694                 //
695                 // The property can be used after parameter types were resolved.
696                 //
697                 public Type ExtensionMethodType {
698                         get {
699                                 if (count == 0)
700                                         return null;
701
702                                 return FixedParameters [0].HasExtensionMethodModifier ?
703                                         types [0] : null;
704                         }
705                 }
706
707                 public bool HasExtensionMethodType {
708                         get {
709                                 if (count == 0)
710                                         return false;
711
712                                 return FixedParameters [0].HasExtensionMethodModifier;
713                         }
714                 }
715
716
717                 bool VerifyArgs ()
718                 {
719                         if (count < 2)
720                                 return true;
721
722                         for (int i = 0; i < count; i++){
723                                 string base_name = FixedParameters [i].Name;
724                                 for (int j = i + 1; j < count; j++){
725                                         if (base_name != FixedParameters [j].Name)
726                                                 continue;
727
728                                         Report.Error (100, FixedParameters [i].Location,
729                                                 "The parameter name `{0}' is a duplicate", base_name);
730                                         return false;
731                                 }
732                         }
733                         return true;
734                 }
735                 
736                 
737                 /// <summary>
738                 ///    Returns the paramenter information based on the name
739                 /// </summary>
740                 public Parameter GetParameterByName (string name, out int idx)
741                 {
742                         idx = 0;
743
744                         if (count == 0)
745                                 return null;
746
747                         int i = 0;
748
749                         foreach (Parameter par in FixedParameters){
750                                 if (par.Name == name){
751                                         idx = i;
752                                         return par;
753                                 }
754                                 i++;
755                         }
756                         return null;
757                 }
758
759                 public Parameter GetParameterByName (string name)
760                 {
761                         int idx;
762
763                         return GetParameterByName (name, out idx);
764                 }
765                 
766                 public bool Resolve (IResolveContext ec)
767                 {
768                         if (types != null)
769                                 return true;
770
771                         types = new Type [count];
772                         
773                         if (!VerifyArgs ())
774                                 return false;
775
776                         bool ok = true;
777                         Parameter p;
778                         for (int i = 0; i < FixedParameters.Length; ++i) {
779                                 p = FixedParameters [i];
780                                 if (!p.Resolve (ec)) {
781                                         ok = false;
782                                         continue;
783                                 }
784                                 types [i] = p.ExternalType ();
785                         }
786
787                         return ok;
788                 }
789
790                 public void ResolveVariable (ToplevelBlock toplevel)
791                 {
792                         for (int i = 0; i < FixedParameters.Length; ++i) {
793                                 Parameter p = FixedParameters [i];
794                                 p.ResolveVariable (toplevel, i);
795                         }
796                 }
797
798                 public CallingConventions CallingConvention
799                 {
800                         get {
801                                 if (HasArglist)
802                                         return CallingConventions.VarArgs;
803                                 else
804                                         return CallingConventions.Standard;
805                         }
806                 }
807
808                 // Define each type attribute (in/out/ref) and
809                 // the argument names.
810                 public void ApplyAttributes (MethodBase builder)
811                 {
812                         if (count == 0)
813                                 return;
814
815                         MethodBuilder mb = builder as MethodBuilder;
816                         ConstructorBuilder cb = builder as ConstructorBuilder;
817
818                         for (int i = 0; i < FixedParameters.Length; i++) {
819                                 FixedParameters [i].ApplyAttributes (mb, cb, i + 1);
820                         }
821                 }
822
823 #if MS_COMPATIBLE
824                 public void InflateTypes (Type[] genArguments, Type[] argTypes)
825                 {
826                         for (int i = 0; i < count; ++i) {
827                                 if (FixedParameters[i].IsTypeParameter) {
828                                         for (int ii = 0; ii < genArguments.Length; ++ii) {
829                                                 if (types[i] != genArguments[ii])
830                                                         continue;
831
832                                                 types[i] = argTypes[ii];
833                                                 FixedParameters[i].ParameterType = types[i];
834                                                 break;
835                                         }
836                                         continue;
837                                 }
838
839                                 if (types[i].IsGenericType) {
840                                         types[i] = types[i].GetGenericTypeDefinition().MakeGenericType (argTypes[0]); // FIXME: The order can be different
841                                         FixedParameters[i].ParameterType = types[i];
842                                 }
843                         }
844                 }
845 #endif
846
847                 public void VerifyClsCompliance ()
848                 {
849                         foreach (Parameter p in FixedParameters)
850                                 p.IsClsCompliant ();
851                 }
852
853                 public string GetSignatureForError ()
854                 {
855                         StringBuilder sb = new StringBuilder ("(");
856                         if (count > 0) {
857                                 for (int i = 0; i < FixedParameters.Length; ++i) {
858                                         sb.Append (FixedParameters[i].GetSignatureForError ());
859                                         if (i < FixedParameters.Length - 1)
860                                                 sb.Append (", ");
861                                 }
862                         }
863
864                         if (HasArglist) {
865                                 if (sb.Length > 1)
866                                         sb.Append (", ");
867                                 sb.Append ("__arglist");
868                         }
869
870                         sb.Append (')');
871                         return sb.ToString ();
872                 }
873
874                 public Type[] Types {
875                         get {
876                                 return types;
877                         }
878                 }
879
880                 public Parameter this [int pos]
881                 {
882                         get {
883                                 if (pos >= count && (HasArglist || HasParams)) {
884                                         if (HasArglist && (pos == 0 || pos >= count))
885                                                 return ArgList;
886                                         pos = count - 1;
887                                 }
888
889                                 return FixedParameters [pos];
890                         }
891                 }
892
893                 #region ParameterData Members
894
895                 public Type ParameterType (int pos)
896                 {
897                         return this [pos].ExternalType ();
898                 }
899
900                 public bool HasParams {
901                         get {
902                                 if (count == 0)
903                                         return false;
904
905                                 for (int i = count; i != 0; --i) {
906                                         if ((FixedParameters [i - 1].ModFlags & Parameter.Modifier.PARAMS) != 0)
907                                                 return true;
908                                 }
909                                 return false;
910                         }
911                 }
912
913                 public string ParameterName (int pos)
914                 {
915                         return this [pos].Name;
916                 }
917
918                 public string ParameterDesc (int pos)
919                 {
920                         return this [pos].GetSignatureForError ();
921                 }
922
923                 public Parameter.Modifier ParameterModifier (int pos)
924                 {
925                         return this [pos].ModFlags;
926                 }
927
928                 public Parameters Clone ()
929                 {
930                         Parameter [] parameters_copy = new Parameter [FixedParameters.Length];
931                         int i = 0;
932                         foreach (Parameter p in FixedParameters)
933                                 parameters_copy [i++] = p.Clone ();
934                         Parameters ps = new Parameters (parameters_copy, HasArglist);
935                         if (types != null)
936                                 ps.types = (Type[])types.Clone ();
937                         return ps;
938                 }
939                 
940                 #endregion
941         }
942 }