Beispiel #1
0
        /// <summary>
        /// From:
        ///     pointer
        /// To:
        ///     pointer, integral
        /// </summary>
        public static Expr FromPointer(Expr expr, ExprType type, Env env)
        {
            ExprTypeKind from = expr.Type.Kind;
            ExprTypeKind to   = type.Kind;

            if (from != ExprTypeKind.POINTER)
            {
                throw new InvalidOperationException("Expected a pointer.");
            }

            // if we are casting to another pointer, do a nop
            if (to == ExprTypeKind.POINTER)
            {
                if (expr.IsConstExpr)
                {
                    return(new ConstPtr(((ConstPtr)expr).Value, type, env));
                }
                return(new TypeCast(TypeCastType.NOP, expr, type, env));
            }

            // if we are casting to an integral
            if (type.IsIntegral)
            {
                // pointer -> ulong -> whatever integral
                if (expr.IsConstExpr)
                {
                    expr = new ConstULong(((ConstPtr)expr).Value, env);
                }
                else
                {
                    expr = new TypeCast(TypeCastType.NOP, expr, new ULongType(type.IsConst, type.IsVolatile), env);
                }
                return(MakeCast(expr, type, env));
            }

            throw new InvalidOperationException("Casting from a pointer to an unsupported Type.");
        }
Beispiel #2
0
        /// <summary>
        /// From:
        ///     pointer
        /// To:
        ///     pointer, integral
        /// </summary>
        public static Expr FromPointer(Expr expr, ExprType type, Env env) {
            ExprTypeKind from = expr.Type.Kind;
            ExprTypeKind to = type.Kind;

            if (from != ExprTypeKind.POINTER) {
                throw new InvalidOperationException("Expected a pointer.");
            }

            // if we are casting to another pointer, do a nop
            if (to == ExprTypeKind.POINTER) {
                if (expr.IsConstExpr) {
                    return new ConstPtr(((ConstPtr)expr).Value, type, env);
                }
                return new TypeCast(TypeCastType.NOP, expr, type, env);
            }

            // if we are casting to an integral
            if (type.IsIntegral) {
                // pointer -> ulong -> whatever integral
                if (expr.IsConstExpr) {
                    expr = new ConstULong(((ConstPtr)expr).Value, env);
                } else {
                    expr = new TypeCast(TypeCastType.NOP, expr, new ULongType(type.IsConst, type.IsVolatile), env);
                }
                return MakeCast(expr, type, env);
            }

            throw new InvalidOperationException("Casting from a pointer to an unsupported Type.");
        }