blob: b7ca6b7bf71b7f53eac5bec41f1e71c99adcdc9f [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;
David Tolnay3caa50a2020-04-19 21:25:34 -070010pub mod mangle;
David Tolnay08419302020-04-19 20:38:20 -070011pub mod namespace;
David Tolnay7db73692019-10-20 14:51:12 -040012mod parse;
13pub mod set;
14mod tokens;
15pub mod types;
16
David Tolnaye3a48152020-04-08 19:38:05 -070017use self::parse::kw;
18use proc_macro2::{Ident, Span};
19use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070020use syn::token::{Brace, Bracket, Paren};
David Tolnaye3a48152020-04-08 19:38:05 -070021use syn::{LitStr, Token};
David Tolnay7db73692019-10-20 14:51:12 -040022
23pub use self::atom::Atom;
24pub use self::doc::Doc;
25pub use self::parse::parse_items;
26pub use self::types::Types;
27
28pub enum Api {
29 Include(LitStr),
30 Struct(Struct),
31 CxxType(ExternType),
32 CxxFunction(ExternFn),
33 RustType(ExternType),
34 RustFunction(ExternFn),
35}
36
37pub struct ExternType {
38 pub doc: Doc,
39 pub type_token: Token![type],
40 pub ident: Ident,
41}
42
43pub struct Struct {
44 pub doc: Doc,
45 pub derives: Vec<Ident>,
46 pub struct_token: Token![struct],
47 pub ident: Ident,
David Tolnay09462ac2020-03-20 14:58:41 -070048 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040049 pub fields: Vec<Var>,
50}
51
52pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070053 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040054 pub doc: Doc,
David Tolnay7db73692019-10-20 14:51:12 -040055 pub ident: Ident,
David Tolnay16448732020-03-18 12:39:36 -070056 pub sig: Signature,
57 pub semi_token: Token![;],
58}
59
60pub struct Signature {
61 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -040062 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -070063 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -040064 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -070065 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -070066 pub paren_token: Paren,
67 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -040068}
69
David Tolnay417305a2020-03-18 13:54:00 -070070#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -040071pub struct Var {
72 pub ident: Ident,
73 pub ty: Type,
74}
75
76pub struct Receiver {
77 pub mutability: Option<Token![mut]>,
78 pub ident: Ident,
79}
80
David Tolnay7db73692019-10-20 14:51:12 -040081pub enum Type {
82 Ident(Ident),
83 RustBox(Box<Ty1>),
84 UniquePtr(Box<Ty1>),
85 Ref(Box<Ref>),
86 Str(Box<Ref>),
David Tolnay417305a2020-03-18 13:54:00 -070087 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -070088 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -070089 Slice(Box<Slice>),
90 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -040091}
92
93pub struct Ty1 {
94 pub name: Ident,
95 pub langle: Token![<],
96 pub inner: Type,
97 pub rangle: Token![>],
98}
99
100pub struct Ref {
101 pub ampersand: Token![&],
102 pub mutability: Option<Token![mut]>,
103 pub inner: Type,
104}
105
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700106pub struct Slice {
107 pub bracket: Bracket,
108 pub inner: Type,
109}
110
David Tolnay7db73692019-10-20 14:51:12 -0400111#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700112pub enum Lang {
113 Cxx,
114 Rust,
115}
116
117#[derive(Copy, Clone, PartialEq)]
David Tolnay7db73692019-10-20 14:51:12 -0400118pub enum Derive {
119 Clone,
120 Copy,
121}