summaryrefslogtreecommitdiff
path: root/Source/AbsInt/ExprFactories.cs
blob: 34564cac47a840ce6f60045c5b7f1357ee8e1dcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All Rights Reserved.
//
//-----------------------------------------------------------------------------
using Microsoft.Boogie;
using AI = Microsoft.AbstractInterpretationFramework;
using System.Diagnostics.Contracts;
using Microsoft.Basetypes;

namespace Microsoft.Boogie.AbstractInterpretation {

  public class BoogiePropFactory : BoogieFactory, AI.IQuantPropExprFactory {
    public AI.IFunApp False {
      get {
        Contract.Ensures(Contract.Result<AI.IFunApp>() != null);

        return Expr.False;
      }
    }
    public AI.IFunApp True {
      get {
        Contract.Ensures(Contract.Result<AI.IFunApp>() != null);

        return Expr.True;
      }
    }

    public AI.IFunApp Not(AI.IExpr p) {
      //Contract.Requires(p != null);
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);

      return Expr.Unary(Token.NoToken, UnaryOperator.Opcode.Not, IExpr2Expr(p));
    }

    public AI.IFunApp And(AI.IExpr p, AI.IExpr q) {
      //Contract.Requires(p != null);
      //Contract.Requires(q != null);
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);

      return Expr.Binary(BinaryOperator.Opcode.And, IExpr2Expr(p), IExpr2Expr(q));
    }

    public AI.IFunApp Or(AI.IExpr p, AI.IExpr q) {
      //Contract.Requires(p != null);
      //Contract.Requires(q != null);
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);
      return Expr.Binary(BinaryOperator.Opcode.Or, IExpr2Expr(p), IExpr2Expr(q));
    }

    public AI.IFunApp Implies(AI.IExpr p, AI.IExpr q) {
      //Contract.Requires(p != null);
      //Contract.Requires(q != null);
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);
      return Expr.Binary(BinaryOperator.Opcode.Imp, IExpr2Expr(p), IExpr2Expr(q));
    }

    public AI.IFunApp Exists(AI.IFunction p) {
      //Contract.Requires(p != null);
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);

      return (AI.IFunApp)(new ExistsExpr(Token.NoToken, new VariableSeq((Variable)p.Param), IExpr2Expr(p.Body))).IExpr;
    }
    public AI.IFunApp Exists(AI.AIType paramType, AI.FunctionBody bodygen) {
      //Contract.Requires(bodygen != null);
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);

      // generate a fresh new variable
      Variable var = FreshBoundVariable(paramType);
      Contract.Assert(var != null);
      Expr expr = IExpr2Expr(bodygen(var));
      Contract.Assert(expr != null);
      return (AI.IFunApp)(new ExistsExpr(Token.NoToken, new VariableSeq(var), expr)).IExpr;
    }

    public AI.IFunApp Forall(AI.IFunction p) {
      //Contract.Requires(p != null);
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);

      return (AI.IFunApp)(new ForallExpr(Token.NoToken, new VariableSeq((Variable)p.Param), IExpr2Expr(p.Body))).IExpr;
    }
    public AI.IFunApp Forall(AI.AIType paramType, AI.FunctionBody bodygen) {
      //Contract.Requires(bodygen != null);
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);

      // generate a fresh new variable
      Variable var = FreshBoundVariable(paramType);
      Contract.Assert(var != null);
      Expr expr = IExpr2Expr(bodygen(var));
      Contract.Assert(expr != null);
      return (AI.IFunApp)(new ForallExpr(Token.NoToken, new VariableSeq(var), expr)).IExpr;
    }

    private static int freshVarNum = 0;
    private static Variable FreshBoundVariable(AI.AIType paramType) {
      Contract.Ensures(Contract.Result<Variable>() != null);

      // TODO: Should use the AI.AIType given in Exists and Forall to determine what Boogie type
      // to make this new variable?  For now, we use Type.Any.
      Type t = Type.Int;
      Contract.Assert(t != null);
      /*
        if (paramType is AI.Ref)
          t = Type.Ref;
        else if (paramType is AI.FieldName)
          t = Type.Name;
        else
          System.Console.WriteLine("*** unsupported type in AI {0}", paramType); */
      Contract.Assert(false);
      throw new cce.UnreachableException();
      TypedIdent id = new TypedIdent(Token.NoToken, "propfactory_freshvar" + freshVarNum, t);
      freshVarNum++;
      return new BoundVariable(Token.NoToken, id);
    }
  }

  public class BoogieValueFactory : BoogieFactory, AI.IValueExprFactory {
    public AI.IFunApp Eq(AI.IExpr e0, AI.IExpr e1) {
      //Contract.Requires(e0 != null);
      //Contract.Requires(e1 != null);
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);

      return Expr.Eq(IExpr2Expr(e0), IExpr2Expr(e1));
    }
    public AI.IFunApp Neq(AI.IExpr e0, AI.IExpr e1) {
      //Contract.Requires(e0 != null);
      //Contract.Requires(e1 != null);
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);
      return Expr.Neq(IExpr2Expr(e0), IExpr2Expr(e1));
    }
  }

  public class BoogieNullnessFactory : BoogieFactory, AI.INullnessFactory {
    public AI.IFunApp Eq(AI.IExpr e0, AI.IExpr e1) {
      //Contract.Requires(e0 != null);
      //Contract.Requires(e1 != null);
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);
      return Expr.Eq(IExpr2Expr(e0), IExpr2Expr(e1));
    }

    public AI.IFunApp Neq(AI.IExpr e0, AI.IExpr e1) {
      //Contract.Requires(e0 != null);
      //Contract.Requires(e1 != null);
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);
      return Expr.Neq(IExpr2Expr(e0), IExpr2Expr(e1));
    }

    public AI.IFunApp Null {
      get {
        Contract.Assert(false);       // don't know where to get null from\
        throw new cce.UnreachableException();
      }
    }
  }

  public class BoogieIntFactory : BoogieValueFactory, AI.IIntExprFactory {
    public AI.IFunApp Const(BigNum i) {
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);

      return new LiteralExpr(Token.NoToken, i);
    }
  }

  public class BoogieLinearFactory : BoogieIntFactory, AI.ILinearExprFactory {
    public AI.IFunApp AtMost(AI.IExpr e0, AI.IExpr e1) {
      //Contract.Requires(e0 != null);
      //Contract.Requires(e1 != null);
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);
      return Expr.Le(IExpr2Expr(e0), IExpr2Expr(e1));
    }
    public AI.IFunApp Add(AI.IExpr e0, AI.IExpr e1) {
      //Contract.Requires(e0 != null);
      //Contract.Requires(e1 != null);
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);
      return Expr.Add(IExpr2Expr(e0), IExpr2Expr(e1));
    }
    public AI.IExpr Term(Rational r, AI.IVariable var) {
      Contract.Ensures(Contract.Result<AI.IExpr>() != null);

      if (var != null && r == Rational.ONE) {
        return var;
      } else {
        Expr product;
        if (r.IsIntegral) {
          product = Expr.Literal(r.AsBigNum);
        } else {
          product = Expr.Div(Expr.Literal(r.Numerator), Expr.Literal(r.Denominator));
        }
        if (var != null) {
          product = Expr.Mul(product, IExpr2Expr(var));
        }
        return product.IExpr;
      }
    }

    public AI.IFunApp False {
      get {
        Contract.Ensures(Contract.Result<AI.IFunApp>() != null);

        return Expr.False;
      }
    }
    public AI.IFunApp True {
      get {
        Contract.Ensures(Contract.Result<AI.IFunApp>() != null);

        return Expr.True;
      }
    }
    public AI.IFunApp And(AI.IExpr p, AI.IExpr q) {
      //Contract.Requires(p != null);
      //Contract.Requires(q != null);
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);

      return Expr.Binary(BinaryOperator.Opcode.And, IExpr2Expr(p), IExpr2Expr(q));
    }
  }

  public class BoogieTypeFactory : BoogieFactory, AI.ITypeExprFactory {
    /// <summary>
    /// Returns an expression denoting the top of the type hierarchy.
    /// </summary>
    public AI.IExpr RootType {
      get {
        Contract.Assert(false);  // BUGBUG: TODO
        throw new System.NotImplementedException();
      }
    }

    /// <summary>
    /// Returns true iff "t" denotes a type constant.
    /// </summary>
    [Pure]
    public bool IsTypeConstant(AI.IExpr t) {
      //Contract.Requires(t != null);
      Contract.Ensures(Contract.Result<bool>() == (t is Constant));
      return t is Constant;
    }

    /// <summary>
    /// Returns true iff t0 and t1 are types such that t0 and t1 are equal.
    /// </summary>
    [Pure]
    public bool IsTypeEqual(AI.IExpr t0, AI.IExpr t1) {
      //Contract.Requires(t0 != null);
      //Contract.Requires(t1 != null);
      Constant c0 = t0 as Constant;
      Constant c1 = t1 as Constant;
      return c0 != null && c1 != null && c0 == c1;
    }

    /// <summary>
    /// Returns true iff t0 and t1 are types such that t0 is a subtype of t1.
    /// </summary>
    [Pure]
    public bool IsSubType(AI.IExpr t0, AI.IExpr t1) {
      //Contract.Requires(t0 != null);
      //Contract.Requires(t1 != null);

      Contract.Assert(false);  // BUGBUG: TODO
      throw new cce.UnreachableException();
    }

    /// <summary>
    /// Returns the most derived supertype of both "t0" and "t1".  A precondition is
    /// that "t0" and "t1" both represent types.
    /// </summary>
    public AI.IExpr JoinTypes(AI.IExpr t0, AI.IExpr t1) {
      //Contract.Requires(t0 != null);
      //Contract.Requires(t1 != null);
      Contract.Ensures(Contract.Result<AI.IExpr>() != null);


      Contract.Assert(false);  // BUGBUG: TODO
      throw new cce.UnreachableException();
    }

    public AI.IFunApp IsExactlyA(AI.IExpr e, AI.IExpr type) {
      //PM: We need this assume because Boogie does not yet allow us to use the
      //PM: inherited precondition "requires IsTypeConstant(type)".
      //PM: Note that that precondition is currently commented out.
      //Contract.Requires(e != null);
      //Contract.Requires(type != null);
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);

      Contract.Assume(type is Constant);
      Constant theType = (Constant)type;
      Contract.Assert(false);
      throw new cce.UnreachableException();
      Expr typeofExpr = new NAryExpr(Token.NoToken,
          new FunctionCall(new IdentifierExpr(Token.NoToken, "$typeof", Type.Int // Name
                           )),
          new ExprSeq(IExpr2Expr(e)));
      return Expr.Eq(typeofExpr, Expr.Ident(theType));
    }

    public AI.IFunApp IsA(AI.IExpr e, AI.IExpr type) {
      //Contract.Requires(e != null);
      //Contract.Requires(type != null);
      Contract.Ensures(Contract.Result<AI.IFunApp>() != null);
      //PM: We need this assume because Boogie does not yet allow us to use the
      //PM: inherited precondition "requires IsTypeConstant(type)".
      //PM: Note that that precondition is currently commented out.
      

      Contract.Assume(type is Constant);
      Contract.Assert(false);
      throw new cce.UnreachableException();
      Expr typeofExpr = new NAryExpr(Token.NoToken,
          new FunctionCall(new IdentifierExpr(Token.NoToken, "$typeof", Type.Int // Name
                           )),
          new ExprSeq(IExpr2Expr(e)));
      return new NAryExpr(Token.NoToken,
          new BinaryOperator(Token.NoToken, BinaryOperator.Opcode.Subtype),
          new ExprSeq(typeofExpr, IExpr2Expr(e)));
    }
  }
}