blob: 742ecc2ad083fe8f82b0c1472b05e3ce92e80d14 [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<()> {
8 let mut errors = Vec::new();
9
10 for ty in types {
11 match ty {
12 Type::Ident(ident) => {
13 if Atom::from(ident).is_none()
14 && !types.structs.contains_key(ident)
15 && !types.cxx.contains(ident)
16 && !types.rust.contains(ident)
17 {
18 errors.push(unsupported_type(ident));
19 }
20 }
21 Type::RustBox(ptr) => {
22 if let Type::Ident(ident) = &ptr.inner {
23 if types.cxx.contains(ident) {
24 errors.push(unsupported_cxx_type_in_box(ptr));
25 }
26 if Atom::from(ident).is_none() {
27 continue;
28 }
29 }
30 errors.push(unsupported_box_target(ptr));
31 }
32 Type::UniquePtr(ptr) => {
33 if let Type::Ident(ident) = &ptr.inner {
34 if types.rust.contains(ident) {
35 errors.push(unsupported_rust_type_in_unique_ptr(ptr));
36 }
37 match Atom::from(ident) {
38 None | Some(CxxString) => continue,
39 _ => {}
40 }
41 }
42 errors.push(unsupported_unique_ptr_target(ptr));
43 }
David Tolnay1fa1ae42020-03-15 23:39:58 -070044 Type::Ref(ty) => {
45 if let Type::Void(_) = ty.inner {
46 errors.push(unsupported_reference_type(ty));
47 }
48 }
David Tolnayc071b892020-03-18 16:59:53 -070049 Type::Fn(_) => errors.push(unimplemented_fn_type(ty)),
David Tolnay7db73692019-10-20 14:51:12 -040050 _ => {}
51 }
52 }
53
54 for api in apis {
55 match api {
56 Api::Struct(strct) => {
David Tolnay09462ac2020-03-20 14:58:41 -070057 if strct.fields.is_empty() {
58 errors.push(struct_empty(strct));
59 }
David Tolnay7db73692019-10-20 14:51:12 -040060 for field in &strct.fields {
61 if is_unsized(&field.ty, types) {
62 errors.push(field_by_value(field, types));
63 }
64 }
65 }
66 Api::CxxFunction(efn) | Api::RustFunction(efn) => {
67 for arg in &efn.args {
68 if is_unsized(&arg.ty, types) {
69 errors.push(argument_by_value(arg, types));
70 }
71 }
72 if let Some(ty) = &efn.ret {
73 if is_unsized(ty, types) {
74 errors.push(return_by_value(ty, types));
75 }
76 }
77 }
78 _ => {}
79 }
80 }
81
82 for api in apis {
83 if let Api::CxxFunction(efn) = api {
84 errors.extend(check_mut_return_restriction(efn).err());
85 }
86 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
87 errors.extend(check_multiple_arg_lifetimes(efn).err());
88 }
89 }
90
91 ident::check_all(apis, &mut errors);
92
93 let mut iter = errors.into_iter();
94 let mut all_errors = match iter.next() {
95 Some(err) => err,
96 None => return Ok(()),
97 };
98 for err in iter {
99 all_errors.combine(err);
100 }
101 Err(all_errors)
102}
103
104fn is_unsized(ty: &Type, types: &Types) -> bool {
105 let ident = match ty {
106 Type::Ident(ident) => ident,
David Tolnay1fa1ae42020-03-15 23:39:58 -0700107 Type::Void(_) => return true,
David Tolnay7db73692019-10-20 14:51:12 -0400108 _ => return false,
109 };
David Tolnaya52602b2020-03-06 10:24:34 -0800110 ident == CxxString || types.cxx.contains(ident) || types.rust.contains(ident)
David Tolnay7db73692019-10-20 14:51:12 -0400111}
112
113fn check_mut_return_restriction(efn: &ExternFn) -> Result<()> {
114 match &efn.ret {
115 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
116 _ => return Ok(()),
117 }
118
119 for arg in &efn.args {
120 if let Type::Ref(ty) = &arg.ty {
121 if ty.mutability.is_some() {
122 return Ok(());
123 }
124 }
125 }
126
127 Err(Error::new_spanned(
128 efn,
129 "&mut return type is not allowed unless there is a &mut argument",
130 ))
131}
132
133fn check_multiple_arg_lifetimes(efn: &ExternFn) -> Result<()> {
134 match &efn.ret {
135 Some(Type::Ref(_)) => {}
136 _ => return Ok(()),
137 }
138
139 let mut reference_args = 0;
140 for arg in &efn.args {
141 if let Type::Ref(_) = &arg.ty {
142 reference_args += 1;
143 }
144 }
145
146 if reference_args == 1 {
147 Ok(())
148 } else {
149 Err(Error::new_spanned(
150 efn,
151 "functions that return a reference must take exactly one input reference",
152 ))
153 }
154}
155
156fn describe(ty: &Type, types: &Types) -> String {
157 match ty {
158 Type::Ident(ident) => {
159 if types.structs.contains_key(ident) {
160 "struct".to_owned()
161 } else if types.cxx.contains(ident) {
162 "C++ type".to_owned()
163 } else if types.rust.contains(ident) {
164 "opaque Rust type".to_owned()
165 } else if Atom::from(ident) == Some(CxxString) {
166 "C++ string".to_owned()
167 } else {
168 ident.to_string()
169 }
170 }
171 Type::RustBox(_) => "Box".to_owned(),
172 Type::UniquePtr(_) => "unique_ptr".to_owned(),
173 Type::Ref(_) => "reference".to_owned(),
174 Type::Str(_) => "&str".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700175 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700176 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400177 }
178}
179
180fn unsupported_type(ident: &Ident) -> Error {
181 Error::new(ident.span(), "unsupported type")
182}
183
David Tolnay1fa1ae42020-03-15 23:39:58 -0700184fn unsupported_reference_type(ty: &Ref) -> Error {
185 Error::new_spanned(ty, "unsupported reference type")
186}
187
David Tolnay7db73692019-10-20 14:51:12 -0400188fn unsupported_cxx_type_in_box(unique_ptr: &Ty1) -> Error {
189 Error::new_spanned(unique_ptr, error::BOX_CXX_TYPE.msg)
190}
191
192fn unsupported_box_target(unique_ptr: &Ty1) -> Error {
193 Error::new_spanned(unique_ptr, "unsupported target type of Box")
194}
195
196fn unsupported_rust_type_in_unique_ptr(unique_ptr: &Ty1) -> Error {
197 Error::new_spanned(unique_ptr, "unique_ptr of a Rust type is not supported yet")
198}
199
200fn unsupported_unique_ptr_target(unique_ptr: &Ty1) -> Error {
201 Error::new_spanned(unique_ptr, "unsupported unique_ptr target type")
202}
203
David Tolnay09462ac2020-03-20 14:58:41 -0700204fn struct_empty(strct: &Struct) -> Error {
205 let struct_token = strct.struct_token;
206 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
207 brace_token.set_span(strct.brace_token.span);
208 let span = quote!(#struct_token #brace_token);
209 Error::new_spanned(span, "structs without any fields are not supported")
210}
211
David Tolnay7db73692019-10-20 14:51:12 -0400212fn field_by_value(field: &Var, types: &Types) -> Error {
213 let desc = describe(&field.ty, types);
214 let message = format!("using {} by value is not supported", desc);
215 Error::new_spanned(field, message)
216}
217
218fn argument_by_value(arg: &Var, types: &Types) -> Error {
219 let desc = describe(&arg.ty, types);
220 let message = format!("passing {} by value is not supported", desc);
221 Error::new_spanned(arg, message)
222}
223
224fn return_by_value(ty: &Type, types: &Types) -> Error {
225 let desc = describe(ty, types);
226 let message = format!("returning {} by value is not supported", desc);
227 Error::new_spanned(ty, message)
228}
David Tolnayc071b892020-03-18 16:59:53 -0700229
230fn unimplemented_fn_type(ty: &Type) -> Error {
231 Error::new_spanned(ty, "function pointer support is not implemented yet")
232}