blob: b160e5a639038474b111ac9917f4d41d313e2f98 [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 Tolnaye3a48152020-04-08 19:38:05 -070022use syn::{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 Tolnay7db73692019-10-20 14:51:12 -040079 pub mutability: Option<Token![mut]>,
80 pub ident: Ident,
81}
82
David Tolnay7db73692019-10-20 14:51:12 -040083pub enum Type {
84 Ident(Ident),
85 RustBox(Box<Ty1>),
86 UniquePtr(Box<Ty1>),
87 Ref(Box<Ref>),
88 Str(Box<Ref>),
David Tolnay417305a2020-03-18 13:54:00 -070089 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -070090 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -070091 Slice(Box<Slice>),
92 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -040093}
94
95pub struct Ty1 {
96 pub name: Ident,
97 pub langle: Token![<],
98 pub inner: Type,
99 pub rangle: Token![>],
100}
101
102pub struct Ref {
103 pub ampersand: Token![&],
104 pub mutability: Option<Token![mut]>,
105 pub inner: Type,
106}
107
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700108pub struct Slice {
109 pub bracket: Bracket,
110 pub inner: Type,
111}
112
David Tolnay7db73692019-10-20 14:51:12 -0400113#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700114pub enum Lang {
115 Cxx,
116 Rust,
117}
118
119#[derive(Copy, Clone, PartialEq)]
David Tolnay7db73692019-10-20 14:51:12 -0400120pub enum Derive {
121 Clone,
122 Copy,
123}