blob: 5f767c076a39a0d0c1a60f54faab3f591b51afe0 [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 Tolnayfff4c8a2020-04-25 11:45:20 -070032 Type::RustVec(ptr) => check_type_rust_vec(cx, ptr),
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 Tolnayfff4c8a2020-04-25 11:45:20 -070092fn check_type_rust_vec(cx: &mut Check, ptr: &Ty1) {
Myron Ahneba35cf2020-02-05 19:41:51 +070093 // Vec can contain either user-defined type or u8
94 if let Type::Ident(ident) = &ptr.inner {
David Tolnay7ff1b8c2020-04-25 11:44:26 -070095 if Atom::from(ident).map(is_valid_vector_target) == Some(true) {
Myron Ahneba35cf2020-02-05 19:41:51 +070096 return;
97 } else if cx.types.cxx.contains(ident) {
98 cx.error(ptr, error::VEC_CXX_TYPE.msg);
99 } else {
100 return;
101 }
102 }
103
104 cx.error(ptr, "unsupported target type of Vec");
105}
106
David Tolnay26a2a1d2020-03-25 17:26:43 -0700107fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -0700108 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700109 if cx.types.rust.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -0700110 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayd4e68302020-03-25 12:04:17 -0700111 }
David Tolnaya420f012020-03-25 17:55:56 -0700112
David Tolnayd4e68302020-03-25 12:04:17 -0700113 match Atom::from(ident) {
114 None | Some(CxxString) => return,
115 _ => {}
116 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700117 } else if let Type::CxxVector(_) = &ptr.inner {
Myron Ahneba35cf2020-02-05 19:41:51 +0700118 return;
David Tolnayd4e68302020-03-25 12:04:17 -0700119 }
David Tolnaya420f012020-03-25 17:55:56 -0700120
121 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -0700122}
123
David Tolnayfff4c8a2020-04-25 11:45:20 -0700124fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700125 if let Type::Ident(ident) = &ptr.inner {
126 if cx.types.rust.contains(ident) {
127 cx.error(ptr, "vector of a Rust type is not supported yet");
128 }
129
130 match Atom::from(ident) {
131 None => return,
132 Some(atom) => {
David Tolnay7ff1b8c2020-04-25 11:44:26 -0700133 if is_valid_vector_target(atom) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700134 return;
135 }
136 }
137 }
138 }
139 cx.error(ptr, "unsupported vector target type");
140}
141
David Tolnay26a2a1d2020-03-25 17:26:43 -0700142fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700143 if ty.lifetime.is_some() {
144 cx.error(ty, "references with explicit lifetimes are not supported");
145 }
146
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700147 match ty.inner {
148 Type::Fn(_) | Type::Void(_) => {}
149 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700150 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700151
152 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700153}
154
David Tolnayeebe9b72020-04-14 16:32:18 -0700155fn check_type_slice(cx: &mut Check, ty: &Slice) {
156 cx.error(ty, "only &[u8] is supported so far, not other slice types");
157}
158
David Tolnay26a2a1d2020-03-25 17:26:43 -0700159fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnay0b368ae2020-04-22 17:55:02 -0700160 check_reserved_name(cx, &strct.ident);
161
David Tolnayd4e68302020-03-25 12:04:17 -0700162 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700163 let span = span_for_struct_error(strct);
164 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700165 }
David Tolnaya420f012020-03-25 17:55:56 -0700166
David Tolnayd4e68302020-03-25 12:04:17 -0700167 for field in &strct.fields {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700168 if is_unsized(cx, &field.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700169 let desc = describe(cx, &field.ty);
170 let msg = format!("using {} by value is not supported", desc);
171 cx.error(field, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700172 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700173 if let Type::Fn(_) = field.ty {
174 cx.error(
175 field,
176 "function pointers in a struct field are not implemented yet",
177 );
178 }
David Tolnayd4e68302020-03-25 12:04:17 -0700179 }
180}
181
David Tolnay0b368ae2020-04-22 17:55:02 -0700182fn check_api_type(cx: &mut Check, ty: &ExternType) {
183 check_reserved_name(cx, &ty.ident);
184}
185
David Tolnay26a2a1d2020-03-25 17:26:43 -0700186fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700187 if let Some(receiver) = &efn.receiver {
David Tolnaya1f29c42020-04-22 18:01:38 -0700188 let ref span = span_for_receiver_error(receiver);
189
190 if receiver.ty == "Self" {
191 let mutability = match receiver.mutability {
192 Some(_) => "mut ",
193 None => "",
194 };
195 let msg = format!(
196 "unnamed receiver type is only allowed if the surrounding \
197 extern block contains exactly one extern type; \
198 use `self: &{mutability}TheType`",
199 mutability = mutability,
200 );
201 cx.error(span, msg);
202 } else if !cx.types.structs.contains_key(&receiver.ty)
David Tolnay8b60bf12020-04-22 16:52:19 -0700203 && !cx.types.cxx.contains(&receiver.ty)
204 && !cx.types.rust.contains(&receiver.ty)
205 {
David Tolnay8b60bf12020-04-22 16:52:19 -0700206 cx.error(span, "unrecognized receiver type");
207 }
208
David Tolnayd763f4c2020-04-22 16:09:19 -0700209 if receiver.lifetime.is_some() {
David Tolnayd763f4c2020-04-22 16:09:19 -0700210 cx.error(span, "references with explicit lifetimes are not supported");
211 }
212 }
213
David Tolnayd4e68302020-03-25 12:04:17 -0700214 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700215 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700216 let desc = describe(cx, &arg.ty);
217 let msg = format!("passing {} by value is not supported", desc);
218 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700219 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700220 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700221 if efn.lang == Lang::Rust {
222 cx.error(
223 arg,
224 "passing a function pointer from C++ to Rust is not implemented yet",
225 );
226 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700227 }
David Tolnayd4e68302020-03-25 12:04:17 -0700228 }
David Tolnaya420f012020-03-25 17:55:56 -0700229
David Tolnayd4e68302020-03-25 12:04:17 -0700230 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700231 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700232 let desc = describe(cx, ty);
233 let msg = format!("returning {} by value is not supported", desc);
234 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700235 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700236 if let Type::Fn(_) = ty {
237 cx.error(ty, "returning a function pointer is not implemented yet");
238 }
David Tolnayd4e68302020-03-25 12:04:17 -0700239 }
David Tolnay7db73692019-10-20 14:51:12 -0400240}
241
David Tolnay26a2a1d2020-03-25 17:26:43 -0700242fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400243 match &efn.ret {
244 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700245 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400246 }
247
248 for arg in &efn.args {
249 if let Type::Ref(ty) = &arg.ty {
250 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700251 return;
David Tolnay7db73692019-10-20 14:51:12 -0400252 }
253 }
254 }
255
David Tolnaya420f012020-03-25 17:55:56 -0700256 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400257 efn,
258 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700259 );
David Tolnay7db73692019-10-20 14:51:12 -0400260}
261
David Tolnay26a2a1d2020-03-25 17:26:43 -0700262fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400263 match &efn.ret {
264 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700265 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400266 }
267
268 let mut reference_args = 0;
269 for arg in &efn.args {
270 if let Type::Ref(_) = &arg.ty {
271 reference_args += 1;
272 }
273 }
274
Joel Galenson3d4f6122020-04-07 15:54:05 -0700275 if efn.receiver.is_some() {
276 reference_args += 1;
277 }
278
David Tolnay26a2a1d2020-03-25 17:26:43 -0700279 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700280 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400281 efn,
282 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700283 );
David Tolnay7db73692019-10-20 14:51:12 -0400284 }
285}
286
David Tolnay0b368ae2020-04-22 17:55:02 -0700287fn check_reserved_name(cx: &mut Check, ident: &Ident) {
288 if ident == "Box" || ident == "UniquePtr" || Atom::from(ident).is_some() {
289 cx.error(ident, "reserved name");
290 }
291}
292
David Tolnay26a2a1d2020-03-25 17:26:43 -0700293fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700294 let ident = match ty {
295 Type::Ident(ident) => ident,
David Tolnayeebe9b72020-04-14 16:32:18 -0700296 Type::Slice(_) | Type::Void(_) => return true,
David Tolnayd4e68302020-03-25 12:04:17 -0700297 _ => return false,
298 };
David Tolnay26a2a1d2020-03-25 17:26:43 -0700299 ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
300}
301
David Tolnay7ff1b8c2020-04-25 11:44:26 -0700302fn is_valid_vector_target(atom: Atom) -> bool {
303 atom == U8
304 || atom == U16
305 || atom == U32
306 || atom == U64
307 || atom == Usize
308 || atom == I8
309 || atom == I16
310 || atom == I32
311 || atom == I64
312 || atom == Isize
313 || atom == F32
314 || atom == F64
315}
316
David Tolnaya420f012020-03-25 17:55:56 -0700317fn span_for_struct_error(strct: &Struct) -> TokenStream {
318 let struct_token = strct.struct_token;
319 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
320 brace_token.set_span(strct.brace_token.span);
321 quote!(#struct_token #brace_token)
322}
323
David Tolnayd763f4c2020-04-22 16:09:19 -0700324fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
325 let ampersand = receiver.ampersand;
326 let lifetime = &receiver.lifetime;
327 let mutability = receiver.mutability;
328 if receiver.shorthand {
329 let var = receiver.var;
330 quote!(#ampersand #lifetime #mutability #var)
331 } else {
332 let ty = &receiver.ty;
333 quote!(#ampersand #lifetime #mutability #ty)
334 }
335}
336
David Tolnay26a2a1d2020-03-25 17:26:43 -0700337fn combine_errors(errors: Vec<Error>) -> Result<()> {
338 let mut iter = errors.into_iter();
339 let mut all_errors = match iter.next() {
340 Some(err) => err,
341 None => return Ok(()),
342 };
343 for err in iter {
344 all_errors.combine(err);
345 }
346 Err(all_errors)
David Tolnayd4e68302020-03-25 12:04:17 -0700347}
348
David Tolnaya420f012020-03-25 17:55:56 -0700349fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400350 match ty {
351 Type::Ident(ident) => {
David Tolnaya420f012020-03-25 17:55:56 -0700352 if cx.types.structs.contains_key(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400353 "struct".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700354 } else if cx.types.cxx.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400355 "C++ type".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700356 } else if cx.types.rust.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400357 "opaque Rust type".to_owned()
358 } else if Atom::from(ident) == Some(CxxString) {
359 "C++ string".to_owned()
360 } else {
361 ident.to_string()
362 }
363 }
364 Type::RustBox(_) => "Box".to_owned(),
Myron Ahneba35cf2020-02-05 19:41:51 +0700365 Type::RustVec(_) => "Vec".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400366 Type::UniquePtr(_) => "unique_ptr".to_owned(),
367 Type::Ref(_) => "reference".to_owned(),
368 Type::Str(_) => "&str".to_owned(),
David Tolnay4377a9e2020-04-24 15:20:26 -0700369 Type::CxxVector(_) => "vector".to_owned(),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700370 Type::Slice(_) => "slice".to_owned(),
371 Type::SliceRefU8(_) => "&[u8]".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700372 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700373 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400374 }
375}