blob: e2356e1bd24bfe67addd4cac251b757a5dfb8921 [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 Tolnay08419302020-04-19 20:38:20 -070014pub mod namespace;
David Tolnay7db73692019-10-20 14:51:12 -040015mod parse;
David Tolnaydf344a82020-05-03 23:23:18 -070016pub mod report;
David Tolnay7db73692019-10-20 14:51:12 -040017pub mod set;
David Tolnay891061b2020-04-19 22:42:33 -070018pub mod symbol;
David Tolnay7db73692019-10-20 14:51:12 -040019mod tokens;
20pub mod types;
21
David Tolnay17e137f2020-05-08 15:55:28 -070022use self::discriminant::Discriminant;
David Tolnaye3a48152020-04-08 19:38:05 -070023use self::parse::kw;
24use proc_macro2::{Ident, Span};
25use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070026use syn::token::{Brace, Bracket, Paren};
David Tolnay91e87fa2020-05-11 19:10:23 -070027use syn::{Expr, Lifetime, Token, Type as RustType};
David Tolnay7db73692019-10-20 14:51:12 -040028
29pub use self::atom::Atom;
David Tolnay64703b42020-05-10 22:12:33 -070030pub use self::derive::Derive;
David Tolnay7db73692019-10-20 14:51:12 -040031pub use self::doc::Doc;
32pub use self::parse::parse_items;
33pub use self::types::Types;
34
35pub enum Api {
David Tolnay91e87fa2020-05-11 19:10:23 -070036 Include(String),
David Tolnay7db73692019-10-20 14:51:12 -040037 Struct(Struct),
Joel Galensonc03402a2020-04-23 17:31:09 -070038 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040039 CxxType(ExternType),
40 CxxFunction(ExternFn),
41 RustType(ExternType),
42 RustFunction(ExternFn),
David Tolnay99383812020-05-04 02:34:33 -070043 TypeAlias(TypeAlias),
David Tolnay7db73692019-10-20 14:51:12 -040044}
45
46pub struct ExternType {
47 pub doc: Doc,
48 pub type_token: Token![type],
49 pub ident: Ident,
David Tolnayc8361022020-08-25 21:57:53 -070050 pub semi_token: Token![;],
David Tolnay00f236a2020-08-29 19:07:18 -070051 pub trusted: bool,
David Tolnay7db73692019-10-20 14:51:12 -040052}
53
54pub struct Struct {
55 pub doc: Doc,
David Tolnaye86b9cf2020-05-10 14:24:29 -070056 pub derives: Vec<Derive>,
David Tolnay7db73692019-10-20 14:51:12 -040057 pub struct_token: Token![struct],
58 pub ident: Ident,
David Tolnay09462ac2020-03-20 14:58:41 -070059 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040060 pub fields: Vec<Var>,
61}
62
Joel Galensonc03402a2020-04-23 17:31:09 -070063pub struct Enum {
64 pub doc: Doc,
65 pub enum_token: Token![enum],
66 pub ident: Ident,
67 pub brace_token: Brace,
68 pub variants: Vec<Variant>,
David Tolnaye2e303f2020-05-10 20:52:00 -070069 pub repr: Atom,
Joel Galensonc03402a2020-04-23 17:31:09 -070070}
71
David Tolnay7db73692019-10-20 14:51:12 -040072pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070073 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040074 pub doc: Doc,
David Tolnay7db73692019-10-20 14:51:12 -040075 pub ident: Ident,
David Tolnay16448732020-03-18 12:39:36 -070076 pub sig: Signature,
77 pub semi_token: Token![;],
78}
79
David Tolnay99383812020-05-04 02:34:33 -070080pub struct TypeAlias {
81 pub type_token: Token![type],
82 pub ident: Ident,
83 pub eq_token: Token![=],
84 pub ty: RustType,
85 pub semi_token: Token![;],
86}
87
David Tolnay16448732020-03-18 12:39:36 -070088pub struct Signature {
89 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -040090 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -070091 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -040092 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -070093 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -070094 pub paren_token: Paren,
95 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -040096}
97
David Tolnay417305a2020-03-18 13:54:00 -070098#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -040099pub struct Var {
100 pub ident: Ident,
101 pub ty: Type,
102}
103
104pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -0700105 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700106 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400107 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -0700108 pub var: Token![self],
109 pub ty: Ident,
David Tolnay62d360c2020-04-22 16:26:21 -0700110 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400111}
112
Joel Galensonc03402a2020-04-23 17:31:09 -0700113pub struct Variant {
114 pub ident: Ident,
David Tolnay17e137f2020-05-08 15:55:28 -0700115 pub discriminant: Discriminant,
David Tolnay2b8bf6d2020-05-10 17:37:16 -0700116 pub expr: Option<Expr>,
Joel Galensonc03402a2020-04-23 17:31:09 -0700117}
118
David Tolnay7db73692019-10-20 14:51:12 -0400119pub enum Type {
120 Ident(Ident),
121 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700122 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400123 UniquePtr(Box<Ty1>),
124 Ref(Box<Ref>),
125 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700126 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700127 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700128 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700129 Slice(Box<Slice>),
130 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400131}
132
133pub struct Ty1 {
134 pub name: Ident,
135 pub langle: Token![<],
136 pub inner: Type,
137 pub rangle: Token![>],
138}
139
140pub struct Ref {
141 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700142 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400143 pub mutability: Option<Token![mut]>,
144 pub inner: Type,
145}
146
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700147pub struct Slice {
148 pub bracket: Bracket,
149 pub inner: Type,
150}
151
David Tolnay7db73692019-10-20 14:51:12 -0400152#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700153pub enum Lang {
154 Cxx,
155 Rust,
156}