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
extern crate proc_macro;

mod buffer;
mod bytecode;

use crate::buffer::{InputBuffer, OutputBuffer};
use crate::bytecode::Bytecode;
use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
use std::io::{ErrorKind, Read, Write};
use std::iter::FromIterator;
use std::path::Path;
use std::process::{Command, ExitStatus, Stdio};
use std::str::FromStr;

#[proc_macro_derive(Serialize, attributes(serde))]
pub fn derive_serialize(input: TokenStream) -> TokenStream {
    derive(0, input)
}

#[proc_macro_derive(Deserialize, attributes(serde))]
pub fn derive_deserialize(input: TokenStream) -> TokenStream {
    derive(1 + cfg!(feature = "deserialize_in_place") as u8, input)
}

fn derive(select: u8, input: TokenStream) -> TokenStream {
    let mut memory = TokenMemory::default();
    let mut buf = OutputBuffer::new();
    buf.write_u8(select);

    memory.spans.push(Span::call_site());
    for token in input {
        memory.linearize_token(token, &mut buf);
    }

    let exe_path = Path::new(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/serde_derive-x86_64-unknown-linux-gnu",
    ));
    let mut child = match Command::new(exe_path)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
    {
        Ok(child) => child,
        Err(io_error) => {
            if io_error.kind() == ErrorKind::NotFound {
                panic!(
                    "file missing from serde_derive manifest directory during macro expansion: {}",
                    exe_path.display(),
                );
            } else {
                panic!("failed to spawn process: {}", io_error);
            }
        }
    };

    let mut stdin = child.stdin.take().unwrap();
    let mut buf = buf.into_bytes();
    stdin.write_all(&buf).unwrap();
    drop(stdin);

    let mut stdout = child.stdout.take().unwrap();
    buf.clear();
    stdout.read_to_end(&mut buf).unwrap();

    let success = child.wait().as_ref().map_or(true, ExitStatus::success);
    if !success || buf.is_empty() {
        panic!();
    }

    let mut buf = InputBuffer::new(&buf);
    memory.receive(&mut buf)
}

#[derive(Default)]
struct TokenMemory {
    spans: Vec<Span>,
    groups: Vec<Group>,
    idents: Vec<Ident>,
    puncts: Vec<Punct>,
    literals: Vec<Literal>,
}

enum Kind {
    Group(Delimiter),
    Ident,
    Punct(Spacing),
    Literal,
}

impl TokenMemory {
    // Depth-first post-order traversal.
    fn linearize_token(&mut self, token: TokenTree, buf: &mut OutputBuffer) {
        match token {
            TokenTree::Group(group) => {
                let mut len = 0usize;
                for token in group.stream() {
                    self.linearize_token(token, buf);
                    len += 1;
                }
                assert!(len <= u32::MAX as usize);
                buf.write_u8(match group.delimiter() {
                    Delimiter::Parenthesis => Bytecode::GROUP_PARENTHESIS,
                    Delimiter::Brace => Bytecode::GROUP_BRACE,
                    Delimiter::Bracket => Bytecode::GROUP_BRACKET,
                    Delimiter::None => Bytecode::GROUP_NONE,
                });
                buf.write_u32(len as u32);
                self.spans
                    .extend([group.span(), group.span_open(), group.span_close()]);
                self.groups.push(group);
            }
            TokenTree::Ident(ident) => {
                buf.write_u8(Bytecode::IDENT);
                let repr = ident.to_string();
                assert!(repr.len() <= u16::MAX as usize);
                buf.write_u16(repr.len() as u16);
                buf.write_str(&repr);
                self.spans.push(ident.span());
                self.idents.push(ident);
            }
            TokenTree::Punct(punct) => {
                buf.write_u8(match punct.spacing() {
                    Spacing::Alone => Bytecode::PUNCT_ALONE,
                    Spacing::Joint => Bytecode::PUNCT_JOINT,
                });
                let ch = punct.as_char();
                assert!(ch.is_ascii());
                buf.write_u8(ch as u8);
                self.spans.push(punct.span());
                self.puncts.push(punct);
            }
            TokenTree::Literal(literal) => {
                buf.write_u8(Bytecode::LITERAL);
                let repr = literal.to_string();
                assert!(repr.len() <= u16::MAX as usize);
                buf.write_u16(repr.len() as u16);
                buf.write_str(&repr);
                self.spans.push(literal.span());
                self.literals.push(literal);
            }
        }
    }

