blob: d5b90f0df994280d5878d7a5d8d593145268d7d7 [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;
Myron Ahneba35cf2020-02-05 19:41:51 +070011pub mod mangled;
David Tolnay08419302020-04-19 20:38:20 -070012pub mod namespace;
David Tolnay7db73692019-10-20 14:51:12 -040013mod parse;
14pub mod set;
David Tolnay891061b2020-04-19 22:42:33 -070015pub mod symbol;
David Tolnay7db73692019-10-20 14:51:12 -040016mod tokens;
17pub mod types;
18
David Tolnaye3a48152020-04-08 19:38:05 -070019use self::parse::kw;
20use proc_macro2::{Ident, Span};
21use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070022use syn::token::{Brace, Bracket, Paren};
David Tolnay0bd50fa2020-04-22 15:31:33 -070023use syn::{Lifetime, LitStr, Token};
David Tolnay7db73692019-10-20 14:51:12 -040024
25pub use self::atom::Atom;
26pub use self::doc::Doc;
27pub use self::parse::parse_items;
28pub use self::types::Types;
29
30pub enum Api {
31 Include(LitStr),
32 Struct(Struct),
33 CxxType(ExternType),
34 CxxFunction(ExternFn),
35 RustType(ExternType),
36 RustFunction(ExternFn),
37}
38
39pub struct ExternType {
40 pub doc: Doc,
41 pub type_token: Token![type],
42 pub ident: Ident,
43}
44
45pub struct Struct {
46 pub doc: Doc,
47 pub derives: Vec<Ident>,
48 pub struct_token: Token![struct],
49 pub ident: Ident,
David Tolnay09462ac2020-03-20 14:58:41 -070050 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040051 pub fields: Vec<Var>,
52}
53
54pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070055 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040056 pub doc: Doc,
David Tolnay7db73692019-10-20 14:51:12 -040057 pub ident: Ident,
David Tolnay16448732020-03-18 12:39:36 -070058 pub sig: Signature,
59 pub semi_token: Token![;],
60}
61
62pub struct Signature {
63 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -040064 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -070065 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -040066 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -070067 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -070068 pub paren_token: Paren,
69 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -040070}
71
David Tolnay417305a2020-03-18 13:54:00 -070072#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -040073pub struct Var {
74 pub ident: Ident,
75 pub ty: Type,
76}
77
78pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -070079 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -070080 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -040081 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -070082 pub var: Token![self],
83 pub ty: Ident,
David Tolnay62d360c2020-04-22 16:26:21 -070084 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -040085}
86
David Tolnay7db73692019-10-20 14:51:12 -040087pub enum Type {
88 Ident(Ident),
89 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +070090 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -040091 UniquePtr(Box<Ty1>),
92 Ref(Box<Ref>),
93 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -070094 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -070095 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -070096 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -070097 Slice(Box<Slice>),
98 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -040099}
100
101pub struct Ty1 {
102 pub name: Ident,
103 pub langle: Token![<],
104 pub inner: Type,
105 pub rangle: Token![>],
106}
107
108pub struct Ref {
109 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700110 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400111 pub mutability: Option<Token![mut]>,
112 pub inner: Type,
113}
114
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700115pub struct Slice {
116 pub bracket: Bracket,
117 pub inner: Type,
118}
119
David Tolnay7db73692019-10-20 14:51:12 -0400120#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700121pub enum Lang {
122 Cxx,
123 Rust,
124}
125
126#[derive(Copy, Clone, PartialEq)]
David Tolnay7db73692019-10-20 14:51:12 -0400127pub enum Derive {
128 Clone,
129 Copy,
130}