blob: 1fe3daf560b1e528afb54b709ec4d3314579e6fd [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::syntax::atom::Atom::{self, *};
David Tolnayebef4a22020-03-17 15:33:47 -07002use crate::syntax::{error, ident, Api, ExternFn, Ref, Ty1, Type, Types, Var};
David Tolnay7db73692019-10-20 14:51:12 -04003use proc_macro2::Ident;
4use syn::{Error, Result};
5
6pub(crate) fn typecheck(apis: &[Api], types: &Types) -> Result<()> {
7 let mut errors = Vec::new();
8
9 for ty in types {
10 match ty {
11 Type::Ident(ident) => {
12 if Atom::from(ident).is_none()
13 && !types.structs.contains_key(ident)
14 && !types.cxx.contains(ident)
15 && !types.rust.contains(ident)
16 {
17 errors.push(unsupported_type(ident));
18 }
19 }
20 Type::RustBox(ptr) => {
21 if let Type::Ident(ident) = &ptr.inner {
22 if types.cxx.contains(ident) {
23 errors.push(unsupported_cxx_type_in_box(ptr));
24 }
25 if Atom::from(ident).is_none() {
26 continue;
27 }
28 }
29 errors.push(unsupported_box_target(ptr));
30 }
31 Type::UniquePtr(ptr) => {
32 if let Type::Ident(ident) = &ptr.inner {
33 if types.rust.contains(ident) {
34 errors.push(unsupported_rust_type_in_unique_ptr(ptr));
35 }
36 match Atom::from(ident) {
37 None | Some(CxxString) => continue,
38 _ => {}
39 }
40 }
41 errors.push(unsupported_unique_ptr_target(ptr));
42 }
David Tolnay1fa1ae42020-03-15 23:39:58 -070043 Type::Ref(ty) => {
44 if let Type::Void(_) = ty.inner {
45 errors.push(unsupported_reference_type(ty));
46 }
47 }
David Tolnayc071b892020-03-18 16:59:53 -070048 Type::Fn(_) => errors.push(unimplemented_fn_type(ty)),
David Tolnay7db73692019-10-20 14:51:12 -040049 _ => {}
50 }
51 }
52
53 for api in apis {
54 match api {
55 Api::Struct(strct) => {
56 for field in &strct.fields {
57 if is_unsized(&field.ty, types) {
58 errors.push(field_by_value(field, types));
59 }
60 }
61 }
62 Api::CxxFunction(efn) | Api::RustFunction(efn) => {
63 for arg in &efn.args {
64 if is_unsized(&arg.ty, types) {
65 errors.push(argument_by_value(arg, types));
66 }
67 }
68 if let Some(ty) = &efn.ret {
69 if is_unsized(ty, types) {
70 errors.push(return_by_value(ty, types));
71 }
72 }
73 }
74 _ => {}
75 }
76 }
77
78 for api in apis {
79 if let Api::CxxFunction(efn) = api {
80 errors.extend(check_mut_return_restriction(efn).err());
81 }
82 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
83 errors.extend(check_multiple_arg_lifetimes(efn).err());
84 }
85 }
86
87 ident::check_all(apis, &mut errors);
88
89 let mut iter = errors.into_iter();
90 let mut all_errors = match iter.next() {
91 Some(err) => err,
92 None => return Ok(()),
93 };
94 for err in iter {
95 all_errors.combine(err);
96 }
97 Err(all_errors)
98}
99
100fn is_unsized(ty: &Type, types: &Types) -> bool {
101 let ident = match ty {
102 Type::Ident(ident) => ident,
David Tolnay1fa1ae42020-03-15 23:39:58 -0700103 Type::Void(_) => return true,
David Tolnay7db73692019-10-20 14:51:12 -0400104 _ => return false,
105 };
David Tolnaya52602b2020-03-06 10:24:34 -0800106 ident == CxxString || types.cxx.contains(ident) || types.rust.contains(ident)
David Tolnay7db73692019-10-20 14:51:12 -0400107}
108
109fn check_mut_return_restriction(efn: &ExternFn) -> Result<()> {
110 match &efn.ret {
111 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
112 _ => return Ok(()),
113 }
114
115 for arg in &efn.args {
116 if let Type::Ref(ty) = &arg.ty {
117 if ty.mutability.is_some() {
118 return Ok(());
119 }
120 }
121 }
122
123 Err(Error::new_spanned(
124 efn,
125 "&mut return type is not allowed unless there is a &mut argument",
126 ))
127}
128
129fn check_multiple_arg_lifetimes(efn: &ExternFn) -> Result<()> {
130 match &efn.ret {
131 Some(Type::Ref(_)) => {}
132 _ => return Ok(()),
133 }
134
135 let mut reference_args = 0;
136 for arg in &efn.args {
137 if let Type::Ref(_) = &arg.ty {
138 reference_args += 1;
139 }
140 }
141
142 if reference_args == 1 {
143 Ok(())
144 } else {
145 Err(Error::new_spanned(
146 efn,
147 "functions that return a reference must take exactly one input reference",
148 ))
149 }
150}
151
152fn describe(ty: &Type, types: &Types) -> String {
153 match ty {
154 Type::Ident(ident) => {
155 if types.structs.contains_key(ident) {
156 "struct".to_owned()
157 } else if types.cxx.contains(ident) {
158 "C++ type".to_owned()
159 } else if types.rust.contains(ident) {
160 "opaque Rust type".to_owned()
161 } else if Atom::from(ident) == Some(CxxString) {
162 "C++ string".to_owned()
163 } else {
164 ident.to_string()
165 }
166 }
167 Type::RustBox(_) => "Box".to_owned(),
168 Type::UniquePtr(_) => "unique_ptr".to_owned(),
169 Type::Ref(_) => "reference".to_owned(),
170 Type::Str(_) => "&str".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700171 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700172 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400173 }
174}
175
176fn unsupported_type(ident: &Ident) -> Error {
177 Error::new(ident.span(), "unsupported type")
178}
179
David Tolnay1fa1ae42020-03-15 23:39:58 -0700180fn unsupported_reference_type(ty: &Ref) -> Error {
181 Error::new_spanned(ty, "unsupported reference type")
182}
183
David Tolnay7db73692019-10-20 14:51:12 -0400184fn unsupported_cxx_type_in_box(unique_ptr: &Ty1) -> Error {
185 Error::new_spanned(unique_ptr, error::BOX_CXX_TYPE.msg)
186}
187
188fn unsupported_box_target(unique_ptr: &Ty1) -> Error {
189 Error::new_spanned(unique_ptr, "unsupported target type of Box")
190}
191
192fn unsupported_rust_type_in_unique_ptr(unique_ptr: &Ty1) -> Error {
193 Error::new_spanned(unique_ptr, "unique_ptr of a Rust type is not supported yet")
194}
195
196fn unsupported_unique_ptr_target(unique_ptr: &Ty1) -> Error {
197 Error::new_spanned(unique_ptr, "unsupported unique_ptr target type")
198}
199
200fn field_by_value(field: &Var, types: &Types) -> Error {
201 let desc = describe(&field.ty, types);
202 let message = format!("using {} by value is not supported", desc);
203 Error::new_spanned(field, message)
204}
205
206fn argument_by_value(arg: &Var, types: &Types) -> Error {
207 let desc = describe(&arg.ty, types);
208 let message = format!("passing {} by value is not supported", desc);
209 Error::new_spanned(arg, message)
210}
211
212fn return_by_value(ty: &Type, types: &Types) -> Error {
213 let desc = describe(ty, types);
214 let message = format!("returning {} by value is not supported", desc);
215 Error::new_spanned(ty, message)
216}
David Tolnayc071b892020-03-18 16:59:53 -0700217
218fn unimplemented_fn_type(ty: &Type) -> Error {
219 Error::new_spanned(ty, "function pointer support is not implemented yet")
220}