mirror of
https://gitee.com/openharmony/third_party_rust_cxx
synced 2024-11-24 07:40:19 +00:00
Collect typecheck context into a struct
This commit is contained in:
parent
d4e6830d40
commit
26a2a1deda
139
syntax/check.rs
139
syntax/check.rs
@ -4,139 +4,144 @@ use proc_macro2::{Delimiter, Group, Ident, TokenStream};
|
||||
use quote::quote;
|
||||
use syn::{Error, Result};
|
||||
|
||||
struct Check<'a> {
|
||||
apis: &'a [Api],
|
||||
types: &'a Types<'a>,
|
||||
errors: &'a mut Vec<Error>,
|
||||
}
|
||||
|
||||
pub(crate) fn typecheck(apis: &[Api], types: &Types) -> Result<()> {
|
||||
let ref mut errors = Vec::new();
|
||||
let mut errors = Vec::new();
|
||||
let mut cx = Check {
|
||||
apis,
|
||||
types,
|
||||
errors: &mut errors,
|
||||
};
|
||||
do_typecheck(&mut cx);
|
||||
combine_errors(errors)
|
||||
}
|
||||
|
||||
for ty in types {
|
||||
fn do_typecheck(cx: &mut Check) {
|
||||
for ty in cx.types {
|
||||
match ty {
|
||||
Type::Ident(ident) => check_type_ident(errors, types, ident),
|
||||
Type::RustBox(ptr) => check_type_box(errors, types, ptr),
|
||||
Type::UniquePtr(ptr) => check_type_unique_ptr(errors, types, ptr),
|
||||
Type::Ref(ty) => check_type_ref(errors, ty),
|
||||
Type::Fn(_) => errors.push(unimplemented_fn_type(ty)),
|
||||
Type::Ident(ident) => check_type_ident(cx, ident),
|
||||
Type::RustBox(ptr) => check_type_box(cx, ptr),
|
||||
Type::UniquePtr(ptr) => check_type_unique_ptr(cx, ptr),
|
||||
Type::Ref(ty) => check_type_ref(cx, ty),
|
||||
Type::Fn(_) => cx.errors.push(unimplemented_fn_type(ty)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
for api in apis {
|
||||
for api in cx.apis {
|
||||
match api {
|
||||
Api::Struct(strct) => check_api_struct(errors, types, strct),
|
||||
Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(errors, types, efn),
|
||||
Api::Struct(strct) => check_api_struct(cx, strct),
|
||||
Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
for api in apis {
|
||||
for api in cx.apis {
|
||||
if let Api::CxxFunction(efn) = api {
|
||||
errors.extend(check_mut_return_restriction(efn).err());
|
||||
check_mut_return_restriction(cx, efn);
|
||||
}
|
||||
if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
|
||||
errors.extend(check_multiple_arg_lifetimes(efn).err());
|
||||
check_multiple_arg_lifetimes(cx, efn);
|
||||
}
|
||||
}
|
||||
|
||||
ident::check_all(apis, errors);
|
||||
|
||||
let mut iter = errors.drain(..);
|
||||
let mut all_errors = match iter.next() {
|
||||
Some(err) => err,
|
||||
None => return Ok(()),
|
||||
};
|
||||
for err in iter {
|
||||
all_errors.combine(err);
|
||||
}
|
||||
Err(all_errors)
|
||||
ident::check_all(cx.apis, cx.errors);
|
||||
}
|
||||
|
||||
fn check_type_ident(errors: &mut Vec<Error>, types: &Types, ident: &Ident) {
|
||||
fn check_type_ident(cx: &mut Check, ident: &Ident) {
|
||||
if Atom::from(ident).is_none()
|
||||
&& !types.structs.contains_key(ident)
|
||||
&& !types.cxx.contains(ident)
|
||||
&& !types.rust.contains(ident)
|
||||
&& !cx.types.structs.contains_key(ident)
|
||||
&& !cx.types.cxx.contains(ident)
|
||||
&& !cx.types.rust.contains(ident)
|
||||
{
|
||||
errors.push(unsupported_type(ident));
|
||||
cx.errors.push(unsupported_type(ident));
|
||||
}
|
||||
}
|
||||
|
||||
fn check_type_box(errors: &mut Vec<Error>, types: &Types, ptr: &Ty1) {
|
||||
fn check_type_box(cx: &mut Check, ptr: &Ty1) {
|
||||
if let Type::Ident(ident) = &ptr.inner {
|
||||
if types.cxx.contains(ident) {
|
||||
errors.push(unsupported_cxx_type_in_box(ptr));
|
||||
if cx.types.cxx.contains(ident) {
|
||||
cx.errors.push(unsupported_cxx_type_in_box(ptr));
|
||||
}
|
||||
if Atom::from(ident).is_none() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
errors.push(unsupported_box_target(ptr));
|
||||
cx.errors.push(unsupported_box_target(ptr));
|
||||
}
|
||||
|
||||
fn check_type_unique_ptr(errors: &mut Vec<Error>, types: &Types, ptr: &Ty1) {
|
||||
fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
|
||||
if let Type::Ident(ident) = &ptr.inner {
|
||||
if types.rust.contains(ident) {
|
||||
errors.push(unsupported_rust_type_in_unique_ptr(ptr));
|
||||
if cx.types.rust.contains(ident) {
|
||||
cx.errors.push(unsupported_rust_type_in_unique_ptr(ptr));
|
||||
}
|
||||
match Atom::from(ident) {
|
||||
None | Some(CxxString) => return,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
errors.push(unsupported_unique_ptr_target(ptr));
|
||||
cx.errors.push(unsupported_unique_ptr_target(ptr));
|
||||
}
|
||||
|
||||
fn check_type_ref(errors: &mut Vec<Error>, ty: &Ref) {
|
||||
fn check_type_ref(cx: &mut Check, ty: &Ref) {
|
||||
if let Type::Void(_) = ty.inner {
|
||||
errors.push(unsupported_reference_type(ty));
|
||||
cx.errors.push(unsupported_reference_type(ty));
|
||||
}
|
||||
}
|
||||
|
||||
fn check_api_struct(errors: &mut Vec<Error>, types: &Types, strct: &Struct) {
|
||||
fn check_api_struct(cx: &mut Check, strct: &Struct) {
|
||||
if strct.fields.is_empty() {
|
||||
errors.push(struct_empty(strct));
|
||||
cx.errors.push(struct_empty(strct));
|
||||
}
|
||||
for field in &strct.fields {
|
||||
if is_unsized(&field.ty, types) {
|
||||
errors.push(field_by_value(field, types));
|
||||
if is_unsized(cx, &field.ty) {
|
||||
cx.errors.push(field_by_value(field, cx.types));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_api_fn(errors: &mut Vec<Error>, types: &Types, efn: &ExternFn) {
|
||||
fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
|
||||
for arg in &efn.args {
|
||||
if is_unsized(&arg.ty, types) {
|
||||
errors.push(argument_by_value(arg, types));
|
||||
if is_unsized(cx, &arg.ty) {
|
||||
cx.errors.push(argument_by_value(arg, cx.types));
|
||||
}
|
||||
}
|
||||
if let Some(ty) = &efn.ret {
|
||||
if is_unsized(ty, types) {
|
||||
errors.push(return_by_value(ty, types));
|
||||
if is_unsized(cx, ty) {
|
||||
cx.errors.push(return_by_value(ty, cx.types));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_mut_return_restriction(efn: &ExternFn) -> Result<()> {
|
||||
fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
|
||||
match &efn.ret {
|
||||
Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
|
||||
_ => return Ok(()),
|
||||
_ => return,
|
||||
}
|
||||
|
||||
for arg in &efn.args {
|
||||
if let Type::Ref(ty) = &arg.ty {
|
||||
if ty.mutability.is_some() {
|
||||
return Ok(());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Err(Error::new_spanned(
|
||||
cx.errors.push(Error::new_spanned(
|
||||
efn,
|
||||
"&mut return type is not allowed unless there is a &mut argument",
|
||||
))
|
||||
));
|
||||
}
|
||||
|
||||
fn check_multiple_arg_lifetimes(efn: &ExternFn) -> Result<()> {
|
||||
fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
|
||||
match &efn.ret {
|
||||
Some(Type::Ref(_)) => {}
|
||||
_ => return Ok(()),
|
||||
_ => return,
|
||||
}
|
||||
|
||||
let mut reference_args = 0;
|
||||
@ -146,23 +151,33 @@ fn check_multiple_arg_lifetimes(efn: &ExternFn) -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
if reference_args == 1 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::new_spanned(
|
||||
if reference_args != 1 {
|
||||
cx.errors.push(Error::new_spanned(
|
||||
efn,
|
||||
"functions that return a reference must take exactly one input reference",
|
||||
))
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
fn is_unsized(ty: &Type, types: &Types) -> bool {
|
||||
fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
|
||||
let ident = match ty {
|
||||
Type::Ident(ident) => ident,
|
||||
Type::Void(_) => return true,
|
||||
_ => return false,
|
||||
};
|
||||
ident == CxxString || types.cxx.contains(ident) || types.rust.contains(ident)
|
||||
ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
|
||||
}
|
||||
|
||||
fn combine_errors(errors: Vec<Error>) -> Result<()> {
|
||||
let mut iter = errors.into_iter();
|
||||
let mut all_errors = match iter.next() {
|
||||
Some(err) => err,
|
||||
None => return Ok(()),
|
||||
};
|
||||
for err in iter {
|
||||
all_errors.combine(err);
|
||||
}
|
||||
Err(all_errors)
|
||||
}
|
||||
|
||||
fn describe(ty: &Type, types: &Types) -> String {
|
||||
|
Loading…
Reference in New Issue
Block a user