blob: b1aaa13dbd55b47ef05e80f85bfe569c566067ec [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
38pub enum Api {
David Tolnayb0cd3272020-10-27 21:32:54 -070039 Include(Include),
David Tolnay7db73692019-10-20 14:51:12 -040040 Struct(Struct),
Joel Galensonc03402a2020-04-23 17:31:09 -070041 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040042 CxxType(ExternType),
43 CxxFunction(ExternFn),
44 RustType(ExternType),
45 RustFunction(ExternFn),
David Tolnay99383812020-05-04 02:34:33 -070046 TypeAlias(TypeAlias),
David Tolnay7e69f892020-10-03 22:20:22 -070047 Impl(Impl),
David Tolnay7db73692019-10-20 14:51:12 -040048}
49
David Tolnayb0cd3272020-10-27 21:32:54 -070050pub struct Include {
51 pub path: String,
52 pub kind: IncludeKind,
53 pub begin_span: Span,
54 pub end_span: Span,
55}
56
David Tolnay700cd0c2020-10-28 12:40:27 -070057/// Whether to emit `#include "path"` or `#include <path>`.
58#[derive(Copy, Clone, PartialEq, Debug)]
David Tolnayb0cd3272020-10-27 21:32:54 -070059pub enum IncludeKind {
David Tolnay700cd0c2020-10-28 12:40:27 -070060 /// `#include "quoted/path/to"`
61 Quoted,
62 /// `#include <bracketed/path/to>`
63 Bracketed,
David Tolnayb0cd3272020-10-27 21:32:54 -070064}
65
David Tolnay7db73692019-10-20 14:51:12 -040066pub struct ExternType {
67 pub doc: Doc,
68 pub type_token: Token![type],
Adrian Taylorc8713432020-10-21 18:20:55 -070069 pub ident: Pair,
David Tolnayc8361022020-08-25 21:57:53 -070070 pub semi_token: Token![;],
David Tolnay00f236a2020-08-29 19:07:18 -070071 pub trusted: bool,
David Tolnay7db73692019-10-20 14:51:12 -040072}
73
74pub struct Struct {
75 pub doc: Doc,
David Tolnaye86b9cf2020-05-10 14:24:29 -070076 pub derives: Vec<Derive>,
David Tolnay7db73692019-10-20 14:51:12 -040077 pub struct_token: Token![struct],
Adrian Taylorc8713432020-10-21 18:20:55 -070078 pub ident: Pair,
David Tolnay09462ac2020-03-20 14:58:41 -070079 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040080 pub fields: Vec<Var>,
81}
82
Joel Galensonc03402a2020-04-23 17:31:09 -070083pub struct Enum {
84 pub doc: Doc,
85 pub enum_token: Token![enum],
Adrian Taylorc8713432020-10-21 18:20:55 -070086 pub ident: Pair,
Joel Galensonc03402a2020-04-23 17:31:09 -070087 pub brace_token: Brace,
88 pub variants: Vec<Variant>,
David Tolnaye2e303f2020-05-10 20:52:00 -070089 pub repr: Atom,
Joel Galensonc03402a2020-04-23 17:31:09 -070090}
91
David Tolnay7db73692019-10-20 14:51:12 -040092pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070093 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040094 pub doc: Doc,
David Tolnaya4641c72020-09-08 14:05:53 -070095 pub ident: Pair,
David Tolnay16448732020-03-18 12:39:36 -070096 pub sig: Signature,
97 pub semi_token: Token![;],
98}
99
David Tolnay99383812020-05-04 02:34:33 -0700100pub struct TypeAlias {
Bryan Henry890083d2020-09-13 10:34:31 -0700101 pub doc: Doc,
David Tolnay99383812020-05-04 02:34:33 -0700102 pub type_token: Token![type],
Adrian Taylorc8713432020-10-21 18:20:55 -0700103 pub ident: Pair,
David Tolnay99383812020-05-04 02:34:33 -0700104 pub eq_token: Token![=],
105 pub ty: RustType,
106 pub semi_token: Token![;],
107}
108
David Tolnay7e69f892020-10-03 22:20:22 -0700109pub struct Impl {
110 pub impl_token: Token![impl],
111 pub ty: Type,
112 pub brace_token: Brace,
113}
114
David Tolnay16448732020-03-18 12:39:36 -0700115pub struct Signature {
David Tolnaybdb576c2020-09-06 23:45:55 -0700116 pub unsafety: Option<Token![unsafe]>,
David Tolnay16448732020-03-18 12:39:36 -0700117 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -0400118 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -0700119 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -0400120 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -0700121 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -0700122 pub paren_token: Paren,
123 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -0400124}
125
David Tolnay417305a2020-03-18 13:54:00 -0700126#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -0400127pub struct Var {
David Tolnay50de2c42020-10-30 21:16:03 -0700128 pub ident: Ident,
David Tolnay7db73692019-10-20 14:51:12 -0400129 pub ty: Type,
130}
131
132pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -0700133 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700134 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400135 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -0700136 pub var: Token![self],
Adrian Taylorc8713432020-10-21 18:20:55 -0700137 pub ty: ResolvableName,
David Tolnay62d360c2020-04-22 16:26:21 -0700138 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400139}
140
Joel Galensonc03402a2020-04-23 17:31:09 -0700141pub struct Variant {
David Tolnay50de2c42020-10-30 21:16:03 -0700142 pub ident: Ident,
David Tolnay17e137f2020-05-08 15:55:28 -0700143 pub discriminant: Discriminant,
David Tolnay2b8bf6d2020-05-10 17:37:16 -0700144 pub expr: Option<Expr>,
Joel Galensonc03402a2020-04-23 17:31:09 -0700145}
146
David Tolnay7db73692019-10-20 14:51:12 -0400147pub enum Type {
Adrian Taylorc8713432020-10-21 18:20:55 -0700148 Ident(ResolvableName),
David Tolnay7db73692019-10-20 14:51:12 -0400149 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700150 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400151 UniquePtr(Box<Ty1>),
152 Ref(Box<Ref>),
153 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700154 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700155 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700156 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700157 Slice(Box<Slice>),
158 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400159}
160
161pub struct Ty1 {
Adrian Taylorc8713432020-10-21 18:20:55 -0700162 pub name: ResolvableName,
David Tolnay7db73692019-10-20 14:51:12 -0400163 pub langle: Token![<],
164 pub inner: Type,
165 pub rangle: Token![>],
166}
167
168pub struct Ref {
169 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700170 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400171 pub mutability: Option<Token![mut]>,
172 pub inner: Type,
173}
174
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700175pub struct Slice {
176 pub bracket: Bracket,
177 pub inner: Type,
178}
179
David Tolnay7db73692019-10-20 14:51:12 -0400180#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700181pub enum Lang {
182 Cxx,
183 Rust,
184}
Adrian Taylorc8713432020-10-21 18:20:55 -0700185
David Tolnaybe7e30e2020-10-30 21:21:23 -0700186// An association of a defined Rust name with a fully resolved, namespace
187// qualified C++ name.
David Tolnay9071c262020-10-30 21:18:54 -0700188#[derive(Clone)]
189pub struct Pair {
190 pub cxx: CppName,
191 pub rust: Ident,
192}
193
David Tolnayabff2d52020-10-30 21:20:07 -0700194// A C++ identifier in a particular namespace. It is intentional that this does
195// not impl Display, because we want to force users actively to decide whether
David Tolnaybe7e30e2020-10-30 21:21:23 -0700196// to output it as a qualified name or as an unqualfied name.
David Tolnay9071c262020-10-30 21:18:54 -0700197#[derive(Clone)]
David Tolnay9071c262020-10-30 21:18:54 -0700198pub struct CppName {
199 pub ns: Namespace,
200 pub ident: Ident,
201}
202
David Tolnayabff2d52020-10-30 21:20:07 -0700203// Wrapper for a type which needs to be resolved before it can be printed in
204// C++.
Adrian Taylorc8713432020-10-21 18:20:55 -0700205#[derive(Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
206pub struct ResolvableName {
David Tolnay50de2c42020-10-30 21:16:03 -0700207 pub rust: Ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700208}