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