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