use std::collections::HashMap;
use react_compiler_hir::environment::Environment;
use react_compiler_hir::{EvaluationOrder, HirFunction, IdentifierId, InstructionValue, ScopeId};
use react_compiler_utils::DisjointSet;
pub fn align_method_call_scopes(func: &mut HirFunction, env: &mut Environment) {
let mut scope_mapping: HashMap<IdentifierId, Option<ScopeId>> = HashMap::new();
let mut merged_scopes = DisjointSet::<ScopeId>::new();
for (_block_id, block) in &func.body.blocks {
for &instr_id in &block.instructions {
let instr = &func.instructions[instr_id.0 as usize];
match &instr.value {
InstructionValue::MethodCall { property, .. } => {
let lvalue_scope =
env.identifiers[instr.lvalue.identifier.0 as usize].scope;
let property_scope =
env.identifiers[property.identifier.0 as usize].scope;
match (lvalue_scope, property_scope) {
(Some(lvalue_sid), Some(property_sid)) => {
merged_scopes.union(&[lvalue_sid, property_sid]);
}
(Some(lvalue_sid), None) => {
scope_mapping
.insert(property.identifier, Some(lvalue_sid));
}
(None, Some(_)) => {
scope_mapping.insert(property.identifier, None);
}
(None, None) => {
}
}
}
InstructionValue::FunctionExpression { lowered_func, .. }
| InstructionValue::ObjectMethod { lowered_func, .. } => {
let func_id = lowered_func.func;
let mut inner_func = std::mem::replace(
&mut env.functions[func_id.0 as usize],
react_compiler_ssa::enter_ssa::placeholder_function(),
);
align_method_call_scopes(&mut inner_func, env);
env.functions[func_id.0 as usize] = inner_func;
}
_ => {}
}
}
}
let mut range_updates: HashMap<ScopeId, (EvaluationOrder, EvaluationOrder)> = HashMap::new();
merged_scopes.for_each(|scope_id, root_id| {
if scope_id == root_id {
return;
}
let scope_range = env.scopes[scope_id.0 as usize].range.clone();
let root_range = env.scopes[root_id.0 as usize].range.clone();
let entry = range_updates
.entry(root_id)
.or_insert_with(|| (root_range.start, root_range.end));
entry.0 = EvaluationOrder(std::cmp::min(entry.0 .0, scope_range.start.0));
entry.1 = EvaluationOrder(std::cmp::max(entry.1 .0, scope_range.end.0));
});
let original_range_ids: HashMap<ScopeId, react_compiler_hir::MutableRangeId> = range_updates
.keys()
.map(|&root_id| {
let range_id = env.scopes[root_id.0 as usize].range.id;
(root_id, range_id)
})
.collect();
for (root_id, (new_start, new_end)) in &range_updates {
env.scopes[root_id.0 as usize].range.start = *new_start;
env.scopes[root_id.0 as usize].range.end = *new_end;
}
for ident in &mut env.identifiers {
if let Some(scope_id) = ident.scope {
if let Some(&orig_range_id) = original_range_ids.get(&scope_id) {
if ident.mutable_range.id == orig_range_id {
let new_range = &env.scopes[scope_id.0 as usize].range;
ident.mutable_range.start = new_range.start;
ident.mutable_range.end = new_range.end;
}
}
}
}
for (_block_id, block) in &func.body.blocks {
for &instr_id in &block.instructions {
let lvalue_id = func.instructions[instr_id.0 as usize].lvalue.identifier;
if let Some(mapped_scope) = scope_mapping.get(&lvalue_id) {
env.identifiers[lvalue_id.0 as usize].scope = *mapped_scope;
} else if let Some(current_scope) =
env.identifiers[lvalue_id.0 as usize].scope
{
if let Some(merged) = merged_scopes.find_opt(current_scope) {
env.identifiers[lvalue_id.0 as usize].scope = Some(merged);
}
}
}
}
}