• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Functionality that is shared between the cxxbridge macro and the cmd.
2 
3 pub mod atom;
4 pub mod attrs;
5 pub mod check;
6 pub mod derive;
7 mod discriminant;
8 mod doc;
9 pub mod error;
10 pub mod file;
11 pub mod ident;
12 mod impls;
13 mod improper;
14 pub mod instantiate;
15 pub mod mangle;
16 pub mod map;
17 mod names;
18 pub mod namespace;
19 mod parse;
20 mod pod;
21 pub mod qualified;
22 pub mod report;
23 pub mod resolve;
24 pub mod set;
25 pub mod symbol;
26 mod tokens;
27 mod toposort;
28 pub mod trivial;
29 pub mod types;
30 mod visit;
31 
32 use self::attrs::OtherAttrs;
33 use self::discriminant::Discriminant;
34 use self::namespace::Namespace;
35 use self::parse::kw;
36 use self::symbol::Symbol;
37 use proc_macro2::{Ident, Span};
38 use syn::punctuated::Punctuated;
39 use syn::token::{Brace, Bracket, Paren};
40 use syn::{Expr, Generics, Lifetime, LitInt, Token, Type as RustType};
41 
42 pub use self::atom::Atom;
43 pub use self::derive::{Derive, Trait};
44 pub use self::doc::Doc;
45 pub use self::names::ForeignName;
46 pub use self::parse::parse_items;
47 pub use self::types::Types;
48 
49 pub enum Api {
50     Include(Include),
51     Struct(Struct),
52     Enum(Enum),
53     CxxType(ExternType),
54     CxxFunction(ExternFn),
55     RustType(ExternType),
56     RustFunction(ExternFn),
57     TypeAlias(TypeAlias),
58     Impl(Impl),
59 }
60 
61 pub struct Include {
62     pub path: String,
63     pub kind: IncludeKind,
64     pub begin_span: Span,
65     pub end_span: Span,
66 }
67 
68 /// Whether to emit `#include "path"` or `#include <path>`.
69 #[derive(Copy, Clone, PartialEq, Debug)]
70 pub enum IncludeKind {
71     /// `#include "quoted/path/to"`
72     Quoted,
73     /// `#include <bracketed/path/to>`
74     Bracketed,
75 }
76 
77 pub struct ExternType {
78     pub lang: Lang,
79     pub doc: Doc,
80     pub derives: Vec<Derive>,
81     pub attrs: OtherAttrs,
82     pub visibility: Token![pub],
83     pub type_token: Token![type],
84     pub name: Pair,
85     pub generics: Lifetimes,
86     pub colon_token: Option<Token![:]>,
87     pub bounds: Vec<Derive>,
88     pub semi_token: Token![;],
89     pub trusted: bool,
90 }
91 
92 pub struct Struct {
93     pub doc: Doc,
94     pub derives: Vec<Derive>,
95     pub attrs: OtherAttrs,
96     pub visibility: Token![pub],
97     pub struct_token: Token![struct],
98     pub name: Pair,
99     pub generics: Lifetimes,
100     pub brace_token: Brace,
101     pub fields: Vec<Var>,
102 }
103 
104 pub struct Enum {
105     pub doc: Doc,
106     pub derives: Vec<Derive>,
107     pub attrs: OtherAttrs,
108     pub visibility: Token![pub],
109     pub enum_token: Token![enum],
110     pub name: Pair,
111     pub generics: Lifetimes,
112     pub brace_token: Brace,
113     pub variants: Vec<Variant>,
114     pub repr: Atom,
115     pub repr_type: Type,
116     pub explicit_repr: bool,
117 }
118 
119 pub struct ExternFn {
120     pub lang: Lang,
121     pub doc: Doc,
122     pub attrs: OtherAttrs,
123     pub visibility: Token![pub],
124     pub name: Pair,
125     pub sig: Signature,
126     pub semi_token: Token![;],
127     pub trusted: bool,
128 }
129 
130 pub struct TypeAlias {
131     pub doc: Doc,
132     pub derives: Vec<Derive>,
133     pub attrs: OtherAttrs,
134     pub visibility: Token![pub],
135     pub type_token: Token![type],
136     pub name: Pair,
137     pub generics: Lifetimes,
138     pub eq_token: Token![=],
139     pub ty: RustType,
140     pub semi_token: Token![;],
141 }
142 
143 pub struct Impl {
144     pub impl_token: Token![impl],
145     pub impl_generics: Lifetimes,
146     pub negative: bool,
147     pub ty: Type,
148     pub ty_generics: Lifetimes,
149     pub brace_token: Brace,
150     pub negative_token: Option<Token![!]>,
151 }
152 
153 #[derive(Clone, Default)]
154 pub struct Lifetimes {
155     pub lt_token: Option<Token![<]>,
156     pub lifetimes: Punctuated<Lifetime, Token![,]>,
157     pub gt_token: Option<Token![>]>,
158 }
159 
160 pub struct Signature {
161     pub unsafety: Option<Token![unsafe]>,
162     pub fn_token: Token![fn],
163     pub generics: Generics,
164     pub receiver: Option<Receiver>,
165     pub args: Punctuated<Var, Token![,]>,
166     pub ret: Option<Type>,
167     pub throws: bool,
168     pub paren_token: Paren,
169     pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
170 }
171 
172 pub struct Var {
173     pub doc: Doc,
174     pub attrs: OtherAttrs,
175     pub visibility: Token![pub],
176     pub name: Pair,
177     pub ty: Type,
178 }
179 
180 pub struct Receiver {
181     pub pinned: bool,
182     pub ampersand: Token![&],
183     pub lifetime: Option<Lifetime>,
184     pub mutable: bool,
185     pub var: Token![self],
186     pub ty: NamedType,
187     pub shorthand: bool,
188     pub pin_tokens: Option<(kw::Pin, Token![<], Token![>])>,
189     pub mutability: Option<Token![mut]>,
190 }
191 
192 pub struct Variant {
193     pub doc: Doc,
194     pub attrs: OtherAttrs,
195     pub name: Pair,
196     pub discriminant: Discriminant,
197     pub expr: Option<Expr>,
198 }
199 
200 pub enum Type {
201     Ident(NamedType),
202     RustBox(Box<Ty1>),
203     RustVec(Box<Ty1>),
204     UniquePtr(Box<Ty1>),
205     SharedPtr(Box<Ty1>),
206     WeakPtr(Box<Ty1>),
207     Ref(Box<Ref>),
208     Ptr(Box<Ptr>),
209     Str(Box<Ref>),
210     CxxVector(Box<Ty1>),
211     Fn(Box<Signature>),
212     Void(Span),
213     SliceRef(Box<SliceRef>),
214     Array(Box<Array>),
215 }
216 
217 pub struct Ty1 {
218     pub name: Ident,
219     pub langle: Token![<],
220     pub inner: Type,
221     pub rangle: Token![>],
222 }
223 
224 pub struct Ref {
225     pub pinned: bool,
226     pub ampersand: Token![&],
227     pub lifetime: Option<Lifetime>,
228     pub mutable: bool,
229     pub inner: Type,
230     pub pin_tokens: Option<(kw::Pin, Token![<], Token![>])>,
231     pub mutability: Option<Token![mut]>,
232 }
233 
234 pub struct Ptr {
235     pub star: Token![*],
236     pub mutable: bool,
237     pub inner: Type,
238     pub mutability: Option<Token![mut]>,
239     pub constness: Option<Token![const]>,
240 }
241 
242 pub struct SliceRef {
243     pub ampersand: Token![&],
244     pub lifetime: Option<Lifetime>,
245     pub mutable: bool,
246     pub bracket: Bracket,
247     pub inner: Type,
248     pub mutability: Option<Token![mut]>,
249 }
250 
251 pub struct Array {
252     pub bracket: Bracket,
253     pub inner: Type,
254     pub semi_token: Token![;],
255     pub len: usize,
256     pub len_token: LitInt,
257 }
258 
259 #[derive(Copy, Clone, PartialEq)]
260 pub enum Lang {
261     Cxx,
262     Rust,
263 }
264 
265 // An association of a defined Rust name with a fully resolved, namespace
266 // qualified C++ name.
267 #[derive(Clone)]
268 pub struct Pair {
269     pub namespace: Namespace,
270     pub cxx: ForeignName,
271     pub rust: Ident,
272 }
273 
274 // Wrapper for a type which needs to be resolved before it can be printed in
275 // C++.
276 #[derive(PartialEq, Eq, Hash)]
277 pub struct NamedType {
278     pub rust: Ident,
279     pub generics: Lifetimes,
280 }
281