2009-06-01 Marek Safar <marek.safar@gmail.com>
[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 // Dual licensed under the terms of the MIT X11 or GNU GPL
8 //
9 // Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
10 // Copyright 2003-2008 Novell, Inc. 
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, PredefinedAttributes pa)
34                 {
35 #if !NET_2_0
36                         if (a.Type == pa.MarshalAs) {
37                                 UnmanagedMarshal marshal = a.GetMarshal (this);
38                                 if (marshal != null) {
39                                         builder.SetMarshal (marshal);
40                                 }
41                                 return;
42                         }
43 #endif
44                         if (a.HasSecurityAttribute) {
45                                 a.Error_InvalidSecurityParent ();
46                                 return;
47                         }
48
49                         builder.SetCustomAttribute (cb);
50                 }
51
52                 public override bool IsClsComplianceRequired()
53                 {
54                         return false;
55                 }
56         }
57
58         /// <summary>
59         /// Class for applying custom attributes on the return type
60         /// </summary>
61         public class ReturnParameter : ParameterBase {
62                 public ReturnParameter (MethodBuilder mb, Location location):
63                         base (null)
64                 {
65                         try {
66                                 builder = mb.DefineParameter (0, ParameterAttributes.None, "");                 
67                         }
68                         catch (ArgumentOutOfRangeException) {
69                                 Report.RuntimeMissingSupport (location, "custom attributes on the return type");
70                         }
71                 }
72
73                 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
74                 {
75                         if (a.Type == pa.CLSCompliant) {
76                                 Report.Warning (3023, 1, a.Location, "CLSCompliant attribute has no meaning when applied to return types. Try putting it on the method instead");
77                         }
78
79                         // This occurs after Warning -28
80                         if (builder == null)
81                                 return;
82
83                         base.ApplyAttributeBuilder (a, cb, pa);
84                 }
85
86                 public override AttributeTargets AttributeTargets {
87                         get {
88                                 return AttributeTargets.ReturnValue;
89                         }
90                 }
91
92                 public override IResolveContext ResolveContext {
93                         get {
94                                 throw new NotSupportedException ();
95                         }
96                 }
97
98                 /// <summary>
99                 /// Is never called
100                 /// </summary>
101                 public override string[] ValidAttributeTargets {
102                         get {
103                                 return null;
104                         }
105                 }
106         }
107
108         /// <summary>
109         /// Class for applying custom attributes on the implicit parameter type
110         /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
111         /// </summary>
112         /// 
113         // TODO: should use more code from Parameter.ApplyAttributeBuilder
114         public class ImplicitParameter : ParameterBase {
115                 public ImplicitParameter (MethodBuilder mb):
116                         base (null)
117                 {
118                         builder = mb.DefineParameter (1, ParameterAttributes.None, "value");                    
119                 }
120
121                 public override AttributeTargets AttributeTargets {
122                         get {
123                                 return AttributeTargets.Parameter;
124                         }
125                 }
126
127                 public override IResolveContext ResolveContext {
128                         get {
129                                 throw new NotSupportedException ();
130                         }
131                 }
132
133                 /// <summary>
134                 /// Is never called
135                 /// </summary>
136                 public override string[] ValidAttributeTargets {
137                         get {
138                                 return null;
139                         }
140                 }
141         }
142
143         public class ImplicitLambdaParameter : Parameter
144         {
145                 public ImplicitLambdaParameter (string name, Location loc)
146                         : base (null, name, Modifier.NONE, null, loc)
147                 {
148                 }
149
150                 public override Type Resolve (IResolveContext ec)
151                 {
152                         if (parameter_type == null)
153                                 throw new InternalErrorException ("A type of implicit lambda parameter `{0}' is not set",
154                                         Name);
155
156                         return parameter_type;
157                 }
158
159                 public Type Type {
160                         set { parameter_type = value; }
161                 }
162         }
163
164         public class ParamsParameter : Parameter {
165                 public ParamsParameter (FullNamedExpression type, string name, Attributes attrs, Location loc):
166                         base (type, name, Parameter.Modifier.PARAMS, attrs, loc)
167                 {
168                 }
169
170                 public override Type Resolve (IResolveContext ec)
171                 {
172                         if (base.Resolve (ec) == null)
173                                 return null;
174
175                         if (!parameter_type.IsArray || parameter_type.GetArrayRank () != 1) {
176                                 Report.Error (225, Location, "The params parameter must be a single dimensional array");
177                                 return null;
178                         }
179
180                         return parameter_type;
181                 }
182
183                 public override void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index)
184                 {
185                         base.ApplyAttributes (mb, cb, index);
186                         PredefinedAttributes.Get.ParamArray.EmitAttribute (builder, Location);
187                 }
188         }
189
190         public class ArglistParameter : Parameter {
191                 // Doesn't have proper type because it's never chosen for better conversion
192                 public ArglistParameter (Location loc) :
193                         base (null, String.Empty, Parameter.Modifier.ARGLIST, null, loc)
194                 {
195                 }
196
197                 public override void  ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index)
198                 {
199                         // Nothing to do
200                 }
201
202                 public override bool CheckAccessibility (InterfaceMemberBase member)
203                 {
204                         return true;
205                 }
206
207                 public override Type Resolve (IResolveContext ec)
208                 {
209                         return typeof (ArglistParameter);
210                 }
211
212                 public override string GetSignatureForError ()
213                 {
214                         return "__arglist";
215                 }
216         }
217
218         public interface IParameterData
219         {
220                 bool HasExtensionMethodModifier { get; }
221                 Parameter.Modifier ModFlags { get; }
222                 string Name { get; }
223         }
224
225         //
226         // Parameter information created by parser
227         //
228         public class Parameter : ParameterBase, IParameterData, ILocalVariable {
229                 [Flags]
230                 public enum Modifier : byte {
231                         NONE    = 0,
232                         REF     = REFMASK | ISBYREF,
233                         OUT     = OUTMASK | ISBYREF,
234                         PARAMS  = 4,
235                         // This is a flag which says that it's either REF or OUT.
236                         ISBYREF = 8,
237                         ARGLIST = 16,
238                         REFMASK = 32,
239                         OUTMASK = 64,
240                         This    = 128
241                 }
242
243                 static string[] attribute_targets = new string [] { "param" };
244
245                 protected FullNamedExpression TypeName;
246                 readonly Modifier modFlags;
247                 string name;
248                 protected Type parameter_type;
249                 public readonly Location Location;
250                 int idx;
251                 public bool HasAddressTaken;
252
253                 IResolveContext resolve_context;
254                 LocalVariableReference expr_tree_variable;
255                 static TypeExpr parameter_expr_tree_type;
256
257                 public HoistedVariable HoistedVariableReference;
258
259                 public Parameter (FullNamedExpression type, string name, Modifier mod, Attributes attrs, Location loc)
260                         : base (attrs)
261                 {
262                         if (type == TypeManager.system_void_expr)
263                                 Report.Error (1536, loc, "Invalid parameter type `void'");
264
265                         this.name = name;
266                         modFlags = mod;
267                         Location = loc;
268                         TypeName = type;
269                 }
270
271                 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
272                 {
273                         if (a.Type == pa.In && ModFlags == Modifier.OUT) {
274                                 Report.Error (36, a.Location, "An out parameter cannot have the `In' attribute");
275                                 return;
276                         }
277
278                         if (a.Type == pa.ParamArray) {
279                                 Report.Error (674, a.Location, "Do not use `System.ParamArrayAttribute'. Use the `params' keyword instead");
280                                 return;
281                         }
282
283                         if (a.Type == PredefinedAttributes.Get.Out && (ModFlags & Modifier.REF) == Modifier.REF &&
284                             !OptAttributes.Contains (pa.In)) {
285                                 Report.Error (662, a.Location,
286                                         "Cannot specify only `Out' attribute on a ref parameter. Use both `In' and `Out' attributes or neither");
287                                 return;
288                         }
289
290                         if (a.Type == pa.CLSCompliant) {
291                                 Report.Warning (3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
292                         }
293
294                         // TypeManager.default_parameter_value_attribute_type is null if !NET_2_0, or if System.dll is not referenced
295                         if (a.Type == pa.DefaultParameterValue) {
296                                 object val = a.GetParameterDefaultValue ();
297                                 if (val != null) {
298                                         Type t = val.GetType ();
299                                         if (t.IsArray || TypeManager.IsSubclassOf (t, TypeManager.type_type)) {
300                                                 if (parameter_type == TypeManager.object_type) {
301                                                         if (!t.IsArray)
302                                                                 t = TypeManager.type_type;
303
304                                                         Report.Error (1910, a.Location, "Argument of type `{0}' is not applicable for the DefaultValue attribute",
305                                                                 TypeManager.CSharpName (t));
306                                                 } else {
307                                                         Report.Error (1909, a.Location, "The DefaultValue attribute is not applicable on parameters of type `{0}'",
308                                                                 TypeManager.CSharpName (parameter_type)); ;
309                                                 }
310                                                 return;
311                                         }
312                                 }
313
314                                 if (parameter_type == TypeManager.object_type ||
315                                     (val == null && !TypeManager.IsGenericParameter (parameter_type) && TypeManager.IsReferenceType (parameter_type)) ||
316                                     (val != null && TypeManager.TypeToCoreType (val.GetType ()) == parameter_type))
317                                         builder.SetConstant (val);
318                                 else
319                                         Report.Error (1908, a.Location, "The type of the default value should match the type of the parameter");
320                                 return;
321                         }
322
323                         base.ApplyAttributeBuilder (a, cb, pa);
324                 }
325                 
326                 public virtual bool CheckAccessibility (InterfaceMemberBase member)
327                 {
328                         if (parameter_type == null || TypeManager.IsGenericParameter (parameter_type))
329                                 return true;
330
331                         return member.IsAccessibleAs (parameter_type);
332                 }
333
334                 public override IResolveContext ResolveContext {
335                         get {
336                                 return resolve_context;
337                         }
338                 }
339
340                 // <summary>
341                 //   Resolve is used in method definitions
342                 // </summary>
343                 public virtual Type Resolve (IResolveContext ec)
344                 {
345                         // HACK: to resolve attributes correctly
346                         this.resolve_context = ec;
347
348                         if (parameter_type != null)
349                                 return parameter_type;
350
351                         TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec, false);
352                         if (texpr == null)
353                                 return null;
354
355                         parameter_type = texpr.Type;
356
357                         if ((modFlags & Parameter.Modifier.ISBYREF) != 0 &&
358                                 TypeManager.IsSpecialType (parameter_type)) {
359                                 Report.Error (1601, Location, "Method or delegate parameter cannot be of type `{0}'",
360                                         GetSignatureForError ());
361                                 return null;
362                         }
363
364                         TypeManager.CheckTypeVariance (parameter_type,
365                                 (modFlags & Parameter.Modifier.ISBYREF) != 0 ? Variance.None : Variance.Contravariant,
366                                 ec as MemberCore);
367
368                         if (texpr is TypeParameterExpr)
369                                 return parameter_type;
370
371                         if ((parameter_type.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
372                                 Report.Error (721, Location, "`{0}': static types cannot be used as parameters",
373                                         texpr.GetSignatureForError ());
374                                 return parameter_type;
375                         }
376
377                         if ((modFlags & Modifier.This) != 0 && parameter_type.IsPointer) {
378                                 Report.Error (1103, Location, "The type of extension method cannot be `{0}'",
379                                         TypeManager.CSharpName (parameter_type));
380                         }
381
382                         return parameter_type;
383                 }
384
385                 public void ResolveVariable (int idx)
386                 {
387                         this.idx = idx;
388                 }
389
390                 public bool HasExtensionMethodModifier {
391                         get { return (modFlags & Modifier.This) != 0; }
392                 }
393
394                 public Modifier ModFlags {
395                         get { return modFlags & ~Modifier.This; }
396                 }
397
398                 public string Name {
399                         get { return name; }
400                         set { name = value; }
401                 }
402
403                 ParameterAttributes Attributes {
404                         get { return ParametersCompiled.GetParameterAttribute (modFlags); }
405                 }
406
407                 public override AttributeTargets AttributeTargets {
408                         get {
409                                 return AttributeTargets.Parameter;
410                         }
411                 }
412
413                 public virtual string GetSignatureForError ()
414                 {
415                         string type_name;
416                         if (parameter_type != null)
417                                 type_name = TypeManager.CSharpName (parameter_type);
418                         else
419                                 type_name = TypeName.GetSignatureForError ();
420
421                         string mod = GetModifierSignature (modFlags);
422                         if (mod.Length > 0)
423                                 return String.Concat (mod, " ", type_name);
424
425                         return type_name;
426                 }
427
428                 public static string GetModifierSignature (Modifier mod)
429                 {
430                         switch (mod) {
431                         case Modifier.OUT:
432                                 return "out";
433                         case Modifier.PARAMS:
434                                 return "params";
435                         case Modifier.REF:
436                                 return "ref";
437                         case Modifier.This:
438                                 return "this";
439                         default:
440                                 return "";
441                         }
442                 }
443
444                 public void IsClsCompliant ()
445                 {
446                         if (AttributeTester.IsClsCompliant (parameter_type))
447                                 return;
448
449                         Report.Warning (3001, 1, Location, "Argument type `{0}' is not CLS-compliant", GetSignatureForError ());
450                 }
451
452                 public virtual void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index)
453                 {
454                         if (mb == null)
455                                 builder = cb.DefineParameter (index, Attributes, Name);
456                         else
457                                 builder = mb.DefineParameter (index, Attributes, Name);
458
459                         if (OptAttributes != null)
460                                 OptAttributes.Emit ();
461                 }
462
463                 public override string[] ValidAttributeTargets {
464                         get {
465                                 return attribute_targets;
466                         }
467                 }
468
469                 public Parameter Clone ()
470                 {
471                         Parameter p = (Parameter) MemberwiseClone ();
472                         if (attributes != null) {
473                                 p.attributes = attributes.Clone ();
474                                 p.attributes.AttachTo (p);
475                         }
476
477                         return p;
478                 }
479
480                 public ExpressionStatement CreateExpressionTreeVariable (EmitContext ec)
481                 {
482                         //
483                         // A parameter is not hoisted when used directly as ET
484                         //
485                         HoistedVariableReference = null;
486
487                         if ((modFlags & Modifier.ISBYREF) != 0)
488                                 Report.Error (1951, Location, "An expression tree parameter cannot use `ref' or `out' modifier");
489
490                         LocalInfo variable = ec.CurrentBlock.AddTemporaryVariable (
491                                 ResolveParameterExpressionType (ec, Location), Location);
492                         variable.Resolve (ec);
493
494                         expr_tree_variable = new LocalVariableReference (
495                                 ec.CurrentBlock, variable.Name, Location, variable, false);
496
497                         ArrayList arguments = new ArrayList (2);
498                         arguments.Add (new Argument (new TypeOf (
499                                 new TypeExpression (parameter_type, Location), Location)));
500                         arguments.Add (new Argument (new StringConstant (Name, Location)));
501                         return new SimpleAssign (ExpressionTreeVariableReference (),
502                                 Expression.CreateExpressionFactoryCall ("Parameter", null, arguments, Location));
503                 }
504
505                 public void Emit (EmitContext ec)
506                 {
507                         int arg_idx = idx;
508                         if (!ec.IsStatic)
509                                 arg_idx++;
510
511                         ParameterReference.EmitLdArg (ec.ig, arg_idx);
512                 }
513
514                 public void EmitAssign (EmitContext ec)
515                 {
516                         int arg_idx = idx;
517                         if (!ec.IsStatic)
518                                 arg_idx++;
519
520                         if (arg_idx <= 255)
521                                 ec.ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
522                         else
523                                 ec.ig.Emit (OpCodes.Starg, arg_idx);
524                 }
525
526                 public void EmitAddressOf (EmitContext ec)
527                 {
528                         int arg_idx = idx;
529
530                         if (!ec.IsStatic)
531                                 arg_idx++;
532
533                         bool is_ref = (ModFlags & Modifier.ISBYREF) != 0;
534                         if (is_ref) {
535                                 ParameterReference.EmitLdArg (ec.ig, arg_idx);
536                         } else {
537                                 if (arg_idx <= 255)
538                                         ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
539                                 else
540                                         ec.ig.Emit (OpCodes.Ldarga, arg_idx);
541                         }
542                 }
543
544                 public Expression ExpressionTreeVariableReference ()
545                 {
546                         return expr_tree_variable;
547                 }
548
549                 //
550                 // System.Linq.Expressions.ParameterExpression type
551                 //
552                 public static TypeExpr ResolveParameterExpressionType (EmitContext ec, Location location)
553                 {
554                         if (parameter_expr_tree_type != null)
555                                 return parameter_expr_tree_type;
556
557                         Type p_type = TypeManager.parameter_expression_type;
558                         if (p_type == null) {
559                                 p_type = TypeManager.CoreLookupType ("System.Linq.Expressions", "ParameterExpression", Kind.Class, true);
560                                 TypeManager.parameter_expression_type = p_type;
561                         }
562
563                         parameter_expr_tree_type = new TypeExpression (p_type, location).
564                                 ResolveAsTypeTerminal (ec, false);
565
566                         return parameter_expr_tree_type;
567                 }
568         }
569
570         //
571         // Imported or resolved parameter information
572         //
573         public class ParameterData : IParameterData
574         {
575                 readonly string name;
576                 readonly Parameter.Modifier modifiers;
577
578                 public ParameterData (string name, Parameter.Modifier modifiers)
579                 {
580                         this.name = name;
581                         this.modifiers = modifiers;
582                 }
583
584                 #region IParameterData Members
585
586                 public bool HasExtensionMethodModifier {
587                         get { return (modifiers & Parameter.Modifier.This) != 0; }
588                 }
589
590                 public Parameter.Modifier ModFlags {
591                         get { return modifiers & ~Parameter.Modifier.This; }
592                 }
593
594                 public string Name {
595                         get { return name; }
596                 }
597
598                 #endregion
599         }
600
601         public abstract class AParametersCollection
602         {
603                 protected bool has_arglist;
604                 protected bool has_params;
605
606                 // Null object pattern
607                 protected IParameterData [] parameters;
608                 protected Type [] types;
609
610                 public int Count {
611                         get { return parameters.Length; }
612                 }
613
614                 public Type ExtensionMethodType {
615                         get {
616                                 if (Count == 0)
617                                         return null;
618
619                                 return FixedParameters [0].HasExtensionMethodModifier ?
620                                         types [0] : null;
621                         }
622                 }
623
624                 public IParameterData [] FixedParameters {
625                         get {
626                                 return parameters;
627                         }
628                 }
629
630                 public static ParameterAttributes GetParameterAttribute (Parameter.Modifier modFlags)
631                 {
632                         return (modFlags & Parameter.Modifier.OUT) == Parameter.Modifier.OUT ?
633                                 ParameterAttributes.Out : ParameterAttributes.None;
634                 }
635
636                 public Type [] GetEmitTypes ()
637                 {
638                         Type [] types = null;
639                         if (has_arglist) {
640                                 if (Count == 1)
641                                         return Type.EmptyTypes;
642
643                                 types = new Type [Count - 1];
644                                 Array.Copy (Types, types, types.Length);
645                         }
646
647                         for (int i = 0; i < Count; ++i) {
648                                 if ((FixedParameters [i].ModFlags & Parameter.Modifier.ISBYREF) == 0)
649                                         continue;
650
651                                 if (types == null)
652                                         types = (Type []) Types.Clone ();
653
654                                 types [i] = TypeManager.GetReferenceType (types [i]);
655                         }
656
657                         if (types == null)
658                                 types = Types;
659
660                         return types;
661                 }
662
663                 public string GetSignatureForError ()
664                 {
665                         StringBuilder sb = new StringBuilder ("(");
666                         for (int i = 0; i < Count; ++i) {
667                                 if (i != 0)
668                                         sb.Append (", ");
669                                 sb.Append (ParameterDesc (i));
670                         }
671                         sb.Append (')');
672                         return sb.ToString ();
673                 }
674
675                 public bool HasArglist {
676                         get { return has_arglist; }
677                 }
678
679                 public bool HasExtensionMethodType {
680                         get {
681                                 if (Count == 0)
682                                         return false;
683
684                                 return FixedParameters [0].HasExtensionMethodModifier;
685                         }
686                 }
687
688                 public bool HasParams {
689                         get { return has_params; }
690                 }
691
692                 public bool IsEmpty {
693                         get { return parameters.Length == 0; }
694                 }
695
696                 public string ParameterDesc (int pos)
697                 {
698                         if (types == null || types [pos] == null)
699                                 return ((Parameter)FixedParameters [pos]).GetSignatureForError ();
700
701                         string type = TypeManager.CSharpName (types [pos]);
702                         if (FixedParameters [pos].HasExtensionMethodModifier)
703                                 return "this " + type;
704
705                         Parameter.Modifier mod = FixedParameters [pos].ModFlags & ~Parameter.Modifier.ARGLIST;
706                         if (mod == 0)
707                                 return type;
708
709                         return Parameter.GetModifierSignature (mod) + " " + type;
710                 }
711
712                 public Type[] Types {
713                         get { return types; }
714                         set { types = value; }
715                 }
716
717 #if MS_COMPATIBLE
718                 public AParametersCollection InflateTypes (Type[] genArguments, Type[] argTypes)
719                 {
720                         AParametersCollection p = (AParametersCollection) MemberwiseClone (); // Clone ();
721
722                         for (int i = 0; i < Count; ++i) {
723                                 if (types[i].IsGenericType) {
724                                         Type[] gen_arguments_open = new Type[types[i].GetGenericTypeDefinition ().GetGenericArguments ().Length];
725                                         Type[] gen_arguments = types[i].GetGenericArguments ();
726                                         for (int ii = 0; ii < gen_arguments_open.Length; ++ii) {
727                                                 if (gen_arguments[ii].IsGenericParameter) {
728                                                         Type t = argTypes[gen_arguments[ii].GenericParameterPosition];
729                                                         gen_arguments_open[ii] = t;
730                                                 } else
731                                                         gen_arguments_open[ii] = gen_arguments[ii];
732                                         }
733
734                                         p.types[i] = types[i].GetGenericTypeDefinition ().MakeGenericType (gen_arguments_open);
735                                         continue;
736                                 }
737
738                                 if (types[i].IsGenericParameter) {
739                                         Type gen_argument = argTypes[types[i].GenericParameterPosition];
740                                         p.types[i] = gen_argument;
741                                         continue;
742                                 }
743                         }
744
745                         return p;
746                 }
747 #endif
748         }
749
750         //
751         // A collection of imported or resolved parameters
752         //
753         public class ParametersImported : AParametersCollection
754         {
755                 ParametersImported (AParametersCollection param, Type[] types)
756                 {
757                         this.parameters = param.FixedParameters;
758                         this.types = types;
759                         has_arglist = param.HasArglist;
760                         has_params = param.HasParams;
761                 }
762
763                 ParametersImported (IParameterData [] parameters, Type [] types, MethodBase method, bool hasParams)
764                 {
765                         this.parameters = parameters;
766                         this.types = types;
767                         has_arglist = (method.CallingConvention & CallingConventions.VarArgs) != 0;
768                         if (has_arglist) {
769                                 this.parameters = new IParameterData [parameters.Length + 1];
770                                 parameters.CopyTo (this.parameters, 0);
771                                 this.parameters [parameters.Length] = new ArglistParameter (Location.Null);
772                                 this.types = new Type [types.Length + 1];
773                                 types.CopyTo (this.types, 0);
774                                 this.types [types.Length] = TypeManager.arg_iterator_type;
775                         }
776                         has_params = hasParams;
777                 }
778
779                 public ParametersImported (IParameterData [] param, Type[] types)
780                 {
781                         this.parameters = param;
782                         this.types = types;
783                 }
784
785                 public static AParametersCollection Create (MethodBase method)
786                 {
787                         return Create (method.GetParameters (), method);
788                 }
789
790                 //
791                 // Generic method parameters importer, param is shared between all instances
792                 //
793                 public static AParametersCollection Create (AParametersCollection param, MethodBase method)
794                 {
795                         if (param.IsEmpty)
796                                 return param;
797
798                         ParameterInfo [] pi = method.GetParameters ();
799                         Type [] types = new Type [pi.Length];
800                         for (int i = 0; i < types.Length; i++) {
801                                 Type t = pi [i].ParameterType;
802                                 if (t.IsByRef)
803                                         t = TypeManager.GetElementType (t);
804
805                                 types [i] = TypeManager.TypeToCoreType (t);
806                         }
807
808                         return new ParametersImported (param, types);
809                 }
810
811                 //
812                 // Imports SRE parameters
813                 //
814                 public static AParametersCollection Create (ParameterInfo [] pi, MethodBase method)
815                 {
816                         if (pi.Length == 0) {
817                                 if (method != null && (method.CallingConvention & CallingConventions.VarArgs) != 0)
818                                         return new ParametersImported (new IParameterData [0], Type.EmptyTypes, method, false);
819
820                                 return ParametersCompiled.EmptyReadOnlyParameters;
821                         }
822
823                         Type [] types = new Type [pi.Length];
824                         IParameterData [] par = new IParameterData [pi.Length];
825                         bool is_params = false;
826                         PredefinedAttribute extension_attr = PredefinedAttributes.Get.Extension;
827                         PredefinedAttribute param_attr = PredefinedAttributes.Get.ParamArray;
828                         for (int i = 0; i < types.Length; i++) {
829                                 types [i] = TypeManager.TypeToCoreType (pi [i].ParameterType);
830
831                                 ParameterInfo p = pi [i];
832                                 Parameter.Modifier mod = 0;
833                                 if (types [i].IsByRef) {
834                                         if ((p.Attributes & (ParameterAttributes.Out | ParameterAttributes.In)) == ParameterAttributes.Out)
835                                                 mod = Parameter.Modifier.OUT;
836                                         else
837                                                 mod = Parameter.Modifier.REF;
838
839                                         //
840                                         // Strip reference wrapping
841                                         //
842                                         types [i] = TypeManager.GetElementType (types [i]);
843                                 } else if (i == 0 && extension_attr.IsDefined && method != null && method.IsStatic &&
844                                 (method.DeclaringType.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute &&
845                                         method.IsDefined (extension_attr.Type, false)) {
846                                         mod = Parameter.Modifier.This;
847                                 } else if (i >= pi.Length - 2) {
848                                         if (types[i].IsArray) {
849                                                 if (p.IsDefined (param_attr.Type, false)) {
850                                                         mod = Parameter.Modifier.PARAMS;
851                                                         is_params = true;
852                                                 }
853                                         } else if (types [i] == TypeManager.runtime_argument_handle_type) {
854                                                 par [i] = new ArglistParameter (Location.Null);
855                                                 continue;
856                                         }
857                                 }
858
859                                 par [i] = new ParameterData (p.Name, mod);
860                         }
861
862                         return method != null ?
863                                 new ParametersImported (par, types, method, is_params) :
864                                 new ParametersImported (par, types);
865                 }
866         }
867
868         /// <summary>
869         ///   Represents the methods parameters
870         /// </summary>
871         public class ParametersCompiled : AParametersCollection
872         {
873                 public static readonly ParametersCompiled EmptyReadOnlyParameters = new ParametersCompiled ();
874                 
875                 // Used by C# 2.0 delegates
876                 public static readonly ParametersCompiled Undefined = new ParametersCompiled ();
877
878                 private ParametersCompiled ()
879                 {
880                         parameters = new Parameter [0];
881                         types = Type.EmptyTypes;
882                 }
883
884                 private ParametersCompiled (Parameter [] parameters, Type [] types)
885                 {
886                         this.parameters = parameters;
887                     this.types = types;
888                 }
889                 
890                 public ParametersCompiled (params Parameter[] parameters)
891                 {
892                         if (parameters == null)
893                                 throw new ArgumentException ("Use EmptyReadOnlyParameters");
894
895                         this.parameters = parameters;
896                         int count = parameters.Length;
897
898                         if (count == 0)
899                                 return;
900
901                         if (count == 1) {
902                                 has_params = (parameters [0].ModFlags & Parameter.Modifier.PARAMS) != 0;
903                                 return;
904                         }
905
906                         for (int i = 0; i < count; i++){
907                                 string base_name = parameters [i].Name;
908                                 has_params |= (parameters [i].ModFlags & Parameter.Modifier.PARAMS) != 0;
909
910                                 for (int j = i + 1; j < count; j++){
911                                         if (base_name != parameters [j].Name)
912                                                 continue;
913
914                                         ErrorDuplicateName (parameters [i]);
915                                         i = j;
916                                 }
917                         }
918                 }
919
920                 public ParametersCompiled (Parameter [] parameters, bool has_arglist) :
921                         this (parameters)
922                 {
923                         this.has_arglist = has_arglist;
924                 }
925                 
926                 public static ParametersCompiled CreateFullyResolved (Parameter p, Type type)
927                 {
928                         return new ParametersCompiled (new Parameter [] { p }, new Type [] { type });
929                 }
930                 
931                 public static ParametersCompiled CreateFullyResolved (Parameter[] parameters, Type[] types)
932                 {
933                         return new ParametersCompiled (parameters, types);
934                 }
935
936                 public static ParametersCompiled MergeGenerated (ParametersCompiled userParams, bool checkConflicts, Parameter compilerParams, Type compilerTypes)
937                 {
938                         return MergeGenerated (userParams, checkConflicts,
939                                 new Parameter [] { compilerParams },
940                                 new Type [] { compilerTypes });
941                 }
942
943                 //
944                 // Use this method when you merge compiler generated parameters with user parameters
945                 //
946                 public static ParametersCompiled MergeGenerated (ParametersCompiled userParams, bool checkConflicts, Parameter[] compilerParams, Type[] compilerTypes)
947                 {
948                         Parameter[] all_params = new Parameter [userParams.Count + compilerParams.Length];
949                         userParams.FixedParameters.CopyTo(all_params, 0);
950
951                         Type [] all_types;
952                         if (userParams.types != null) {
953                                 all_types = new Type [all_params.Length];
954                                 userParams.Types.CopyTo (all_types, 0);
955                         } else {
956                                 all_types = null;
957                         }
958
959                         int last_filled = userParams.Count;
960                         int index = 0;
961                         foreach (Parameter p in compilerParams) {
962                                 for (int i = 0; i < last_filled; ++i) {
963                                         while (p.Name == all_params [i].Name) {
964                                                 if (checkConflicts && i < userParams.Count) {
965                                                         Report.Error (316, userParams [i].Location,
966                                                                 "The parameter name `{0}' conflicts with a compiler generated name", p.Name);
967                                                 }
968                                                 p.Name = '_' + p.Name;
969                                         }
970                                 }
971                                 all_params [last_filled] = p;
972                                 if (all_types != null)
973                                         all_types [last_filled] = compilerTypes [index++];
974                                 ++last_filled;
975                         }
976                         
977                         ParametersCompiled parameters = new ParametersCompiled (all_params, all_types);
978                         parameters.has_params = userParams.has_params;
979                         return parameters;
980                 }
981
982                 protected virtual void ErrorDuplicateName (Parameter p)
983                 {
984                         Report.Error (100, p.Location, "The parameter name `{0}' is a duplicate", p.Name);
985                 }
986
987                 /// <summary>
988                 ///    Returns the parameter information based on the name
989                 /// </summary>
990                 public int GetParameterIndexByName (string name)
991                 {
992                         for (int idx = 0; idx < Count; ++idx) {
993                                 if (parameters [idx].Name == name)
994                                         return idx;
995                         }
996
997                         return -1;
998                 }
999
1000                 public bool Resolve (IResolveContext ec)
1001                 {
1002                         if (types != null)
1003                                 return true;
1004                         
1005                         types = new Type [Count];
1006                         
1007                         bool ok = true;
1008                         Parameter p;
1009                         for (int i = 0; i < FixedParameters.Length; ++i) {
1010                                 p = this [i];
1011                                 Type t = p.Resolve (ec);
1012                                 if (t == null) {
1013                                         ok = false;
1014                                         continue;
1015                                 }
1016
1017                                 types [i] = t;
1018                         }
1019
1020                         return ok;
1021                 }
1022
1023                 public void ResolveVariable ()
1024                 {
1025                         for (int i = 0; i < FixedParameters.Length; ++i) {
1026                                 this [i].ResolveVariable (i);
1027                         }
1028                 }
1029
1030                 public CallingConventions CallingConvention
1031                 {
1032                         get {
1033                                 if (HasArglist)
1034                                         return CallingConventions.VarArgs;
1035                                 else
1036                                         return CallingConventions.Standard;
1037                         }
1038                 }
1039
1040                 // Define each type attribute (in/out/ref) and
1041                 // the argument names.
1042                 public void ApplyAttributes (MethodBase builder)
1043                 {
1044                         if (Count == 0)
1045                                 return;
1046
1047                         MethodBuilder mb = builder as MethodBuilder;
1048                         ConstructorBuilder cb = builder as ConstructorBuilder;
1049
1050                         for (int i = 0; i < Count; i++) {
1051                                 this [i].ApplyAttributes (mb, cb, i + 1);
1052                         }
1053                 }
1054
1055                 public void VerifyClsCompliance ()
1056                 {
1057                         foreach (Parameter p in FixedParameters)
1058                                 p.IsClsCompliant ();
1059                 }
1060
1061                 public Parameter this [int pos] {
1062                         get { return (Parameter) parameters [pos]; }
1063                 }
1064
1065                 public Expression CreateExpressionTree (EmitContext ec, Location loc)
1066                 {
1067                         ArrayList initializers = new ArrayList (Count);
1068                         foreach (Parameter p in FixedParameters) {
1069                                 //
1070                                 // Each parameter expression is stored to local variable
1071                                 // to save some memory when referenced later.
1072                                 //
1073                                 StatementExpression se = new StatementExpression (p.CreateExpressionTreeVariable (ec));
1074                                 if (se.Resolve (ec))
1075                                         ec.CurrentBlock.AddScopeStatement (se);
1076                                 
1077                                 initializers.Add (p.ExpressionTreeVariableReference ());
1078                         }
1079
1080                         return new ArrayCreation (
1081                                 Parameter.ResolveParameterExpressionType (ec, loc),
1082                                 "[]", initializers, loc);
1083                 }
1084
1085                 public ParametersCompiled Clone ()
1086                 {
1087                         ParametersCompiled p = (ParametersCompiled) MemberwiseClone ();
1088
1089                         p.parameters = new IParameterData [parameters.Length];
1090                         for (int i = 0; i < Count; ++i)
1091                                 p.parameters [i] = this [i].Clone ();
1092
1093                         return p;
1094                 }
1095         }
1096 }