blob: 9e4cbbca6a1addca3125d91bfeae5577e4cafb56 [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;
David Tolnaydf344a82020-05-03 23:23:18 -070013pub mod report;
David Tolnay7db73692019-10-20 14:51:12 -040014pub 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 Tolnay99383812020-05-04 02:34:33 -070023use syn::{Lifetime, LitStr, Token, Type as RustType};
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),
Joel Galensonc03402a2020-04-23 17:31:09 -070033 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040034 CxxType(ExternType),
35 CxxFunction(ExternFn),
36 RustType(ExternType),
37 RustFunction(ExternFn),
David Tolnay99383812020-05-04 02:34:33 -070038 TypeAlias(TypeAlias),
David Tolnay7db73692019-10-20 14:51:12 -040039}
40
41pub struct ExternType {
42 pub doc: Doc,
43 pub type_token: Token![type],
44 pub ident: Ident,
45}
46
47pub struct Struct {
48 pub doc: Doc,
David Tolnaye86b9cf2020-05-10 14:24:29 -070049 pub derives: Vec<Derive>,
David Tolnay7db73692019-10-20 14:51:12 -040050 pub struct_token: Token![struct],
51 pub ident: Ident,
David Tolnay09462ac2020-03-20 14:58:41 -070052 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040053 pub fields: Vec<Var>,
54}
55
Joel Galensonc03402a2020-04-23 17:31:09 -070056pub struct Enum {
57 pub doc: Doc,
58 pub enum_token: Token![enum],
59 pub ident: Ident,
60 pub brace_token: Brace,
61 pub variants: Vec<Variant>,
62}
63
David Tolnay7db73692019-10-20 14:51:12 -040064pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070065 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040066 pub doc: Doc,
David Tolnay7db73692019-10-20 14:51:12 -040067 pub ident: Ident,
David Tolnay16448732020-03-18 12:39:36 -070068 pub sig: Signature,
69 pub semi_token: Token![;],
70}
71
David Tolnay99383812020-05-04 02:34:33 -070072pub struct TypeAlias {
73 pub type_token: Token![type],
74 pub ident: Ident,
75 pub eq_token: Token![=],
76 pub ty: RustType,
77 pub semi_token: Token![;],
78}
79
David Tolnay16448732020-03-18 12:39:36 -070080pub struct Signature {
81 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -040082 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -070083 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -040084 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -070085 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -070086 pub paren_token: Paren,
87 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -040088}
89
David Tolnay417305a2020-03-18 13:54:00 -070090#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -040091pub struct Var {
92 pub ident: Ident,
93 pub ty: Type,
94}
95
96pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -070097 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -070098 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -040099 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -0700100 pub var: Token![self],
101 pub ty: Ident,
David Tolnay62d360c2020-04-22 16:26:21 -0700102 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400103}
104
Joel Galensonc03402a2020-04-23 17:31:09 -0700105pub struct Variant {
106 pub ident: Ident,
Joel Galenson88547732020-05-05 08:23:42 -0700107 pub discriminant: u32,
Joel Galensonc03402a2020-04-23 17:31:09 -0700108}
109
David Tolnay7db73692019-10-20 14:51:12 -0400110pub enum Type {
111 Ident(Ident),
112 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700113 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400114 UniquePtr(Box<Ty1>),
115 Ref(Box<Ref>),
116 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700117 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700118 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700119 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700120 Slice(Box<Slice>),
121 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400122}
123
124pub struct Ty1 {
125 pub name: Ident,
126 pub langle: Token![<],
127 pub inner: Type,
128 pub rangle: Token![>],
129}
130
131pub struct Ref {
132 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700133 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400134 pub mutability: Option<Token![mut]>,
135 pub inner: Type,
136}
137
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700138pub struct Slice {
139 pub bracket: Bracket,
140 pub inner: Type,
141}
142
David Tolnay7db73692019-10-20 14:51:12 -0400143#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700144pub enum Lang {
145 Cxx,
146 Rust,
147}
148
149#[derive(Copy, Clone, PartialEq)]
David Tolnay7db73692019-10-20 14:51:12 -0400150pub enum Derive {
151 Clone,
152 Copy,
153}