コード例 #1
0
        public override Constant ConvertInitializer(ResolveContext rc, Constant expr)
        {
            if (expr is EnumConstant)
            {
                expr = ((EnumConstant)expr).Child;
            }

            var underlying = ((Enum)Parent).UnderlyingType;

            if (expr != null)
            {
                expr = expr.ImplicitConversionRequired(rc, underlying, Location);
                if (expr != null && !IsValidEnumType(expr.Type))
                {
                    Enum.Error_1008(Location, Report);
                    expr = null;
                }
            }

            if (expr == null)
            {
                expr = New.Constantify(underlying, Location);
            }

            return(new EnumConstant(expr, MemberType));
        }
コード例 #2
0
        protected virtual Expression DoResolveInitializer(ResolveContext rc)
        {
            if (in_transit)
            {
                field.Compiler.Report.Error(110, expr.Location,
                                            "The evaluation of the constant value for `{0}' involves a circular definition",
                                            GetSignatureForError());

                expr = null;
            }
            else
            {
                in_transit = true;
                expr       = expr.Resolve(rc);
            }

            in_transit = false;

            if (expr != null)
            {
                Constant c = expr as Constant;
                if (c != null)
                {
                    c = field.ConvertInitializer(rc, c);
                }

                if (c == null)
                {
                    if (TypeSpec.IsReferenceType(field.MemberType))
                    {
                        Error_ConstantCanBeInitializedWithNullOnly(rc, field.MemberType, expr.Location, GetSignatureForError());
                    }
                    else if (!(expr is Constant))
                    {
                        Error_ExpressionMustBeConstant(rc, expr.Location, GetSignatureForError());
                    }
                    else
                    {
                        expr.Error_ValueCannotBeConverted(rc, field.MemberType, false);
                    }
                }

                expr = c;
            }

            if (expr == null)
            {
                expr = New.Constantify(field.MemberType, Location);
                if (expr == null)
                {
                    expr = Constant.CreateConstantFromValue(field.MemberType, null, Location);
                }
                expr = expr.Resolve(rc);
            }

            return(expr);
        }
コード例 #3
0
            protected override Expression DoResolve(ResolveContext rc)
            {
                // We are the first member
                if (prev == null)
                {
                    return(New.Constantify(current.Parent.Definition, Location));
                }

                var c = ((ConstSpec)prev.Spec).GetConstant(rc) as EnumConstant;

                try {
                    return(c.Increment());
                } catch (OverflowException) {
                    rc.Report.Error(543, current.Location,
                                    "The enumerator value `{0}' is outside the range of enumerator underlying type `{1}'",
                                    current.GetSignatureForError(), ((Enum)current.Parent).UnderlyingType.GetSignatureForError());

                    return(New.Constantify(current.Parent.Definition, current.Location));
                }
            }