blob: 934f6c67aef73d3df1796eca91cb0b58bb32153d [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 Tolnaya8d94a12020-09-06 23:28:18 -070016pub mod qualified;
David Tolnaydf344a82020-05-03 23:23:18 -070017pub mod report;
David Tolnay7db73692019-10-20 14:51:12 -040018pub mod set;
David Tolnay891061b2020-04-19 22:42:33 -070019pub mod symbol;
David Tolnay7db73692019-10-20 14:51:12 -040020mod tokens;
21pub mod types;
22
David Tolnay17e137f2020-05-08 15:55:28 -070023use self::discriminant::Discriminant;
David Tolnaye3a48152020-04-08 19:38:05 -070024use self::parse::kw;
25use proc_macro2::{Ident, Span};
26use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070027use syn::token::{Brace, Bracket, Paren};
David Tolnay91e87fa2020-05-11 19:10:23 -070028use syn::{Expr, Lifetime, Token, Type as RustType};
David Tolnay7db73692019-10-20 14:51:12 -040029
30pub use self::atom::Atom;
David Tolnay64703b42020-05-10 22:12:33 -070031pub use self::derive::Derive;
David Tolnay7db73692019-10-20 14:51:12 -040032pub use self::doc::Doc;
33pub use self::parse::parse_items;
34pub use self::types::Types;
35
36pub enum Api {
David Tolnay91e87fa2020-05-11 19:10:23 -070037 Include(String),
David Tolnay7db73692019-10-20 14:51:12 -040038 Struct(Struct),
Joel Galensonc03402a2020-04-23 17:31:09 -070039 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040040 CxxType(ExternType),
41 CxxFunction(ExternFn),
42 RustType(ExternType),
43 RustFunction(ExternFn),
David Tolnay99383812020-05-04 02:34:33 -070044 TypeAlias(TypeAlias),
David Tolnay7e69f892020-10-03 22:20:22 -070045 Impl(Impl),
David Tolnay7db73692019-10-20 14:51:12 -040046}
47
48pub struct ExternType {
49 pub doc: Doc,
50 pub type_token: Token![type],
51 pub ident: Ident,
David Tolnayc8361022020-08-25 21:57:53 -070052 pub semi_token: Token![;],
David Tolnay00f236a2020-08-29 19:07:18 -070053 pub trusted: bool,
David Tolnay7db73692019-10-20 14:51:12 -040054}
55
56pub struct Struct {
57 pub doc: Doc,
David Tolnaye86b9cf2020-05-10 14:24:29 -070058 pub derives: Vec<Derive>,
David Tolnay7db73692019-10-20 14:51:12 -040059 pub struct_token: Token![struct],
60 pub ident: Ident,
David Tolnay09462ac2020-03-20 14:58:41 -070061 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040062 pub fields: Vec<Var>,
63}
64
Joel Galensonc03402a2020-04-23 17:31:09 -070065pub struct Enum {
66 pub doc: Doc,
67 pub enum_token: Token![enum],
68 pub ident: Ident,
69 pub brace_token: Brace,
70 pub variants: Vec<Variant>,
David Tolnaye2e303f2020-05-10 20:52:00 -070071 pub repr: Atom,
Joel Galensonc03402a2020-04-23 17:31:09 -070072}
73
David Tolnay7db73692019-10-20 14:51:12 -040074pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070075 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040076 pub doc: Doc,
David Tolnay7db73692019-10-20 14:51:12 -040077 pub ident: Ident,
David Tolnay16448732020-03-18 12:39:36 -070078 pub sig: Signature,
79 pub semi_token: Token![;],
80}
81
David Tolnay99383812020-05-04 02:34:33 -070082pub struct TypeAlias {
Bryan Henry890083d2020-09-13 10:34:31 -070083 pub doc: Doc,
David Tolnay99383812020-05-04 02:34:33 -070084 pub type_token: Token![type],
85 pub ident: Ident,
86 pub eq_token: Token![=],
87 pub ty: RustType,
88 pub semi_token: Token![;],
89}
90
David Tolnay7e69f892020-10-03 22:20:22 -070091pub struct Impl {
92 pub impl_token: Token![impl],
93 pub ty: Type,
94 pub brace_token: Brace,
95}
96
David Tolnay16448732020-03-18 12:39:36 -070097pub struct Signature {
David Tolnaybdb576c2020-09-06 23:45:55 -070098 pub unsafety: Option<Token![unsafe]>,
David Tolnay16448732020-03-18 12:39:36 -070099 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -0400100 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -0700101 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -0400102 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -0700103 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -0700104 pub paren_token: Paren,
105 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -0400106}
107
David Tolnay417305a2020-03-18 13:54:00 -0700108#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -0400109pub struct Var {
110 pub ident: Ident,
111 pub ty: Type,
112}
113
114pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -0700115 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700116 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400117 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -0700118 pub var: Token![self],
119 pub ty: Ident,
David Tolnay62d360c2020-04-22 16:26:21 -0700120 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400121}
122
Joel Galensonc03402a2020-04-23 17:31:09 -0700123pub struct Variant {
124 pub ident: Ident,
David Tolnay17e137f2020-05-08 15:55:28 -0700125 pub discriminant: Discriminant,
David Tolnay2b8bf6d2020-05-10 17:37:16 -0700126 pub expr: Option<Expr>,
Joel Galensonc03402a2020-04-23 17:31:09 -0700127}
128
David Tolnay7db73692019-10-20 14:51:12 -0400129pub enum Type {
130 Ident(Ident),
131 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700132 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400133 UniquePtr(Box<Ty1>),
134 Ref(Box<Ref>),
135 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700136 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700137 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700138 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700139 Slice(Box<Slice>),
140 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400141}
142
143pub struct Ty1 {
144 pub name: Ident,
145 pub langle: Token![<],
146 pub inner: Type,
147 pub rangle: Token![>],
148}
149
150pub struct Ref {
151 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700152 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400153 pub mutability: Option<Token![mut]>,
154 pub inner: Type,
155}
156
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700157pub struct Slice {
158 pub bracket: Bracket,
159 pub inner: Type,
160}
161
David Tolnay7db73692019-10-20 14:51:12 -0400162#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700163pub enum Lang {
164 Cxx,
165 Rust,
166}