blob: 9b1a7e5527ddb971271df70a5715071c580f7475 [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;
6mod doc;
7pub mod error;
8pub mod ident;
9mod impls;
David Tolnay3caa50a2020-04-19 21:25:34 -070010pub mod mangle;
David Tolnay08419302020-04-19 20:38:20 -070011pub mod namespace;
David Tolnay7db73692019-10-20 14:51:12 -040012mod parse;
13pub mod set;
David Tolnay891061b2020-04-19 22:42:33 -070014pub mod symbol;
David Tolnay7db73692019-10-20 14:51:12 -040015mod tokens;
16pub mod types;
17
David Tolnaye3a48152020-04-08 19:38:05 -070018use self::parse::kw;
19use proc_macro2::{Ident, Span};
20use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070021use syn::token::{Brace, Bracket, Paren};
David Tolnay0bd50fa2020-04-22 15:31:33 -070022use syn::{Lifetime, LitStr, Token};
David Tolnay7db73692019-10-20 14:51:12 -040023
24pub use self::atom::Atom;
25pub use self::doc::Doc;
26pub use self::parse::parse_items;
27pub use self::types::Types;
28
29pub enum Api {
30 Include(LitStr),
31 Struct(Struct),
Joel Galensonc03402a2020-04-23 17:31:09 -070032 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040033 CxxType(ExternType),
34 CxxFunction(ExternFn),
35 RustType(ExternType),
36 RustFunction(ExternFn),
37}
38
39pub struct ExternType {
40 pub doc: Doc,
41 pub type_token: Token![type],
42 pub ident: Ident,
43}
44
45pub struct Struct {
46 pub doc: Doc,
47 pub derives: Vec<Ident>,
48 pub struct_token: Token![struct],
49 pub ident: Ident,
David Tolnay09462ac2020-03-20 14:58:41 -070050 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040051 pub fields: Vec<Var>,
52}
53
Joel Galensonc03402a2020-04-23 17:31:09 -070054pub struct Enum {
55 pub doc: Doc,
56 pub enum_token: Token![enum],
57 pub ident: Ident,
58 pub brace_token: Brace,
59 pub variants: Vec<Variant>,
60}
61
David Tolnay7db73692019-10-20 14:51:12 -040062pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070063 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040064 pub doc: Doc,
David Tolnay7db73692019-10-20 14:51:12 -040065 pub ident: Ident,
David Tolnay16448732020-03-18 12:39:36 -070066 pub sig: Signature,
67 pub semi_token: Token![;],
68}
69
70pub struct Signature {
71 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -040072 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -070073 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -040074 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -070075 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -070076 pub paren_token: Paren,
77 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -040078}
79
David Tolnay417305a2020-03-18 13:54:00 -070080#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -040081pub struct Var {
82 pub ident: Ident,
83 pub ty: Type,
84}
85
86pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -070087 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -070088 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -040089 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -070090 pub var: Token![self],
91 pub ty: Ident,
David Tolnay62d360c2020-04-22 16:26:21 -070092 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -040093}
94
Joel Galensonc03402a2020-04-23 17:31:09 -070095pub struct Variant {
96 pub ident: Ident,
97 pub discriminant: Option<u32>,
98}
99
David Tolnay7db73692019-10-20 14:51:12 -0400100pub enum Type {
101 Ident(Ident),
102 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700103 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400104 UniquePtr(Box<Ty1>),
105 Ref(Box<Ref>),
106 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700107 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700108 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700109 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700110 Slice(Box<Slice>),
111 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400112}
113
114pub struct Ty1 {
115 pub name: Ident,
116 pub langle: Token![<],
117 pub inner: Type,
118 pub rangle: Token![>],
119}
120
121pub struct Ref {
122 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700123 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400124 pub mutability: Option<Token![mut]>,
125 pub inner: Type,
126}
127
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700128pub struct Slice {
129 pub bracket: Bracket,
130 pub inner: Type,
131}
132
David Tolnay7db73692019-10-20 14:51:12 -0400133#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700134pub enum Lang {
135 Cxx,
136 Rust,
137}
138
139#[derive(Copy, Clone, PartialEq)]
David Tolnay7db73692019-10-20 14:51:12 -0400140pub enum Derive {
141 Clone,
142 Copy,
143}