blob: ae37802dec02174b07a4c49b7df437faec5b3bef [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;
David Tolnay05ef6ff2020-08-29 11:27:05 -070010pub mod file;
David Tolnay7db73692019-10-20 14:51:12 -040011pub mod ident;
12mod impls;
David Tolnay3caa50a2020-04-19 21:25:34 -070013pub mod mangle;
David Tolnay08419302020-04-19 20:38:20 -070014pub mod namespace;
David Tolnay7db73692019-10-20 14:51:12 -040015mod parse;
David Tolnaydf344a82020-05-03 23:23:18 -070016pub mod report;
David Tolnay7db73692019-10-20 14:51:12 -040017pub mod set;
David Tolnay891061b2020-04-19 22:42:33 -070018pub mod symbol;
David Tolnay7db73692019-10-20 14:51:12 -040019mod tokens;
20pub mod types;
21
David Tolnay17e137f2020-05-08 15:55:28 -070022use self::discriminant::Discriminant;
David Tolnaye3a48152020-04-08 19:38:05 -070023use self::parse::kw;
24use proc_macro2::{Ident, Span};
25use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070026use syn::token::{Brace, Bracket, Paren};
David Tolnay91e87fa2020-05-11 19:10:23 -070027use syn::{Expr, Lifetime, Token, Type as RustType};
David Tolnay7db73692019-10-20 14:51:12 -040028
29pub use self::atom::Atom;
David Tolnay64703b42020-05-10 22:12:33 -070030pub use self::derive::Derive;
David Tolnay7db73692019-10-20 14:51:12 -040031pub use self::doc::Doc;
32pub use self::parse::parse_items;
33pub use self::types::Types;
34
35pub enum Api {
David Tolnay91e87fa2020-05-11 19:10:23 -070036 Include(String),
David Tolnay7db73692019-10-20 14:51:12 -040037 Struct(Struct),
Joel Galensonc03402a2020-04-23 17:31:09 -070038 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040039 CxxType(ExternType),
40 CxxFunction(ExternFn),
41 RustType(ExternType),
42 RustFunction(ExternFn),
David Tolnay99383812020-05-04 02:34:33 -070043 TypeAlias(TypeAlias),
David Tolnay7db73692019-10-20 14:51:12 -040044}
45
46pub struct ExternType {
47 pub doc: Doc,
48 pub type_token: Token![type],
49 pub ident: Ident,
David Tolnayc8361022020-08-25 21:57:53 -070050 pub semi_token: Token![;],
David Tolnay7db73692019-10-20 14:51:12 -040051}
52
53pub struct Struct {
54 pub doc: Doc,
David Tolnaye86b9cf2020-05-10 14:24:29 -070055 pub derives: Vec<Derive>,
David Tolnay7db73692019-10-20 14:51:12 -040056 pub struct_token: Token![struct],
57 pub ident: Ident,
David Tolnay09462ac2020-03-20 14:58:41 -070058 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040059 pub fields: Vec<Var>,
60}
61
Joel Galensonc03402a2020-04-23 17:31:09 -070062pub struct Enum {
63 pub doc: Doc,
64 pub enum_token: Token![enum],
65 pub ident: Ident,
66 pub brace_token: Brace,
67 pub variants: Vec<Variant>,
David Tolnaye2e303f2020-05-10 20:52:00 -070068 pub repr: Atom,
Joel Galensonc03402a2020-04-23 17:31:09 -070069}
70
David Tolnay7db73692019-10-20 14:51:12 -040071pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070072 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040073 pub doc: Doc,
David Tolnay7db73692019-10-20 14:51:12 -040074 pub ident: Ident,
David Tolnay16448732020-03-18 12:39:36 -070075 pub sig: Signature,
76 pub semi_token: Token![;],
77}
78
David Tolnay99383812020-05-04 02:34:33 -070079pub struct TypeAlias {
80 pub type_token: Token![type],
81 pub ident: Ident,
82 pub eq_token: Token![=],
83 pub ty: RustType,
84 pub semi_token: Token![;],
85}
86
David Tolnay16448732020-03-18 12:39:36 -070087pub struct Signature {
88 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -040089 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -070090 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -040091 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -070092 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -070093 pub paren_token: Paren,
94 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -040095}
96
David Tolnay417305a2020-03-18 13:54:00 -070097#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -040098pub struct Var {
99 pub ident: Ident,
100 pub ty: Type,
101}
102
103pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -0700104 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700105 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400106 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -0700107 pub var: Token![self],
108 pub ty: Ident,
David Tolnay62d360c2020-04-22 16:26:21 -0700109 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400110}
111
Joel Galensonc03402a2020-04-23 17:31:09 -0700112pub struct Variant {
113 pub ident: Ident,
David Tolnay17e137f2020-05-08 15:55:28 -0700114 pub discriminant: Discriminant,
David Tolnay2b8bf6d2020-05-10 17:37:16 -0700115 pub expr: Option<Expr>,
Joel Galensonc03402a2020-04-23 17:31:09 -0700116}
117
David Tolnay7db73692019-10-20 14:51:12 -0400118pub enum Type {
119 Ident(Ident),
120 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700121 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400122 UniquePtr(Box<Ty1>),
123 Ref(Box<Ref>),
124 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700125 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700126 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700127 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700128 Slice(Box<Slice>),
129 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400130}
131
132pub struct Ty1 {
133 pub name: Ident,
134 pub langle: Token![<],
135 pub inner: Type,
136 pub rangle: Token![>],
137}
138
139pub struct Ref {
140 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700141 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400142 pub mutability: Option<Token![mut]>,
143 pub inner: Type,
144}
145
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700146pub struct Slice {
147 pub bracket: Bracket,
148 pub inner: Type,
149}
150
David Tolnay7db73692019-10-20 14:51:12 -0400151#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700152pub enum Lang {
153 Cxx,
154 Rust,
155}