Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / mcs / parameter.cs
index 365f876f0aa4f318f21cd34f114b7a1703dd8d66..da2b13e4ab69e7fd6949e65c4f065eced3f065e7 100644 (file)
@@ -338,7 +338,7 @@ namespace Mono.CSharp {
                                if (HasOptionalExpression) {
                                        a.Report.Error (1745, a.Location,
                                                "Cannot specify `{0}' attribute on optional parameter `{1}'",
-                                               TypeManager.CSharpName (a.Type).Replace ("Attribute", ""), Name);
+                                               a.Type.GetSignatureForError ().Replace ("Attribute", ""), Name);
                                }
 
                                if (a.Type == pa.DefaultParameterValue)
@@ -394,7 +394,7 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       TypeManager.CheckTypeVariance (parameter_type,
+                       VarianceDecl.CheckTypeVariance (parameter_type,
                                (modFlags & Parameter.Modifier.RefOutMask) != 0 ? Variance.None : Variance.Contravariant,
                                rc);
 
@@ -406,7 +406,7 @@ namespace Mono.CSharp {
 
                        if ((modFlags & Modifier.This) != 0 && (parameter_type.IsPointer || parameter_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)) {
                                rc.Module.Compiler.Report.Error (1103, Location, "The extension method cannot be of type `{0}'",
-                                       TypeManager.CSharpName (parameter_type));
+                                       parameter_type.GetSignatureForError ());
                        }
 
                        return parameter_type;
@@ -418,7 +418,7 @@ namespace Mono.CSharp {
                        TypeSpec caller_type;
 
                        foreach (var attr in attributes.Attrs) {
-                               var atype = attr.ResolveType ();
+                               var atype = attr.ResolveTypeForComparison ();
                                if (atype == null)
                                        continue;
 
@@ -501,7 +501,7 @@ namespace Mono.CSharp {
                                        } else {
                                                rc.Report.Error (1909, default_expr.Location,
                                                        "The DefaultParameterValue attribute is not applicable on parameters of type `{0}'",
-                                                       default_expr.Type.GetSignatureForError ()); ;
+                                                       default_expr.Type.GetSignatureForError ());
                                        }
 
                                        default_expr = null;
@@ -582,7 +582,7 @@ namespace Mono.CSharp {
                {
                        string type_name;
                        if (parameter_type != null)
-                               type_name = TypeManager.CSharpName (parameter_type);
+                               type_name = parameter_type.GetSignatureForError ();
                        else
                                type_name = texpr.GetSignatureForError ();
 
@@ -635,7 +635,7 @@ namespace Mono.CSharp {
                        if (OptAttributes != null)
                                OptAttributes.Emit ();
 
-                       if (HasDefaultValue) {
+                       if (HasDefaultValue && default_expr.Type != null) {
                                //
                                // Emit constant values for true constants only, the other
                                // constant-like expressions will rely on default value expression
@@ -932,9 +932,14 @@ namespace Mono.CSharp {
                                        if (inflated_param == types[i])
                                                continue;
 
-                                       default_value |= FixedParameters[i] is DefaultValueExpression;
+                                       default_value |= FixedParameters[i].HasDefaultValue;
                                        inflated_types = new TypeSpec[types.Length];
-                                       Array.Copy (types, inflated_types, types.Length);       
+                                       Array.Copy (types, inflated_types, types.Length);
+                               } else {
+                                       if (inflated_param == types[i])
+                                               continue;
+
+                                       default_value |= FixedParameters[i].HasDefaultValue;
                                }
 
                                inflated_types[i] = inflated_param;
@@ -945,13 +950,42 @@ namespace Mono.CSharp {
 
                        var clone = (AParametersCollection) MemberwiseClone ();
                        clone.types = inflated_types;
+
+                       //
+                       // Default expression is original expression from the parameter
+                       // declaration context which can be of nested enum in generic class type.
+                       // In such case we end up with expression type of G<T>.E and e.g. parameter
+                       // type of G<int>.E and conversion would fail without inflate in this
+                       // context.
+                       //
                        if (default_value) {
+                               clone.parameters = new IParameterData[Count];
                                for (int i = 0; i < Count; ++i) {
-                                       var dve = clone.FixedParameters[i] as DefaultValueExpression;
-                                       if (dve != null) {
-                                               throw new NotImplementedException ("net");
-                                               //      clone.FixedParameters [i].DefaultValue = new DefaultValueExpression ();
-                                       }
+                                       var fp = FixedParameters[i];
+                                       clone.FixedParameters[i] = fp;
+
+                                       if (!fp.HasDefaultValue)
+                                               continue;
+
+                                       var expr = fp.DefaultValue;
+
+                                       if (inflated_types[i] == expr.Type)
+                                               continue;
+
+                                       var c = expr as Constant;
+                                       if (c != null) {
+                                               //
+                                               // It may fail we are inflating before type validation is done
+                                               //
+                                               c = Constant.ExtractConstantFromValue (inflated_types[i], c.GetValue (), expr.Location);
+                                               if (c == null)
+                                                       expr = new DefaultValueExpression (new TypeExpression (inflated_types[i], expr.Location), expr.Location);
+                                               else
+                                                       expr = c;
+                                       } else if (expr is DefaultValueExpression)
+                                               expr = new DefaultValueExpression (new TypeExpression (inflated_types[i], expr.Location), expr.Location);
+
+                                       clone.FixedParameters[i] = new ParameterData (fp.Name, fp.ModFlags, expr);
                                }
                        }
 
@@ -963,11 +997,11 @@ namespace Mono.CSharp {
                        if (types == null || types [pos] == null)
                                return ((Parameter)FixedParameters [pos]).GetSignatureForError ();
 
-                       string type = TypeManager.CSharpName (types [pos]);
+                       string type = types [pos].GetSignatureForError ();
                        if (FixedParameters [pos].HasExtensionMethodModifier)
                                return "this " + type;
 
-                       Parameter.Modifier mod = FixedParameters [pos].ModFlags;
+                       var mod = FixedParameters[pos].ModFlags & Parameter.Modifier.ModifierMask;
                        if (mod == 0)
                                return type;
 
@@ -1299,16 +1333,13 @@ namespace Mono.CSharp {
                {
                }
 
-               protected override Expression DoResolve (ResolveContext rc)
-               {
-                       return base.DoResolve (rc);
-               }
-
                public void Resolve (ResolveContext rc, Parameter p)
                {
                        var expr = Resolve (rc);
-                       if (expr == null)
+                       if (expr == null) {
+                               this.expr = ErrorExpression.Instance;
                                return;
+                       }
 
                        expr = Child;
 
@@ -1352,6 +1383,8 @@ namespace Mono.CSharp {
                        rc.Report.Error (1750, Location,
                                "Optional parameter expression of type `{0}' cannot be converted to parameter type `{1}'",
                                type.GetSignatureForError (), parameter_type.GetSignatureForError ());
+
+                       this.expr = ErrorExpression.Instance;
                }
                
                public override object Accept (StructuralVisitor visitor)