blob: 5a3590ff452bcdc9e4caae16a3a11424b68a97b4 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::syntax::atom::Atom::{self, *};
David Tolnay09462ac2020-03-20 14:58:41 -07002use crate::syntax::{error, ident, Api, ExternFn, Ref, Struct, Ty1, Type, Types, Var};
3use proc_macro2::{Delimiter, Group, Ident, TokenStream};
4use quote::quote;
David Tolnay7db73692019-10-20 14:51:12 -04005use syn::{Error, Result};
6
David Tolnay26a2a1d2020-03-25 17:26:43 -07007struct Check<'a> {
8 apis: &'a [Api],
9 types: &'a Types<'a>,
10 errors: &'a mut Vec<Error>,
11}
12
David Tolnay7db73692019-10-20 14:51:12 -040013pub(crate) fn typecheck(apis: &[Api], types: &Types) -> Result<()> {
David Tolnay26a2a1d2020-03-25 17:26:43 -070014 let mut errors = Vec::new();
15 let mut cx = Check {
16 apis,
17 types,
18 errors: &mut errors,
19 };
20 do_typecheck(&mut cx);
21 combine_errors(errors)
22}
David Tolnay7db73692019-10-20 14:51:12 -040023
David Tolnay26a2a1d2020-03-25 17:26:43 -070024fn do_typecheck(cx: &mut Check) {
25 for ty in cx.types {
David Tolnay7db73692019-10-20 14:51:12 -040026 match ty {
David Tolnay26a2a1d2020-03-25 17:26:43 -070027 Type::Ident(ident) => check_type_ident(cx, ident),
28 Type::RustBox(ptr) => check_type_box(cx, ptr),
29 Type::UniquePtr(ptr) => check_type_unique_ptr(cx, ptr),
30 Type::Ref(ty) => check_type_ref(cx, ty),
31 Type::Fn(_) => cx.errors.push(unimplemented_fn_type(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 Tolnay26a2a1d2020-03-25 17:26:43 -070056fn check_type_ident(cx: &mut Check, ident: &Ident) {
David Tolnayd4e68302020-03-25 12:04:17 -070057 if Atom::from(ident).is_none()
David Tolnay26a2a1d2020-03-25 17:26:43 -070058 && !cx.types.structs.contains_key(ident)
59 && !cx.types.cxx.contains(ident)
60 && !cx.types.rust.contains(ident)
David Tolnayd4e68302020-03-25 12:04:17 -070061 {
David Tolnay26a2a1d2020-03-25 17:26:43 -070062 cx.errors.push(unsupported_type(ident));
David Tolnayd4e68302020-03-25 12:04:17 -070063 }
64}
65
David Tolnay26a2a1d2020-03-25 17:26:43 -070066fn check_type_box(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070067 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -070068 if cx.types.cxx.contains(ident) {
69 cx.errors.push(unsupported_cxx_type_in_box(ptr));
David Tolnayd4e68302020-03-25 12:04:17 -070070 }
71 if Atom::from(ident).is_none() {
72 return;
73 }
74 }
David Tolnay26a2a1d2020-03-25 17:26:43 -070075 cx.errors.push(unsupported_box_target(ptr));
David Tolnayd4e68302020-03-25 12:04:17 -070076}
77
David Tolnay26a2a1d2020-03-25 17:26:43 -070078fn check_type_unique_ptr(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.rust.contains(ident) {
81 cx.errors.push(unsupported_rust_type_in_unique_ptr(ptr));
David Tolnayd4e68302020-03-25 12:04:17 -070082 }
83 match Atom::from(ident) {
84 None | Some(CxxString) => return,
85 _ => {}
86 }
87 }
David Tolnay26a2a1d2020-03-25 17:26:43 -070088 cx.errors.push(unsupported_unique_ptr_target(ptr));
David Tolnayd4e68302020-03-25 12:04:17 -070089}
90
David Tolnay26a2a1d2020-03-25 17:26:43 -070091fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnayd4e68302020-03-25 12:04:17 -070092 if let Type::Void(_) = ty.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -070093 cx.errors.push(unsupported_reference_type(ty));
David Tolnayd4e68302020-03-25 12:04:17 -070094 }
95}
96
David Tolnay26a2a1d2020-03-25 17:26:43 -070097fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnayd4e68302020-03-25 12:04:17 -070098 if strct.fields.is_empty() {
David Tolnay26a2a1d2020-03-25 17:26:43 -070099 cx.errors.push(struct_empty(strct));
David Tolnayd4e68302020-03-25 12:04:17 -0700100 }
101 for field in &strct.fields {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700102 if is_unsized(cx, &field.ty) {
103 cx.errors.push(field_by_value(field, cx.types));
David Tolnayd4e68302020-03-25 12:04:17 -0700104 }
105 }
106}
107
David Tolnay26a2a1d2020-03-25 17:26:43 -0700108fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnayd4e68302020-03-25 12:04:17 -0700109 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700110 if is_unsized(cx, &arg.ty) {
111 cx.errors.push(argument_by_value(arg, cx.types));
David Tolnayd4e68302020-03-25 12:04:17 -0700112 }
113 }
114 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700115 if is_unsized(cx, ty) {
116 cx.errors.push(return_by_value(ty, cx.types));
David Tolnayd4e68302020-03-25 12:04:17 -0700117 }
118 }
David Tolnay7db73692019-10-20 14:51:12 -0400119}
120
David Tolnay26a2a1d2020-03-25 17:26:43 -0700121fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400122 match &efn.ret {
123 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700124 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400125 }
126
127 for arg in &efn.args {
128 if let Type::Ref(ty) = &arg.ty {
129 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700130 return;
David Tolnay7db73692019-10-20 14:51:12 -0400131 }
132 }
133 }
134
David Tolnay26a2a1d2020-03-25 17:26:43 -0700135 cx.errors.push(Error::new_spanned(
David Tolnay7db73692019-10-20 14:51:12 -0400136 efn,
137 "&mut return type is not allowed unless there is a &mut argument",
David Tolnay26a2a1d2020-03-25 17:26:43 -0700138 ));
David Tolnay7db73692019-10-20 14:51:12 -0400139}
140
David Tolnay26a2a1d2020-03-25 17:26:43 -0700141fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400142 match &efn.ret {
143 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700144 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400145 }
146
147 let mut reference_args = 0;
148 for arg in &efn.args {
149 if let Type::Ref(_) = &arg.ty {
150 reference_args += 1;
151 }
152 }
153
David Tolnay26a2a1d2020-03-25 17:26:43 -0700154 if reference_args != 1 {
155 cx.errors.push(Error::new_spanned(
David Tolnay7db73692019-10-20 14:51:12 -0400156 efn,
157 "functions that return a reference must take exactly one input reference",
David Tolnay26a2a1d2020-03-25 17:26:43 -0700158 ));
David Tolnay7db73692019-10-20 14:51:12 -0400159 }
160}
161
David Tolnay26a2a1d2020-03-25 17:26:43 -0700162fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700163 let ident = match ty {
164 Type::Ident(ident) => ident,
165 Type::Void(_) => return true,
166 _ => return false,
167 };
David Tolnay26a2a1d2020-03-25 17:26:43 -0700168 ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
169}
170
171fn combine_errors(errors: Vec<Error>) -> Result<()> {
172 let mut iter = errors.into_iter();
173 let mut all_errors = match iter.next() {
174 Some(err) => err,
175 None => return Ok(()),
176 };
177 for err in iter {
178 all_errors.combine(err);
179 }
180 Err(all_errors)
David Tolnayd4e68302020-03-25 12:04:17 -0700181}
182
David Tolnay7db73692019-10-20 14:51:12 -0400183fn describe(ty: &Type, types: &Types) -> String {
184 match ty {
185 Type::Ident(ident) => {
186 if types.structs.contains_key(ident) {
187 "struct".to_owned()
188 } else if types.cxx.contains(ident) {
189 "C++ type".to_owned()
190 } else if types.rust.contains(ident) {
191 "opaque Rust type".to_owned()
192 } else if Atom::from(ident) == Some(CxxString) {
193 "C++ string".to_owned()
194 } else {
195 ident.to_string()
196 }
197 }
198 Type::RustBox(_) => "Box".to_owned(),
199 Type::UniquePtr(_) => "unique_ptr".to_owned(),
200 Type::Ref(_) => "reference".to_owned(),
201 Type::Str(_) => "&str".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700202 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700203 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400204 }
205}
206
207fn unsupported_type(ident: &Ident) -> Error {
208 Error::new(ident.span(), "unsupported type")
209}
210
David Tolnay1fa1ae42020-03-15 23:39:58 -0700211fn unsupported_reference_type(ty: &Ref) -> Error {
212 Error::new_spanned(ty, "unsupported reference type")
213}
214
David Tolnay7db73692019-10-20 14:51:12 -0400215fn unsupported_cxx_type_in_box(unique_ptr: &Ty1) -> Error {
216 Error::new_spanned(unique_ptr, error::BOX_CXX_TYPE.msg)
217}
218
219fn unsupported_box_target(unique_ptr: &Ty1) -> Error {
220 Error::new_spanned(unique_ptr, "unsupported target type of Box")
221}
222
223fn unsupported_rust_type_in_unique_ptr(unique_ptr: &Ty1) -> Error {
224 Error::new_spanned(unique_ptr, "unique_ptr of a Rust type is not supported yet")
225}
226
227fn unsupported_unique_ptr_target(unique_ptr: &Ty1) -> Error {
228 Error::new_spanned(unique_ptr, "unsupported unique_ptr target type")
229}
230
David Tolnay09462ac2020-03-20 14:58:41 -0700231fn struct_empty(strct: &Struct) -> Error {
232 let struct_token = strct.struct_token;
233 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
234 brace_token.set_span(strct.brace_token.span);
235 let span = quote!(#struct_token #brace_token);
236 Error::new_spanned(span, "structs without any fields are not supported")
237}
238
David Tolnay7db73692019-10-20 14:51:12 -0400239fn field_by_value(field: &Var, types: &Types) -> Error {
240 let desc = describe(&field.ty, types);
241 let message = format!("using {} by value is not supported", desc);
242 Error::new_spanned(field, message)
243}
244
245fn argument_by_value(arg: &Var, types: &Types) -> Error {
246 let desc = describe(&arg.ty, types);
247 let message = format!("passing {} by value is not supported", desc);
248 Error::new_spanned(arg, message)
249}
250
251fn return_by_value(ty: &Type, types: &Types) -> Error {
252 let desc = describe(ty, types);
253 let message = format!("returning {} by value is not supported", desc);
254 Error::new_spanned(ty, message)
255}
David Tolnayc071b892020-03-18 16:59:53 -0700256
257fn unimplemented_fn_type(ty: &Type) -> Error {
258 Error::new_spanned(ty, "function pointer support is not implemented yet")
259}