blob: 4eaf81e22315a170f52d51e565b4b01cad4f7744 [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;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070018use syn::token::{Brace, Bracket, Paren};
David Tolnaye3a48152020-04-08 19:38:05 -070019use 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),
Adrian Taylorf5dd5522020-04-13 16:50:14 -070087 Slice(Box<Slice>),
88 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -040089}
90
91pub struct Ty1 {
92 pub name: Ident,
93 pub langle: Token![<],
94 pub inner: Type,
95 pub rangle: Token![>],
96}
97
98pub struct Ref {
99 pub ampersand: Token![&],
100 pub mutability: Option<Token![mut]>,
101 pub inner: Type,
102}
103
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700104pub struct Slice {
105 pub bracket: Bracket,
106 pub inner: Type,
107}
108
David Tolnay7db73692019-10-20 14:51:12 -0400109#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700110pub enum Lang {
111 Cxx,
112 Rust,
113}
114
115#[derive(Copy, Clone, PartialEq)]
David Tolnay7db73692019-10-20 14:51:12 -0400116pub enum Derive {
117 Clone,
118 Copy,
119}