2008-06-11 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / mcs / parameter.cs
1 //
2 // parameter.cs: Parameter definition.
3 //
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 //         Marek Safar (marek.safar@seznam.cz)
6 //
7 // Dual licensed under the terms of the MIT X11 or GNU GPL
8 //
9 // Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
10 // Copyright 2003-2008 Novell, Inc. 
11 //
12 //
13 using System;
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Collections;
17 using System.Text;
18
19 namespace Mono.CSharp {
20
21         /// <summary>
22         ///   Abstract Base class for parameters of a method.
23         /// </summary>
24         public abstract class ParameterBase : Attributable {
25
26                 protected ParameterBuilder builder;
27
28                 protected ParameterBase (Attributes attrs)
29                         : base (attrs)
30                 {
31                 }
32
33                 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
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                 class ParameterVariable : Variable
240                 {
241                         readonly Parameter Parameter;
242                         readonly int Idx;
243
244                         public ParameterVariable (Parameter par, int idx)
245                         {
246                                 this.Parameter = par;
247                                 this.Idx = idx;
248                         }
249
250                         public override Type Type
251                         {
252                                 get { return Parameter.ParameterType; }
253                         }
254
255                         public override bool HasInstance
256                         {
257                                 get { return false; }
258                         }
259
260                         public override bool NeedsTemporary
261                         {
262                                 get { return false; }
263                         }
264
265                         public override void EmitInstance (EmitContext ec)
266                         {
267                         }
268
269                         public override void Emit (EmitContext ec)
270                         {
271                                 int arg_idx = Idx;
272                                 if (!ec.MethodIsStatic)
273                                         arg_idx++;
274
275                                 ParameterReference.EmitLdArg (ec.ig, arg_idx);
276                         }
277
278                         public override void EmitAssign (EmitContext ec)
279                         {
280                                 int arg_idx = Idx;
281                                 if (!ec.MethodIsStatic)
282                                         arg_idx++;
283
284                                 if (arg_idx <= 255)
285                                         ec.ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
286                                 else
287                                         ec.ig.Emit (OpCodes.Starg, arg_idx);
288                         }
289
290                         public override void EmitAddressOf (EmitContext ec)
291                         {
292                                 int arg_idx = Idx;
293
294                                 if (!ec.MethodIsStatic)
295                                         arg_idx++;
296
297                                 bool is_ref = (Parameter.ModFlags & Parameter.Modifier.ISBYREF) != 0;
298                                 if (is_ref) {
299                                         if (arg_idx <= 255)
300                                                 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
301                                         else
302                                                 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
303                                 } else {
304                                         if (arg_idx <= 255)
305                                                 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
306                                         else
307                                                 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
308                                 }
309                         }
310                 }
311
312                 static string[] attribute_targets = new string [] { "param" };
313
314                 FullNamedExpression TypeName;
315                 readonly Modifier modFlags;
316                 public string Name;
317                 public bool IsCaptured;
318                 protected Type parameter_type;
319                 public readonly Location Location;
320
321                 IResolveContext resolve_context;
322                 LocalVariableReference expr_tree_variable;
323                 static TypeExpr parameter_expr_tree_type;
324
325                 Variable var;
326                 public Variable Variable {
327                         get { return var; }
328                 }
329
330 #if GMCS_SOURCE
331                 public bool IsTypeParameter;
332 #else
333                 public bool IsTypeParameter {
334                         get {
335                                 return false;
336                         }
337                         set {
338                                 if (value)
339                                         throw new Exception ("You can not se TypeParameter in MCS");
340                         }
341                 }
342 #endif
343                 
344                 public Parameter (FullNamedExpression type, string name, Modifier mod, Attributes attrs, Location loc)
345                         : this (type.Type, name, mod, attrs, loc)
346                 {
347                         if (type == TypeManager.system_void_expr)
348                                 Report.Error (1536, loc, "Invalid parameter type `void'");
349                         
350                         TypeName = type;
351                 }
352
353                 public Parameter (Type type, string name, Modifier mod, Attributes attrs, Location loc)
354                         : base (attrs)
355                 {
356                         Name = name;
357                         modFlags = mod;
358                         parameter_type = type;
359                         Location = loc;
360                 }
361
362                 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
363                 {
364                         if (a.Type == TypeManager.in_attribute_type && ModFlags == Modifier.OUT) {
365                                 Report.Error (36, a.Location, "An out parameter cannot have the `In' attribute");
366                                 return;
367                         }
368
369                         if (a.Type == TypeManager.param_array_type) {
370                                 Report.Error (674, a.Location, "Do not use `System.ParamArrayAttribute'. Use the `params' keyword instead");
371                                 return;
372                         }
373
374                         if (a.Type == TypeManager.out_attribute_type && (ModFlags & Modifier.REF) == Modifier.REF &&
375                             TypeManager.in_attribute_type != null && !OptAttributes.Contains (TypeManager.in_attribute_type)) {
376                                 Report.Error (662, a.Location,
377                                         "Cannot specify only `Out' attribute on a ref parameter. Use both `In' and `Out' attributes or neither");
378                                 return;
379                         }
380
381                         if (a.Type == TypeManager.cls_compliant_attribute_type) {
382                                 Report.Warning (3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
383                         }
384
385                         // TypeManager.default_parameter_value_attribute_type is null if !NET_2_0, or if System.dll is not referenced
386                         if (a.Type == TypeManager.default_parameter_value_attribute_type) {
387                                 object val = a.GetParameterDefaultValue ();
388                                 if (val != null) {
389                                         Type t = val.GetType ();
390                                         if (t.IsArray || TypeManager.IsSubclassOf (t, TypeManager.type_type)) {
391                                                 if (parameter_type == TypeManager.object_type) {
392                                                         if (!t.IsArray)
393                                                                 t = TypeManager.type_type;
394
395                                                         Report.Error (1910, a.Location, "Argument of type `{0}' is not applicable for the DefaultValue attribute",
396                                                                 TypeManager.CSharpName (t));
397                                                 } else {
398                                                         Report.Error (1909, a.Location, "The DefaultValue attribute is not applicable on parameters of type `{0}'",
399                                                                 TypeManager.CSharpName (parameter_type)); ;
400                                                 }
401                                                 return;
402                                         }
403                                 }
404
405                                 if (parameter_type == TypeManager.object_type ||
406                                     (val == null && !TypeManager.IsValueType (parameter_type)) ||
407                                     (val != null && TypeManager.TypeToCoreType (val.GetType ()) == parameter_type))
408                                         builder.SetConstant (val);
409                                 else
410                                         Report.Error (1908, a.Location, "The type of the default value should match the type of the parameter");
411                                 return;
412                         }
413
414                         base.ApplyAttributeBuilder (a, cb);
415                 }
416                 
417                 public virtual bool CheckAccessibility (InterfaceMemberBase member)
418                 {
419                         if (IsTypeParameter)
420                                 return true;
421
422                         return member.IsAccessibleAs (parameter_type);
423                 }
424
425                 public override IResolveContext ResolveContext {
426                         get {
427                                 return resolve_context;
428                         }
429                 }
430
431                 // <summary>
432                 //   Resolve is used in method definitions
433                 // </summary>
434                 public virtual bool Resolve (IResolveContext ec)
435                 {
436                         // HACK: to resolve attributes correctly
437                         this.resolve_context = ec;
438
439                         if (parameter_type != null)
440                                 return true;
441
442                         TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec, false);
443                         if (texpr == null)
444                                 return false;
445
446                         parameter_type = texpr.Type;
447
448 #if GMCS_SOURCE
449                         TypeParameterExpr tparam = texpr as TypeParameterExpr;
450                         if (tparam != null) {
451                                 IsTypeParameter = true;
452                                 return true;
453                         }
454 #endif
455
456                         if ((parameter_type.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
457                                 Report.Error (721, Location, "`{0}': static types cannot be used as parameters", 
458                                         texpr.GetSignatureForError ());
459                                 return false;
460                         }
461
462                         if ((modFlags & Parameter.Modifier.ISBYREF) != 0){
463                                 if (parameter_type == TypeManager.typed_reference_type ||
464                                     parameter_type == TypeManager.arg_iterator_type){
465                                         Report.Error (1601, Location, "Method or delegate parameter cannot be of type `{0}'",
466                                                 GetSignatureForError ());
467                                         return false;
468                                 }
469                         }
470
471                         if ((modFlags & Modifier.This) != 0 && parameter_type.IsPointer) {
472                                 Report.Error (1103, Location, "The type of extension method cannot be `{0}'",
473                                         TypeManager.CSharpName (parameter_type));
474                                 return false;
475                         }
476                         
477                         return true;
478                 }
479
480                 public void ResolveVariable (ToplevelBlock toplevel, int idx)
481                 {
482                         if (toplevel.RootScope != null)
483                                 var = toplevel.RootScope.GetCapturedParameter (this);
484                         if (var == null)
485                                 var = new ParameterVariable (this, idx);
486                 }
487
488                 public Type ExternalType ()
489                 {
490                         if ((modFlags & Parameter.Modifier.ISBYREF) != 0)
491                                 return TypeManager.GetReferenceType (parameter_type);
492                         
493                         return parameter_type;
494                 }
495
496                 public bool HasExtensionMethodModifier {
497                         get { return (modFlags & Modifier.This) != 0; }
498                 }
499
500                 public Modifier ModFlags {
501                         get { return modFlags & ~Modifier.This; }
502                 }
503
504                 public Type ParameterType {
505                         get {
506                                 return parameter_type;
507                         }
508                         set {
509                                 parameter_type = value;
510                                 IsTypeParameter = false;
511                         }
512                 }
513
514                 ParameterAttributes Attributes {
515                         get {
516                                 return (modFlags & Modifier.OUT) == Modifier.OUT ?
517                                         ParameterAttributes.Out : ParameterAttributes.None;
518                         }
519                 }
520
521                 // TODO: should be removed !!!!!!!
522                 public static ParameterAttributes GetParameterAttributes (Modifier mod)
523                 {
524                         int flags = ((int) mod) & ~((int) Parameter.Modifier.ISBYREF);
525                         switch ((Modifier) flags) {
526                         case Modifier.NONE:
527                                 return ParameterAttributes.None;
528                         case Modifier.REF:
529                                 return ParameterAttributes.None;
530                         case Modifier.OUT:
531                                 return ParameterAttributes.Out;
532                         case Modifier.PARAMS:
533                                 return 0;
534                         }
535                                 
536                         return ParameterAttributes.None;
537                 }
538                 
539                 public override AttributeTargets AttributeTargets {
540                         get {
541                                 return AttributeTargets.Parameter;
542                         }
543                 }
544
545                 public virtual string GetSignatureForError ()
546                 {
547                         string type_name;
548                         if (parameter_type != null)
549                                 type_name = TypeManager.CSharpName (parameter_type);
550                         else
551                                 type_name = TypeName.GetSignatureForError ();
552
553                         string mod = GetModifierSignature (modFlags);
554                         if (mod.Length > 0)
555                                 return String.Concat (mod, ' ', type_name);
556
557                         return type_name;
558                 }
559
560                 public static string GetModifierSignature (Modifier mod)
561                 {
562                         switch (mod) {
563                                 case Modifier.OUT:
564                                         return "out";
565                                 case Modifier.PARAMS:
566                                         return "params";
567                                 case Modifier.REF:
568                                         return "ref";
569                                 case Modifier.ARGLIST:
570                                         return "__arglist";
571                                 case Modifier.This:
572                                         return "this";
573                                 default:
574                                         return "";
575                         }
576                 }
577
578                 public void IsClsCompliant ()
579                 {
580                         if (AttributeTester.IsClsCompliant (ExternalType ()))
581                                 return;
582
583                         Report.Error (3001, Location, "Argument type `{0}' is not CLS-compliant", GetSignatureForError ());
584                 }
585
586                 public virtual void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index)
587                 {
588 #if !GMCS_SOURCE || !MS_COMPATIBLE
589                         // TODO: It should use mb.DefineGenericParameters
590                         if (mb == null)
591                                 builder = cb.DefineParameter (index, Attributes, Name);
592                         else 
593                                 builder = mb.DefineParameter (index, Attributes, Name);
594 #endif
595
596                         if (OptAttributes != null)
597                                 OptAttributes.Emit ();
598                 }
599
600                 public override string[] ValidAttributeTargets {
601                         get {
602                                 return attribute_targets;
603                         }
604                 }
605
606                 public Parameter Clone ()
607                 {
608                         Parameter p = new Parameter (parameter_type, Name, modFlags, attributes, Location);
609                         p.IsTypeParameter = IsTypeParameter;
610
611                         return p;
612                 }
613
614                 public ExpressionStatement CreateExpressionTreeVariable (EmitContext ec)
615                 {
616                         if ((modFlags & Modifier.ISBYREF) != 0)
617                                 Report.Error (1951, Location, "An expression tree parameter cannot use `ref' or `out' modifier");
618
619                         LocalInfo variable = ec.CurrentBlock.AddTemporaryVariable (
620                                 ResolveParameterExpressionType (ec, Location), Location);
621                         variable.Resolve (ec);
622
623                         expr_tree_variable = new LocalVariableReference (
624                                 ec.CurrentBlock, variable.Name, Location, variable, false);
625
626                         ArrayList arguments = new ArrayList (2);
627                         arguments.Add (new Argument (new TypeOf (
628                                 new TypeExpression (parameter_type, Location), Location)));
629                         arguments.Add (new Argument (new StringConstant (Name, Location)));
630                         return new SimpleAssign (ExpressionTreeVariableReference (),
631                                 Expression.CreateExpressionFactoryCall ("Parameter", null, arguments, Location));
632                 }
633
634                 public Expression ExpressionTreeVariableReference ()
635                 {
636                         return expr_tree_variable;
637                 }
638
639                 //
640                 // System.Linq.Expressions.ParameterExpression type
641                 //
642                 public static TypeExpr ResolveParameterExpressionType (EmitContext ec, Location location)
643                 {
644                         if (parameter_expr_tree_type != null)
645                                 return parameter_expr_tree_type;
646
647                         Type p_type = TypeManager.parameter_expression_type;
648                         if (p_type == null) {
649                                 p_type = TypeManager.CoreLookupType ("System.Linq.Expressions", "ParameterExpression", Kind.Class, true);
650                                 TypeManager.parameter_expression_type = p_type;
651                         }
652
653                         parameter_expr_tree_type = new TypeExpression (p_type, location).
654                                 ResolveAsTypeTerminal (ec, false);
655
656                         return parameter_expr_tree_type;
657                 }
658         }
659
660         /// <summary>
661         ///   Represents the methods parameters
662         /// </summary>
663         public class Parameters : ParameterData {
664                 // Null object pattern
665                 public readonly Parameter [] FixedParameters;
666                 public readonly bool HasArglist;
667                 Type [] types;
668                 readonly int count;
669
670                 public static readonly Parameters EmptyReadOnlyParameters = new Parameters ();
671                 static readonly Parameter ArgList = new ArglistParameter ();
672
673 #if GMCS_SOURCE
674 //              public readonly TypeParameter[] TypeParameters;
675 #endif
676
677                 private Parameters ()
678                 {
679                         FixedParameters = new Parameter[0];
680                         types = new Type [0];
681                 }
682
683                 private Parameters (Parameter[] parameters, Type[] types)
684                 {
685                         FixedParameters = parameters;
686                         this.types = types;
687                         count = types.Length;
688                 }
689                 
690                 public Parameters (params Parameter[] parameters)
691                 {
692                         if (parameters == null)
693                                 throw new ArgumentException ("Use EmptyReadOnlyPatameters");
694
695                         FixedParameters = parameters;
696                         count = parameters.Length;
697                 }
698
699                 public Parameters (Parameter[] parameters, bool has_arglist):
700                         this (parameters)
701                 {
702                         HasArglist = has_arglist;
703                 }
704                 
705                 public static Parameters CreateFullyResolved (Parameter p)
706                 {
707                         return new Parameters (new Parameter [] { p }, new Type [] { p.ParameterType });
708                 }
709                 
710                 public static Parameters CreateFullyResolved (Parameter[] parameters, Type[] types)
711                 {
712                         return new Parameters (parameters, types);
713                 }
714
715                 /// <summary>
716                 /// Use this method when you merge compiler generated argument with user arguments
717                 /// </summary>
718                 public static Parameters MergeGenerated (Parameters userParams, params Parameter[] compilerParams)
719                 {
720                         Parameter[] all_params = new Parameter [userParams.count + compilerParams.Length];
721                         Type[] all_types = new Type[all_params.Length];
722                         userParams.FixedParameters.CopyTo(all_params, 0);
723                         userParams.Types.CopyTo (all_types, 0);
724
725                         int last_filled = userParams.Count;
726                         foreach (Parameter p in compilerParams) {
727                                 for (int i = 0; i < last_filled; ++i) {
728                                         while (p.Name == all_params [i].Name) {
729                                                 p.Name = '_' + p.Name;
730                                         }
731                                 }
732                                 all_params [last_filled] = p;
733                                 all_types [last_filled] = p.ParameterType;
734                                 ++last_filled;
735                         }
736                         
737                         return new Parameters (all_params, all_types);
738                 }
739
740                 public bool Empty {
741                         get {
742                                 return count == 0;
743                         }
744                 }
745
746                 public int Count {
747                         get {
748                                 return HasArglist ? count + 1 : count;
749                         }
750                 }
751
752                 //
753                 // The property can be used after parameter types were resolved.
754                 //
755                 public Type ExtensionMethodType {
756                         get {
757                                 if (count == 0)
758                                         return null;
759
760                                 return FixedParameters [0].HasExtensionMethodModifier ?
761                                         types [0] : null;
762                         }
763                 }
764
765                 public bool HasExtensionMethodType {
766                         get {
767                                 if (count == 0)
768                                         return false;
769
770                                 return FixedParameters [0].HasExtensionMethodModifier;
771                         }
772                 }
773
774
775                 bool VerifyArgs ()
776                 {
777                         if (count < 2)
778                                 return true;
779
780                         for (int i = 0; i < count; i++){
781                                 string base_name = FixedParameters [i].Name;
782                                 for (int j = i + 1; j < count; j++){
783                                         if (base_name != FixedParameters [j].Name)
784                                                 continue;
785
786                                         Report.Error (100, FixedParameters [i].Location,
787                                                 "The parameter name `{0}' is a duplicate", base_name);
788                                         return false;
789                                 }
790                         }
791                         return true;
792                 }
793                 
794                 
795                 /// <summary>
796                 ///    Returns the paramenter information based on the name
797                 /// </summary>
798                 public Parameter GetParameterByName (string name, out int idx)
799                 {
800                         idx = 0;
801
802                         if (count == 0)
803                                 return null;
804
805                         int i = 0;
806
807                         foreach (Parameter par in FixedParameters){
808                                 if (par.Name == name){
809                                         idx = i;
810                                         return par;
811                                 }
812                                 i++;
813                         }
814                         return null;
815                 }
816
817                 public Parameter GetParameterByName (string name)
818                 {
819                         int idx;
820
821                         return GetParameterByName (name, out idx);
822                 }
823                 
824                 public bool Resolve (IResolveContext ec)
825                 {
826                         if (types != null)
827                                 return true;
828
829                         types = new Type [count];
830                         
831                         if (!VerifyArgs ())
832                                 return false;
833
834                         bool ok = true;
835                         Parameter p;
836                         for (int i = 0; i < FixedParameters.Length; ++i) {
837                                 p = FixedParameters [i];
838                                 if (!p.Resolve (ec)) {
839                                         ok = false;
840                                         continue;
841                                 }
842                                 types [i] = p.ExternalType ();
843                         }
844
845                         return ok;
846                 }
847
848                 public void ResolveVariable (ToplevelBlock toplevel)
849                 {
850                         for (int i = 0; i < FixedParameters.Length; ++i) {
851                                 Parameter p = FixedParameters [i];
852                                 p.ResolveVariable (toplevel, i);
853                         }
854                 }
855
856                 public CallingConventions CallingConvention
857                 {
858                         get {
859                                 if (HasArglist)
860                                         return CallingConventions.VarArgs;
861                                 else
862                                         return CallingConventions.Standard;
863                         }
864                 }
865
866                 // Define each type attribute (in/out/ref) and
867                 // the argument names.
868                 public void ApplyAttributes (MethodBase builder)
869                 {
870                         if (count == 0)
871                                 return;
872
873                         MethodBuilder mb = builder as MethodBuilder;
874                         ConstructorBuilder cb = builder as ConstructorBuilder;
875
876                         for (int i = 0; i < FixedParameters.Length; i++) {
877                                 FixedParameters [i].ApplyAttributes (mb, cb, i + 1);
878                         }
879                 }
880
881 #if MS_COMPATIBLE
882                 public ParameterData InflateTypes (Type[] genArguments, Type[] argTypes)
883                 {
884                         Parameters p = Clone ();
885                         for (int i = 0; i < count; ++i) {
886                                 if (types[i].IsGenericType) {
887                                         Type[] gen_arguments_open = new Type [types[i].GetGenericTypeDefinition ().GetGenericArguments ().Length];
888                                         Type[] gen_arguments = types[i].GetGenericArguments ();
889                                         for (int ii = 0; ii < gen_arguments_open.Length; ++ii) {
890                                                 if (gen_arguments[ii].IsGenericParameter) {
891                                                         Type t = argTypes[gen_arguments[ii].GenericParameterPosition];
892                                                         gen_arguments_open[ii] = t;
893                                                 } else
894                                                         gen_arguments_open[ii] = gen_arguments[ii];
895                                         }
896
897                                         p.FixedParameters [i].ParameterType = p.types[i] =
898                                                 types[i].GetGenericTypeDefinition ().MakeGenericType (gen_arguments_open);
899                                         continue;
900                                 }
901
902                                 if (types[i].IsGenericParameter) {
903                                         Type gen_argument = argTypes[types[i].GenericParameterPosition];
904                                         p.FixedParameters[i].ParameterType = p.types[i] = gen_argument;
905                                         continue;
906                                 }
907                         }
908
909                         return p;
910                 }
911 #endif
912
913                 public void VerifyClsCompliance ()
914                 {
915                         foreach (Parameter p in FixedParameters)
916                                 p.IsClsCompliant ();
917                 }
918
919                 public string GetSignatureForError ()
920                 {
921                         StringBuilder sb = new StringBuilder ("(");
922                         if (count > 0) {
923                                 for (int i = 0; i < FixedParameters.Length; ++i) {
924                                         sb.Append (FixedParameters[i].GetSignatureForError ());
925                                         if (i < FixedParameters.Length - 1)
926                                                 sb.Append (", ");
927                                 }
928                         }
929
930                         if (HasArglist) {
931                                 if (sb.Length > 1)
932                                         sb.Append (", ");
933                                 sb.Append ("__arglist");
934                         }
935
936                         sb.Append (')');
937                         return sb.ToString ();
938                 }
939
940                 public Type[] Types {
941                         get {
942                                 return types;
943                         }
944                         //
945                         // Dangerous, used by implicit lambda parameters
946                         // only to workaround bad design
947                         //
948                         set {
949                                 types = value;
950                         }
951                 }
952
953                 public Parameter this [int pos]
954                 {
955                         get {
956                                 if (pos >= count && (HasArglist || HasParams)) {
957                                         if (HasArglist && (pos == 0 || pos >= count))
958                                                 return ArgList;
959                                         pos = count - 1;
960                                 }
961
962                                 return FixedParameters [pos];
963                         }
964                 }
965
966                 #region ParameterData Members
967
968                 public Type ParameterType (int pos)
969                 {
970                         return this [pos].ExternalType ();
971                 }
972
973                 public bool HasParams {
974                         get {
975                                 if (count == 0)
976                                         return false;
977
978                                 for (int i = count; i != 0; --i) {
979                                         if ((FixedParameters [i - 1].ModFlags & Parameter.Modifier.PARAMS) != 0)
980                                                 return true;
981                                 }
982                                 return false;
983                         }
984                 }
985
986                 public string ParameterName (int pos)
987                 {
988                         return this [pos].Name;
989                 }
990
991                 public string ParameterDesc (int pos)
992                 {
993                         return this [pos].GetSignatureForError ();
994                 }
995
996                 public Parameter.Modifier ParameterModifier (int pos)
997                 {
998                         return this [pos].ModFlags;
999                 }
1000
1001                 public Expression CreateExpressionTree (EmitContext ec, Location loc)
1002                 {
1003                         ArrayList initializers = new ArrayList (count);
1004                         foreach (Parameter p in FixedParameters) {
1005                                 //
1006                                 // Each parameter expression is stored to local variable
1007                                 // to save some memory when referenced later.
1008                                 //
1009                                 StatementExpression se = new StatementExpression (p.CreateExpressionTreeVariable (ec));
1010                                 if (se.Resolve (ec))
1011                                         ec.CurrentBlock.AddScopeStatement (se);
1012                                 
1013                                 initializers.Add (p.ExpressionTreeVariableReference ());
1014                         }
1015
1016                         return new ArrayCreation (
1017                                 Parameter.ResolveParameterExpressionType (ec, loc),
1018                                 "[]", initializers, loc);
1019                 }
1020
1021                 public Parameters Clone ()
1022                 {
1023                         Parameter [] parameters_copy = new Parameter [FixedParameters.Length];
1024                         int i = 0;
1025                         foreach (Parameter p in FixedParameters)
1026                                 parameters_copy [i++] = p.Clone ();
1027                         Parameters ps = new Parameters (parameters_copy, HasArglist);
1028                         if (types != null)
1029                                 ps.types = (Type[])types.Clone ();
1030                         return ps;
1031                 }
1032                 
1033                 #endregion
1034         }
1035 }