CountArrayDimensions() public static method

Returns the rank of an array symbol when IsArrayKeyword is true, or 0 if the symbol does not represent an array type.
public static CountArrayDimensions ( Symbol s ) : int
s Symbol
return int
Exemplo n.º 1
0
        int CountDimensionsIfArrayType(LNode type)
        {
            LNode dimsNode;

            if (type.Calls(S.Of, 2) && (dimsNode = type.Args[0]).IsId)
            {
                return(S.CountArrayDimensions(dimsNode.Name));
            }
            return(0);
        }
Exemplo n.º 2
0
        public bool AutoPrintNewOperator(Precedence precedence, Precedence context, Ambiguity flags)
        {
            // Prints the new Xyz(...) {...} operator
            Debug.Assert(_n.Name == S.New);
            int argCount = _n.ArgCount;

            if (argCount == 0)
            {
                return(false);
            }
            bool needParens;

            Debug.Assert(CanAppearIn(precedence, context, out needParens) && !needParens);

            LNode cons     = _n.Args[0];
            LNode type     = cons.Target;
            var   consArgs = cons.Args;

            // There are two basic uses of new: for objects, and for arrays.
            // In all cases, #new has 1 arg plus optional initializer arguments,
            // and there's always a list of "constructor args" even if it is empty
            // (exception: new {...}).
            // 1. Init an object: 1a. new Foo<Bar>() { ... }  <=> #new(Foo<bar>(...), ...)
            //                    1b. new { ... }             <=> #new(@``, ...)
            // 2. Init an array:  2a. new int[] { ... },      <=> #new(int[](), ...) <=> #new(#of(@`[]`, int)(), ...)
            //                    2b. new[,] { ... }.         <=> #new(@`[,]`(), ...)
            //                    2c. new int[10,10] { ... }, <=> #new(#of(@`[,]`, int)(10,10), ...)
            //                    2d. new int[10][] { ... },  <=> #new(#of(@`[]`, #of(@`[]`, int))(10), ...)
            if (HasPAttrs(cons))
            {
                return(false);
            }
            if (type == null ? !cons.IsIdNamed(S.Missing) : HasPAttrs(type) || !IsComplexIdentifier(type))
            {
                return(false);
            }

            // Okay, we can now be sure that it's printable, but is it an array decl?
            if (type == null)
            {
                // 1b, new {...}
                _out.Write("new ", true);
                PrintBracedBlockInNewExpr();
            }
            else if (type != null && type.IsId && S.CountArrayDimensions(type.Name) > 0)                 // 2b
            {
                _out.Write("new", true);
                _out.Write(type.Name.Name, true);
                Space(SpaceOpt.Default);
                PrintBracedBlockInNewExpr();
            }
            else
            {
                _out.Write("new ", true);
                int dims = CountDimensionsIfArrayType(type);
                if (dims > 0 && cons.Args.Count == dims)
                {
                    PrintTypeWithArraySizes(cons);
                }
                else
                {
                    // Otherwise we can print the type name without caring if it's an array or not.
                    PrintType(type, EP.Primary.LeftContext(context));
                    if (cons.ArgCount != 0 || (argCount == 1 && dims == 0))
                    {
                        PrintArgList(cons, ParenFor.MethodCall, cons.ArgCount, 0, OmitMissingArguments);
                    }
                }
                if (_n.Args.Count > 1)
                {
                    PrintBracedBlockInNewExpr();
                }
            }
            return(true);
        }