blob: 0d07fd0f02e3931cc14937e183de95181364db1c [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;
David Tolnay067638e2020-12-30 16:18:05 -08004pub mod attrs;
David Tolnay7db73692019-10-20 14:51:12 -04005pub 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 Tolnay0634b1f2020-12-20 21:04:04 -080025pub mod trivial;
David Tolnay7db73692019-10-20 14:51:12 -040026pub mod types;
27
David Tolnaycba3d442020-12-30 16:39:21 -080028use self::attrs::OtherAttrs;
David Tolnay17e137f2020-05-08 15:55:28 -070029use self::discriminant::Discriminant;
Adrian Taylorc8713432020-10-21 18:20:55 -070030use self::namespace::Namespace;
David Tolnaye3a48152020-04-08 19:38:05 -070031use self::parse::kw;
Adrian Taylorc8713432020-10-21 18:20:55 -070032use self::symbol::Symbol;
David Tolnaye3a48152020-04-08 19:38:05 -070033use proc_macro2::{Ident, Span};
34use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070035use syn::token::{Brace, Bracket, Paren};
David Tolnaycba3d442020-12-30 16:39:21 -080036use syn::{Expr, Generics, Lifetime, LitInt, Token, Type as RustType};
David Tolnay7db73692019-10-20 14:51:12 -040037
38pub use self::atom::Atom;
David Tolnayb247df12020-11-27 12:06:12 -080039pub use self::derive::{Derive, Trait};
David Tolnay7db73692019-10-20 14:51:12 -040040pub use self::doc::Doc;
41pub use self::parse::parse_items;
42pub use self::types::Types;
43
44pub enum Api {
David Tolnayb0cd3272020-10-27 21:32:54 -070045 Include(Include),
David Tolnay7db73692019-10-20 14:51:12 -040046 Struct(Struct),
Joel Galensonc03402a2020-04-23 17:31:09 -070047 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040048 CxxType(ExternType),
49 CxxFunction(ExternFn),
50 RustType(ExternType),
51 RustFunction(ExternFn),
David Tolnay99383812020-05-04 02:34:33 -070052 TypeAlias(TypeAlias),
David Tolnay7e69f892020-10-03 22:20:22 -070053 Impl(Impl),
David Tolnay7db73692019-10-20 14:51:12 -040054}
55
David Tolnayb0cd3272020-10-27 21:32:54 -070056pub struct Include {
57 pub path: String,
58 pub kind: IncludeKind,
59 pub begin_span: Span,
60 pub end_span: Span,
61}
62
David Tolnay700cd0c2020-10-28 12:40:27 -070063/// Whether to emit `#include "path"` or `#include <path>`.
64#[derive(Copy, Clone, PartialEq, Debug)]
David Tolnayb0cd3272020-10-27 21:32:54 -070065pub enum IncludeKind {
David Tolnay700cd0c2020-10-28 12:40:27 -070066 /// `#include "quoted/path/to"`
67 Quoted,
68 /// `#include <bracketed/path/to>`
69 Bracketed,
David Tolnayb0cd3272020-10-27 21:32:54 -070070}
71
David Tolnay7db73692019-10-20 14:51:12 -040072pub struct ExternType {
David Tolnay16e26202020-11-27 19:28:37 -080073 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040074 pub doc: Doc,
David Tolnay16e26202020-11-27 19:28:37 -080075 pub derives: Vec<Derive>,
David Tolnaycba3d442020-12-30 16:39:21 -080076 pub attrs: OtherAttrs,
David Tolnay7db73692019-10-20 14:51:12 -040077 pub type_token: Token![type],
David Tolnay17a934c2020-11-02 00:40:04 -080078 pub name: Pair,
David Tolnayee312322020-12-28 22:04:10 -080079 pub generics: Lifetimes,
David Tolnayb7eb65e2020-11-27 20:00:53 -080080 pub colon_token: Option<Token![:]>,
81 pub bounds: Vec<Derive>,
David Tolnayc8361022020-08-25 21:57:53 -070082 pub semi_token: Token![;],
David Tolnay00f236a2020-08-29 19:07:18 -070083 pub trusted: bool,
David Tolnay7db73692019-10-20 14:51:12 -040084}
85
86pub struct Struct {
87 pub doc: Doc,
David Tolnaye86b9cf2020-05-10 14:24:29 -070088 pub derives: Vec<Derive>,
David Tolnaycba3d442020-12-30 16:39:21 -080089 pub attrs: OtherAttrs,
David Tolnay64343af2020-12-30 15:26:06 -080090 pub visibility: Token![pub],
David Tolnay7db73692019-10-20 14:51:12 -040091 pub struct_token: Token![struct],
David Tolnay17a934c2020-11-02 00:40:04 -080092 pub name: Pair,
David Tolnay09462ac2020-03-20 14:58:41 -070093 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040094 pub fields: Vec<Var>,
95}
96
Joel Galensonc03402a2020-04-23 17:31:09 -070097pub struct Enum {
98 pub doc: Doc,
David Tolnayfbc46692020-11-27 11:33:29 -080099 pub derives: Vec<Derive>,
David Tolnaycba3d442020-12-30 16:39:21 -0800100 pub attrs: OtherAttrs,
David Tolnay8fbaea82020-12-30 17:15:46 -0800101 pub visibility: Token![pub],
Joel Galensonc03402a2020-04-23 17:31:09 -0700102 pub enum_token: Token![enum],
David Tolnay17a934c2020-11-02 00:40:04 -0800103 pub name: Pair,
Joel Galensonc03402a2020-04-23 17:31:09 -0700104 pub brace_token: Brace,
105 pub variants: Vec<Variant>,
David Tolnaye2e303f2020-05-10 20:52:00 -0700106 pub repr: Atom,
David Tolnay9a69d8a2020-11-16 09:16:41 -0800107 pub repr_type: Type,
David Tolnay58eee392020-11-20 20:37:58 -0800108 pub explicit_repr: bool,
Joel Galensonc03402a2020-04-23 17:31:09 -0700109}
110
David Tolnay7db73692019-10-20 14:51:12 -0400111pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -0700112 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -0400113 pub doc: Doc,
David Tolnaycba3d442020-12-30 16:39:21 -0800114 pub attrs: OtherAttrs,
David Tolnay17a934c2020-11-02 00:40:04 -0800115 pub name: Pair,
David Tolnay16448732020-03-18 12:39:36 -0700116 pub sig: Signature,
117 pub semi_token: Token![;],
David Tolnay556738d2020-11-15 13:58:44 -0800118 pub trusted: bool,
David Tolnay16448732020-03-18 12:39:36 -0700119}
120
David Tolnay99383812020-05-04 02:34:33 -0700121pub struct TypeAlias {
Bryan Henry890083d2020-09-13 10:34:31 -0700122 pub doc: Doc,
David Tolnayb7eb65e2020-11-27 20:00:53 -0800123 pub derives: Vec<Derive>,
David Tolnaycba3d442020-12-30 16:39:21 -0800124 pub attrs: OtherAttrs,
David Tolnay99383812020-05-04 02:34:33 -0700125 pub type_token: Token![type],
David Tolnay17a934c2020-11-02 00:40:04 -0800126 pub name: Pair,
David Tolnayee312322020-12-28 22:04:10 -0800127 pub generics: Lifetimes,
David Tolnay99383812020-05-04 02:34:33 -0700128 pub eq_token: Token![=],
129 pub ty: RustType,
130 pub semi_token: Token![;],
131}
132
David Tolnay7e69f892020-10-03 22:20:22 -0700133pub struct Impl {
134 pub impl_token: Token![impl],
David Tolnay028d3d22020-11-29 18:09:52 -0800135 pub negative: bool,
David Tolnay7e69f892020-10-03 22:20:22 -0700136 pub ty: Type,
137 pub brace_token: Brace,
David Tolnay028d3d22020-11-29 18:09:52 -0800138 pub negative_token: Option<Token![!]>,
David Tolnay7e69f892020-10-03 22:20:22 -0700139}
140
David Tolnayee312322020-12-28 22:04:10 -0800141pub struct Lifetimes {
142 pub lt_token: Option<Token![<]>,
143 pub lifetimes: Punctuated<Lifetime, Token![,]>,
144 pub gt_token: Option<Token![>]>,
145}
146
David Tolnay16448732020-03-18 12:39:36 -0700147pub struct Signature {
David Tolnaybdb576c2020-09-06 23:45:55 -0700148 pub unsafety: Option<Token![unsafe]>,
David Tolnay16448732020-03-18 12:39:36 -0700149 pub fn_token: Token![fn],
David Tolnay9938b642020-11-15 18:11:40 -0800150 pub generics: Generics,
David Tolnay7db73692019-10-20 14:51:12 -0400151 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -0700152 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -0400153 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -0700154 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -0700155 pub paren_token: Paren,
156 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -0400157}
158
159pub struct Var {
David Tolnayb4a05c32020-12-30 15:37:01 -0800160 pub doc: Doc,
David Tolnaycba3d442020-12-30 16:39:21 -0800161 pub attrs: OtherAttrs,
David Tolnay89902442020-12-30 15:10:40 -0800162 pub visibility: Token![pub],
David Tolnay50de2c42020-10-30 21:16:03 -0700163 pub ident: Ident,
David Tolnay7db73692019-10-20 14:51:12 -0400164 pub ty: Type,
165}
166
167pub struct Receiver {
David Tolnay9c76c392020-11-15 16:07:08 -0800168 pub pinned: bool,
David Tolnayfb6e3862020-04-20 01:33:23 -0700169 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700170 pub lifetime: Option<Lifetime>,
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800171 pub mutable: bool,
David Tolnay05e11cc2020-04-20 02:13:56 -0700172 pub var: Token![self],
David Tolnay75ea17c2020-12-06 21:08:34 -0800173 pub ty: RustName,
David Tolnay62d360c2020-04-22 16:26:21 -0700174 pub shorthand: bool,
David Tolnay9c76c392020-11-15 16:07:08 -0800175 pub pin_tokens: Option<(kw::Pin, Token![<], Token![>])>,
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800176 pub mutability: Option<Token![mut]>,
David Tolnay7db73692019-10-20 14:51:12 -0400177}
178
Joel Galensonc03402a2020-04-23 17:31:09 -0700179pub struct Variant {
David Tolnay4486f722020-12-30 00:01:26 -0800180 pub doc: Doc,
David Tolnaycba3d442020-12-30 16:39:21 -0800181 pub attrs: OtherAttrs,
David Tolnaye6f62142020-12-21 16:00:41 -0800182 pub name: Pair,
David Tolnay17e137f2020-05-08 15:55:28 -0700183 pub discriminant: Discriminant,
David Tolnay2b8bf6d2020-05-10 17:37:16 -0700184 pub expr: Option<Expr>,
Joel Galensonc03402a2020-04-23 17:31:09 -0700185}
186
David Tolnay7db73692019-10-20 14:51:12 -0400187pub enum Type {
David Tolnay75ea17c2020-12-06 21:08:34 -0800188 Ident(RustName),
David Tolnay7db73692019-10-20 14:51:12 -0400189 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700190 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400191 UniquePtr(Box<Ty1>),
David Tolnayb3b24a12020-12-01 15:27:43 -0800192 SharedPtr(Box<Ty1>),
David Tolnay215e77f2020-12-28 17:09:48 -0800193 WeakPtr(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400194 Ref(Box<Ref>),
195 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700196 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700197 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700198 Void(Span),
David Tolnay73b72642020-11-25 17:44:05 -0800199 SliceRef(Box<SliceRef>),
Xiangpeng Hao78762352020-11-12 10:24:18 +0800200 Array(Box<Array>),
David Tolnay7db73692019-10-20 14:51:12 -0400201}
202
203pub struct Ty1 {
David Tolnayda71ef62020-11-02 00:36:00 -0800204 pub name: Ident,
David Tolnay7db73692019-10-20 14:51:12 -0400205 pub langle: Token![<],
206 pub inner: Type,
207 pub rangle: Token![>],
208}
209
210pub struct Ref {
David Tolnayb27f7872020-11-15 15:07:04 -0800211 pub pinned: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400212 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700213 pub lifetime: Option<Lifetime>,
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800214 pub mutable: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400215 pub inner: Type,
David Tolnayb27f7872020-11-15 15:07:04 -0800216 pub pin_tokens: Option<(kw::Pin, Token![<], Token![>])>,
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800217 pub mutability: Option<Token![mut]>,
David Tolnay7db73692019-10-20 14:51:12 -0400218}
219
David Tolnaye0dca7b2020-11-25 17:18:57 -0800220pub struct SliceRef {
221 pub ampersand: Token![&],
222 pub lifetime: Option<Lifetime>,
223 pub mutable: bool,
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700224 pub bracket: Bracket,
225 pub inner: Type,
David Tolnaye0dca7b2020-11-25 17:18:57 -0800226 pub mutability: Option<Token![mut]>,
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700227}
228
Xiangpeng Hao78762352020-11-12 10:24:18 +0800229pub struct Array {
230 pub bracket: Bracket,
231 pub inner: Type,
232 pub semi_token: Token![;],
233 pub len: usize,
David Tolnayc351dc42020-11-24 20:45:13 -0800234 pub len_token: LitInt,
Xiangpeng Hao78762352020-11-12 10:24:18 +0800235}
236
David Tolnay7db73692019-10-20 14:51:12 -0400237#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700238pub enum Lang {
239 Cxx,
240 Rust,
241}
Adrian Taylorc8713432020-10-21 18:20:55 -0700242
David Tolnaybe7e30e2020-10-30 21:21:23 -0700243// An association of a defined Rust name with a fully resolved, namespace
244// qualified C++ name.
David Tolnay9071c262020-10-30 21:18:54 -0700245#[derive(Clone)]
246pub struct Pair {
David Tolnayd7a3a182020-11-01 20:45:14 -0800247 pub namespace: Namespace,
David Tolnay8faec772020-11-02 00:18:19 -0800248 pub cxx: Ident,
249 pub rust: Ident,
David Tolnay9071c262020-10-30 21:18:54 -0700250}
251
David Tolnayabff2d52020-10-30 21:20:07 -0700252// Wrapper for a type which needs to be resolved before it can be printed in
253// C++.
David Tolnay5804bb72020-12-06 21:06:15 -0800254#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
255#[repr(transparent)]
David Tolnay75ea17c2020-12-06 21:08:34 -0800256pub struct RustName {
David Tolnay50de2c42020-10-30 21:16:03 -0700257 pub rust: Ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700258}