blob: 0d0328b4543fdf910f589b5c46336f8fb5abc5ec [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 Tolnaye3a48152020-04-08 19:38:05 -070015use self::parse::kw;
16use proc_macro2::{Ident, Span};
17use syn::punctuated::Punctuated;
18use syn::token::{Brace, Paren};
19use syn::{LitStr, Token};
David Tolnay7db73692019-10-20 14:51:12 -040020
21pub use self::atom::Atom;
22pub use self::doc::Doc;
23pub use self::parse::parse_items;
24pub use self::types::Types;
25
26pub enum Api {
27 Include(LitStr),
28 Struct(Struct),
29 CxxType(ExternType),
30 CxxFunction(ExternFn),
31 RustType(ExternType),
32 RustFunction(ExternFn),
33}
34
35pub struct ExternType {
36 pub doc: Doc,
37 pub type_token: Token![type],
38 pub ident: Ident,
39}
40
41pub struct Struct {
42 pub doc: Doc,
43 pub derives: Vec<Ident>,
44 pub struct_token: Token![struct],
45 pub ident: Ident,
David Tolnay09462ac2020-03-20 14:58:41 -070046 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040047 pub fields: Vec<Var>,
48}
49
50pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070051 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040052 pub doc: Doc,
David Tolnay7db73692019-10-20 14:51:12 -040053 pub ident: Ident,
David Tolnay16448732020-03-18 12:39:36 -070054 pub sig: Signature,
55 pub semi_token: Token![;],
56}
57
58pub struct Signature {
59 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -040060 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -070061 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -040062 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -070063 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -070064 pub paren_token: Paren,
65 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -040066}
67
David Tolnay417305a2020-03-18 13:54:00 -070068#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -040069pub struct Var {
70 pub ident: Ident,
71 pub ty: Type,
72}
73
74pub struct Receiver {
75 pub mutability: Option<Token![mut]>,
76 pub ident: Ident,
77}
78
David Tolnay7db73692019-10-20 14:51:12 -040079pub enum Type {
80 Ident(Ident),
81 RustBox(Box<Ty1>),
82 UniquePtr(Box<Ty1>),
83 Ref(Box<Ref>),
84 Str(Box<Ref>),
David Tolnay417305a2020-03-18 13:54:00 -070085 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -070086 Void(Span),
David Tolnay7db73692019-10-20 14:51:12 -040087}
88
89pub struct Ty1 {
90 pub name: Ident,
91 pub langle: Token![<],
92 pub inner: Type,
93 pub rangle: Token![>],
94}
95
96pub struct Ref {
97 pub ampersand: Token![&],
98 pub mutability: Option<Token![mut]>,
99 pub inner: Type,
100}
101
102#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700103pub enum Lang {
104 Cxx,
105 Rust,
106}
107
108#[derive(Copy, Clone, PartialEq)]
David Tolnay7db73692019-10-20 14:51:12 -0400109pub enum Derive {
110 Clone,
111 Copy,
112}