blob: 090a9aeddcc380a179ae14dccb72f6c8078b27c8 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::syntax::atom::Atom::{self, *};
David Tolnayd763f4c2020-04-22 16:09:19 -07002use crate::syntax::{
David Tolnay0b368ae2020-04-22 17:55:02 -07003 error, ident, Api, ExternFn, ExternType, Lang, Receiver, Ref, Slice, Struct, Ty1, Type, Types,
David Tolnayd763f4c2020-04-22 16:09:19 -07004};
David Tolnay09462ac2020-03-20 14:58:41 -07005use proc_macro2::{Delimiter, Group, Ident, TokenStream};
David Tolnaya420f012020-03-25 17:55:56 -07006use quote::{quote, ToTokens};
7use std::fmt::Display;
David Tolnay7db73692019-10-20 14:51:12 -04008use syn::{Error, Result};
9
David Tolnay26a2a1d2020-03-25 17:26:43 -070010struct Check<'a> {
11 apis: &'a [Api],
12 types: &'a Types<'a>,
13 errors: &'a mut Vec<Error>,
14}
15
David Tolnay7db73692019-10-20 14:51:12 -040016pub(crate) fn typecheck(apis: &[Api], types: &Types) -> Result<()> {
David Tolnay26a2a1d2020-03-25 17:26:43 -070017 let mut errors = Vec::new();
18 let mut cx = Check {
19 apis,
20 types,
21 errors: &mut errors,
22 };
23 do_typecheck(&mut cx);
24 combine_errors(errors)
25}
David Tolnay7db73692019-10-20 14:51:12 -040026
David Tolnay26a2a1d2020-03-25 17:26:43 -070027fn do_typecheck(cx: &mut Check) {
28 for ty in cx.types {
David Tolnay7db73692019-10-20 14:51:12 -040029 match ty {
David Tolnay26a2a1d2020-03-25 17:26:43 -070030 Type::Ident(ident) => check_type_ident(cx, ident),
31 Type::RustBox(ptr) => check_type_box(cx, ptr),
David Tolnayc6d891e2020-04-25 12:06:34 -070032 Type::RustVec(ty) => check_type_rust_vec(cx, ty),
David Tolnay26a2a1d2020-03-25 17:26:43 -070033 Type::UniquePtr(ptr) => check_type_unique_ptr(cx, ptr),
David Tolnayfff4c8a2020-04-25 11:45:20 -070034 Type::CxxVector(ptr) => check_type_cxx_vector(cx, ptr),
David Tolnay26a2a1d2020-03-25 17:26:43 -070035 Type::Ref(ty) => check_type_ref(cx, ty),
David Tolnayeebe9b72020-04-14 16:32:18 -070036 Type::Slice(ty) => check_type_slice(cx, ty),
David Tolnay7db73692019-10-20 14:51:12 -040037 _ => {}
38 }
39 }
40
David Tolnay26a2a1d2020-03-25 17:26:43 -070041 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040042 match api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070043 Api::Struct(strct) => check_api_struct(cx, strct),
David Tolnay0b368ae2020-04-22 17:55:02 -070044 Api::CxxType(ty) | Api::RustType(ty) => check_api_type(cx, ty),
David Tolnay26a2a1d2020-03-25 17:26:43 -070045 Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn),
David Tolnay7db73692019-10-20 14:51:12 -040046 _ => {}
47 }
48 }
49
David Tolnay26a2a1d2020-03-25 17:26:43 -070050 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040051 if let Api::CxxFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070052 check_mut_return_restriction(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040053 }
54 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070055 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040056 }
57 }
58
David Tolnay26a2a1d2020-03-25 17:26:43 -070059 ident::check_all(cx.apis, cx.errors);
David Tolnay7db73692019-10-20 14:51:12 -040060}
61
David Tolnaya420f012020-03-25 17:55:56 -070062impl Check<'_> {
63 fn error(&mut self, sp: impl ToTokens, msg: impl Display) {
64 self.errors.push(Error::new_spanned(sp, msg));
65 }
66}
67
David Tolnay26a2a1d2020-03-25 17:26:43 -070068fn check_type_ident(cx: &mut Check, ident: &Ident) {
David Tolnayd4e68302020-03-25 12:04:17 -070069 if Atom::from(ident).is_none()
David Tolnay26a2a1d2020-03-25 17:26:43 -070070 && !cx.types.structs.contains_key(ident)
71 && !cx.types.cxx.contains(ident)
72 && !cx.types.rust.contains(ident)
David Tolnayd4e68302020-03-25 12:04:17 -070073 {
David Tolnaya420f012020-03-25 17:55:56 -070074 cx.error(ident, "unsupported type");
David Tolnayd4e68302020-03-25 12:04:17 -070075 }
76}
77
David Tolnay26a2a1d2020-03-25 17:26:43 -070078fn check_type_box(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070079 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -070080 if cx.types.cxx.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -070081 cx.error(ptr, error::BOX_CXX_TYPE.msg);
David Tolnayd4e68302020-03-25 12:04:17 -070082 }
David Tolnaya420f012020-03-25 17:55:56 -070083
David Tolnayd4e68302020-03-25 12:04:17 -070084 if Atom::from(ident).is_none() {
85 return;
86 }
87 }
David Tolnaya420f012020-03-25 17:55:56 -070088
89 cx.error(ptr, "unsupported target type of Box");
David Tolnayd4e68302020-03-25 12:04:17 -070090}
91
David Tolnayc6d891e2020-04-25 12:06:34 -070092fn check_type_rust_vec(cx: &mut Check, ty: &Ty1) {
93 if let Type::Ident(ident) = &ty.inner {
94 if cx.types.cxx.contains(ident) {
95 cx.error(ty, "Rust Vec containing C++ type is not supported yet");
Myron Ahneba35cf2020-02-05 19:41:51 +070096 return;
David Tolnayc6d891e2020-04-25 12:06:34 -070097 }
98
99 match Atom::from(ident) {
100 Some(atom) if is_valid_vector_element(atom) => return,
101 None => return,
102 _ => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700103 }
104 }
105
David Tolnayc6d891e2020-04-25 12:06:34 -0700106 cx.error(ty, "unsupported element type of Vec");
Myron Ahneba35cf2020-02-05 19:41:51 +0700107}
108
David Tolnay26a2a1d2020-03-25 17:26:43 -0700109fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -0700110 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700111 if cx.types.rust.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -0700112 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayd4e68302020-03-25 12:04:17 -0700113 }
David Tolnaya420f012020-03-25 17:55:56 -0700114
David Tolnayd4e68302020-03-25 12:04:17 -0700115 match Atom::from(ident) {
116 None | Some(CxxString) => return,
117 _ => {}
118 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700119 } else if let Type::CxxVector(_) = &ptr.inner {
Myron Ahneba35cf2020-02-05 19:41:51 +0700120 return;
David Tolnayd4e68302020-03-25 12:04:17 -0700121 }
David Tolnaya420f012020-03-25 17:55:56 -0700122
123 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -0700124}
125
David Tolnayfff4c8a2020-04-25 11:45:20 -0700126fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700127 if let Type::Ident(ident) = &ptr.inner {
128 if cx.types.rust.contains(ident) {
David Tolnayc0faaf62020-04-25 12:18:38 -0700129 cx.error(
130 ptr,
131 "C++ vector containing a Rust type is not supported yet",
132 );
Myron Ahneba35cf2020-02-05 19:41:51 +0700133 }
134
135 match Atom::from(ident) {
David Tolnayc0faaf62020-04-25 12:18:38 -0700136 Some(atom) if is_valid_vector_element(atom) => return,
Myron Ahneba35cf2020-02-05 19:41:51 +0700137 None => return,
David Tolnayc0faaf62020-04-25 12:18:38 -0700138 _ => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700139 }
140 }
David Tolnayc0faaf62020-04-25 12:18:38 -0700141
Myron Ahneba35cf2020-02-05 19:41:51 +0700142 cx.error(ptr, "unsupported vector target type");
143}
144
David Tolnay26a2a1d2020-03-25 17:26:43 -0700145fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700146 if ty.lifetime.is_some() {
147 cx.error(ty, "references with explicit lifetimes are not supported");
148 }
149
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700150 match ty.inner {
151 Type::Fn(_) | Type::Void(_) => {}
152 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700153 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700154
155 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700156}
157
David Tolnayeebe9b72020-04-14 16:32:18 -0700158fn check_type_slice(cx: &mut Check, ty: &Slice) {
159 cx.error(ty, "only &[u8] is supported so far, not other slice types");
160}
161
David Tolnay26a2a1d2020-03-25 17:26:43 -0700162fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnay0b368ae2020-04-22 17:55:02 -0700163 check_reserved_name(cx, &strct.ident);
164
David Tolnayd4e68302020-03-25 12:04:17 -0700165 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700166 let span = span_for_struct_error(strct);
167 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700168 }
David Tolnaya420f012020-03-25 17:55:56 -0700169
David Tolnayd4e68302020-03-25 12:04:17 -0700170 for field in &strct.fields {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700171 if is_unsized(cx, &field.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700172 let desc = describe(cx, &field.ty);
173 let msg = format!("using {} by value is not supported", desc);
174 cx.error(field, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700175 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700176 if let Type::Fn(_) = field.ty {
177 cx.error(
178 field,
179 "function pointers in a struct field are not implemented yet",
180 );
181 }
David Tolnayd4e68302020-03-25 12:04:17 -0700182 }
183}
184
David Tolnay0b368ae2020-04-22 17:55:02 -0700185fn check_api_type(cx: &mut Check, ty: &ExternType) {
186 check_reserved_name(cx, &ty.ident);
187}
188
David Tolnay26a2a1d2020-03-25 17:26:43 -0700189fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700190 if let Some(receiver) = &efn.receiver {
David Tolnaya1f29c42020-04-22 18:01:38 -0700191 let ref span = span_for_receiver_error(receiver);
192
193 if receiver.ty == "Self" {
194 let mutability = match receiver.mutability {
195 Some(_) => "mut ",
196 None => "",
197 };
198 let msg = format!(
199 "unnamed receiver type is only allowed if the surrounding \
200 extern block contains exactly one extern type; \
201 use `self: &{mutability}TheType`",
202 mutability = mutability,
203 );
204 cx.error(span, msg);
205 } else if !cx.types.structs.contains_key(&receiver.ty)
David Tolnay8b60bf12020-04-22 16:52:19 -0700206 && !cx.types.cxx.contains(&receiver.ty)
207 && !cx.types.rust.contains(&receiver.ty)
208 {
David Tolnay8b60bf12020-04-22 16:52:19 -0700209 cx.error(span, "unrecognized receiver type");
210 }
211
David Tolnayd763f4c2020-04-22 16:09:19 -0700212 if receiver.lifetime.is_some() {
David Tolnayd763f4c2020-04-22 16:09:19 -0700213 cx.error(span, "references with explicit lifetimes are not supported");
214 }
215 }
216
David Tolnayd4e68302020-03-25 12:04:17 -0700217 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700218 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700219 let desc = describe(cx, &arg.ty);
220 let msg = format!("passing {} by value is not supported", desc);
221 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700222 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700223 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700224 if efn.lang == Lang::Rust {
225 cx.error(
226 arg,
227 "passing a function pointer from C++ to Rust is not implemented yet",
228 );
229 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700230 }
David Tolnayd4e68302020-03-25 12:04:17 -0700231 }
David Tolnaya420f012020-03-25 17:55:56 -0700232
David Tolnayd4e68302020-03-25 12:04:17 -0700233 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700234 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700235 let desc = describe(cx, ty);
236 let msg = format!("returning {} by value is not supported", desc);
237 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700238 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700239 if let Type::Fn(_) = ty {
240 cx.error(ty, "returning a function pointer is not implemented yet");
241 }
David Tolnayd4e68302020-03-25 12:04:17 -0700242 }
David Tolnay7db73692019-10-20 14:51:12 -0400243}
244
David Tolnay26a2a1d2020-03-25 17:26:43 -0700245fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400246 match &efn.ret {
247 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700248 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400249 }
250
251 for arg in &efn.args {
252 if let Type::Ref(ty) = &arg.ty {
253 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700254 return;
David Tolnay7db73692019-10-20 14:51:12 -0400255 }
256 }
257 }
258
David Tolnaya420f012020-03-25 17:55:56 -0700259 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400260 efn,
261 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700262 );
David Tolnay7db73692019-10-20 14:51:12 -0400263}
264
David Tolnay26a2a1d2020-03-25 17:26:43 -0700265fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400266 match &efn.ret {
267 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700268 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400269 }
270
271 let mut reference_args = 0;
272 for arg in &efn.args {
273 if let Type::Ref(_) = &arg.ty {
274 reference_args += 1;
275 }
276 }
277
Joel Galenson3d4f6122020-04-07 15:54:05 -0700278 if efn.receiver.is_some() {
279 reference_args += 1;
280 }
281
David Tolnay26a2a1d2020-03-25 17:26:43 -0700282 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700283 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400284 efn,
285 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700286 );
David Tolnay7db73692019-10-20 14:51:12 -0400287 }
288}
289
David Tolnay0b368ae2020-04-22 17:55:02 -0700290fn check_reserved_name(cx: &mut Check, ident: &Ident) {
291 if ident == "Box" || ident == "UniquePtr" || Atom::from(ident).is_some() {
292 cx.error(ident, "reserved name");
293 }
294}
295
David Tolnay26a2a1d2020-03-25 17:26:43 -0700296fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700297 let ident = match ty {
298 Type::Ident(ident) => ident,
David Tolnayeebe9b72020-04-14 16:32:18 -0700299 Type::Slice(_) | Type::Void(_) => return true,
David Tolnayd4e68302020-03-25 12:04:17 -0700300 _ => return false,
301 };
David Tolnay26a2a1d2020-03-25 17:26:43 -0700302 ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
303}
304
David Tolnay2410aff2020-04-25 12:05:57 -0700305fn is_valid_vector_element(atom: Atom) -> bool {
David Tolnay76a84242020-04-25 11:58:30 -0700306 match atom {
307 U8 | U16 | U32 | U64 | Usize | I8 | I16 | I32 | I64 | Isize | F32 | F64 => true,
308 Bool | CxxString | RustString => false,
309 }
David Tolnay7ff1b8c2020-04-25 11:44:26 -0700310}
311
David Tolnaya420f012020-03-25 17:55:56 -0700312fn span_for_struct_error(strct: &Struct) -> TokenStream {
313 let struct_token = strct.struct_token;
314 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
315 brace_token.set_span(strct.brace_token.span);
316 quote!(#struct_token #brace_token)
317}
318
David Tolnayd763f4c2020-04-22 16:09:19 -0700319fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
320 let ampersand = receiver.ampersand;
321 let lifetime = &receiver.lifetime;
322 let mutability = receiver.mutability;
323 if receiver.shorthand {
324 let var = receiver.var;
325 quote!(#ampersand #lifetime #mutability #var)
326 } else {
327 let ty = &receiver.ty;
328 quote!(#ampersand #lifetime #mutability #ty)
329 }
330}
331
David Tolnay26a2a1d2020-03-25 17:26:43 -0700332fn combine_errors(errors: Vec<Error>) -> Result<()> {
333 let mut iter = errors.into_iter();
334 let mut all_errors = match iter.next() {
335 Some(err) => err,
336 None => return Ok(()),
337 };
338 for err in iter {
339 all_errors.combine(err);
340 }
341 Err(all_errors)
David Tolnayd4e68302020-03-25 12:04:17 -0700342}
343
David Tolnaya420f012020-03-25 17:55:56 -0700344fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400345 match ty {
346 Type::Ident(ident) => {
David Tolnaya420f012020-03-25 17:55:56 -0700347 if cx.types.structs.contains_key(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400348 "struct".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700349 } else if cx.types.cxx.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400350 "C++ type".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700351 } else if cx.types.rust.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400352 "opaque Rust type".to_owned()
353 } else if Atom::from(ident) == Some(CxxString) {
354 "C++ string".to_owned()
355 } else {
356 ident.to_string()
357 }
358 }
359 Type::RustBox(_) => "Box".to_owned(),
Myron Ahneba35cf2020-02-05 19:41:51 +0700360 Type::RustVec(_) => "Vec".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400361 Type::UniquePtr(_) => "unique_ptr".to_owned(),
362 Type::Ref(_) => "reference".to_owned(),
363 Type::Str(_) => "&str".to_owned(),
David Tolnay4377a9e2020-04-24 15:20:26 -0700364 Type::CxxVector(_) => "vector".to_owned(),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700365 Type::Slice(_) => "slice".to_owned(),
366 Type::SliceRefU8(_) => "&[u8]".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700367 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700368 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400369 }
370}