blob: e6c5bb8c5f71c99bdc39f62cacdca3e5ac552714 [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;
David Tolnaydf344a82020-05-03 23:23:18 -070013pub mod report;
David Tolnay7db73692019-10-20 14:51:12 -040014pub mod set;
David Tolnay891061b2020-04-19 22:42:33 -070015pub mod symbol;
David Tolnay7db73692019-10-20 14:51:12 -040016mod tokens;
17pub mod types;
18
David Tolnaye3a48152020-04-08 19:38:05 -070019use self::parse::kw;
20use proc_macro2::{Ident, Span};
21use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070022use syn::token::{Brace, Bracket, Paren};
David Tolnay0bd50fa2020-04-22 15:31:33 -070023use syn::{Lifetime, LitStr, Token};
David Tolnay7db73692019-10-20 14:51:12 -040024
25pub use self::atom::Atom;
26pub use self::doc::Doc;
27pub use self::parse::parse_items;
28pub use self::types::Types;
29
30pub enum Api {
31 Include(LitStr),
32 Struct(Struct),
Joel Galensonc03402a2020-04-23 17:31:09 -070033 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040034 CxxType(ExternType),
35 CxxFunction(ExternFn),
36 RustType(ExternType),
37 RustFunction(ExternFn),
38}
39
40pub struct ExternType {
41 pub doc: Doc,
42 pub type_token: Token![type],
43 pub ident: Ident,
44}
45
46pub struct Struct {
47 pub doc: Doc,
48 pub derives: Vec<Ident>,
49 pub struct_token: Token![struct],
50 pub ident: Ident,
David Tolnay09462ac2020-03-20 14:58:41 -070051 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040052 pub fields: Vec<Var>,
53}
54
Joel Galensonc03402a2020-04-23 17:31:09 -070055pub struct Enum {
56 pub doc: Doc,
57 pub enum_token: Token![enum],
58 pub ident: Ident,
59 pub brace_token: Brace,
60 pub variants: Vec<Variant>,
61}
62
David Tolnay7db73692019-10-20 14:51:12 -040063pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070064 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040065 pub doc: Doc,
David Tolnay7db73692019-10-20 14:51:12 -040066 pub ident: Ident,
David Tolnay16448732020-03-18 12:39:36 -070067 pub sig: Signature,
68 pub semi_token: Token![;],
69}
70
71pub struct Signature {
72 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -040073 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -070074 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -040075 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -070076 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -070077 pub paren_token: Paren,
78 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -040079}
80
David Tolnay417305a2020-03-18 13:54:00 -070081#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -040082pub struct Var {
83 pub ident: Ident,
84 pub ty: Type,
85}
86
87pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -070088 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -070089 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -040090 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -070091 pub var: Token![self],
92 pub ty: Ident,
David Tolnay62d360c2020-04-22 16:26:21 -070093 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -040094}
95
Joel Galensonc03402a2020-04-23 17:31:09 -070096pub struct Variant {
97 pub ident: Ident,
98 pub discriminant: Option<u32>,
99}
100
David Tolnay7db73692019-10-20 14:51:12 -0400101pub enum Type {
102 Ident(Ident),
103 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700104 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400105 UniquePtr(Box<Ty1>),
106 Ref(Box<Ref>),
107 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700108 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700109 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700110 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700111 Slice(Box<Slice>),
112 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400113}
114
115pub struct Ty1 {
116 pub name: Ident,
117 pub langle: Token![<],
118 pub inner: Type,
119 pub rangle: Token![>],
120}
121
122pub struct Ref {
123 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700124 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400125 pub mutability: Option<Token![mut]>,
126 pub inner: Type,
127}
128
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700129pub struct Slice {
130 pub bracket: Bracket,
131 pub inner: Type,
132}
133
David Tolnay7db73692019-10-20 14:51:12 -0400134#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700135pub enum Lang {
136 Cxx,
137 Rust,
138}
139
140#[derive(Copy, Clone, PartialEq)]
David Tolnay7db73692019-10-20 14:51:12 -0400141pub enum Derive {
142 Clone,
143 Copy,
144}