Example #1
0
        public override Expression DoResolve(ResolveContext ec)
        {
            //
            // It's null when lifting non-nullable type
            //
            if (unwrap == null)
            {
                // S -> T? is wrap only
                if (TypeManager.IsNullableType(type))
                {
                    return(Wrap.Create(expr, type));
                }

                // S -> T can be simplified
                return(expr);
            }

            // Wrap target for T?
            if (TypeManager.IsNullableType(type))
            {
                expr = Wrap.Create(expr, type);
                if (expr == null)
                {
                    return(null);
                }

                null_value = LiftedNull.Create(type, loc);
            }
            else
            {
                null_value = new NullLiteral(type, loc);
            }

            eclass = ExprClass.Value;
            return(this);
        }
        public void Resolve(ResolveContext rc, Parameter p)
        {
            var expr = Resolve(rc);

            if (expr == null)
            {
                return;
            }

            expr = Child;

            if (!(expr is Constant || expr is DefaultValueExpression || (expr is New && ((New)expr).IsDefaultStruct)))
            {
                rc.Report.Error(1736, Location,
                                "The expression being assigned to optional parameter `{0}' must be a constant or default value",
                                p.Name);

                return;
            }

            var parameter_type = p.Type;

            if (type == parameter_type)
            {
                return;
            }

            var res = Convert.ImplicitConversionStandard(rc, expr, parameter_type, Location);

            if (res != null)
            {
                if (parameter_type.IsNullableType && res is Nullable.Wrap)
                {
                    Nullable.Wrap wrap = (Nullable.Wrap)res;
                    res = wrap.Child;
                    if (!(res is Constant))
                    {
                        rc.Report.Error(1770, Location,
                                        "The expression being assigned to nullable optional parameter `{0}' must be default value",
                                        p.Name);
                        return;
                    }
                }

                if (!expr.IsNull && TypeSpec.IsReferenceType(parameter_type) && parameter_type.BuiltinType != BuiltinTypeSpec.Type.String)
                {
                    rc.Report.Error(1763, Location,
                                    "Optional parameter `{0}' of type `{1}' can only be initialized with `null'",
                                    p.Name, parameter_type.GetSignatureForError());

                    return;
                }

                this.expr = res;
                return;
            }

            rc.Report.Error(1750, Location,
                            "Optional parameter expression of type `{0}' cannot be converted to parameter type `{1}'",
                            type.GetSignatureForError(), parameter_type.GetSignatureForError());
        }