blob: d21c0835a931e807e54dfc1f24877413930bada5 [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 Tolnay7db73692019-10-20 14:51:12 -04007mod doc;
8pub mod error;
9pub mod ident;
10mod impls;
David Tolnay3caa50a2020-04-19 21:25:34 -070011pub mod mangle;
David Tolnay08419302020-04-19 20:38:20 -070012pub mod namespace;
David Tolnay7db73692019-10-20 14:51:12 -040013mod parse;
David Tolnaydf344a82020-05-03 23:23:18 -070014pub mod report;
David Tolnay7db73692019-10-20 14:51:12 -040015pub mod set;
David Tolnay891061b2020-04-19 22:42:33 -070016pub mod symbol;
David Tolnay7db73692019-10-20 14:51:12 -040017mod tokens;
18pub mod types;
19
David Tolnaye3a48152020-04-08 19:38:05 -070020use self::parse::kw;
21use proc_macro2::{Ident, Span};
22use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070023use syn::token::{Brace, Bracket, Paren};
David Tolnay99383812020-05-04 02:34:33 -070024use syn::{Lifetime, LitStr, Token, Type as RustType};
David Tolnay7db73692019-10-20 14:51:12 -040025
26pub use self::atom::Atom;
David Tolnay64703b42020-05-10 22:12:33 -070027pub use self::derive::Derive;
David Tolnay7db73692019-10-20 14:51:12 -040028pub use self::doc::Doc;
29pub use self::parse::parse_items;
30pub use self::types::Types;
31
32pub enum Api {
33 Include(LitStr),
34 Struct(Struct),
Joel Galensonc03402a2020-04-23 17:31:09 -070035 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040036 CxxType(ExternType),
37 CxxFunction(ExternFn),
38 RustType(ExternType),
39 RustFunction(ExternFn),
David Tolnay99383812020-05-04 02:34:33 -070040 TypeAlias(TypeAlias),
David Tolnay7db73692019-10-20 14:51:12 -040041}
42
43pub struct ExternType {
44 pub doc: Doc,
45 pub type_token: Token![type],
46 pub ident: Ident,
47}
48
49pub struct Struct {
50 pub doc: Doc,
David Tolnaye86b9cf2020-05-10 14:24:29 -070051 pub derives: Vec<Derive>,
David Tolnay7db73692019-10-20 14:51:12 -040052 pub struct_token: Token![struct],
53 pub ident: Ident,
David Tolnay09462ac2020-03-20 14:58:41 -070054 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040055 pub fields: Vec<Var>,
56}
57
Joel Galensonc03402a2020-04-23 17:31:09 -070058pub struct Enum {
59 pub doc: Doc,
60 pub enum_token: Token![enum],
61 pub ident: Ident,
62 pub brace_token: Brace,
63 pub variants: Vec<Variant>,
64}
65
David Tolnay7db73692019-10-20 14:51:12 -040066pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070067 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040068 pub doc: Doc,
David Tolnay7db73692019-10-20 14:51:12 -040069 pub ident: Ident,
David Tolnay16448732020-03-18 12:39:36 -070070 pub sig: Signature,
71 pub semi_token: Token![;],
72}
73
David Tolnay99383812020-05-04 02:34:33 -070074pub struct TypeAlias {
75 pub type_token: Token![type],
76 pub ident: Ident,
77 pub eq_token: Token![=],
78 pub ty: RustType,
79 pub semi_token: Token![;],
80}
81
David Tolnay16448732020-03-18 12:39:36 -070082pub struct Signature {
83 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -040084 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -070085 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -040086 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -070087 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -070088 pub paren_token: Paren,
89 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -040090}
91
David Tolnay417305a2020-03-18 13:54:00 -070092#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -040093pub struct Var {
94 pub ident: Ident,
95 pub ty: Type,
96}
97
98pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -070099 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700100 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400101 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -0700102 pub var: Token![self],
103 pub ty: Ident,
David Tolnay62d360c2020-04-22 16:26:21 -0700104 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400105}
106
Joel Galensonc03402a2020-04-23 17:31:09 -0700107pub struct Variant {
108 pub ident: Ident,
Joel Galenson88547732020-05-05 08:23:42 -0700109 pub discriminant: u32,
Joel Galensonc03402a2020-04-23 17:31:09 -0700110}
111
David Tolnay7db73692019-10-20 14:51:12 -0400112pub enum Type {
113 Ident(Ident),
114 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700115 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400116 UniquePtr(Box<Ty1>),
117 Ref(Box<Ref>),
118 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700119 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700120 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700121 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700122 Slice(Box<Slice>),
123 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400124}
125
126pub struct Ty1 {
127 pub name: Ident,
128 pub langle: Token![<],
129 pub inner: Type,
130 pub rangle: Token![>],
131}
132
133pub struct Ref {
134 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]>,
137 pub inner: Type,
138}
139
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700140pub struct Slice {
141 pub bracket: Bracket,
142 pub inner: Type,
143}
144
David Tolnay7db73692019-10-20 14:51:12 -0400145#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700146pub enum Lang {
147 Cxx,
148 Rust,
149}