blob: ab5cecccee87a391a12e112b6613d604d04d62ed [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 Tolnay891061b2020-04-19 22:42:33 -070014pub mod 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 Tolnay0bd50fa2020-04-22 15:31:33 -070022use syn::{Lifetime, 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 {
David Tolnayfb6e3862020-04-20 01:33:23 -070078 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -070079 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -040080 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -070081 pub var: Token![self],
82 pub ty: Ident,
David Tolnay62d360c2020-04-22 16:26:21 -070083 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -040084}
85
David Tolnay7db73692019-10-20 14:51:12 -040086pub enum Type {
87 Ident(Ident),
88 RustBox(Box<Ty1>),
89 UniquePtr(Box<Ty1>),
90 Ref(Box<Ref>),
91 Str(Box<Ref>),
David Tolnay417305a2020-03-18 13:54:00 -070092 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -070093 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -070094 Slice(Box<Slice>),
95 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -040096}
97
98pub struct Ty1 {
99 pub name: Ident,
100 pub langle: Token![<],
101 pub inner: Type,
102 pub rangle: Token![>],
103}
104
105pub struct Ref {
106 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700107 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400108 pub mutability: Option<Token![mut]>,
109 pub inner: Type,
110}
111
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700112pub struct Slice {
113 pub bracket: Bracket,
114 pub inner: Type,
115}
116
David Tolnay7db73692019-10-20 14:51:12 -0400117#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700118pub enum Lang {
119 Cxx,
120 Rust,
121}
122
123#[derive(Copy, Clone, PartialEq)]
David Tolnay7db73692019-10-20 14:51:12 -0400124pub enum Derive {
125 Clone,
126 Copy,
127}