001// ***** This file is automatically generated from Range.java.jpp
002
003package daikon.inv.unary.scalar;
004
005import org.checkerframework.checker.interning.qual.Interned;
006import org.checkerframework.checker.lock.qual.GuardSatisfied;
007import org.checkerframework.checker.nullness.qual.Nullable;
008import org.checkerframework.dataflow.qual.Pure;
009import org.checkerframework.dataflow.qual.SideEffectFree;
010import daikon.*;
011import daikon.Quantify.QuantFlags;
012import daikon.derive.unary.*;
013import daikon.inv.*;
014import daikon.inv.binary.sequenceScalar.*;
015import daikon.inv.unary.sequence.*;
016import java.util.*;
017import java.util.logging.Level;
018import java.util.logging.Logger;
019import org.plumelib.util.Intern;
020import org.plumelib.util.UtilPlume;
021import typequals.prototype.qual.NonPrototype;
022import typequals.prototype.qual.Prototype;
023
024/**
025 * Baseclass for unary range based invariants. Each invariant is a special stateless version of
026 * bound or oneof. For example EqualZero, BooleanVal, etc). These are never printed, but are used
027 * internally as suppressors for ni-suppressions.
028 *
029 * Each specific invariant is implemented in a subclass (typically in this file).
030 */
031
032public abstract class RangeInt extends SingleScalar {
033
034  static final long serialVersionUID = 20040311L;
035
036  protected RangeInt(PptSlice ppt) {
037    super(ppt);
038  }
039
040  protected @Prototype RangeInt() {
041    super();
042  }
043
044  @Override
045  public boolean instantiate_ok(VarInfo[] vis) {
046
047    if (!valid_types(vis)) {
048      return false;
049    }
050
051    if (!vis[0].file_rep_type.baseIsIntegral()) {
052      return false;
053    }
054
055    return true;
056  }
057
058  /**
059   * Returns a string in the specified format that describes the invariant.
060   *
061   * The generic format string is obtained from the subclass specific get_format_str(). Instances of
062   * %var1% are replaced by the variable name in the specified format.
063   */
064  @SideEffectFree
065  @Override
066  public String format_using(@GuardSatisfied RangeInt this, OutputFormat format) {
067
068    String fmt_str = get_format_str(format);
069
070    VarInfo var1 = ppt.var_infos[0];
071    String v1 = null;
072
073    if (v1 == null) {
074      v1 = var1.name_using(format);
075    }
076
077    fmt_str = fmt_str.replace("%var1%", v1);
078    return fmt_str;
079  }
080
081  @Override
082  public InvariantStatus check_modified(long x, int count) {
083    if (eq_check(x)) {
084      return InvariantStatus.NO_CHANGE;
085    } else {
086      return InvariantStatus.FALSIFIED;
087    }
088  }
089
090  @Override
091  public InvariantStatus add_modified(long x, int count) {
092    return check_modified(x, count);
093  }
094
095  @Override
096  protected double computeConfidence() {
097    if (ppt.num_samples() == 0) {
098      return Invariant.CONFIDENCE_UNJUSTIFIED;
099    }
100    return CONFIDENCE_JUSTIFIED;
101  }
102
103  @Pure
104  @Override
105  public boolean isSameFormula(Invariant other) {
106    assert other.getClass() == getClass();
107    return true;
108  }
109  @Pure
110  @Override
111  public boolean isExclusiveFormula(Invariant other) {
112    return false;
113  }
114
115  /**
116   * All range invariants except Even and PowerOfTwo are obvious since they are represented by some
117   * version of OneOf or Bound.
118   */
119  @Pure
120  @Override
121  public @Nullable DiscardInfo isObviousDynamically(VarInfo[] vis) {
122
123    return new DiscardInfo(this, DiscardCode.obvious,
124                            "Implied by Oneof or Bound");
125  }
126
127  /**
128   * Looks for a OneOf invariant over vis. Used by Even and PowerOfTwo to dynamically suppress those
129   * invariants if a OneOf exists.
130   */
131  protected @Nullable OneOfScalar find_oneof(VarInfo[] vis) {
132    return (OneOfScalar) ppt.parent.find_inv_by_class(vis, OneOfScalar.class);
133  }
134
135  /**
136   * Returns a format string for the specified output format. Each instance of %varN% will be
137   * replaced by the correct name for varN.
138   */
139  public abstract String get_format_str(@GuardSatisfied RangeInt this, OutputFormat format);
140
141  /** Returns true if x and y don't invalidate the invariant. */
142  public abstract boolean eq_check(long x);
143
144  /**
145   * Returns a list of prototypes of all of the range
146   * invariants.
147   */
148  public static List<@Prototype Invariant> get_proto_all() {
149
150    List<@Prototype Invariant> result = new ArrayList<>();
151    result.add(EqualZero.get_proto());
152    result.add(EqualOne.get_proto());
153    result.add(EqualMinusOne.get_proto());
154    result.add(GreaterEqualZero.get_proto());
155    result.add(GreaterEqual64.get_proto());
156
157      result.add(BooleanVal.get_proto());
158      result.add(PowerOfTwo.get_proto());
159      result.add(Even.get_proto());
160      result.add(Bound0_63.get_proto());
161
162    return result;
163  }
164
165  /**
166   * Internal invariant representing long scalars that are equal to zero. Used for
167   * non-instantiating suppressions. Will never print since OneOf accomplishes the same thing.
168   */
169  public static class EqualZero extends RangeInt {
170
171    // We are Serializable, so we specify a version to allow changes to
172    // method signatures without breaking serialization.  If you add or
173    // remove fields, you should change this number to the current date.
174    static final long serialVersionUID = 20040113L;
175
176    protected EqualZero(PptSlice ppt) {
177      super(ppt);
178    }
179
180    protected @Prototype EqualZero() {
181      super();
182    }
183
184    private static @Prototype EqualZero proto = new @Prototype EqualZero();
185
186    /** returns the prototype invariant */
187    public static @Prototype EqualZero get_proto() {
188      return proto;
189    }
190
191    @Override
192    public boolean enabled() {
193      return OneOfScalar.dkconfig_enabled;
194    }
195
196    @Override
197    public EqualZero instantiate_dyn(@Prototype EqualZero this, PptSlice slice) {
198      return new EqualZero(slice);
199    }
200
201    @Override
202    public String get_format_str(@GuardSatisfied EqualZero this, OutputFormat format) {
203      if (format == OutputFormat.SIMPLIFY) {
204        return "(EQ 0 %var1%)";
205      } else {
206        return "%var1% == 0";
207      }
208    }
209
210    @Override
211    public boolean eq_check(long x) {
212      return x == 0;
213    }
214  }
215
216  /**
217   * Internal invariant representing long scalars that are equal to one. Used for
218   * non-instantiating suppressions. Will never print since OneOf accomplishes the same thing.
219   */
220  public static class EqualOne extends RangeInt {
221
222    // We are Serializable, so we specify a version to allow changes to
223    // method signatures without breaking serialization.  If you add or
224    // remove fields, you should change this number to the current date.
225    static final long serialVersionUID = 20040113L;
226
227    protected EqualOne(PptSlice ppt) {
228      super(ppt);
229    }
230
231    protected @Prototype EqualOne() {
232      super();
233    }
234
235    private static @Prototype EqualOne proto = new @Prototype EqualOne();
236
237    /** returns the prototype invariant */
238    public static @Prototype EqualOne get_proto() {
239      return proto;
240    }
241
242    @Override
243    public boolean enabled() {
244      return OneOfScalar.dkconfig_enabled;
245    }
246
247    @Override
248    public EqualOne instantiate_dyn(@Prototype EqualOne this, PptSlice slice) {
249      return new EqualOne(slice);
250    }
251
252    @Override
253    public String get_format_str(@GuardSatisfied EqualOne this, OutputFormat format) {
254      if (format == OutputFormat.SIMPLIFY) {
255        return "(EQ 1 %var1%)";
256      } else {
257        return "%var1% == 1";
258      }
259    }
260
261    @Override
262    public boolean eq_check(long x) {
263      return x == 1;
264    }
265  }
266
267  /**
268   * Internal invariant representing long scalars that are equal to minus one. Used for
269   * non-instantiating suppressions. Will never print since OneOf accomplishes the same thing.
270   */
271  public static class EqualMinusOne extends RangeInt {
272
273    // We are Serializable, so we specify a version to allow changes to
274    // method signatures without breaking serialization.  If you add or
275    // remove fields, you should change this number to the current date.
276    static final long serialVersionUID = 20040824L;
277
278    protected EqualMinusOne(PptSlice ppt) {
279      super(ppt);
280    }
281
282    protected @Prototype EqualMinusOne() {
283      super();
284    }
285
286    private static @Prototype EqualMinusOne proto = new @Prototype EqualMinusOne();
287
288    /** returns the prototype invariant */
289    public static @Prototype EqualMinusOne get_proto() {
290      return proto;
291    }
292
293    @Override
294    public boolean enabled() {
295      return OneOfScalar.dkconfig_enabled;
296    }
297
298    @Override
299    public EqualMinusOne instantiate_dyn(@Prototype EqualMinusOne this, PptSlice slice) {
300      return new EqualMinusOne(slice);
301    }
302
303    @Override
304    public String get_format_str(@GuardSatisfied EqualMinusOne this, OutputFormat format) {
305      if (format == OutputFormat.SIMPLIFY) {
306        return "(EQ -1 %var1%)";
307      } else {
308        return "%var1% == -1";
309      }
310    }
311
312    @Override
313    public boolean eq_check(long x) {
314      return x == -1;
315    }
316  }
317
318  /**
319   * Internal invariant representing long scalars that are greater than or equal to 0. Used
320   * for non-instantiating suppressions. Will never print since Bound accomplishes the same thing.
321   */
322  public static class GreaterEqualZero extends RangeInt {
323
324    // We are Serializable, so we specify a version to allow changes to
325    // method signatures without breaking serialization.  If you add or
326    // remove fields, you should change this number to the current date.
327    static final long serialVersionUID = 20040113L;
328
329    protected GreaterEqualZero(PptSlice ppt) {
330      super(ppt);
331    }
332
333    protected @Prototype GreaterEqualZero() {
334      super();
335    }
336
337    private static @Prototype GreaterEqualZero proto = new @Prototype GreaterEqualZero();
338
339    /** returns the prototype invariant */
340    public static @Prototype GreaterEqualZero get_proto() {
341      return proto;
342    }
343
344    @Override
345    public boolean enabled() {
346      return LowerBound.dkconfig_enabled;
347    }
348
349    @Override
350    public GreaterEqualZero instantiate_dyn(@Prototype GreaterEqualZero this, PptSlice slice) {
351      return new GreaterEqualZero(slice);
352    }
353
354    @Override
355    public String get_format_str(@GuardSatisfied GreaterEqualZero this, OutputFormat format) {
356      if (format == OutputFormat.SIMPLIFY) {
357        return "(>= %var1% 0)";
358      } else {
359        return "%var1% >= 0";
360      }
361    }
362
363    @Override
364    public boolean eq_check(long x) {
365      return x >= 0;
366    }
367  }
368
369  /**
370   * Internal invariant representing long scalars that are greater than or equal to 64. Used
371   * for non-instantiating suppressions. Will never print since Bound accomplishes the same thing.
372   */
373  public static class GreaterEqual64 extends RangeInt {
374
375    // We are Serializable, so we specify a version to allow changes to
376    // method signatures without breaking serialization.  If you add or
377    // remove fields, you should change this number to the current date.
378    static final long serialVersionUID = 20040113L;
379
380    protected GreaterEqual64(PptSlice ppt) {
381      super(ppt);
382    }
383
384    protected @Prototype GreaterEqual64() {
385      super();
386    }
387
388    private static @Prototype GreaterEqual64 proto = new @Prototype GreaterEqual64();
389
390    /** returns the prototype invariant */
391    public static @Prototype GreaterEqual64 get_proto() {
392      return proto;
393    }
394
395    @Override
396    public boolean enabled() {
397      return LowerBound.dkconfig_enabled;
398    }
399
400    @Override
401    public GreaterEqual64 instantiate_dyn(@Prototype GreaterEqual64 this, PptSlice slice) {
402      return new GreaterEqual64(slice);
403    }
404
405    @Override
406    public String get_format_str(@GuardSatisfied GreaterEqual64 this, OutputFormat format) {
407      if (format == OutputFormat.SIMPLIFY) {
408        return "(>= %var1% 64)";
409      } else {
410        return "%var1% >= 64";
411      }
412    }
413
414    @Override
415    public boolean eq_check(long x) {
416      return x >= 64;
417    }
418  }
419
420  /**
421   * Internal invariant representing longs whose values are always 0 or 1. Used for
422   * non-instantiating suppressions. Will never print since OneOf accomplishes the same thing.
423   */
424  public static class BooleanVal extends RangeInt {
425
426    // We are Serializable, so we specify a version to allow changes to
427    // method signatures without breaking serialization.  If you add or
428    // remove fields, you should change this number to the current date.
429    static final long serialVersionUID = 20040113L;
430
431    protected BooleanVal(PptSlice ppt) {
432      super(ppt);
433    }
434
435    protected @Prototype BooleanVal() {
436      super();
437    }
438
439    private static @Prototype BooleanVal proto = new @Prototype BooleanVal();
440
441    /** returns the prototype invariant */
442    public static @Prototype BooleanVal get_proto() {
443      return proto;
444    }
445
446    @Override
447    public boolean enabled() {
448      return LowerBound.dkconfig_enabled && UpperBound.dkconfig_enabled;
449    }
450
451    @Override
452    public BooleanVal instantiate_dyn(@Prototype BooleanVal this, PptSlice slice) {
453      return new BooleanVal(slice);
454    }
455
456    @Override
457    public String get_format_str(@GuardSatisfied BooleanVal this, OutputFormat format) {
458      if (format == OutputFormat.SIMPLIFY) {
459        return "(OR (EQ 0 %var1%) (EQ 1 %var1%))";
460      } else {
461        return "%var1% is boolean";
462      }
463    }
464
465    @Override
466    public boolean eq_check(long x) {
467      return (x == 0) || (x == 1);
468    }
469  }
470
471  /**
472   * Invariant representing longs whose values are always a power of 2 (exactly one bit is set).
473   * Used for non-instantiating suppressions. Since this is not covered by the Bound or OneOf
474   * invariants it is printed. Prints as {@code x is a power of 2}.
475   */
476  public static class PowerOfTwo extends RangeInt {
477
478    // We are Serializable, so we specify a version to allow changes to
479    // method signatures without breaking serialization.  If you add or
480    // remove fields, you should change this number to the current date.
481    static final long serialVersionUID = 20040113L;
482
483    /** Boolean. True if PowerOfTwo invariants should be considered. */
484    public static boolean dkconfig_enabled = Invariant.invariantEnabledDefault;
485
486    protected PowerOfTwo(PptSlice ppt) {
487      super(ppt);
488    }
489
490    protected @Prototype PowerOfTwo() {
491      super();
492    }
493
494    private static @Prototype PowerOfTwo proto = new @Prototype PowerOfTwo();
495
496    /** returns the prototype invariant */
497    public static @Prototype PowerOfTwo get_proto() {
498      return proto;
499    }
500
501    @Override
502    public boolean enabled() {
503      return dkconfig_enabled;
504    }
505
506    @Override
507    public PowerOfTwo instantiate_dyn(@Prototype PowerOfTwo this, PptSlice slice) {
508      return new PowerOfTwo(slice);
509    }
510
511    @Override
512    public String get_format_str(@GuardSatisfied PowerOfTwo this, OutputFormat format) {
513      if (format == OutputFormat.SIMPLIFY) {
514        return "(EXISTS (p) (EQ %var1% (pow 2 p)))";
515      }
516      if (format == OutputFormat.JAVA) {
517        return "daikon.tools.runtimechecker.Runtime.isPowerOfTwo(%var1%)";
518      }
519      if (format == OutputFormat.CSHARPCONTRACT) {
520        return "%var1%.IsPowerOfTwo()";
521      } else {
522        return "%var1% is a power of 2";
523      }
524    }
525
526    /**
527     * Returns true if x is a power of 2 (has one bit on). The check is to and x with itself -
528     * 1. The theory is that if there are multiple bits turned on, at least one of those bits is
529     * unmodified by a subtract operation and thus the bitwise-and will be non-zero. There is probably a
530     * more elegant way to do this.
531     */
532    @Override
533    public boolean eq_check(long x) {
534      return (x >= 1) && ((x & (x - 1)) == 0);
535    }
536
537    /**
538     * Since PowerOfTwo is not covered by Bound or OneOf, it is not obvious
539     * (and should thus be printed).
540     */
541    @Pure
542    @Override
543    public @Nullable DiscardInfo isObviousDynamically(VarInfo[] vis) {
544
545       OneOfScalar oneof = find_oneof(vis);
546       if (oneof != null) {
547         return new DiscardInfo(this, DiscardCode.obvious, "Implied by Oneof");
548       }
549
550       return null;
551     }
552
553  }
554
555  /**
556   * Invariant representing longs whose values are always even. Used for non-instantiating
557   * suppressions. Since this is not covered by the Bound or OneOf invariants it is printed. Prints
558   * as {@code x is even}.
559   */
560  public static class Even extends RangeInt {
561
562    // We are Serializable, so we specify a version to allow changes to
563    // method signatures without breaking serialization.  If you add or
564    // remove fields, you should change this number to the current date.
565    static final long serialVersionUID = 20040113L;
566
567    /** Boolean. True if Even invariants should be considered. */
568    public static boolean dkconfig_enabled = false;
569
570    protected Even(PptSlice ppt) {
571      super(ppt);
572    }
573
574    protected @Prototype Even() {
575      super();
576    }
577
578    private static @Prototype Even proto = new @Prototype Even();
579
580    /** returns the prototype invariant */
581    public static @Prototype Even get_proto() {
582      return proto;
583    }
584
585    @Override
586    public boolean enabled() {
587      return dkconfig_enabled;
588    }
589
590    @Override
591    public Even instantiate_dyn(@Prototype Even this, PptSlice slice) {
592      return new Even(slice);
593    }
594
595    @Override
596    public String get_format_str(@GuardSatisfied Even this, OutputFormat format) {
597      if (format == OutputFormat.SIMPLIFY) {
598        return "(EQ (MOD %var1% 2) 0)";
599      }
600      if (format == OutputFormat.CSHARPCONTRACT) {
601        return "%var1% % 2 == 0";
602      } else {
603        return "%var1% is even";
604      }
605    }
606
607    @Override
608    public boolean eq_check(long x) {
609      return (x & 1) == 0;
610    }
611
612    /**
613     * Since Even is not covered by Bound or OneOf, it is not obvious
614     * (and should thus be printed).
615     */
616    @Pure
617    @Override
618    public @Nullable DiscardInfo isObviousDynamically(VarInfo[] vis) {
619       // If there is a oneof, it implies this
620       OneOfScalar oneof = find_oneof(vis);
621       if (oneof != null) {
622         return new DiscardInfo(this, DiscardCode.obvious, "Implied by Oneof");
623       }
624
625       return null;
626     }
627  }
628
629  /**
630   * Internal invariant representing longs whose values are between 0 and 63. Used for
631   * non-instantiating suppressions. Will never print since Bound accomplishes the same thing.
632   */
633  public static class Bound0_63 extends RangeInt {
634
635    // We are Serializable, so we specify a version to allow changes to
636    // method signatures without breaking serialization.  If you add or
637    // remove fields, you should change this number to the current date.
638    static final long serialVersionUID = 20040113L;
639
640    protected Bound0_63(PptSlice ppt) {
641      super(ppt);
642    }
643
644    protected @Prototype Bound0_63() {
645      super();
646    }
647
648    private static @Prototype Bound0_63 proto = new @Prototype Bound0_63();
649
650    /** returns the prototype invariant */
651    public static @Prototype Bound0_63 get_proto() {
652      return proto;
653    }
654
655    @Override
656    public boolean enabled() {
657      return LowerBound.dkconfig_enabled && UpperBound.dkconfig_enabled;
658    }
659
660    @Override
661    public Bound0_63 instantiate_dyn(@Prototype Bound0_63 this, PptSlice slice) {
662      return new Bound0_63(slice);
663    }
664
665    @Override
666    public String get_format_str(@GuardSatisfied Bound0_63 this, OutputFormat format) {
667      if (format == OutputFormat.SIMPLIFY) {
668        return "(AND (>= %var1% 0) (>= 63 %var1%))";
669      } else {
670        return "0 <= %var1% <= 63";
671      }
672    }
673
674    @Override
675    public boolean eq_check(long x) {
676      return (x >= 0) && (x <= 63);
677    }
678  }
679
680}