Skip to main content

proc_macro_api/
lib.rs

1//! Client-side Proc-Macro crate
2//!
3//! We separate proc-macro expanding logic to an extern program to allow
4//! different implementations (e.g. wasm or dylib loading). And this crate
5//! is used to provide basic infrastructure for communication between two
6//! processes: Client (RA itself), Server (the external program)
7
8#![cfg_attr(feature = "in-rust-tree", feature(rustc_private))]
9#![allow(internal_features, unused_features)]
10
11#[cfg(feature = "in-rust-tree")]
12extern crate rustc_driver as _;
13
14stdx::rustc_crates! {
15    extern crate rustc_lexer or ra_ap_rustc_lexer;
16}
17
18pub mod bidirectional_protocol;
19#[cfg(feature = "in-ra")]
20pub mod client;
21pub mod flat;
22pub mod legacy_protocol;
23#[cfg(feature = "in-ra")]
24pub mod pool;
25#[cfg(feature = "in-ra")]
26pub mod process;
27#[cfg(feature = "in-proc-macro-srv")]
28pub mod token_stream;
29pub mod transport;
30
31use std::fmt;
32
33/// The versions of the server protocol
34pub mod version {
35    pub const NO_VERSION_CHECK_VERSION: u32 = 0;
36    pub const VERSION_CHECK_VERSION: u32 = 1;
37    pub const ENCODE_CLOSE_SPAN_VERSION: u32 = 2;
38    pub const HAS_GLOBAL_SPANS: u32 = 3;
39    pub const RUST_ANALYZER_SPAN_SUPPORT: u32 = 4;
40    /// Whether literals encode their kind as an additional u32 field and idents their rawness as a u32 field.
41    pub const EXTENDED_LEAF_DATA: u32 = 5;
42    pub const HASHED_AST_ID: u32 = 6;
43
44    /// Current API version of the proc-macro protocol.
45    pub const CURRENT_API_VERSION: u32 = HASHED_AST_ID;
46}
47
48/// Protocol format for communication between client and server.
49#[derive(Copy, Clone, Debug, PartialEq, Eq)]
50pub enum ProtocolFormat {
51    /// JSON-based legacy protocol (newline-delimited JSON).
52    JsonLegacy,
53    /// Bidirectional postcard protocol with sub-request support.
54    BidirectionalPostcardPrototype,
55}
56
57impl fmt::Display for ProtocolFormat {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        match self {
60            ProtocolFormat::JsonLegacy => write!(f, "json-legacy"),
61            ProtocolFormat::BidirectionalPostcardPrototype => {
62                write!(f, "bidirectional-postcard-prototype")
63            }
64        }
65    }
66}
67
68/// Represents different kinds of procedural macros that can be expanded by the external server.
69#[derive(Copy, Clone, Eq, PartialEq, Debug, serde_derive::Serialize, serde_derive::Deserialize)]
70pub enum ProcMacroKind {
71    /// A macro that derives implementations for a struct or enum.
72    CustomDerive,
73    /// An attribute-like procedural macro.
74    Attr,
75    // This used to be called FuncLike, so that's what the server expects currently.
76    #[serde(alias = "Bang")]
77    #[serde(rename(serialize = "FuncLike", deserialize = "FuncLike"))]
78    Bang,
79}