blob: 38e21b00b463fe8bfe1e7269fa1186e51de56f58 [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 Tolnay09462ac2020-03-20 14:58:41 -070016use syn::{token::Brace, LitStr, Token};
David Tolnay7db73692019-10-20 14:51:12 -040017
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,
David Tolnay09462ac2020-03-20 14:58:41 -070043 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040044 pub fields: Vec<Var>,
45}
46
47pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070048 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040049 pub doc: Doc,
David Tolnay7db73692019-10-20 14:51:12 -040050 pub ident: Ident,
David Tolnay16448732020-03-18 12:39:36 -070051 pub sig: Signature,
52 pub semi_token: Token![;],
53}
54
55pub struct Signature {
56 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -040057 pub receiver: Option<Receiver>,
58 pub args: Vec<Var>,
59 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -070060 pub throws: bool,
David Tolnayd95b1192020-03-18 20:07:46 -070061 pub tokens: TokenStream,
David Tolnay7db73692019-10-20 14:51:12 -040062}
63
David Tolnay417305a2020-03-18 13:54:00 -070064#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -040065pub struct Var {
66 pub ident: Ident,
67 pub ty: Type,
68}
69
70pub struct Receiver {
71 pub mutability: Option<Token![mut]>,
72 pub ident: Ident,
73}
74
David Tolnay7db73692019-10-20 14:51:12 -040075pub enum Type {
76 Ident(Ident),
77 RustBox(Box<Ty1>),
78 UniquePtr(Box<Ty1>),
79 Ref(Box<Ref>),
80 Str(Box<Ref>),
David Tolnay417305a2020-03-18 13:54:00 -070081 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -070082 Void(Span),
David Tolnay7db73692019-10-20 14:51:12 -040083}
84
85pub struct Ty1 {
86 pub name: Ident,
87 pub langle: Token![<],
88 pub inner: Type,
89 pub rangle: Token![>],
90}
91
92pub struct Ref {
93 pub ampersand: Token![&],
94 pub mutability: Option<Token![mut]>,
95 pub inner: Type,
96}
97
98#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -070099pub enum Lang {
100 Cxx,
101 Rust,
102}
103
104#[derive(Copy, Clone, PartialEq)]
David Tolnay7db73692019-10-20 14:51:12 -0400105pub enum Derive {
106 Clone,
107 Copy,
108}