/// <summary> /// The following constructor is suitable for recognized attributes. /// </summary> private IdRegion(Bpl.IToken from, Bpl.IToken thru, OccurrenceKind occurrenceKind) { this.Start = from.pos; this.Length = thru.pos + thru.val.Length - from.pos; this.Kind = occurrenceKind; this.HoverText = null; }
public static void AddCaptureState(this BoogieStmtListBuilder builder, Bpl.IToken tok, bool isEndToken, string /*?*/ additionalInfo) { if (DafnyOptions.O.ModelViewFile != null || DafnyOptions.O.TestGenOptions.WarnDeadCode) { builder.Add(CaptureState(tok, isEndToken, additionalInfo)); } }
public Bpl.Expr FromUnion(Bpl.IToken tok, Bpl.Type boogieType, Bpl.Expr expr, bool isStruct) { if (boogieType == UnionType || boogieType == RefType) { return(expr); } Bpl.Function conversion = null; if (boogieType == Bpl.Type.Bool) { conversion = this.Union2Bool; } else if (boogieType == Bpl.Type.Int) { conversion = this.Union2Int; } else if (boogieType == RealType) { conversion = this.Union2Real; } else { throw new InvalidOperationException(String.Format("Unknown Boogie type: '{0}'", boogieType.ToString())); } var callExpr = new Bpl.NAryExpr( tok, new Bpl.FunctionCall(conversion), new List <Bpl.Expr>(new Bpl.Expr[] { expr }) ); callExpr.Type = boogieType; return(callExpr); }
public RtlExp GhostLet(Microsoft.Boogie.IToken tok, List <BoundVar> lhss, List <RtlExp> rhss, Func <RtlExp> body) { if (stmtExprEnabled && ignoreStmtExpr == 0) { var oldRenamer1 = PushRename(); for (int i = 0; i < lhss.Count; i++) { var lhs = lhss[i]; var rhs = rhss[i]; AddGhostVarDecl(lhs.Name, lhs.Type, true); MoveGhost(new RtlVar(GhostVar(lhs.Name), true, AppType(lhs.Type)), rhs); } body(); PopRename(oldRenamer1); } var oldRenamer2 = PushRename(); lhss.ForEach(e => AddRename(e.Name)); ignoreStmtExpr++; RtlExp rExp = new RtlLiteral("(" + String.Concat(lhss.Zip(rhss, (lhs, rhs) => "let " + GhostVar(lhs.Name) + ":" + TypeString(AppType(lhs.Type)) + " := (" + rhs + ") in ")) + " (" + body() + "))"); ignoreStmtExpr--; PopRename(oldRenamer2); return(rExp); }
public static void Add(List <IdRegion> regions, Bpl.IToken tok, IVariable v, bool isDefinition, ModuleDefinition context) { Contract.Requires(regions != null); Contract.Requires(tok != null); Contract.Requires(v != null); Add(regions, tok, v, isDefinition, null, context); }
public DareResult(Bpl.IToken startToken, Bpl.IToken endToken, string typeOfRemovable, object replace) { StartTok = startToken; Length = endToken.pos - startToken.pos; TypeOfRemovable = typeOfRemovable; Replace = replace; }
public DareResult(Bpl.IToken startToken, int length, string typeOfRemovable, object replace) { StartTok = startToken; Length = length; TypeOfRemovable = typeOfRemovable; Replace = replace; }
public Bpl.Function CreateTypeFunction(ITypeReference type, int parameterCount) { System.Diagnostics.Debug.Assert(parameterCount >= 0); string typename = TypeHelper.GetTypeName(type, NameFormattingOptions.DocumentationId); typename = TranslationHelper.TurnStringIntoValidIdentifier(typename); Bpl.IToken tok = type.Token(); List <Bpl.Variable> inputs = new List <Bpl.Variable>(); //for (int i = 0; i < parameterCount; i++) { // inputs.Add(new Bpl.Formal(tok, new Bpl.TypedIdent(tok, "arg"+i, this.TypeType), true)); //} foreach (var t in TranslationHelper.ConsolidatedGenericParameters(type)) { var n = t.Name.Value; var n2 = TranslationHelper.TurnStringIntoValidIdentifier(n); inputs.Add(new Bpl.Formal(Bpl.Token.NoToken, new Bpl.TypedIdent(Bpl.Token.NoToken, n2, this.TypeType), true)); } Bpl.Variable output = new Bpl.Formal(tok, new Bpl.TypedIdent(tok, "result", this.TypeType), false); Bpl.Function func = new Bpl.Function(tok, typename, inputs, output); var attrib = new Bpl.QKeyValue(Bpl.Token.NoToken, "constructor", new List <object>(1), null); func.Attributes = attrib; return(func); }
internal Bpl.Expr BplBvLiteralExpr(Bpl.IToken tok, BaseTypes.BigNum n, int width) { Contract.Requires(tok != null); Contract.Requires(0 <= width); if (width == 0) { // see comment in BplBvType Contract.Assert(n.IsZero); return(Bpl.Expr.Literal(0)); } else if (n.IsNegative) { // This can only happen if some error is reported elsewhere. Nevertheless, we do need to // generate a Boogie expression and Boogie would crash if we pass a negative number to // Bpl.LiteralExpr for a bitvector. var zero = new Bpl.LiteralExpr(tok, BaseTypes.BigNum.ZERO, width); var absN = new Bpl.LiteralExpr(tok, -n, width); var etran = new ExpressionTranslator(this, predef, tok); return(etran.TrToFunctionCall(tok, "sub_bv" + width, BplBvType(width), zero, absN, false)); } else { return(new Bpl.LiteralExpr(tok, n, width)); } }
/// <summary> /// /// </summary> public override void TraverseChildren(IReturnStatement returnStatement) { Bpl.IToken tok = returnStatement.Token(); if (returnStatement.Expression != null) { ExpressionTraverser etrav = this.factory.MakeExpressionTraverser(this.sink, this, this.contractContext); etrav.Traverse(returnStatement.Expression); if (this.sink.ReturnVariable == null || etrav.TranslatedExpressions.Count < 1) { throw new TranslationException(String.Format("{0} returns a value that is not supported by the function", returnStatement.ToString())); } StmtBuilder.Add(Bpl.Cmd.SimpleAssign(tok, new Bpl.IdentifierExpr(tok, this.sink.ReturnVariable), etrav.TranslatedExpressions.Pop())); } // FEEDBACK TODO extract into a method if (PhoneCodeHelper.instance().PhoneFeedbackToggled) { IMethodDefinition methodTranslated = sink.getMethodBeingTranslated(); if (methodTranslated != null && PhoneCodeHelper.instance().isMethodInputHandlerOrFeedbackOverride(methodTranslated) && !PhoneCodeHelper.instance().isMethodIgnoredForFeedback(methodTranslated)) { Bpl.AssertCmd falseAssertion = new Bpl.AssertCmd(Bpl.Token.NoToken, Bpl.LiteralExpr.False); StmtBuilder.Add(falseAssertion); } } StmtBuilder.Add(new Bpl.ReturnCmd(returnStatement.Token())); }
protected override void EmitNewArray(Type elmtType, Bpl.IToken tok, List <Expression> dimensions, bool mustInitialize, TargetWriter wr) { var initValue = mustInitialize ? DefaultValue(elmtType, wr, tok) : null; if (dimensions.Count == 1) { // handle the common case of 1-dimensional arrays separately wr.Write("Array"); TrParenExpr(dimensions[0], wr, false); if (initValue != null) { wr.Write(".fill({0})", initValue); } } else { // the general case wr.Write("_dafny.newArray({0}", initValue ?? "undefined"); foreach (var dim in dimensions) { wr.Write(", "); TrExpr(dim, wr, false); } wr.Write(")"); } }
public override Bpl.Variable CreateEventVariable(IEventDefinition e) { Bpl.Variable v; string fieldName = MemberHelper.GetMemberSignature(e, NameFormattingOptions.DocumentationId); // HACK fieldName = fieldName.Replace("E:", "F:"); fieldName = TranslationHelper.TurnStringIntoValidIdentifier(fieldName); Bpl.IToken tok = e.Token(); Bpl.Type t = this.sink.CciTypeToBoogie(e.Type.ResolvedType); if (e.Adder.ResolvedMethod.IsStatic) { Bpl.TypedIdent tident = new Bpl.TypedIdent(tok, fieldName, t); v = new Bpl.GlobalVariable(tok, tident); } else { Bpl.Type mt = new Bpl.MapType(tok, new List <Bpl.TypeVariable>(), new List <Bpl.Type>(new Bpl.Type[] { this.RefType }), t); Bpl.TypedIdent tident = new Bpl.TypedIdent(tok, fieldName, mt); v = new Bpl.GlobalVariable(tok, tident); } return(v); }
protected override void DeclareLocalVar(string name, Type /*?*/ type, Bpl.IToken /*?*/ tok, Expression rhs, bool inLetExprBody, TargetWriter wr) { wr.Indent(); wr.Write("let {0} = ", name); TrExpr(rhs, wr, inLetExprBody); wr.WriteLine(";"); }
public override void Error(Bpl.IToken tok, string msg, params object[] args) { string s = string.Format(msg, args); dd.RecordError(tok.filename, tok.line - 1, tok.col - 1, ErrorCategory.ResolveError, s); ErrorCount++; }
private IdRegion(Bpl.IToken tok, IVariable v, bool isDefinition, string kind, ICallable callableContext, ModuleDefinition context) { Contract.Requires(tok != null); Contract.Requires(v != null); Start = tok.pos; Length = v.DisplayName.Length; if (kind == null) { // use default if (v is LocalVariable) { kind = "local variable"; } else if (v is BoundVar) { kind = "bound variable"; } else { var formal = (Formal)v; kind = formal.InParam ? "in-parameter" : "out-parameter"; if (callableContext is TwoStateLemma && !formal.IsOld) { kind = "new " + kind; } if (formal is ImplicitFormal) { kind = "implicit " + kind; } } } Variable = v; HoverText = string.Format("({2}{3}) {0}: {1}", v.DisplayName, v.Type.TypeName(context), v.IsGhost ? "ghost " : "", kind); Kind = !isDefinition ? OccurrenceKind.Use : LocalVariable.HasWildcardName(v) ? OccurrenceKind.WildDefinition : OccurrenceKind.Definition; }
private IdRegion(Bpl.IToken tok, OccurrenceKind occurrenceKind, string info, int length) { this.Start = tok.pos; this.Length = length; this.Kind = occurrenceKind; this.HoverText = info; }
public Bpl.Expr ProperMultiset(Bpl.IToken tok, Bpl.Expr e0, Bpl.Expr e1) { Contract.Requires(tok != null); Contract.Requires(e0 != null); Contract.Requires(e1 != null); return(Bpl.Expr.Binary(tok, Bpl.BinaryOperator.Opcode.And, FunctionCall(tok, BuiltinFunction.MultiSetSubset, null, e0, e1), Bpl.Expr.Not(FunctionCall(tok, BuiltinFunction.MultiSetEqual, null, e0, e1)))); }
static Bpl.NAryExpr FunctionCall(Bpl.IToken tok, string function, Bpl.Type returnType, params Bpl.Expr[] args) { Contract.Requires(tok != null); Contract.Requires(function != null); Contract.Requires(returnType != null); Contract.Requires(args != null); Contract.Ensures(Contract.Result <Bpl.NAryExpr>() != null); return(new Bpl.NAryExpr(tok, new Bpl.FunctionCall(new Bpl.IdentifierExpr(tok, function, returnType)), new List <Bpl.Expr>(args))); }
/// <summary> /// Creates a fresh BPL variable to represent <paramref name="type"/>, deciding /// on its type based on the heap representation. I.e., the value of this /// variable represents the value of the expression "typeof(type)". /// </summary> public Bpl.Variable CreateTypeVariable(ITypeReference type, List <Bpl.ConstantParent> parents) { string typename = TypeHelper.GetTypeName(type, NameFormattingOptions.DocumentationId); typename = TranslationHelper.TurnStringIntoValidIdentifier(typename); Bpl.IToken tok = type.Token(); Bpl.TypedIdent tident = new Bpl.TypedIdent(tok, typename, this.TypeType); Bpl.Constant v = new Bpl.Constant(tok, tident, true /*unique*/, parents, false, null); return(v); }
public static void Add(List <IdRegion> regions, Bpl.IToken tok, string text, int length) { Contract.Requires(regions != null); Contract.Requires(tok != null); Contract.Requires(text != null); if (SurfaceSyntaxToken(tok)) { regions.Add(new IdRegion(tok, OccurrenceKind.AdditionalInformation, text, length)); } }
public static void Add(List <IdRegion> regions, Bpl.IToken tok, IVariable v, bool isDefinition, string kind, ModuleDefinition context) { Contract.Requires(regions != null); Contract.Requires(tok != null); Contract.Requires(v != null); if (SurfaceSyntaxToken(tok)) { regions.Add(new IdRegion(tok, v, isDefinition, kind, context)); } }
void RecordUseAndDef(Bpl.IToken useTok, int length, Bpl.IToken defTok) { // add to the definition table so we know where the definition for this expr is SnapshotSpan span = new SnapshotSpan(this._snapshot, useTok.pos, length); if (!_definitions.ContainsKey(span)) { _definitions.Add(span, defTok); } }
public Bpl.Expr ProperSubset(Bpl.IToken tok, Bpl.Expr e0, Bpl.Expr e1) { Contract.Requires(tok != null); Contract.Requires(e0 != null); Contract.Requires(e1 != null); Contract.Ensures(Contract.Result <Bpl.Expr>() != null); return(Bpl.Expr.Binary(tok, Bpl.BinaryOperator.Opcode.And, FunctionCall(tok, BuiltinFunction.SetSubset, null, e0, e1), Bpl.Expr.Not(FunctionCall(tok, BuiltinFunction.SetSubset, null, e1, e0)))); }
public static void Add(List <IdRegion> regions, Bpl.IToken tok, Field decl, Microsoft.Dafny.Type showType, string kind, bool isDefinition, ModuleDefinition context) { Contract.Requires(regions != null); Contract.Requires(tok != null); Contract.Requires(decl != null); Contract.Requires(kind != null); if (SurfaceSyntaxToken(tok)) { regions.Add(new IdRegion(tok, decl, showType, kind, isDefinition, context)); } }
protected override TargetWriter CreateIIFE(string source, Type sourceType, Bpl.IToken sourceTok, Type resultType, Bpl.IToken resultTok, string bvName, TargetWriter wr) { var w = wr.NewNamedBlock("function ({0})", bvName); w.SetBraceStyle(BlockTargetWriter.BraceStyle.Space, BlockTargetWriter.BraceStyle.Nothing); w.Indent(); w.Write("return "); w.BodySuffix = ";" + w.NewLine; wr.Write("({0})", source); return(w); }
void RecordUseAndDef(Program prog, Bpl.IToken useTok, int length, Bpl.IToken defTok) { if (prog.FullName.ToLower().CompareTo(useTok.filename.ToLower()) == 0) // Otherwise, we're looking at an included file // add to the definition table so we know where the definition for this expr is { SnapshotSpan span = new SnapshotSpan(this._snapshot, useTok.pos, length); if (!_definitions.ContainsKey(span)) { _definitions.Add(span, defTok); } } }
protected override bool DeclareFormal(string prefix, string name, Type type, Bpl.IToken tok, bool isInParam, TextWriter wr) { if (isInParam) { wr.Write("{0}{1}", prefix, name); return(true); } else { return(false); } }
public Bpl.Expr ProperPrefix(Bpl.IToken tok, Bpl.Expr e0, Bpl.Expr e1) { Contract.Requires(tok != null); Contract.Requires(e0 != null); Contract.Requires(e1 != null); Contract.Ensures(Contract.Result <Bpl.Expr>() != null); Bpl.Expr len0 = FunctionCall(tok, BuiltinFunction.SeqLength, null, e0); Bpl.Expr len1 = FunctionCall(tok, BuiltinFunction.SeqLength, null, e1); return(Bpl.Expr.And( Bpl.Expr.Lt(len0, len1), FunctionCall(tok, BuiltinFunction.SeqSameUntil, null, e0, e1, len0))); }
protected override BlockTargetWriter CreateLambda(List <Type> inTypes, Bpl.IToken tok, List <string> inNames, Type resultType, TargetWriter wr) { wr.Write("function ("); Contract.Assert(inTypes.Count == inNames.Count); // guaranteed by precondition for (var i = 0; i < inNames.Count; i++) { wr.Write("{0}{1}", i == 0 ? "" : ", ", inNames[i]); } var w = wr.NewBlock(")"); w.SetBraceStyle(BlockTargetWriter.BraceStyle.Space, BlockTargetWriter.BraceStyle.Nothing); return(w); }
private static void BplIfIf(Bpl.IToken tk, bool yes, Bpl.Expr guard, BoogieStmtListBuilder builder, Action <BoogieStmtListBuilder> k) { if (yes) { var newBuilder = new BoogieStmtListBuilder(builder.tran); k(newBuilder); builder.Add(new Bpl.IfCmd(tk, guard, newBuilder.Collect(tk), null, null)); } else { k(builder); } }