2009-06-17 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / parameter.cs
index a16f19bd98c562292ba816f01815fb17cbdbd2d9..a6c5c63da7c0b41811e27bc1af73a1f09de97f86 100644 (file)
@@ -217,7 +217,9 @@ namespace Mono.CSharp {
 
        public interface IParameterData
        {
+               Expression DefaultValue { get; }
                bool HasExtensionMethodModifier { get; }
+               bool HasDefaultValue { get; }
                Parameter.Modifier ModFlags { get; }
                string Name { get; }
        }
@@ -245,6 +247,7 @@ namespace Mono.CSharp {
                protected FullNamedExpression TypeName;
                readonly Modifier modFlags;
                string name;
+               Expression default_expr;
                protected Type parameter_type;
                public readonly Location Location;
                int idx;
@@ -291,7 +294,13 @@ namespace Mono.CSharp {
                                Report.Warning (3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
                        }
 
-                       // TypeManager.default_parameter_value_attribute_type is null if !NET_2_0, or if System.dll is not referenced
+                       if (HasDefaultValue && (a.Type == pa.DefaultParameterValue || a.Type == pa.OptionalParameter)) {
+                               Report.Error (1745, a.Location,
+                                       "Cannot specify `{0}' attribute on optional parameter `{1}'",
+                                       TypeManager.CSharpName (a.Type).Replace ("Attribute", ""), Name);
+                               return;
+                       }
+
                        if (a.Type == pa.DefaultParameterValue) {
                                object val = a.GetParameterDefaultValue ();
                                if (val != null) {
@@ -301,10 +310,10 @@ namespace Mono.CSharp {
                                                        if (!t.IsArray)
                                                                t = TypeManager.type_type;
 
-                                                       Report.Error (1910, a.Location, "Argument of type `{0}' is not applicable for the DefaultValue attribute",
+                                                       Report.Error (1910, a.Location, "Argument of type `{0}' is not applicable for the DefaultParameterValue attribute",
                                                                TypeManager.CSharpName (t));
                                                } else {
-                                                       Report.Error (1909, a.Location, "The DefaultValue attribute is not applicable on parameters of type `{0}'",
+                                                       Report.Error (1909, a.Location, "The DefaultParameterValue attribute is not applicable on parameters of type `{0}'",
                                                                TypeManager.CSharpName (parameter_type)); ;
                                                }
                                                return;
@@ -340,20 +349,54 @@ namespace Mono.CSharp {
                // <summary>
                //   Resolve is used in method definitions
                // </summary>
-               public virtual Type Resolve (IResolveContext ec)
+               public virtual Type Resolve (IResolveContext rc)
                {
                        // HACK: to resolve attributes correctly
-                       this.resolve_context = ec;
+                       this.resolve_context = rc;
 
                        if (parameter_type != null)
                                return parameter_type;
 
-                       TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec, false);
+                       TypeExpr texpr = TypeName.ResolveAsTypeTerminal (rc, false);
                        if (texpr == null)
                                return null;
 
                        parameter_type = texpr.Type;
 
+                       // Ignore all checks for dummy members
+                       AbstractPropertyEventMethod pem = rc as AbstractPropertyEventMethod;
+                       if (pem != null && pem.IsDummy)
+                               return parameter_type;
+
+                       if (default_expr != null) {
+                               EmitContext ec = new EmitContext (rc, rc.GenericDeclContainer, Location, null, parameter_type, 0);
+                               default_expr = default_expr.Resolve (ec);
+                               if (default_expr != null) {
+                                       Constant value = default_expr as Constant;
+                                       if (value == null) {
+                                               if (default_expr != null && !(default_expr is DefaultValueExpression)) {
+                                                       Report.Error (1736, default_expr.Location, "The expression being assigned to optional parameter `{0}' must be constant",
+                                                               Name);
+                                                       default_expr = null;
+                                               }
+                                       } else {
+                                               Constant c = value.ConvertImplicitly (parameter_type);
+                                               if (c == null) {
+                                                       if (parameter_type == TypeManager.object_type) {
+                                                               Report.Error (1763, Location,
+                                                                       "Optional parameter `{0}' of type `{1}' can only be initialized with `null'",
+                                                                       Name, GetSignatureForError ());
+                                                       } else {
+                                                               Report.Error (1750, Location,
+                                                                       "Optional parameter value `{0}' cannot be converted to parameter type `{1}'",
+                                                                       value.GetValue (), GetSignatureForError ());
+                                                       }
+                                                       default_expr = null;
+                                               }
+                                       }
+                               }
+                       }
+
                        if ((modFlags & Parameter.Modifier.ISBYREF) != 0 &&
                                TypeManager.IsSpecialType (parameter_type)) {
                                Report.Error (1601, Location, "Method or delegate parameter cannot be of type `{0}'",
@@ -363,7 +406,7 @@ namespace Mono.CSharp {
 
                        TypeManager.CheckTypeVariance (parameter_type,
                                (modFlags & Parameter.Modifier.ISBYREF) != 0 ? Variance.None : Variance.Contravariant,
-                               ec as MemberCore);
+                               rc as MemberCore);
 
                        if (texpr is TypeParameterExpr)
                                return parameter_type;
@@ -387,6 +430,10 @@ namespace Mono.CSharp {
                        this.idx = idx;
                }
 
+               public bool HasDefaultValue {
+                       get { return default_expr != null; }
+               }
+
                public bool HasExtensionMethodModifier {
                        get { return (modFlags & Modifier.This) != 0; }
                }
@@ -401,7 +448,8 @@ namespace Mono.CSharp {
                }
 
                ParameterAttributes Attributes {
-                       get { return ParametersCompiled.GetParameterAttribute (modFlags); }
+                       get { return ParametersCompiled.GetParameterAttribute (modFlags) |
+                               (HasDefaultValue ? ParameterAttributes.Optional : ParameterAttributes.None); }
                }
 
                public override AttributeTargets AttributeTargets {
@@ -458,6 +506,21 @@ namespace Mono.CSharp {
 
                        if (OptAttributes != null)
                                OptAttributes.Emit ();
+
+                       if (HasDefaultValue) {
+                               //
+                               // Emit constant values for true constants only, the other
+                               // constant-like expressions will rely on default value expression
+                               //
+                               Constant c = default_expr as Constant;
+                               if (c != null) {
+                                       if (default_expr.Type == TypeManager.decimal_type) {
+                                               builder.SetCustomAttribute (Const.CreateDecimalConstantAttribute (c));
+                                       } else {
+                                               builder.SetConstant (c.GetValue ());
+                                       }
+                               }
+                       }
                }
 
                public override string[] ValidAttributeTargets {
@@ -502,6 +565,11 @@ namespace Mono.CSharp {
                                Expression.CreateExpressionFactoryCall ("Parameter", null, arguments, Location));
                }
 
+               public Expression DefaultValue {
+                       get { return default_expr; }
+                       set { default_expr = value; }
+               }
+
                public void Emit (EmitContext ec)
                {
                        int arg_idx = idx;
@@ -565,6 +633,13 @@ namespace Mono.CSharp {
 
                        return parameter_expr_tree_type;
                }
+
+               public void Warning_UselessOptionalParameter ()
+               {
+                       Report.Warning (1066, 1, Location,
+                               "The default value specified for optional parameter `{0}' will never be used",
+                               Name);
+               }
        }
 
        //
@@ -574,6 +649,7 @@ namespace Mono.CSharp {
        {
                readonly string name;
                readonly Parameter.Modifier modifiers;
+               readonly Expression default_value;
 
                public ParameterData (string name, Parameter.Modifier modifiers)
                {
@@ -581,12 +657,26 @@ namespace Mono.CSharp {
                        this.modifiers = modifiers;
                }
 
+               public ParameterData (string name, Parameter.Modifier modifiers, Expression defaultValue)
+                       : this (name, modifiers)
+               {
+                       this.default_value = defaultValue;
+               }
+
                #region IParameterData Members
 
+               public Expression DefaultValue {
+                       get { return default_value; }
+               }
+
                public bool HasExtensionMethodModifier {
                        get { return (modifiers & Parameter.Modifier.This) != 0; }
                }
 
+               public bool HasDefaultValue {
+                       get { return default_value != null; }
+               }
+
                public Parameter.Modifier ModFlags {
                        get { return modifiers & ~Parameter.Modifier.This; }
                }
@@ -830,6 +920,7 @@ namespace Mono.CSharp {
 
                                ParameterInfo p = pi [i];
                                Parameter.Modifier mod = 0;
+                               Expression default_value = null;
                                if (types [i].IsByRef) {
                                        if ((p.Attributes & (ParameterAttributes.Out | ParameterAttributes.In)) == ParameterAttributes.Out)
                                                mod = Parameter.Modifier.OUT;
@@ -844,14 +935,28 @@ namespace Mono.CSharp {
                                (method.DeclaringType.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute &&
                                        method.IsDefined (extension_attr.Type, false)) {
                                        mod = Parameter.Modifier.This;
-                               } else if (i >= pi.Length - 2 && types [i].IsArray) {
-                                       if (p.IsDefined (param_attr.Type, false)) {
-                                               mod = Parameter.Modifier.PARAMS;
-                                               is_params = true;
+                               } else {
+                                       if (i >= pi.Length - 2) {
+                                               if (types[i].IsArray) {
+                                                       if (p.IsDefined (param_attr.Type, false)) {
+                                                               mod = Parameter.Modifier.PARAMS;
+                                                               is_params = true;
+                                                       }
+                                               } else if (types[i] == TypeManager.runtime_argument_handle_type) {
+                                                       par[i] = new ArglistParameter (Location.Null);
+                                                       continue;
+                                               }
+                                       }
+
+                                       if (!is_params && p.IsOptional) {
+                                               if (p.DefaultValue == Missing.Value)
+                                                       default_value = EmptyExpression.Null;
+                                               else
+                                                       default_value = Constant.CreateConstant (types[i], p.DefaultValue, Location.Null);
                                        }
                                }
 
-                               par [i] = new ParameterData (p.Name, mod);
+                               par [i] = new ParameterData (p.Name, mod, default_value);
                        }
 
                        return method != null ?