Expression ResolveDefaultExpression (ResolveContext rc)
{
default_expr = default_expr.Resolve (rc);
if (default_expr == null)
return null;
if (!(default_expr is Constant || default_expr is DefaultValueExpression || (default_expr is New && ((New)default_expr).IsDefaultStruct))) {
rc.Compiler.Report.Error (1736, default_expr.Location,
"The expression being assigned to optional parameter `{0}' must be a constant or default value",
Name);
return null;
}
if (default_expr.Type == parameter_type)
return default_expr;
var res = Convert.ImplicitConversionStandard (rc, default_expr, parameter_type, default_expr.Location);
if (res != null) {
if (TypeManager.IsNullableType (parameter_type) && res is Nullable.Wrap) {
Nullable.Wrap wrap = (Nullable.Wrap) res;
res = wrap.Child;
if (!(res is Constant)) {
rc.Compiler.Report.Error (1770, default_expr.Location,
"The expression being assigned to nullable optional parameter `{0}' must be default value",
Name);
return null;
}
}
if (!default_expr.IsNull && TypeManager.IsReferenceType (parameter_type) && parameter_type != TypeManager.string_type) {
rc.Compiler.Report.Error (1763, default_expr.Location,
"Optional parameter `{0}' of type `{1}' can only be initialized with `null'",
Name, GetSignatureForError ());
return null;
}
return res;
}
rc.Compiler.Report.Error (1750, Location,
"Optional parameter expression of type `{0}' cannot be converted to parameter type `{1}'",
TypeManager.CSharpName (default_expr.Type), GetSignatureForError ());
return null;
}