blob: cd6f2359ee01e406182283578d6b41748dd98700 [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;
10pub mod ident;
11mod impls;
David Tolnay3caa50a2020-04-19 21:25:34 -070012pub mod mangle;
David Tolnay08419302020-04-19 20:38:20 -070013pub mod namespace;
David Tolnay7db73692019-10-20 14:51:12 -040014mod parse;
David Tolnaydf344a82020-05-03 23:23:18 -070015pub mod report;
David Tolnay7db73692019-10-20 14:51:12 -040016pub mod set;
David Tolnay891061b2020-04-19 22:42:33 -070017pub mod symbol;
David Tolnay7db73692019-10-20 14:51:12 -040018mod tokens;
19pub mod types;
20
David Tolnay17e137f2020-05-08 15:55:28 -070021use self::discriminant::Discriminant;
David Tolnaye3a48152020-04-08 19:38:05 -070022use self::parse::kw;
23use proc_macro2::{Ident, Span};
24use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070025use syn::token::{Brace, Bracket, Paren};
David Tolnay91e87fa2020-05-11 19:10:23 -070026use syn::{Expr, Lifetime, Token, Type as RustType};
David Tolnay7db73692019-10-20 14:51:12 -040027
28pub use self::atom::Atom;
David Tolnay64703b42020-05-10 22:12:33 -070029pub use self::derive::Derive;
David Tolnay7db73692019-10-20 14:51:12 -040030pub use self::doc::Doc;
31pub use self::parse::parse_items;
32pub use self::types::Types;
33
34pub enum Api {
David Tolnay91e87fa2020-05-11 19:10:23 -070035 Include(String),
David Tolnay7db73692019-10-20 14:51:12 -040036 Struct(Struct),
Joel Galensonc03402a2020-04-23 17:31:09 -070037 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040038 CxxType(ExternType),
39 CxxFunction(ExternFn),
40 RustType(ExternType),
41 RustFunction(ExternFn),
David Tolnay99383812020-05-04 02:34:33 -070042 TypeAlias(TypeAlias),
David Tolnay7db73692019-10-20 14:51:12 -040043}
44
45pub struct ExternType {
46 pub doc: Doc,
47 pub type_token: Token![type],
48 pub ident: Ident,
David Tolnayc8361022020-08-25 21:57:53 -070049 pub semi_token: Token![;],
David Tolnay7db73692019-10-20 14:51:12 -040050}
51
52pub struct Struct {
53 pub doc: Doc,
David Tolnaye86b9cf2020-05-10 14:24:29 -070054 pub derives: Vec<Derive>,
David Tolnay7db73692019-10-20 14:51:12 -040055 pub struct_token: Token![struct],
56 pub ident: Ident,
David Tolnay09462ac2020-03-20 14:58:41 -070057 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040058 pub fields: Vec<Var>,
59}
60
Joel Galensonc03402a2020-04-23 17:31:09 -070061pub struct Enum {
62 pub doc: Doc,
63 pub enum_token: Token![enum],
64 pub ident: Ident,
65 pub brace_token: Brace,
66 pub variants: Vec<Variant>,
David Tolnaye2e303f2020-05-10 20:52:00 -070067 pub repr: Atom,
Joel Galensonc03402a2020-04-23 17:31:09 -070068}
69
David Tolnay7db73692019-10-20 14:51:12 -040070pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070071 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040072 pub doc: Doc,
David Tolnay7db73692019-10-20 14:51:12 -040073 pub ident: Ident,
David Tolnay16448732020-03-18 12:39:36 -070074 pub sig: Signature,
75 pub semi_token: Token![;],
76}
77
David Tolnay99383812020-05-04 02:34:33 -070078pub struct TypeAlias {
79 pub type_token: Token![type],
80 pub ident: Ident,
81 pub eq_token: Token![=],
82 pub ty: RustType,
83 pub semi_token: Token![;],
84}
85
David Tolnay16448732020-03-18 12:39:36 -070086pub struct Signature {
87 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -040088 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -070089 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -040090 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -070091 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -070092 pub paren_token: Paren,
93 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -040094}
95
David Tolnay417305a2020-03-18 13:54:00 -070096#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -040097pub struct Var {
98 pub ident: Ident,
99 pub ty: Type,
100}
101
102pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -0700103 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700104 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400105 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -0700106 pub var: Token![self],
107 pub ty: Ident,
David Tolnay62d360c2020-04-22 16:26:21 -0700108 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400109}
110
Joel Galensonc03402a2020-04-23 17:31:09 -0700111pub struct Variant {
112 pub ident: Ident,
David Tolnay17e137f2020-05-08 15:55:28 -0700113 pub discriminant: Discriminant,
David Tolnay2b8bf6d2020-05-10 17:37:16 -0700114 pub expr: Option<Expr>,
Joel Galensonc03402a2020-04-23 17:31:09 -0700115}
116
David Tolnay7db73692019-10-20 14:51:12 -0400117pub enum Type {
118 Ident(Ident),
119 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700120 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400121 UniquePtr(Box<Ty1>),
122 Ref(Box<Ref>),
123 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700124 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700125 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700126 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700127 Slice(Box<Slice>),
128 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400129}
130
131pub struct Ty1 {
132 pub name: Ident,
133 pub langle: Token![<],
134 pub inner: Type,
135 pub rangle: Token![>],
136}
137
138pub struct Ref {
139 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700140 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400141 pub mutability: Option<Token![mut]>,
142 pub inner: Type,
143}
144
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700145pub struct Slice {
146 pub bracket: Bracket,
147 pub inner: Type,
148}
149
David Tolnay7db73692019-10-20 14:51:12 -0400150#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700151pub enum Lang {
152 Cxx,
153 Rust,
154}