| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1 | use crate::syntax::atom::Atom::{self, *}; |
| 2 | use crate::syntax::{error, ident, Api, ExternFn, Ty1, Type, Types, Var}; |
| 3 | use proc_macro2::Ident; |
| 4 | use syn::{Error, Result}; |
| 5 | |
| 6 | pub(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 | } |
| 43 | _ => {} |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | for api in apis { |
| 48 | match api { |
| 49 | Api::Struct(strct) => { |
| 50 | for field in &strct.fields { |
| 51 | if is_unsized(&field.ty, types) { |
| 52 | errors.push(field_by_value(field, types)); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | Api::CxxFunction(efn) | Api::RustFunction(efn) => { |
| 57 | for arg in &efn.args { |
| 58 | if is_unsized(&arg.ty, types) { |
| 59 | errors.push(argument_by_value(arg, types)); |
| 60 | } |
| 61 | } |
| 62 | if let Some(ty) = &efn.ret { |
| 63 | if is_unsized(ty, types) { |
| 64 | errors.push(return_by_value(ty, types)); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | _ => {} |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | for api in apis { |
| 73 | if let Api::CxxFunction(efn) = api { |
| 74 | errors.extend(check_mut_return_restriction(efn).err()); |
| 75 | } |
| 76 | if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api { |
| 77 | errors.extend(check_multiple_arg_lifetimes(efn).err()); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | ident::check_all(apis, &mut errors); |
| 82 | |
| 83 | let mut iter = errors.into_iter(); |
| 84 | let mut all_errors = match iter.next() { |
| 85 | Some(err) => err, |
| 86 | None => return Ok(()), |
| 87 | }; |
| 88 | for err in iter { |
| 89 | all_errors.combine(err); |
| 90 | } |
| 91 | Err(all_errors) |
| 92 | } |
| 93 | |
| 94 | fn is_unsized(ty: &Type, types: &Types) -> bool { |
| 95 | let ident = match ty { |
| 96 | Type::Ident(ident) => ident, |
| 97 | _ => return false, |
| 98 | }; |
| David Tolnay | a52602b | 2020-03-06 10:24:34 -0800 | [diff] [blame] | 99 | ident == CxxString || types.cxx.contains(ident) || types.rust.contains(ident) |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | fn check_mut_return_restriction(efn: &ExternFn) -> Result<()> { |
| 103 | match &efn.ret { |
| 104 | Some(Type::Ref(ty)) if ty.mutability.is_some() => {} |
| 105 | _ => return Ok(()), |
| 106 | } |
| 107 | |
| 108 | for arg in &efn.args { |
| 109 | if let Type::Ref(ty) = &arg.ty { |
| 110 | if ty.mutability.is_some() { |
| 111 | return Ok(()); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | Err(Error::new_spanned( |
| 117 | efn, |
| 118 | "&mut return type is not allowed unless there is a &mut argument", |
| 119 | )) |
| 120 | } |
| 121 | |
| 122 | fn check_multiple_arg_lifetimes(efn: &ExternFn) -> Result<()> { |
| 123 | match &efn.ret { |
| 124 | Some(Type::Ref(_)) => {} |
| 125 | _ => return Ok(()), |
| 126 | } |
| 127 | |
| 128 | let mut reference_args = 0; |
| 129 | for arg in &efn.args { |
| 130 | if let Type::Ref(_) = &arg.ty { |
| 131 | reference_args += 1; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if reference_args == 1 { |
| 136 | Ok(()) |
| 137 | } else { |
| 138 | Err(Error::new_spanned( |
| 139 | efn, |
| 140 | "functions that return a reference must take exactly one input reference", |
| 141 | )) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | fn describe(ty: &Type, types: &Types) -> String { |
| 146 | match ty { |
| 147 | Type::Ident(ident) => { |
| 148 | if types.structs.contains_key(ident) { |
| 149 | "struct".to_owned() |
| 150 | } else if types.cxx.contains(ident) { |
| 151 | "C++ type".to_owned() |
| 152 | } else if types.rust.contains(ident) { |
| 153 | "opaque Rust type".to_owned() |
| 154 | } else if Atom::from(ident) == Some(CxxString) { |
| 155 | "C++ string".to_owned() |
| 156 | } else { |
| 157 | ident.to_string() |
| 158 | } |
| 159 | } |
| 160 | Type::RustBox(_) => "Box".to_owned(), |
| 161 | Type::UniquePtr(_) => "unique_ptr".to_owned(), |
| 162 | Type::Ref(_) => "reference".to_owned(), |
| 163 | Type::Str(_) => "&str".to_owned(), |
| David Tolnay | 2fb14e9 | 2020-03-15 23:11:38 -0700 | [diff] [blame^] | 164 | Type::Void(_) => "()".to_owned(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | fn unsupported_type(ident: &Ident) -> Error { |
| 169 | Error::new(ident.span(), "unsupported type") |
| 170 | } |
| 171 | |
| 172 | fn unsupported_cxx_type_in_box(unique_ptr: &Ty1) -> Error { |
| 173 | Error::new_spanned(unique_ptr, error::BOX_CXX_TYPE.msg) |
| 174 | } |
| 175 | |
| 176 | fn unsupported_box_target(unique_ptr: &Ty1) -> Error { |
| 177 | Error::new_spanned(unique_ptr, "unsupported target type of Box") |
| 178 | } |
| 179 | |
| 180 | fn unsupported_rust_type_in_unique_ptr(unique_ptr: &Ty1) -> Error { |
| 181 | Error::new_spanned(unique_ptr, "unique_ptr of a Rust type is not supported yet") |
| 182 | } |
| 183 | |
| 184 | fn unsupported_unique_ptr_target(unique_ptr: &Ty1) -> Error { |
| 185 | Error::new_spanned(unique_ptr, "unsupported unique_ptr target type") |
| 186 | } |
| 187 | |
| 188 | fn field_by_value(field: &Var, types: &Types) -> Error { |
| 189 | let desc = describe(&field.ty, types); |
| 190 | let message = format!("using {} by value is not supported", desc); |
| 191 | Error::new_spanned(field, message) |
| 192 | } |
| 193 | |
| 194 | fn argument_by_value(arg: &Var, types: &Types) -> Error { |
| 195 | let desc = describe(&arg.ty, types); |
| 196 | let message = format!("passing {} by value is not supported", desc); |
| 197 | Error::new_spanned(arg, message) |
| 198 | } |
| 199 | |
| 200 | fn return_by_value(ty: &Type, types: &Types) -> Error { |
| 201 | let desc = describe(ty, types); |
| 202 | let message = format!("returning {} by value is not supported", desc); |
| 203 | Error::new_spanned(ty, message) |
| 204 | } |