xref: /third_party/rust/crates/cxx/syntax/mod.rs (revision 33d722a9)
1// Functionality that is shared between the cxxbridge macro and the cmd.
2
3pub mod atom;
4pub mod attrs;
5pub mod cfg;
6pub mod check;
7pub mod derive;
8mod discriminant;
9mod doc;
10pub mod error;
11pub mod file;
12pub mod ident;
13mod impls;
14mod improper;
15pub mod instantiate;
16pub mod mangle;
17pub mod map;
18mod names;
19pub mod namespace;
20mod parse;
21mod pod;
22pub mod qualified;
23pub mod report;
24pub mod resolve;
25pub mod set;
26pub mod symbol;
27mod tokens;
28mod toposort;
29pub mod trivial;
30pub mod types;
31mod visit;
32
33use self::attrs::OtherAttrs;
34use self::cfg::CfgExpr;
35use self::namespace::Namespace;
36use self::parse::kw;
37use self::symbol::Symbol;
38use proc_macro2::{Ident, Span};
39use syn::punctuated::Punctuated;
40use syn::token::{Brace, Bracket, Paren};
41use syn::{Attribute, Expr, Generics, Lifetime, LitInt, Token, Type as RustType};
42
43pub use self::atom::Atom;
44pub use self::derive::{Derive, Trait};
45pub use self::discriminant::Discriminant;
46pub use self::doc::Doc;
47pub use self::names::ForeignName;
48pub use self::parse::parse_items;
49pub use self::types::Types;
50
51pub enum Api {
52    Include(Include),
53    Struct(Struct),
54    Enum(Enum),
55    CxxType(ExternType),
56    CxxFunction(ExternFn),
57    RustType(ExternType),
58    RustFunction(ExternFn),
59    TypeAlias(TypeAlias),
60    Impl(Impl),
61}
62
63pub struct Include {
64    pub cfg: CfgExpr,
65    pub path: String,
66    pub kind: IncludeKind,
67    pub begin_span: Span,
68    pub end_span: Span,
69}
70
71/// Whether to emit `#include "path"` or `#include <path>`.
72#[derive(Copy, Clone, PartialEq, Debug)]
73pub enum IncludeKind {
74    /// `#include "quoted/path/to"`
75    Quoted,
76    /// `#include <bracketed/path/to>`
77    Bracketed,
78}
79
80pub struct ExternType {
81    pub cfg: CfgExpr,
82    pub lang: Lang,
83    pub doc: Doc,
84    pub derives: Vec<Derive>,
85    pub attrs: OtherAttrs,
86    pub visibility: Token![pub],
87    pub type_token: Token![type],
88    pub name: Pair,
89    pub generics: Lifetimes,
90    pub colon_token: Option<Token![:]>,
91    pub bounds: Vec<Derive>,
92    pub semi_token: Token![;],
93    pub trusted: bool,
94}
95
96pub struct Struct {
97    pub cfg: CfgExpr,
98    pub doc: Doc,
99    pub derives: Vec<Derive>,
100    pub attrs: OtherAttrs,
101    pub visibility: Token![pub],
102    pub struct_token: Token![struct],
103    pub name: Pair,
104    pub generics: Lifetimes,
105    pub brace_token: Brace,
106    pub fields: Vec<Var>,
107}
108
109pub struct Enum {
110    pub cfg: CfgExpr,
111    pub doc: Doc,
112    pub derives: Vec<Derive>,
113    pub attrs: OtherAttrs,
114    pub visibility: Token![pub],
115    pub enum_token: Token![enum],
116    pub name: Pair,
117    pub generics: Lifetimes,
118    pub brace_token: Brace,
119    pub variants: Vec<Variant>,
120    pub variants_from_header: bool,
121    pub variants_from_header_attr: Option<Attribute>,
122    pub repr: EnumRepr,
123    pub explicit_repr: bool,
124}
125
126pub enum EnumRepr {
127    Native {
128        atom: Atom,
129        repr_type: Type,
130    },
131    #[cfg(feature = "experimental-enum-variants-from-header")]
132    Foreign {
133        rust_type: syn::Path,
134    },
135}
136
137pub struct ExternFn {
138    pub cfg: CfgExpr,
139    pub lang: Lang,
140    pub doc: Doc,
141    pub attrs: OtherAttrs,
142    pub visibility: Token![pub],
143    pub name: Pair,
144    pub sig: Signature,
145    pub semi_token: Token![;],
146    pub trusted: bool,
147}
148
149pub struct TypeAlias {
150    pub cfg: CfgExpr,
151    pub doc: Doc,
152    pub derives: Vec<Derive>,
153    pub attrs: OtherAttrs,
154    pub visibility: Token![pub],
155    pub type_token: Token![type],
156    pub name: Pair,
157    pub generics: Lifetimes,
158    pub eq_token: Token![=],
159    pub ty: RustType,
160    pub semi_token: Token![;],
161}
162
163pub struct Impl {
164    pub cfg: CfgExpr,
165    pub impl_token: Token![impl],
166    pub impl_generics: Lifetimes,
167    pub negative: bool,
168    pub ty: Type,
169    pub ty_generics: Lifetimes,
170    pub brace_token: Brace,
171    pub negative_token: Option<Token![!]>,
172}
173
174#[derive(Clone, Default)]
175pub struct Lifetimes {
176    pub lt_token: Option<Token![<]>,
177    pub lifetimes: Punctuated<Lifetime, Token![,]>,
178    pub gt_token: Option<Token![>]>,
179}
180
181pub struct Signature {
182    pub asyncness: Option<Token![async]>,
183    pub unsafety: Option<Token![unsafe]>,
184    pub fn_token: Token![fn],
185    pub generics: Generics,
186    pub receiver: Option<Receiver>,
187    pub args: Punctuated<Var, Token![,]>,
188    pub ret: Option<Type>,
189    pub throws: bool,
190    pub paren_token: Paren,
191    pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
192}
193
194pub struct Var {
195    pub cfg: CfgExpr,
196    pub doc: Doc,
197    pub attrs: OtherAttrs,
198    pub visibility: Token![pub],
199    pub name: Pair,
200    pub colon_token: Token![:],
201    pub ty: Type,
202}
203
204pub struct Receiver {
205    pub pinned: bool,
206    pub ampersand: Token![&],
207    pub lifetime: Option<Lifetime>,
208    pub mutable: bool,
209    pub var: Token![self],
210    pub ty: NamedType,
211    pub colon_token: Token![:],
212    pub shorthand: bool,
213    pub pin_tokens: Option<(kw::Pin, Token![<], Token![>])>,
214    pub mutability: Option<Token![mut]>,
215}
216
217pub struct Variant {
218    pub cfg: CfgExpr,
219    pub doc: Doc,
220    pub attrs: OtherAttrs,
221    pub name: Pair,
222    pub discriminant: Discriminant,
223    pub expr: Option<Expr>,
224}
225
226pub enum Type {
227    Ident(NamedType),
228    RustBox(Box<Ty1>),
229    RustVec(Box<Ty1>),
230    UniquePtr(Box<Ty1>),
231    SharedPtr(Box<Ty1>),
232    WeakPtr(Box<Ty1>),
233    Ref(Box<Ref>),
234    Ptr(Box<Ptr>),
235    Str(Box<Ref>),
236    CxxVector(Box<Ty1>),
237    Fn(Box<Signature>),
238    Void(Span),
239    SliceRef(Box<SliceRef>),
240    Array(Box<Array>),
241}
242
243pub struct Ty1 {
244    pub name: Ident,
245    pub langle: Token![<],
246    pub inner: Type,
247    pub rangle: Token![>],
248}
249
250pub struct Ref {
251    pub pinned: bool,
252    pub ampersand: Token![&],
253    pub lifetime: Option<Lifetime>,
254    pub mutable: bool,
255    pub inner: Type,
256    pub pin_tokens: Option<(kw::Pin, Token![<], Token![>])>,
257    pub mutability: Option<Token![mut]>,
258}
259
260pub struct Ptr {
261    pub star: Token![*],
262    pub mutable: bool,
263    pub inner: Type,
264    pub mutability: Option<Token![mut]>,
265    pub constness: Option<Token![const]>,
266}
267
268pub struct SliceRef {
269    pub ampersand: Token![&],
270    pub lifetime: Option<Lifetime>,
271    pub mutable: bool,
272    pub bracket: Bracket,
273    pub inner: Type,
274    pub mutability: Option<Token![mut]>,
275}
276
277pub struct Array {
278    pub bracket: Bracket,
279    pub inner: Type,
280    pub semi_token: Token![;],
281    pub len: usize,
282    pub len_token: LitInt,
283}
284
285#[derive(Copy, Clone, PartialEq)]
286pub enum Lang {
287    Cxx,
288    Rust,
289}
290
291// An association of a defined Rust name with a fully resolved, namespace
292// qualified C++ name.
293#[derive(Clone)]
294pub struct Pair {
295    pub namespace: Namespace,
296    pub cxx: ForeignName,
297    pub rust: Ident,
298}
299
300// Wrapper for a type which needs to be resolved before it can be printed in
301// C++.
302#[derive(PartialEq, Eq, Hash)]
303pub struct NamedType {
304    pub rust: Ident,
305    pub generics: Lifetimes,
306}
307