• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Functionality that is shared between the cxxbridge macro and the cmd.
2 
3 pub mod atom;
4 pub mod attrs;
5 pub mod cfg;
6 pub mod check;
7 pub mod derive;
8 mod discriminant;
9 mod doc;
10 pub mod error;
11 pub mod file;
12 pub mod ident;
13 mod impls;
14 mod improper;
15 pub mod instantiate;
16 pub mod mangle;
17 pub mod map;
18 mod names;
19 pub mod namespace;
20 mod parse;
21 mod pod;
22 pub mod qualified;
23 pub mod report;
24 pub mod resolve;
25 pub mod set;
26 pub mod symbol;
27 mod tokens;
28 mod toposort;
29 pub mod trivial;
30 pub mod types;
31 mod visit;
32 
33 use self::attrs::OtherAttrs;
34 use self::cfg::CfgExpr;
35 use self::namespace::Namespace;
36 use self::parse::kw;
37 use self::symbol::Symbol;
38 use proc_macro2::{Ident, Span};
39 use syn::punctuated::Punctuated;
40 use syn::token::{Brace, Bracket, Paren};
41 use syn::{Attribute, Expr, Generics, Lifetime, LitInt, Token, Type as RustType};
42 
43 pub use self::atom::Atom;
44 pub use self::derive::{Derive, Trait};
45 pub use self::discriminant::Discriminant;
46 pub use self::doc::Doc;
47 pub use self::names::ForeignName;
48 pub use self::parse::parse_items;
49 pub use self::types::Types;
50 
51 pub 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 
63 pub 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)]
73 pub enum IncludeKind {
74     /// `#include "quoted/path/to"`
75     Quoted,
76     /// `#include <bracketed/path/to>`
77     Bracketed,
78 }
79 
80 pub 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 
96 pub 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 
109 pub 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 
126 pub 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 
137 pub 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 
149 pub 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 
163 pub 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)]
175 pub struct Lifetimes {
176     pub lt_token: Option<Token![<]>,
177     pub lifetimes: Punctuated<Lifetime, Token![,]>,
178     pub gt_token: Option<Token![>]>,
179 }
180 
181 pub 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 
194 pub 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 
204 pub 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 
217 pub 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 
226 pub 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 
243 pub struct Ty1 {
244     pub name: Ident,
245     pub langle: Token![<],
246     pub inner: Type,
247     pub rangle: Token![>],
248 }
249 
250 pub 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 
260 pub 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 
268 pub 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 
277 pub 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)]
286 pub 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)]
294 pub 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)]
303 pub struct NamedType {
304     pub rust: Ident,
305     pub generics: Lifetimes,
306 }
307