proc_macro_api/
msg.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//! Defines messages for cross-process message passing based on `ndjson` wire protocol
pub(crate) mod flat;

use std::io::{self, BufRead, Write};

use paths::Utf8PathBuf;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::ProcMacroKind;

pub use crate::msg::flat::{
    deserialize_span_data_index_map, serialize_span_data_index_map, FlatTree, SpanDataIndexMap,
    TokenId,
};

// The versions of the server protocol
pub const NO_VERSION_CHECK_VERSION: u32 = 0;
pub const VERSION_CHECK_VERSION: u32 = 1;
pub const ENCODE_CLOSE_SPAN_VERSION: u32 = 2;
pub const HAS_GLOBAL_SPANS: u32 = 3;
pub const RUST_ANALYZER_SPAN_SUPPORT: u32 = 4;
/// Whether literals encode their kind as an additional u32 field and idents their rawness as a u32 field
pub const EXTENDED_LEAF_DATA: u32 = 5;

pub const CURRENT_API_VERSION: u32 = EXTENDED_LEAF_DATA;

#[derive(Debug, Serialize, Deserialize)]
pub enum Request {
    /// Since [`NO_VERSION_CHECK_VERSION`]
    ListMacros { dylib_path: Utf8PathBuf },
    /// Since [`NO_VERSION_CHECK_VERSION`]
    ExpandMacro(Box<ExpandMacro>),
    /// Since [`VERSION_CHECK_VERSION`]
    ApiVersionCheck {},
    /// Since [`RUST_ANALYZER_SPAN_SUPPORT`]
    SetConfig(ServerConfig),
}

