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
299
300
301
302
303
304
305
306
307
308
309
//! RA Proc Macro Server
//!
//! This library is able to call compiled Rust custom derive dynamic libraries on arbitrary code.
//! The general idea here is based on <https://github.com/fedochet/rust-proc-macro-expander>.
//!
//! But we adapt it to better fit RA needs:
//!
//! * We use `tt` for proc-macro `TokenStream` server, it is easier to manipulate and interact with
//!   RA than `proc-macro2` token stream.
//! * By **copying** the whole rustc `lib_proc_macro` code, we are able to build this with `stable`
//!   rustc rather than `unstable`. (Although in general ABI compatibility is still an issue)…

#![cfg(any(feature = "sysroot-abi", rust_analyzer))]
#![cfg_attr(feature = "in-rust-tree", feature(rustc_private))]
#![feature(proc_macro_internals, proc_macro_diagnostic, proc_macro_span)]
#![warn(rust_2018_idioms, unused_lifetimes)]
#![allow(unreachable_pub, internal_features)]

extern crate proc_macro;
#[cfg(feature = "in-rust-tree")]
extern crate rustc_driver as _;

#[cfg(not(feature = "in-rust-tree"))]
extern crate ra_ap_rustc_lexer as rustc_lexer;
#[cfg(feature = "in-rust-tree")]
extern crate rustc_lexer;

mod dylib;
mod proc_macros;
mod server;

use std::{
    collections::{hash_map::Entry, HashMap},
    env,
    ffi::OsString,
    fs, thread,
    time::SystemTime,
};

use paths::{Utf8Path, Utf8PathBuf};
use proc_macro_api::{
    msg::{
        self, deserialize_span_data_index_map, serialize_span_data_index_map, ExpnGlobals,
        SpanMode, TokenId, CURRENT_API_VERSION,
    },
    ProcMacroKind,
};
use span::Span;

use crate::server::TokenStream;

// see `build.rs`
include!(concat!(env!("OUT_DIR"), "/rustc_version.rs"));

trait ProcMacroSrvSpan: Copy {
    type Server: proc_macro::bridge::server::Server<TokenStream = TokenStream<Self>>;
    fn make_server(call_site: Self, def_site: Self, mixed_site: Self) -> Self::Server;
}

impl ProcMacroSrvSpan for TokenId {
    type Server = server::token_id::TokenIdServer;

    fn make_server(call_site: Self, def_site: Self, mixed_site: Self) -> Self::Server {
        Self::Server { interner: &server::SYMBOL_INTERNER, call_site, def_site, mixed_site }
    }
}
impl ProcMacroSrvSpan for Span {
    type Server = server::rust_analyzer_span::RaSpanServer;
    fn make_server(call_site: Self, def_site: Self, mixed_site: Self) -> Self::Server {
        Self::Server {
            interner: &server::SYMBOL_INTERNER,
            call_site,
            def_site,
            mixed_site,
            tracked_env_vars: Default::default(),
            tracked_paths: Default::default(),
        }
    }
}

#[derive(Default)]
pub struct ProcMacroSrv {
    expanders: HashMap<(Utf8PathBuf, SystemTime), dylib::Expander>,
    span_mode: SpanMode,
}

const EXPANDER_STACK_SIZE: usize = 8 * 1024 * 1024;

impl ProcMacroSrv {
    pub fn set_span_mode(&mut self, span_mode: SpanMode) {
        self.span_mode = span_mode;
    }

    pub fn span_mode(&self) -> SpanMode {
        self.span_mode
    }

    pub fn expand(
        &mut self,
        task: msg::ExpandMacro,
    ) -> Result<(msg::FlatTree, Vec<u32>), msg::PanicMessage> {
        let span_mode = self.span_mode;
        let expander = self.expander(task.lib.as_ref()).map_err(|err| {
            debug_assert!(false, "should list macros before asking to expand");
            msg::PanicMessage(format!("failed to load macro: {err}"))
        })?;

        let prev_env = EnvSnapshot::new();
        for (k, v) in &task.env {
            env::set_var(k, v);
        }
        let prev_working_dir = match &task.current_dir {
            Some(dir) => {
                let prev_working_dir = std::env::current_dir().ok();
                if let Err(err) = std::env::set_current_dir(dir) {
                    eprintln!("Failed to set the current working dir to {dir}. Error: {err:?}")
                }
                prev_working_dir
            }
            None => None,
        };

        let ExpnGlobals { def_site, call_site, mixed_site, .. } = task.has_global_spans;

        let result = match span_mode {
            SpanMode::Id => {
                expand_id(task, expander, def_site, call_site, mixed_site).map(|it| (it, vec![]))
            }
            SpanMode::RustAnalyzer => {
                expand_ra_span(task, expander, def_site, call_site, mixed_site)
            }
        };

        prev_env.rollback();

        if let Some(dir) = prev_working_dir {
            if let Err(err) = std::env::set_current_dir(&dir) {
                eprintln!(
                    "Failed to set the current working dir to {}. Error: {:?}",
                    dir.display(),
                    err
                )
            }
        }

        result.map_err(msg::PanicMessage)
    }

