rapx/preprocess/
ssa_preprocess.rs

1use rustc_ast::{token::CommentKind, *};
2use rustc_span::{
3    symbol::{Ident, Symbol},
4    DUMMY_SP,
5};
6use thin_vec::ThinVec;
7
8use crate::rap_debug;
9pub(crate) fn create_ssa_struct(_krate: &mut Crate) {
10    rap_debug!("[CALLBACK] Injecting new structs into the AST...");
11
12    let ssa_struct = create_struct(
13        "SSAstmt",
14        vec![
15            ("para1", Symbol::intern("i128")),
16            ("para2", Symbol::intern("i128")),
17            ("para3", Symbol::intern("i128")),
18            ("para4", Symbol::intern("i128")),
19            ("para5", Symbol::intern("i128")),
20            ("para6", Symbol::intern("i128")),
21            ("para7", Symbol::intern("i128")),
22            ("para8", Symbol::intern("i128")),
23            ("para9", Symbol::intern("i128")),
24            ("para10", Symbol::intern("i128")),
25        ],
26    );
27
28    let essa_struct = create_struct(
29        "ESSAstmt",
30        vec![
31            ("op1", Symbol::intern("i128")),
32            ("op2", Symbol::intern("i128")),
33            ("cmp", Symbol::intern("i128")),
34        ],
35    );
36
37    _krate.items.push(ssa_struct);
38    _krate.items.push(essa_struct);
39
40    // println!("[CALLBACK] Injection complete. Continuing compilation...");
41}
42pub(crate) fn create_struct(name: &str, fields_def: Vec<(&str, Symbol)>) -> Box<Item> {
43    let fields: ThinVec<FieldDef> = fields_def
44        .into_iter()
45        .map(|(fname, fty)| FieldDef {
46            attrs: ThinVec::from([doc_attr()]),
47            vis: Visibility {
48                span: DUMMY_SP,
49                kind: VisibilityKind::Public,
50                tokens: None,
51            },
52            ident: Some(Ident::from_str(fname)),
53            ty: Box::new(Ty {
54                id: NodeId::from_u32(0),
55                kind: TyKind::Path(None, Path::from_ident(Ident::with_dummy_span(fty))),
56                span: DUMMY_SP,
57                tokens: None,
58            }),
59            id: NodeId::from_u32(0),
60            span: DUMMY_SP,
61            is_placeholder: false,
62            safety: Safety::Default,
63            default: None,
64        })
65        .collect();
66
67    let ident = Ident {
68        name: Symbol::intern(name),
69        span: DUMMY_SP,
70    };
71    let variant_data = VariantData::Struct {
72        fields,
73        recovered: Recovered::No,
74    };
75
76    let item_kind = ItemKind::Struct(ident, Generics::default(), variant_data);
77
78    Box::new(Item {
79        attrs: ThinVec::from([doc_attr()]),
80        id: NodeId::from_u32(0),
81        kind: item_kind,
82        vis: Visibility {
83            span: DUMMY_SP,
84            kind: VisibilityKind::Public,
85            tokens: None,
86        },
87        span: DUMMY_SP,
88        tokens: None,
89    })
90}
91
92/// Empty `#[doc]` on the struct.
93/// cc https://github.com/Artisan-Lab/RAPx/issues/184
94fn doc_attr() -> Attribute {
95    Attribute {
96        kind: AttrKind::DocComment(CommentKind::Line, Symbol::intern("doc")),
97        id: AttrId::ZERO,
98        style: AttrStyle::Outer,
99        span: DUMMY_SP,
100    }
101}