#[derive(Copy, Clone, Default, Debug, Serialize, Deserialize)]
pub enum SpanMode {
    #[default]
    Id,
    RustAnalyzer,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum Response {
    /// Since [`NO_VERSION_CHECK_VERSION`]
    ListMacros(Result<Vec<(String, ProcMacroKind)>, String>),
    /// Since [`NO_VERSION_CHECK_VERSION`]
    ExpandMacro(Result<FlatTree, PanicMessage>),
    /// Since [`NO_VERSION_CHECK_VERSION`]
    ApiVersionCheck(u32),
    /// Since [`RUST_ANALYZER_SPAN_SUPPORT`]
    SetConfig(ServerConfig),
    /// Since [`RUST_ANALYZER_SPAN_SUPPORT`]
    ExpandMacroExtended(Result<ExpandMacroExtended, PanicMessage>),
}

#[derive(Debug, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct ServerConfig {
    pub span_mode: SpanMode,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ExpandMacroExtended {
    pub tree: FlatTree,
    pub span_data_table: Vec<u32>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PanicMessage(pub String);

#[derive(Debug, Serialize, Deserialize)]
pub struct ExpandMacro {
    pub lib: Utf8PathBuf,
    /// Environment variables to set during macro expansion.
    pub env: Vec<(String, String)>,
    pub current_dir: Option<String>,
    #[serde(flatten)]
    pub data: ExpandMacroData,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ExpandMacroData {
    /// Argument of macro call.
    ///
    /// In custom derive this will be a struct or enum; in attribute-like macro - underlying
    /// item; in function-like macro - the macro body.
    pub macro_body: FlatTree,

    /// Name of macro to expand.
    ///
    /// In custom derive this is the name of the derived trait (`Serialize`, `Getters`, etc.).
    /// In attribute-like and function-like macros - single name of macro itself (`show_streams`).
    pub macro_name: String,

    /// Possible attributes for the attribute-like macros.
    pub attributes: Option<FlatTree>,
    /// marker for serde skip stuff
    #[serde(skip_serializing_if = "ExpnGlobals::skip_serializing_if")]
    #[serde(default)]
    pub has_global_spans: ExpnGlobals,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    #[serde(default)]
    pub span_data_table: Vec<u32>,
}

#[derive(Copy, Clone, Default, Debug, Serialize, Deserialize)]
pub struct ExpnGlobals {
    #[serde(skip_serializing)]
    #[serde(default)]
    pub serialize: bool,
    pub def_site: usize,
    pub call_site: usize,
    pub mixed_site: usize,
}

impl ExpnGlobals {
    fn skip_serializing_if(&self) -> bool {
        !self.serialize
    }
}

pub trait Message: Serialize + DeserializeOwned {
    fn read<R: BufRead>(
        from_proto: ProtocolRead<R>,
        inp: &mut R,
        buf: &mut String,
    ) -> io::Result<Option<Self>> {
        Ok(match from_proto(inp, buf)? {
            None => None,
            Some(text) => {
                let mut deserializer = serde_json::Deserializer::from_str(text);
                // Note that some proc-macro generate very deep syntax tree
                // We have to disable the current limit of serde here
                deserializer.disable_recursion_limit();
                Some(Self::deserialize(&mut deserializer)?)
            }
        })
    }
    fn write<W: Write>(self, to_proto: ProtocolWrite<W>, out: &mut W) -> io::Result<()> {
        let text = serde_json::to_string(&self)?;
        to_proto(out, &text)
    }
}

impl Message for Request {}
impl Message for Response {}

#[allow(type_alias_bounds)]
type ProtocolRead<R: BufRead> =
    for<'i, 'buf> fn(inp: &'i mut R, buf: &'buf mut String) -> io::Result<Option<&'buf String>>;
#[allow(type_alias_bounds)]
type ProtocolWrite<W: Write> = for<'o, 'msg> fn(out: &'o mut W, msg: &'msg str) -> io::Result<()>;

#[cfg(test)]
mod tests {
    use intern::{sym, Symbol};
    use span::{ErasedFileAstId, Span, SpanAnchor, SyntaxContextId, TextRange, TextSize};
    use tt::{Delimiter, DelimiterKind, Ident, Leaf, Literal, Punct, Spacing, Subtree, TokenTree};

    use super::*;

    fn fixture_token_tree() -> Subtree<Span> {
        let anchor = SpanAnchor {
            file_id: span::EditionedFileId::new(
                span::FileId::from_raw(0xe4e4e),
                span::Edition::CURRENT,
            ),
            ast_id: ErasedFileAstId::from_raw(0),
        };

        let token_trees = Box::new([
            TokenTree::Leaf(
                Ident {
                    sym: Symbol::intern("struct"),
                    span: Span {
                        range: TextRange::at(TextSize::new(0), TextSize::of("struct")),
                        anchor,
                        ctx: SyntaxContextId::ROOT,
                    },
                    is_raw: tt::IdentIsRaw::No,
                }
                .into(),
            ),
            TokenTree::Leaf(
                Ident {
                    sym: Symbol::intern("Foo"),
                    span: Span {
                        range: TextRange::at(TextSize::new(5), TextSize::of("r#Foo")),
                        anchor,
                        ctx: SyntaxContextId::ROOT,
                    },
                    is_raw: tt::IdentIsRaw::Yes,
                }
                .into(),
            ),
            TokenTree::Leaf(Leaf::Literal(Literal {
                symbol: Symbol::intern("Foo"),
                span: Span {
                    range: TextRange::at(TextSize::new(10), TextSize::of("\"Foo\"")),
                    anchor,
                    ctx: SyntaxContextId::ROOT,
                },
                kind: tt::LitKind::Str,
                suffix: None,
            })),
            TokenTree::Leaf(Leaf::Punct(Punct {
                char: '@',
                span: Span {
                    range: TextRange::at(TextSize::new(13), TextSize::of('@')),
                    anchor,
                    ctx: SyntaxContextId::ROOT,
                },
                spacing: Spacing::Joint,
            })),
            TokenTree::Subtree(Subtree {
                delimiter: Delimiter {
                    open: Span {
                        range: TextRange::at(TextSize::new(14), TextSize::of('{')),
                        anchor,
                        ctx: SyntaxContextId::ROOT,
                    },
                    close: Span {
                        range: TextRange::at(TextSize::new(19), TextSize::of('}')),
                        anchor,
                        ctx: SyntaxContextId::ROOT,
                    },
                    kind: DelimiterKind::Brace,
                },
                token_trees: Box::new([TokenTree::Leaf(Leaf::Literal(Literal {
                    symbol: sym::INTEGER_0.clone(),
                    span: Span {
                        range: TextRange::at(TextSize::new(15), TextSize::of("0u32")),
                        anchor,
                        ctx: SyntaxContextId::ROOT,
                    },
                    kind: tt::LitKind::Integer,
                    suffix: Some(sym::u32.clone()),
                }))]),
            }),
        ]);

        Subtree {
            delimiter: Delimiter {
                open: Span {
                    range: TextRange::empty(TextSize::new(0)),
                    anchor,
                    ctx: SyntaxContextId::ROOT,
                },
                close: Span {
                    range: TextRange::empty(TextSize::new(19)),
                    anchor,
                    ctx: SyntaxContextId::ROOT,
                },
                kind: DelimiterKind::Invisible,
            },
            token_trees,
        }
    }

    #[test]
    fn test_proc_macro_rpc_works() {
        let tt = fixture_token_tree();
        for v in RUST_ANALYZER_SPAN_SUPPORT..=CURRENT_API_VERSION {
            let mut span_data_table = Default::default();
            let task = ExpandMacro {
                data: ExpandMacroData {
                    macro_body: FlatTree::new(&tt, v, &mut span_data_table),
                    macro_name: Default::default(),
                    attributes: None,
                    has_global_spans: ExpnGlobals {
                        serialize: true,
                        def_site: 0,
                        call_site: 0,
                        mixed_site: 0,
                    },
                    span_data_table: Vec::new(),
                },
                lib: Utf8PathBuf::from_path_buf(std::env::current_dir().unwrap()).unwrap(),
                env: Default::default(),
                current_dir: Default::default(),
            };

            let json = serde_json::to_string(&task).unwrap();
            // println!("{}", json);
            let back: ExpandMacro = serde_json::from_str(&json).unwrap();

            assert_eq!(
                tt,
                back.data.macro_body.to_subtree_resolved(v, &span_data_table),
                "version: {v}"
            );
        }
    }
}