Exemple #1
0
 internal Bpl.Expr BplBvLiteralExpr(Bpl.IToken tok, BaseTypes.BigNum n, BitvectorType bitvectorType)
 {
     Contract.Requires(tok != null);
     Contract.Requires(bitvectorType != null);
     return(BplBvLiteralExpr(tok, n, bitvectorType.Width));
 }
Exemple #2
0
 private static void BitvectorTruncation(BitvectorType bvType, TextWriter wr, bool after, bool surroundByUnchecked)
 {
     Contract.Requires(bvType != null);
       Contract.Requires(wr != null);
       if (!after) {
     if (bvType.NativeType == null) {
       wr.Write("((");
     } else {
       if (surroundByUnchecked) {
     // Unfortunately, the following will apply "unchecked" to all subexpressions as well.  There
     // shouldn't ever be any problem with this, but stylistically it would have been nice to have
     // applied the "unchecked" only to the actual operation that may overflow.
     wr.Write("unchecked(");
       }
       wr.Write("({0})((", bvType.NativeType.Name);
     }
       } else {
     // do the truncation, if needed
     if (bvType.NativeType == null) {
       wr.Write(") & ((new BigInteger(1) << {0}) - 1))", bvType.Width);
     } else {
       if (bvType.NativeType.Bitwidth != bvType.Width) {
     // print in hex, because that looks nice
     wr.Write(") & ({2})0x{0:X}{1})", (1UL << bvType.Width) - 1, bvType.NativeType.Suffix, bvType.NativeType.Name);
       } else {
     wr.Write("))");  // close the parentheses for the cast
       }
       if (surroundByUnchecked) {
     wr.Write(")");  // close the parentheses for the "unchecked"
       }
     }
       }
 }
Exemple #3
0
        void TypeAndToken(out IToken tok, out Type ty, bool inExpressionContext)
        {
            Contract.Ensures(Contract.ValueAtReturn(out tok)!=null); Contract.Ensures(Contract.ValueAtReturn(out ty) != null);
            tok = Token.NoToken;  ty = new BoolType();  /*keep compiler happy*/
            List<Type> gt; List<Type> tupleArgTypes = null;

            switch (la.kind) {
            case 7: {
            Get();
            tok = t;
            break;
            }
            case 8: {
            Get();
            tok = t;  ty = new CharType();
            break;
            }
            case 9: {
            Get();
            tok = t;  ty = new IntType();
            break;
            }
            case 10: {
            Get();
            tok = t;  ty = new UserDefinedType(tok, tok.val, null);
            break;
            }
            case 11: {
            Get();
            tok = t;  ty = new RealType();
            break;
            }
            case 6: {
            Get();
            tok = t;
            int w = StringToInt(tok.val.Substring(2), 0, "bitvectors that wide");
            ty = new BitvectorType(w);

            break;
            }
            case 12: {
            Get();
            tok = t;  ty = new ObjectType();
            break;
            }
            case 14: {
            Get();
            tok = t;
            OptGenericInstantiation(out gt, inExpressionContext);
            if (gt != null && gt.Count > 1) {
             SemErr("set type expects only one type argument");
            }
            ty = new SetType(true, gt != null ?gt[0] : null);

            break;
            }
            case 15: {
            Get();
            tok = t;
            OptGenericInstantiation(out gt, inExpressionContext);
            if (gt != null && gt.Count > 1) {
             SemErr("set type expects only one type argument");
            }
            ty = new SetType(false, gt != null ? gt[0] : null);

            break;
            }
            case 16: {
            Get();
            tok = t;
            OptGenericInstantiation(out gt, inExpressionContext);
            if (gt != null && gt.Count > 1) {
             SemErr("multiset type expects only one type argument");
            }
            ty = new MultiSetType(gt != null ? gt[0] : null);

            break;
            }
            case 17: {
            Get();
            tok = t;
            OptGenericInstantiation(out gt, inExpressionContext);
            if (gt != null && gt.Count > 1) {
             SemErr("seq type expects only one type argument");
            }
            ty = new SeqType(gt != null ? gt[0] : null);

            break;
            }
            case 13: {
            Get();
            tok = t;  ty = new UserDefinedType(tok, tok.val, null);
            break;
            }
            case 18: {
            Get();
            tok = t;
            OptGenericInstantiation(out gt, inExpressionContext);
            if (gt == null) {
             ty = new MapType(true, null, null);
            } else if (gt.Count != 2) {
             SemErr("map type expects two type arguments");
             ty = new MapType(true, gt[0], gt.Count == 1 ? new InferredTypeProxy() : gt[1]);
            } else {
             ty = new MapType(true, gt[0], gt[1]);
            }

            break;
            }
            case 19: {
            Get();
            tok = t;
            OptGenericInstantiation(out gt, inExpressionContext);
            if (gt == null) {
             ty = new MapType(false, null, null);
            } else if (gt.Count != 2) {
             SemErr("imap type expects two type arguments");
             ty = new MapType(false, gt[0], gt.Count == 1 ? new InferredTypeProxy() : gt[1]);
            } else {
             ty = new MapType(false, gt[0], gt[1]);
            }

            break;
            }
            case 5: {
            Get();
            tok = t;
            OptGenericInstantiation(out gt, inExpressionContext);
            int dims = StringToInt(tok.val.Substring(5), 1, "arrays of that many dimensions");
            ty = theBuiltIns.ArrayType(tok, dims, gt, true);

            break;
            }
            case 54: {
            Get();
            tok = t; tupleArgTypes = new List<Type>();
            if (StartOf(6)) {
                Type(out ty);
                tupleArgTypes.Add(ty);
                while (la.kind == 23) {
                    Get();
                    Type(out ty);
                    tupleArgTypes.Add(ty);
                }
            }
            Expect(55);
            if (tupleArgTypes.Count == 1) {
             // just return the type 'ty'
            } else {
             var dims = tupleArgTypes.Count;
             var tmp = theBuiltIns.TupleType(tok, dims, true);  // make sure the tuple type exists
             ty = new UserDefinedType(tok, BuiltIns.TupleTypeName(dims), dims == 0 ? null : tupleArgTypes);
            }

            break;
            }
            case 1: {
            Expression e;
            NameSegmentForTypeName(out e, inExpressionContext);
            tok = t;
            while (la.kind == 28) {
                Get();
                Expect(1);
                tok = t; List<Type> typeArgs;
                OptGenericInstantiation(out typeArgs, inExpressionContext);
                e = new ExprDotName(tok, e, tok.val, typeArgs);
            }
            ty = new UserDefinedType(e.tok, e);
            break;
            }
            default: SynErr(182); break;
            }
            if (IsArrow()) {
            Expect(32);
            tok = t; Type t2;
            Type(out t2);
            if (tupleArgTypes != null) {
             gt = tupleArgTypes;
            } else {
             gt = new List<Type>{ ty };
            }
            ty = new ArrowType(tok, gt, t2);
            theBuiltIns.CreateArrowTypeDecl(gt.Count);

            }
        }