blob: 180c40a04af71a22b47bbfb0714a4ef2762dde8d [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 Tolnaya8d94a12020-09-06 23:28:18 -070016pub mod qualified;
David Tolnaydf344a82020-05-03 23:23:18 -070017pub mod report;
David Tolnay7db73692019-10-20 14:51:12 -040018pub mod set;
David Tolnay891061b2020-04-19 22:42:33 -070019pub mod symbol;
David Tolnay7db73692019-10-20 14:51:12 -040020mod tokens;
21pub mod types;
22
David Tolnay17e137f2020-05-08 15:55:28 -070023use self::discriminant::Discriminant;
David Tolnaye3a48152020-04-08 19:38:05 -070024use self::parse::kw;
25use proc_macro2::{Ident, Span};
26use syn::punctuated::Punctuated;
Adrian Taylorf5dd5522020-04-13 16:50:14 -070027use syn::token::{Brace, Bracket, Paren};
David Tolnay91e87fa2020-05-11 19:10:23 -070028use syn::{Expr, Lifetime, Token, Type as RustType};
David Tolnay7db73692019-10-20 14:51:12 -040029
30pub use self::atom::Atom;
David Tolnay64703b42020-05-10 22:12:33 -070031pub use self::derive::Derive;
David Tolnay7db73692019-10-20 14:51:12 -040032pub use self::doc::Doc;
33pub use self::parse::parse_items;
34pub use self::types::Types;
35
36pub enum Api {
David Tolnayb0cd3272020-10-27 21:32:54 -070037 Include(Include),
David Tolnay7db73692019-10-20 14:51:12 -040038 Struct(Struct),
Joel Galensonc03402a2020-04-23 17:31:09 -070039 Enum(Enum),
David Tolnay7db73692019-10-20 14:51:12 -040040 CxxType(ExternType),
41 CxxFunction(ExternFn),
42 RustType(ExternType),
43 RustFunction(ExternFn),
David Tolnay99383812020-05-04 02:34:33 -070044 TypeAlias(TypeAlias),
David Tolnay7e69f892020-10-03 22:20:22 -070045 Impl(Impl),
David Tolnay7db73692019-10-20 14:51:12 -040046}
47
David Tolnayb0cd3272020-10-27 21:32:54 -070048pub struct Include {
49 pub path: String,
50 pub kind: IncludeKind,
51 pub begin_span: Span,
52 pub end_span: Span,
53}
54
David Tolnay700cd0c2020-10-28 12:40:27 -070055/// Whether to emit `#include "path"` or `#include <path>`.
56#[derive(Copy, Clone, PartialEq, Debug)]
David Tolnayb0cd3272020-10-27 21:32:54 -070057pub enum IncludeKind {
David Tolnay700cd0c2020-10-28 12:40:27 -070058 /// `#include "quoted/path/to"`
59 Quoted,
60 /// `#include <bracketed/path/to>`
61 Bracketed,
David Tolnayb0cd3272020-10-27 21:32:54 -070062}
63
David Tolnay7db73692019-10-20 14:51:12 -040064pub struct ExternType {
65 pub doc: Doc,
66 pub type_token: Token![type],
67 pub ident: Ident,
David Tolnayc8361022020-08-25 21:57:53 -070068 pub semi_token: Token![;],
David Tolnay00f236a2020-08-29 19:07:18 -070069 pub trusted: bool,
David Tolnay7db73692019-10-20 14:51:12 -040070}
71
72pub struct Struct {
73 pub doc: Doc,
David Tolnaye86b9cf2020-05-10 14:24:29 -070074 pub derives: Vec<Derive>,
David Tolnay7db73692019-10-20 14:51:12 -040075 pub struct_token: Token![struct],
76 pub ident: Ident,
David Tolnay09462ac2020-03-20 14:58:41 -070077 pub brace_token: Brace,
David Tolnay7db73692019-10-20 14:51:12 -040078 pub fields: Vec<Var>,
79}
80
Joel Galensonc03402a2020-04-23 17:31:09 -070081pub struct Enum {
82 pub doc: Doc,
83 pub enum_token: Token![enum],
84 pub ident: Ident,
85 pub brace_token: Brace,
86 pub variants: Vec<Variant>,
David Tolnaye2e303f2020-05-10 20:52:00 -070087 pub repr: Atom,
Joel Galensonc03402a2020-04-23 17:31:09 -070088}
89
David Tolnaya4641c72020-09-08 14:05:53 -070090pub struct Pair {
91 pub cxx: Ident,
92 pub rust: Ident,
93}
94
David Tolnay7db73692019-10-20 14:51:12 -040095pub struct ExternFn {
David Tolnay6cde49f2020-03-16 12:25:45 -070096 pub lang: Lang,
David Tolnay7db73692019-10-20 14:51:12 -040097 pub doc: Doc,
David Tolnaya4641c72020-09-08 14:05:53 -070098 pub ident: Pair,
David Tolnay16448732020-03-18 12:39:36 -070099 pub sig: Signature,
100 pub semi_token: Token![;],
101}
102
David Tolnay99383812020-05-04 02:34:33 -0700103pub struct TypeAlias {
Bryan Henry890083d2020-09-13 10:34:31 -0700104 pub doc: Doc,
David Tolnay99383812020-05-04 02:34:33 -0700105 pub type_token: Token![type],
106 pub ident: Ident,
107 pub eq_token: Token![=],
108 pub ty: RustType,
109 pub semi_token: Token![;],
110}
111
David Tolnay7e69f892020-10-03 22:20:22 -0700112pub struct Impl {
113 pub impl_token: Token![impl],
114 pub ty: Type,
115 pub brace_token: Brace,
116}
117
David Tolnay16448732020-03-18 12:39:36 -0700118pub struct Signature {
David Tolnaybdb576c2020-09-06 23:45:55 -0700119 pub unsafety: Option<Token![unsafe]>,
David Tolnay16448732020-03-18 12:39:36 -0700120 pub fn_token: Token![fn],
David Tolnay7db73692019-10-20 14:51:12 -0400121 pub receiver: Option<Receiver>,
David Tolnaye3a48152020-04-08 19:38:05 -0700122 pub args: Punctuated<Var, Token![,]>,
David Tolnay7db73692019-10-20 14:51:12 -0400123 pub ret: Option<Type>,
David Tolnay59b7ede2020-03-16 00:30:23 -0700124 pub throws: bool,
David Tolnaye3a48152020-04-08 19:38:05 -0700125 pub paren_token: Paren,
126 pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
David Tolnay7db73692019-10-20 14:51:12 -0400127}
128
David Tolnay417305a2020-03-18 13:54:00 -0700129#[derive(Eq, PartialEq, Hash)]
David Tolnay7db73692019-10-20 14:51:12 -0400130pub struct Var {
131 pub ident: Ident,
132 pub ty: Type,
133}
134
135pub struct Receiver {
David Tolnayfb6e3862020-04-20 01:33:23 -0700136 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700137 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400138 pub mutability: Option<Token![mut]>,
David Tolnay05e11cc2020-04-20 02:13:56 -0700139 pub var: Token![self],
140 pub ty: Ident,
David Tolnay62d360c2020-04-22 16:26:21 -0700141 pub shorthand: bool,
David Tolnay7db73692019-10-20 14:51:12 -0400142}
143
Joel Galensonc03402a2020-04-23 17:31:09 -0700144pub struct Variant {
145 pub ident: Ident,
David Tolnay17e137f2020-05-08 15:55:28 -0700146 pub discriminant: Discriminant,
David Tolnay2b8bf6d2020-05-10 17:37:16 -0700147 pub expr: Option<Expr>,
Joel Galensonc03402a2020-04-23 17:31:09 -0700148}
149
David Tolnay7db73692019-10-20 14:51:12 -0400150pub enum Type {
151 Ident(Ident),
152 RustBox(Box<Ty1>),
Myron Ahneba35cf2020-02-05 19:41:51 +0700153 RustVec(Box<Ty1>),
David Tolnay7db73692019-10-20 14:51:12 -0400154 UniquePtr(Box<Ty1>),
155 Ref(Box<Ref>),
156 Str(Box<Ref>),
David Tolnay4377a9e2020-04-24 15:20:26 -0700157 CxxVector(Box<Ty1>),
David Tolnay417305a2020-03-18 13:54:00 -0700158 Fn(Box<Signature>),
David Tolnay2fb14e92020-03-15 23:11:38 -0700159 Void(Span),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700160 Slice(Box<Slice>),
161 SliceRefU8(Box<Ref>),
David Tolnay7db73692019-10-20 14:51:12 -0400162}
163
164pub struct Ty1 {
165 pub name: Ident,
166 pub langle: Token![<],
167 pub inner: Type,
168 pub rangle: Token![>],
169}
170
171pub struct Ref {
172 pub ampersand: Token![&],
David Tolnay0bd50fa2020-04-22 15:31:33 -0700173 pub lifetime: Option<Lifetime>,
David Tolnay7db73692019-10-20 14:51:12 -0400174 pub mutability: Option<Token![mut]>,
175 pub inner: Type,
176}
177
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700178pub struct Slice {
179 pub bracket: Bracket,
180 pub inner: Type,
181}
182
David Tolnay7db73692019-10-20 14:51:12 -0400183#[derive(Copy, Clone, PartialEq)]
David Tolnay6cde49f2020-03-16 12:25:45 -0700184pub enum Lang {
185 Cxx,
186 Rust,
187}