Exemple #1
0
        public override void Dump(SourceWriter sw, int indentChange)
        {
            sw.Write("new");

            if (BaseType != null)
            {
                sw.Write(" ");
                DumpChild(BaseType, sw);
            }

            sw.Write("[");

            var i = 0;

            foreach (var dimension in _dimensions)
            {
                if (i++ != 0)
                {
                    sw.Write(", ");
                }
                DumpChild(dimension, sw);
            }

            sw.Write("]");

            if (RankSpecifier != null)
            {
                sw.Write(RankSpecifier.Substring(2));
            }

            if (_initializer == null)
            {
                return;
            }

            sw.Write(" ");
            DumpChild(_initializer, sw);
        }
Exemple #2
0
        private bool CheckIndices(ParseContext ec, IList <Expression> probe, int idx, bool explicitDimensions, int childBounds)
        {
            if (explicitDimensions)
            {
                var a = _dimensions[idx];
                a = a.Resolve(ec);
                if (a == null)
                {
                    return(false);
                }

                var c = a as ConstantExpression;
                if (c != null)
                {
                    c = c.ConvertImplicitly(TypeManager.CoreTypes.Int32);
                }

                if (c == null)
                {
                    ec.ReportError(
                        150,
                        "A constant value is expected.",
                        a.Span);
                    return(false);
                }

                var value = (int)c.Value;
                if (value != probe.Count)
                {
                    ec.ReportError(
                        847,
                        string.Format("An array initializer of length '{0}' was expected.", value),
                        Span);

                    return(false);
                }

                //bounds[idx] = value;
            }

            for (var i = 0; i < probe.Count; ++i)
            {
                var o        = probe[i];
                var subProbe = o as ArrayInitializerExpression;
                if (subProbe != null)
                {
                    if (idx + 1 >= _resolvedDimensions)
                    {
                        ec.ReportError(
                            623,
                            "Array initializers can only be used in a variable or field initializer.  Try using a new expression instead.",
                            subProbe.Span);

                        return(false);
                    }

                    var subProbeCheck = CheckIndices(ec, subProbe.Values, idx + 1, explicitDimensions, childBounds - 1);
                    if (!subProbeCheck)
                    {
                        return(false);
                    }

                    probe[i] = new ArrayCreationExpression
                    {
                        _baseType     = _baseType,
                        _initializer  = subProbe,
                        RankSpecifier = RankSpecifier.Substring(RankSpecifier.IndexOf(']') + 1),
                        Span          = subProbe.Span
                    }.Resolve(ec);
                }
                else if (childBounds > 1)
                {
                    ec.ReportError(
                        846,
                        "A nested array initializer was expected.",
                        o.Span);
                }
                else
                {
                    var element = ResolveArrayElement(ec, o);
                    if (element == null)
                    {
                        continue;
                    }

                    // Initializers with the default values can be ignored
                    var c = element as ConstantExpression;
                    if (c != null)
                    {
                        //if (c.IsDefaultInitializer(_resolvedElementType))
                        //{
                        //    element = null;
                        //}
                    }

                    _arrayData.Add(element);
                }
            }

            return(true);
        }