blob: cf811cf530f733af7539fd8f00f52cc989c2ec6d [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 Tolnay7db73692019-10-20 14:51:12 -040083}
84
David Tolnay7db73692019-10-20 14:51:12 -040085pub enum Type {
86 Ident(Ident),
87 RustBox(Box<Ty1>),
88 UniquePtr(Box<Ty1>),
89 Ref(Box<Ref>),
90 Str(Box<Ref>),
David Tolnay417305a2020-03-18 13:54:00 -070091 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -070092 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -070093 Slice(Box<Slice>),
94 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -040095}
96
97pub struct Ty1 {
98 pub name: Ident,
99 pub langle: Token![<],
100 pub inner: Type,
101 pub rangle: Token![>],
102}
103
104pub struct Ref {
105 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700106 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400107 pub mutability: Option<Token![mut]>,
108 pub inner: Type,
109}
110
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700111pub struct Slice {
112 pub bracket: Bracket,
113 pub inner: Type,
114}
115
David Tolnay7db73692019-10-20 14:51:12 -0400116#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700117pub enum Lang {
118 Cxx,
119 Rust,
120}
121
122#[derive(Copy, Clone, PartialEq)]
David Tolnay7db73692019-10-20 14:51:12 -0400123pub enum Derive {
124 Clone,
125 Copy,
126}