blob: 21ff447df1a129ff0f07f1ba610c840ff59da2fc [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 Tolnay08419302020-04-19 20:38:20 -070010pub mod namespace;
David Tolnay7db73692019-10-20 14:51:12 -040011mod parse;
12pub mod set;
13mod tokens;
14pub mod types;
15
David Tolnaye3a48152020-04-08 19:38:05 -070016use self::parse::kw;
17use proc_macro2::{Ident, Span};
18use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070019use syn::token::{Brace, Bracket, Paren};
David Tolnaye3a48152020-04-08 19:38:05 -070020use syn::{LitStr, Token};
David Tolnay7db73692019-10-20 14:51:12 -040021
22pub use self::atom::Atom;
23pub use self::doc::Doc;
24pub use self::parse::parse_items;
25pub use self::types::Types;
26
27pub enum Api {
28 Include(LitStr),
29 Struct(Struct),
30 CxxType(ExternType),
31 CxxFunction(ExternFn),
32 RustType(ExternType),
33 RustFunction(ExternFn),
34}
35
36pub struct ExternType {
37 pub doc: Doc,
38 pub type_token: Token![type],
39 pub ident: Ident,
40}
41
42pub struct Struct {
43 pub doc: Doc,
44 pub derives: Vec<Ident>,
45 pub struct_token: Token![struct],
46 pub ident: Ident,
David Tolnay09462ac2020-03-20 14:58:41 -070047 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040048 pub fields: Vec<Var>,
49}
50
51pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070052 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040053 pub doc: Doc,
David Tolnay7db73692019-10-20 14:51:12 -040054 pub ident: Ident,
David Tolnay16448732020-03-18 12:39:36 -070055 pub sig: Signature,
56 pub semi_token: Token![;],
57}
58
59pub struct Signature {
60 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -040061 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -070062 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -040063 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -070064 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -070065 pub paren_token: Paren,
66 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -040067}
68
David Tolnay417305a2020-03-18 13:54:00 -070069#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -040070pub struct Var {
71 pub ident: Ident,
72 pub ty: Type,
73}
74
75pub struct Receiver {
76 pub mutability: Option<Token![mut]>,
77 pub ident: Ident,
78}
79
David Tolnay7db73692019-10-20 14:51:12 -040080pub enum Type {
81 Ident(Ident),
82 RustBox(Box<Ty1>),
83 UniquePtr(Box<Ty1>),
84 Ref(Box<Ref>),
85 Str(Box<Ref>),
David Tolnay417305a2020-03-18 13:54:00 -070086 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -070087 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -070088 Slice(Box<Slice>),
89 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -040090}
91
92pub struct Ty1 {
93 pub name: Ident,
94 pub langle: Token![<],
95 pub inner: Type,
96 pub rangle: Token![>],
97}
98
99pub struct Ref {
100 pub ampersand: Token![&],
101 pub mutability: Option<Token![mut]>,
102 pub inner: Type,
103}
104
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700105pub struct Slice {
106 pub bracket: Bracket,
107 pub inner: Type,
108}
109
David Tolnay7db73692019-10-20 14:51:12 -0400110#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700111pub enum Lang {
112 Cxx,
113 Rust,
114}
115
116#[derive(Copy, Clone, PartialEq)]
David Tolnay7db73692019-10-20 14:51:12 -0400117pub enum Derive {
118 Clone,
119 Copy,
120}