    pub fn list_macros(
        &mut self,
        dylib_path: &Utf8Path,
    ) -> Result<Vec<(String, ProcMacroKind)>, String> {
        let expander = self.expander(dylib_path)?;
        Ok(expander.list_macros())
    }

    fn expander(&mut self, path: &Utf8Path) -> Result<&dylib::Expander, String> {
        let time = fs::metadata(path)
            .and_then(|it| it.modified())
            .map_err(|err| format!("Failed to get file metadata for {path}: {err}",))?;

        Ok(match self.expanders.entry((path.to_path_buf(), time)) {
            Entry::Vacant(v) => v.insert(
                dylib::Expander::new(path)
                    .map_err(|err| format!("Cannot create expander for {path}: {err}",))?,
            ),
            Entry::Occupied(e) => e.into_mut(),
        })
    }
}

fn expand_id(
    task: msg::ExpandMacro,
    expander: &dylib::Expander,
    def_site: usize,
    call_site: usize,
    mixed_site: usize,
) -> Result<msg::FlatTree, String> {
    let def_site = TokenId(def_site as u32);
    let call_site = TokenId(call_site as u32);
    let mixed_site = TokenId(mixed_site as u32);

    let macro_body = task.macro_body.to_subtree_unresolved(CURRENT_API_VERSION);
    let attributes = task.attributes.map(|it| it.to_subtree_unresolved(CURRENT_API_VERSION));
    let result = thread::scope(|s| {
        let thread = thread::Builder::new()
            .stack_size(EXPANDER_STACK_SIZE)
            .name(task.macro_name.clone())
            .spawn_scoped(s, || {
                expander
                    .expand(
                        &task.macro_name,
                        macro_body,
                        attributes,
                        def_site,
                        call_site,
                        mixed_site,
                    )
                    .map(|it| msg::FlatTree::new_raw(&it, CURRENT_API_VERSION))
            });
        let res = match thread {
            Ok(handle) => handle.join(),
            Err(e) => std::panic::resume_unwind(Box::new(e)),
        };

        match res {
            Ok(res) => res,
            Err(e) => std::panic::resume_unwind(e),
        }
    });
    result
}

fn expand_ra_span(
    task: msg::ExpandMacro,
    expander: &dylib::Expander,
    def_site: usize,
    call_site: usize,
    mixed_site: usize,
) -> Result<(msg::FlatTree, Vec<u32>), String> {
    let mut span_data_table = deserialize_span_data_index_map(&task.span_data_table);

    let def_site = span_data_table[def_site];
    let call_site = span_data_table[call_site];
    let mixed_site = span_data_table[mixed_site];

    let macro_body = task.macro_body.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table);
    let attributes =
        task.attributes.map(|it| it.to_subtree_resolved(CURRENT_API_VERSION, &span_data_table));
    let result = thread::scope(|s| {
        let thread = thread::Builder::new()
            .stack_size(EXPANDER_STACK_SIZE)
            .name(task.macro_name.clone())
            .spawn_scoped(s, || {
                expander
                    .expand(
                        &task.macro_name,
                        macro_body,
                        attributes,
                        def_site,
                        call_site,
                        mixed_site,
                    )
                    .map(|it| {
                        (
                            msg::FlatTree::new(&it, CURRENT_API_VERSION, &mut span_data_table),
                            serialize_span_data_index_map(&span_data_table),
                        )
                    })
            });
        let res = match thread {
            Ok(handle) => handle.join(),
            Err(e) => std::panic::resume_unwind(Box::new(e)),
        };

        match res {
            Ok(res) => res,
            Err(e) => std::panic::resume_unwind(e),
        }
    });
    result
}

pub struct PanicMessage {
    message: Option<String>,
}

impl PanicMessage {
    pub fn into_string(self) -> Option<String> {
        self.message
    }
}

struct EnvSnapshot {
    vars: HashMap<OsString, OsString>,
}

impl EnvSnapshot {
    fn new() -> EnvSnapshot {
        EnvSnapshot { vars: env::vars_os().collect() }
    }

    fn rollback(self) {}
}

impl Drop for EnvSnapshot {
    fn drop(&mut self) {
        for (name, value) in env::vars_os() {
            let old_value = self.vars.remove(&name);
            if old_value != Some(value) {
                match old_value {
                    None => env::remove_var(name),
                    Some(old_value) => env::set_var(name, old_value),
                }
            }
        }
        for (name, old_value) in self.vars.drain() {
            env::set_var(name, old_value)
        }
    }
}

#[cfg(test)]
mod tests;

#[cfg(test)]
pub fn proc_macro_test_dylib_path() -> paths::Utf8PathBuf {
    proc_macro_test::PROC_MACRO_TEST_LOCATION.into()
}