blob: f5ccba911d289d9c7508f66ec4da7f413e34bb0a [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
7pub(crate) fn typecheck(apis: &[Api], types: &Types) -> Result<()> {
David Tolnayd4e68302020-03-25 12:04:17 -07008 let ref mut errors = Vec::new();
David Tolnay7db73692019-10-20 14:51:12 -04009
10 for ty in types {
11 match ty {
David Tolnayd4e68302020-03-25 12:04:17 -070012 Type::Ident(ident) => check_type_ident(errors, types, ident),
13 Type::RustBox(ptr) => check_type_box(errors, types, ptr),
14 Type::UniquePtr(ptr) => check_type_unique_ptr(errors, types, ptr),
15 Type::Ref(ty) => check_type_ref(errors, ty),
David Tolnayc071b892020-03-18 16:59:53 -070016 Type::Fn(_) => errors.push(unimplemented_fn_type(ty)),
David Tolnay7db73692019-10-20 14:51:12 -040017 _ => {}
18 }
19 }
20
21 for api in apis {
22 match api {
David Tolnayd4e68302020-03-25 12:04:17 -070023 Api::Struct(strct) => check_api_struct(errors, types, strct),
24 Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(errors, types, efn),
David Tolnay7db73692019-10-20 14:51:12 -040025 _ => {}
26 }
27 }
28
29 for api in apis {
30 if let Api::CxxFunction(efn) = api {
31 errors.extend(check_mut_return_restriction(efn).err());
32 }
33 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
34 errors.extend(check_multiple_arg_lifetimes(efn).err());
35 }
36 }
37
David Tolnayd4e68302020-03-25 12:04:17 -070038 ident::check_all(apis, errors);
David Tolnay7db73692019-10-20 14:51:12 -040039
David Tolnayd4e68302020-03-25 12:04:17 -070040 let mut iter = errors.drain(..);
David Tolnay7db73692019-10-20 14:51:12 -040041 let mut all_errors = match iter.next() {
42 Some(err) => err,
43 None => return Ok(()),
44 };
45 for err in iter {
46 all_errors.combine(err);
47 }
48 Err(all_errors)
49}
50
David Tolnayd4e68302020-03-25 12:04:17 -070051fn check_type_ident(errors: &mut Vec<Error>, types: &Types, ident: &Ident) {
52 if Atom::from(ident).is_none()
53 && !types.structs.contains_key(ident)
54 && !types.cxx.contains(ident)
55 && !types.rust.contains(ident)
56 {
57 errors.push(unsupported_type(ident));
58 }
59}
60
61fn check_type_box(errors: &mut Vec<Error>, types: &Types, ptr: &Ty1) {
62 if let Type::Ident(ident) = &ptr.inner {
63 if types.cxx.contains(ident) {
64 errors.push(unsupported_cxx_type_in_box(ptr));
65 }
66 if Atom::from(ident).is_none() {
67 return;
68 }
69 }
70 errors.push(unsupported_box_target(ptr));
71}
72
73fn check_type_unique_ptr(errors: &mut Vec<Error>, types: &Types, ptr: &Ty1) {
74 if let Type::Ident(ident) = &ptr.inner {
75 if types.rust.contains(ident) {
76 errors.push(unsupported_rust_type_in_unique_ptr(ptr));
77 }
78 match Atom::from(ident) {
79 None | Some(CxxString) => return,
80 _ => {}
81 }
82 }
83 errors.push(unsupported_unique_ptr_target(ptr));
84}
85
86fn check_type_ref(errors: &mut Vec<Error>, ty: &Ref) {
87 if let Type::Void(_) = ty.inner {
88 errors.push(unsupported_reference_type(ty));
89 }
90}
91
92fn check_api_struct(errors: &mut Vec<Error>, types: &Types, strct: &Struct) {
93 if strct.fields.is_empty() {
94 errors.push(struct_empty(strct));
95 }
96 for field in &strct.fields {
97 if is_unsized(&field.ty, types) {
98 errors.push(field_by_value(field, types));
99 }
100 }
101}
102
103fn check_api_fn(errors: &mut Vec<Error>, types: &Types, efn: &ExternFn) {
104 for arg in &efn.args {
105 if is_unsized(&arg.ty, types) {
106 errors.push(argument_by_value(arg, types));
107 }
108 }
109 if let Some(ty) = &efn.ret {
110 if is_unsized(ty, types) {
111 errors.push(return_by_value(ty, types));
112 }
113 }
David Tolnay7db73692019-10-20 14:51:12 -0400114}
115
116fn check_mut_return_restriction(efn: &ExternFn) -> Result<()> {
117 match &efn.ret {
118 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
119 _ => return Ok(()),
120 }
121
122 for arg in &efn.args {
123 if let Type::Ref(ty) = &arg.ty {
124 if ty.mutability.is_some() {
125 return Ok(());
126 }
127 }
128 }
129
130 Err(Error::new_spanned(
131 efn,
132 "&mut return type is not allowed unless there is a &mut argument",
133 ))
134}
135
136fn check_multiple_arg_lifetimes(efn: &ExternFn) -> Result<()> {
137 match &efn.ret {
138 Some(Type::Ref(_)) => {}
139 _ => return Ok(()),
140 }
141
142 let mut reference_args = 0;
143 for arg in &efn.args {
144 if let Type::Ref(_) = &arg.ty {
145 reference_args += 1;
146 }
147 }
148
149 if reference_args == 1 {
150 Ok(())
151 } else {
152 Err(Error::new_spanned(
153 efn,
154 "functions that return a reference must take exactly one input reference",
155 ))
156 }
157}
158
David Tolnayd4e68302020-03-25 12:04:17 -0700159fn is_unsized(ty: &Type, types: &Types) -> bool {
160 let ident = match ty {
161 Type::Ident(ident) => ident,
162 Type::Void(_) => return true,
163 _ => return false,
164 };
165 ident == CxxString || types.cxx.contains(ident) || types.rust.contains(ident)
166}
167
David Tolnay7db73692019-10-20 14:51:12 -0400168fn describe(ty: &Type, types: &Types) -> String {
169 match ty {
170 Type::Ident(ident) => {
171 if types.structs.contains_key(ident) {
172 "struct".to_owned()
173 } else if types.cxx.contains(ident) {
174 "C++ type".to_owned()
175 } else if types.rust.contains(ident) {
176 "opaque Rust type".to_owned()
177 } else if Atom::from(ident) == Some(CxxString) {
178 "C++ string".to_owned()
179 } else {
180 ident.to_string()
181 }
182 }
183 Type::RustBox(_) => "Box".to_owned(),
184 Type::UniquePtr(_) => "unique_ptr".to_owned(),
185 Type::Ref(_) => "reference".to_owned(),
186 Type::Str(_) => "&str".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700187 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700188 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400189 }
190}
191
192fn unsupported_type(ident: &Ident) -> Error {
193 Error::new(ident.span(), "unsupported type")
194}
195
David Tolnay1fa1ae42020-03-15 23:39:58 -0700196fn unsupported_reference_type(ty: &Ref) -> Error {
197 Error::new_spanned(ty, "unsupported reference type")
198}
199
David Tolnay7db73692019-10-20 14:51:12 -0400200fn unsupported_cxx_type_in_box(unique_ptr: &Ty1) -> Error {
201 Error::new_spanned(unique_ptr, error::BOX_CXX_TYPE.msg)
202}
203
204fn unsupported_box_target(unique_ptr: &Ty1) -> Error {
205 Error::new_spanned(unique_ptr, "unsupported target type of Box")
206}
207
208fn unsupported_rust_type_in_unique_ptr(unique_ptr: &Ty1) -> Error {
209 Error::new_spanned(unique_ptr, "unique_ptr of a Rust type is not supported yet")
210}
211
212fn unsupported_unique_ptr_target(unique_ptr: &Ty1) -> Error {
213 Error::new_spanned(unique_ptr, "unsupported unique_ptr target type")
214}
215
David Tolnay09462ac2020-03-20 14:58:41 -0700216fn struct_empty(strct: &Struct) -> Error {
217 let struct_token = strct.struct_token;
218 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
219 brace_token.set_span(strct.brace_token.span);
220 let span = quote!(#struct_token #brace_token);
221 Error::new_spanned(span, "structs without any fields are not supported")
222}
223
David Tolnay7db73692019-10-20 14:51:12 -0400224fn field_by_value(field: &Var, types: &Types) -> Error {
225 let desc = describe(&field.ty, types);
226 let message = format!("using {} by value is not supported", desc);
227 Error::new_spanned(field, message)
228}
229
230fn argument_by_value(arg: &Var, types: &Types) -> Error {
231 let desc = describe(&arg.ty, types);
232 let message = format!("passing {} by value is not supported", desc);
233 Error::new_spanned(arg, message)
234}
235
236fn return_by_value(ty: &Type, types: &Types) -> Error {
237 let desc = describe(ty, types);
238 let message = format!("returning {} by value is not supported", desc);
239 Error::new_spanned(ty, message)
240}
David Tolnayc071b892020-03-18 16:59:53 -0700241
242fn unimplemented_fn_type(ty: &Type) -> Error {
243 Error::new_spanned(ty, "function pointer support is not implemented yet")
244}