blob: d770d49882c21f0cd203fb8ccf863d5402ad1cca [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#[derive(Clone)]
39/// A C++ identifier in a particular namespace.
40/// It is intentional that this does not impl Display,
41/// because we want to force users actively to decide whether to output
42/// it as a qualified name or as an unqualfiied name.
43pub struct CppName {
44 pub ns: Namespace,
David Tolnay50de2c42020-10-30 21:16:03 -070045 pub ident: Ident,
Adrian Taylorc8713432020-10-21 18:20:55 -070046}
47
David Tolnay7db73692019-10-20 14:51:12 -040048pub enum Api {
David Tolnayb0cd3272020-10-27 21:32:54 -070049 Include(Include),
David Tolnay7db73692019-10-20 14:51:12 -040050 Struct(Struct),
Joel Galensonc03402a2020-04-23 17:31:09 -070051 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040052 CxxType(ExternType),
53 CxxFunction(ExternFn),
54 RustType(ExternType),
55 RustFunction(ExternFn),
David Tolnay99383812020-05-04 02:34:33 -070056 TypeAlias(TypeAlias),
David Tolnay7e69f892020-10-03 22:20:22 -070057 Impl(Impl),
David Tolnay7db73692019-10-20 14:51:12 -040058}
59
David Tolnayb0cd3272020-10-27 21:32:54 -070060pub struct Include {
61 pub path: String,
62 pub kind: IncludeKind,
63 pub begin_span: Span,
64 pub end_span: Span,
65}
66
David Tolnay700cd0c2020-10-28 12:40:27 -070067/// Whether to emit `#include "path"` or `#include <path>`.
68#[derive(Copy, Clone, PartialEq, Debug)]
David Tolnayb0cd3272020-10-27 21:32:54 -070069pub enum IncludeKind {
David Tolnay700cd0c2020-10-28 12:40:27 -070070 /// `#include "quoted/path/to"`
71 Quoted,
72 /// `#include <bracketed/path/to>`
73 Bracketed,
David Tolnayb0cd3272020-10-27 21:32:54 -070074}
75
David Tolnay7db73692019-10-20 14:51:12 -040076pub struct ExternType {
77 pub doc: Doc,
78 pub type_token: Token![type],
Adrian Taylorc8713432020-10-21 18:20:55 -070079 pub ident: Pair,
David Tolnayc8361022020-08-25 21:57:53 -070080 pub semi_token: Token![;],
David Tolnay00f236a2020-08-29 19:07:18 -070081 pub trusted: bool,
David Tolnay7db73692019-10-20 14:51:12 -040082}
83
84pub struct Struct {
85 pub doc: Doc,
David Tolnaye86b9cf2020-05-10 14:24:29 -070086 pub derives: Vec<Derive>,
David Tolnay7db73692019-10-20 14:51:12 -040087 pub struct_token: Token![struct],
Adrian Taylorc8713432020-10-21 18:20:55 -070088 pub ident: Pair,
David Tolnay09462ac2020-03-20 14:58:41 -070089 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040090 pub fields: Vec<Var>,
91}
92
Joel Galensonc03402a2020-04-23 17:31:09 -070093pub struct Enum {
94 pub doc: Doc,
95 pub enum_token: Token![enum],
Adrian Taylorc8713432020-10-21 18:20:55 -070096 pub ident: Pair,
Joel Galensonc03402a2020-04-23 17:31:09 -070097 pub brace_token: Brace,
98 pub variants: Vec<Variant>,
David Tolnaye2e303f2020-05-10 20:52:00 -070099 pub repr: Atom,
Joel Galensonc03402a2020-04-23 17:31:09 -0700100}
101
Adrian Taylorc8713432020-10-21 18:20:55 -0700102/// A type with a defined Rust name and a fully resolved,
103/// qualified, namespaced, C++ name.
104#[derive(Clone)]
David Tolnaya4641c72020-09-08 14:05:53 -0700105pub struct Pair {
Adrian Taylorc8713432020-10-21 18:20:55 -0700106 pub cxx: CppName,
David Tolnay50de2c42020-10-30 21:16:03 -0700107 pub rust: Ident,
David Tolnaya4641c72020-09-08 14:05:53 -0700108}
109
David Tolnay7db73692019-10-20 14:51:12 -0400110pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -0700111 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -0400112 pub doc: Doc,
David Tolnaya4641c72020-09-08 14:05:53 -0700113 pub ident: Pair,
David Tolnay16448732020-03-18 12:39:36 -0700114 pub sig: Signature,
115 pub semi_token: Token![;],
116}
117
David Tolnay99383812020-05-04 02:34:33 -0700118pub struct TypeAlias {
Bryan Henry890083d2020-09-13 10:34:31 -0700119 pub doc: Doc,
David Tolnay99383812020-05-04 02:34:33 -0700120 pub type_token: Token![type],
Adrian Taylorc8713432020-10-21 18:20:55 -0700121 pub ident: Pair,
David Tolnay99383812020-05-04 02:34:33 -0700122 pub eq_token: Token![=],
123 pub ty: RustType,
124 pub semi_token: Token![;],
125}
126
David Tolnay7e69f892020-10-03 22:20:22 -0700127pub struct Impl {
128 pub impl_token: Token![impl],
129 pub ty: Type,
130 pub brace_token: Brace,
131}
132
David Tolnay16448732020-03-18 12:39:36 -0700133pub struct Signature {
David Tolnaybdb576c2020-09-06 23:45:55 -0700134 pub unsafety: Option<Token![unsafe]>,
David Tolnay16448732020-03-18 12:39:36 -0700135 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -0400136 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -0700137 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -0400138 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -0700139 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -0700140 pub paren_token: Paren,
141 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -0400142}
143
David Tolnay417305a2020-03-18 13:54:00 -0700144#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -0400145pub struct Var {
David Tolnay50de2c42020-10-30 21:16:03 -0700146 pub ident: Ident,
David Tolnay7db73692019-10-20 14:51:12 -0400147 pub ty: Type,
148}
149
150pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -0700151 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]>,
David Tolnay05e11cc2020-04-20 02:13:56 -0700154 pub var: Token![self],
Adrian Taylorc8713432020-10-21 18:20:55 -0700155 pub ty: ResolvableName,
David Tolnay62d360c2020-04-22 16:26:21 -0700156 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400157}
158
Joel Galensonc03402a2020-04-23 17:31:09 -0700159pub struct Variant {
David Tolnay50de2c42020-10-30 21:16:03 -0700160 pub ident: Ident,
David Tolnay17e137f2020-05-08 15:55:28 -0700161 pub discriminant: Discriminant,
David Tolnay2b8bf6d2020-05-10 17:37:16 -0700162 pub expr: Option<Expr>,
Joel Galensonc03402a2020-04-23 17:31:09 -0700163}
164
David Tolnay7db73692019-10-20 14:51:12 -0400165pub enum Type {
Adrian Taylorc8713432020-10-21 18:20:55 -0700166 Ident(ResolvableName),
David Tolnay7db73692019-10-20 14:51:12 -0400167 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700168 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400169 UniquePtr(Box<Ty1>),
170 Ref(Box<Ref>),
171 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700172 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700173 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700174 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700175 Slice(Box<Slice>),
176 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400177}
178
179pub struct Ty1 {
Adrian Taylorc8713432020-10-21 18:20:55 -0700180 pub name: ResolvableName,
David Tolnay7db73692019-10-20 14:51:12 -0400181 pub langle: Token![<],
182 pub inner: Type,
183 pub rangle: Token![>],
184}
185
186pub struct Ref {
187 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700188 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400189 pub mutability: Option<Token![mut]>,
190 pub inner: Type,
191}
192
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700193pub struct Slice {
194 pub bracket: Bracket,
195 pub inner: Type,
196}
197
David Tolnay7db73692019-10-20 14:51:12 -0400198#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700199pub enum Lang {
200 Cxx,
201 Rust,
202}
Adrian Taylorc8713432020-10-21 18:20:55 -0700203
204/// Wrapper for a type which needs to be resolved
205/// before it can be printed in C++.
206#[derive(Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
207pub struct ResolvableName {
David Tolnay50de2c42020-10-30 21:16:03 -0700208 pub rust: Ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700209}