blob: b742e37a90a0a3d4ae22759507a6d8f912da5ac3 [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;
23pub mod types;
24
David Tolnay17e137f2020-05-08 15:55:28 -070025use self::discriminant::Discriminant;
Adrian Taylorc8713432020-10-21 18:20:55 -070026use self::namespace::Namespace;
David Tolnaye3a48152020-04-08 19:38:05 -070027use self::parse::kw;
Adrian Taylorc8713432020-10-21 18:20:55 -070028use self::symbol::Symbol;
David Tolnaye3a48152020-04-08 19:38:05 -070029use proc_macro2::{Ident, Span};
30use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070031use syn::token::{Brace, Bracket, Paren};
David Tolnay91e87fa2020-05-11 19:10:23 -070032use syn::{Expr, Lifetime, Token, Type as RustType};
David Tolnay7db73692019-10-20 14:51:12 -040033
34pub use self::atom::Atom;
David Tolnay64703b42020-05-10 22:12:33 -070035pub use self::derive::Derive;
David Tolnay7db73692019-10-20 14:51:12 -040036pub use self::doc::Doc;
37pub use self::parse::parse_items;
38pub use self::types::Types;
39
40pub enum Api {
David Tolnayb0cd3272020-10-27 21:32:54 -070041 Include(Include),
David Tolnay7db73692019-10-20 14:51:12 -040042 Struct(Struct),
Joel Galensonc03402a2020-04-23 17:31:09 -070043 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040044 CxxType(ExternType),
45 CxxFunction(ExternFn),
46 RustType(ExternType),
47 RustFunction(ExternFn),
David Tolnay99383812020-05-04 02:34:33 -070048 TypeAlias(TypeAlias),
David Tolnay7e69f892020-10-03 22:20:22 -070049 Impl(Impl),
David Tolnay7db73692019-10-20 14:51:12 -040050}
51
David Tolnayb0cd3272020-10-27 21:32:54 -070052pub struct Include {
53 pub path: String,
54 pub kind: IncludeKind,
55 pub begin_span: Span,
56 pub end_span: Span,
57}
58
David Tolnay700cd0c2020-10-28 12:40:27 -070059/// Whether to emit `#include "path"` or `#include <path>`.
60#[derive(Copy, Clone, PartialEq, Debug)]
David Tolnayb0cd3272020-10-27 21:32:54 -070061pub enum IncludeKind {
David Tolnay700cd0c2020-10-28 12:40:27 -070062 /// `#include "quoted/path/to"`
63 Quoted,
64 /// `#include <bracketed/path/to>`
65 Bracketed,
David Tolnayb0cd3272020-10-27 21:32:54 -070066}
67
David Tolnay7db73692019-10-20 14:51:12 -040068pub struct ExternType {
69 pub doc: Doc,
70 pub type_token: Token![type],
David Tolnay17a934c2020-11-02 00:40:04 -080071 pub name: Pair,
David Tolnayc8361022020-08-25 21:57:53 -070072 pub semi_token: Token![;],
David Tolnay00f236a2020-08-29 19:07:18 -070073 pub trusted: bool,
David Tolnay7db73692019-10-20 14:51:12 -040074}
75
76pub struct Struct {
77 pub doc: Doc,
David Tolnaye86b9cf2020-05-10 14:24:29 -070078 pub derives: Vec<Derive>,
David Tolnay7db73692019-10-20 14:51:12 -040079 pub struct_token: Token![struct],
David Tolnay17a934c2020-11-02 00:40:04 -080080 pub name: Pair,
David Tolnay09462ac2020-03-20 14:58:41 -070081 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040082 pub fields: Vec<Var>,
83}
84
Joel Galensonc03402a2020-04-23 17:31:09 -070085pub struct Enum {
86 pub doc: Doc,
87 pub enum_token: Token![enum],
David Tolnay17a934c2020-11-02 00:40:04 -080088 pub name: Pair,
Joel Galensonc03402a2020-04-23 17:31:09 -070089 pub brace_token: Brace,
90 pub variants: Vec<Variant>,
David Tolnaye2e303f2020-05-10 20:52:00 -070091 pub repr: Atom,
Joel Galensonc03402a2020-04-23 17:31:09 -070092}
93
David Tolnay7db73692019-10-20 14:51:12 -040094pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070095 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040096 pub doc: Doc,
David Tolnay17a934c2020-11-02 00:40:04 -080097 pub name: Pair,
David Tolnay16448732020-03-18 12:39:36 -070098 pub sig: Signature,
99 pub semi_token: Token![;],
100}
101
David Tolnay99383812020-05-04 02:34:33 -0700102pub struct TypeAlias {
Bryan Henry890083d2020-09-13 10:34:31 -0700103 pub doc: Doc,
David Tolnay99383812020-05-04 02:34:33 -0700104 pub type_token: Token![type],
David Tolnay17a934c2020-11-02 00:40:04 -0800105 pub name: Pair,
David Tolnay99383812020-05-04 02:34:33 -0700106 pub eq_token: Token![=],
107 pub ty: RustType,
108 pub semi_token: Token![;],
109}
110
David Tolnay7e69f892020-10-03 22:20:22 -0700111pub struct Impl {
112 pub impl_token: Token![impl],
113 pub ty: Type,
114 pub brace_token: Brace,
115}
116
David Tolnay16448732020-03-18 12:39:36 -0700117pub struct Signature {
David Tolnaybdb576c2020-09-06 23:45:55 -0700118 pub unsafety: Option<Token![unsafe]>,
David Tolnay16448732020-03-18 12:39:36 -0700119 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -0400120 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -0700121 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -0400122 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -0700123 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -0700124 pub paren_token: Paren,
125 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -0400126}
127
David Tolnay417305a2020-03-18 13:54:00 -0700128#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -0400129pub struct Var {
David Tolnay50de2c42020-10-30 21:16:03 -0700130 pub ident: Ident,
David Tolnay7db73692019-10-20 14:51:12 -0400131 pub ty: Type,
132}
133
134pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -0700135 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700136 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400137 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -0700138 pub var: Token![self],
Adrian Taylorc8713432020-10-21 18:20:55 -0700139 pub ty: ResolvableName,
David Tolnay62d360c2020-04-22 16:26:21 -0700140 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400141}
142
Joel Galensonc03402a2020-04-23 17:31:09 -0700143pub struct Variant {
David Tolnay50de2c42020-10-30 21:16:03 -0700144 pub ident: Ident,
David Tolnay17e137f2020-05-08 15:55:28 -0700145 pub discriminant: Discriminant,
David Tolnay2b8bf6d2020-05-10 17:37:16 -0700146 pub expr: Option<Expr>,
Joel Galensonc03402a2020-04-23 17:31:09 -0700147}
148
David Tolnay7db73692019-10-20 14:51:12 -0400149pub enum Type {
Adrian Taylorc8713432020-10-21 18:20:55 -0700150 Ident(ResolvableName),
David Tolnay7db73692019-10-20 14:51:12 -0400151 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700152 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400153 UniquePtr(Box<Ty1>),
154 Ref(Box<Ref>),
155 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700156 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700157 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700158 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700159 Slice(Box<Slice>),
160 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400161}
162
163pub struct Ty1 {
David Tolnayda71ef62020-11-02 00:36:00 -0800164 pub name: Ident,
David Tolnay7db73692019-10-20 14:51:12 -0400165 pub langle: Token![<],
166 pub inner: Type,
167 pub rangle: Token![>],
168}
169
170pub struct Ref {
171 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700172 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400173 pub mutability: Option<Token![mut]>,
174 pub inner: Type,
175}
176
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700177pub struct Slice {
178 pub bracket: Bracket,
179 pub inner: Type,
180}
181
David Tolnay7db73692019-10-20 14:51:12 -0400182#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700183pub enum Lang {
184 Cxx,
185 Rust,
186}
Adrian Taylorc8713432020-10-21 18:20:55 -0700187
David Tolnaybe7e30e2020-10-30 21:21:23 -0700188// An association of a defined Rust name with a fully resolved, namespace
189// qualified C++ name.
David Tolnay9071c262020-10-30 21:18:54 -0700190#[derive(Clone)]
191pub struct Pair {
David Tolnayd7a3a182020-11-01 20:45:14 -0800192 pub namespace: Namespace,
David Tolnay8faec772020-11-02 00:18:19 -0800193 pub cxx: Ident,
194 pub rust: Ident,
David Tolnay9071c262020-10-30 21:18:54 -0700195}
196
David Tolnayabff2d52020-10-30 21:20:07 -0700197// Wrapper for a type which needs to be resolved before it can be printed in
198// C++.
David Tolnay59815422020-10-30 21:24:05 -0700199#[derive(Clone, PartialEq, Hash)]
Adrian Taylorc8713432020-10-21 18:20:55 -0700200pub struct ResolvableName {
David Tolnay50de2c42020-10-30 21:16:03 -0700201 pub rust: Ident,
Adrian Taylorc8713432020-10-21 18:20:55 -0700202}