1use serde::Serialize;
4
5use crate::plan::CmpOp;
6
7pub const RUNTIME_MOD: &str = "__tmt_rt";
9
10#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
11#[serde(rename_all = "snake_case")]
12pub enum LiteralSide {
13 None,
14 Rhs(i128),
15 Lhs(i128),
16}
17
18#[derive(Clone, Copy, PartialEq, Eq, Debug)]
19pub enum BitValueType {
20 Unsigned(u32),
21 Signed(u32),
22 Bool,
23 Char,
24}
25
26impl BitValueType {
27 pub(crate) fn width(self) -> u32 {
28 match self {
29 BitValueType::Unsigned(w) | BitValueType::Signed(w) => w,
30 BitValueType::Bool => 1,
31 BitValueType::Char => 21,
32 }
33 }
34}
35
36#[derive(Clone, Copy, PartialEq, Eq, Debug)]
37pub enum LocalBitType {
38 Value(BitValueType),
39 SliceOf(BitValueType),
40}
41
42#[derive(Clone, PartialEq, Eq, Debug)]
43pub enum SiteKind {
44 Cmp {
45 op: CmpOp,
46 lhs: String,
47 rhs: String,
48 literal: LiteralSide,
49 gap: (usize, usize),
51 },
52 Logic {
54 and: bool,
55 lhs: String,
56 rhs: String,
57 gap: (usize, usize),
58 },
59 Arith {
62 op: ArithOp,
63 assign: bool,
65 lhs: String,
66 rhs: String,
67 gap: (usize, usize),
68 local_types: Vec<(String, LocalBitType)>,
70 shadowed_prims: Vec<String>,
72 cast_probes: Vec<(u32, usize, usize)>,
74 },
75 Not {
77 bang: (usize, usize),
79 operand: String,
80 needs_space: bool,
82 },
83 Return {
86 name: String,
87 option: Option<String>,
90 sole_expr: Option<(usize, usize)>,
93 },
94 MinMax {
96 max: bool,
97 receiver: String,
98 argument: String,
99 call: (usize, usize),
101 close: (usize, usize),
102 },
103 SliceEnd {
106 first: bool,
107 receiver: String,
108 call: (usize, usize),
110 method: (usize, usize),
112 },
113 StrAffix {
115 starts: bool,
116 receiver: String,
117 pattern: String,
118 call: (usize, usize),
120 close: (usize, usize),
122 method: (usize, usize),
124 },
125 StrReplace {
127 replace_all: bool,
129 receiver: String,
130 from: String,
131 to: String,
132 call: (usize, usize),
134 comma: (usize, usize),
136 close: (usize, usize),
138 method: (usize, usize),
140 },
141 Clamp {
143 receiver: String,
144 low: String,
145 high: String,
146 call: (usize, usize),
148 comma: (usize, usize),
150 close: (usize, usize),
152 },
153 Rounding {
155 op: RoundingOp,
156 receiver: String,
157 call: (usize, usize),
159 method: (usize, usize),
161 },
162 Quantifier {
164 all: bool,
165 receiver: String,
166 predicate: String,
167 call: (usize, usize),
169 close: (usize, usize),
171 method: (usize, usize),
173 },
174 Position {
177 forward: bool,
179 receiver: String,
180 predicate: String,
181 call: (usize, usize),
183 close: (usize, usize),
185 method: (usize, usize),
187 },
188 SaturatingArith {
190 op: SaturatingOp,
191 receiver: String,
192 argument: String,
193 call: (usize, usize),
195 close: (usize, usize),
197 method: (usize, usize),
199 },
200 StrTrim {
202 op: TrimOp,
203 receiver: String,
204 call: (usize, usize),
206 method: (usize, usize),
208 },
209 StrCaseEq {
211 receiver: String,
212 argument: String,
213 call: (usize, usize),
215 close: (usize, usize),
217 method: (usize, usize),
219 },
220 Abs {
222 receiver: String,
223 call: (usize, usize),
225 method: (usize, usize),
227 },
228 IterLimit {
230 take: bool,
231 receiver: String,
232 count: String,
233 call: (usize, usize),
235 close: (usize, usize),
237 method: (usize, usize),
239 },
240 EndianSwap {
242 op: EndianOp,
243 receiver: String,
244 call: (usize, usize),
246 method: (usize, usize),
248 },
249 BitCount {
251 op: BitCountOp,
252 receiver: String,
253 call: (usize, usize),
255 method: (usize, usize),
257 },
258 LoopCtrl { is_break: bool },
260 LiteralSet {
262 receiver: String,
263 argument: String,
264 call: (usize, usize),
266 close: (usize, usize),
268 elements: Vec<(String, usize, usize)>,
270 },
271 RangeContains {
273 inclusive: bool,
274 lhs: String,
275 rhs: String,
276 argument: String,
277 open: (usize, usize),
279 gap: (usize, usize),
281 call: (usize, usize),
283 close: (usize, usize),
285 },
286 Range {
289 inclusive: bool,
290 lhs: String,
291 rhs: String,
292 gap: (usize, usize),
293 index: Option<RangeIndex>,
296 },
297 Arm {
300 pattern: String,
301 guard: ArmGuard,
303 removal: (usize, usize),
304 },
305 Stmt,
307 Neg {
309 minus: (usize, usize),
311 operand: String,
312 literal: bool,
314 needs_space: bool,
316 },
317 Guard {
319 can_be_false: bool,
322 },
323 Field {
325 name: String,
326 shorthand: bool,
328 from_base: Option<String>,
330 removal: (usize, usize),
332 },
333 Body {
336 name: String,
337 value: String,
338 sole_expr: Option<(usize, usize)>,
342 },
343}
344
345#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
346#[serde(rename_all = "snake_case")]
347pub enum RoundingOp {
348 Floor,
349 Ceil,
350 Round,
351 Trunc,
352}
353
354impl RoundingOp {
355 pub fn code(self) -> u8 {
356 match self {
357 RoundingOp::Floor => 0,
358 RoundingOp::Ceil => 1,
359 RoundingOp::Round => 2,
360 RoundingOp::Trunc => 3,
361 }
362 }
363
364 pub fn name(self) -> &'static str {
365 match self {
366 RoundingOp::Floor => "floor",
367 RoundingOp::Ceil => "ceil",
368 RoundingOp::Round => "round",
369 RoundingOp::Trunc => "trunc",
370 }
371 }
372
373 pub fn alt1(self) -> RoundingOp {
374 match self {
375 RoundingOp::Floor => RoundingOp::Trunc,
376 RoundingOp::Ceil => RoundingOp::Floor,
377 RoundingOp::Round => RoundingOp::Floor,
378 RoundingOp::Trunc => RoundingOp::Floor,
379 }
380 }
381
382 pub fn alt2(self) -> RoundingOp {
383 match self {
384 RoundingOp::Floor => RoundingOp::Ceil,
385 RoundingOp::Ceil => RoundingOp::Trunc,
386 RoundingOp::Round => RoundingOp::Trunc,
387 RoundingOp::Trunc => RoundingOp::Round,
388 }
389 }
390
391 pub fn differs_for(self, other: RoundingOp) -> &'static str {
393 match (self, other) {
394 (RoundingOp::Floor, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Floor) => {
395 "negative and not a whole number (e.g. -0.5)"
396 }
397 (RoundingOp::Ceil, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Ceil) => {
398 "positive and not a whole number (e.g. 0.5)"
399 }
400 (RoundingOp::Floor, RoundingOp::Round) | (RoundingOp::Round, RoundingOp::Floor) => {
401 "a number that rounds up (e.g. 1.5 or -1.4)"
402 }
403 (RoundingOp::Ceil, RoundingOp::Round) | (RoundingOp::Round, RoundingOp::Ceil) => {
404 "a number that rounds down (e.g. 1.4 or -1.6)"
405 }
406 (RoundingOp::Round, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Round) => {
407 "a number that rounds away from zero (e.g. 1.5 or -1.6)"
408 }
409 _ => "not a whole number (e.g. 0.5)",
410 }
411 }
412}
413
414#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
415#[serde(rename_all = "snake_case")]
416pub enum SaturatingOp {
417 SatAdd,
418 SatSub,
419 SatMul,
420 WrapAdd,
421 WrapSub,
422 WrapMul,
423}
424
425impl SaturatingOp {
426 pub fn code(self) -> u8 {
427 match self {
428 SaturatingOp::SatAdd => 0,
429 SaturatingOp::SatSub => 1,
430 SaturatingOp::SatMul => 2,
431 SaturatingOp::WrapAdd => 3,
432 SaturatingOp::WrapSub => 4,
433 SaturatingOp::WrapMul => 5,
434 }
435 }
436
437 pub fn name(self) -> &'static str {
438 match self {
439 SaturatingOp::SatAdd => "saturating_add",
440 SaturatingOp::SatSub => "saturating_sub",
441 SaturatingOp::SatMul => "saturating_mul",
442 SaturatingOp::WrapAdd => "wrapping_add",
443 SaturatingOp::WrapSub => "wrapping_sub",
444 SaturatingOp::WrapMul => "wrapping_mul",
445 }
446 }
447
448 pub fn swapped(self) -> Self {
449 match self {
450 SaturatingOp::SatAdd => SaturatingOp::WrapAdd,
451 SaturatingOp::SatSub => SaturatingOp::WrapSub,
452 SaturatingOp::SatMul => SaturatingOp::WrapMul,
453 SaturatingOp::WrapAdd => SaturatingOp::SatAdd,
454 SaturatingOp::WrapSub => SaturatingOp::SatSub,
455 SaturatingOp::WrapMul => SaturatingOp::SatMul,
456 }
457 }
458
459 pub fn overflow_phrase(self) -> &'static str {
460 match self {
461 SaturatingOp::SatAdd | SaturatingOp::WrapAdd => "whose sum overflows",
462 SaturatingOp::SatSub | SaturatingOp::WrapSub => {
463 "whose difference underflows or overflows"
464 }
465 SaturatingOp::SatMul | SaturatingOp::WrapMul => "whose product overflows",
466 }
467 }
468}
469
470#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
471#[serde(rename_all = "snake_case")]
472pub enum TrimOp {
473 Trim,
474 TrimStart,
475 TrimEnd,
476}
477
478impl TrimOp {
479 pub fn code(self) -> u8 {
480 match self {
481 TrimOp::Trim => 0,
482 TrimOp::TrimStart => 1,
483 TrimOp::TrimEnd => 2,
484 }
485 }
486
487 pub fn name(self) -> &'static str {
488 match self {
489 TrimOp::Trim => "trim",
490 TrimOp::TrimStart => "trim_start",
491 TrimOp::TrimEnd => "trim_end",
492 }
493 }
494
495 pub fn alt1(self) -> Self {
496 match self {
497 TrimOp::Trim => TrimOp::TrimStart,
498 TrimOp::TrimStart | TrimOp::TrimEnd => TrimOp::Trim,
499 }
500 }
501
502 pub fn alt2(self) -> Option<Self> {
503 match self {
504 TrimOp::Trim => Some(TrimOp::TrimEnd),
505 TrimOp::TrimStart | TrimOp::TrimEnd => None,
506 }
507 }
508
509 pub fn whitespace_phrase(self, alt: TrimOp) -> &'static str {
510 match (self, alt) {
511 (TrimOp::Trim, TrimOp::TrimStart) | (TrimOp::TrimStart, TrimOp::Trim) => {
512 "trailing whitespace"
513 }
514 (TrimOp::Trim, TrimOp::TrimEnd) | (TrimOp::TrimEnd, TrimOp::Trim) => {
515 "leading whitespace"
516 }
517 _ => "leading or trailing whitespace",
518 }
519 }
520}
521
522#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
523#[serde(rename_all = "snake_case")]
524pub enum EndianOp {
525 ToBeBytes,
526 ToLeBytes,
527 ToBe,
528 ToLe,
529}
530
531impl EndianOp {
532 pub fn code(self) -> u8 {
533 match self {
534 EndianOp::ToBeBytes => 0,
535 EndianOp::ToLeBytes => 1,
536 EndianOp::ToBe => 2,
537 EndianOp::ToLe => 3,
538 }
539 }
540
541 pub fn name(self) -> &'static str {
542 match self {
543 EndianOp::ToBeBytes => "to_be_bytes",
544 EndianOp::ToLeBytes => "to_le_bytes",
545 EndianOp::ToBe => "to_be",
546 EndianOp::ToLe => "to_le",
547 }
548 }
549
550 pub fn swapped(self) -> Self {
551 match self {
552 EndianOp::ToBeBytes => EndianOp::ToLeBytes,
553 EndianOp::ToLeBytes => EndianOp::ToBeBytes,
554 EndianOp::ToBe => EndianOp::ToLe,
555 EndianOp::ToLe => EndianOp::ToBe,
556 }
557 }
558}
559
560#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
561#[serde(rename_all = "snake_case")]
562pub enum BitCountOp {
563 LeadingZeros,
564 TrailingZeros,
565 LeadingOnes,
566 TrailingOnes,
567}
568
569impl BitCountOp {
570 pub fn code(self) -> u8 {
571 match self {
572 BitCountOp::LeadingZeros => 0,
573 BitCountOp::TrailingZeros => 1,
574 BitCountOp::LeadingOnes => 2,
575 BitCountOp::TrailingOnes => 3,
576 }
577 }
578
579 pub fn name(self) -> &'static str {
580 match self {
581 BitCountOp::LeadingZeros => "leading_zeros",
582 BitCountOp::TrailingZeros => "trailing_zeros",
583 BitCountOp::LeadingOnes => "leading_ones",
584 BitCountOp::TrailingOnes => "trailing_ones",
585 }
586 }
587
588 pub fn swapped(self) -> Self {
589 match self {
590 BitCountOp::LeadingZeros => BitCountOp::TrailingZeros,
591 BitCountOp::TrailingZeros => BitCountOp::LeadingZeros,
592 BitCountOp::LeadingOnes => BitCountOp::TrailingOnes,
593 BitCountOp::TrailingOnes => BitCountOp::LeadingOnes,
594 }
595 }
596}
597
598#[derive(Clone, Copy, PartialEq, Eq, Debug)]
599pub enum RangeIndexMode {
600 Ref,
602 RefMut,
604 Deref,
606 Get,
608 GetMut,
610}
611
612#[derive(Clone, PartialEq, Eq, Debug)]
613pub struct RangeIndex {
614 pub mode: RangeIndexMode,
615 pub base: String,
617 pub head: (usize, usize),
619 pub close_bracket: (usize, usize),
621}
622
623#[derive(Clone, Copy, PartialEq, Eq, Debug)]
624pub enum ArmGuard {
625 None(usize),
627 Existing(usize, usize),
629}
630
631#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
632#[serde(rename_all = "snake_case")]
633pub enum ArithOp {
634 Add,
635 Sub,
636 Mul,
637 Div,
638 Rem,
639 BitAnd,
640 BitOr,
641 BitXor,
642 Shl,
643 Shr,
644}
645
646impl ArithOp {
647 pub fn binding(self) -> u8 {
649 match self {
650 ArithOp::Mul | ArithOp::Div | ArithOp::Rem => 5,
651 ArithOp::Add | ArithOp::Sub => 4,
652 ArithOp::Shl | ArithOp::Shr => 3,
653 ArithOp::BitAnd => 2,
654 ArithOp::BitXor => 1,
655 ArithOp::BitOr => 0,
656 }
657 }
658
659 pub const ALL: [ArithOp; 10] = [
660 ArithOp::Add,
661 ArithOp::Sub,
662 ArithOp::Mul,
663 ArithOp::Div,
664 ArithOp::Rem,
665 ArithOp::BitAnd,
666 ArithOp::BitOr,
667 ArithOp::BitXor,
668 ArithOp::Shl,
669 ArithOp::Shr,
670 ];
671
672 pub fn symbol(self) -> &'static str {
673 match self {
674 ArithOp::Add => "+",
675 ArithOp::Sub => "-",
676 ArithOp::Mul => "*",
677 ArithOp::Div => "/",
678 ArithOp::Rem => "%",
679 ArithOp::BitAnd => "&",
680 ArithOp::BitOr => "|",
681 ArithOp::BitXor => "^",
682 ArithOp::Shl => "<<",
683 ArithOp::Shr => ">>",
684 }
685 }
686
687 pub fn swapped(self) -> ArithOp {
689 match self {
690 ArithOp::Add => ArithOp::Sub,
691 ArithOp::Sub => ArithOp::Add,
692 ArithOp::Mul => ArithOp::Div,
693 ArithOp::Div => ArithOp::Mul,
694 ArithOp::Rem => ArithOp::Div,
695 ArithOp::BitAnd => ArithOp::BitOr,
696 ArithOp::BitOr => ArithOp::BitAnd,
697 ArithOp::BitXor => ArithOp::BitAnd,
698 ArithOp::Shl => ArithOp::Shr,
699 ArithOp::Shr => ArithOp::Shl,
700 }
701 }
702
703 pub fn swapped2(self) -> Option<ArithOp> {
705 Some(match self {
706 ArithOp::Add => ArithOp::Mul,
707 ArithOp::Sub => ArithOp::Div,
708 ArithOp::Mul => ArithOp::Add,
709 ArithOp::Div => ArithOp::Rem,
710 ArithOp::Rem => ArithOp::Add,
711 ArithOp::BitAnd | ArithOp::BitOr => ArithOp::BitXor,
712 ArithOp::BitXor => ArithOp::BitOr,
713 ArithOp::Shl | ArithOp::Shr => return None,
714 })
715 }
716
717 pub fn second(self, assign: bool) -> Option<ArithOp> {
721 if assign && matches!(self, ArithOp::BitAnd | ArithOp::BitOr) {
722 return None;
723 }
724 self.swapped2()
725 }
726
727 pub fn is_bitwise(self) -> bool {
729 matches!(
730 self,
731 ArithOp::BitAnd | ArithOp::BitOr | ArithOp::BitXor | ArithOp::Shl | ArithOp::Shr
732 )
733 }
734
735 fn stem(self) -> (&'static str, &'static str) {
737 match self {
738 ArithOp::Add => ("add", "Add"),
739 ArithOp::Sub => ("sub", "Sub"),
740 ArithOp::Mul => ("mul", "Mul"),
741 ArithOp::Div => ("div", "Div"),
742 ArithOp::Rem => ("rem", "Rem"),
743 ArithOp::BitAnd => ("bitand", "BitAnd"),
744 ArithOp::BitOr => ("bitor", "BitOr"),
745 ArithOp::BitXor => ("bitxor", "BitXor"),
746 ArithOp::Shl => ("shl", "Shl"),
747 ArithOp::Shr => ("shr", "Shr"),
748 }
749 }
750
751 pub(crate) fn method(self, assign: bool) -> String {
753 format!(
754 "tmt_{}{}",
755 self.stem().0,
756 if assign { "_assign" } else { "" }
757 )
758 }
759
760 pub(crate) fn fallback_trait(self, assign: bool) -> String {
762 format!("Any{}{}", self.stem().1, if assign { "Assign" } else { "" })
763 }
764
765 pub(super) fn prim_trait(self, assign: bool) -> &'static str {
767 match (self.is_bitwise(), assign) {
768 (false, false) => "ArithPrim",
769 (true, false) => "BitPrim",
770 (false, true) => "ArithAssignPrim",
771 (true, true) => "BitAssignPrim",
772 }
773 }
774}
775
776#[derive(Clone, PartialEq, Eq, Debug)]
777pub struct Site {
778 pub id: u32,
779 pub kind: SiteKind,
780 pub line: usize,
782 pub column: usize,
783 pub start: usize,
785 pub end: usize,
786 pub text: String,
787}
788
789#[derive(Debug, PartialEq, Eq)]
790pub struct TestModule {
791 pub name: String,
792 pub path: Option<String>,
794}
795
796#[derive(Debug, Default)]
797pub struct Discovery {
798 pub sites: Vec<Site>,
799 pub rebuild_sites: Vec<Site>,
804 pub single_statement_bodies: Vec<String>,
807 pub test_modules: Vec<TestModule>,
809}
810
811pub const NO_ID: u32 = u32::MAX;