forked from M-Labs/nac3
1
0
Fork 0

[core] toplevel/composer: Reduce lock scope while analyzing function

This commit is contained in:
David Mak 2024-10-05 15:53:20 +08:00
parent 581b2f7bb2
commit 1a197c67f6
1 changed files with 280 additions and 268 deletions

View File

@ -485,7 +485,7 @@ impl TopLevelComposer {
// things like `class A(Generic[T, V, ImportedModule.T])` is not supported // things like `class A(Generic[T, V, ImportedModule.T])` is not supported
// i.e. only simple names are allowed in the subscript // i.e. only simple names are allowed in the subscript
// should update the TopLevelDef::Class.typevars and the TypeEnum::TObj.params // should update the TopLevelDef::Class.typevars and the TypeEnum::TObj.params
ast::ExprKind::Subscript { value, slice, .. } ExprKind::Subscript { value, slice, .. }
if { if {
matches!( matches!(
&value.node, &value.node,
@ -501,9 +501,9 @@ impl TopLevelComposer {
} }
is_generic = true; is_generic = true;
let type_var_list: Vec<&ast::Expr<()>>; let type_var_list: Vec<&Expr<()>>;
// if `class A(Generic[T, V, G])` // if `class A(Generic[T, V, G])`
if let ast::ExprKind::Tuple { elts, .. } = &slice.node { if let ExprKind::Tuple { elts, .. } = &slice.node {
type_var_list = elts.iter().collect_vec(); type_var_list = elts.iter().collect_vec();
// `class A(Generic[T])` // `class A(Generic[T])`
} else { } else {
@ -1014,15 +1014,15 @@ impl TopLevelComposer {
} }
} }
let arg_with_default: Vec<(&ast::Located<ast::ArgData<()>>, Option<&ast::Expr>)> = let arg_with_default: Vec<(&ast::Located<ast::ArgData<()>>, Option<&Expr>)> = args
args.args .args
.iter() .iter()
.rev() .rev()
.zip( .zip(
args.defaults args.defaults
.iter() .iter()
.rev() .rev()
.map(|x| -> Option<&ast::Expr> { Some(x) }) .map(|x| -> Option<&Expr> { Some(x) })
.chain(std::iter::repeat(None)), .chain(std::iter::repeat(None)),
) )
.collect_vec(); .collect_vec();
@ -1283,7 +1283,7 @@ impl TopLevelComposer {
let arg_with_default: Vec<( let arg_with_default: Vec<(
&ast::Located<ast::ArgData<()>>, &ast::Located<ast::ArgData<()>>,
Option<&ast::Expr>, Option<&Expr>,
)> = args )> = args
.args .args
.iter() .iter()
@ -1292,7 +1292,7 @@ impl TopLevelComposer {
args.defaults args.defaults
.iter() .iter()
.rev() .rev()
.map(|x| -> Option<&ast::Expr> { Some(x) }) .map(|x| -> Option<&Expr> { Some(x) })
.chain(std::iter::repeat(None)), .chain(std::iter::repeat(None)),
) )
.collect_vec(); .collect_vec();
@ -1449,7 +1449,7 @@ impl TopLevelComposer {
.map_err(|e| HashSet::from([e.to_display(unifier).to_string()]))?; .map_err(|e| HashSet::from([e.to_display(unifier).to_string()]))?;
} }
ast::StmtKind::AnnAssign { target, annotation, value, .. } => { ast::StmtKind::AnnAssign { target, annotation, value, .. } => {
if let ast::ExprKind::Name { id: attr, .. } = &target.node { if let ExprKind::Name { id: attr, .. } = &target.node {
if defined_fields.insert(attr.to_string()) { if defined_fields.insert(attr.to_string()) {
let dummy_field_type = unifier.get_dummy_var().ty; let dummy_field_type = unifier.get_dummy_var().ty;
@ -1457,7 +1457,7 @@ impl TopLevelComposer {
None => { None => {
// handle Kernel[T], KernelInvariant[T] // handle Kernel[T], KernelInvariant[T]
let (annotation, mutable) = match &annotation.node { let (annotation, mutable) = match &annotation.node {
ast::ExprKind::Subscript { value, slice, .. } ExprKind::Subscript { value, slice, .. }
if matches!( if matches!(
&value.node, &value.node,
ast::ExprKind::Name { id, .. } if id == &core_config.kernel_invariant_ann.into() ast::ExprKind::Name { id, .. } if id == &core_config.kernel_invariant_ann.into()
@ -1465,7 +1465,7 @@ impl TopLevelComposer {
{ {
(slice, false) (slice, false)
} }
ast::ExprKind::Subscript { value, slice, .. } ExprKind::Subscript { value, slice, .. }
if matches!( if matches!(
&value.node, &value.node,
ast::ExprKind::Name { id, .. } if core_config.kernel_ann.map_or(false, |c| id == &c.into()) ast::ExprKind::Name { id, .. } if core_config.kernel_ann.map_or(false, |c| id == &c.into())
@ -1483,13 +1483,13 @@ impl TopLevelComposer {
Some(boxed_expr) => { Some(boxed_expr) => {
// Class attributes are set as immutable regardless // Class attributes are set as immutable regardless
let (annotation, _) = match &annotation.node { let (annotation, _) = match &annotation.node {
ast::ExprKind::Subscript { slice, .. } => (slice, false), ExprKind::Subscript { slice, .. } => (slice, false),
_ if core_config.kernel_ann.is_none() => (annotation, false), _ if core_config.kernel_ann.is_none() => (annotation, false),
_ => continue, _ => continue,
}; };
match &**boxed_expr { match &**boxed_expr {
ast::Located {location: _, custom: (), node: ast::ExprKind::Constant { value: v, kind: _ }} => { ast::Located {location: _, custom: (), node: ExprKind::Constant { value: v, kind: _ }} => {
// Restricting the types allowed to be defined as class attributes // Restricting the types allowed to be defined as class attributes
match v { match v {
ast::Constant::Bool(_) | ast::Constant::Str(_) | ast::Constant::Int(_) | ast::Constant::Float(_) => {} ast::Constant::Bool(_) | ast::Constant::Str(_) | ast::Constant::Int(_) | ast::Constant::Float(_) => {}
@ -1937,20 +1937,20 @@ impl TopLevelComposer {
if ast.is_none() { if ast.is_none() {
return Ok(()); return Ok(());
} }
let mut function_def = def.write();
if let TopLevelDef::Function { let (name, simple_name, signature, resolver) = {
instance_to_stmt, let function_def = def.read();
instance_to_symbol, let TopLevelDef::Function { name, simple_name, signature, resolver, .. } =
name, &*function_def
simple_name, else {
signature, return Ok(());
resolver, };
..
} = &mut *function_def (name.clone(), *simple_name, *signature, resolver.clone())
{ };
let signature_ty_enum = unifier.get_ty(*signature);
let TypeEnum::TFunc(FunSignature { args, ret, vars, .. }) = let signature_ty_enum = unifier.get_ty(signature);
signature_ty_enum.as_ref() let TypeEnum::TFunc(FunSignature { args, ret, vars, .. }) = signature_ty_enum.as_ref()
else { else {
unreachable!("must be typeenum::tfunc") unreachable!("must be typeenum::tfunc")
}; };
@ -2062,8 +2062,7 @@ impl TopLevelComposer {
if self_type.is_some() { if self_type.is_some() {
result.insert("self".into(), IdentifierInfo::default()); result.insert("self".into(), IdentifierInfo::default());
} }
result result.extend(inst_args.iter().map(|x| (x.name, IdentifierInfo::default())));
.extend(inst_args.iter().map(|x| (x.name, IdentifierInfo::default())));
result result
}; };
let mut calls: HashMap<CodeLocation, CallId> = HashMap::new(); let mut calls: HashMap<CodeLocation, CallId> = HashMap::new();
@ -2100,33 +2099,43 @@ impl TopLevelComposer {
else { else {
unreachable!("must be function def ast") unreachable!("must be function def ast")
}; };
if !decorator_list.is_empty()
&& matches!(&decorator_list[0].node,
ast::ExprKind::Name{ id, .. } if id == &"extern".into())
{
instance_to_symbol.insert(String::new(), simple_name.to_string());
continue;
}
if !decorator_list.is_empty()
&& matches!(&decorator_list[0].node,
ast::ExprKind::Name{ id, .. } if id == &"rpc".into())
{
instance_to_symbol.insert(String::new(), simple_name.to_string());
continue;
}
if !decorator_list.is_empty() { if !decorator_list.is_empty() {
if let ast::ExprKind::Call { func, .. } = &decorator_list[0].node { if matches!(&decorator_list[0].node, ExprKind::Name { id, .. } if id == &"extern".into())
if matches!(&func.node,
ast::ExprKind::Name{ id, .. } if id == &"rpc".into())
{ {
let TopLevelDef::Function { instance_to_symbol, .. } = &mut *def.write()
else {
unreachable!()
};
instance_to_symbol.insert(String::new(), simple_name.to_string());
continue;
}
if matches!(&decorator_list[0].node, ExprKind::Name { id, .. } if id == &"rpc".into())
{
let TopLevelDef::Function { instance_to_symbol, .. } = &mut *def.write()
else {
unreachable!()
};
instance_to_symbol.insert(String::new(), simple_name.to_string());
continue;
}
if let ExprKind::Call { func, .. } = &decorator_list[0].node {
if matches!(&func.node, ExprKind::Name { id, .. } if id == &"rpc".into()) {
let TopLevelDef::Function { instance_to_symbol, .. } =
&mut *def.write()
else {
unreachable!()
};
instance_to_symbol.insert(String::new(), simple_name.to_string()); instance_to_symbol.insert(String::new(), simple_name.to_string());
continue; continue;
} }
} }
} }
let fun_body = body let fun_body =
.into_iter() body.into_iter()
.map(|b| inferencer.fold_stmt(b)) .map(|b| inferencer.fold_stmt(b))
.collect::<Result<Vec<_>, _>>()?; .collect::<Result<Vec<_>, _>>()?;
@ -2196,6 +2205,9 @@ impl TopLevelComposer {
)])); )]));
} }
let TopLevelDef::Function { instance_to_stmt, .. } = &mut *def.write() else {
unreachable!()
};
instance_to_stmt.insert( instance_to_stmt.insert(
get_subst_key( get_subst_key(
unifier, unifier,
@ -2211,10 +2223,10 @@ impl TopLevelComposer {
}, },
); );
} }
}
Ok(()) Ok(())
}; };
for (id, (def, ast)) in self.definition_ast_list.iter().enumerate().skip(self.builtin_num) { for (id, (def, ast)) in self.definition_ast_list.iter().enumerate().skip(self.builtin_num) {
if ast.is_none() { if ast.is_none() {
continue; continue;