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 SaturatingArith {
176 op: SaturatingOp,
177 receiver: String,
178 argument: String,
179 call: (usize, usize),
181 close: (usize, usize),
183 method: (usize, usize),
185 },
186 StrTrim {
188 op: TrimOp,
189 receiver: String,
190 call: (usize, usize),
192 method: (usize, usize),
194 },
195 StrCaseEq {
197 receiver: String,
198 argument: String,
199 call: (usize, usize),
201 close: (usize, usize),
203 method: (usize, usize),
205 },
206 Abs {
208 receiver: String,
209 call: (usize, usize),
211 method: (usize, usize),
213 },
214 IterLimit {
216 take: bool,
217 receiver: String,
218 count: String,
219 call: (usize, usize),
221 close: (usize, usize),
223 method: (usize, usize),
225 },
226 EndianSwap {
228 op: EndianOp,
229 receiver: String,
230 call: (usize, usize),
232 method: (usize, usize),
234 },
235 BitCount {
237 op: BitCountOp,
238 receiver: String,
239 call: (usize, usize),
241 method: (usize, usize),
243 },
244 LoopCtrl { is_break: bool },
246 LiteralSet {
248 receiver: String,
249 argument: String,
250 call: (usize, usize),
252 close: (usize, usize),
254 elements: Vec<(String, usize, usize)>,
256 },
257 Range {
260 inclusive: bool,
261 lhs: String,
262 rhs: String,
263 gap: (usize, usize),
264 index: Option<RangeIndex>,
267 },
268 Arm {
271 pattern: String,
272 guard: ArmGuard,
274 removal: (usize, usize),
275 },
276 Stmt,
278 Neg {
280 minus: (usize, usize),
282 operand: String,
283 literal: bool,
285 needs_space: bool,
287 },
288 Guard {
290 can_be_false: bool,
293 },
294 Field {
296 name: String,
297 shorthand: bool,
299 from_base: Option<String>,
301 removal: (usize, usize),
303 },
304 Body {
307 name: String,
308 value: String,
309 sole_expr: Option<(usize, usize)>,
313 },
314}
315
316#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
317#[serde(rename_all = "snake_case")]
318pub enum RoundingOp {
319 Floor,
320 Ceil,
321 Round,
322 Trunc,
323}
324
325impl RoundingOp {
326 pub fn code(self) -> u8 {
327 match self {
328 RoundingOp::Floor => 0,
329 RoundingOp::Ceil => 1,
330 RoundingOp::Round => 2,
331 RoundingOp::Trunc => 3,
332 }
333 }
334
335 pub fn name(self) -> &'static str {
336 match self {
337 RoundingOp::Floor => "floor",
338 RoundingOp::Ceil => "ceil",
339 RoundingOp::Round => "round",
340 RoundingOp::Trunc => "trunc",
341 }
342 }
343
344 pub fn alt1(self) -> RoundingOp {
345 match self {
346 RoundingOp::Floor => RoundingOp::Trunc,
347 RoundingOp::Ceil => RoundingOp::Floor,
348 RoundingOp::Round => RoundingOp::Floor,
349 RoundingOp::Trunc => RoundingOp::Floor,
350 }
351 }
352
353 pub fn alt2(self) -> RoundingOp {
354 match self {
355 RoundingOp::Floor => RoundingOp::Ceil,
356 RoundingOp::Ceil => RoundingOp::Trunc,
357 RoundingOp::Round => RoundingOp::Trunc,
358 RoundingOp::Trunc => RoundingOp::Round,
359 }
360 }
361
362 pub fn differs_for(self, other: RoundingOp) -> &'static str {
364 match (self, other) {
365 (RoundingOp::Floor, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Floor) => {
366 "negative and not a whole number (e.g. -0.5)"
367 }
368 (RoundingOp::Ceil, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Ceil) => {
369 "positive and not a whole number (e.g. 0.5)"
370 }
371 (RoundingOp::Floor, RoundingOp::Round) | (RoundingOp::Round, RoundingOp::Floor) => {
372 "a number that rounds up (e.g. 1.5 or -1.4)"
373 }
374 (RoundingOp::Ceil, RoundingOp::Round) | (RoundingOp::Round, RoundingOp::Ceil) => {
375 "a number that rounds down (e.g. 1.4 or -1.6)"
376 }
377 (RoundingOp::Round, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Round) => {
378 "a number that rounds away from zero (e.g. 1.5 or -1.6)"
379 }
380 _ => "not a whole number (e.g. 0.5)",
381 }
382 }
383}
384
385#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
386#[serde(rename_all = "snake_case")]
387pub enum SaturatingOp {
388 SatAdd,
389 SatSub,
390 SatMul,
391 WrapAdd,
392 WrapSub,
393 WrapMul,
394}
395
396impl SaturatingOp {
397 pub fn code(self) -> u8 {
398 match self {
399 SaturatingOp::SatAdd => 0,
400 SaturatingOp::SatSub => 1,
401 SaturatingOp::SatMul => 2,
402 SaturatingOp::WrapAdd => 3,
403 SaturatingOp::WrapSub => 4,
404 SaturatingOp::WrapMul => 5,
405 }
406 }
407
408 pub fn name(self) -> &'static str {
409 match self {
410 SaturatingOp::SatAdd => "saturating_add",
411 SaturatingOp::SatSub => "saturating_sub",
412 SaturatingOp::SatMul => "saturating_mul",
413 SaturatingOp::WrapAdd => "wrapping_add",
414 SaturatingOp::WrapSub => "wrapping_sub",
415 SaturatingOp::WrapMul => "wrapping_mul",
416 }
417 }
418
419 pub fn swapped(self) -> Self {
420 match self {
421 SaturatingOp::SatAdd => SaturatingOp::WrapAdd,
422 SaturatingOp::SatSub => SaturatingOp::WrapSub,
423 SaturatingOp::SatMul => SaturatingOp::WrapMul,
424 SaturatingOp::WrapAdd => SaturatingOp::SatAdd,
425 SaturatingOp::WrapSub => SaturatingOp::SatSub,
426 SaturatingOp::WrapMul => SaturatingOp::SatMul,
427 }
428 }
429
430 pub fn overflow_phrase(self) -> &'static str {
431 match self {
432 SaturatingOp::SatAdd | SaturatingOp::WrapAdd => "whose sum overflows",
433 SaturatingOp::SatSub | SaturatingOp::WrapSub => {
434 "whose difference underflows or overflows"
435 }
436 SaturatingOp::SatMul | SaturatingOp::WrapMul => "whose product overflows",
437 }
438 }
439}
440
441#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
442#[serde(rename_all = "snake_case")]
443pub enum TrimOp {
444 Trim,
445 TrimStart,
446 TrimEnd,
447}
448
449impl TrimOp {
450 pub fn code(self) -> u8 {
451 match self {
452 TrimOp::Trim => 0,
453 TrimOp::TrimStart => 1,
454 TrimOp::TrimEnd => 2,
455 }
456 }
457
458 pub fn name(self) -> &'static str {
459 match self {
460 TrimOp::Trim => "trim",
461 TrimOp::TrimStart => "trim_start",
462 TrimOp::TrimEnd => "trim_end",
463 }
464 }
465
466 pub fn alt1(self) -> Self {
467 match self {
468 TrimOp::Trim => TrimOp::TrimStart,
469 TrimOp::TrimStart | TrimOp::TrimEnd => TrimOp::Trim,
470 }
471 }
472
473 pub fn alt2(self) -> Option<Self> {
474 match self {
475 TrimOp::Trim => Some(TrimOp::TrimEnd),
476 TrimOp::TrimStart | TrimOp::TrimEnd => None,
477 }
478 }
479
480 pub fn whitespace_phrase(self, alt: TrimOp) -> &'static str {
481 match (self, alt) {
482 (TrimOp::Trim, TrimOp::TrimStart) | (TrimOp::TrimStart, TrimOp::Trim) => {
483 "trailing whitespace"
484 }
485 (TrimOp::Trim, TrimOp::TrimEnd) | (TrimOp::TrimEnd, TrimOp::Trim) => {
486 "leading whitespace"
487 }
488 _ => "leading or trailing whitespace",
489 }
490 }
491}
492
493#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
494#[serde(rename_all = "snake_case")]
495pub enum EndianOp {
496 ToBeBytes,
497 ToLeBytes,
498 ToBe,
499 ToLe,
500}
501
502impl EndianOp {
503 pub fn code(self) -> u8 {
504 match self {
505 EndianOp::ToBeBytes => 0,
506 EndianOp::ToLeBytes => 1,
507 EndianOp::ToBe => 2,
508 EndianOp::ToLe => 3,
509 }
510 }
511
512 pub fn name(self) -> &'static str {
513 match self {
514 EndianOp::ToBeBytes => "to_be_bytes",
515 EndianOp::ToLeBytes => "to_le_bytes",
516 EndianOp::ToBe => "to_be",
517 EndianOp::ToLe => "to_le",
518 }
519 }
520
521 pub fn swapped(self) -> Self {
522 match self {
523 EndianOp::ToBeBytes => EndianOp::ToLeBytes,
524 EndianOp::ToLeBytes => EndianOp::ToBeBytes,
525 EndianOp::ToBe => EndianOp::ToLe,
526 EndianOp::ToLe => EndianOp::ToBe,
527 }
528 }
529}
530
531#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
532#[serde(rename_all = "snake_case")]
533pub enum BitCountOp {
534 LeadingZeros,
535 TrailingZeros,
536 LeadingOnes,
537 TrailingOnes,
538}
539
540impl BitCountOp {
541 pub fn code(self) -> u8 {
542 match self {
543 BitCountOp::LeadingZeros => 0,
544 BitCountOp::TrailingZeros => 1,
545 BitCountOp::LeadingOnes => 2,
546 BitCountOp::TrailingOnes => 3,
547 }
548 }
549
550 pub fn name(self) -> &'static str {
551 match self {
552 BitCountOp::LeadingZeros => "leading_zeros",
553 BitCountOp::TrailingZeros => "trailing_zeros",
554 BitCountOp::LeadingOnes => "leading_ones",
555 BitCountOp::TrailingOnes => "trailing_ones",
556 }
557 }
558
559 pub fn swapped(self) -> Self {
560 match self {
561 BitCountOp::LeadingZeros => BitCountOp::TrailingZeros,
562 BitCountOp::TrailingZeros => BitCountOp::LeadingZeros,
563 BitCountOp::LeadingOnes => BitCountOp::TrailingOnes,
564 BitCountOp::TrailingOnes => BitCountOp::LeadingOnes,
565 }
566 }
567}
568
569#[derive(Clone, Copy, PartialEq, Eq, Debug)]
570pub enum RangeIndexMode {
571 Ref,
573 RefMut,
575 Deref,
577}
578
579#[derive(Clone, PartialEq, Eq, Debug)]
580pub struct RangeIndex {
581 pub mode: RangeIndexMode,
582 pub base: String,
584 pub head: (usize, usize),
586 pub close_bracket: (usize, usize),
588}
589
590#[derive(Clone, Copy, PartialEq, Eq, Debug)]
591pub enum ArmGuard {
592 None(usize),
594 Existing(usize, usize),
596}
597
598#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
599#[serde(rename_all = "snake_case")]
600pub enum ArithOp {
601 Add,
602 Sub,
603 Mul,
604 Div,
605 Rem,
606 BitAnd,
607 BitOr,
608 BitXor,
609 Shl,
610 Shr,
611}
612
613impl ArithOp {
614 pub fn binding(self) -> u8 {
616 match self {
617 ArithOp::Mul | ArithOp::Div | ArithOp::Rem => 5,
618 ArithOp::Add | ArithOp::Sub => 4,
619 ArithOp::Shl | ArithOp::Shr => 3,
620 ArithOp::BitAnd => 2,
621 ArithOp::BitXor => 1,
622 ArithOp::BitOr => 0,
623 }
624 }
625
626 pub const ALL: [ArithOp; 10] = [
627 ArithOp::Add,
628 ArithOp::Sub,
629 ArithOp::Mul,
630 ArithOp::Div,
631 ArithOp::Rem,
632 ArithOp::BitAnd,
633 ArithOp::BitOr,
634 ArithOp::BitXor,
635 ArithOp::Shl,
636 ArithOp::Shr,
637 ];
638
639 pub fn symbol(self) -> &'static str {
640 match self {
641 ArithOp::Add => "+",
642 ArithOp::Sub => "-",
643 ArithOp::Mul => "*",
644 ArithOp::Div => "/",
645 ArithOp::Rem => "%",
646 ArithOp::BitAnd => "&",
647 ArithOp::BitOr => "|",
648 ArithOp::BitXor => "^",
649 ArithOp::Shl => "<<",
650 ArithOp::Shr => ">>",
651 }
652 }
653
654 pub fn swapped(self) -> ArithOp {
656 match self {
657 ArithOp::Add => ArithOp::Sub,
658 ArithOp::Sub => ArithOp::Add,
659 ArithOp::Mul => ArithOp::Div,
660 ArithOp::Div => ArithOp::Mul,
661 ArithOp::Rem => ArithOp::Div,
662 ArithOp::BitAnd => ArithOp::BitOr,
663 ArithOp::BitOr => ArithOp::BitAnd,
664 ArithOp::BitXor => ArithOp::BitAnd,
665 ArithOp::Shl => ArithOp::Shr,
666 ArithOp::Shr => ArithOp::Shl,
667 }
668 }
669
670 pub fn swapped2(self) -> Option<ArithOp> {
672 Some(match self {
673 ArithOp::Add => ArithOp::Mul,
674 ArithOp::Sub => ArithOp::Div,
675 ArithOp::Mul => ArithOp::Add,
676 ArithOp::Div => ArithOp::Rem,
677 ArithOp::Rem => ArithOp::Add,
678 ArithOp::BitAnd | ArithOp::BitOr => ArithOp::BitXor,
679 ArithOp::BitXor => ArithOp::BitOr,
680 ArithOp::Shl | ArithOp::Shr => return None,
681 })
682 }
683
684 pub fn second(self, assign: bool) -> Option<ArithOp> {
688 if assign && matches!(self, ArithOp::BitAnd | ArithOp::BitOr) {
689 return None;
690 }
691 self.swapped2()
692 }
693
694 pub fn is_bitwise(self) -> bool {
696 matches!(
697 self,
698 ArithOp::BitAnd | ArithOp::BitOr | ArithOp::BitXor | ArithOp::Shl | ArithOp::Shr
699 )
700 }
701
702 fn stem(self) -> (&'static str, &'static str) {
704 match self {
705 ArithOp::Add => ("add", "Add"),
706 ArithOp::Sub => ("sub", "Sub"),
707 ArithOp::Mul => ("mul", "Mul"),
708 ArithOp::Div => ("div", "Div"),
709 ArithOp::Rem => ("rem", "Rem"),
710 ArithOp::BitAnd => ("bitand", "BitAnd"),
711 ArithOp::BitOr => ("bitor", "BitOr"),
712 ArithOp::BitXor => ("bitxor", "BitXor"),
713 ArithOp::Shl => ("shl", "Shl"),
714 ArithOp::Shr => ("shr", "Shr"),
715 }
716 }
717
718 pub(crate) fn method(self, assign: bool) -> String {
720 format!(
721 "tmt_{}{}",
722 self.stem().0,
723 if assign { "_assign" } else { "" }
724 )
725 }
726
727 pub(crate) fn fallback_trait(self, assign: bool) -> String {
729 format!("Any{}{}", self.stem().1, if assign { "Assign" } else { "" })
730 }
731
732 pub(super) fn prim_trait(self, assign: bool) -> &'static str {
734 match (self.is_bitwise(), assign) {
735 (false, false) => "ArithPrim",
736 (true, false) => "BitPrim",
737 (false, true) => "ArithAssignPrim",
738 (true, true) => "BitAssignPrim",
739 }
740 }
741}
742
743#[derive(Clone, PartialEq, Eq, Debug)]
744pub struct Site {
745 pub id: u32,
746 pub kind: SiteKind,
747 pub line: usize,
749 pub column: usize,
750 pub start: usize,
752 pub end: usize,
753 pub text: String,
754}
755
756#[derive(Debug, PartialEq, Eq)]
757pub struct TestModule {
758 pub name: String,
759 pub path: Option<String>,
761}
762
763#[derive(Debug, Default)]
764pub struct Discovery {
765 pub sites: Vec<Site>,
766 pub rebuild_sites: Vec<Site>,
771 pub single_statement_bodies: Vec<String>,
774 pub test_modules: Vec<TestModule>,
776}
777
778pub const NO_ID: u32 = u32::MAX;