    fn receive(&self, buf: &mut InputBuffer) -> TokenStream {
        let mut trees = Vec::new();
        while !buf.is_empty() {
            match match buf.read_u8() {
                Bytecode::GROUP_PARENTHESIS => Kind::Group(Delimiter::Parenthesis),
                Bytecode::GROUP_BRACE => Kind::Group(Delimiter::Brace),
                Bytecode::GROUP_BRACKET => Kind::Group(Delimiter::Bracket),
                Bytecode::GROUP_NONE => Kind::Group(Delimiter::None),
                Bytecode::IDENT => Kind::Ident,
                Bytecode::PUNCT_ALONE => Kind::Punct(Spacing::Alone),
                Bytecode::PUNCT_JOINT => Kind::Punct(Spacing::Joint),
                Bytecode::LITERAL => Kind::Literal,
                Bytecode::LOAD_GROUP => {
                    let identity = buf.read_u32();
                    let group = self.groups[identity as usize].clone();
                    trees.push(TokenTree::Group(group));
                    continue;
                }
                Bytecode::LOAD_IDENT => {
                    let identity = buf.read_u32();
                    let ident = self.idents[identity as usize].clone();
                    trees.push(TokenTree::Ident(ident));
                    continue;
                }
                Bytecode::LOAD_PUNCT => {
                    let identity = buf.read_u32();
                    let punct = self.puncts[identity as usize].clone();
                    trees.push(TokenTree::Punct(punct));
                    continue;
                }
                Bytecode::LOAD_LITERAL => {
                    let identity = buf.read_u32();
                    let literal = self.literals[identity as usize].clone();
                    trees.push(TokenTree::Literal(literal));
                    continue;
                }
                Bytecode::SET_SPAN => {
                    trees.last_mut().unwrap().set_span(self.read_span(buf));
                    continue;
                }
                _ => unreachable!(),
            } {
                Kind::Group(delimiter) => {
                    let len = buf.read_u32();
                    let stream = trees.drain(trees.len() - len as usize..).collect();
                    let group = Group::new(delimiter, stream);
                    trees.push(TokenTree::Group(group));
                }
                Kind::Ident => {
                    let len = buf.read_u16();
                    let repr = buf.read_str(len as usize);
                    let span = self.read_span(buf);
                    let ident = if let Some(repr) = repr.strip_prefix("r#") {
                        Ident::new_raw(repr, span)
                    } else {
                        Ident::new(repr, span)
                    };
                    trees.push(TokenTree::Ident(ident));
                }
                Kind::Punct(spacing) => {
                    let ch = buf.read_u8();
                    assert!(ch.is_ascii());
                    let punct = Punct::new(ch as char, spacing);
                    trees.push(TokenTree::Punct(punct));
                }
                Kind::Literal => {
                    let len = buf.read_u16();
                    let repr = buf.read_str(len as usize);
                    let literal = Literal::from_str(repr).unwrap();
                    trees.push(TokenTree::Literal(literal));
                }
            }
        }

        TokenStream::from_iter(trees)
    }

    fn read_span(&self, buf: &mut InputBuffer) -> Span {
        let lo = buf.read_u32();
        let hi = buf.read_u32();
        let span = self.spans[lo as usize];
        if lo == hi {
            span
        } else {
            #[cfg(any())] // FIXME
            return span.join(self.spans[hi as usize]).unwrap_or(span);
            span
        }
    }
}