| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1 | use crate::syntax::atom::Atom::{self, *}; |
| David Tolnay | 09462ac | 2020-03-20 14:58:41 -0700 | [diff] [blame] | 2 | use crate::syntax::{error, ident, Api, ExternFn, Ref, Struct, Ty1, Type, Types, Var}; |
| 3 | use proc_macro2::{Delimiter, Group, Ident, TokenStream}; |
| 4 | use quote::quote; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 5 | use syn::{Error, Result}; |
| 6 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 7 | struct Check<'a> { |
| 8 | apis: &'a [Api], |
| 9 | types: &'a Types<'a>, |
| 10 | errors: &'a mut Vec<Error>, |
| 11 | } |
| 12 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 13 | pub(crate) fn typecheck(apis: &[Api], types: &Types) -> Result<()> { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 14 | 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 Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 23 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 24 | fn do_typecheck(cx: &mut Check) { |
| 25 | for ty in cx.types { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 26 | match ty { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 27 | 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 Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 32 | _ => {} |
| 33 | } |
| 34 | } |
| 35 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 36 | for api in cx.apis { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 37 | match api { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 38 | Api::Struct(strct) => check_api_struct(cx, strct), |
| 39 | Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 40 | _ => {} |
| 41 | } |
| 42 | } |
| 43 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 44 | for api in cx.apis { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 45 | if let Api::CxxFunction(efn) = api { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 46 | check_mut_return_restriction(cx, efn); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 47 | } |
| 48 | if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 49 | check_multiple_arg_lifetimes(cx, efn); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 53 | ident::check_all(cx.apis, cx.errors); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 56 | fn check_type_ident(cx: &mut Check, ident: &Ident) { |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 57 | if Atom::from(ident).is_none() |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 58 | && !cx.types.structs.contains_key(ident) |
| 59 | && !cx.types.cxx.contains(ident) |
| 60 | && !cx.types.rust.contains(ident) |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 61 | { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 62 | cx.errors.push(unsupported_type(ident)); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 66 | fn check_type_box(cx: &mut Check, ptr: &Ty1) { |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 67 | if let Type::Ident(ident) = &ptr.inner { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 68 | if cx.types.cxx.contains(ident) { |
| 69 | cx.errors.push(unsupported_cxx_type_in_box(ptr)); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 70 | } |
| 71 | if Atom::from(ident).is_none() { |
| 72 | return; |
| 73 | } |
| 74 | } |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 75 | cx.errors.push(unsupported_box_target(ptr)); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 78 | fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) { |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 79 | if let Type::Ident(ident) = &ptr.inner { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 80 | if cx.types.rust.contains(ident) { |
| 81 | cx.errors.push(unsupported_rust_type_in_unique_ptr(ptr)); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 82 | } |
| 83 | match Atom::from(ident) { |
| 84 | None | Some(CxxString) => return, |
| 85 | _ => {} |
| 86 | } |
| 87 | } |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 88 | cx.errors.push(unsupported_unique_ptr_target(ptr)); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 91 | fn check_type_ref(cx: &mut Check, ty: &Ref) { |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 92 | if let Type::Void(_) = ty.inner { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 93 | cx.errors.push(unsupported_reference_type(ty)); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 97 | fn check_api_struct(cx: &mut Check, strct: &Struct) { |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 98 | if strct.fields.is_empty() { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 99 | cx.errors.push(struct_empty(strct)); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 100 | } |
| 101 | for field in &strct.fields { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 102 | if is_unsized(cx, &field.ty) { |
| 103 | cx.errors.push(field_by_value(field, cx.types)); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 108 | fn check_api_fn(cx: &mut Check, efn: &ExternFn) { |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 109 | for arg in &efn.args { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 110 | if is_unsized(cx, &arg.ty) { |
| 111 | cx.errors.push(argument_by_value(arg, cx.types)); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | if let Some(ty) = &efn.ret { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 115 | if is_unsized(cx, ty) { |
| 116 | cx.errors.push(return_by_value(ty, cx.types)); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 121 | fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 122 | match &efn.ret { |
| 123 | Some(Type::Ref(ty)) if ty.mutability.is_some() => {} |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 124 | _ => return, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | for arg in &efn.args { |
| 128 | if let Type::Ref(ty) = &arg.ty { |
| 129 | if ty.mutability.is_some() { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 130 | return; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 135 | cx.errors.push(Error::new_spanned( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 136 | efn, |
| 137 | "&mut return type is not allowed unless there is a &mut argument", |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 138 | )); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 139 | } |
| 140 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 141 | fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 142 | match &efn.ret { |
| 143 | Some(Type::Ref(_)) => {} |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 144 | _ => return, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 145 | } |
| 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 Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 154 | if reference_args != 1 { |
| 155 | cx.errors.push(Error::new_spanned( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 156 | efn, |
| 157 | "functions that return a reference must take exactly one input reference", |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 158 | )); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 162 | fn is_unsized(cx: &mut Check, ty: &Type) -> bool { |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 163 | let ident = match ty { |
| 164 | Type::Ident(ident) => ident, |
| 165 | Type::Void(_) => return true, |
| 166 | _ => return false, |
| 167 | }; |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame^] | 168 | ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident) |
| 169 | } |
| 170 | |
| 171 | fn 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 Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 183 | fn 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 Tolnay | 417305a | 2020-03-18 13:54:00 -0700 | [diff] [blame] | 202 | Type::Fn(_) => "function pointer".to_owned(), |
| David Tolnay | 2fb14e9 | 2020-03-15 23:11:38 -0700 | [diff] [blame] | 203 | Type::Void(_) => "()".to_owned(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | fn unsupported_type(ident: &Ident) -> Error { |
| 208 | Error::new(ident.span(), "unsupported type") |
| 209 | } |
| 210 | |
| David Tolnay | 1fa1ae4 | 2020-03-15 23:39:58 -0700 | [diff] [blame] | 211 | fn unsupported_reference_type(ty: &Ref) -> Error { |
| 212 | Error::new_spanned(ty, "unsupported reference type") |
| 213 | } |
| 214 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 215 | fn unsupported_cxx_type_in_box(unique_ptr: &Ty1) -> Error { |
| 216 | Error::new_spanned(unique_ptr, error::BOX_CXX_TYPE.msg) |
| 217 | } |
| 218 | |
| 219 | fn unsupported_box_target(unique_ptr: &Ty1) -> Error { |
| 220 | Error::new_spanned(unique_ptr, "unsupported target type of Box") |
| 221 | } |
| 222 | |
| 223 | fn 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 | |
| 227 | fn unsupported_unique_ptr_target(unique_ptr: &Ty1) -> Error { |
| 228 | Error::new_spanned(unique_ptr, "unsupported unique_ptr target type") |
| 229 | } |
| 230 | |
| David Tolnay | 09462ac | 2020-03-20 14:58:41 -0700 | [diff] [blame] | 231 | fn 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 Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 239 | fn 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 | |
| 245 | fn 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 | |
| 251 | fn 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 Tolnay | c071b89 | 2020-03-18 16:59:53 -0700 | [diff] [blame] | 256 | |
| 257 | fn unimplemented_fn_type(ty: &Type) -> Error { |
| 258 | Error::new_spanned(ty, "function pointer support is not implemented yet") |
| 259 | } |