blob: 7f9b918c5e533e36edf0302d62055001ddfcc851 [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 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 Tolnaya8d94a12020-09-06 23:28:18 -070018pub mod qualified;
David Tolnaydf344a82020-05-03 23:23:18 -070019pub mod report;
David Tolnay7db73692019-10-20 14:51:12 -040020pub mod set;
David Tolnay891061b2020-04-19 22:42:33 -070021pub mod symbol;
David Tolnay7db73692019-10-20 14:51:12 -040022mod tokens;
David Tolnay942cffd2020-11-03 18:27:10 -080023mod toposort;
David Tolnay7db73692019-10-20 14:51:12 -040024pub mod types;
25
David Tolnay17e137f2020-05-08 15:55:28 -070026use self::discriminant::Discriminant;
Adrian Taylorc8713432020-10-21 18:20:55 -070027use self::namespace::Namespace;
David Tolnaye3a48152020-04-08 19:38:05 -070028use self::parse::kw;
Adrian Taylorc8713432020-10-21 18:20:55 -070029use self::symbol::Symbol;
David Tolnaye3a48152020-04-08 19:38:05 -070030use proc_macro2::{Ident, Span};
31use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070032use syn::token::{Brace, Bracket, Paren};
David Tolnay9938b642020-11-15 18:11:40 -080033use syn::{Expr, Generics, Lifetime, Token, Type as RustType};
David Tolnay7db73692019-10-20 14:51:12 -040034
35pub use self::atom::Atom;
David Tolnay64703b42020-05-10 22:12:33 -070036pub use self::derive::Derive;
David Tolnay7db73692019-10-20 14:51:12 -040037pub use self::doc::Doc;
38pub use self::parse::parse_items;
39pub use self::types::Types;
40
41pub enum Api {
David Tolnayb0cd3272020-10-27 21:32:54 -070042 Include(Include),
David Tolnay7db73692019-10-20 14:51:12 -040043 Struct(Struct),
Joel Galensonc03402a2020-04-23 17:31:09 -070044 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040045 CxxType(ExternType),
46 CxxFunction(ExternFn),
47 RustType(ExternType),
48 RustFunction(ExternFn),
David Tolnay99383812020-05-04 02:34:33 -070049 TypeAlias(TypeAlias),
David Tolnay7e69f892020-10-03 22:20:22 -070050 Impl(Impl),
David Tolnay7db73692019-10-20 14:51:12 -040051}
52
David Tolnayb0cd3272020-10-27 21:32:54 -070053pub struct Include {
54 pub path: String,
55 pub kind: IncludeKind,
56 pub begin_span: Span,
57 pub end_span: Span,
58}
59
David Tolnay700cd0c2020-10-28 12:40:27 -070060/// Whether to emit `#include "path"` or `#include <path>`.
61#[derive(Copy, Clone, PartialEq, Debug)]
David Tolnayb0cd3272020-10-27 21:32:54 -070062pub enum IncludeKind {
David Tolnay700cd0c2020-10-28 12:40:27 -070063 /// `#include "quoted/path/to"`
64 Quoted,
65 /// `#include <bracketed/path/to>`
66 Bracketed,
David Tolnayb0cd3272020-10-27 21:32:54 -070067}
68
David Tolnay7db73692019-10-20 14:51:12 -040069pub struct ExternType {
70 pub doc: Doc,
71 pub type_token: Token![type],
David Tolnay17a934c2020-11-02 00:40:04 -080072 pub name: Pair,
David Tolnayc8361022020-08-25 21:57:53 -070073 pub semi_token: Token![;],
David Tolnay00f236a2020-08-29 19:07:18 -070074 pub trusted: bool,
David Tolnay7db73692019-10-20 14:51:12 -040075}
76
77pub struct Struct {
78 pub doc: Doc,
David Tolnaye86b9cf2020-05-10 14:24:29 -070079 pub derives: Vec<Derive>,
David Tolnay7db73692019-10-20 14:51:12 -040080 pub struct_token: Token![struct],
David Tolnay17a934c2020-11-02 00:40:04 -080081 pub name: Pair,
David Tolnay09462ac2020-03-20 14:58:41 -070082 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040083 pub fields: Vec<Var>,
84}
85
Joel Galensonc03402a2020-04-23 17:31:09 -070086pub struct Enum {
87 pub doc: Doc,
88 pub enum_token: Token![enum],
David Tolnay17a934c2020-11-02 00:40:04 -080089 pub name: Pair,
Joel Galensonc03402a2020-04-23 17:31:09 -070090 pub brace_token: Brace,
91 pub variants: Vec<Variant>,
David Tolnaye2e303f2020-05-10 20:52:00 -070092 pub repr: Atom,
Joel Galensonc03402a2020-04-23 17:31:09 -070093}
94
David Tolnay7db73692019-10-20 14:51:12 -040095pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070096 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040097 pub doc: Doc,
David Tolnay17a934c2020-11-02 00:40:04 -080098 pub name: Pair,
David Tolnay16448732020-03-18 12:39:36 -070099 pub sig: Signature,
100 pub semi_token: Token![;],
David Tolnay556738d2020-11-15 13:58:44 -0800101 pub trusted: bool,
David Tolnay16448732020-03-18 12:39:36 -0700102}
103
David Tolnay99383812020-05-04 02:34:33 -0700104pub struct TypeAlias {
Bryan Henry890083d2020-09-13 10:34:31 -0700105 pub doc: Doc,
David Tolnay99383812020-05-04 02:34:33 -0700106 pub type_token: Token![type],
David Tolnay17a934c2020-11-02 00:40:04 -0800107 pub name: Pair,
David Tolnay99383812020-05-04 02:34:33 -0700108 pub eq_token: Token![=],
109 pub ty: RustType,
110 pub semi_token: Token![;],
111}
112
David Tolnay7e69f892020-10-03 22:20:22 -0700113pub struct Impl {
114 pub impl_token: Token![impl],
115 pub ty: Type,
116 pub brace_token: Brace,
117}
118
David Tolnay16448732020-03-18 12:39:36 -0700119pub struct Signature {
David Tolnaybdb576c2020-09-06 23:45:55 -0700120 pub unsafety: Option<Token![unsafe]>,
David Tolnay16448732020-03-18 12:39:36 -0700121 pub fn_token: Token![fn],
David Tolnay9938b642020-11-15 18:11:40 -0800122 pub generics: Generics,
David Tolnay7db73692019-10-20 14:51:12 -0400123 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -0700124 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -0400125 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -0700126 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -0700127 pub paren_token: Paren,
128 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -0400129}
130
David Tolnay417305a2020-03-18 13:54:00 -0700131#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -0400132pub struct Var {
David Tolnay50de2c42020-10-30 21:16:03 -0700133 pub ident: Ident,
David Tolnay7db73692019-10-20 14:51:12 -0400134 pub ty: Type,
135}
136
137pub struct Receiver {
David Tolnay9c76c392020-11-15 16:07:08 -0800138 pub pinned: bool,
David Tolnayfb6e3862020-04-20 01:33:23 -0700139 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700140 pub lifetime: Option<Lifetime>,
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800141 pub mutable: bool,
David Tolnay05e11cc2020-04-20 02:13:56 -0700142 pub var: Token![self],
Adrian Taylorc8713432020-10-21 18:20:55 -0700143 pub ty: ResolvableName,
David Tolnay62d360c2020-04-22 16:26:21 -0700144 pub shorthand: bool,
David Tolnay9c76c392020-11-15 16:07:08 -0800145 pub pin_tokens: Option<(kw::Pin, Token![<], Token![>])>,
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800146 pub mutability: Option<Token![mut]>,
David Tolnay7db73692019-10-20 14:51:12 -0400147}
148
Joel Galensonc03402a2020-04-23 17:31:09 -0700149pub struct Variant {
David Tolnay50de2c42020-10-30 21:16:03 -0700150 pub ident: Ident,
David Tolnay17e137f2020-05-08 15:55:28 -0700151 pub discriminant: Discriminant,
David Tolnay2b8bf6d2020-05-10 17:37:16 -0700152 pub expr: Option<Expr>,
Joel Galensonc03402a2020-04-23 17:31:09 -0700153}
154
David Tolnay7db73692019-10-20 14:51:12 -0400155pub enum Type {
Adrian Taylorc8713432020-10-21 18:20:55 -0700156 Ident(ResolvableName),
David Tolnay7db73692019-10-20 14:51:12 -0400157 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700158 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400159 UniquePtr(Box<Ty1>),
160 Ref(Box<Ref>),
161 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700162 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700163 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700164 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700165 Slice(Box<Slice>),
166 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400167}
168
169pub struct Ty1 {
David Tolnayc94035a2020-11-15 16:49:18 -0800170 pub pinned: bool,
David Tolnayda71ef62020-11-02 00:36:00 -0800171 pub name: Ident,
David Tolnay7db73692019-10-20 14:51:12 -0400172 pub langle: Token![<],
173 pub inner: Type,
174 pub rangle: Token![>],
David Tolnayc94035a2020-11-15 16:49:18 -0800175 pub pin_tokens: Option<(kw::Pin, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -0400176}
177
178pub struct Ref {
David Tolnayb27f7872020-11-15 15:07:04 -0800179 pub pinned: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400180 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700181 pub lifetime: Option<Lifetime>,
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800182 pub mutable: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400183 pub inner: Type,
David Tolnayb27f7872020-11-15 15:07:04 -0800184 pub pin_tokens: Option<(kw::Pin, Token![<], Token![>])>,
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800185 pub mutability: Option<Token![mut]>,
David Tolnay7db73692019-10-20 14:51:12 -0400186}
187
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700188pub struct Slice {
189 pub bracket: Bracket,
190 pub inner: Type,
191}
192
David Tolnay7db73692019-10-20 14:51:12 -0400193#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700194pub enum Lang {
195 Cxx,
196 Rust,
197}
Adrian Taylorc8713432020-10-21 18:20:55 -0700198
David Tolnaybe7e30e2020-10-30 21:21:23 -0700199// An association of a defined Rust name with a fully resolved, namespace
200// qualified C++ name.
David Tolnay9071c262020-10-30 21:18:54 -0700201#[derive(Clone)]
202pub struct Pair {
David Tolnayd7a3a182020-11-01 20:45:14 -0800203 pub namespace: Namespace,
David Tolnay8faec772020-11-02 00:18:19 -0800204 pub cxx: Ident,
205 pub rust: Ident,
David Tolnay9071c262020-10-30 21:18:54 -0700206}
207
David Tolnayabff2d52020-10-30 21:20:07 -0700208// Wrapper for a type which needs to be resolved before it can be printed in
209// C++.
David Tolnay59815422020-10-30 21:24:05 -0700210#[derive(Clone, PartialEq, Hash)]
Adrian Taylorc8713432020-10-21 18:20:55 -0700211pub struct ResolvableName {
David Tolnay50de2c42020-10-30 21:16:03 -0700212 pub rust: Ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700213}