blob: 6684b6a8ac23edd272416b6e903add8ce61d801d [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 Tolnay60e7aa62020-11-01 12:34:51 -080014mod names;
David Tolnay08419302020-04-19 20:38:20 -070015pub mod namespace;
David Tolnay7db73692019-10-20 14:51:12 -040016mod parse;
David Tolnaya8d94a12020-09-06 23:28:18 -070017pub mod qualified;
David Tolnaydf344a82020-05-03 23:23:18 -070018pub mod report;
David Tolnay7db73692019-10-20 14:51:12 -040019pub mod set;
David Tolnay891061b2020-04-19 22:42:33 -070020pub mod symbol;
David Tolnay7db73692019-10-20 14:51:12 -040021mod tokens;
22pub mod types;
23
David Tolnay17e137f2020-05-08 15:55:28 -070024use self::discriminant::Discriminant;
Adrian Taylorc8713432020-10-21 18:20:55 -070025use self::namespace::Namespace;
David Tolnaye3a48152020-04-08 19:38:05 -070026use self::parse::kw;
Adrian Taylorc8713432020-10-21 18:20:55 -070027use self::symbol::Symbol;
David Tolnaye3a48152020-04-08 19:38:05 -070028use proc_macro2::{Ident, Span};
29use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070030use syn::token::{Brace, Bracket, Paren};
David Tolnay91e87fa2020-05-11 19:10:23 -070031use syn::{Expr, Lifetime, Token, Type as RustType};
David Tolnay7db73692019-10-20 14:51:12 -040032
33pub use self::atom::Atom;
David Tolnay64703b42020-05-10 22:12:33 -070034pub use self::derive::Derive;
David Tolnay7db73692019-10-20 14:51:12 -040035pub use self::doc::Doc;
36pub use self::parse::parse_items;
37pub use self::types::Types;
38
39pub enum Api {
David Tolnayb0cd3272020-10-27 21:32:54 -070040 Include(Include),
David Tolnay7db73692019-10-20 14:51:12 -040041 Struct(Struct),
Joel Galensonc03402a2020-04-23 17:31:09 -070042 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040043 CxxType(ExternType),
44 CxxFunction(ExternFn),
45 RustType(ExternType),
46 RustFunction(ExternFn),
David Tolnay99383812020-05-04 02:34:33 -070047 TypeAlias(TypeAlias),
David Tolnay7e69f892020-10-03 22:20:22 -070048 Impl(Impl),
David Tolnay7db73692019-10-20 14:51:12 -040049}
50
David Tolnayb0cd3272020-10-27 21:32:54 -070051pub struct Include {
52 pub path: String,
53 pub kind: IncludeKind,
54 pub begin_span: Span,
55 pub end_span: Span,
56}
57
David Tolnay700cd0c2020-10-28 12:40:27 -070058/// Whether to emit `#include "path"` or `#include <path>`.
59#[derive(Copy, Clone, PartialEq, Debug)]
David Tolnayb0cd3272020-10-27 21:32:54 -070060pub enum IncludeKind {
David Tolnay700cd0c2020-10-28 12:40:27 -070061 /// `#include "quoted/path/to"`
62 Quoted,
63 /// `#include <bracketed/path/to>`
64 Bracketed,
David Tolnayb0cd3272020-10-27 21:32:54 -070065}
66
David Tolnay7db73692019-10-20 14:51:12 -040067pub struct ExternType {
68 pub doc: Doc,
69 pub type_token: Token![type],
Adrian Taylorc8713432020-10-21 18:20:55 -070070 pub ident: Pair,
David Tolnayc8361022020-08-25 21:57:53 -070071 pub semi_token: Token![;],
David Tolnay00f236a2020-08-29 19:07:18 -070072 pub trusted: bool,
David Tolnay7db73692019-10-20 14:51:12 -040073}
74
75pub struct Struct {
76 pub doc: Doc,
David Tolnaye86b9cf2020-05-10 14:24:29 -070077 pub derives: Vec<Derive>,
David Tolnay7db73692019-10-20 14:51:12 -040078 pub struct_token: Token![struct],
Adrian Taylorc8713432020-10-21 18:20:55 -070079 pub ident: Pair,
David Tolnay09462ac2020-03-20 14:58:41 -070080 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040081 pub fields: Vec<Var>,
82}
83
Joel Galensonc03402a2020-04-23 17:31:09 -070084pub struct Enum {
85 pub doc: Doc,
86 pub enum_token: Token![enum],
Adrian Taylorc8713432020-10-21 18:20:55 -070087 pub ident: Pair,
Joel Galensonc03402a2020-04-23 17:31:09 -070088 pub brace_token: Brace,
89 pub variants: Vec<Variant>,
David Tolnaye2e303f2020-05-10 20:52:00 -070090 pub repr: Atom,
Joel Galensonc03402a2020-04-23 17:31:09 -070091}
92
David Tolnay7db73692019-10-20 14:51:12 -040093pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070094 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040095 pub doc: Doc,
David Tolnaya4641c72020-09-08 14:05:53 -070096 pub ident: Pair,
David Tolnay16448732020-03-18 12:39:36 -070097 pub sig: Signature,
98 pub semi_token: Token![;],
99}
100
David Tolnay99383812020-05-04 02:34:33 -0700101pub struct TypeAlias {
Bryan Henry890083d2020-09-13 10:34:31 -0700102 pub doc: Doc,
David Tolnay99383812020-05-04 02:34:33 -0700103 pub type_token: Token![type],
Adrian Taylorc8713432020-10-21 18:20:55 -0700104 pub ident: Pair,
David Tolnay99383812020-05-04 02:34:33 -0700105 pub eq_token: Token![=],
106 pub ty: RustType,
107 pub semi_token: Token![;],
108}
109
David Tolnay7e69f892020-10-03 22:20:22 -0700110pub struct Impl {
111 pub impl_token: Token![impl],
112 pub ty: Type,
113 pub brace_token: Brace,
114}
115
David Tolnay16448732020-03-18 12:39:36 -0700116pub struct Signature {
David Tolnaybdb576c2020-09-06 23:45:55 -0700117 pub unsafety: Option<Token![unsafe]>,
David Tolnay16448732020-03-18 12:39:36 -0700118 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -0400119 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -0700120 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -0400121 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -0700122 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -0700123 pub paren_token: Paren,
124 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -0400125}
126
David Tolnay417305a2020-03-18 13:54:00 -0700127#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -0400128pub struct Var {
David Tolnay50de2c42020-10-30 21:16:03 -0700129 pub ident: Ident,
David Tolnay7db73692019-10-20 14:51:12 -0400130 pub ty: Type,
131}
132
133pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -0700134 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700135 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400136 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -0700137 pub var: Token![self],
Adrian Taylorc8713432020-10-21 18:20:55 -0700138 pub ty: ResolvableName,
David Tolnay62d360c2020-04-22 16:26:21 -0700139 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400140}
141
Joel Galensonc03402a2020-04-23 17:31:09 -0700142pub struct Variant {
David Tolnay50de2c42020-10-30 21:16:03 -0700143 pub ident: Ident,
David Tolnay17e137f2020-05-08 15:55:28 -0700144 pub discriminant: Discriminant,
David Tolnay2b8bf6d2020-05-10 17:37:16 -0700145 pub expr: Option<Expr>,
Joel Galensonc03402a2020-04-23 17:31:09 -0700146}
147
David Tolnay7db73692019-10-20 14:51:12 -0400148pub enum Type {
Adrian Taylorc8713432020-10-21 18:20:55 -0700149 Ident(ResolvableName),
David Tolnay7db73692019-10-20 14:51:12 -0400150 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700151 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400152 UniquePtr(Box<Ty1>),
153 Ref(Box<Ref>),
154 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700155 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700156 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700157 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700158 Slice(Box<Slice>),
159 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400160}
161
162pub struct Ty1 {
Adrian Taylorc8713432020-10-21 18:20:55 -0700163 pub name: ResolvableName,
David Tolnay7db73692019-10-20 14:51:12 -0400164 pub langle: Token![<],
165 pub inner: Type,
166 pub rangle: Token![>],
167}
168
169pub struct Ref {
170 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700171 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400172 pub mutability: Option<Token![mut]>,
173 pub inner: Type,
174}
175
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700176pub struct Slice {
177 pub bracket: Bracket,
178 pub inner: Type,
179}
180
David Tolnay7db73692019-10-20 14:51:12 -0400181#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700182pub enum Lang {
183 Cxx,
184 Rust,
185}
Adrian Taylorc8713432020-10-21 18:20:55 -0700186
David Tolnaybe7e30e2020-10-30 21:21:23 -0700187// An association of a defined Rust name with a fully resolved, namespace
188// qualified C++ name.
David Tolnay9071c262020-10-30 21:18:54 -0700189#[derive(Clone)]
190pub struct Pair {
David Tolnayd7a3a182020-11-01 20:45:14 -0800191 pub namespace: Namespace,
David Tolnay8faec772020-11-02 00:18:19 -0800192 pub cxx: Ident,
193 pub rust: Ident,
David Tolnay9071c262020-10-30 21:18:54 -0700194}
195
David Tolnayabff2d52020-10-30 21:20:07 -0700196// Wrapper for a type which needs to be resolved before it can be printed in
197// C++.
David Tolnay59815422020-10-30 21:24:05 -0700198#[derive(Clone, PartialEq, Hash)]
Adrian Taylorc8713432020-10-21 18:20:55 -0700199pub struct ResolvableName {
David Tolnay50de2c42020-10-30 21:16:03 -0700200 pub rust: Ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700201}