blob: 0ea53df4d37f8b5b9ee4fa281ffef87635444923 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::syntax::atom::Atom::{self, *};
David Tolnay9dcb8332020-04-30 20:37:33 -07002use crate::syntax::namespace::Namespace;
David Tolnayd763f4c2020-04-22 16:09:19 -07003use crate::syntax::{
Joel Galensonc03402a2020-04-23 17:31:09 -07004 error, ident, Api, Enum, ExternFn, ExternType, Lang, Receiver, Ref, Slice, Struct, Ty1, Type,
5 Types,
David Tolnayd763f4c2020-04-22 16:09:19 -07006};
David Tolnay09462ac2020-03-20 14:58:41 -07007use proc_macro2::{Delimiter, Group, Ident, TokenStream};
David Tolnaya420f012020-03-25 17:55:56 -07008use quote::{quote, ToTokens};
Joel Galensonc03402a2020-04-23 17:31:09 -07009use std::collections::HashSet;
David Tolnaya420f012020-03-25 17:55:56 -070010use std::fmt::Display;
David Tolnay8adb2232020-05-01 10:14:27 -070011use std::u32;
David Tolnay7db73692019-10-20 14:51:12 -040012use syn::{Error, Result};
13
David Tolnaya83301c2020-04-30 20:32:37 -070014pub(crate) struct Check<'a> {
David Tolnay9dcb8332020-04-30 20:37:33 -070015 namespace: &'a Namespace,
David Tolnay26a2a1d2020-03-25 17:26:43 -070016 apis: &'a [Api],
17 types: &'a Types<'a>,
18 errors: &'a mut Vec<Error>,
19}
20
David Tolnay9dcb8332020-04-30 20:37:33 -070021pub(crate) fn typecheck(namespace: &Namespace, apis: &[Api], types: &Types) -> Result<()> {
David Tolnay26a2a1d2020-03-25 17:26:43 -070022 let mut errors = Vec::new();
23 let mut cx = Check {
David Tolnay9dcb8332020-04-30 20:37:33 -070024 namespace,
David Tolnay26a2a1d2020-03-25 17:26:43 -070025 apis,
26 types,
27 errors: &mut errors,
28 };
29 do_typecheck(&mut cx);
30 combine_errors(errors)
31}
David Tolnay7db73692019-10-20 14:51:12 -040032
David Tolnay26a2a1d2020-03-25 17:26:43 -070033fn do_typecheck(cx: &mut Check) {
David Tolnay6b6423e2020-04-30 20:46:24 -070034 ident::check_all(cx, cx.namespace, cx.apis);
David Tolnay9dcb8332020-04-30 20:37:33 -070035
David Tolnay26a2a1d2020-03-25 17:26:43 -070036 for ty in cx.types {
David Tolnay7db73692019-10-20 14:51:12 -040037 match ty {
David Tolnay26a2a1d2020-03-25 17:26:43 -070038 Type::Ident(ident) => check_type_ident(cx, ident),
39 Type::RustBox(ptr) => check_type_box(cx, ptr),
David Tolnayc6d891e2020-04-25 12:06:34 -070040 Type::RustVec(ty) => check_type_rust_vec(cx, ty),
David Tolnay26a2a1d2020-03-25 17:26:43 -070041 Type::UniquePtr(ptr) => check_type_unique_ptr(cx, ptr),
David Tolnayfff4c8a2020-04-25 11:45:20 -070042 Type::CxxVector(ptr) => check_type_cxx_vector(cx, ptr),
David Tolnay26a2a1d2020-03-25 17:26:43 -070043 Type::Ref(ty) => check_type_ref(cx, ty),
David Tolnayeebe9b72020-04-14 16:32:18 -070044 Type::Slice(ty) => check_type_slice(cx, ty),
David Tolnay7db73692019-10-20 14:51:12 -040045 _ => {}
46 }
47 }
48
David Tolnay26a2a1d2020-03-25 17:26:43 -070049 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040050 match api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070051 Api::Struct(strct) => check_api_struct(cx, strct),
Joel Galensonc03402a2020-04-23 17:31:09 -070052 Api::Enum(enm) => check_api_enum(cx, enm),
David Tolnay0b368ae2020-04-22 17:55:02 -070053 Api::CxxType(ty) | Api::RustType(ty) => check_api_type(cx, ty),
David Tolnay26a2a1d2020-03-25 17:26:43 -070054 Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn),
David Tolnay7db73692019-10-20 14:51:12 -040055 _ => {}
56 }
57 }
58
David Tolnay26a2a1d2020-03-25 17:26:43 -070059 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040060 if let Api::CxxFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070061 check_mut_return_restriction(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040062 }
63 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070064 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040065 }
66 }
David Tolnay7db73692019-10-20 14:51:12 -040067}
68
David Tolnaya420f012020-03-25 17:55:56 -070069impl Check<'_> {
David Tolnaya83301c2020-04-30 20:32:37 -070070 pub(crate) fn error(&mut self, sp: impl ToTokens, msg: impl Display) {
David Tolnaya420f012020-03-25 17:55:56 -070071 self.errors.push(Error::new_spanned(sp, msg));
72 }
73}
74
David Tolnay26a2a1d2020-03-25 17:26:43 -070075fn check_type_ident(cx: &mut Check, ident: &Ident) {
David Tolnayd4e68302020-03-25 12:04:17 -070076 if Atom::from(ident).is_none()
David Tolnay26a2a1d2020-03-25 17:26:43 -070077 && !cx.types.structs.contains_key(ident)
Joel Galensonc03402a2020-04-23 17:31:09 -070078 && !cx.types.enums.contains_key(ident)
David Tolnay26a2a1d2020-03-25 17:26:43 -070079 && !cx.types.cxx.contains(ident)
80 && !cx.types.rust.contains(ident)
David Tolnayd4e68302020-03-25 12:04:17 -070081 {
David Tolnaya420f012020-03-25 17:55:56 -070082 cx.error(ident, "unsupported type");
David Tolnayd4e68302020-03-25 12:04:17 -070083 }
84}
85
David Tolnay26a2a1d2020-03-25 17:26:43 -070086fn check_type_box(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070087 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -070088 if cx.types.cxx.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -070089 cx.error(ptr, error::BOX_CXX_TYPE.msg);
David Tolnayd4e68302020-03-25 12:04:17 -070090 }
David Tolnaya420f012020-03-25 17:55:56 -070091
David Tolnayd4e68302020-03-25 12:04:17 -070092 if Atom::from(ident).is_none() {
93 return;
94 }
95 }
David Tolnaya420f012020-03-25 17:55:56 -070096
97 cx.error(ptr, "unsupported target type of Box");
David Tolnayd4e68302020-03-25 12:04:17 -070098}
99
David Tolnayc6d891e2020-04-25 12:06:34 -0700100fn check_type_rust_vec(cx: &mut Check, ty: &Ty1) {
101 if let Type::Ident(ident) = &ty.inner {
102 if cx.types.cxx.contains(ident) {
103 cx.error(ty, "Rust Vec containing C++ type is not supported yet");
Myron Ahneba35cf2020-02-05 19:41:51 +0700104 return;
David Tolnayc6d891e2020-04-25 12:06:34 -0700105 }
106
107 match Atom::from(ident) {
David Tolnay6bd63de2020-04-25 12:20:09 -0700108 None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
109 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64) => return,
110 Some(Bool) | Some(RustString) => { /* todo */ }
111 Some(CxxString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700112 }
113 }
114
David Tolnayc6d891e2020-04-25 12:06:34 -0700115 cx.error(ty, "unsupported element type of Vec");
Myron Ahneba35cf2020-02-05 19:41:51 +0700116}
117
David Tolnay26a2a1d2020-03-25 17:26:43 -0700118fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -0700119 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700120 if cx.types.rust.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -0700121 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayd4e68302020-03-25 12:04:17 -0700122 }
David Tolnaya420f012020-03-25 17:55:56 -0700123
David Tolnayd4e68302020-03-25 12:04:17 -0700124 match Atom::from(ident) {
125 None | Some(CxxString) => return,
126 _ => {}
127 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700128 } else if let Type::CxxVector(_) = &ptr.inner {
Myron Ahneba35cf2020-02-05 19:41:51 +0700129 return;
David Tolnayd4e68302020-03-25 12:04:17 -0700130 }
David Tolnaya420f012020-03-25 17:55:56 -0700131
132 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -0700133}
134
David Tolnayfff4c8a2020-04-25 11:45:20 -0700135fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700136 if let Type::Ident(ident) = &ptr.inner {
137 if cx.types.rust.contains(ident) {
David Tolnayc0faaf62020-04-25 12:18:38 -0700138 cx.error(
139 ptr,
140 "C++ vector containing a Rust type is not supported yet",
141 );
Myron Ahneba35cf2020-02-05 19:41:51 +0700142 }
143
144 match Atom::from(ident) {
David Tolnay6bd63de2020-04-25 12:20:09 -0700145 None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
146 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64) => return,
147 Some(CxxString) => { /* todo */ }
148 Some(Bool) | Some(RustString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700149 }
150 }
David Tolnayc0faaf62020-04-25 12:18:38 -0700151
Myron Ahneba35cf2020-02-05 19:41:51 +0700152 cx.error(ptr, "unsupported vector target type");
153}
154
David Tolnay26a2a1d2020-03-25 17:26:43 -0700155fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700156 if ty.lifetime.is_some() {
157 cx.error(ty, "references with explicit lifetimes are not supported");
158 }
159
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700160 match ty.inner {
161 Type::Fn(_) | Type::Void(_) => {}
David Tolnay776fd892020-04-28 13:38:21 -0700162 Type::Ref(_) => {
163 cx.error(ty, "C++ does not allow references to references");
164 return;
165 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700166 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700167 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700168
169 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700170}
171
David Tolnayeebe9b72020-04-14 16:32:18 -0700172fn check_type_slice(cx: &mut Check, ty: &Slice) {
173 cx.error(ty, "only &[u8] is supported so far, not other slice types");
174}
175
David Tolnay26a2a1d2020-03-25 17:26:43 -0700176fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnay0b368ae2020-04-22 17:55:02 -0700177 check_reserved_name(cx, &strct.ident);
178
David Tolnayd4e68302020-03-25 12:04:17 -0700179 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700180 let span = span_for_struct_error(strct);
181 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700182 }
David Tolnaya420f012020-03-25 17:55:56 -0700183
David Tolnayd4e68302020-03-25 12:04:17 -0700184 for field in &strct.fields {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700185 if is_unsized(cx, &field.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700186 let desc = describe(cx, &field.ty);
187 let msg = format!("using {} by value is not supported", desc);
188 cx.error(field, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700189 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700190 if let Type::Fn(_) = field.ty {
191 cx.error(
192 field,
193 "function pointers in a struct field are not implemented yet",
194 );
195 }
David Tolnayd4e68302020-03-25 12:04:17 -0700196 }
197}
198
Joel Galensonc03402a2020-04-23 17:31:09 -0700199fn check_api_enum(cx: &mut Check, enm: &Enum) {
200 check_reserved_name(cx, &enm.ident);
201
202 if enm.variants.is_empty() {
203 let span = span_for_enum_error(enm);
204 cx.error(span, "enums without any variants are not supported");
205 }
206
207 let mut discriminants = HashSet::new();
Joel Galenson04fa0962020-05-01 10:00:31 -0700208 enm.variants
209 .iter()
210 .fold(None, |prev_discriminant, variant| {
211 if variant.discriminant.is_none() && prev_discriminant.unwrap_or(0) == u32::MAX {
212 let msg = format!("overflowed on value after {}", prev_discriminant.unwrap());
213 cx.error(span_for_enum_error(enm), msg);
214 return None;
215 }
216 let discriminant = variant
217 .discriminant
218 .unwrap_or_else(|| prev_discriminant.map_or(0, |n| n + 1));
219 if !discriminants.insert(discriminant) {
220 let msg = format!("discriminant value `{}` already exists", discriminant);
221 cx.error(span_for_enum_error(enm), msg);
222 }
223 Some(discriminant)
224 });
Joel Galensonc03402a2020-04-23 17:31:09 -0700225}
226
David Tolnay0b368ae2020-04-22 17:55:02 -0700227fn check_api_type(cx: &mut Check, ty: &ExternType) {
228 check_reserved_name(cx, &ty.ident);
229}
230
David Tolnay26a2a1d2020-03-25 17:26:43 -0700231fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700232 if let Some(receiver) = &efn.receiver {
David Tolnaya1f29c42020-04-22 18:01:38 -0700233 let ref span = span_for_receiver_error(receiver);
234
235 if receiver.ty == "Self" {
236 let mutability = match receiver.mutability {
237 Some(_) => "mut ",
238 None => "",
239 };
240 let msg = format!(
241 "unnamed receiver type is only allowed if the surrounding \
242 extern block contains exactly one extern type; \
243 use `self: &{mutability}TheType`",
244 mutability = mutability,
245 );
246 cx.error(span, msg);
247 } else if !cx.types.structs.contains_key(&receiver.ty)
David Tolnay8b60bf12020-04-22 16:52:19 -0700248 && !cx.types.cxx.contains(&receiver.ty)
249 && !cx.types.rust.contains(&receiver.ty)
250 {
David Tolnay8b60bf12020-04-22 16:52:19 -0700251 cx.error(span, "unrecognized receiver type");
252 }
253
David Tolnayd763f4c2020-04-22 16:09:19 -0700254 if receiver.lifetime.is_some() {
David Tolnayd763f4c2020-04-22 16:09:19 -0700255 cx.error(span, "references with explicit lifetimes are not supported");
256 }
257 }
258
David Tolnayd4e68302020-03-25 12:04:17 -0700259 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700260 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700261 let desc = describe(cx, &arg.ty);
262 let msg = format!("passing {} by value is not supported", desc);
263 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700264 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700265 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700266 if efn.lang == Lang::Rust {
267 cx.error(
268 arg,
269 "passing a function pointer from C++ to Rust is not implemented yet",
270 );
271 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700272 }
David Tolnayd4e68302020-03-25 12:04:17 -0700273 }
David Tolnaya420f012020-03-25 17:55:56 -0700274
David Tolnayd4e68302020-03-25 12:04:17 -0700275 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700276 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700277 let desc = describe(cx, ty);
278 let msg = format!("returning {} by value is not supported", desc);
279 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700280 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700281 if let Type::Fn(_) = ty {
282 cx.error(ty, "returning a function pointer is not implemented yet");
283 }
David Tolnayd4e68302020-03-25 12:04:17 -0700284 }
David Tolnay7db73692019-10-20 14:51:12 -0400285}
286
David Tolnay26a2a1d2020-03-25 17:26:43 -0700287fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400288 match &efn.ret {
289 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700290 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400291 }
292
293 for arg in &efn.args {
294 if let Type::Ref(ty) = &arg.ty {
295 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700296 return;
David Tolnay7db73692019-10-20 14:51:12 -0400297 }
298 }
299 }
300
David Tolnaya420f012020-03-25 17:55:56 -0700301 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400302 efn,
303 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700304 );
David Tolnay7db73692019-10-20 14:51:12 -0400305}
306
David Tolnay26a2a1d2020-03-25 17:26:43 -0700307fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400308 match &efn.ret {
309 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700310 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400311 }
312
313 let mut reference_args = 0;
314 for arg in &efn.args {
315 if let Type::Ref(_) = &arg.ty {
316 reference_args += 1;
317 }
318 }
319
Joel Galenson3d4f6122020-04-07 15:54:05 -0700320 if efn.receiver.is_some() {
321 reference_args += 1;
322 }
323
David Tolnay26a2a1d2020-03-25 17:26:43 -0700324 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700325 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400326 efn,
327 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700328 );
David Tolnay7db73692019-10-20 14:51:12 -0400329 }
330}
331
David Tolnay0b368ae2020-04-22 17:55:02 -0700332fn check_reserved_name(cx: &mut Check, ident: &Ident) {
David Tolnay99c93d82020-04-25 14:07:44 -0700333 if ident == "Box"
334 || ident == "UniquePtr"
335 || ident == "Vec"
336 || ident == "CxxVector"
337 || Atom::from(ident).is_some()
338 {
David Tolnay0b368ae2020-04-22 17:55:02 -0700339 cx.error(ident, "reserved name");
340 }
341}
342
David Tolnay26a2a1d2020-03-25 17:26:43 -0700343fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700344 let ident = match ty {
345 Type::Ident(ident) => ident,
David Tolnaye70303c2020-04-25 15:02:37 -0700346 Type::CxxVector(_) | Type::Slice(_) | Type::Void(_) => return true,
David Tolnayd4e68302020-03-25 12:04:17 -0700347 _ => return false,
348 };
David Tolnay26a2a1d2020-03-25 17:26:43 -0700349 ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
350}
351
David Tolnaya420f012020-03-25 17:55:56 -0700352fn span_for_struct_error(strct: &Struct) -> TokenStream {
353 let struct_token = strct.struct_token;
354 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
355 brace_token.set_span(strct.brace_token.span);
356 quote!(#struct_token #brace_token)
357}
358
Joel Galensonc03402a2020-04-23 17:31:09 -0700359fn span_for_enum_error(enm: &Enum) -> TokenStream {
360 let enum_token = enm.enum_token;
361 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
362 brace_token.set_span(enm.brace_token.span);
363 quote!(#enum_token #brace_token)
364}
365
David Tolnayd763f4c2020-04-22 16:09:19 -0700366fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
367 let ampersand = receiver.ampersand;
368 let lifetime = &receiver.lifetime;
369 let mutability = receiver.mutability;
370 if receiver.shorthand {
371 let var = receiver.var;
372 quote!(#ampersand #lifetime #mutability #var)
373 } else {
374 let ty = &receiver.ty;
375 quote!(#ampersand #lifetime #mutability #ty)
376 }
377}
378
David Tolnay26a2a1d2020-03-25 17:26:43 -0700379fn combine_errors(errors: Vec<Error>) -> Result<()> {
380 let mut iter = errors.into_iter();
381 let mut all_errors = match iter.next() {
382 Some(err) => err,
383 None => return Ok(()),
384 };
385 for err in iter {
386 all_errors.combine(err);
387 }
388 Err(all_errors)
David Tolnayd4e68302020-03-25 12:04:17 -0700389}
390
David Tolnaya420f012020-03-25 17:55:56 -0700391fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400392 match ty {
393 Type::Ident(ident) => {
David Tolnaya420f012020-03-25 17:55:56 -0700394 if cx.types.structs.contains_key(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400395 "struct".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700396 } else if cx.types.cxx.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400397 "C++ type".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700398 } else if cx.types.rust.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400399 "opaque Rust type".to_owned()
400 } else if Atom::from(ident) == Some(CxxString) {
401 "C++ string".to_owned()
402 } else {
403 ident.to_string()
404 }
405 }
406 Type::RustBox(_) => "Box".to_owned(),
Myron Ahneba35cf2020-02-05 19:41:51 +0700407 Type::RustVec(_) => "Vec".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400408 Type::UniquePtr(_) => "unique_ptr".to_owned(),
409 Type::Ref(_) => "reference".to_owned(),
410 Type::Str(_) => "&str".to_owned(),
David Tolnayfa85fce2020-04-25 15:03:25 -0700411 Type::CxxVector(_) => "C++ vector".to_owned(),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700412 Type::Slice(_) => "slice".to_owned(),
413 Type::SliceRefU8(_) => "&[u8]".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700414 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700415 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400416 }
417}