blob: bb9566d5079c4525f7e00bf5ae75b7704cd3c7ef [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;
Adrian Taylorc8713432020-10-21 18:20:55 -070024use self::namespace::Namespace;
David Tolnaye3a48152020-04-08 19:38:05 -070025use self::parse::kw;
Adrian Taylorc8713432020-10-21 18:20:55 -070026use self::symbol::Symbol;
David Tolnaye3a48152020-04-08 19:38:05 -070027use proc_macro2::{Ident, Span};
28use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070029use syn::token::{Brace, Bracket, Paren};
David Tolnay91e87fa2020-05-11 19:10:23 -070030use syn::{Expr, Lifetime, Token, Type as RustType};
David Tolnay7db73692019-10-20 14:51:12 -040031
32pub use self::atom::Atom;
David Tolnay64703b42020-05-10 22:12:33 -070033pub use self::derive::Derive;
David Tolnay7db73692019-10-20 14:51:12 -040034pub use self::doc::Doc;
35pub use self::parse::parse_items;
36pub use self::types::Types;
37
Adrian Taylorc8713432020-10-21 18:20:55 -070038/// A Rust identifier will forver == a proc_macro2::Ident,
39/// but for completeness here's a type alias.
40pub type RsIdent = Ident;
41
42/// At the moment, a Rust name is simply a proc_macro2::Ident.
43/// In the future, it may become namespaced based on a mod path.
44pub type RsName = RsIdent;
45
46/// At the moment, a C++ identifier is also a proc_macro2::Ident.
47/// In the future, we may wish to make a newtype wrapper here
48/// to avoid confusion between C++ and Rust identifiers.
49pub type CppIdent = Ident;
50
51#[derive(Clone)]
52/// A C++ identifier in a particular namespace.
53/// It is intentional that this does not impl Display,
54/// because we want to force users actively to decide whether to output
55/// it as a qualified name or as an unqualfiied name.
56pub struct CppName {
57 pub ns: Namespace,
58 pub ident: CppIdent,
59}
60
David Tolnay7db73692019-10-20 14:51:12 -040061pub enum Api {
David Tolnay91e87fa2020-05-11 19:10:23 -070062 Include(String),
David Tolnay7db73692019-10-20 14:51:12 -040063 Struct(Struct),
Joel Galensonc03402a2020-04-23 17:31:09 -070064 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040065 CxxType(ExternType),
66 CxxFunction(ExternFn),
67 RustType(ExternType),
68 RustFunction(ExternFn),
David Tolnay99383812020-05-04 02:34:33 -070069 TypeAlias(TypeAlias),
David Tolnay7e69f892020-10-03 22:20:22 -070070 Impl(Impl),
David Tolnay7db73692019-10-20 14:51:12 -040071}
72
73pub struct ExternType {
74 pub doc: Doc,
75 pub type_token: Token![type],
Adrian Taylorc8713432020-10-21 18:20:55 -070076 pub ident: Pair,
David Tolnayc8361022020-08-25 21:57:53 -070077 pub semi_token: Token![;],
David Tolnay00f236a2020-08-29 19:07:18 -070078 pub trusted: bool,
David Tolnay7db73692019-10-20 14:51:12 -040079}
80
81pub struct Struct {
82 pub doc: Doc,
David Tolnaye86b9cf2020-05-10 14:24:29 -070083 pub derives: Vec<Derive>,
David Tolnay7db73692019-10-20 14:51:12 -040084 pub struct_token: Token![struct],
Adrian Taylorc8713432020-10-21 18:20:55 -070085 pub ident: Pair,
David Tolnay09462ac2020-03-20 14:58:41 -070086 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040087 pub fields: Vec<Var>,
88}
89
Joel Galensonc03402a2020-04-23 17:31:09 -070090pub struct Enum {
91 pub doc: Doc,
92 pub enum_token: Token![enum],
Adrian Taylorc8713432020-10-21 18:20:55 -070093 pub ident: Pair,
Joel Galensonc03402a2020-04-23 17:31:09 -070094 pub brace_token: Brace,
95 pub variants: Vec<Variant>,
David Tolnaye2e303f2020-05-10 20:52:00 -070096 pub repr: Atom,
Joel Galensonc03402a2020-04-23 17:31:09 -070097}
98
Adrian Taylorc8713432020-10-21 18:20:55 -070099/// A type with a defined Rust name and a fully resolved,
100/// qualified, namespaced, C++ name.
101#[derive(Clone)]
David Tolnaya4641c72020-09-08 14:05:53 -0700102pub struct Pair {
Adrian Taylorc8713432020-10-21 18:20:55 -0700103 pub cxx: CppName,
104 pub rust: RsName,
David Tolnaya4641c72020-09-08 14:05:53 -0700105}
106
David Tolnay7db73692019-10-20 14:51:12 -0400107pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -0700108 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -0400109 pub doc: Doc,
David Tolnaya4641c72020-09-08 14:05:53 -0700110 pub ident: Pair,
David Tolnay16448732020-03-18 12:39:36 -0700111 pub sig: Signature,
112 pub semi_token: Token![;],
113}
114
David Tolnay99383812020-05-04 02:34:33 -0700115pub struct TypeAlias {
Bryan Henry890083d2020-09-13 10:34:31 -0700116 pub doc: Doc,
David Tolnay99383812020-05-04 02:34:33 -0700117 pub type_token: Token![type],
Adrian Taylorc8713432020-10-21 18:20:55 -0700118 pub ident: Pair,
David Tolnay99383812020-05-04 02:34:33 -0700119 pub eq_token: Token![=],
120 pub ty: RustType,
121 pub semi_token: Token![;],
122}
123
David Tolnay7e69f892020-10-03 22:20:22 -0700124pub struct Impl {
125 pub impl_token: Token![impl],
126 pub ty: Type,
127 pub brace_token: Brace,
128}
129
David Tolnay16448732020-03-18 12:39:36 -0700130pub struct Signature {
David Tolnaybdb576c2020-09-06 23:45:55 -0700131 pub unsafety: Option<Token![unsafe]>,
David Tolnay16448732020-03-18 12:39:36 -0700132 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -0400133 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -0700134 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -0400135 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -0700136 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -0700137 pub paren_token: Paren,
138 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -0400139}
140
David Tolnay417305a2020-03-18 13:54:00 -0700141#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -0400142pub struct Var {
Adrian Taylorc8713432020-10-21 18:20:55 -0700143 pub ident: RsIdent, // fields and variables are not namespaced
David Tolnay7db73692019-10-20 14:51:12 -0400144 pub ty: Type,
145}
146
147pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -0700148 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700149 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400150 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -0700151 pub var: Token![self],
Adrian Taylorc8713432020-10-21 18:20:55 -0700152 pub ty: ResolvableName,
David Tolnay62d360c2020-04-22 16:26:21 -0700153 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400154}
155
Joel Galensonc03402a2020-04-23 17:31:09 -0700156pub struct Variant {
Adrian Taylorc8713432020-10-21 18:20:55 -0700157 pub ident: RsIdent,
David Tolnay17e137f2020-05-08 15:55:28 -0700158 pub discriminant: Discriminant,
David Tolnay2b8bf6d2020-05-10 17:37:16 -0700159 pub expr: Option<Expr>,
Joel Galensonc03402a2020-04-23 17:31:09 -0700160}
161
David Tolnay7db73692019-10-20 14:51:12 -0400162pub enum Type {
Adrian Taylorc8713432020-10-21 18:20:55 -0700163 Ident(ResolvableName),
David Tolnay7db73692019-10-20 14:51:12 -0400164 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700165 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400166 UniquePtr(Box<Ty1>),
167 Ref(Box<Ref>),
168 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700169 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700170 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700171 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700172 Slice(Box<Slice>),
173 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400174}
175
176pub struct Ty1 {
Adrian Taylorc8713432020-10-21 18:20:55 -0700177 pub name: ResolvableName,
David Tolnay7db73692019-10-20 14:51:12 -0400178 pub langle: Token![<],
179 pub inner: Type,
180 pub rangle: Token![>],
181}
182
183pub struct Ref {
184 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700185 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400186 pub mutability: Option<Token![mut]>,
187 pub inner: Type,
188}
189
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700190pub struct Slice {
191 pub bracket: Bracket,
192 pub inner: Type,
193}
194
David Tolnay7db73692019-10-20 14:51:12 -0400195#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700196pub enum Lang {
197 Cxx,
198 Rust,
199}
Adrian Taylorc8713432020-10-21 18:20:55 -0700200
201/// Wrapper for a type which needs to be resolved
202/// before it can be printed in C++.
203#[derive(Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
204pub struct ResolvableName {
205 pub rust: RsName,
206}