Пример #1
0
        protected override void TrStringLiteral(StringLiteralExpr str, TextWriter wr)
        {
            var s = (string)str.Value;
            var n = s.Length;

            wr.Write("\"");
            for (int i = 0; i < n; i++)
            {
                if (s[i] == '\\' && s[i + 1] == '0')
                {
                    wr.Write("\\u0000");
                    i++;
                }
                else if (s[i] == '\n') // may appear in a verbatim string
                {
                    wr.Write("\\n");
                }
                else if (s[i] == '\r') // may appear in a verbatim string
                {
                    wr.Write("\\r");
                }
                else
                {
                    wr.Write(s[i]);
                }
            }
            wr.Write("\"");
        }
Пример #2
0
        /// <summary>
        /// Resolve all variables in expression to either literal values
        /// or to orignal declared nameSegments
        /// </summary>
        /// <param name="guard"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public static IEnumerable <ExpressionTree> ResolveExpression(ExpressionTree guard, ProofState state)
        {
            Contract.Requires <ArgumentNullException>(guard != null, "guard");
            Contract.Requires <ArgumentNullException>(state != null, "state");

            if (guard.IsLeaf())
            {
                if (!(guard.Data is NameSegment))
                {
                    yield return(guard.Copy());
                }
                var newGuard = guard.Copy();
                var result   = EvaluateLeaf(newGuard, state);
                foreach (var item in result)
                {
                    Contract.Assert(result != null);
                    Expression newNs = null; // potential encapsulation problems
                    if (item is MemberDecl)
                    {
                        var md = item as MemberDecl;
                        newNs = new StringLiteralExpr(new Token(), md.Name, true);
                    }
                    else if (item is Formal)
                    {
                        var tmp = item as Formal;
                        newNs = new NameSegment(tmp.tok, tmp.Name, null);
                    }
                    else if (item is NameSegment)
                    {
                        newNs = item as NameSegment;
                    }
                    else
                    {
                        newNs = item as Expression; // Dafny.LiteralExpr;
                    }
                    newGuard.Data = newNs;
                    yield return(newGuard);
                }
            }
            else
            {
                foreach (var lChild in ResolveExpression(guard.LChild, state))
                {
                    if (guard.RChild != null)
                    {
                        foreach (var rChild in ResolveExpression(guard.RChild, state))
                        {
                            yield return(new ExpressionTree(guard.Data, null, lChild, rChild));
                        }
                    }
                    else
                    {
                        yield return(new ExpressionTree(guard.Data, null, lChild, null));
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Process the declaration's "dllimport" attribute, if any, by emitting the corresponding .NET custom attribute.
        /// Returns "true" if the declaration has an active "dllimport" attribute; "false", otherwise.
        /// </summary>
        public bool ProcessDllImport(MemberDecl decl, TextWriter wr)
        {
            Contract.Requires(decl != null);
            Contract.Requires(wr != null);

            var dllimportsArgs = Attributes.FindExpressions(decl.Attributes, "dllimport");

            if (!DafnyOptions.O.DisallowExterns && dllimportsArgs != null)
            {
                StringLiteralExpr libName    = null;
                StringLiteralExpr entryPoint = null;
                if (dllimportsArgs.Count == 2)
                {
                    libName    = dllimportsArgs[0] as StringLiteralExpr;
                    entryPoint = dllimportsArgs[1] as StringLiteralExpr;
                }
                else if (dllimportsArgs.Count == 1)
                {
                    libName = dllimportsArgs[0] as StringLiteralExpr;
                    // use the given name, not the .CompileName (if user needs something else, the user can supply it as a second argument to :dllimport)
                    entryPoint = new StringLiteralExpr(decl.tok, decl.Name, false);
                }
                if (libName == null || entryPoint == null)
                {
                    Error(decl.tok, "Expected arguments are {{:dllimport dllName}} or {{:dllimport dllName, entryPoint}} where dllName and entryPoint are strings: {0}", wr, decl.FullName);
                }
                else if ((decl is Method m && m.Body != null) || (decl is Function f && f.Body != null))
                {
                    Error(decl.tok, "A {0} declared with :dllimport is not allowed a body: {1}", wr, decl.WhatKind, decl.FullName);
                }
                else if (!decl.IsStatic)
                {
                    Error(decl.tok, "A {0} declared with :dllimport must be static: {1}", wr, decl.WhatKind, decl.FullName);
                }
                else
                {
                    wr.Write("[System.Runtime.InteropServices.DllImport(");
                    TrStringLiteral(libName, wr);
                    wr.Write(", EntryPoint=");
                    TrStringLiteral(entryPoint, wr);
                    wr.WriteLine(")]");
                }
                return(true);
            }
Пример #4
0
 private static bool ShallowEq(StringLiteralExpr expr1, StringLiteralExpr expr2)
 {
     return(true);
 }
Пример #5
0
 public static StringLiteralExpr CopyStringLiteralExpr(StringLiteralExpr expr) {
   return new StringLiteralExpr(expr.tok, expr.AsStringLiteral(), expr.IsVerbatim);
 }
Пример #6
0
        void ConstAtomExpression(out Expression e, bool allowSemi, bool allowLambda)
        {
            Contract.Ensures(Contract.ValueAtReturn(out e) != null);
            IToken/*!*/ x;  BigInteger n;   Basetypes.BigDec d;
            e = dummyExpr;  Type toType = null;

            switch (la.kind) {
            case 131: {
            Get();
            e = new LiteralExpr(t, false);
            break;
            }
            case 132: {
            Get();
            e = new LiteralExpr(t, true);
            break;
            }
            case 133: {
            Get();
            e = new LiteralExpr(t);
            break;
            }
            case 2: case 3: {
            Nat(out n);
            e = new LiteralExpr(t, n);
            break;
            }
            case 4: {
            Dec(out d);
            e = new LiteralExpr(t, d);
            break;
            }
            case 19: {
            Get();
            e = new CharLiteralExpr(t, t.val.Substring(1, t.val.Length - 2));
            break;
            }
            case 20: {
            Get();
            bool isVerbatimString;
            string s = Util.RemoveParsedStringQuotes(t.val, out isVerbatimString);
            e = new StringLiteralExpr(t, s, isVerbatimString);

            break;
            }
            case 134: {
            Get();
            e = new ThisExpr(t);
            break;
            }
            case 135: {
            Get();
            x = t;
            Expect(50);
            Expression(out e, true, true);
            Expect(51);
            e = new UnaryOpExpr(x, UnaryOpExpr.Opcode.Fresh, e);
            break;
            }
            case 136: {
            Get();
            x = t;
            Expect(50);
            Expression(out e, true, true);
            Expect(51);
            e = new OldExpr(x, e);
            break;
            }
            case 23: {
            Get();
            x = t;
            Expression(out e, true, true);
            e = new UnaryOpExpr(x, UnaryOpExpr.Opcode.Cardinality, e);
            Expect(23);
            break;
            }
            case 8: case 10: {
            if (la.kind == 8) {
                Get();
                x = t; toType = new IntType();
            } else {
                Get();
                x = t; toType = new RealType();
            }
            Expect(50);
            Expression(out e, true, true);
            Expect(51);
            e = new ConversionExpr(x, e, toType);
            break;
            }
            case 50: {
            ParensExpression(out e, allowSemi, allowLambda);
            break;
            }
            default: SynErr(234); break;
            }
        }
Пример #7
0
        private void AddVariant(Statement st, ref List <Solution> solution_list)
        {
            List <Expression> call_arguments = null;
            List <Expression> dec_list       = null;
            Expression        input          = null;

            InitArgs(st, out call_arguments);
            Contract.Assert(tcce.OfSize(call_arguments, 1), Util.Error.MkErr(st, 0, 1, call_arguments.Count));

            StringLiteralExpr wildCard = call_arguments[0] as StringLiteralExpr;

            if (wildCard != null)
            {
                if (wildCard.Value.Equals("*"))
                {
                    input = new WildcardExpr(wildCard.tok);
                }
            }
            else
            {
                // hack

                /*
                 * TODO:
                 * Implement propper variable replacement
                 */
                object tmp;
                ProcessArg(call_arguments[0], out tmp);
                Contract.Assert(tmp != null);
                IVariable form = tmp as IVariable;
                if (form != null)
                {
                    input = new NameSegment(form.Tok, form.Name, null);
                }
                else if (tmp is BinaryExpr)
                {
                    input = tmp as BinaryExpr;
                }
                else if (tmp is NameSegment)
                {
                    input = tmp as NameSegment;
                }
            }
            WhileStmt ws = FindWhileStmt(globalContext.tac_call, globalContext.md);

            if (ws != null)
            {
                WhileStmt nws = null;
                dec_list = new List <Expression>(ws.Decreases.Expressions.ToArray());

                dec_list.Add(input);
                Specification <Expression> decreases = new Specification <Expression>(dec_list, ws.Attributes);
                nws = new WhileStmt(ws.Tok, ws.EndTok, ws.Guard, ws.Invariants, decreases, ws.Mod, ws.Body);
                AddUpdated(ws, nws);
            }
            else
            {
                Method target = Program.FindMember(globalContext.program.ParseProgram(), localContext.md.Name) as Method;
                if (GetNewTarget() != null && GetNewTarget().Name == target.Name)
                {
                    target = GetNewTarget();
                }
                Contract.Assert(target != null, Util.Error.MkErr(st, 3));

                dec_list = target.Decreases.Expressions;
                // insert new variants at the end of the existing variants list
                Contract.Assert(input != null);
                dec_list.Add(input);

                Specification <Expression> decreases = new Specification <Expression>(dec_list, target.Decreases.Attributes);
                Method  result = null;
                dynamic lemma  = null;
                if ((lemma = target as Lemma) != null)
                {
                    result = new Lemma(lemma.tok, lemma.Name, lemma.HasStaticKeyword, lemma.TypeArgs, lemma.Ins, lemma.Outs,
                                       lemma.Req, lemma.Mod, lemma.Ens, decreases, lemma.Body, lemma.Attributes, lemma.SignatureEllipsis);
                }
                else if ((lemma = target as CoLemma) != null)
                {
                    result = new CoLemma(lemma.tok, lemma.Name, lemma.HasStaticKeyword, lemma.TypeArgs, lemma.Ins, lemma.Outs,
                                         lemma.Req, lemma.Mod, lemma.Ens, decreases, lemma.Body, lemma.Attributes, lemma.SignatureEllipsis);
                }
                else
                {
                    result = new Method(target.tok, target.Name, target.HasStaticKeyword, target.IsGhost, target.TypeArgs,
                                        target.Ins, target.Outs, target.Req, target.Mod, target.Ens, decreases, target.Body, target.Attributes,
                                        target.SignatureEllipsis);
                }

                // register new method
                this.localContext.new_target = result;
                globalContext.program.IncTotalBranchCount(globalContext.program.currentDebug);
            }

            solution_list.Add(new Solution(this.Copy()));
        }
Пример #8
0
/// <summary>
/// Encode an 'extern' declaration modifier as an {:extern name} attribute.
///
/// We also include an {:axiom} attribute since the specification of an
/// external entity is assumed to hold, but only for methods or functions.
///</summary>
static void EncodeExternAsAttribute(DeclModifierData dmod, ref Attributes attrs, IToken/*!*/ id, bool needAxiom) {
  if (dmod.IsExtern) {
    StringLiteralExpr name = dmod.ExternName;
    if (name == null) {
      bool isVerbatimString = false;
      name = new StringLiteralExpr(id, id.val, isVerbatimString);
    }
    var args = new List<Expression>();
    args.Add(name);
    attrs = new Attributes("extern", args, attrs);

    // Also 'extern' implies 'axiom' for methods or functions.
    if (needAxiom) {
      attrs = new Attributes("axiom", new List<Expression>(), attrs);
    }
  }
}
Пример #9
0
 private Expression ParseFactor()
 {
     if (Accept(Token.Types.IntLiteral))
     {
         IntLiteralExpr exp = new IntLiteralExpr(AcceptedToken.Line, AcceptedToken.Column);
         exp.ExprValue = int.Parse(AcceptedToken.Content);
         return exp;
     }
     else if (Accept(Token.Types.StringLiteral))
     {
         StringLiteralExpr expr = new StringLiteralExpr(AcceptedToken.Line, AcceptedToken.Column);
         expr.ExprValue = AcceptedToken.Content;
         return expr;
     }
     else if (Accept(Token.Types.Identifier))
     {
         IdentifierExpr node = new IdentifierExpr(AcceptedToken.Line, AcceptedToken.Column, AcceptedToken.Content);
         return node;
     }
     else if (Accept(Token.Types.LParen))
     {
         Expression exp = ParseExpression();
         Match(Token.Types.RParen);
         return exp;
     }
     else throw new ParserException(String.Format("Expected literal or identifier, got {0} instead at line {1} column {2}.",
         CurrentToken.Type, CurrentToken.Line, CurrentToken.Column));
 }
 public void Visit(StringLiteralExpr stringLiteralExpr)
 {
     PrintNode("String " + stringLiteralExpr.ExprValue, true);
 }
Пример #11
0
 public static StringLiteralExpr CopyStringLiteralExpr(StringLiteralExpr expr)
 {
     return(new StringLiteralExpr(expr.tok, expr.AsStringLiteral(), expr.IsVerbatim));
 }
Пример #12
0
        void ConstAtomExpression(out Expression e, bool allowSemi, bool allowLambda)
        {
            Contract.Ensures(Contract.ValueAtReturn(out e) != null);
            IToken/*!*/ x;  BigInteger n;   Basetypes.BigDec d;
            e = dummyExpr;  Type toType = null;

            switch (la.kind) {
            case 138: {
            Get();
            e = new LiteralExpr(t, false);
            break;
            }
            case 139: {
            Get();
            e = new LiteralExpr(t, true);
            break;
            }
            case 140: {
            Get();
            e = new LiteralExpr(t);
            break;
            }
            case 2: case 3: {
            Nat(out n);
            e = new LiteralExpr(t, n);
            break;
            }
            case 4: {
            Dec(out d);
            e = new LiteralExpr(t, d);
            break;
            }
            case 20: {
            Get();
            e = new CharLiteralExpr(t, t.val.Substring(1, t.val.Length - 2));
            break;
            }
            case 21: {
            Get();
            bool isVerbatimString;
            string s = Util.RemoveParsedStringQuotes(t.val, out isVerbatimString);
            e = new StringLiteralExpr(t, s, isVerbatimString);

            break;
            }
            case 141: {
            Get();
            e = new ThisExpr(t);
            break;
            }
            case 142: {
            Get();
            x = t;
            Expect(54);
            Expression(out e, true, true);
            Expect(55);
            e = new UnaryOpExpr(x, UnaryOpExpr.Opcode.Fresh, e);
            break;
            }
            case 143: {
            Get();
            x = t;
            Expect(54);
            Expression(out e, true, true);
            Expect(55);
            e = new UnaryOpExpr(x, UnaryOpExpr.Opcode.Allocated, e);
            break;
            }
            case 144: {
            Get();
            x = t; FrameExpression fe; var mod = new List<FrameExpression>();
            Expect(54);
            FrameExpression(out fe, false, false);
            mod.Add(fe);
            while (la.kind == 23) {
                Get();
                FrameExpression(out fe, false, false);
                mod.Add(fe);
            }
            Expect(55);
            e = new UnchangedExpr(x, mod);
            break;
            }
            case 145: {
            Get();
            x = t;
            Expect(54);
            Expression(out e, true, true);
            Expect(55);
            e = new OldExpr(x, e);
            break;
            }
            case 24: {
            Get();
            x = t;
            Expression(out e, true, true, false);
            e = new UnaryOpExpr(x, UnaryOpExpr.Opcode.Cardinality, e);
            Expect(24);
            break;
            }
            case 9: case 11: {
            if (la.kind == 9) {
                Get();
                x = t; toType = new IntType();
            } else {
                Get();
                x = t; toType = new RealType();
            }
            errors.Deprecated(t, string.Format("the syntax \"{0}(expr)\" for type conversions has been deprecated; the new syntax is \"expr as {0}\"", x.val));
            Expect(54);
            Expression(out e, true, true);
            Expect(55);
            e = new ConversionExpr(x, e, toType);
            break;
            }
            case 54: {
            ParensExpression(out e, allowSemi, allowLambda);
            break;
            }
            default: SynErr(262); break;
            }
        }
Пример #13
0
    /// <summary>
    /// Resolve all variables in expression to either literal values
    /// or to orignal declared nameSegments
    /// </summary>
    /// <param name="guard"></param>
    /// <param name="state"></param>
    /// <returns></returns>
    public static IEnumerable<ExpressionTree> ResolveExpression(ExpressionTree guard, ProofState state) {
      Contract.Requires<ArgumentNullException>(guard != null, "guard");
      Contract.Requires<ArgumentNullException>(state != null, "state");

      if (guard.IsLeaf()) {
        if (!(guard.Data is NameSegment)) {
          yield return guard.Copy();
        }
        var newGuard = guard.Copy();
        var result = EvaluateLeaf(newGuard, state);
        foreach (var item in result) {
          Contract.Assert(result != null);
          Expression newNs = null; // potential encapsulation problems
          if (item is MemberDecl) {
            var md = item as MemberDecl;
            newNs = new StringLiteralExpr(new Token(), md.Name, true);
          } else if (item is Formal) {
            var tmp = item as Formal;
            newNs = new NameSegment(tmp.tok, tmp.Name, null);
          } else if (item is NameSegment) {
            newNs = item as NameSegment;
          } else {
            newNs = item as Expression; // Dafny.LiteralExpr;
          }
          newGuard.Data = newNs;
          yield return newGuard;
        }
      } else {
        foreach (var lChild in ResolveExpression(guard.LChild, state)) {
          if (guard.RChild != null) {
            foreach (var rChild in ResolveExpression(guard.RChild, state)) {
              yield return new ExpressionTree(guard.Data, null, lChild, rChild);
            }
          } else {
            yield return new ExpressionTree(guard.Data, null, lChild, null);
          }
        }
      }
    }