blob: 7d2ca19d319ded204bdd8ab64b86166b819421e2 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001// Functionality that is shared between the cxxbridge macro and the cmd.
2
3pub mod atom;
4mod attrs;
5pub mod check;
6mod doc;
7pub mod error;
8pub mod ident;
9mod impls;
10mod parse;
11pub mod set;
12mod tokens;
13pub mod types;
14
David Tolnayd95b1192020-03-18 20:07:46 -070015use proc_macro2::{Ident, Span, TokenStream};
David Tolnay7db73692019-10-20 14:51:12 -040016use syn::{LitStr, Token};
17
18pub use self::atom::Atom;
19pub use self::doc::Doc;
20pub use self::parse::parse_items;
21pub use self::types::Types;
22
23pub enum Api {
24 Include(LitStr),
25 Struct(Struct),
26 CxxType(ExternType),
27 CxxFunction(ExternFn),
28 RustType(ExternType),
29 RustFunction(ExternFn),
30}
31
32pub struct ExternType {
33 pub doc: Doc,
34 pub type_token: Token![type],
35 pub ident: Ident,
36}
37
38pub struct Struct {
39 pub doc: Doc,
40 pub derives: Vec<Ident>,
41 pub struct_token: Token![struct],
42 pub ident: Ident,
43 pub fields: Vec<Var>,
44}
45
46pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070047 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040048 pub doc: Doc,
David Tolnay7db73692019-10-20 14:51:12 -040049 pub ident: Ident,
David Tolnay16448732020-03-18 12:39:36 -070050 pub sig: Signature,
51 pub semi_token: Token![;],
52}
53
54pub struct Signature {
55 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -040056 pub receiver: Option<Receiver>,
57 pub args: Vec<Var>,
58 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -070059 pub throws: bool,
David Tolnayd95b1192020-03-18 20:07:46 -070060 pub tokens: TokenStream,
David Tolnay7db73692019-10-20 14:51:12 -040061}
62
David Tolnay417305a2020-03-18 13:54:00 -070063#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -040064pub struct Var {
65 pub ident: Ident,
66 pub ty: Type,
67}
68
69pub struct Receiver {
70 pub mutability: Option<Token![mut]>,
71 pub ident: Ident,
72}
73
David Tolnay7db73692019-10-20 14:51:12 -040074pub enum Type {
75 Ident(Ident),
76 RustBox(Box<Ty1>),
77 UniquePtr(Box<Ty1>),
78 Ref(Box<Ref>),
79 Str(Box<Ref>),
David Tolnay417305a2020-03-18 13:54:00 -070080 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -070081 Void(Span),
David Tolnay7db73692019-10-20 14:51:12 -040082}
83
84pub struct Ty1 {
85 pub name: Ident,
86 pub langle: Token![<],
87 pub inner: Type,
88 pub rangle: Token![>],
89}
90
91pub struct Ref {
92 pub ampersand: Token![&],
93 pub mutability: Option<Token![mut]>,
94 pub inner: Type,
95}
96
97#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -070098pub enum Lang {
99 Cxx,
100 Rust,
101}
102
103#[derive(Copy, Clone, PartialEq)]
David Tolnay7db73692019-10-20 14:51:12 -0400104pub enum Derive {
105 Clone,
106 Copy,
107}