| 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 | |
| 7 | pub(crate) fn typecheck(apis: &[Api], types: &Types) -> Result<()> { |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame^] | 8 | let ref mut errors = Vec::new(); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 9 | |
| 10 | for ty in types { |
| 11 | match ty { |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame^] | 12 | 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 Tolnay | c071b89 | 2020-03-18 16:59:53 -0700 | [diff] [blame] | 16 | Type::Fn(_) => errors.push(unimplemented_fn_type(ty)), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 17 | _ => {} |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | for api in apis { |
| 22 | match api { |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame^] | 23 | Api::Struct(strct) => check_api_struct(errors, types, strct), |
| 24 | Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(errors, types, efn), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 25 | _ => {} |
| 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 Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame^] | 38 | ident::check_all(apis, errors); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 39 | |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame^] | 40 | let mut iter = errors.drain(..); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 41 | 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 Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame^] | 51 | fn 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 | |
| 61 | fn 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 | |
| 73 | fn 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 | |
| 86 | fn 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 | |
| 92 | fn 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 | |
| 103 | fn 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 Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | fn 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 | |
| 136 | fn 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 Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame^] | 159 | fn 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 Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 168 | fn 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 Tolnay | 417305a | 2020-03-18 13:54:00 -0700 | [diff] [blame] | 187 | Type::Fn(_) => "function pointer".to_owned(), |
| David Tolnay | 2fb14e9 | 2020-03-15 23:11:38 -0700 | [diff] [blame] | 188 | Type::Void(_) => "()".to_owned(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | fn unsupported_type(ident: &Ident) -> Error { |
| 193 | Error::new(ident.span(), "unsupported type") |
| 194 | } |
| 195 | |
| David Tolnay | 1fa1ae4 | 2020-03-15 23:39:58 -0700 | [diff] [blame] | 196 | fn unsupported_reference_type(ty: &Ref) -> Error { |
| 197 | Error::new_spanned(ty, "unsupported reference type") |
| 198 | } |
| 199 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 200 | fn unsupported_cxx_type_in_box(unique_ptr: &Ty1) -> Error { |
| 201 | Error::new_spanned(unique_ptr, error::BOX_CXX_TYPE.msg) |
| 202 | } |
| 203 | |
| 204 | fn unsupported_box_target(unique_ptr: &Ty1) -> Error { |
| 205 | Error::new_spanned(unique_ptr, "unsupported target type of Box") |
| 206 | } |
| 207 | |
| 208 | fn 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 | |
| 212 | fn unsupported_unique_ptr_target(unique_ptr: &Ty1) -> Error { |
| 213 | Error::new_spanned(unique_ptr, "unsupported unique_ptr target type") |
| 214 | } |
| 215 | |
| David Tolnay | 09462ac | 2020-03-20 14:58:41 -0700 | [diff] [blame] | 216 | fn 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 Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 224 | fn 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 | |
| 230 | fn 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 | |
| 236 | fn 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 Tolnay | c071b89 | 2020-03-18 16:59:53 -0700 | [diff] [blame] | 241 | |
| 242 | fn unimplemented_fn_type(ty: &Type) -> Error { |
| 243 | Error::new_spanned(ty, "function pointer support is not implemented yet") |
| 244 | } |