blob: 33ca5bb225d471233534c9f90f2aac16feb47104 [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 Tolnay7db73692019-10-20 14:51:12 -040011use syn::{Error, Result};
12
David Tolnaya83301c2020-04-30 20:32:37 -070013pub(crate) struct Check<'a> {
David Tolnay9dcb8332020-04-30 20:37:33 -070014 namespace: &'a Namespace,
David Tolnay26a2a1d2020-03-25 17:26:43 -070015 apis: &'a [Api],
16 types: &'a Types<'a>,
17 errors: &'a mut Vec<Error>,
18}
19
David Tolnay9dcb8332020-04-30 20:37:33 -070020pub(crate) fn typecheck(namespace: &Namespace, apis: &[Api], types: &Types) -> Result<()> {
David Tolnay26a2a1d2020-03-25 17:26:43 -070021 let mut errors = Vec::new();
22 let mut cx = Check {
David Tolnay9dcb8332020-04-30 20:37:33 -070023 namespace,
David Tolnay26a2a1d2020-03-25 17:26:43 -070024 apis,
25 types,
26 errors: &mut errors,
27 };
28 do_typecheck(&mut cx);
29 combine_errors(errors)
30}
David Tolnay7db73692019-10-20 14:51:12 -040031
David Tolnay26a2a1d2020-03-25 17:26:43 -070032fn do_typecheck(cx: &mut Check) {
David Tolnay9dcb8332020-04-30 20:37:33 -070033 for segment in cx.namespace {
David Tolnaya83301c2020-04-30 20:32:37 -070034 ident::check(cx, segment);
David Tolnay9dcb8332020-04-30 20:37:33 -070035 }
David Tolnaya83301c2020-04-30 20:32:37 -070036 ident::check_all(cx, cx.apis);
David Tolnay9dcb8332020-04-30 20:37:33 -070037
David Tolnay26a2a1d2020-03-25 17:26:43 -070038 for ty in cx.types {
David Tolnay7db73692019-10-20 14:51:12 -040039 match ty {
David Tolnay26a2a1d2020-03-25 17:26:43 -070040 Type::Ident(ident) => check_type_ident(cx, ident),
41 Type::RustBox(ptr) => check_type_box(cx, ptr),
David Tolnayc6d891e2020-04-25 12:06:34 -070042 Type::RustVec(ty) => check_type_rust_vec(cx, ty),
David Tolnay26a2a1d2020-03-25 17:26:43 -070043 Type::UniquePtr(ptr) => check_type_unique_ptr(cx, ptr),
David Tolnayfff4c8a2020-04-25 11:45:20 -070044 Type::CxxVector(ptr) => check_type_cxx_vector(cx, ptr),
David Tolnay26a2a1d2020-03-25 17:26:43 -070045 Type::Ref(ty) => check_type_ref(cx, ty),
David Tolnayeebe9b72020-04-14 16:32:18 -070046 Type::Slice(ty) => check_type_slice(cx, ty),
David Tolnay7db73692019-10-20 14:51:12 -040047 _ => {}
48 }
49 }
50
David Tolnay26a2a1d2020-03-25 17:26:43 -070051 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040052 match api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070053 Api::Struct(strct) => check_api_struct(cx, strct),
Joel Galensonc03402a2020-04-23 17:31:09 -070054 Api::Enum(enm) => check_api_enum(cx, enm),
David Tolnay0b368ae2020-04-22 17:55:02 -070055 Api::CxxType(ty) | Api::RustType(ty) => check_api_type(cx, ty),
David Tolnay26a2a1d2020-03-25 17:26:43 -070056 Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn),
David Tolnay7db73692019-10-20 14:51:12 -040057 _ => {}
58 }
59 }
60
David Tolnay26a2a1d2020-03-25 17:26:43 -070061 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040062 if let Api::CxxFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070063 check_mut_return_restriction(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040064 }
65 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070066 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040067 }
68 }
David Tolnay7db73692019-10-20 14:51:12 -040069}
70
David Tolnaya420f012020-03-25 17:55:56 -070071impl Check<'_> {
David Tolnaya83301c2020-04-30 20:32:37 -070072 pub(crate) fn error(&mut self, sp: impl ToTokens, msg: impl Display) {
David Tolnaya420f012020-03-25 17:55:56 -070073 self.errors.push(Error::new_spanned(sp, msg));
74 }
75}
76
David Tolnay26a2a1d2020-03-25 17:26:43 -070077fn check_type_ident(cx: &mut Check, ident: &Ident) {
David Tolnayd4e68302020-03-25 12:04:17 -070078 if Atom::from(ident).is_none()
David Tolnay26a2a1d2020-03-25 17:26:43 -070079 && !cx.types.structs.contains_key(ident)
Joel Galensonc03402a2020-04-23 17:31:09 -070080 && !cx.types.enums.contains_key(ident)
David Tolnay26a2a1d2020-03-25 17:26:43 -070081 && !cx.types.cxx.contains(ident)
82 && !cx.types.rust.contains(ident)
David Tolnayd4e68302020-03-25 12:04:17 -070083 {
David Tolnaya420f012020-03-25 17:55:56 -070084 cx.error(ident, "unsupported type");
David Tolnayd4e68302020-03-25 12:04:17 -070085 }
86}
87
David Tolnay26a2a1d2020-03-25 17:26:43 -070088fn check_type_box(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070089 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -070090 if cx.types.cxx.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -070091 cx.error(ptr, error::BOX_CXX_TYPE.msg);
David Tolnayd4e68302020-03-25 12:04:17 -070092 }
David Tolnaya420f012020-03-25 17:55:56 -070093
David Tolnayd4e68302020-03-25 12:04:17 -070094 if Atom::from(ident).is_none() {
95 return;
96 }
97 }
David Tolnaya420f012020-03-25 17:55:56 -070098
99 cx.error(ptr, "unsupported target type of Box");
David Tolnayd4e68302020-03-25 12:04:17 -0700100}
101
David Tolnayc6d891e2020-04-25 12:06:34 -0700102fn check_type_rust_vec(cx: &mut Check, ty: &Ty1) {
103 if let Type::Ident(ident) = &ty.inner {
104 if cx.types.cxx.contains(ident) {
105 cx.error(ty, "Rust Vec containing C++ type is not supported yet");
Myron Ahneba35cf2020-02-05 19:41:51 +0700106 return;
David Tolnayc6d891e2020-04-25 12:06:34 -0700107 }
108
109 match Atom::from(ident) {
David Tolnay6bd63de2020-04-25 12:20:09 -0700110 None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
111 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64) => return,
112 Some(Bool) | Some(RustString) => { /* todo */ }
113 Some(CxxString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700114 }
115 }
116
David Tolnayc6d891e2020-04-25 12:06:34 -0700117 cx.error(ty, "unsupported element type of Vec");
Myron Ahneba35cf2020-02-05 19:41:51 +0700118}
119
David Tolnay26a2a1d2020-03-25 17:26:43 -0700120fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -0700121 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700122 if cx.types.rust.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -0700123 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayd4e68302020-03-25 12:04:17 -0700124 }
David Tolnaya420f012020-03-25 17:55:56 -0700125
David Tolnayd4e68302020-03-25 12:04:17 -0700126 match Atom::from(ident) {
127 None | Some(CxxString) => return,
128 _ => {}
129 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700130 } else if let Type::CxxVector(_) = &ptr.inner {
Myron Ahneba35cf2020-02-05 19:41:51 +0700131 return;
David Tolnayd4e68302020-03-25 12:04:17 -0700132 }
David Tolnaya420f012020-03-25 17:55:56 -0700133
134 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -0700135}
136
David Tolnayfff4c8a2020-04-25 11:45:20 -0700137fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700138 if let Type::Ident(ident) = &ptr.inner {
139 if cx.types.rust.contains(ident) {
David Tolnayc0faaf62020-04-25 12:18:38 -0700140 cx.error(
141 ptr,
142 "C++ vector containing a Rust type is not supported yet",
143 );
Myron Ahneba35cf2020-02-05 19:41:51 +0700144 }
145
146 match Atom::from(ident) {
David Tolnay6bd63de2020-04-25 12:20:09 -0700147 None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
148 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64) => return,
149 Some(CxxString) => { /* todo */ }
150 Some(Bool) | Some(RustString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700151 }
152 }
David Tolnayc0faaf62020-04-25 12:18:38 -0700153
Myron Ahneba35cf2020-02-05 19:41:51 +0700154 cx.error(ptr, "unsupported vector target type");
155}
156
David Tolnay26a2a1d2020-03-25 17:26:43 -0700157fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700158 if ty.lifetime.is_some() {
159 cx.error(ty, "references with explicit lifetimes are not supported");
160 }
161
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700162 match ty.inner {
163 Type::Fn(_) | Type::Void(_) => {}
David Tolnay776fd892020-04-28 13:38:21 -0700164 Type::Ref(_) => {
165 cx.error(ty, "C++ does not allow references to references");
166 return;
167 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700168 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700169 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700170
171 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700172}
173
David Tolnayeebe9b72020-04-14 16:32:18 -0700174fn check_type_slice(cx: &mut Check, ty: &Slice) {
175 cx.error(ty, "only &[u8] is supported so far, not other slice types");
176}
177
David Tolnay26a2a1d2020-03-25 17:26:43 -0700178fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnay0b368ae2020-04-22 17:55:02 -0700179 check_reserved_name(cx, &strct.ident);
180
David Tolnayd4e68302020-03-25 12:04:17 -0700181 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700182 let span = span_for_struct_error(strct);
183 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700184 }
David Tolnaya420f012020-03-25 17:55:56 -0700185
David Tolnayd4e68302020-03-25 12:04:17 -0700186 for field in &strct.fields {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700187 if is_unsized(cx, &field.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700188 let desc = describe(cx, &field.ty);
189 let msg = format!("using {} by value is not supported", desc);
190 cx.error(field, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700191 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700192 if let Type::Fn(_) = field.ty {
193 cx.error(
194 field,
195 "function pointers in a struct field are not implemented yet",
196 );
197 }
David Tolnayd4e68302020-03-25 12:04:17 -0700198 }
199}
200
Joel Galensonc03402a2020-04-23 17:31:09 -0700201fn check_api_enum(cx: &mut Check, enm: &Enum) {
202 check_reserved_name(cx, &enm.ident);
203
204 if enm.variants.is_empty() {
205 let span = span_for_enum_error(enm);
206 cx.error(span, "enums without any variants are not supported");
207 }
208
209 let mut discriminants = HashSet::new();
210 enm.variants.iter().fold(0, |next_discriminant, variant| {
David Tolnayd7984c22020-04-30 20:09:31 -0700211 let discriminant = variant.discriminant.unwrap_or(next_discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700212 if !discriminants.insert(discriminant) {
213 let msg = format!("discriminant value `{}` already exists", discriminant);
214 cx.error(span_for_enum_error(enm), msg);
215 }
216 discriminant + 1
217 });
218}
219
David Tolnay0b368ae2020-04-22 17:55:02 -0700220fn check_api_type(cx: &mut Check, ty: &ExternType) {
221 check_reserved_name(cx, &ty.ident);
222}
223
David Tolnay26a2a1d2020-03-25 17:26:43 -0700224fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700225 if let Some(receiver) = &efn.receiver {
David Tolnaya1f29c42020-04-22 18:01:38 -0700226 let ref span = span_for_receiver_error(receiver);
227
228 if receiver.ty == "Self" {
229 let mutability = match receiver.mutability {
230 Some(_) => "mut ",
231 None => "",
232 };
233 let msg = format!(
234 "unnamed receiver type is only allowed if the surrounding \
235 extern block contains exactly one extern type; \
236 use `self: &{mutability}TheType`",
237 mutability = mutability,
238 );
239 cx.error(span, msg);
240 } else if !cx.types.structs.contains_key(&receiver.ty)
David Tolnay8b60bf12020-04-22 16:52:19 -0700241 && !cx.types.cxx.contains(&receiver.ty)
242 && !cx.types.rust.contains(&receiver.ty)
243 {
David Tolnay8b60bf12020-04-22 16:52:19 -0700244 cx.error(span, "unrecognized receiver type");
245 }
246
David Tolnayd763f4c2020-04-22 16:09:19 -0700247 if receiver.lifetime.is_some() {
David Tolnayd763f4c2020-04-22 16:09:19 -0700248 cx.error(span, "references with explicit lifetimes are not supported");
249 }
250 }
251
David Tolnayd4e68302020-03-25 12:04:17 -0700252 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700253 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700254 let desc = describe(cx, &arg.ty);
255 let msg = format!("passing {} by value is not supported", desc);
256 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700257 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700258 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700259 if efn.lang == Lang::Rust {
260 cx.error(
261 arg,
262 "passing a function pointer from C++ to Rust is not implemented yet",
263 );
264 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700265 }
David Tolnayd4e68302020-03-25 12:04:17 -0700266 }
David Tolnaya420f012020-03-25 17:55:56 -0700267
David Tolnayd4e68302020-03-25 12:04:17 -0700268 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700269 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700270 let desc = describe(cx, ty);
271 let msg = format!("returning {} by value is not supported", desc);
272 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700273 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700274 if let Type::Fn(_) = ty {
275 cx.error(ty, "returning a function pointer is not implemented yet");
276 }
David Tolnayd4e68302020-03-25 12:04:17 -0700277 }
David Tolnay7db73692019-10-20 14:51:12 -0400278}
279
David Tolnay26a2a1d2020-03-25 17:26:43 -0700280fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400281 match &efn.ret {
282 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700283 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400284 }
285
286 for arg in &efn.args {
287 if let Type::Ref(ty) = &arg.ty {
288 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700289 return;
David Tolnay7db73692019-10-20 14:51:12 -0400290 }
291 }
292 }
293
David Tolnaya420f012020-03-25 17:55:56 -0700294 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400295 efn,
296 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700297 );
David Tolnay7db73692019-10-20 14:51:12 -0400298}
299
David Tolnay26a2a1d2020-03-25 17:26:43 -0700300fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400301 match &efn.ret {
302 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700303 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400304 }
305
306 let mut reference_args = 0;
307 for arg in &efn.args {
308 if let Type::Ref(_) = &arg.ty {
309 reference_args += 1;
310 }
311 }
312
Joel Galenson3d4f6122020-04-07 15:54:05 -0700313 if efn.receiver.is_some() {
314 reference_args += 1;
315 }
316
David Tolnay26a2a1d2020-03-25 17:26:43 -0700317 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700318 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400319 efn,
320 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700321 );
David Tolnay7db73692019-10-20 14:51:12 -0400322 }
323}
324
David Tolnay0b368ae2020-04-22 17:55:02 -0700325fn check_reserved_name(cx: &mut Check, ident: &Ident) {
David Tolnay99c93d82020-04-25 14:07:44 -0700326 if ident == "Box"
327 || ident == "UniquePtr"
328 || ident == "Vec"
329 || ident == "CxxVector"
330 || Atom::from(ident).is_some()
331 {
David Tolnay0b368ae2020-04-22 17:55:02 -0700332 cx.error(ident, "reserved name");
333 }
334}
335
David Tolnay26a2a1d2020-03-25 17:26:43 -0700336fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700337 let ident = match ty {
338 Type::Ident(ident) => ident,
David Tolnaye70303c2020-04-25 15:02:37 -0700339 Type::CxxVector(_) | Type::Slice(_) | Type::Void(_) => return true,
David Tolnayd4e68302020-03-25 12:04:17 -0700340 _ => return false,
341 };
David Tolnay26a2a1d2020-03-25 17:26:43 -0700342 ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
343}
344
David Tolnaya420f012020-03-25 17:55:56 -0700345fn span_for_struct_error(strct: &Struct) -> TokenStream {
346 let struct_token = strct.struct_token;
347 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
348 brace_token.set_span(strct.brace_token.span);
349 quote!(#struct_token #brace_token)
350}
351
Joel Galensonc03402a2020-04-23 17:31:09 -0700352fn span_for_enum_error(enm: &Enum) -> TokenStream {
353 let enum_token = enm.enum_token;
354 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
355 brace_token.set_span(enm.brace_token.span);
356 quote!(#enum_token #brace_token)
357}
358
David Tolnayd763f4c2020-04-22 16:09:19 -0700359fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
360 let ampersand = receiver.ampersand;
361 let lifetime = &receiver.lifetime;
362 let mutability = receiver.mutability;
363 if receiver.shorthand {
364 let var = receiver.var;
365 quote!(#ampersand #lifetime #mutability #var)
366 } else {
367 let ty = &receiver.ty;
368 quote!(#ampersand #lifetime #mutability #ty)
369 }
370}
371
David Tolnay26a2a1d2020-03-25 17:26:43 -0700372fn combine_errors(errors: Vec<Error>) -> Result<()> {
373 let mut iter = errors.into_iter();
374 let mut all_errors = match iter.next() {
375 Some(err) => err,
376 None => return Ok(()),
377 };
378 for err in iter {
379 all_errors.combine(err);
380 }
381 Err(all_errors)
David Tolnayd4e68302020-03-25 12:04:17 -0700382}
383
David Tolnaya420f012020-03-25 17:55:56 -0700384fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400385 match ty {
386 Type::Ident(ident) => {
David Tolnaya420f012020-03-25 17:55:56 -0700387 if cx.types.structs.contains_key(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400388 "struct".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700389 } else if cx.types.cxx.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400390 "C++ type".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700391 } else if cx.types.rust.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400392 "opaque Rust type".to_owned()
393 } else if Atom::from(ident) == Some(CxxString) {
394 "C++ string".to_owned()
395 } else {
396 ident.to_string()
397 }
398 }
399 Type::RustBox(_) => "Box".to_owned(),
Myron Ahneba35cf2020-02-05 19:41:51 +0700400 Type::RustVec(_) => "Vec".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400401 Type::UniquePtr(_) => "unique_ptr".to_owned(),
402 Type::Ref(_) => "reference".to_owned(),
403 Type::Str(_) => "&str".to_owned(),
David Tolnayfa85fce2020-04-25 15:03:25 -0700404 Type::CxxVector(_) => "C++ vector".to_owned(),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700405 Type::Slice(_) => "slice".to_owned(),
406 Type::SliceRefU8(_) => "&[u8]".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700407 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700408 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400409 }
410}