blob: c54430c724b764cca54d0d7973d4cc971ede6a50 [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 Tolnaydf344a82020-05-03 23:23:18 -07003use crate::syntax::report::Errors;
David Tolnayd763f4c2020-04-22 16:09:19 -07004use crate::syntax::{
Joel Galensonc03402a2020-04-23 17:31:09 -07005 error, ident, Api, Enum, ExternFn, ExternType, Lang, Receiver, Ref, Slice, Struct, Ty1, Type,
6 Types,
David Tolnayd763f4c2020-04-22 16:09:19 -07007};
David Tolnay09462ac2020-03-20 14:58:41 -07008use proc_macro2::{Delimiter, Group, Ident, TokenStream};
David Tolnaya420f012020-03-25 17:55:56 -07009use quote::{quote, ToTokens};
10use std::fmt::Display;
David Tolnay7db73692019-10-20 14:51:12 -040011
David Tolnaya83301c2020-04-30 20:32:37 -070012pub(crate) struct Check<'a> {
David Tolnay9dcb8332020-04-30 20:37:33 -070013 namespace: &'a Namespace,
David Tolnay26a2a1d2020-03-25 17:26:43 -070014 apis: &'a [Api],
15 types: &'a Types<'a>,
David Tolnaydf344a82020-05-03 23:23:18 -070016 errors: &'a mut Errors,
David Tolnay26a2a1d2020-03-25 17:26:43 -070017}
18
David Tolnay0dd85ff2020-05-03 23:43:33 -070019pub(crate) fn typecheck(cx: &mut Errors, namespace: &Namespace, apis: &[Api], types: &Types) {
20 do_typecheck(&mut Check {
David Tolnay9dcb8332020-04-30 20:37:33 -070021 namespace,
David Tolnay26a2a1d2020-03-25 17:26:43 -070022 apis,
23 types,
David Tolnay0dd85ff2020-05-03 23:43:33 -070024 errors: cx,
25 });
David Tolnay26a2a1d2020-03-25 17:26:43 -070026}
David Tolnay7db73692019-10-20 14:51:12 -040027
David Tolnay26a2a1d2020-03-25 17:26:43 -070028fn do_typecheck(cx: &mut Check) {
David Tolnay6b6423e2020-04-30 20:46:24 -070029 ident::check_all(cx, cx.namespace, cx.apis);
David Tolnay9dcb8332020-04-30 20:37:33 -070030
David Tolnay26a2a1d2020-03-25 17:26:43 -070031 for ty in cx.types {
David Tolnay7db73692019-10-20 14:51:12 -040032 match ty {
David Tolnay26a2a1d2020-03-25 17:26:43 -070033 Type::Ident(ident) => check_type_ident(cx, ident),
34 Type::RustBox(ptr) => check_type_box(cx, ptr),
David Tolnayc6d891e2020-04-25 12:06:34 -070035 Type::RustVec(ty) => check_type_rust_vec(cx, ty),
David Tolnay26a2a1d2020-03-25 17:26:43 -070036 Type::UniquePtr(ptr) => check_type_unique_ptr(cx, ptr),
David Tolnayfff4c8a2020-04-25 11:45:20 -070037 Type::CxxVector(ptr) => check_type_cxx_vector(cx, ptr),
David Tolnay26a2a1d2020-03-25 17:26:43 -070038 Type::Ref(ty) => check_type_ref(cx, ty),
David Tolnayeebe9b72020-04-14 16:32:18 -070039 Type::Slice(ty) => check_type_slice(cx, ty),
David Tolnay7db73692019-10-20 14:51:12 -040040 _ => {}
41 }
42 }
43
David Tolnay26a2a1d2020-03-25 17:26:43 -070044 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040045 match api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070046 Api::Struct(strct) => check_api_struct(cx, strct),
Joel Galensonc03402a2020-04-23 17:31:09 -070047 Api::Enum(enm) => check_api_enum(cx, enm),
David Tolnay0b368ae2020-04-22 17:55:02 -070048 Api::CxxType(ty) | Api::RustType(ty) => check_api_type(cx, ty),
David Tolnay26a2a1d2020-03-25 17:26:43 -070049 Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn),
David Tolnay7db73692019-10-20 14:51:12 -040050 _ => {}
51 }
52 }
David Tolnay7db73692019-10-20 14:51:12 -040053}
54
David Tolnaya420f012020-03-25 17:55:56 -070055impl Check<'_> {
David Tolnaya83301c2020-04-30 20:32:37 -070056 pub(crate) fn error(&mut self, sp: impl ToTokens, msg: impl Display) {
David Tolnaydf344a82020-05-03 23:23:18 -070057 self.errors.error(sp, msg);
David Tolnaya420f012020-03-25 17:55:56 -070058 }
59}
60
David Tolnay26a2a1d2020-03-25 17:26:43 -070061fn check_type_ident(cx: &mut Check, ident: &Ident) {
David Tolnayd4e68302020-03-25 12:04:17 -070062 if Atom::from(ident).is_none()
David Tolnay26a2a1d2020-03-25 17:26:43 -070063 && !cx.types.structs.contains_key(ident)
Joel Galensonc03402a2020-04-23 17:31:09 -070064 && !cx.types.enums.contains_key(ident)
David Tolnay26a2a1d2020-03-25 17:26:43 -070065 && !cx.types.cxx.contains(ident)
66 && !cx.types.rust.contains(ident)
David Tolnayd4e68302020-03-25 12:04:17 -070067 {
David Tolnaya420f012020-03-25 17:55:56 -070068 cx.error(ident, "unsupported type");
David Tolnayd4e68302020-03-25 12:04:17 -070069 }
70}
71
David Tolnay26a2a1d2020-03-25 17:26:43 -070072fn check_type_box(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070073 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -070074 if cx.types.cxx.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -070075 cx.error(ptr, error::BOX_CXX_TYPE.msg);
David Tolnayd4e68302020-03-25 12:04:17 -070076 }
David Tolnaya420f012020-03-25 17:55:56 -070077
David Tolnayd4e68302020-03-25 12:04:17 -070078 if Atom::from(ident).is_none() {
79 return;
80 }
81 }
David Tolnaya420f012020-03-25 17:55:56 -070082
83 cx.error(ptr, "unsupported target type of Box");
David Tolnayd4e68302020-03-25 12:04:17 -070084}
85
David Tolnayc6d891e2020-04-25 12:06:34 -070086fn check_type_rust_vec(cx: &mut Check, ty: &Ty1) {
87 if let Type::Ident(ident) = &ty.inner {
88 if cx.types.cxx.contains(ident) {
89 cx.error(ty, "Rust Vec containing C++ type is not supported yet");
Myron Ahneba35cf2020-02-05 19:41:51 +070090 return;
David Tolnayc6d891e2020-04-25 12:06:34 -070091 }
92
93 match Atom::from(ident) {
David Tolnay6bd63de2020-04-25 12:20:09 -070094 None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
95 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64) => return,
96 Some(Bool) | Some(RustString) => { /* todo */ }
97 Some(CxxString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +070098 }
99 }
100
David Tolnayc6d891e2020-04-25 12:06:34 -0700101 cx.error(ty, "unsupported element type of Vec");
Myron Ahneba35cf2020-02-05 19:41:51 +0700102}
103
David Tolnay26a2a1d2020-03-25 17:26:43 -0700104fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -0700105 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700106 if cx.types.rust.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -0700107 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayd4e68302020-03-25 12:04:17 -0700108 }
David Tolnaya420f012020-03-25 17:55:56 -0700109
David Tolnayd4e68302020-03-25 12:04:17 -0700110 match Atom::from(ident) {
111 None | Some(CxxString) => return,
112 _ => {}
113 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700114 } else if let Type::CxxVector(_) = &ptr.inner {
Myron Ahneba35cf2020-02-05 19:41:51 +0700115 return;
David Tolnayd4e68302020-03-25 12:04:17 -0700116 }
David Tolnaya420f012020-03-25 17:55:56 -0700117
118 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -0700119}
120
David Tolnayfff4c8a2020-04-25 11:45:20 -0700121fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700122 if let Type::Ident(ident) = &ptr.inner {
123 if cx.types.rust.contains(ident) {
David Tolnayc0faaf62020-04-25 12:18:38 -0700124 cx.error(
125 ptr,
126 "C++ vector containing a Rust type is not supported yet",
127 );
Myron Ahneba35cf2020-02-05 19:41:51 +0700128 }
129
130 match Atom::from(ident) {
David Tolnay6bd63de2020-04-25 12:20:09 -0700131 None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
132 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64) => return,
133 Some(CxxString) => { /* todo */ }
134 Some(Bool) | Some(RustString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700135 }
136 }
David Tolnayc0faaf62020-04-25 12:18:38 -0700137
Myron Ahneba35cf2020-02-05 19:41:51 +0700138 cx.error(ptr, "unsupported vector target type");
139}
140
David Tolnay26a2a1d2020-03-25 17:26:43 -0700141fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700142 if ty.lifetime.is_some() {
143 cx.error(ty, "references with explicit lifetimes are not supported");
144 }
145
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700146 match ty.inner {
147 Type::Fn(_) | Type::Void(_) => {}
David Tolnay776fd892020-04-28 13:38:21 -0700148 Type::Ref(_) => {
149 cx.error(ty, "C++ does not allow references to references");
150 return;
151 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700152 _ => 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
Joel Galensonc03402a2020-04-23 17:31:09 -0700185fn check_api_enum(cx: &mut Check, enm: &Enum) {
186 check_reserved_name(cx, &enm.ident);
187
188 if enm.variants.is_empty() {
189 let span = span_for_enum_error(enm);
190 cx.error(span, "enums without any variants are not supported");
191 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700192}
193
David Tolnay0b368ae2020-04-22 17:55:02 -0700194fn check_api_type(cx: &mut Check, ty: &ExternType) {
195 check_reserved_name(cx, &ty.ident);
196}
197
David Tolnay26a2a1d2020-03-25 17:26:43 -0700198fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700199 if let Some(receiver) = &efn.receiver {
David Tolnaya1f29c42020-04-22 18:01:38 -0700200 let ref span = span_for_receiver_error(receiver);
201
202 if receiver.ty == "Self" {
203 let mutability = match receiver.mutability {
204 Some(_) => "mut ",
205 None => "",
206 };
207 let msg = format!(
208 "unnamed receiver type is only allowed if the surrounding \
209 extern block contains exactly one extern type; \
210 use `self: &{mutability}TheType`",
211 mutability = mutability,
212 );
213 cx.error(span, msg);
214 } else if !cx.types.structs.contains_key(&receiver.ty)
David Tolnay8b60bf12020-04-22 16:52:19 -0700215 && !cx.types.cxx.contains(&receiver.ty)
216 && !cx.types.rust.contains(&receiver.ty)
217 {
David Tolnay8b60bf12020-04-22 16:52:19 -0700218 cx.error(span, "unrecognized receiver type");
219 }
220
David Tolnayd763f4c2020-04-22 16:09:19 -0700221 if receiver.lifetime.is_some() {
David Tolnayd763f4c2020-04-22 16:09:19 -0700222 cx.error(span, "references with explicit lifetimes are not supported");
223 }
224 }
225
David Tolnayd4e68302020-03-25 12:04:17 -0700226 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700227 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700228 let desc = describe(cx, &arg.ty);
229 let msg = format!("passing {} by value is not supported", desc);
230 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700231 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700232 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700233 if efn.lang == Lang::Rust {
234 cx.error(
235 arg,
236 "passing a function pointer from C++ to Rust is not implemented yet",
237 );
238 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700239 }
David Tolnayd4e68302020-03-25 12:04:17 -0700240 }
David Tolnaya420f012020-03-25 17:55:56 -0700241
David Tolnayd4e68302020-03-25 12:04:17 -0700242 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700243 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700244 let desc = describe(cx, ty);
245 let msg = format!("returning {} by value is not supported", desc);
246 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700247 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700248 if let Type::Fn(_) = ty {
249 cx.error(ty, "returning a function pointer is not implemented yet");
250 }
David Tolnayd4e68302020-03-25 12:04:17 -0700251 }
David Tolnay96a826b2020-05-04 00:17:12 -0700252
253 if efn.lang == Lang::Cxx {
254 check_mut_return_restriction(cx, efn);
255 }
256
257 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -0400258}
259
David Tolnay26a2a1d2020-03-25 17:26:43 -0700260fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400261 match &efn.ret {
262 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700263 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400264 }
265
266 for arg in &efn.args {
267 if let Type::Ref(ty) = &arg.ty {
268 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700269 return;
David Tolnay7db73692019-10-20 14:51:12 -0400270 }
271 }
272 }
273
David Tolnaya420f012020-03-25 17:55:56 -0700274 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400275 efn,
276 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700277 );
David Tolnay7db73692019-10-20 14:51:12 -0400278}
279
David Tolnay26a2a1d2020-03-25 17:26:43 -0700280fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400281 match &efn.ret {
282 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700283 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400284 }
285
286 let mut reference_args = 0;
287 for arg in &efn.args {
288 if let Type::Ref(_) = &arg.ty {
289 reference_args += 1;
290 }
291 }
292
Joel Galenson3d4f6122020-04-07 15:54:05 -0700293 if efn.receiver.is_some() {
294 reference_args += 1;
295 }
296
David Tolnay26a2a1d2020-03-25 17:26:43 -0700297 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700298 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400299 efn,
300 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700301 );
David Tolnay7db73692019-10-20 14:51:12 -0400302 }
303}
304
David Tolnay0b368ae2020-04-22 17:55:02 -0700305fn check_reserved_name(cx: &mut Check, ident: &Ident) {
David Tolnay99c93d82020-04-25 14:07:44 -0700306 if ident == "Box"
307 || ident == "UniquePtr"
308 || ident == "Vec"
309 || ident == "CxxVector"
310 || Atom::from(ident).is_some()
311 {
David Tolnay0b368ae2020-04-22 17:55:02 -0700312 cx.error(ident, "reserved name");
313 }
314}
315
David Tolnay26a2a1d2020-03-25 17:26:43 -0700316fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700317 let ident = match ty {
318 Type::Ident(ident) => ident,
David Tolnaye70303c2020-04-25 15:02:37 -0700319 Type::CxxVector(_) | Type::Slice(_) | Type::Void(_) => return true,
David Tolnayd4e68302020-03-25 12:04:17 -0700320 _ => return false,
321 };
David Tolnay26a2a1d2020-03-25 17:26:43 -0700322 ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
323}
324
David Tolnaya420f012020-03-25 17:55:56 -0700325fn span_for_struct_error(strct: &Struct) -> TokenStream {
326 let struct_token = strct.struct_token;
327 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
328 brace_token.set_span(strct.brace_token.span);
329 quote!(#struct_token #brace_token)
330}
331
Joel Galensonc03402a2020-04-23 17:31:09 -0700332fn span_for_enum_error(enm: &Enum) -> TokenStream {
333 let enum_token = enm.enum_token;
334 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
335 brace_token.set_span(enm.brace_token.span);
336 quote!(#enum_token #brace_token)
337}
338
David Tolnayd763f4c2020-04-22 16:09:19 -0700339fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
340 let ampersand = receiver.ampersand;
341 let lifetime = &receiver.lifetime;
342 let mutability = receiver.mutability;
343 if receiver.shorthand {
344 let var = receiver.var;
345 quote!(#ampersand #lifetime #mutability #var)
346 } else {
347 let ty = &receiver.ty;
348 quote!(#ampersand #lifetime #mutability #ty)
349 }
350}
351
David Tolnaya420f012020-03-25 17:55:56 -0700352fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400353 match ty {
354 Type::Ident(ident) => {
David Tolnaya420f012020-03-25 17:55:56 -0700355 if cx.types.structs.contains_key(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400356 "struct".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700357 } else if cx.types.cxx.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400358 "C++ type".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700359 } else if cx.types.rust.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400360 "opaque Rust type".to_owned()
361 } else if Atom::from(ident) == Some(CxxString) {
362 "C++ string".to_owned()
363 } else {
364 ident.to_string()
365 }
366 }
367 Type::RustBox(_) => "Box".to_owned(),
Myron Ahneba35cf2020-02-05 19:41:51 +0700368 Type::RustVec(_) => "Vec".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400369 Type::UniquePtr(_) => "unique_ptr".to_owned(),
370 Type::Ref(_) => "reference".to_owned(),
371 Type::Str(_) => "&str".to_owned(),
David Tolnayfa85fce2020-04-25 15:03:25 -0700372 Type::CxxVector(_) => "C++ vector".to_owned(),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700373 Type::Slice(_) => "slice".to_owned(),
374 Type::SliceRefU8(_) => "&[u8]".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700375 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700376 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400377 }
378}