blob: 256a1933af2cb0c32f5530c8b1590395249aa2a7 [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 Tolnaya420f012020-03-25 17:55:56 -070032 Type::Fn(_) => cx.error(ty, "function pointer support is not implemented yet"),
David Tolnay7db73692019-10-20 14:51:12 -040033 _ => {}
34 }
35 }
36
David Tolnay26a2a1d2020-03-25 17:26:43 -070037 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040038 match api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070039 Api::Struct(strct) => check_api_struct(cx, strct),
40 Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn),
David Tolnay7db73692019-10-20 14:51:12 -040041 _ => {}
42 }
43 }
44
David Tolnay26a2a1d2020-03-25 17:26:43 -070045 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040046 if let Api::CxxFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070047 check_mut_return_restriction(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040048 }
49 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070050 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040051 }
52 }
53
David Tolnay26a2a1d2020-03-25 17:26:43 -070054 ident::check_all(cx.apis, cx.errors);
David Tolnay7db73692019-10-20 14:51:12 -040055}
56
David Tolnaya420f012020-03-25 17:55:56 -070057impl Check<'_> {
58 fn error(&mut self, sp: impl ToTokens, msg: impl Display) {
59 self.errors.push(Error::new_spanned(sp, msg));
60 }
61}
62
David Tolnay26a2a1d2020-03-25 17:26:43 -070063fn check_type_ident(cx: &mut Check, ident: &Ident) {
David Tolnayd4e68302020-03-25 12:04:17 -070064 if Atom::from(ident).is_none()
David Tolnay26a2a1d2020-03-25 17:26:43 -070065 && !cx.types.structs.contains_key(ident)
66 && !cx.types.cxx.contains(ident)
67 && !cx.types.rust.contains(ident)
David Tolnayd4e68302020-03-25 12:04:17 -070068 {
David Tolnaya420f012020-03-25 17:55:56 -070069 cx.error(ident, "unsupported type");
David Tolnayd4e68302020-03-25 12:04:17 -070070 }
71}
72
David Tolnay26a2a1d2020-03-25 17:26:43 -070073fn check_type_box(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070074 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -070075 if cx.types.cxx.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -070076 cx.error(ptr, error::BOX_CXX_TYPE.msg);
David Tolnayd4e68302020-03-25 12:04:17 -070077 }
David Tolnaya420f012020-03-25 17:55:56 -070078
David Tolnayd4e68302020-03-25 12:04:17 -070079 if Atom::from(ident).is_none() {
80 return;
81 }
82 }
David Tolnaya420f012020-03-25 17:55:56 -070083
84 cx.error(ptr, "unsupported target type of Box");
David Tolnayd4e68302020-03-25 12:04:17 -070085}
86
David Tolnay26a2a1d2020-03-25 17:26:43 -070087fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070088 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -070089 if cx.types.rust.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -070090 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayd4e68302020-03-25 12:04:17 -070091 }
David Tolnaya420f012020-03-25 17:55:56 -070092
David Tolnayd4e68302020-03-25 12:04:17 -070093 match Atom::from(ident) {
94 None | Some(CxxString) => return,
95 _ => {}
96 }
97 }
David Tolnaya420f012020-03-25 17:55:56 -070098
99 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -0700100}
101
David Tolnay26a2a1d2020-03-25 17:26:43 -0700102fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnayd4e68302020-03-25 12:04:17 -0700103 if let Type::Void(_) = ty.inner {
David Tolnaya420f012020-03-25 17:55:56 -0700104 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700105 }
106}
107
David Tolnay26a2a1d2020-03-25 17:26:43 -0700108fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnayd4e68302020-03-25 12:04:17 -0700109 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700110 let span = span_for_struct_error(strct);
111 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700112 }
David Tolnaya420f012020-03-25 17:55:56 -0700113
David Tolnayd4e68302020-03-25 12:04:17 -0700114 for field in &strct.fields {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700115 if is_unsized(cx, &field.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700116 let desc = describe(cx, &field.ty);
117 let msg = format!("using {} by value is not supported", desc);
118 cx.error(field, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700119 }
120 }
121}
122
David Tolnay26a2a1d2020-03-25 17:26:43 -0700123fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnayd4e68302020-03-25 12:04:17 -0700124 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700125 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700126 let desc = describe(cx, &arg.ty);
127 let msg = format!("passing {} by value is not supported", desc);
128 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700129 }
130 }
David Tolnaya420f012020-03-25 17:55:56 -0700131
David Tolnayd4e68302020-03-25 12:04:17 -0700132 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700133 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700134 let desc = describe(cx, ty);
135 let msg = format!("returning {} by value is not supported", desc);
136 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700137 }
138 }
David Tolnay7db73692019-10-20 14:51:12 -0400139}
140
David Tolnay26a2a1d2020-03-25 17:26:43 -0700141fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400142 match &efn.ret {
143 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700144 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400145 }
146
147 for arg in &efn.args {
148 if let Type::Ref(ty) = &arg.ty {
149 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700150 return;
David Tolnay7db73692019-10-20 14:51:12 -0400151 }
152 }
153 }
154
David Tolnaya420f012020-03-25 17:55:56 -0700155 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400156 efn,
157 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700158 );
David Tolnay7db73692019-10-20 14:51:12 -0400159}
160
David Tolnay26a2a1d2020-03-25 17:26:43 -0700161fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400162 match &efn.ret {
163 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700164 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400165 }
166
167 let mut reference_args = 0;
168 for arg in &efn.args {
169 if let Type::Ref(_) = &arg.ty {
170 reference_args += 1;
171 }
172 }
173
David Tolnay26a2a1d2020-03-25 17:26:43 -0700174 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700175 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400176 efn,
177 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700178 );
David Tolnay7db73692019-10-20 14:51:12 -0400179 }
180}
181
David Tolnay26a2a1d2020-03-25 17:26:43 -0700182fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700183 let ident = match ty {
184 Type::Ident(ident) => ident,
185 Type::Void(_) => return true,
186 _ => return false,
187 };
David Tolnay26a2a1d2020-03-25 17:26:43 -0700188 ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
189}
190
David Tolnaya420f012020-03-25 17:55:56 -0700191fn span_for_struct_error(strct: &Struct) -> TokenStream {
192 let struct_token = strct.struct_token;
193 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
194 brace_token.set_span(strct.brace_token.span);
195 quote!(#struct_token #brace_token)
196}
197
David Tolnay26a2a1d2020-03-25 17:26:43 -0700198fn combine_errors(errors: Vec<Error>) -> Result<()> {
199 let mut iter = errors.into_iter();
200 let mut all_errors = match iter.next() {
201 Some(err) => err,
202 None => return Ok(()),
203 };
204 for err in iter {
205 all_errors.combine(err);
206 }
207 Err(all_errors)
David Tolnayd4e68302020-03-25 12:04:17 -0700208}
209
David Tolnaya420f012020-03-25 17:55:56 -0700210fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400211 match ty {
212 Type::Ident(ident) => {
David Tolnaya420f012020-03-25 17:55:56 -0700213 if cx.types.structs.contains_key(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400214 "struct".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700215 } else if cx.types.cxx.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400216 "C++ type".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700217 } else if cx.types.rust.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400218 "opaque Rust type".to_owned()
219 } else if Atom::from(ident) == Some(CxxString) {
220 "C++ string".to_owned()
221 } else {
222 ident.to_string()
223 }
224 }
225 Type::RustBox(_) => "Box".to_owned(),
226 Type::UniquePtr(_) => "unique_ptr".to_owned(),
227 Type::Ref(_) => "reference".to_owned(),
228 Type::Str(_) => "&str".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700229 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700230 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400231 }
232}