blob: 4fbc6e22d3d1be31fa29886d539bfb1c50b53a77 [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)
David Tolnay33f56ad2020-08-27 17:06:35 -070095 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64)
96 | Some(RustString) => return,
97 Some(Bool) => { /* todo */ }
David Tolnay6bd63de2020-04-25 12:20:09 -070098 Some(CxxString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +070099 }
100 }
101
David Tolnayc6d891e2020-04-25 12:06:34 -0700102 cx.error(ty, "unsupported element type of Vec");
Myron Ahneba35cf2020-02-05 19:41:51 +0700103}
104
David Tolnay26a2a1d2020-03-25 17:26:43 -0700105fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -0700106 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700107 if cx.types.rust.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -0700108 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayd4e68302020-03-25 12:04:17 -0700109 }
David Tolnaya420f012020-03-25 17:55:56 -0700110
David Tolnayd4e68302020-03-25 12:04:17 -0700111 match Atom::from(ident) {
112 None | Some(CxxString) => return,
113 _ => {}
114 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700115 } else if let Type::CxxVector(_) = &ptr.inner {
Myron Ahneba35cf2020-02-05 19:41:51 +0700116 return;
David Tolnayd4e68302020-03-25 12:04:17 -0700117 }
David Tolnaya420f012020-03-25 17:55:56 -0700118
119 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -0700120}
121
David Tolnayfff4c8a2020-04-25 11:45:20 -0700122fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700123 if let Type::Ident(ident) = &ptr.inner {
124 if cx.types.rust.contains(ident) {
David Tolnayc0faaf62020-04-25 12:18:38 -0700125 cx.error(
126 ptr,
127 "C++ vector containing a Rust type is not supported yet",
128 );
Myron Ahneba35cf2020-02-05 19:41:51 +0700129 }
130
131 match Atom::from(ident) {
David Tolnay6bd63de2020-04-25 12:20:09 -0700132 None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
David Tolnay47e239d2020-08-28 00:32:04 -0700133 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64)
134 | Some(CxxString) => return,
David Tolnay6bd63de2020-04-25 12:20:09 -0700135 Some(Bool) | Some(RustString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700136 }
137 }
David Tolnayc0faaf62020-04-25 12:18:38 -0700138
Myron Ahneba35cf2020-02-05 19:41:51 +0700139 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(_) => {}
David Tolnay776fd892020-04-28 13:38:21 -0700149 Type::Ref(_) => {
150 cx.error(ty, "C++ does not allow references to references");
151 return;
152 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700153 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700154 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700155
156 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700157}
158
David Tolnayeebe9b72020-04-14 16:32:18 -0700159fn check_type_slice(cx: &mut Check, ty: &Slice) {
160 cx.error(ty, "only &[u8] is supported so far, not other slice types");
161}
162
David Tolnay26a2a1d2020-03-25 17:26:43 -0700163fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnay0b368ae2020-04-22 17:55:02 -0700164 check_reserved_name(cx, &strct.ident);
165
David Tolnayd4e68302020-03-25 12:04:17 -0700166 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700167 let span = span_for_struct_error(strct);
168 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700169 }
David Tolnaya420f012020-03-25 17:55:56 -0700170
David Tolnayd4e68302020-03-25 12:04:17 -0700171 for field in &strct.fields {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700172 if is_unsized(cx, &field.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700173 let desc = describe(cx, &field.ty);
174 let msg = format!("using {} by value is not supported", desc);
175 cx.error(field, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700176 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700177 if let Type::Fn(_) = field.ty {
178 cx.error(
179 field,
180 "function pointers in a struct field are not implemented yet",
181 );
182 }
David Tolnayd4e68302020-03-25 12:04:17 -0700183 }
184}
185
Joel Galensonc03402a2020-04-23 17:31:09 -0700186fn check_api_enum(cx: &mut Check, enm: &Enum) {
187 check_reserved_name(cx, &enm.ident);
188
189 if enm.variants.is_empty() {
190 let span = span_for_enum_error(enm);
191 cx.error(span, "enums without any variants are not supported");
192 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700193}
194
David Tolnay0b368ae2020-04-22 17:55:02 -0700195fn check_api_type(cx: &mut Check, ty: &ExternType) {
196 check_reserved_name(cx, &ty.ident);
197}
198
David Tolnay26a2a1d2020-03-25 17:26:43 -0700199fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700200 if let Some(receiver) = &efn.receiver {
David Tolnaya1f29c42020-04-22 18:01:38 -0700201 let ref span = span_for_receiver_error(receiver);
202
203 if receiver.ty == "Self" {
204 let mutability = match receiver.mutability {
205 Some(_) => "mut ",
206 None => "",
207 };
208 let msg = format!(
209 "unnamed receiver type is only allowed if the surrounding \
210 extern block contains exactly one extern type; \
211 use `self: &{mutability}TheType`",
212 mutability = mutability,
213 );
214 cx.error(span, msg);
215 } else if !cx.types.structs.contains_key(&receiver.ty)
David Tolnay8b60bf12020-04-22 16:52:19 -0700216 && !cx.types.cxx.contains(&receiver.ty)
217 && !cx.types.rust.contains(&receiver.ty)
218 {
David Tolnay8b60bf12020-04-22 16:52:19 -0700219 cx.error(span, "unrecognized receiver type");
220 }
221
David Tolnayd763f4c2020-04-22 16:09:19 -0700222 if receiver.lifetime.is_some() {
David Tolnayd763f4c2020-04-22 16:09:19 -0700223 cx.error(span, "references with explicit lifetimes are not supported");
224 }
225 }
226
David Tolnayd4e68302020-03-25 12:04:17 -0700227 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700228 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700229 let desc = describe(cx, &arg.ty);
230 let msg = format!("passing {} by value is not supported", desc);
231 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700232 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700233 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700234 if efn.lang == Lang::Rust {
235 cx.error(
236 arg,
237 "passing a function pointer from C++ to Rust is not implemented yet",
238 );
239 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700240 }
David Tolnayd4e68302020-03-25 12:04:17 -0700241 }
David Tolnaya420f012020-03-25 17:55:56 -0700242
David Tolnayd4e68302020-03-25 12:04:17 -0700243 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700244 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700245 let desc = describe(cx, ty);
246 let msg = format!("returning {} by value is not supported", desc);
247 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700248 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700249 if let Type::Fn(_) = ty {
250 cx.error(ty, "returning a function pointer is not implemented yet");
251 }
David Tolnayd4e68302020-03-25 12:04:17 -0700252 }
David Tolnay96a826b2020-05-04 00:17:12 -0700253
254 if efn.lang == Lang::Cxx {
255 check_mut_return_restriction(cx, efn);
256 }
257
258 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -0400259}
260
David Tolnay26a2a1d2020-03-25 17:26:43 -0700261fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400262 match &efn.ret {
263 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700264 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400265 }
266
267 for arg in &efn.args {
268 if let Type::Ref(ty) = &arg.ty {
269 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700270 return;
David Tolnay7db73692019-10-20 14:51:12 -0400271 }
272 }
273 }
274
David Tolnaya420f012020-03-25 17:55:56 -0700275 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400276 efn,
277 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700278 );
David Tolnay7db73692019-10-20 14:51:12 -0400279}
280
David Tolnay26a2a1d2020-03-25 17:26:43 -0700281fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400282 match &efn.ret {
283 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700284 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400285 }
286
287 let mut reference_args = 0;
288 for arg in &efn.args {
289 if let Type::Ref(_) = &arg.ty {
290 reference_args += 1;
291 }
292 }
293
Joel Galenson3d4f6122020-04-07 15:54:05 -0700294 if efn.receiver.is_some() {
295 reference_args += 1;
296 }
297
David Tolnay26a2a1d2020-03-25 17:26:43 -0700298 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700299 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400300 efn,
301 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700302 );
David Tolnay7db73692019-10-20 14:51:12 -0400303 }
304}
305
David Tolnay0b368ae2020-04-22 17:55:02 -0700306fn check_reserved_name(cx: &mut Check, ident: &Ident) {
David Tolnay99c93d82020-04-25 14:07:44 -0700307 if ident == "Box"
308 || ident == "UniquePtr"
309 || ident == "Vec"
310 || ident == "CxxVector"
311 || Atom::from(ident).is_some()
312 {
David Tolnay0b368ae2020-04-22 17:55:02 -0700313 cx.error(ident, "reserved name");
314 }
315}
316
David Tolnay26a2a1d2020-03-25 17:26:43 -0700317fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700318 let ident = match ty {
319 Type::Ident(ident) => ident,
David Tolnaye70303c2020-04-25 15:02:37 -0700320 Type::CxxVector(_) | Type::Slice(_) | Type::Void(_) => return true,
David Tolnayd4e68302020-03-25 12:04:17 -0700321 _ => return false,
322 };
David Tolnay26a2a1d2020-03-25 17:26:43 -0700323 ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
324}
325
David Tolnaya420f012020-03-25 17:55:56 -0700326fn span_for_struct_error(strct: &Struct) -> TokenStream {
327 let struct_token = strct.struct_token;
328 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
329 brace_token.set_span(strct.brace_token.span);
330 quote!(#struct_token #brace_token)
331}
332
Joel Galensonc03402a2020-04-23 17:31:09 -0700333fn span_for_enum_error(enm: &Enum) -> TokenStream {
334 let enum_token = enm.enum_token;
335 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
336 brace_token.set_span(enm.brace_token.span);
337 quote!(#enum_token #brace_token)
338}
339
David Tolnayd763f4c2020-04-22 16:09:19 -0700340fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
341 let ampersand = receiver.ampersand;
342 let lifetime = &receiver.lifetime;
343 let mutability = receiver.mutability;
344 if receiver.shorthand {
345 let var = receiver.var;
346 quote!(#ampersand #lifetime #mutability #var)
347 } else {
348 let ty = &receiver.ty;
349 quote!(#ampersand #lifetime #mutability #ty)
350 }
351}
352
David Tolnaya420f012020-03-25 17:55:56 -0700353fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400354 match ty {
355 Type::Ident(ident) => {
David Tolnaya420f012020-03-25 17:55:56 -0700356 if cx.types.structs.contains_key(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400357 "struct".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700358 } else if cx.types.cxx.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400359 "C++ type".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700360 } else if cx.types.rust.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400361 "opaque Rust type".to_owned()
362 } else if Atom::from(ident) == Some(CxxString) {
363 "C++ string".to_owned()
364 } else {
365 ident.to_string()
366 }
367 }
368 Type::RustBox(_) => "Box".to_owned(),
Myron Ahneba35cf2020-02-05 19:41:51 +0700369 Type::RustVec(_) => "Vec".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400370 Type::UniquePtr(_) => "unique_ptr".to_owned(),
371 Type::Ref(_) => "reference".to_owned(),
372 Type::Str(_) => "&str".to_owned(),
David Tolnayfa85fce2020-04-25 15:03:25 -0700373 Type::CxxVector(_) => "C++ vector".to_owned(),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700374 Type::Slice(_) => "slice".to_owned(),
375 Type::SliceRefU8(_) => "&[u8]".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700376 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700377 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400378 }
379}