blob: 31c88c9f1563f159225da0ef964c5310aca56854 [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 Tolnaybc047bb2020-11-27 14:30:12 -08006pub mod 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 Tolnay7953a112020-11-02 12:57:57 -080013mod improper;
David Tolnay3caa50a2020-04-19 21:25:34 -070014pub mod mangle;
David Tolnay60e7aa62020-11-01 12:34:51 -080015mod names;
David Tolnay08419302020-04-19 20:38:20 -070016pub mod namespace;
David Tolnay7db73692019-10-20 14:51:12 -040017mod parse;
David Tolnay0beba912020-12-09 23:42:07 -080018mod pod;
David Tolnaya8d94a12020-09-06 23:28:18 -070019pub mod qualified;
David Tolnaydf344a82020-05-03 23:23:18 -070020pub mod report;
David Tolnay7db73692019-10-20 14:51:12 -040021pub mod set;
David Tolnay891061b2020-04-19 22:42:33 -070022pub mod symbol;
David Tolnay7db73692019-10-20 14:51:12 -040023mod tokens;
David Tolnay942cffd2020-11-03 18:27:10 -080024mod toposort;
David Tolnay7db73692019-10-20 14:51:12 -040025pub mod types;
26
David Tolnay17e137f2020-05-08 15:55:28 -070027use self::discriminant::Discriminant;
Adrian Taylorc8713432020-10-21 18:20:55 -070028use self::namespace::Namespace;
David Tolnaye3a48152020-04-08 19:38:05 -070029use self::parse::kw;
Adrian Taylorc8713432020-10-21 18:20:55 -070030use self::symbol::Symbol;
David Tolnaye3a48152020-04-08 19:38:05 -070031use proc_macro2::{Ident, Span};
32use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070033use syn::token::{Brace, Bracket, Paren};
David Tolnayc351dc42020-11-24 20:45:13 -080034use syn::{Expr, Generics, Lifetime, LitInt, Token, Type as RustType};
David Tolnay7db73692019-10-20 14:51:12 -040035
36pub use self::atom::Atom;
David Tolnayb247df12020-11-27 12:06:12 -080037pub use self::derive::{Derive, Trait};
David Tolnay7db73692019-10-20 14:51:12 -040038pub use self::doc::Doc;
39pub use self::parse::parse_items;
40pub use self::types::Types;
41
42pub enum Api {
David Tolnayb0cd3272020-10-27 21:32:54 -070043 Include(Include),
David Tolnay7db73692019-10-20 14:51:12 -040044 Struct(Struct),
Joel Galensonc03402a2020-04-23 17:31:09 -070045 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040046 CxxType(ExternType),
47 CxxFunction(ExternFn),
48 RustType(ExternType),
49 RustFunction(ExternFn),
David Tolnay99383812020-05-04 02:34:33 -070050 TypeAlias(TypeAlias),
David Tolnay7e69f892020-10-03 22:20:22 -070051 Impl(Impl),
David Tolnay7db73692019-10-20 14:51:12 -040052}
53
David Tolnayb0cd3272020-10-27 21:32:54 -070054pub struct Include {
55 pub path: String,
56 pub kind: IncludeKind,
57 pub begin_span: Span,
58 pub end_span: Span,
59}
60
David Tolnay700cd0c2020-10-28 12:40:27 -070061/// Whether to emit `#include "path"` or `#include <path>`.
62#[derive(Copy, Clone, PartialEq, Debug)]
David Tolnayb0cd3272020-10-27 21:32:54 -070063pub enum IncludeKind {
David Tolnay700cd0c2020-10-28 12:40:27 -070064 /// `#include "quoted/path/to"`
65 Quoted,
66 /// `#include <bracketed/path/to>`
67 Bracketed,
David Tolnayb0cd3272020-10-27 21:32:54 -070068}
69
David Tolnay7db73692019-10-20 14:51:12 -040070pub struct ExternType {
David Tolnay16e26202020-11-27 19:28:37 -080071 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040072 pub doc: Doc,
David Tolnay16e26202020-11-27 19:28:37 -080073 pub derives: Vec<Derive>,
David Tolnay7db73692019-10-20 14:51:12 -040074 pub type_token: Token![type],
David Tolnay17a934c2020-11-02 00:40:04 -080075 pub name: Pair,
David Tolnayb7eb65e2020-11-27 20:00:53 -080076 pub colon_token: Option<Token![:]>,
77 pub bounds: Vec<Derive>,
David Tolnayc8361022020-08-25 21:57:53 -070078 pub semi_token: Token![;],
David Tolnay00f236a2020-08-29 19:07:18 -070079 pub trusted: bool,
David Tolnay7db73692019-10-20 14:51:12 -040080}
81
82pub struct Struct {
83 pub doc: Doc,
David Tolnaye86b9cf2020-05-10 14:24:29 -070084 pub derives: Vec<Derive>,
David Tolnay7db73692019-10-20 14:51:12 -040085 pub struct_token: Token![struct],
David Tolnay17a934c2020-11-02 00:40:04 -080086 pub name: Pair,
David Tolnay09462ac2020-03-20 14:58:41 -070087 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040088 pub fields: Vec<Var>,
89}
90
Joel Galensonc03402a2020-04-23 17:31:09 -070091pub struct Enum {
92 pub doc: Doc,
David Tolnayfbc46692020-11-27 11:33:29 -080093 pub derives: Vec<Derive>,
Joel Galensonc03402a2020-04-23 17:31:09 -070094 pub enum_token: Token![enum],
David Tolnay17a934c2020-11-02 00:40:04 -080095 pub name: Pair,
Joel Galensonc03402a2020-04-23 17:31:09 -070096 pub brace_token: Brace,
97 pub variants: Vec<Variant>,
David Tolnaye2e303f2020-05-10 20:52:00 -070098 pub repr: Atom,
David Tolnay9a69d8a2020-11-16 09:16:41 -080099 pub repr_type: Type,
David Tolnay58eee392020-11-20 20:37:58 -0800100 pub explicit_repr: bool,
Joel Galensonc03402a2020-04-23 17:31:09 -0700101}
102
David Tolnay7db73692019-10-20 14:51:12 -0400103pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -0700104 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -0400105 pub doc: Doc,
David Tolnay17a934c2020-11-02 00:40:04 -0800106 pub name: Pair,
David Tolnay16448732020-03-18 12:39:36 -0700107 pub sig: Signature,
108 pub semi_token: Token![;],
David Tolnay556738d2020-11-15 13:58:44 -0800109 pub trusted: bool,
David Tolnay16448732020-03-18 12:39:36 -0700110}
111
David Tolnay99383812020-05-04 02:34:33 -0700112pub struct TypeAlias {
Bryan Henry890083d2020-09-13 10:34:31 -0700113 pub doc: Doc,
David Tolnayb7eb65e2020-11-27 20:00:53 -0800114 pub derives: Vec<Derive>,
David Tolnay99383812020-05-04 02:34:33 -0700115 pub type_token: Token![type],
David Tolnay17a934c2020-11-02 00:40:04 -0800116 pub name: Pair,
David Tolnay99383812020-05-04 02:34:33 -0700117 pub eq_token: Token![=],
118 pub ty: RustType,
119 pub semi_token: Token![;],
120}
121
David Tolnay7e69f892020-10-03 22:20:22 -0700122pub struct Impl {
123 pub impl_token: Token![impl],
David Tolnay028d3d22020-11-29 18:09:52 -0800124 pub negative: bool,
David Tolnay7e69f892020-10-03 22:20:22 -0700125 pub ty: Type,
126 pub brace_token: Brace,
David Tolnay028d3d22020-11-29 18:09:52 -0800127 pub negative_token: Option<Token![!]>,
David Tolnay7e69f892020-10-03 22:20:22 -0700128}
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 Tolnay9938b642020-11-15 18:11:40 -0800133 pub generics: Generics,
David Tolnay7db73692019-10-20 14:51:12 -0400134 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -0700135 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -0400136 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -0700137 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -0700138 pub paren_token: Paren,
139 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -0400140}
141
David Tolnay417305a2020-03-18 13:54:00 -0700142#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -0400143pub struct Var {
David Tolnay50de2c42020-10-30 21:16:03 -0700144 pub ident: Ident,
David Tolnay7db73692019-10-20 14:51:12 -0400145 pub ty: Type,
146}
147
148pub struct Receiver {
David Tolnay9c76c392020-11-15 16:07:08 -0800149 pub pinned: bool,
David Tolnayfb6e3862020-04-20 01:33:23 -0700150 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700151 pub lifetime: Option<Lifetime>,
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800152 pub mutable: bool,
David Tolnay05e11cc2020-04-20 02:13:56 -0700153 pub var: Token![self],
David Tolnay75ea17c2020-12-06 21:08:34 -0800154 pub ty: RustName,
David Tolnay62d360c2020-04-22 16:26:21 -0700155 pub shorthand: bool,
David Tolnay9c76c392020-11-15 16:07:08 -0800156 pub pin_tokens: Option<(kw::Pin, Token![<], Token![>])>,
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800157 pub mutability: Option<Token![mut]>,
David Tolnay7db73692019-10-20 14:51:12 -0400158}
159
Joel Galensonc03402a2020-04-23 17:31:09 -0700160pub struct Variant {
David Tolnay50de2c42020-10-30 21:16:03 -0700161 pub ident: Ident,
David Tolnay17e137f2020-05-08 15:55:28 -0700162 pub discriminant: Discriminant,
David Tolnay2b8bf6d2020-05-10 17:37:16 -0700163 pub expr: Option<Expr>,
Joel Galensonc03402a2020-04-23 17:31:09 -0700164}
165
David Tolnay7db73692019-10-20 14:51:12 -0400166pub enum Type {
David Tolnay75ea17c2020-12-06 21:08:34 -0800167 Ident(RustName),
David Tolnay7db73692019-10-20 14:51:12 -0400168 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700169 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400170 UniquePtr(Box<Ty1>),
David Tolnayb3b24a12020-12-01 15:27:43 -0800171 SharedPtr(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400172 Ref(Box<Ref>),
173 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700174 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700175 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700176 Void(Span),
David Tolnay73b72642020-11-25 17:44:05 -0800177 SliceRef(Box<SliceRef>),
Xiangpeng Hao78762352020-11-12 10:24:18 +0800178 Array(Box<Array>),
David Tolnay7db73692019-10-20 14:51:12 -0400179}
180
181pub struct Ty1 {
David Tolnayda71ef62020-11-02 00:36:00 -0800182 pub name: Ident,
David Tolnay7db73692019-10-20 14:51:12 -0400183 pub langle: Token![<],
184 pub inner: Type,
185 pub rangle: Token![>],
186}
187
188pub struct Ref {
David Tolnayb27f7872020-11-15 15:07:04 -0800189 pub pinned: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400190 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700191 pub lifetime: Option<Lifetime>,
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800192 pub mutable: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400193 pub inner: Type,
David Tolnayb27f7872020-11-15 15:07:04 -0800194 pub pin_tokens: Option<(kw::Pin, Token![<], Token![>])>,
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800195 pub mutability: Option<Token![mut]>,
David Tolnay7db73692019-10-20 14:51:12 -0400196}
197
David Tolnaye0dca7b2020-11-25 17:18:57 -0800198pub struct SliceRef {
199 pub ampersand: Token![&],
200 pub lifetime: Option<Lifetime>,
201 pub mutable: bool,
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700202 pub bracket: Bracket,
203 pub inner: Type,
David Tolnaye0dca7b2020-11-25 17:18:57 -0800204 pub mutability: Option<Token![mut]>,
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700205}
206
Xiangpeng Hao78762352020-11-12 10:24:18 +0800207pub struct Array {
208 pub bracket: Bracket,
209 pub inner: Type,
210 pub semi_token: Token![;],
211 pub len: usize,
David Tolnayc351dc42020-11-24 20:45:13 -0800212 pub len_token: LitInt,
Xiangpeng Hao78762352020-11-12 10:24:18 +0800213}
214
David Tolnay7db73692019-10-20 14:51:12 -0400215#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700216pub enum Lang {
217 Cxx,
218 Rust,
219}
Adrian Taylorc8713432020-10-21 18:20:55 -0700220
David Tolnaybe7e30e2020-10-30 21:21:23 -0700221// An association of a defined Rust name with a fully resolved, namespace
222// qualified C++ name.
David Tolnay9071c262020-10-30 21:18:54 -0700223#[derive(Clone)]
224pub struct Pair {
David Tolnayd7a3a182020-11-01 20:45:14 -0800225 pub namespace: Namespace,
David Tolnay8faec772020-11-02 00:18:19 -0800226 pub cxx: Ident,
227 pub rust: Ident,
David Tolnay9071c262020-10-30 21:18:54 -0700228}
229
David Tolnayabff2d52020-10-30 21:20:07 -0700230// Wrapper for a type which needs to be resolved before it can be printed in
231// C++.
David Tolnay5804bb72020-12-06 21:06:15 -0800232#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
233#[repr(transparent)]
David Tolnay75ea17c2020-12-06 21:08:34 -0800234pub struct RustName {
David Tolnay50de2c42020-10-30 21:16:03 -0700235 pub rust: Ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700236}