blob: a4b0ac46ed6ebf3220c4e3ac16bcedc2deb6b466 [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>),
Myron Ahneba35cf2020-02-05 19:41:51 +070089 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -040090 UniquePtr(Box<Ty1>),
91 Ref(Box<Ref>),
92 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -070093 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -070094 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -070095 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -070096 Slice(Box<Slice>),
97 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -040098}
99
100pub struct Ty1 {
101 pub name: Ident,
102 pub langle: Token![<],
103 pub inner: Type,
104 pub rangle: Token![>],
105}
106
107pub struct Ref {
108 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700109 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400110 pub mutability: Option<Token![mut]>,
111 pub inner: Type,
112}
113
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700114pub struct Slice {
115 pub bracket: Bracket,
116 pub inner: Type,
117}
118
David Tolnay7db73692019-10-20 14:51:12 -0400119#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700120pub enum Lang {
121 Cxx,
122 Rust,
123}
124
125#[derive(Copy, Clone, PartialEq)]
David Tolnay7db73692019-10-20 14:51:12 -0400126pub enum Derive {
127 Clone,
128 Copy,
129}