0a0bde756a2f62ef82e3309da6798cff8282281e
[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                         TypeParameter tp = TypeManager.LookupTypeParameter (parameter_type);
365                         if (tp != null) {
366                                 if ((modFlags & Parameter.Modifier.ISBYREF) != 0) {
367                                         if (tp.Variance != Variance.None)
368                                                 tp.ErrorInvalidVariance ((MemberCore) ec, Variance.None);
369                                 } else if (tp.Variance == Variance.Covariant) {
370                                         tp.ErrorInvalidVariance ((MemberCore) ec, Variance.Contravariant);
371                                 }
372                         } else {
373                                 if ((parameter_type.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
374                                         Report.Error (721, Location, "`{0}': static types cannot be used as parameters",
375                                                 texpr.GetSignatureForError ());
376                                         return parameter_type;
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                                 }
383                         }
384
385                         return parameter_type;
386                 }
387
388                 public void ResolveVariable (int idx)
389                 {
390                         this.idx = idx;
391                 }
392
393                 public bool HasExtensionMethodModifier {
394                         get { return (modFlags & Modifier.This) != 0; }
395                 }
396
397                 public Modifier ModFlags {
398                         get { return modFlags & ~Modifier.This; }
399                 }
400
401                 public string Name {
402                         get { return name; }
403                         set { name = value; }
404                 }
405
406                 ParameterAttributes Attributes {
407                         get { return ParametersCompiled.GetParameterAttribute (modFlags); }
408                 }
409
410                 public override AttributeTargets AttributeTargets {
411                         get {
412                                 return AttributeTargets.Parameter;
413                         }
414                 }
415
416                 public virtual string GetSignatureForError ()
417                 {
418                         string type_name;
419                         if (parameter_type != null)
420                                 type_name = TypeManager.CSharpName (parameter_type);
421                         else
422                                 type_name = TypeName.GetSignatureForError ();
423
424                         string mod = GetModifierSignature (modFlags);
425                         if (mod.Length > 0)
426                                 return String.Concat (mod, " ", type_name);
427
428                         return type_name;
429                 }
430
431                 public static string GetModifierSignature (Modifier mod)
432                 {
433                         switch (mod) {
434                         case Modifier.OUT:
435                                 return "out";
436                         case Modifier.PARAMS:
437                                 return "params";
438                         case Modifier.REF:
439                                 return "ref";
440                         case Modifier.This:
441                                 return "this";
442                         default:
443                                 return "";
444                         }
445                 }
446
447                 public void IsClsCompliant ()
448                 {
449                         if (AttributeTester.IsClsCompliant (parameter_type))
450                                 return;
451
452                         Report.Warning (3001, 1, Location, "Argument type `{0}' is not CLS-compliant", GetSignatureForError ());
453                 }
454
455                 public virtual void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index)
456                 {
457                         if (mb == null)
458                                 builder = cb.DefineParameter (index, Attributes, Name);
459                         else
460                                 builder = mb.DefineParameter (index, Attributes, Name);
461
462                         if (OptAttributes != null)
463                                 OptAttributes.Emit ();
464                 }
465
466                 public override string[] ValidAttributeTargets {
467                         get {
468                                 return attribute_targets;
469                         }
470                 }
471
472                 public Parameter Clone ()
473                 {
474                         Parameter p = (Parameter) MemberwiseClone ();
475                         if (attributes != null) {
476                                 p.attributes = attributes.Clone ();
477                                 p.attributes.AttachTo (p);
478                         }
479
480                         return p;
481                 }
482
483                 public ExpressionStatement CreateExpressionTreeVariable (EmitContext ec)
484                 {
485                         //
486                         // A parameter is not hoisted when used directly as ET
487                         //
488                         HoistedVariableReference = null;
489
490                         if ((modFlags & Modifier.ISBYREF) != 0)
491                                 Report.Error (1951, Location, "An expression tree parameter cannot use `ref' or `out' modifier");
492
493                         LocalInfo variable = ec.CurrentBlock.AddTemporaryVariable (
494                                 ResolveParameterExpressionType (ec, Location), Location);
495                         variable.Resolve (ec);
496
497                         expr_tree_variable = new LocalVariableReference (
498                                 ec.CurrentBlock, variable.Name, Location, variable, false);
499
500                         ArrayList arguments = new ArrayList (2);
501                         arguments.Add (new Argument (new TypeOf (
502                                 new TypeExpression (parameter_type, Location), Location)));
503                         arguments.Add (new Argument (new StringConstant (Name, Location)));
504                         return new SimpleAssign (ExpressionTreeVariableReference (),
505                                 Expression.CreateExpressionFactoryCall ("Parameter", null, arguments, Location));
506                 }
507
508                 public void Emit (EmitContext ec)
509                 {
510                         int arg_idx = idx;
511                         if (!ec.IsStatic)
512                                 arg_idx++;
513
514                         ParameterReference.EmitLdArg (ec.ig, arg_idx);
515                 }
516
517                 public void EmitAssign (EmitContext ec)
518                 {
519                         int arg_idx = idx;
520                         if (!ec.IsStatic)
521                                 arg_idx++;
522
523                         if (arg_idx <= 255)
524                                 ec.ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
525                         else
526                                 ec.ig.Emit (OpCodes.Starg, arg_idx);
527                 }
528
529                 public void EmitAddressOf (EmitContext ec)
530                 {
531                         int arg_idx = idx;
532
533                         if (!ec.IsStatic)
534                                 arg_idx++;
535
536                         bool is_ref = (ModFlags & Modifier.ISBYREF) != 0;
537                         if (is_ref) {
538                                 ParameterReference.EmitLdArg (ec.ig, arg_idx);
539                         } else {
540                                 if (arg_idx <= 255)
541                                         ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
542                                 else
543                                         ec.ig.Emit (OpCodes.Ldarga, arg_idx);
544                         }
545                 }
546
547                 public Expression ExpressionTreeVariableReference ()
548                 {
549                         return expr_tree_variable;
550                 }
551
552                 //
553                 // System.Linq.Expressions.ParameterExpression type
554                 //
555                 public static TypeExpr ResolveParameterExpressionType (EmitContext ec, Location location)
556                 {
557                         if (parameter_expr_tree_type != null)
558                                 return parameter_expr_tree_type;
559
560                         Type p_type = TypeManager.parameter_expression_type;
561                         if (p_type == null) {
562                                 p_type = TypeManager.CoreLookupType ("System.Linq.Expressions", "ParameterExpression", Kind.Class, true);
563                                 TypeManager.parameter_expression_type = p_type;
564                         }
565
566                         parameter_expr_tree_type = new TypeExpression (p_type, location).
567                                 ResolveAsTypeTerminal (ec, false);
568
569                         return parameter_expr_tree_type;
570                 }
571         }
572
573         //
574         // Imported or resolved parameter information
575         //
576         public class ParameterData : IParameterData
577         {
578                 readonly string name;
579                 readonly Parameter.Modifier modifiers;
580
581                 public ParameterData (string name, Parameter.Modifier modifiers)
582                 {
583                         this.name = name;
584                         this.modifiers = modifiers;
585                 }
586
587                 #region IParameterData Members
588
589                 public bool HasExtensionMethodModifier {
590                         get { return (modifiers & Parameter.Modifier.This) != 0; }
591                 }
592
593                 public Parameter.Modifier ModFlags {
594                         get { return modifiers & ~Parameter.Modifier.This; }
595                 }
596
597                 public string Name {
598                         get { return name; }
599                 }
600
601                 #endregion
602         }
603
604         public abstract class AParametersCollection
605         {
606                 protected bool has_arglist;
607                 protected bool has_params;
608
609                 // Null object pattern
610                 protected IParameterData [] parameters;
611                 protected Type [] types;
612
613                 public int Count {
614                         get { return parameters.Length; }
615                 }
616
617                 public Type ExtensionMethodType {
618                         get {
619                                 if (Count == 0)
620                                         return null;
621
622                                 return FixedParameters [0].HasExtensionMethodModifier ?
623                                         types [0] : null;
624                         }
625                 }
626
627                 public IParameterData [] FixedParameters {
628                         get {
629                                 return parameters;
630                         }
631                 }
632
633                 public static ParameterAttributes GetParameterAttribute (Parameter.Modifier modFlags)
634                 {
635                         return (modFlags & Parameter.Modifier.OUT) == Parameter.Modifier.OUT ?
636                                 ParameterAttributes.Out : ParameterAttributes.None;
637                 }
638
639                 public Type [] GetEmitTypes ()
640                 {
641                         Type [] types = null;
642                         if (has_arglist) {
643                                 if (Count == 1)
644                                         return Type.EmptyTypes;
645
646                                 types = new Type [Count - 1];
647                                 Array.Copy (Types, types, types.Length);
648                         }
649
650                         for (int i = 0; i < Count; ++i) {
651                                 if ((FixedParameters [i].ModFlags & Parameter.Modifier.ISBYREF) == 0)
652                                         continue;
653
654                                 if (types == null)
655                                         types = (Type []) Types.Clone ();
656
657                                 types [i] = TypeManager.GetReferenceType (types [i]);
658                         }
659
660                         if (types == null)
661                                 types = Types;
662
663                         return types;
664                 }
665
666                 public string GetSignatureForError ()
667                 {
668                         StringBuilder sb = new StringBuilder ("(");
669                         for (int i = 0; i < Count; ++i) {
670                                 if (i != 0)
671                                         sb.Append (", ");
672                                 sb.Append (ParameterDesc (i));
673                         }
674                         sb.Append (')');
675                         return sb.ToString ();
676                 }
677
678                 public bool HasArglist {
679                         get { return has_arglist; }
680                 }
681
682                 public bool HasExtensionMethodType {
683                         get {
684                                 if (Count == 0)
685                                         return false;
686
687                                 return FixedParameters [0].HasExtensionMethodModifier;
688                         }
689                 }
690
691                 public bool HasParams {
692                         get { return has_params; }
693                 }
694
695                 public bool IsEmpty {
696                         get { return parameters.Length == 0; }
697                 }
698
699                 public string ParameterDesc (int pos)
700                 {
701                         if (types == null || types [pos] == null)
702                                 return ((Parameter)FixedParameters [pos]).GetSignatureForError ();
703
704                         string type = TypeManager.CSharpName (types [pos]);
705                         if (FixedParameters [pos].HasExtensionMethodModifier)
706                                 return "this " + type;
707
708                         Parameter.Modifier mod = FixedParameters [pos].ModFlags & ~Parameter.Modifier.ARGLIST;
709                         if (mod == 0)
710                                 return type;
711
712                         return Parameter.GetModifierSignature (mod) + " " + type;
713                 }
714
715                 public Type[] Types {
716                         get { return types; }
717                         set { types = value; }
718                 }
719
720 #if MS_COMPATIBLE
721                 public AParametersCollection InflateTypes (Type[] genArguments, Type[] argTypes)
722                 {
723                         AParametersCollection p = (AParametersCollection) MemberwiseClone (); // Clone ();
724
725                         for (int i = 0; i < Count; ++i) {
726                                 if (types[i].IsGenericType) {
727                                         Type[] gen_arguments_open = new Type[types[i].GetGenericTypeDefinition ().GetGenericArguments ().Length];
728                                         Type[] gen_arguments = types[i].GetGenericArguments ();
729                                         for (int ii = 0; ii < gen_arguments_open.Length; ++ii) {
730                                                 if (gen_arguments[ii].IsGenericParameter) {
731                                                         Type t = argTypes[gen_arguments[ii].GenericParameterPosition];
732                                                         gen_arguments_open[ii] = t;
733                                                 } else
734                                                         gen_arguments_open[ii] = gen_arguments[ii];
735                                         }
736
737                                         p.types[i] = types[i].GetGenericTypeDefinition ().MakeGenericType (gen_arguments_open);
738                                         continue;
739                                 }
740
741                                 if (types[i].IsGenericParameter) {
742                                         Type gen_argument = argTypes[types[i].GenericParameterPosition];
743                                         p.types[i] = gen_argument;
744                                         continue;
745                                 }
746                         }
747
748                         return p;
749                 }
750 #endif
751         }
752
753         //
754         // A collection of imported or resolved parameters
755         //
756         public class ParametersImported : AParametersCollection
757         {
758                 ParametersImported (AParametersCollection param, Type[] types)
759                 {
760                         this.parameters = param.FixedParameters;
761                         this.types = types;
762                         has_arglist = param.HasArglist;
763                         has_params = param.HasParams;
764                 }
765
766                 ParametersImported (IParameterData [] parameters, Type [] types, MethodBase method, bool hasParams)
767                 {
768                         this.parameters = parameters;
769                         this.types = types;
770                         has_arglist = (method.CallingConvention & CallingConventions.VarArgs) != 0;
771                         if (has_arglist) {
772                                 this.parameters = new IParameterData [parameters.Length + 1];
773                                 parameters.CopyTo (this.parameters, 0);
774                                 this.parameters [parameters.Length] = new ArglistParameter (Location.Null);
775                                 this.types = new Type [types.Length + 1];
776                                 types.CopyTo (this.types, 0);
777                                 this.types [types.Length] = TypeManager.arg_iterator_type;
778                         }
779                         has_params = hasParams;
780                 }
781
782                 public ParametersImported (IParameterData [] param, Type[] types)
783                 {
784                         this.parameters = param;
785                         this.types = types;
786                 }
787
788                 public static AParametersCollection Create (MethodBase method)
789                 {
790                         return Create (method.GetParameters (), method);
791                 }
792
793                 //
794                 // Generic method parameters importer, param is shared between all instances
795                 //
796                 public static AParametersCollection Create (AParametersCollection param, MethodBase method)
797                 {
798                         if (param.IsEmpty)
799                                 return param;
800
801                         ParameterInfo [] pi = method.GetParameters ();
802                         Type [] types = new Type [pi.Length];
803                         for (int i = 0; i < types.Length; i++) {
804                                 Type t = pi [i].ParameterType;
805                                 if (t.IsByRef)
806                                         t = TypeManager.GetElementType (t);
807
808                                 types [i] = TypeManager.TypeToCoreType (t);
809                         }
810
811                         return new ParametersImported (param, types);
812                 }
813
814                 //
815                 // Imports SRE parameters
816                 //
817                 public static AParametersCollection Create (ParameterInfo [] pi, MethodBase method)
818                 {
819                         if (pi.Length == 0) {
820                                 if (method != null && (method.CallingConvention & CallingConventions.VarArgs) != 0)
821                                         return new ParametersImported (new IParameterData [0], Type.EmptyTypes, method, false);
822
823                                 return ParametersCompiled.EmptyReadOnlyParameters;
824                         }
825
826                         Type [] types = new Type [pi.Length];
827                         IParameterData [] par = new IParameterData [pi.Length];
828                         bool is_params = false;
829                         PredefinedAttribute extension_attr = PredefinedAttributes.Get.Extension;
830                         PredefinedAttribute param_attr = PredefinedAttributes.Get.ParamArray;
831                         for (int i = 0; i < types.Length; i++) {
832                                 types [i] = TypeManager.TypeToCoreType (pi [i].ParameterType);
833
834                                 ParameterInfo p = pi [i];
835                                 Parameter.Modifier mod = 0;
836                                 if (types [i].IsByRef) {
837                                         if ((p.Attributes & (ParameterAttributes.Out | ParameterAttributes.In)) == ParameterAttributes.Out)
838                                                 mod = Parameter.Modifier.OUT;
839                                         else
840                                                 mod = Parameter.Modifier.REF;
841
842                                         //
843                                         // Strip reference wrapping
844                                         //
845                                         types [i] = TypeManager.GetElementType (types [i]);
846                                 } else if (i == 0 && extension_attr.IsDefined && method != null && method.IsStatic &&
847                                 (method.DeclaringType.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute &&
848                                         method.IsDefined (extension_attr.Type, false)) {
849                                         mod = Parameter.Modifier.This;
850                                 } else if (i >= pi.Length - 2 && types [i].IsArray) {
851                                         if (p.IsDefined (param_attr.Type, false)) {
852                                                 mod = Parameter.Modifier.PARAMS;
853                                                 is_params = true;
854                                         }
855                                 }
856
857                                 par [i] = new ParameterData (p.Name, mod);
858                         }
859
860                         return method != null ?
861                                 new ParametersImported (par, types, method, is_params) :
862                                 new ParametersImported (par, types);
863                 }
864         }
865
866         /// <summary>
867         ///   Represents the methods parameters
868         /// </summary>
869         public class ParametersCompiled : AParametersCollection
870         {
871                 public static readonly ParametersCompiled EmptyReadOnlyParameters = new ParametersCompiled ();
872                 
873                 // Used by C# 2.0 delegates
874                 public static readonly ParametersCompiled Undefined = new ParametersCompiled ();
875
876                 private ParametersCompiled ()
877                 {
878                         parameters = new Parameter [0];
879                         types = Type.EmptyTypes;
880                 }
881
882                 private ParametersCompiled (Parameter [] parameters, Type [] types)
883                 {
884                         this.parameters = parameters;
885                     this.types = types;
886                 }
887                 
888                 public ParametersCompiled (params Parameter[] parameters)
889                 {
890                         if (parameters == null)
891                                 throw new ArgumentException ("Use EmptyReadOnlyParameters");
892
893                         this.parameters = parameters;
894                         int count = parameters.Length;
895
896                         if (count == 0)
897                                 return;
898
899                         if (count == 1) {
900                                 has_params = (parameters [0].ModFlags & Parameter.Modifier.PARAMS) != 0;
901                                 return;
902                         }
903
904                         for (int i = 0; i < count; i++){
905                                 string base_name = parameters [i].Name;
906                                 has_params |= (parameters [i].ModFlags & Parameter.Modifier.PARAMS) != 0;
907
908                                 for (int j = i + 1; j < count; j++){
909                                         if (base_name != parameters [j].Name)
910                                                 continue;
911
912                                         ErrorDuplicateName (parameters [i]);
913                                         i = j;
914                                 }
915                         }
916                 }
917
918                 public ParametersCompiled (Parameter [] parameters, bool has_arglist) :
919                         this (parameters)
920                 {
921                         this.has_arglist = has_arglist;
922                 }
923                 
924                 public static ParametersCompiled CreateFullyResolved (Parameter p, Type type)
925                 {
926                         return new ParametersCompiled (new Parameter [] { p }, new Type [] { type });
927                 }
928                 
929                 public static ParametersCompiled CreateFullyResolved (Parameter[] parameters, Type[] types)
930                 {
931                         return new ParametersCompiled (parameters, types);
932                 }
933
934                 public static ParametersCompiled MergeGenerated (ParametersCompiled userParams, bool checkConflicts, Parameter compilerParams, Type compilerTypes)
935                 {
936                         return MergeGenerated (userParams, checkConflicts,
937                                 new Parameter [] { compilerParams },
938                                 new Type [] { compilerTypes });
939                 }
940
941                 //
942                 // Use this method when you merge compiler generated parameters with user parameters
943                 //
944                 public static ParametersCompiled MergeGenerated (ParametersCompiled userParams, bool checkConflicts, Parameter[] compilerParams, Type[] compilerTypes)
945                 {
946                         Parameter[] all_params = new Parameter [userParams.Count + compilerParams.Length];
947                         userParams.FixedParameters.CopyTo(all_params, 0);
948
949                         Type [] all_types;
950                         if (userParams.types != null) {
951                                 all_types = new Type [all_params.Length];
952                                 userParams.Types.CopyTo (all_types, 0);
953                         } else {
954                                 all_types = null;
955                         }
956
957                         int last_filled = userParams.Count;
958                         int index = 0;
959                         foreach (Parameter p in compilerParams) {
960                                 for (int i = 0; i < last_filled; ++i) {
961                                         while (p.Name == all_params [i].Name) {
962                                                 if (checkConflicts && i < userParams.Count) {
963                                                         Report.Error (316, userParams [i].Location,
964                                                                 "The parameter name `{0}' conflicts with a compiler generated name", p.Name);
965                                                 }
966                                                 p.Name = '_' + p.Name;
967                                         }
968                                 }
969                                 all_params [last_filled] = p;
970                                 if (all_types != null)
971                                         all_types [last_filled] = compilerTypes [index++];
972                                 ++last_filled;
973                         }
974                         
975                         ParametersCompiled parameters = new ParametersCompiled (all_params, all_types);
976                         parameters.has_params = userParams.has_params;
977                         return parameters;
978                 }
979
980                 protected virtual void ErrorDuplicateName (Parameter p)
981                 {
982                         Report.Error (100, p.Location, "The parameter name `{0}' is a duplicate", p.Name);
983                 }
984
985                 /// <summary>
986                 ///    Returns the parameter information based on the name
987                 /// </summary>
988                 public int GetParameterIndexByName (string name)
989                 {
990                         for (int idx = 0; idx < Count; ++idx) {
991                                 if (parameters [idx].Name == name)
992                                         return idx;
993                         }
994
995                         return -1;
996                 }
997
998                 public bool Resolve (IResolveContext ec)
999                 {
1000                         if (types != null)
1001                                 return true;
1002                         
1003                         types = new Type [Count];
1004                         
1005                         bool ok = true;
1006                         Parameter p;
1007                         for (int i = 0; i < FixedParameters.Length; ++i) {
1008                                 p = this [i];
1009                                 Type t = p.Resolve (ec);
1010                                 if (t == null) {
1011                                         ok = false;
1012                                         continue;
1013                                 }
1014
1015                                 types [i] = t;
1016                         }
1017
1018                         return ok;
1019                 }
1020
1021                 public void ResolveVariable ()
1022                 {
1023                         for (int i = 0; i < FixedParameters.Length; ++i) {
1024                                 this [i].ResolveVariable (i);
1025                         }
1026                 }
1027
1028                 public CallingConventions CallingConvention
1029                 {
1030                         get {
1031                                 if (HasArglist)
1032                                         return CallingConventions.VarArgs;
1033                                 else
1034                                         return CallingConventions.Standard;
1035                         }
1036                 }
1037
1038                 // Define each type attribute (in/out/ref) and
1039                 // the argument names.
1040                 public void ApplyAttributes (MethodBase builder)
1041                 {
1042                         if (Count == 0)
1043                                 return;
1044
1045                         MethodBuilder mb = builder as MethodBuilder;
1046                         ConstructorBuilder cb = builder as ConstructorBuilder;
1047
1048                         for (int i = 0; i < Count; i++) {
1049                                 this [i].ApplyAttributes (mb, cb, i + 1);
1050                         }
1051                 }
1052
1053                 public void VerifyClsCompliance ()
1054                 {
1055                         foreach (Parameter p in FixedParameters)
1056                                 p.IsClsCompliant ();
1057                 }
1058
1059                 public Parameter this [int pos] {
1060                         get { return (Parameter) parameters [pos]; }
1061                 }
1062
1063                 public Expression CreateExpressionTree (EmitContext ec, Location loc)
1064                 {
1065                         ArrayList initializers = new ArrayList (Count);
1066                         foreach (Parameter p in FixedParameters) {
1067                                 //
1068                                 // Each parameter expression is stored to local variable
1069                                 // to save some memory when referenced later.
1070                                 //
1071                                 StatementExpression se = new StatementExpression (p.CreateExpressionTreeVariable (ec));
1072                                 if (se.Resolve (ec))
1073                                         ec.CurrentBlock.AddScopeStatement (se);
1074                                 
1075                                 initializers.Add (p.ExpressionTreeVariableReference ());
1076                         }
1077
1078                         return new ArrayCreation (
1079                                 Parameter.ResolveParameterExpressionType (ec, loc),
1080                                 "[]", initializers, loc);
1081                 }
1082
1083                 public ParametersCompiled Clone ()
1084                 {
1085                         ParametersCompiled p = (ParametersCompiled) MemberwiseClone ();
1086
1087                         p.parameters = new IParameterData [parameters.Length];
1088                         for (int i = 0; i < Count; ++i)
1089                                 p.parameters [i] = this [i].Clone ();
1090
1091                         return p;
1092                 }
1093         }
1094 }