blob: 6498d747ccfca860e604d31506fe69245815a350 [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 Tolnayb0cd3272020-10-27 21:32:54 -070062 Include(Include),
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
David Tolnayb0cd3272020-10-27 21:32:54 -070073pub struct Include {
74 pub path: String,
75 pub kind: IncludeKind,
76 pub begin_span: Span,
77 pub end_span: Span,
78}
79
David Tolnay700cd0c2020-10-28 12:40:27 -070080/// Whether to emit `#include "path"` or `#include <path>`.
81#[derive(Copy, Clone, PartialEq, Debug)]
David Tolnayb0cd3272020-10-27 21:32:54 -070082pub enum IncludeKind {
David Tolnay700cd0c2020-10-28 12:40:27 -070083 /// `#include "quoted/path/to"`
84 Quoted,
85 /// `#include <bracketed/path/to>`
86 Bracketed,
David Tolnayb0cd3272020-10-27 21:32:54 -070087}
88
David Tolnay7db73692019-10-20 14:51:12 -040089pub struct ExternType {
90 pub doc: Doc,
91 pub type_token: Token![type],
Adrian Taylorc8713432020-10-21 18:20:55 -070092 pub ident: Pair,
David Tolnayc8361022020-08-25 21:57:53 -070093 pub semi_token: Token![;],
David Tolnay00f236a2020-08-29 19:07:18 -070094 pub trusted: bool,
David Tolnay7db73692019-10-20 14:51:12 -040095}
96
97pub struct Struct {
98 pub doc: Doc,
David Tolnaye86b9cf2020-05-10 14:24:29 -070099 pub derives: Vec<Derive>,
David Tolnay7db73692019-10-20 14:51:12 -0400100 pub struct_token: Token![struct],
Adrian Taylorc8713432020-10-21 18:20:55 -0700101 pub ident: Pair,
David Tolnay09462ac2020-03-20 14:58:41 -0700102 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -0400103 pub fields: Vec<Var>,
104}
105
Joel Galensonc03402a2020-04-23 17:31:09 -0700106pub struct Enum {
107 pub doc: Doc,
108 pub enum_token: Token![enum],
Adrian Taylorc8713432020-10-21 18:20:55 -0700109 pub ident: Pair,
Joel Galensonc03402a2020-04-23 17:31:09 -0700110 pub brace_token: Brace,
111 pub variants: Vec<Variant>,
David Tolnaye2e303f2020-05-10 20:52:00 -0700112 pub repr: Atom,
Joel Galensonc03402a2020-04-23 17:31:09 -0700113}
114
Adrian Taylorc8713432020-10-21 18:20:55 -0700115/// A type with a defined Rust name and a fully resolved,
116/// qualified, namespaced, C++ name.
117#[derive(Clone)]
David Tolnaya4641c72020-09-08 14:05:53 -0700118pub struct Pair {
Adrian Taylorc8713432020-10-21 18:20:55 -0700119 pub cxx: CppName,
120 pub rust: RsName,
David Tolnaya4641c72020-09-08 14:05:53 -0700121}
122
David Tolnay7db73692019-10-20 14:51:12 -0400123pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -0700124 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -0400125 pub doc: Doc,
David Tolnaya4641c72020-09-08 14:05:53 -0700126 pub ident: Pair,
David Tolnay16448732020-03-18 12:39:36 -0700127 pub sig: Signature,
128 pub semi_token: Token![;],
129}
130
David Tolnay99383812020-05-04 02:34:33 -0700131pub struct TypeAlias {
Bryan Henry890083d2020-09-13 10:34:31 -0700132 pub doc: Doc,
David Tolnay99383812020-05-04 02:34:33 -0700133 pub type_token: Token![type],
Adrian Taylorc8713432020-10-21 18:20:55 -0700134 pub ident: Pair,
David Tolnay99383812020-05-04 02:34:33 -0700135 pub eq_token: Token![=],
136 pub ty: RustType,
137 pub semi_token: Token![;],
138}
139
David Tolnay7e69f892020-10-03 22:20:22 -0700140pub struct Impl {
141 pub impl_token: Token![impl],
142 pub ty: Type,
143 pub brace_token: Brace,
144}
145
David Tolnay16448732020-03-18 12:39:36 -0700146pub struct Signature {
David Tolnaybdb576c2020-09-06 23:45:55 -0700147 pub unsafety: Option<Token![unsafe]>,
David Tolnay16448732020-03-18 12:39:36 -0700148 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -0400149 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -0700150 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -0400151 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -0700152 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -0700153 pub paren_token: Paren,
154 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -0400155}
156
David Tolnay417305a2020-03-18 13:54:00 -0700157#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -0400158pub struct Var {
Adrian Taylorc8713432020-10-21 18:20:55 -0700159 pub ident: RsIdent, // fields and variables are not namespaced
David Tolnay7db73692019-10-20 14:51:12 -0400160 pub ty: Type,
161}
162
163pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -0700164 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700165 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400166 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -0700167 pub var: Token![self],
Adrian Taylorc8713432020-10-21 18:20:55 -0700168 pub ty: ResolvableName,
David Tolnay62d360c2020-04-22 16:26:21 -0700169 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400170}
171
Joel Galensonc03402a2020-04-23 17:31:09 -0700172pub struct Variant {
Adrian Taylorc8713432020-10-21 18:20:55 -0700173 pub ident: RsIdent,
David Tolnay17e137f2020-05-08 15:55:28 -0700174 pub discriminant: Discriminant,
David Tolnay2b8bf6d2020-05-10 17:37:16 -0700175 pub expr: Option<Expr>,
Joel Galensonc03402a2020-04-23 17:31:09 -0700176}
177
David Tolnay7db73692019-10-20 14:51:12 -0400178pub enum Type {
Adrian Taylorc8713432020-10-21 18:20:55 -0700179 Ident(ResolvableName),
David Tolnay7db73692019-10-20 14:51:12 -0400180 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700181 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400182 UniquePtr(Box<Ty1>),
183 Ref(Box<Ref>),
184 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700185 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700186 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700187 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700188 Slice(Box<Slice>),
189 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400190}
191
192pub struct Ty1 {
Adrian Taylorc8713432020-10-21 18:20:55 -0700193 pub name: ResolvableName,
David Tolnay7db73692019-10-20 14:51:12 -0400194 pub langle: Token![<],
195 pub inner: Type,
196 pub rangle: Token![>],
197}
198
199pub struct Ref {
200 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700201 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400202 pub mutability: Option<Token![mut]>,
203 pub inner: Type,
204}
205
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700206pub struct Slice {
207 pub bracket: Bracket,
208 pub inner: Type,
209}
210
David Tolnay7db73692019-10-20 14:51:12 -0400211#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700212pub enum Lang {
213 Cxx,
214 Rust,
215}
Adrian Taylorc8713432020-10-21 18:20:55 -0700216
217/// Wrapper for a type which needs to be resolved
218/// before it can be printed in C++.
219#[derive(Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
220pub struct ResolvableName {
221 pub rust: RsName,
222}