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