blob: c10922916533de3a8fa6fd601fb6d24e20cca1ca [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 Tolnay99383812020-05-04 02:34:33 -070026use syn::{Lifetime, LitStr, 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 {
35 Include(LitStr),
36 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>,
66}
67
David Tolnay7db73692019-10-20 14:51:12 -040068pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070069 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040070 pub doc: Doc,
David Tolnay7db73692019-10-20 14:51:12 -040071 pub ident: Ident,
David Tolnay16448732020-03-18 12:39:36 -070072 pub sig: Signature,
73 pub semi_token: Token![;],
74}
75
David Tolnay99383812020-05-04 02:34:33 -070076pub struct TypeAlias {
77 pub type_token: Token![type],
78 pub ident: Ident,
79 pub eq_token: Token![=],
80 pub ty: RustType,
81 pub semi_token: Token![;],
82}
83
David Tolnay16448732020-03-18 12:39:36 -070084pub struct Signature {
85 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -040086 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -070087 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -040088 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -070089 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -070090 pub paren_token: Paren,
91 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -040092}
93
David Tolnay417305a2020-03-18 13:54:00 -070094#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -040095pub struct Var {
96 pub ident: Ident,
97 pub ty: Type,
98}
99
100pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -0700101 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700102 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400103 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -0700104 pub var: Token![self],
105 pub ty: Ident,
David Tolnay62d360c2020-04-22 16:26:21 -0700106 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400107}
108
Joel Galensonc03402a2020-04-23 17:31:09 -0700109pub struct Variant {
110 pub ident: Ident,
David Tolnay17e137f2020-05-08 15:55:28 -0700111 pub discriminant: Discriminant,
Joel Galensonc03402a2020-04-23 17:31:09 -0700112}
113
David Tolnay7db73692019-10-20 14:51:12 -0400114pub enum Type {
115 Ident(Ident),
116 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700117 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400118 UniquePtr(Box<Ty1>),
119 Ref(Box<Ref>),
120 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700121 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700122 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700123 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700124 Slice(Box<Slice>),
125 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400126}
127
128pub struct Ty1 {
129 pub name: Ident,
130 pub langle: Token![<],
131 pub inner: Type,
132 pub rangle: Token![>],
133}
134
135pub struct Ref {
136 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700137 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400138 pub mutability: Option<Token![mut]>,
139 pub inner: Type,
140}
141
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700142pub struct Slice {
143 pub bracket: Bracket,
144 pub inner: Type,
145}
146
David Tolnay7db73692019-10-20 14:51:12 -0400147#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700148pub enum Lang {
149 Cxx,
150 Rust,
151}