blob: 88c96e8cf6df9b26091010ca06e0d79650c59aae [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;
David Tolnay64703b42020-05-10 22:12:33 -07006mod derive;
David Tolnay17e137f2020-05-08 15:55:28 -07007mod discriminant;
David Tolnay7db73692019-10-20 14:51:12 -04008mod doc;
9pub mod error;
10pub mod ident;
11mod impls;
David Tolnay3caa50a2020-04-19 21:25:34 -070012pub mod mangle;
David Tolnay08419302020-04-19 20:38:20 -070013pub mod namespace;
David Tolnay7db73692019-10-20 14:51:12 -040014mod parse;
David Tolnaydf344a82020-05-03 23:23:18 -070015pub mod report;
David Tolnay7db73692019-10-20 14:51:12 -040016pub mod set;
David Tolnay891061b2020-04-19 22:42:33 -070017pub mod symbol;
David Tolnay7db73692019-10-20 14:51:12 -040018mod tokens;
19pub mod types;
20
David Tolnay17e137f2020-05-08 15:55:28 -070021use self::discriminant::Discriminant;
David Tolnaye3a48152020-04-08 19:38:05 -070022use self::parse::kw;
23use proc_macro2::{Ident, Span};
24use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070025use syn::token::{Brace, Bracket, Paren};
David Tolnay91e87fa2020-05-11 19:10:23 -070026use syn::{Expr, Lifetime, Token, Type as RustType};
David Tolnay7db73692019-10-20 14:51:12 -040027
28pub use self::atom::Atom;
David Tolnay64703b42020-05-10 22:12:33 -070029pub use self::derive::Derive;
David Tolnay7db73692019-10-20 14:51:12 -040030pub use self::doc::Doc;
31pub use self::parse::parse_items;
32pub use self::types::Types;
33
34pub enum Api {
David Tolnay91e87fa2020-05-11 19:10:23 -070035 Include(String),
David Tolnay7db73692019-10-20 14:51:12 -040036 Struct(Struct),
Joel Galensonc03402a2020-04-23 17:31:09 -070037 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040038 CxxType(ExternType),
39 CxxFunction(ExternFn),
40 RustType(ExternType),
41 RustFunction(ExternFn),
David Tolnay99383812020-05-04 02:34:33 -070042 TypeAlias(TypeAlias),
David Tolnay7db73692019-10-20 14:51:12 -040043}
44
45pub struct ExternType {
46 pub doc: Doc,
47 pub type_token: Token![type],
48 pub ident: Ident,
49}
50
51pub struct Struct {
52 pub doc: Doc,
David Tolnaye86b9cf2020-05-10 14:24:29 -070053 pub derives: Vec<Derive>,
David Tolnay7db73692019-10-20 14:51:12 -040054 pub struct_token: Token![struct],
55 pub ident: Ident,
David Tolnay09462ac2020-03-20 14:58:41 -070056 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040057 pub fields: Vec<Var>,
58}
59
Joel Galensonc03402a2020-04-23 17:31:09 -070060pub struct Enum {
61 pub doc: Doc,
62 pub enum_token: Token![enum],
63 pub ident: Ident,
64 pub brace_token: Brace,
65 pub variants: Vec<Variant>,
David Tolnaye2e303f2020-05-10 20:52:00 -070066 pub repr: Atom,
Joel Galensonc03402a2020-04-23 17:31:09 -070067}
68
David Tolnay7db73692019-10-20 14:51:12 -040069pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070070 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040071 pub doc: Doc,
David Tolnay7db73692019-10-20 14:51:12 -040072 pub ident: Ident,
David Tolnay16448732020-03-18 12:39:36 -070073 pub sig: Signature,
74 pub semi_token: Token![;],
75}
76
David Tolnay99383812020-05-04 02:34:33 -070077pub struct TypeAlias {
78 pub type_token: Token![type],
79 pub ident: Ident,
80 pub eq_token: Token![=],
81 pub ty: RustType,
82 pub semi_token: Token![;],
83}
84
David Tolnay16448732020-03-18 12:39:36 -070085pub struct Signature {
86 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -040087 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -070088 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -040089 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -070090 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -070091 pub paren_token: Paren,
92 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -040093}
94
David Tolnay417305a2020-03-18 13:54:00 -070095#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -040096pub struct Var {
97 pub ident: Ident,
98 pub ty: Type,
99}
100
101pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -0700102 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700103 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400104 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -0700105 pub var: Token![self],
106 pub ty: Ident,
David Tolnay62d360c2020-04-22 16:26:21 -0700107 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400108}
109
Joel Galensonc03402a2020-04-23 17:31:09 -0700110pub struct Variant {
111 pub ident: Ident,
David Tolnay17e137f2020-05-08 15:55:28 -0700112 pub discriminant: Discriminant,
David Tolnay2b8bf6d2020-05-10 17:37:16 -0700113 pub expr: Option<Expr>,
Joel Galensonc03402a2020-04-23 17:31:09 -0700114}
115
David Tolnay7db73692019-10-20 14:51:12 -0400116pub enum Type {
117 Ident(Ident),
118 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700119 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400120 UniquePtr(Box<Ty1>),
121 Ref(Box<Ref>),
122 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700123 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700124 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700125 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700126 Slice(Box<Slice>),
127 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400128}
129
130pub struct Ty1 {
131 pub name: Ident,
132 pub langle: Token![<],
133 pub inner: Type,
134 pub rangle: Token![>],
135}
136
137pub struct Ref {
138 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700139 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400140 pub mutability: Option<Token![mut]>,
141 pub inner: Type,
142}
143
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700144pub struct Slice {
145 pub bracket: Bracket,
146 pub inner: Type,
147}
148
David Tolnay7db73692019-10-20 14:51:12 -0400149#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700150pub enum Lang {
151 Cxx,
152 Rust,
153}