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 elements: Vec<(String, usize, usize)>,
252 },
253 Range {
255 inclusive: bool,
256 lhs: String,
257 rhs: String,
258 gap: (usize, usize),
259 },
260 Arm {
263 pattern: String,
264 guard: ArmGuard,
266 removal: (usize, usize),
267 },
268 Stmt,
270 Neg {
272 minus: (usize, usize),
274 operand: String,
275 literal: bool,
277 needs_space: bool,
279 },
280 Guard {
282 can_be_false: bool,
285 },
286 Field {
288 name: String,
289 shorthand: bool,
291 from_base: Option<String>,
293 removal: (usize, usize),
295 },
296 Body {
299 name: String,
300 value: String,
301 sole_expr: Option<(usize, usize)>,
305 },
306}
307
308#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
309#[serde(rename_all = "snake_case")]
310pub enum RoundingOp {
311 Floor,
312 Ceil,
313 Round,
314 Trunc,
315}
316
317impl RoundingOp {
318 pub fn code(self) -> u8 {
319 match self {
320 RoundingOp::Floor => 0,
321 RoundingOp::Ceil => 1,
322 RoundingOp::Round => 2,
323 RoundingOp::Trunc => 3,
324 }
325 }
326
327 pub fn name(self) -> &'static str {
328 match self {
329 RoundingOp::Floor => "floor",
330 RoundingOp::Ceil => "ceil",
331 RoundingOp::Round => "round",
332 RoundingOp::Trunc => "trunc",
333 }
334 }
335
336 pub fn alt1(self) -> RoundingOp {
337 match self {
338 RoundingOp::Floor => RoundingOp::Trunc,
339 RoundingOp::Ceil => RoundingOp::Floor,
340 RoundingOp::Round => RoundingOp::Floor,
341 RoundingOp::Trunc => RoundingOp::Floor,
342 }
343 }
344
345 pub fn alt2(self) -> RoundingOp {
346 match self {
347 RoundingOp::Floor => RoundingOp::Ceil,
348 RoundingOp::Ceil => RoundingOp::Trunc,
349 RoundingOp::Round => RoundingOp::Trunc,
350 RoundingOp::Trunc => RoundingOp::Round,
351 }
352 }
353
354 pub fn differs_for(self, other: RoundingOp) -> &'static str {
356 match (self, other) {
357 (RoundingOp::Floor, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Floor) => {
358 "negative and not a whole number (e.g. -0.5)"
359 }
360 (RoundingOp::Ceil, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Ceil) => {
361 "positive and not a whole number (e.g. 0.5)"
362 }
363 (RoundingOp::Floor, RoundingOp::Round) | (RoundingOp::Round, RoundingOp::Floor) => {
364 "a number that rounds up (e.g. 1.5 or -1.4)"
365 }
366 (RoundingOp::Ceil, RoundingOp::Round) | (RoundingOp::Round, RoundingOp::Ceil) => {
367 "a number that rounds down (e.g. 1.4 or -1.6)"
368 }
369 (RoundingOp::Round, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Round) => {
370 "a number that rounds away from zero (e.g. 1.5 or -1.6)"
371 }
372 _ => "not a whole number (e.g. 0.5)",
373 }
374 }
375}
376
377#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
378#[serde(rename_all = "snake_case")]
379pub enum SaturatingOp {
380 SatAdd,
381 SatSub,
382 SatMul,
383 WrapAdd,
384 WrapSub,
385 WrapMul,
386}
387
388impl SaturatingOp {
389 pub fn code(self) -> u8 {
390 match self {
391 SaturatingOp::SatAdd => 0,
392 SaturatingOp::SatSub => 1,
393 SaturatingOp::SatMul => 2,
394 SaturatingOp::WrapAdd => 3,
395 SaturatingOp::WrapSub => 4,
396 SaturatingOp::WrapMul => 5,
397 }
398 }
399
400 pub fn name(self) -> &'static str {
401 match self {
402 SaturatingOp::SatAdd => "saturating_add",
403 SaturatingOp::SatSub => "saturating_sub",
404 SaturatingOp::SatMul => "saturating_mul",
405 SaturatingOp::WrapAdd => "wrapping_add",
406 SaturatingOp::WrapSub => "wrapping_sub",
407 SaturatingOp::WrapMul => "wrapping_mul",
408 }
409 }
410
411 pub fn swapped(self) -> Self {
412 match self {
413 SaturatingOp::SatAdd => SaturatingOp::WrapAdd,
414 SaturatingOp::SatSub => SaturatingOp::WrapSub,
415 SaturatingOp::SatMul => SaturatingOp::WrapMul,
416 SaturatingOp::WrapAdd => SaturatingOp::SatAdd,
417 SaturatingOp::WrapSub => SaturatingOp::SatSub,
418 SaturatingOp::WrapMul => SaturatingOp::SatMul,
419 }
420 }
421
422 pub fn overflow_phrase(self) -> &'static str {
423 match self {
424 SaturatingOp::SatAdd | SaturatingOp::WrapAdd => "whose sum overflows",
425 SaturatingOp::SatSub | SaturatingOp::WrapSub => {
426 "whose difference underflows or overflows"
427 }
428 SaturatingOp::SatMul | SaturatingOp::WrapMul => "whose product overflows",
429 }
430 }
431}
432
433#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
434#[serde(rename_all = "snake_case")]
435pub enum TrimOp {
436 Trim,
437 TrimStart,
438 TrimEnd,
439}
440
441impl TrimOp {
442 pub fn code(self) -> u8 {
443 match self {
444 TrimOp::Trim => 0,
445 TrimOp::TrimStart => 1,
446 TrimOp::TrimEnd => 2,
447 }
448 }
449
450 pub fn name(self) -> &'static str {
451 match self {
452 TrimOp::Trim => "trim",
453 TrimOp::TrimStart => "trim_start",
454 TrimOp::TrimEnd => "trim_end",
455 }
456 }
457
458 pub fn alt1(self) -> Self {
459 match self {
460 TrimOp::Trim => TrimOp::TrimStart,
461 TrimOp::TrimStart | TrimOp::TrimEnd => TrimOp::Trim,
462 }
463 }
464
465 pub fn alt2(self) -> Option<Self> {
466 match self {
467 TrimOp::Trim => Some(TrimOp::TrimEnd),
468 TrimOp::TrimStart | TrimOp::TrimEnd => None,
469 }
470 }
471
472 pub fn whitespace_phrase(self, alt: TrimOp) -> &'static str {
473 match (self, alt) {
474 (TrimOp::Trim, TrimOp::TrimStart) | (TrimOp::TrimStart, TrimOp::Trim) => {
475 "trailing whitespace"
476 }
477 (TrimOp::Trim, TrimOp::TrimEnd) | (TrimOp::TrimEnd, TrimOp::Trim) => {
478 "leading whitespace"
479 }
480 _ => "leading or trailing whitespace",
481 }
482 }
483}
484
485#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
486#[serde(rename_all = "snake_case")]
487pub enum EndianOp {
488 ToBeBytes,
489 ToLeBytes,
490 ToBe,
491 ToLe,
492}
493
494impl EndianOp {
495 pub fn code(self) -> u8 {
496 match self {
497 EndianOp::ToBeBytes => 0,
498 EndianOp::ToLeBytes => 1,
499 EndianOp::ToBe => 2,
500 EndianOp::ToLe => 3,
501 }
502 }
503
504 pub fn name(self) -> &'static str {
505 match self {
506 EndianOp::ToBeBytes => "to_be_bytes",
507 EndianOp::ToLeBytes => "to_le_bytes",
508 EndianOp::ToBe => "to_be",
509 EndianOp::ToLe => "to_le",
510 }
511 }
512
513 pub fn swapped(self) -> Self {
514 match self {
515 EndianOp::ToBeBytes => EndianOp::ToLeBytes,
516 EndianOp::ToLeBytes => EndianOp::ToBeBytes,
517 EndianOp::ToBe => EndianOp::ToLe,
518 EndianOp::ToLe => EndianOp::ToBe,
519 }
520 }
521}
522
523#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
524#[serde(rename_all = "snake_case")]
525pub enum BitCountOp {
526 LeadingZeros,
527 TrailingZeros,
528 LeadingOnes,
529 TrailingOnes,
530}
531
532impl BitCountOp {
533 pub fn code(self) -> u8 {
534 match self {
535 BitCountOp::LeadingZeros => 0,
536 BitCountOp::TrailingZeros => 1,
537 BitCountOp::LeadingOnes => 2,
538 BitCountOp::TrailingOnes => 3,
539 }
540 }
541
542 pub fn name(self) -> &'static str {
543 match self {
544 BitCountOp::LeadingZeros => "leading_zeros",
545 BitCountOp::TrailingZeros => "trailing_zeros",
546 BitCountOp::LeadingOnes => "leading_ones",
547 BitCountOp::TrailingOnes => "trailing_ones",
548 }
549 }
550
551 pub fn swapped(self) -> Self {
552 match self {
553 BitCountOp::LeadingZeros => BitCountOp::TrailingZeros,
554 BitCountOp::TrailingZeros => BitCountOp::LeadingZeros,
555 BitCountOp::LeadingOnes => BitCountOp::TrailingOnes,
556 BitCountOp::TrailingOnes => BitCountOp::LeadingOnes,
557 }
558 }
559}
560
561#[derive(Clone, Copy, PartialEq, Eq, Debug)]
562pub enum ArmGuard {
563 None(usize),
565 Existing(usize, usize),
567}
568
569#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
570#[serde(rename_all = "snake_case")]
571pub enum ArithOp {
572 Add,
573 Sub,
574 Mul,
575 Div,
576 Rem,
577 BitAnd,
578 BitOr,
579 BitXor,
580 Shl,
581 Shr,
582}
583
584impl ArithOp {
585 pub fn binding(self) -> u8 {
587 match self {
588 ArithOp::Mul | ArithOp::Div | ArithOp::Rem => 5,
589 ArithOp::Add | ArithOp::Sub => 4,
590 ArithOp::Shl | ArithOp::Shr => 3,
591 ArithOp::BitAnd => 2,
592 ArithOp::BitXor => 1,
593 ArithOp::BitOr => 0,
594 }
595 }
596
597 pub const ALL: [ArithOp; 10] = [
598 ArithOp::Add,
599 ArithOp::Sub,
600 ArithOp::Mul,
601 ArithOp::Div,
602 ArithOp::Rem,
603 ArithOp::BitAnd,
604 ArithOp::BitOr,
605 ArithOp::BitXor,
606 ArithOp::Shl,
607 ArithOp::Shr,
608 ];
609
610 pub fn symbol(self) -> &'static str {
611 match self {
612 ArithOp::Add => "+",
613 ArithOp::Sub => "-",
614 ArithOp::Mul => "*",
615 ArithOp::Div => "/",
616 ArithOp::Rem => "%",
617 ArithOp::BitAnd => "&",
618 ArithOp::BitOr => "|",
619 ArithOp::BitXor => "^",
620 ArithOp::Shl => "<<",
621 ArithOp::Shr => ">>",
622 }
623 }
624
625 pub fn swapped(self) -> ArithOp {
627 match self {
628 ArithOp::Add => ArithOp::Sub,
629 ArithOp::Sub => ArithOp::Add,
630 ArithOp::Mul => ArithOp::Div,
631 ArithOp::Div => ArithOp::Mul,
632 ArithOp::Rem => ArithOp::Div,
633 ArithOp::BitAnd => ArithOp::BitOr,
634 ArithOp::BitOr => ArithOp::BitAnd,
635 ArithOp::BitXor => ArithOp::BitAnd,
636 ArithOp::Shl => ArithOp::Shr,
637 ArithOp::Shr => ArithOp::Shl,
638 }
639 }
640
641 pub fn swapped2(self) -> Option<ArithOp> {
643 Some(match self {
644 ArithOp::Add => ArithOp::Mul,
645 ArithOp::Sub => ArithOp::Div,
646 ArithOp::Mul => ArithOp::Add,
647 ArithOp::Div => ArithOp::Rem,
648 ArithOp::Rem => ArithOp::Add,
649 ArithOp::BitAnd | ArithOp::BitOr => ArithOp::BitXor,
650 ArithOp::BitXor => ArithOp::BitOr,
651 ArithOp::Shl | ArithOp::Shr => return None,
652 })
653 }
654
655 pub fn second(self, assign: bool) -> Option<ArithOp> {
659 if assign && matches!(self, ArithOp::BitAnd | ArithOp::BitOr) {
660 return None;
661 }
662 self.swapped2()
663 }
664
665 pub fn is_bitwise(self) -> bool {
667 matches!(
668 self,
669 ArithOp::BitAnd | ArithOp::BitOr | ArithOp::BitXor | ArithOp::Shl | ArithOp::Shr
670 )
671 }
672
673 fn stem(self) -> (&'static str, &'static str) {
675 match self {
676 ArithOp::Add => ("add", "Add"),
677 ArithOp::Sub => ("sub", "Sub"),
678 ArithOp::Mul => ("mul", "Mul"),
679 ArithOp::Div => ("div", "Div"),
680 ArithOp::Rem => ("rem", "Rem"),
681 ArithOp::BitAnd => ("bitand", "BitAnd"),
682 ArithOp::BitOr => ("bitor", "BitOr"),
683 ArithOp::BitXor => ("bitxor", "BitXor"),
684 ArithOp::Shl => ("shl", "Shl"),
685 ArithOp::Shr => ("shr", "Shr"),
686 }
687 }
688
689 pub(crate) fn method(self, assign: bool) -> String {
691 format!(
692 "tmt_{}{}",
693 self.stem().0,
694 if assign { "_assign" } else { "" }
695 )
696 }
697
698 pub(crate) fn fallback_trait(self, assign: bool) -> String {
700 format!("Any{}{}", self.stem().1, if assign { "Assign" } else { "" })
701 }
702
703 pub(super) fn prim_trait(self, assign: bool) -> &'static str {
705 match (self.is_bitwise(), assign) {
706 (false, false) => "ArithPrim",
707 (true, false) => "BitPrim",
708 (false, true) => "ArithAssignPrim",
709 (true, true) => "BitAssignPrim",
710 }
711 }
712}
713
714#[derive(Clone, PartialEq, Eq, Debug)]
715pub struct Site {
716 pub id: u32,
717 pub kind: SiteKind,
718 pub line: usize,
720 pub column: usize,
721 pub start: usize,
723 pub end: usize,
724 pub text: String,
725}
726
727#[derive(Debug, PartialEq, Eq)]
728pub struct TestModule {
729 pub name: String,
730 pub path: Option<String>,
732}
733
734#[derive(Debug, Default)]
735pub struct Discovery {
736 pub sites: Vec<Site>,
737 pub rebuild_sites: Vec<Site>,
742 pub single_statement_bodies: Vec<String>,
745 pub test_modules: Vec<TestModule>,
747}
748
749pub const NO_ID: u32 = u32::MAX;