Changed UploadStringAsync to handle UploadString encapsulated exceptions.
[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 // Copyright 2011 Xamarin Inc
12 //
13 //
14 using System;
15 using System.Text;
16
17 #if STATIC
18 using MetaType = IKVM.Reflection.Type;
19 using IKVM.Reflection;
20 using IKVM.Reflection.Emit;
21 #else
22 using MetaType = System.Type;
23 using System.Reflection;
24 using System.Reflection.Emit;
25 #endif
26
27 namespace Mono.CSharp {
28
29         /// <summary>
30         ///   Abstract Base class for parameters of a method.
31         /// </summary>
32         public abstract class ParameterBase : Attributable
33         {
34                 protected ParameterBuilder builder;
35
36                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
37                 {
38 #if false
39                         if (a.Type == pa.MarshalAs) {
40                                 UnmanagedMarshal marshal = a.GetMarshal (this);
41                                 if (marshal != null) {
42                                         builder.SetMarshal (marshal);
43                                 }
44                                 return;
45                         }
46 #endif
47                         if (a.HasSecurityAttribute) {
48                                 a.Error_InvalidSecurityParent ();
49                                 return;
50                         }
51
52                         if (a.Type == pa.Dynamic) {
53                                 a.Error_MisusedDynamicAttribute ();
54                                 return;
55                         }
56
57                         builder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
58                 }
59
60                 public ParameterBuilder Builder {
61                         get {
62                                 return builder;
63                         }
64                 }
65
66                 public override bool IsClsComplianceRequired()
67                 {
68                         return false;
69                 }
70         }
71
72         /// <summary>
73         /// Class for applying custom attributes on the return type
74         /// </summary>
75         public class ReturnParameter : ParameterBase
76         {
77                 MemberCore method;
78
79                 // TODO: merge method and mb
80                 public ReturnParameter (MemberCore method, MethodBuilder mb, Location location)
81                 {
82                         this.method = method;
83                         try {
84                                 builder = mb.DefineParameter (0, ParameterAttributes.None, "");                 
85                         }
86                         catch (ArgumentOutOfRangeException) {
87                                 method.Compiler.Report.RuntimeMissingSupport (location, "custom attributes on the return type");
88                         }
89                 }
90
91                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
92                 {
93                         if (a.Type == pa.CLSCompliant) {
94                                 method.Compiler.Report.Warning (3023, 1, a.Location,
95                                         "CLSCompliant attribute has no meaning when applied to return types. Try putting it on the method instead");
96                         }
97
98                         // This occurs after Warning -28
99                         if (builder == null)
100                                 return;
101
102                         base.ApplyAttributeBuilder (a, ctor, cdata, pa);
103                 }
104
105                 public override AttributeTargets AttributeTargets {
106                         get {
107                                 return AttributeTargets.ReturnValue;
108                         }
109                 }
110
111                 /// <summary>
112                 /// Is never called
113                 /// </summary>
114                 public override string[] ValidAttributeTargets {
115                         get {
116                                 return null;
117                         }
118                 }
119         }
120
121         public class ImplicitLambdaParameter : Parameter
122         {
123                 public ImplicitLambdaParameter (string name, Location loc)
124                         : base (null, name, Modifier.NONE, null, loc)
125                 {
126                 }
127
128                 public override TypeSpec Resolve (IMemberContext ec, int index)
129                 {
130                         if (parameter_type == null)
131                                 throw new InternalErrorException ("A type of implicit lambda parameter `{0}' is not set",
132                                         Name);
133
134                         base.idx = index;
135                         return parameter_type;
136                 }
137
138                 public void SetParameterType (TypeSpec type)
139                 {
140                         parameter_type = type;
141                 }
142         }
143
144         public class ParamsParameter : Parameter {
145                 public ParamsParameter (FullNamedExpression type, string name, Attributes attrs, Location loc):
146                         base (type, name, Parameter.Modifier.PARAMS, attrs, loc)
147                 {
148                 }
149
150                 public override TypeSpec Resolve (IMemberContext ec, int index)
151                 {
152                         if (base.Resolve (ec, index) == null)
153                                 return null;
154
155                         var ac = parameter_type as ArrayContainer;
156                         if (ac == null || ac.Rank != 1) {
157                                 ec.Module.Compiler.Report.Error (225, Location, "The params parameter must be a single dimensional array");
158                                 return null;
159                         }
160
161                         return parameter_type;
162                 }
163
164                 public override void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index, PredefinedAttributes pa)
165                 {
166                         base.ApplyAttributes (mb, cb, index, pa);
167                         pa.ParamArray.EmitAttribute (builder);
168                 }
169         }
170
171         public class ArglistParameter : Parameter {
172                 // Doesn't have proper type because it's never chosen for better conversion
173                 public ArglistParameter (Location loc) :
174                         base (null, String.Empty, Parameter.Modifier.NONE, null, loc)
175                 {
176                         parameter_type = InternalType.Arglist;
177                 }
178
179                 public override void  ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index, PredefinedAttributes pa)
180                 {
181                         // Nothing to do
182                 }
183
184                 public override bool CheckAccessibility (InterfaceMemberBase member)
185                 {
186                         return true;
187                 }
188
189                 public override TypeSpec Resolve (IMemberContext ec, int index)
190                 {
191                         return parameter_type;
192                 }
193         }
194
195         public interface IParameterData
196         {
197                 Expression DefaultValue { get; }
198                 bool HasExtensionMethodModifier { get; }
199                 bool HasDefaultValue { get; }
200                 Parameter.Modifier ModFlags { get; }
201                 string Name { get; }
202         }
203
204         //
205         // Parameter information created by parser
206         //
207         public class Parameter : ParameterBase, IParameterData, ILocalVariable // TODO: INamedBlockVariable
208         {
209                 [Flags]
210                 public enum Modifier : byte {
211                         NONE    = 0,
212                         PARAMS  = 1 << 0,
213                         REF = 1 << 1,
214                         OUT = 1 << 2,
215                         This = 1 << 3,
216                         CallerMemberName = 1 << 4,
217                         CallerLineNumber = 1 << 5,
218                         CallerFilePath = 1 << 6,
219
220                         RefOutMask = REF | OUT,
221                         ModifierMask = PARAMS | REF | OUT | This,
222                         CallerMask = CallerMemberName | CallerLineNumber | CallerFilePath
223                 }
224
225                 static readonly string[] attribute_targets = new string[] { "param" };
226                 static readonly string[] attribute_targets_primary = new string[] { "param", "field" };
227
228                 FullNamedExpression texpr;
229                 Modifier modFlags;
230                 string name;
231                 Expression default_expr;
232                 protected TypeSpec parameter_type;
233                 readonly Location loc;
234                 protected int idx;
235                 public bool HasAddressTaken;
236
237                 Constructor primary_constructor;
238                 TemporaryVariableReference expr_tree_variable;
239
240                 HoistedParameter hoisted_variant;
241
242                 public Parameter (FullNamedExpression type, string name, Modifier mod, Attributes attrs, Location loc)
243                 {
244                         this.name = name;
245                         modFlags = mod;
246                         this.loc = loc;
247                         texpr = type;
248
249                         // Only assign, attributes will be attached during resolve
250                         base.attributes = attrs;
251                 }
252
253                 #region Properties
254
255                 public Expression DefaultExpression {
256                         get {
257                                 return default_expr;
258                         }
259                 }
260
261                 public DefaultParameterValueExpression DefaultValue {
262                         get {
263                                 return default_expr as DefaultParameterValueExpression;
264                         }
265                         set {
266                                 default_expr = value;
267                         }
268                 }
269
270                 Expression IParameterData.DefaultValue {
271                         get {
272                                 var expr = default_expr as DefaultParameterValueExpression;
273                                 return expr == null ? default_expr : expr.Child;
274                         }
275                 }
276
277                 bool HasOptionalExpression {
278                         get {
279                                 return default_expr is DefaultParameterValueExpression;
280                         }
281                 }
282
283                 public Location Location {
284                         get {
285                                 return loc;
286                         }
287                 }
288
289                 public Modifier ParameterModifier {
290                         get {
291                                 return modFlags;
292                         }
293                 }
294
295                 public TypeSpec Type {
296                         get {
297                                 return parameter_type;
298                         }
299                         set {
300                                 parameter_type = value;
301                         }
302                 }
303
304                 public FullNamedExpression TypeExpression  {
305                         get {
306                                 return texpr;
307                         }
308                 }
309
310                 public override string[] ValidAttributeTargets {
311                         get {
312                                 return primary_constructor != null ? attribute_targets_primary : attribute_targets;
313                         }
314                 }
315
316                 #endregion
317
318                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
319                 {
320                         if (a.Target == AttributeTargets.Field) {
321                                 var field = MemberCache.FindMember (primary_constructor.Spec.DeclaringType, MemberFilter.Field (name, parameter_type), BindingRestriction.DeclaredOnly);
322                                 ((Field)field.MemberDefinition).ApplyAttributeBuilder (a, ctor, cdata, pa);
323                                 return;
324                         }
325
326                         if (a.Type == pa.In && ModFlags == Modifier.OUT) {
327                                 a.Report.Error (36, a.Location, "An out parameter cannot have the `In' attribute");
328                                 return;
329                         }
330
331                         if (a.Type == pa.ParamArray) {
332                                 a.Report.Error (674, a.Location, "Do not use `System.ParamArrayAttribute'. Use the `params' keyword instead");
333                                 return;
334                         }
335
336                         if (a.Type == pa.Out && (ModFlags & Modifier.REF) != 0 &&
337                             !OptAttributes.Contains (pa.In)) {
338                                 a.Report.Error (662, a.Location,
339                                         "Cannot specify only `Out' attribute on a ref parameter. Use both `In' and `Out' attributes or neither");
340                                 return;
341                         }
342
343                         if (a.Type == pa.CLSCompliant) {
344                                 a.Report.Warning (3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
345                         } else if (a.Type == pa.DefaultParameterValue || a.Type == pa.OptionalParameter) {
346                                 if (HasOptionalExpression) {
347                                         a.Report.Error (1745, a.Location,
348                                                 "Cannot specify `{0}' attribute on optional parameter `{1}'",
349                                                 a.Type.GetSignatureForError ().Replace ("Attribute", ""), Name);
350                                 }
351
352                                 if (a.Type == pa.DefaultParameterValue)
353                                         return;
354                         } else if (a.Type == pa.CallerMemberNameAttribute) {
355                                 if ((modFlags & Modifier.CallerMemberName) == 0) {
356                                         a.Report.Error (4022, a.Location,
357                                                 "The CallerMemberName attribute can only be applied to parameters with default value");
358                                 }
359                         } else if (a.Type == pa.CallerLineNumberAttribute) {
360                                 if ((modFlags & Modifier.CallerLineNumber) == 0) {
361                                         a.Report.Error (4020, a.Location,
362                                                 "The CallerLineNumber attribute can only be applied to parameters with default value");
363                                 }
364                         } else if (a.Type == pa.CallerFilePathAttribute) {
365                                 if ((modFlags & Modifier.CallerFilePath) == 0) {
366                                         a.Report.Error (4021, a.Location,
367                                                 "The CallerFilePath attribute can only be applied to parameters with default value");
368                                 }
369                         }
370
371                         base.ApplyAttributeBuilder (a, ctor, cdata, pa);
372                 }
373                 
374                 public virtual bool CheckAccessibility (InterfaceMemberBase member)
375                 {
376                         if (parameter_type == null)
377                                 return true;
378
379                         return member.IsAccessibleAs (parameter_type);
380                 }
381
382                 // <summary>
383                 //   Resolve is used in method definitions
384                 // </summary>
385                 public virtual TypeSpec Resolve (IMemberContext rc, int index)
386                 {
387                         if (parameter_type != null)
388                                 return parameter_type;
389
390                         if (attributes != null)
391                                 attributes.AttachTo (this, rc);
392
393                         var ctor = rc.CurrentMemberDefinition as Constructor;
394                         if (ctor != null && ctor.IsPrimaryConstructor)
395                                 primary_constructor = ctor;
396
397                         parameter_type = texpr.ResolveAsType (rc);
398                         if (parameter_type == null)
399                                 return null;
400
401                         this.idx = index;
402
403                         if ((modFlags & Parameter.Modifier.RefOutMask) != 0 && parameter_type.IsSpecialRuntimeType) {
404                                 rc.Module.Compiler.Report.Error (1601, Location, "Method or delegate parameter cannot be of type `{0}'",
405                                         GetSignatureForError ());
406                                 return null;
407                         }
408
409                         VarianceDecl.CheckTypeVariance (parameter_type,
410                                 (modFlags & Parameter.Modifier.RefOutMask) != 0 ? Variance.None : Variance.Contravariant,
411                                 rc);
412
413                         if (parameter_type.IsStatic) {
414                                 rc.Module.Compiler.Report.Error (721, Location, "`{0}': static types cannot be used as parameters",
415                                         texpr.GetSignatureForError ());
416                                 return parameter_type;
417                         }
418
419                         if ((modFlags & Modifier.This) != 0 && (parameter_type.IsPointer || parameter_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)) {
420                                 rc.Module.Compiler.Report.Error (1103, Location, "The extension method cannot be of type `{0}'",
421                                         parameter_type.GetSignatureForError ());
422                         }
423
424                         return parameter_type;
425                 }
426
427                 void ResolveCallerAttributes (ResolveContext rc)
428                 {
429                         var pa = rc.Module.PredefinedAttributes;
430                         TypeSpec caller_type;
431
432                         foreach (var attr in attributes.Attrs) {
433                                 var atype = attr.ResolveTypeForComparison ();
434                                 if (atype == null)
435                                         continue;
436
437                                 if (atype == pa.CallerMemberNameAttribute) {
438                                         caller_type = rc.BuiltinTypes.String;
439                                         if (caller_type != parameter_type && !Convert.ImplicitReferenceConversionExists (caller_type, parameter_type)) {
440                                                 rc.Report.Error (4019, attr.Location,
441                                                         "The CallerMemberName attribute cannot be applied because there is no standard conversion from `{0}' to `{1}'",
442                                                         caller_type.GetSignatureForError (), parameter_type.GetSignatureForError ());
443                                         }
444
445                                         modFlags |= Modifier.CallerMemberName;
446                                         continue;
447                                 }
448
449                                 if (atype == pa.CallerLineNumberAttribute) {
450                                         caller_type = rc.BuiltinTypes.Int;
451                                         if (caller_type != parameter_type && !Convert.ImplicitNumericConversionExists (caller_type, parameter_type)) {
452                                                 rc.Report.Error (4017, attr.Location,
453                                                         "The CallerMemberName attribute cannot be applied because there is no standard conversion from `{0}' to `{1}'",
454                                                         caller_type.GetSignatureForError (), parameter_type.GetSignatureForError ());
455                                         }
456
457                                         modFlags |= Modifier.CallerLineNumber;
458                                         continue;
459                                 }
460
461                                 if (atype == pa.CallerFilePathAttribute) {
462                                         caller_type = rc.BuiltinTypes.String;
463                                         if (caller_type != parameter_type && !Convert.ImplicitReferenceConversionExists (caller_type, parameter_type)) {
464                                                 rc.Report.Error (4018, attr.Location,
465                                                         "The CallerFilePath attribute cannot be applied because there is no standard conversion from `{0}' to `{1}'",
466                                                         caller_type.GetSignatureForError (), parameter_type.GetSignatureForError ());
467                                         }
468
469                                         modFlags |= Modifier.CallerFilePath;
470                                         continue;
471                                 }
472                         }
473                 }
474
475                 public void ResolveDefaultValue (ResolveContext rc)
476                 {
477                         //
478                         // Default value was specified using an expression
479                         //
480                         if (default_expr != null) {
481                                 ((DefaultParameterValueExpression)default_expr).Resolve (rc, this);
482                                 if (attributes != null)
483                                         ResolveCallerAttributes (rc);
484
485                                 return;
486                         }
487
488                         if (attributes == null)
489                                 return;
490
491                         var pa = rc.Module.PredefinedAttributes;
492                         var def_attr = attributes.Search (pa.DefaultParameterValue);
493                         if (def_attr != null) {
494                                 if (def_attr.Resolve () == null)
495                                         return;
496
497                                 var default_expr_attr = def_attr.GetParameterDefaultValue ();
498                                 if (default_expr_attr == null)
499                                         return;
500
501                                 var dpa_rc = def_attr.CreateResolveContext ();
502                                 default_expr = default_expr_attr.Resolve (dpa_rc);
503
504                                 if (default_expr is BoxedCast)
505                                         default_expr = ((BoxedCast) default_expr).Child;
506
507                                 Constant c = default_expr as Constant;
508                                 if (c == null) {
509                                         if (parameter_type.BuiltinType == BuiltinTypeSpec.Type.Object) {
510                                                 rc.Report.Error (1910, default_expr.Location,
511                                                         "Argument of type `{0}' is not applicable for the DefaultParameterValue attribute",
512                                                         default_expr.Type.GetSignatureForError ());
513                                         } else {
514                                                 rc.Report.Error (1909, default_expr.Location,
515                                                         "The DefaultParameterValue attribute is not applicable on parameters of type `{0}'",
516                                                         default_expr.Type.GetSignatureForError ());
517                                         }
518
519                                         default_expr = null;
520                                         return;
521                                 }
522
523                                 if (TypeSpecComparer.IsEqual (default_expr.Type, parameter_type) ||
524                                         (default_expr is NullConstant && TypeSpec.IsReferenceType (parameter_type) && !parameter_type.IsGenericParameter) ||
525                                         parameter_type.BuiltinType == BuiltinTypeSpec.Type.Object) {
526                                         return;
527                                 }
528
529                                 //
530                                 // LAMESPEC: Some really weird csc behaviour which we have to mimic
531                                 // User operators returning same type as parameter type are considered
532                                 // valid for this attribute only
533                                 //
534                                 // struct S { public static implicit operator S (int i) {} }
535                                 //
536                                 // void M ([DefaultParameterValue (3)]S s)
537                                 //
538                                 var expr = Convert.ImplicitUserConversion (dpa_rc, default_expr, parameter_type, loc);
539                                 if (expr != null && TypeSpecComparer.IsEqual (expr.Type, parameter_type)) {
540                                         return;
541                                 }
542                                 
543                                 rc.Report.Error (1908, default_expr.Location, "The type of the default value should match the type of the parameter");
544                                 return;
545                         }
546
547                         var opt_attr = attributes.Search (pa.OptionalParameter);
548                         if (opt_attr != null) {
549                                 default_expr = EmptyExpression.MissingValue;
550                         }
551                 }
552
553                 public bool HasDefaultValue {
554                         get { return default_expr != null; }
555                 }
556
557                 public bool HasExtensionMethodModifier {
558                         get { return (modFlags & Modifier.This) != 0; }
559                 }
560
561                 //
562                 // Hoisted parameter variant
563                 //
564                 public HoistedParameter HoistedVariant {
565                         get {
566                                 return hoisted_variant;
567                         }
568                         set {
569                                 hoisted_variant = value;
570                         }
571                 }
572
573                 public Modifier ModFlags {
574                         get { return modFlags & ~Modifier.This; }
575                 }
576
577                 public string Name {
578                         get { return name; }
579                         set { name = value; }
580                 }
581
582                 public override AttributeTargets AttributeTargets {
583                         get {
584                                 return AttributeTargets.Parameter;
585                         }
586                 }
587
588                 public void Error_DuplicateName (Report r)
589                 {
590                         r.Error (100, Location, "The parameter name `{0}' is a duplicate", Name);
591                 }
592
593                 public virtual string GetSignatureForError ()
594                 {
595                         string type_name;
596                         if (parameter_type != null)
597                                 type_name = parameter_type.GetSignatureForError ();
598                         else
599                                 type_name = texpr.GetSignatureForError ();
600
601                         string mod = GetModifierSignature (modFlags);
602                         if (mod.Length > 0)
603                                 return String.Concat (mod, " ", type_name);
604
605                         return type_name;
606                 }
607
608                 public static string GetModifierSignature (Modifier mod)
609                 {
610                         switch (mod) {
611                         case Modifier.OUT:
612                                 return "out";
613                         case Modifier.PARAMS:
614                                 return "params";
615                         case Modifier.REF:
616                                 return "ref";
617                         case Modifier.This:
618                                 return "this";
619                         default:
620                                 return "";
621                         }
622                 }
623
624                 public void IsClsCompliant (IMemberContext ctx)
625                 {
626                         if (parameter_type.IsCLSCompliant ())
627                                 return;
628
629                         ctx.Module.Compiler.Report.Warning (3001, 1, Location,
630                                 "Argument type `{0}' is not CLS-compliant", parameter_type.GetSignatureForError ());
631                 }
632
633                 public virtual void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index, PredefinedAttributes pa)
634                 {
635                         if (builder != null)
636                                 throw new InternalErrorException ("builder already exists");
637
638                         var pattrs = ParametersCompiled.GetParameterAttribute (modFlags);
639                         if (HasOptionalExpression)
640                                 pattrs |= ParameterAttributes.Optional;
641
642                         if (mb == null)
643                                 builder = cb.DefineParameter (index, pattrs, Name);
644                         else
645                                 builder = mb.DefineParameter (index, pattrs, Name);
646
647                         if (OptAttributes != null)
648                                 OptAttributes.Emit ();
649
650                         if (HasDefaultValue && default_expr.Type != null) {
651                                 //
652                                 // Emit constant values for true constants only, the other
653                                 // constant-like expressions will rely on default value expression
654                                 //
655                                 var def_value = DefaultValue;
656                                 Constant c = def_value != null ? def_value.Child as Constant : default_expr as Constant;
657                                 if (c != null) {
658                                         if (c.Type.BuiltinType == BuiltinTypeSpec.Type.Decimal) {
659                                                 pa.DecimalConstant.EmitAttribute (builder, (decimal) c.GetValue (), c.Location);
660                                         } else {
661                                                 builder.SetConstant (c.GetValue ());
662                                         }
663                                 } else if (default_expr.Type.IsStruct || default_expr.Type.IsGenericParameter) {
664                                         //
665                                         // Handles special case where default expression is used with value-type or type parameter
666                                         //
667                                         // void Foo (S s = default (S)) {}
668                                         //
669                                         builder.SetConstant (null);
670                                 }
671                         }
672
673                         if (parameter_type != null) {
674                                 if (parameter_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
675                                         pa.Dynamic.EmitAttribute (builder);
676                                 } else if (parameter_type.HasDynamicElement) {
677                                         pa.Dynamic.EmitAttribute (builder, parameter_type, Location);
678                                 }
679                         }
680                 }
681
682                 public Parameter Clone ()
683                 {
684                         Parameter p = (Parameter) MemberwiseClone ();
685                         if (attributes != null)
686                                 p.attributes = attributes.Clone ();
687
688                         return p;
689                 }
690
691                 public ExpressionStatement CreateExpressionTreeVariable (BlockContext ec)
692                 {
693                         if ((modFlags & Modifier.RefOutMask) != 0)
694                                 ec.Report.Error (1951, Location, "An expression tree parameter cannot use `ref' or `out' modifier");
695
696                         expr_tree_variable = TemporaryVariableReference.Create (ResolveParameterExpressionType (ec, Location).Type, ec.CurrentBlock.ParametersBlock, Location);
697                         expr_tree_variable = (TemporaryVariableReference) expr_tree_variable.Resolve (ec);
698
699                         Arguments arguments = new Arguments (2);
700                         arguments.Add (new Argument (new TypeOf (parameter_type, Location)));
701                         arguments.Add (new Argument (new StringConstant (ec.BuiltinTypes, Name, Location)));
702                         return new SimpleAssign (ExpressionTreeVariableReference (),
703                                 Expression.CreateExpressionFactoryCall (ec, "Parameter", null, arguments, Location));
704                 }
705
706                 public void Emit (EmitContext ec)
707                 {
708                         ec.EmitArgumentLoad (idx);
709                 }
710
711                 public void EmitAssign (EmitContext ec)
712                 {
713                         ec.EmitArgumentStore (idx);
714                 }
715
716                 public void EmitAddressOf (EmitContext ec)
717                 {
718                         if ((ModFlags & Modifier.RefOutMask) != 0) {
719                                 ec.EmitArgumentLoad (idx);
720                         } else {
721                                 ec.EmitArgumentAddress (idx);
722                         }
723                 }
724
725                 public TemporaryVariableReference ExpressionTreeVariableReference ()
726                 {
727                         return expr_tree_variable;
728                 }
729
730                 //
731                 // System.Linq.Expressions.ParameterExpression type
732                 //
733                 public static TypeExpr ResolveParameterExpressionType (IMemberContext ec, Location location)
734                 {
735                         TypeSpec p_type = ec.Module.PredefinedTypes.ParameterExpression.Resolve ();
736                         return new TypeExpression (p_type, location);
737                 }
738
739                 public void Warning_UselessOptionalParameter (Report Report)
740                 {
741                         Report.Warning (1066, 1, Location,
742                                 "The default value specified for optional parameter `{0}' will never be used",
743                                 Name);
744                 }
745         }
746
747         //
748         // Imported or resolved parameter information
749         //
750         public class ParameterData : IParameterData
751         {
752                 readonly string name;
753                 readonly Parameter.Modifier modifiers;
754                 readonly Expression default_value;
755
756                 public ParameterData (string name, Parameter.Modifier modifiers)
757                 {
758                         this.name = name;
759                         this.modifiers = modifiers;
760                 }
761
762                 public ParameterData (string name, Parameter.Modifier modifiers, Expression defaultValue)
763                         : this (name, modifiers)
764                 {
765                         this.default_value = defaultValue;
766                 }
767
768                 #region IParameterData Members
769
770                 public Expression DefaultValue {
771                         get { return default_value; }
772                 }
773
774                 public bool HasExtensionMethodModifier {
775                         get { return (modifiers & Parameter.Modifier.This) != 0; }
776                 }
777
778                 public bool HasDefaultValue {
779                         get { return default_value != null; }
780                 }
781
782                 public Parameter.Modifier ModFlags {
783                         get { return modifiers; }
784                 }
785
786                 public string Name {
787                         get { return name; }
788                 }
789
790                 #endregion
791         }
792
793         public abstract class AParametersCollection
794         {
795                 protected bool has_arglist;
796                 protected bool has_params;
797
798                 // Null object pattern
799                 protected IParameterData [] parameters;
800                 protected TypeSpec [] types;
801
802                 public CallingConventions CallingConvention {
803                         get {
804                                 return has_arglist ?
805                                         CallingConventions.VarArgs :
806                                         CallingConventions.Standard;
807                         }
808                 }
809
810                 public int Count {
811                         get { return parameters.Length; }
812                 }
813
814                 public TypeSpec ExtensionMethodType {
815                         get {
816                                 if (Count == 0)
817                                         return null;
818
819                                 return FixedParameters [0].HasExtensionMethodModifier ?
820                                         types [0] : null;
821                         }
822                 }
823
824                 public IParameterData [] FixedParameters {
825                         get {
826                                 return parameters;
827                         }
828                 }
829
830                 public static ParameterAttributes GetParameterAttribute (Parameter.Modifier modFlags)
831                 {
832                         return (modFlags & Parameter.Modifier.OUT) != 0 ?
833                                 ParameterAttributes.Out : ParameterAttributes.None;
834                 }
835
836                 // Very expensive operation
837                 public MetaType[] GetMetaInfo ()
838                 {
839                         MetaType[] types;
840                         if (has_arglist) {
841                                 if (Count == 1)
842                                         return MetaType.EmptyTypes;
843
844                                 types = new MetaType[Count - 1];
845                         } else {
846                                 if (Count == 0)
847                                         return MetaType.EmptyTypes;
848
849                                 types = new MetaType[Count];
850                         }
851
852                         for (int i = 0; i < types.Length; ++i) {
853                                 types[i] = Types[i].GetMetaInfo ();
854
855                                 if ((FixedParameters[i].ModFlags & Parameter.Modifier.RefOutMask) == 0)
856                                         continue;
857
858                                 // TODO MemberCache: Should go to MetaInfo getter
859                                 types [i] = types [i].MakeByRefType ();
860                         }
861
862                         return types;
863                 }
864
865                 //
866                 // Returns the parameter information based on the name
867                 //
868                 public int GetParameterIndexByName (string name)
869                 {
870                         for (int idx = 0; idx < Count; ++idx) {
871                                 if (parameters [idx].Name == name)
872                                         return idx;
873                         }
874
875                         return -1;
876                 }
877
878                 public string GetSignatureForDocumentation ()
879                 {
880                         if (IsEmpty)
881                                 return string.Empty;
882
883                         StringBuilder sb = new StringBuilder ("(");
884                         for (int i = 0; i < Count; ++i) {
885                                 if (i != 0)
886                                         sb.Append (",");
887
888                                 sb.Append (types [i].GetSignatureForDocumentation ());
889
890                                 if ((parameters[i].ModFlags & Parameter.Modifier.RefOutMask) != 0)
891                                         sb.Append ("@");
892                         }
893                         sb.Append (")");
894
895                         return sb.ToString ();
896                 }
897
898                 public string GetSignatureForError ()
899                 {
900                         return GetSignatureForError ("(", ")", Count);
901                 }
902
903                 public string GetSignatureForError (string start, string end, int count)
904                 {
905                         StringBuilder sb = new StringBuilder (start);
906                         for (int i = 0; i < count; ++i) {
907                                 if (i != 0)
908                                         sb.Append (", ");
909                                 sb.Append (ParameterDesc (i));
910                         }
911                         sb.Append (end);
912                         return sb.ToString ();
913                 }
914
915                 public bool HasArglist {
916                         get { return has_arglist; }
917                 }
918
919                 public bool HasExtensionMethodType {
920                         get {
921                                 if (Count == 0)
922                                         return false;
923
924                                 return FixedParameters [0].HasExtensionMethodModifier;
925                         }
926                 }
927
928                 public bool HasParams {
929                         get { return has_params; }
930                 }
931
932                 public bool IsEmpty {
933                         get { return parameters.Length == 0; }
934                 }
935
936                 public AParametersCollection Inflate (TypeParameterInflator inflator)
937                 {
938                         TypeSpec[] inflated_types = null;
939                         bool default_value = false;
940
941                         for (int i = 0; i < Count; ++i) {
942                                 var inflated_param = inflator.Inflate (types[i]);
943                                 if (inflated_types == null) {
944                                         if (inflated_param == types[i])
945                                                 continue;
946
947                                         default_value |= FixedParameters[i].HasDefaultValue;
948                                         inflated_types = new TypeSpec[types.Length];
949                                         Array.Copy (types, inflated_types, types.Length);
950                                 } else {
951                                         if (inflated_param == types[i])
952                                                 continue;
953
954                                         default_value |= FixedParameters[i].HasDefaultValue;
955                                 }
956
957                                 inflated_types[i] = inflated_param;
958                         }
959
960                         if (inflated_types == null)
961                                 return this;
962
963                         var clone = (AParametersCollection) MemberwiseClone ();
964                         clone.types = inflated_types;
965
966                         //
967                         // Default expression is original expression from the parameter
968                         // declaration context which can be of nested enum in generic class type.
969                         // In such case we end up with expression type of G<T>.E and e.g. parameter
970                         // type of G<int>.E and conversion would fail without inflate in this
971                         // context.
972                         //
973                         if (default_value) {
974                                 clone.parameters = new IParameterData[Count];
975                                 for (int i = 0; i < Count; ++i) {
976                                         var fp = FixedParameters[i];
977                                         clone.FixedParameters[i] = fp;
978
979                                         if (!fp.HasDefaultValue)
980                                                 continue;
981
982                                         var expr = fp.DefaultValue;
983
984                                         if (inflated_types[i] == expr.Type)
985                                                 continue;
986
987                                         var c = expr as Constant;
988                                         if (c != null) {
989                                                 //
990                                                 // It may fail we are inflating before type validation is done
991                                                 //
992                                                 c = Constant.ExtractConstantFromValue (inflated_types[i], c.GetValue (), expr.Location);
993                                                 if (c == null)
994                                                         expr = new DefaultValueExpression (new TypeExpression (inflated_types[i], expr.Location), expr.Location);
995                                                 else
996                                                         expr = c;
997                                         } else if (expr is DefaultValueExpression)
998                                                 expr = new DefaultValueExpression (new TypeExpression (inflated_types[i], expr.Location), expr.Location);
999
1000                                         clone.FixedParameters[i] = new ParameterData (fp.Name, fp.ModFlags, expr);
1001                                 }
1002                         }
1003
1004                         return clone;
1005                 }
1006
1007                 public string ParameterDesc (int pos)
1008                 {
1009                         if (types == null || types [pos] == null)
1010                                 return ((Parameter)FixedParameters [pos]).GetSignatureForError ();
1011
1012                         string type = types [pos].GetSignatureForError ();
1013                         if (FixedParameters [pos].HasExtensionMethodModifier)
1014                                 return "this " + type;
1015
1016                         var mod = FixedParameters[pos].ModFlags & Parameter.Modifier.ModifierMask;
1017                         if (mod == 0)
1018                                 return type;
1019
1020                         return Parameter.GetModifierSignature (mod) + " " + type;
1021                 }
1022
1023                 public TypeSpec[] Types {
1024                         get { return types; }
1025                         set { types = value; }
1026                 }
1027         }
1028
1029         //
1030         // A collection of imported or resolved parameters
1031         //
1032         public class ParametersImported : AParametersCollection
1033         {
1034                 public ParametersImported (IParameterData [] parameters, TypeSpec [] types, bool hasArglist, bool hasParams)
1035                 {
1036                         this.parameters = parameters;
1037                         this.types = types;
1038                         this.has_arglist = hasArglist;
1039                         this.has_params = hasParams;
1040                 }
1041
1042                 public ParametersImported (IParameterData[] param, TypeSpec[] types, bool hasParams)
1043                 {
1044                         this.parameters = param;
1045                         this.types = types;
1046                         this.has_params = hasParams;
1047                 }
1048         }
1049
1050         /// <summary>
1051         ///   Represents the methods parameters
1052         /// </summary>
1053         public class ParametersCompiled : AParametersCollection
1054         {
1055                 public static readonly ParametersCompiled EmptyReadOnlyParameters = new ParametersCompiled ();
1056                 
1057                 // Used by C# 2.0 delegates
1058                 public static readonly ParametersCompiled Undefined = new ParametersCompiled ();
1059
1060                 private ParametersCompiled ()
1061                 {
1062                         parameters = new Parameter [0];
1063                         types = TypeSpec.EmptyTypes;
1064                 }
1065
1066                 private ParametersCompiled (IParameterData[] parameters, TypeSpec[] types)
1067                 {
1068                         this.parameters = parameters;
1069                     this.types = types;
1070                 }
1071                 
1072                 public ParametersCompiled (params Parameter[] parameters)
1073                 {
1074                         if (parameters == null || parameters.Length == 0)
1075                                 throw new ArgumentException ("Use EmptyReadOnlyParameters");
1076
1077                         this.parameters = parameters;
1078                         int count = parameters.Length;
1079
1080                         for (int i = 0; i < count; i++){
1081                                 has_params |= (parameters [i].ModFlags & Parameter.Modifier.PARAMS) != 0;
1082                         }
1083                 }
1084
1085                 public ParametersCompiled (Parameter [] parameters, bool has_arglist) :
1086                         this (parameters)
1087                 {
1088                         this.has_arglist = has_arglist;
1089                 }
1090                 
1091                 public static ParametersCompiled CreateFullyResolved (Parameter p, TypeSpec type)
1092                 {
1093                         return new ParametersCompiled (new Parameter [] { p }, new TypeSpec [] { type });
1094                 }
1095                 
1096                 public static ParametersCompiled CreateFullyResolved (Parameter[] parameters, TypeSpec[] types)
1097                 {
1098                         return new ParametersCompiled (parameters, types);
1099                 }
1100
1101                 //
1102                 // TODO: This does not fit here, it should go to different version of AParametersCollection
1103                 // as the underlying type is not Parameter and some methods will fail to cast
1104                 //
1105                 public static AParametersCollection CreateFullyResolved (params TypeSpec[] types)
1106                 {
1107                         var pd = new ParameterData [types.Length];
1108                         for (int i = 0; i < pd.Length; ++i)
1109                                 pd[i] = new ParameterData (null, Parameter.Modifier.NONE, null);
1110
1111                         return new ParametersCompiled (pd, types);
1112                 }
1113
1114                 public static ParametersCompiled CreateImplicitParameter (FullNamedExpression texpr, Location loc)
1115                 {
1116                         return new ParametersCompiled (
1117                                 new[] { new Parameter (texpr, "value", Parameter.Modifier.NONE, null, loc) },
1118                                 null);
1119                 }
1120
1121                 public void CheckConstraints (IMemberContext mc)
1122                 {
1123                         foreach (Parameter p in parameters) {
1124                                 //
1125                                 // It's null for compiler generated types or special types like __arglist
1126                                 //
1127                                 if (p.TypeExpression != null)
1128                                         ConstraintChecker.Check (mc, p.Type, p.TypeExpression.Location);
1129                         }
1130                 }
1131
1132                 //
1133                 // Returns non-zero value for equal CLS parameter signatures
1134                 //
1135                 public static int IsSameClsSignature (AParametersCollection a, AParametersCollection b)
1136                 {
1137                         int res = 0;
1138
1139                         for (int i = 0; i < a.Count; ++i) {
1140                                 var a_type = a.Types[i];
1141                                 var b_type = b.Types[i];
1142                                 if (TypeSpecComparer.Override.IsEqual (a_type, b_type)) {
1143                                         if ((a.FixedParameters[i].ModFlags & Parameter.Modifier.RefOutMask) != (b.FixedParameters[i].ModFlags & Parameter.Modifier.RefOutMask))
1144                                                 res |= 1;
1145
1146                                         continue;
1147                                 }
1148
1149                                 var ac_a = a_type as ArrayContainer;
1150                                 if (ac_a == null)
1151                                         return 0;
1152
1153                                 var ac_b = b_type as ArrayContainer;
1154                                 if (ac_b == null)
1155                                         return 0;
1156
1157                                 if (ac_a.Element is ArrayContainer || ac_b.Element is ArrayContainer) {
1158                                         res |= 2;
1159                                         continue;
1160                                 }
1161
1162                                 if (ac_a.Rank != ac_b.Rank && TypeSpecComparer.Override.IsEqual (ac_a.Element, ac_b.Element)) {
1163                                         res |= 1;
1164                                         continue;
1165                                 }
1166
1167                                 return 0;
1168                         }
1169
1170                         return res;
1171                 }
1172
1173                 public static ParametersCompiled MergeGenerated (CompilerContext ctx, ParametersCompiled userParams, bool checkConflicts, Parameter compilerParams, TypeSpec compilerTypes)
1174                 {
1175                         return MergeGenerated (ctx, userParams, checkConflicts,
1176                                 new Parameter [] { compilerParams },
1177                                 new TypeSpec [] { compilerTypes });
1178                 }
1179
1180                 //
1181                 // Use this method when you merge compiler generated parameters with user parameters
1182                 //
1183                 public static ParametersCompiled MergeGenerated (CompilerContext ctx, ParametersCompiled userParams, bool checkConflicts, Parameter[] compilerParams, TypeSpec[] compilerTypes)
1184                 {
1185                         Parameter[] all_params = new Parameter [userParams.Count + compilerParams.Length];
1186                         userParams.FixedParameters.CopyTo(all_params, 0);
1187
1188                         TypeSpec [] all_types;
1189                         if (userParams.types != null) {
1190                                 all_types = new TypeSpec [all_params.Length];
1191                                 userParams.Types.CopyTo (all_types, 0);
1192                         } else {
1193                                 all_types = null;
1194                         }
1195
1196                         int last_filled = userParams.Count;
1197                         int index = 0;
1198                         foreach (Parameter p in compilerParams) {
1199                                 for (int i = 0; i < last_filled; ++i) {
1200                                         while (p.Name == all_params [i].Name) {
1201                                                 if (checkConflicts && i < userParams.Count) {
1202                                                         ctx.Report.Error (316, userParams[i].Location,
1203                                                                 "The parameter name `{0}' conflicts with a compiler generated name", p.Name);
1204                                                 }
1205                                                 p.Name = '_' + p.Name;
1206                                         }
1207                                 }
1208                                 all_params [last_filled] = p;
1209                                 if (all_types != null)
1210                                         all_types [last_filled] = compilerTypes [index++];
1211                                 ++last_filled;
1212                         }
1213                         
1214                         ParametersCompiled parameters = new ParametersCompiled (all_params, all_types);
1215                         parameters.has_params = userParams.has_params;
1216                         return parameters;
1217                 }
1218
1219                 //
1220                 // Parameters checks for members which don't have a block
1221                 //
1222                 public void CheckParameters (MemberCore member)
1223                 {
1224                         for (int i = 0; i < parameters.Length; ++i) {
1225                                 var name = parameters[i].Name;
1226                                 for (int ii = i + 1; ii < parameters.Length; ++ii) {
1227                                         if (parameters[ii].Name == name)
1228                                                 this[ii].Error_DuplicateName (member.Compiler.Report);
1229                                 }
1230                         }
1231                 }
1232
1233                 public bool Resolve (IMemberContext ec)
1234                 {
1235                         if (types != null)
1236                                 return true;
1237                         
1238                         types = new TypeSpec [Count];
1239                         
1240                         bool ok = true;
1241                         Parameter p;
1242                         for (int i = 0; i < FixedParameters.Length; ++i) {
1243                                 p = this [i];
1244                                 TypeSpec t = p.Resolve (ec, i);
1245                                 if (t == null) {
1246                                         ok = false;
1247                                         continue;
1248                                 }
1249
1250                                 types [i] = t;
1251                         }
1252
1253                         return ok;
1254                 }
1255
1256                 public void ResolveDefaultValues (MemberCore m)
1257                 {
1258                         ResolveContext rc = null;
1259                         for (int i = 0; i < parameters.Length; ++i) {
1260                                 Parameter p = (Parameter) parameters [i];
1261
1262                                 //
1263                                 // Try not to enter default values resolution if there are is not any default value possible
1264                                 //
1265                                 if (p.HasDefaultValue || p.OptAttributes != null) {
1266                                         if (rc == null)
1267                                                 rc = new ResolveContext (m);
1268
1269                                         p.ResolveDefaultValue (rc);
1270                                 }
1271                         }
1272                 }
1273
1274                 // Define each type attribute (in/out/ref) and
1275                 // the argument names.
1276                 public void ApplyAttributes (IMemberContext mc, MethodBase builder)
1277                 {
1278                         if (Count == 0)
1279                                 return;
1280
1281                         MethodBuilder mb = builder as MethodBuilder;
1282                         ConstructorBuilder cb = builder as ConstructorBuilder;
1283                         var pa = mc.Module.PredefinedAttributes;
1284
1285                         for (int i = 0; i < Count; i++) {
1286                                 this [i].ApplyAttributes (mb, cb, i + 1, pa);
1287                         }
1288                 }
1289
1290                 public void VerifyClsCompliance (IMemberContext ctx)
1291                 {
1292                         foreach (Parameter p in FixedParameters)
1293                                 p.IsClsCompliant (ctx);
1294                 }
1295
1296                 public Parameter this [int pos] {
1297                         get { return (Parameter) parameters [pos]; }
1298                 }
1299
1300                 public Expression CreateExpressionTree (BlockContext ec, Location loc)
1301                 {
1302                         var initializers = new ArrayInitializer (Count, loc);
1303                         foreach (Parameter p in FixedParameters) {
1304                                 //
1305                                 // Each parameter expression is stored to local variable
1306                                 // to save some memory when referenced later.
1307                                 //
1308                                 StatementExpression se = new StatementExpression (p.CreateExpressionTreeVariable (ec), Location.Null);
1309                                 if (se.Resolve (ec)) {
1310                                         ec.CurrentBlock.AddScopeStatement (new TemporaryVariableReference.Declarator (p.ExpressionTreeVariableReference ()));
1311                                         ec.CurrentBlock.AddScopeStatement (se);
1312                                 }
1313                                 
1314                                 initializers.Add (p.ExpressionTreeVariableReference ());
1315                         }
1316
1317                         return new ArrayCreation (
1318                                 Parameter.ResolveParameterExpressionType (ec, loc),
1319                                 initializers, loc);
1320                 }
1321
1322                 public ParametersCompiled Clone ()
1323                 {
1324                         ParametersCompiled p = (ParametersCompiled) MemberwiseClone ();
1325
1326                         p.parameters = new IParameterData [parameters.Length];
1327                         for (int i = 0; i < Count; ++i)
1328                                 p.parameters [i] = this [i].Clone ();
1329
1330                         return p;
1331                 }
1332         }
1333
1334         //
1335         // Default parameter value expression. We need this wrapper to handle
1336         // default parameter values of folded constants (e.g. indexer parameters).
1337         // The expression is resolved only once but applied to two methods which
1338         // both share reference to this expression and we ensure that resolving
1339         // this expression always returns same instance
1340         //
1341         public class DefaultParameterValueExpression : CompositeExpression
1342         {
1343                 public DefaultParameterValueExpression (Expression expr)
1344                         : base (expr)
1345                 {
1346                 }
1347
1348                 public void Resolve (ResolveContext rc, Parameter p)
1349                 {
1350                         var expr = Resolve (rc);
1351                         if (expr == null) {
1352                                 this.expr = ErrorExpression.Instance;
1353                                 return;
1354                         }
1355
1356                         expr = Child;
1357
1358                         if (!(expr is Constant || expr is DefaultValueExpression || (expr is New && ((New) expr).IsDefaultStruct))) {
1359                                 rc.Report.Error (1736, Location,
1360                                         "The expression being assigned to optional parameter `{0}' must be a constant or default value",
1361                                         p.Name);
1362
1363                                 return;
1364                         }
1365
1366                         var parameter_type = p.Type;
1367                         if (type == parameter_type)
1368                                 return;
1369
1370                         var res = Convert.ImplicitConversionStandard (rc, expr, parameter_type, Location);
1371                         if (res != null) {
1372                                 if (parameter_type.IsNullableType && res is Nullable.Wrap) {
1373                                         Nullable.Wrap wrap = (Nullable.Wrap) res;
1374                                         res = wrap.Child;
1375                                         if (!(res is Constant)) {
1376                                                 rc.Report.Error (1770, Location,
1377                                                         "The expression being assigned to nullable optional parameter `{0}' must be default value",
1378                                                         p.Name);
1379                                                 return;
1380                                         }
1381                                 }
1382
1383                                 if (!expr.IsNull && TypeSpec.IsReferenceType (parameter_type) && parameter_type.BuiltinType != BuiltinTypeSpec.Type.String) {
1384                                         rc.Report.Error (1763, Location,
1385                                                 "Optional parameter `{0}' of type `{1}' can only be initialized with `null'",
1386                                                 p.Name, parameter_type.GetSignatureForError ());
1387
1388                                         return;
1389                                 }
1390
1391                                 this.expr = res;
1392                                 return;
1393                         }
1394
1395                         rc.Report.Error (1750, Location,
1396                                 "Optional parameter expression of type `{0}' cannot be converted to parameter type `{1}'",
1397                                 type.GetSignatureForError (), parameter_type.GetSignatureForError ());
1398
1399                         this.expr = ErrorExpression.Instance;
1400                 }
1401                 
1402                 public override object Accept (StructuralVisitor visitor)
1403                 {
1404                         return visitor.Visit (this);
1405                 }
1406         }
1407 }