Esempio n. 1
0
        //
        // For expressions like int[] foo = new int[] { 1, 2, 3 };
        //
        public ArrayCreation(FullNamedExpression requested_base_type, ComposedTypeSpecifier rank, ArrayInitializer initializers, Location loc)
        {
            this.requested_base_type = requested_base_type;
            this.rank         = rank;
            this.initializers = initializers;
            this.loc          = loc;

            if (rank != null)
            {
                num_arguments = rank.Dimension;
            }
        }
Esempio n. 2
0
 //
 // For compiler generated single dimensional arrays only
 //
 public ArrayCreation(IType arrayType, IList <Expression> sizeArguments, IList <Expression> initializerElements)
     : base(arrayType)
 {
     if (sizeArguments == null)
     {
         throw new ArgumentNullException("sizeArguments");
     }
     arguments         = new List <Expression>(sizeArguments);
     num_arguments     = arguments.Count;
     this.initializers = new ArrayInitializer(initializerElements, Location.Null);
     ResolvedType      = arrayType;
     _resolved         = true;
 }
Esempio n. 3
0
 //
 // For expressions like int[] foo = { 1, 2, 3 };
 //
 public ArrayCreation(FullNamedExpression requested_base_type, ArrayInitializer initializers)
     : this(requested_base_type, null, initializers, initializers.Location)
 {
 }
Esempio n. 4
0
 //
 // For compiler generated single dimensional arrays only
 //
 public ArrayCreation(FullNamedExpression requested_base_type, ArrayInitializer initializers, Location loc)
     : this(requested_base_type, ComposedTypeSpecifier.SingleDimension, initializers, loc)
 {
 }
Esempio n. 5
0
 public ArrayCreation(FullNamedExpression requested_base_type, List <Expression> exprs, ComposedTypeSpecifier rank, ArrayInitializer initializers, Location l)
     : this(requested_base_type, rank, initializers, l)
 {
     arguments     = new List <Expression>(exprs);
     num_arguments = arguments.Count;
 }
Esempio n. 6
0
        bool CheckIndices(ResolveContext ec, ArrayInitializer probe, int idx, bool specified_dims, int child_bounds)
        {
            if (initializers != null && bounds == null)
            {
                //
                // We use this to store all the data values in the order in which we
                // will need to store them in the byte blob later
                //
                array_data = new List <Expression>(probe.Count);
                bounds     = new Dictionary <int, int>();
            }

            if (specified_dims)
            {
                Expression a = arguments[idx];
                a = a.DoResolve(ec);
                if (a == null)
                {
                    return(false);
                }

                a = ConvertExpressionToArrayIndex(ec, a);
                if (a == null)
                {
                    return(false);
                }

                arguments[idx] = a;

                if (initializers != null)
                {
                    Constant c = a as Constant;
                    if (c == null && a is CastExpression)
                    {
                        c = ((CastExpression)a).Expr as Constant;
                    }

                    if (c == null)
                    {
                        ec.Report.Error(0, a.Location, "A constant value is expected");
                        return(false);
                    }

                    int value;
                    try
                    {
                        value = System.Convert.ToInt32(c.GetValue());
                    }
                    catch
                    {
                        ec.Report.Error(0, a.Location, "A constant value is expected");
                        return(false);
                    }

                    // TODO: probe.Count does not fit ulong in
                    if (value != probe.Count)
                    {
                        ec.Report.Error(0, loc, "An array initializer of length `{0}' was expected", value.ToString());
                        return(false);
                    }

                    bounds[idx] = value;
                }
            }

            if (initializers == null)
            {
                return(true);
            }

            for (int i = 0; i < probe.Count; ++i)
            {
                var o = probe[i];
                if (o is ArrayInitializer)
                {
                    var sub_probe = o as ArrayInitializer;
                    if (idx + 1 >= dimensions)
                    {
                        ec.Report.Error(0, loc, "Array initializers can only be used in a variable or field initializer. Try using a new expression instead");
                        return(false);
                    }

                    // When we don't have explicitly specified dimensions, record whatever dimension we first encounter at each level
                    if (!bounds.ContainsKey(idx + 1))
                    {
                        bounds[idx + 1] = sub_probe.Count;
                    }

                    if (bounds[idx + 1] != sub_probe.Count)
                    {
                        ec.Report.Error(0, sub_probe.Location, "An array initializer of length `{0}' was expected", bounds[idx + 1].ToString());
                        return(false);
                    }

                    bool ret = CheckIndices(ec, sub_probe, idx + 1, specified_dims, child_bounds - 1);
                    if (!ret)
                    {
                        return(false);
                    }
                }
                else if (child_bounds > 1)
                {
                    ec.Report.Error(0, o.Location, "A nested array initializer was expected");
                }
                else
                {
                    Expression element = ResolveArrayElement(ec, o);
                    if (element == null)
                    {
                        continue;
                    }
                    array_data.Add(element);
                }
            }

            return(true);
        }
 public ImplicitlyTypedArrayCreation(ComposedTypeSpecifier rank, ArrayInitializer initializers, Location loc)
     : base(null, rank, initializers, loc)
 {
 }
 public ImplicitlyTypedArrayCreation(ArrayInitializer initializers, Location loc)
     : base(null, initializers, loc)
 {
 }