• 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::namespace::Namespace;
34 use self::parse::kw;
35 use self::symbol::Symbol;
36 use proc_macro2::{Ident, Span};
37 use syn::punctuated::Punctuated;
38 use syn::token::{Brace, Bracket, Paren};
39 use syn::{Attribute, Expr, Generics, Lifetime, LitInt, Path, Token, Type as RustType};
40 
41 pub use self::atom::Atom;
42 pub use self::derive::{Derive, Trait};
43 pub use self::discriminant::Discriminant;
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 variants_from_header: bool,
115     pub variants_from_header_attr: Option<Attribute>,
116     pub repr: EnumRepr,
117     pub explicit_repr: bool,
118 }
119 
120 pub enum EnumRepr {
121     Native { atom: Atom, repr_type: Type },
122     Foreign { rust_type: Path },
123 }
124 
125 pub struct ExternFn {
126     pub lang: Lang,
127     pub doc: Doc,
128     pub attrs: OtherAttrs,
129     pub visibility: Token![pub],
130     pub name: Pair,
131     pub sig: Signature,
132     pub semi_token: Token![;],
133     pub trusted: bool,
134 }
135 
136 pub struct TypeAlias {
137     pub doc: Doc,
138     pub derives: Vec<Derive>,
139     pub attrs: OtherAttrs,
140     pub visibility: Token![pub],
141     pub type_token: Token![type],
142     pub name: Pair,
143     pub generics: Lifetimes,
144     pub eq_token: Token![=],
145     pub ty: RustType,
146     pub semi_token: Token![;],
147 }
148 
149 pub struct Impl {
150     pub impl_token: Token![impl],
151     pub impl_generics: Lifetimes,
152     pub negative: bool,
153     pub ty: Type,
154     pub ty_generics: Lifetimes,
155     pub brace_token: Brace,
156     pub negative_token: Option<Token![!]>,
157 }
158 
159 #[derive(Clone, Default)]
160 pub struct Lifetimes {
161     pub lt_token: Option<Token![<]>,
162     pub lifetimes: Punctuated<Lifetime, Token![,]>,
163     pub gt_token: Option<Token![>]>,
164 }
165 
166 pub struct Signature {
167     pub unsafety: Option<Token![unsafe]>,
168     pub fn_token: Token![fn],
169     pub generics: Generics,
170     pub receiver: Option<Receiver>,
171     pub args: Punctuated<Var, Token![,]>,
172     pub ret: Option<Type>,
173     pub throws: bool,
174     pub paren_token: Paren,
175     pub throws_tokens: Option<(kw::Result, Token![<], Token![>])>,
176 }
177 
178 pub struct Var {
179     pub doc: Doc,
180     pub attrs: OtherAttrs,
181     pub visibility: Token![pub],
182     pub name: Pair,
183     pub colon_token: Token![:],
184     pub ty: Type,
185 }
186 
187 pub struct Receiver {
188     pub pinned: bool,
189     pub ampersand: Token![&],
190     pub lifetime: Option<Lifetime>,
191     pub mutable: bool,
192     pub var: Token![self],
193     pub ty: NamedType,
194     pub colon_token: Token![:],
195     pub shorthand: bool,
196     pub pin_tokens: Option<(kw::Pin, Token![<], Token![>])>,
197     pub mutability: Option<Token![mut]>,
198 }
199 
200 pub struct Variant {
201     pub doc: Doc,
202     pub attrs: OtherAttrs,
203     pub name: Pair,
204     pub discriminant: Discriminant,
205     pub expr: Option<Expr>,
206 }
207 
208 pub enum Type {
209     Ident(NamedType),
210     RustBox(Box<Ty1>),
211     RustVec(Box<Ty1>),
212     UniquePtr(Box<Ty1>),
213     SharedPtr(Box<Ty1>),
214     WeakPtr(Box<Ty1>),
215     Ref(Box<Ref>),
216     Ptr(Box<Ptr>),
217     Str(Box<Ref>),
218     CxxVector(Box<Ty1>),
219     Fn(Box<Signature>),
220     Void(Span),
221     SliceRef(Box<SliceRef>),
222     Array(Box<Array>),
223 }
224 
225 pub struct Ty1 {
226     pub name: Ident,
227     pub langle: Token![<],
228     pub inner: Type,
229     pub rangle: Token![>],
230 }
231 
232 pub struct Ref {
233     pub pinned: bool,
234     pub ampersand: Token![&],
235     pub lifetime: Option<Lifetime>,
236     pub mutable: bool,
237     pub inner: Type,
238     pub pin_tokens: Option<(kw::Pin, Token![<], Token![>])>,
239     pub mutability: Option<Token![mut]>,
240 }
241 
242 pub struct Ptr {
243     pub star: Token![*],
244     pub mutable: bool,
245     pub inner: Type,
246     pub mutability: Option<Token![mut]>,
247     pub constness: Option<Token![const]>,
248 }
249 
250 pub struct SliceRef {
251     pub ampersand: Token![&],
252     pub lifetime: Option<Lifetime>,
253     pub mutable: bool,
254     pub bracket: Bracket,
255     pub inner: Type,
256     pub mutability: Option<Token![mut]>,
257 }
258 
259 pub struct Array {
260     pub bracket: Bracket,
261     pub inner: Type,
262     pub semi_token: Token![;],
263     pub len: usize,
264     pub len_token: LitInt,
265 }
266 
267 #[derive(Copy, Clone, PartialEq)]
268 pub enum Lang {
269     Cxx,
270     Rust,
271 }
272 
273 // An association of a defined Rust name with a fully resolved, namespace
274 // qualified C++ name.
275 #[derive(Clone)]
276 pub struct Pair {
277     pub namespace: Namespace,
278     pub cxx: ForeignName,
279     pub rust: Ident,
280 }
281 
282 // Wrapper for a type which needs to be resolved before it can be printed in
283 // C++.
284 #[derive(PartialEq, Eq, Hash)]
285 pub struct NamedType {
286     pub rust: Ident,
287     pub generics: Lifetimes,
288 }
289