blob: 81b1a2f828704201fc72fa87df1cbc660b5c9f63 [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;
David Tolnay5ea922a2020-04-19 21:58:06 -070014mod symbol;
David Tolnay7db73692019-10-20 14:51:12 -040015mod tokens;
16pub mod types;
17
David Tolnaye3a48152020-04-08 19:38:05 -070018use self::parse::kw;
19use proc_macro2::{Ident, Span};
20use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070021use syn::token::{Brace, Bracket, Paren};
David Tolnaye3a48152020-04-08 19:38:05 -070022use syn::{LitStr, Token};
David Tolnay7db73692019-10-20 14:51:12 -040023
24pub use self::atom::Atom;
25pub use self::doc::Doc;
26pub use self::parse::parse_items;
27pub use self::types::Types;
28
29pub enum Api {
30 Include(LitStr),
31 Struct(Struct),
32 CxxType(ExternType),
33 CxxFunction(ExternFn),
34 RustType(ExternType),
35 RustFunction(ExternFn),
36}
37
38pub struct ExternType {
39 pub doc: Doc,
40 pub type_token: Token![type],
41 pub ident: Ident,
42}
43
44pub struct Struct {
45 pub doc: Doc,
46 pub derives: Vec<Ident>,
47 pub struct_token: Token![struct],
48 pub ident: Ident,
David Tolnay09462ac2020-03-20 14:58:41 -070049 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040050 pub fields: Vec<Var>,
51}
52
53pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070054 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040055 pub doc: Doc,
David Tolnay7db73692019-10-20 14:51:12 -040056 pub ident: Ident,
David Tolnay16448732020-03-18 12:39:36 -070057 pub sig: Signature,
58 pub semi_token: Token![;],
59}
60
61pub struct Signature {
62 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -040063 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -070064 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -040065 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -070066 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -070067 pub paren_token: Paren,
68 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -040069}
70
David Tolnay417305a2020-03-18 13:54:00 -070071#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -040072pub struct Var {
73 pub ident: Ident,
74 pub ty: Type,
75}
76
77pub struct Receiver {
78 pub mutability: Option<Token![mut]>,
79 pub ident: Ident,
80}
81
David Tolnay7db73692019-10-20 14:51:12 -040082pub enum Type {
83 Ident(Ident),
84 RustBox(Box<Ty1>),
85 UniquePtr(Box<Ty1>),
86 Ref(Box<Ref>),
87 Str(Box<Ref>),
David Tolnay417305a2020-03-18 13:54:00 -070088 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -070089 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -070090 Slice(Box<Slice>),
91 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -040092}
93
94pub struct Ty1 {
95 pub name: Ident,
96 pub langle: Token![<],
97 pub inner: Type,
98 pub rangle: Token![>],
99}
100
101pub struct Ref {
102 pub ampersand: Token![&],
103 pub mutability: Option<Token![mut]>,
104 pub inner: Type,
105}
106
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700107pub struct Slice {
108 pub bracket: Bracket,
109 pub inner: Type,
110}
111
David Tolnay7db73692019-10-20 14:51:12 -0400112#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700113pub enum Lang {
114 Cxx,
115 Rust,
116}
117
118#[derive(Copy, Clone, PartialEq)]
David Tolnay7db73692019-10-20 14:51:12 -0400119pub enum Derive {
120 Clone,
121 Copy,
122}