Beispiel #1
0
        internal EXPR BindArrayIndexCore(BindingFlag bindFlags, EXPR pOp1, EXPR pOp2)
        {
            EXPR pExpr;
            bool bIsError = false;
            if (!pOp1.isOK() || !pOp2.isOK())
            {
                bIsError = true;
            }

            CType pIntType = GetReqPDT(PredefinedType.PT_INT);

            // Array indexing must occur on an array type.
            if (!pOp1.type.IsArrayType())
            {
                Debug.Assert(!pOp1.type.IsPointerType());
                pExpr = bindIndexer(pOp1, pOp2, bindFlags);
                if (bIsError)
                {
                    pExpr.SetError();
                }
                return pExpr;
            }
            ArrayType pArrayType = pOp1.type.AsArrayType();
            checkUnsafe(pArrayType.GetElementType()); // added to the binder so we don't bind to pointer ops
            // Check the rank of the array against the number of indices provided, and
            // convert the indexes to ints

            CType pDestType = chooseArrayIndexType(pOp2);

            if (null == pDestType)
            {
                // using int as the type will allow us to give a better error...
                pDestType = pIntType;
            }

            int rank = pArrayType.rank;
            int cIndices = 0;

            EXPR transformedIndices = pOp2.Map(GetExprFactory(),
                (EXPR x) =>
                {
                    cIndices++;
                    EXPR pTemp = mustConvert(x, pDestType);
                    if (pDestType == pIntType)
                        return pTemp;
#if CSEE
                    EXPRFLAG flag = 0;
#else
                    EXPRFLAG flag = EXPRFLAG.EXF_INDEXEXPR;
#endif
                    EXPRCLASS exprType = GetExprFactory().MakeClass(pDestType);
                    return GetExprFactory().CreateCast(flag, exprType, pTemp);
                });

            if (cIndices != rank)
            {
                ErrorContext.Error(ErrorCode.ERR_BadIndexCount, rank);
                pExpr = GetExprFactory().CreateArrayIndex(pOp1, transformedIndices);
                pExpr.SetError();
                return pExpr;
            }

            // Allocate a new expression, the type is the element type of the array.
            // Array index operations are always lvalues.
            pExpr = GetExprFactory().CreateArrayIndex(pOp1, transformedIndices);
            pExpr.flags |= EXPRFLAG.EXF_LVALUE | EXPRFLAG.EXF_ASSGOP;

            if (bIsError)
            {
                pExpr.SetError();
            }

            return pExpr;
        }