blob: b44d0cc2b271c3d31e5849e273cfb6659eda31ec [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::syntax::atom::Atom::{self, *};
David Tolnaya420f012020-03-25 17:55:56 -07002use crate::syntax::{error, ident, Api, ExternFn, Ref, Struct, Ty1, Type, Types};
David Tolnay09462ac2020-03-20 14:58:41 -07003use proc_macro2::{Delimiter, Group, Ident, TokenStream};
David Tolnaya420f012020-03-25 17:55:56 -07004use quote::{quote, ToTokens};
5use std::fmt::Display;
David Tolnay7db73692019-10-20 14:51:12 -04006use syn::{Error, Result};
7
David Tolnay26a2a1d2020-03-25 17:26:43 -07008struct Check<'a> {
9 apis: &'a [Api],
10 types: &'a Types<'a>,
11 errors: &'a mut Vec<Error>,
12}
13
David Tolnay7db73692019-10-20 14:51:12 -040014pub(crate) fn typecheck(apis: &[Api], types: &Types) -> Result<()> {
David Tolnay26a2a1d2020-03-25 17:26:43 -070015 let mut errors = Vec::new();
16 let mut cx = Check {
17 apis,
18 types,
19 errors: &mut errors,
20 };
21 do_typecheck(&mut cx);
22 combine_errors(errors)
23}
David Tolnay7db73692019-10-20 14:51:12 -040024
David Tolnay26a2a1d2020-03-25 17:26:43 -070025fn do_typecheck(cx: &mut Check) {
26 for ty in cx.types {
David Tolnay7db73692019-10-20 14:51:12 -040027 match ty {
David Tolnay26a2a1d2020-03-25 17:26:43 -070028 Type::Ident(ident) => check_type_ident(cx, ident),
29 Type::RustBox(ptr) => check_type_box(cx, ptr),
30 Type::UniquePtr(ptr) => check_type_unique_ptr(cx, ptr),
31 Type::Ref(ty) => check_type_ref(cx, ty),
David Tolnay7db73692019-10-20 14:51:12 -040032 _ => {}
33 }
34 }
35
David Tolnay26a2a1d2020-03-25 17:26:43 -070036 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040037 match api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070038 Api::Struct(strct) => check_api_struct(cx, strct),
39 Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn),
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 if let Api::CxxFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070046 check_mut_return_restriction(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040047 }
48 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070049 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040050 }
51 }
52
David Tolnay26a2a1d2020-03-25 17:26:43 -070053 ident::check_all(cx.apis, cx.errors);
David Tolnay7db73692019-10-20 14:51:12 -040054}
55
David Tolnaya420f012020-03-25 17:55:56 -070056impl Check<'_> {
57 fn error(&mut self, sp: impl ToTokens, msg: impl Display) {
58 self.errors.push(Error::new_spanned(sp, msg));
59 }
60}
61
David Tolnay26a2a1d2020-03-25 17:26:43 -070062fn check_type_ident(cx: &mut Check, ident: &Ident) {
David Tolnayd4e68302020-03-25 12:04:17 -070063 if Atom::from(ident).is_none()
David Tolnay26a2a1d2020-03-25 17:26:43 -070064 && !cx.types.structs.contains_key(ident)
65 && !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 Tolnay26a2a1d2020-03-25 17:26:43 -070086fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070087 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -070088 if cx.types.rust.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -070089 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayd4e68302020-03-25 12:04:17 -070090 }
David Tolnaya420f012020-03-25 17:55:56 -070091
David Tolnayd4e68302020-03-25 12:04:17 -070092 match Atom::from(ident) {
93 None | Some(CxxString) => return,
94 _ => {}
95 }
96 }
David Tolnaya420f012020-03-25 17:55:56 -070097
98 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -070099}
100
David Tolnay26a2a1d2020-03-25 17:26:43 -0700101fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700102 match ty.inner {
103 Type::Fn(_) | Type::Void(_) => {}
104 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700105 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700106
107 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700108}
109
David Tolnay26a2a1d2020-03-25 17:26:43 -0700110fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnayd4e68302020-03-25 12:04:17 -0700111 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700112 let span = span_for_struct_error(strct);
113 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700114 }
David Tolnaya420f012020-03-25 17:55:56 -0700115
David Tolnayd4e68302020-03-25 12:04:17 -0700116 for field in &strct.fields {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700117 if is_unsized(cx, &field.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700118 let desc = describe(cx, &field.ty);
119 let msg = format!("using {} by value is not supported", desc);
120 cx.error(field, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700121 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700122 if let Type::Fn(_) = field.ty {
123 cx.error(
124 field,
125 "function pointers in a struct field are not implemented yet",
126 );
127 }
David Tolnayd4e68302020-03-25 12:04:17 -0700128 }
129}
130
David Tolnay26a2a1d2020-03-25 17:26:43 -0700131fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnayd4e68302020-03-25 12:04:17 -0700132 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700133 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700134 let desc = describe(cx, &arg.ty);
135 let msg = format!("passing {} by value is not supported", desc);
136 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700137 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700138 if let Type::Fn(_) = arg.ty {
139 cx.error(
140 arg,
141 "passing a function pointer argument is not implemented yet",
142 );
143 }
David Tolnayd4e68302020-03-25 12:04:17 -0700144 }
David Tolnaya420f012020-03-25 17:55:56 -0700145
David Tolnayd4e68302020-03-25 12:04:17 -0700146 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700147 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700148 let desc = describe(cx, ty);
149 let msg = format!("returning {} by value is not supported", desc);
150 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700151 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700152 if let Type::Fn(_) = ty {
153 cx.error(ty, "returning a function pointer is not implemented yet");
154 }
David Tolnayd4e68302020-03-25 12:04:17 -0700155 }
David Tolnay7db73692019-10-20 14:51:12 -0400156}
157
David Tolnay26a2a1d2020-03-25 17:26:43 -0700158fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400159 match &efn.ret {
160 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700161 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400162 }
163
164 for arg in &efn.args {
165 if let Type::Ref(ty) = &arg.ty {
166 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700167 return;
David Tolnay7db73692019-10-20 14:51:12 -0400168 }
169 }
170 }
171
David Tolnaya420f012020-03-25 17:55:56 -0700172 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400173 efn,
174 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700175 );
David Tolnay7db73692019-10-20 14:51:12 -0400176}
177
David Tolnay26a2a1d2020-03-25 17:26:43 -0700178fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400179 match &efn.ret {
180 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700181 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400182 }
183
184 let mut reference_args = 0;
185 for arg in &efn.args {
186 if let Type::Ref(_) = &arg.ty {
187 reference_args += 1;
188 }
189 }
190
David Tolnay26a2a1d2020-03-25 17:26:43 -0700191 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700192 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400193 efn,
194 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700195 );
David Tolnay7db73692019-10-20 14:51:12 -0400196 }
197}
198
David Tolnay26a2a1d2020-03-25 17:26:43 -0700199fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700200 let ident = match ty {
201 Type::Ident(ident) => ident,
202 Type::Void(_) => return true,
203 _ => return false,
204 };
David Tolnay26a2a1d2020-03-25 17:26:43 -0700205 ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
206}
207
David Tolnaya420f012020-03-25 17:55:56 -0700208fn span_for_struct_error(strct: &Struct) -> TokenStream {
209 let struct_token = strct.struct_token;
210 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
211 brace_token.set_span(strct.brace_token.span);
212 quote!(#struct_token #brace_token)
213}
214
David Tolnay26a2a1d2020-03-25 17:26:43 -0700215fn combine_errors(errors: Vec<Error>) -> Result<()> {
216 let mut iter = errors.into_iter();
217 let mut all_errors = match iter.next() {
218 Some(err) => err,
219 None => return Ok(()),
220 };
221 for err in iter {
222 all_errors.combine(err);
223 }
224 Err(all_errors)
David Tolnayd4e68302020-03-25 12:04:17 -0700225}
226
David Tolnaya420f012020-03-25 17:55:56 -0700227fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400228 match ty {
229 Type::Ident(ident) => {
David Tolnaya420f012020-03-25 17:55:56 -0700230 if cx.types.structs.contains_key(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400231 "struct".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700232 } else if cx.types.cxx.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400233 "C++ type".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700234 } else if cx.types.rust.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400235 "opaque Rust type".to_owned()
236 } else if Atom::from(ident) == Some(CxxString) {
237 "C++ string".to_owned()
238 } else {
239 ident.to_string()
240 }
241 }
242 Type::RustBox(_) => "Box".to_owned(),
243 Type::UniquePtr(_) => "unique_ptr".to_owned(),
244 Type::Ref(_) => "reference".to_owned(),
245 Type::Str(_) => "&str".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700246 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700247 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400248 }
249}