react_compiler_inference/
align_method_call_scopes.rs1use std::collections::HashMap;
13
14use react_compiler_hir::environment::Environment;
15use react_compiler_hir::{EvaluationOrder, HirFunction, IdentifierId, InstructionValue, ScopeId};
16use react_compiler_utils::DisjointSet;
17
18pub fn align_method_call_scopes(func: &mut HirFunction, env: &mut Environment) {
27 let mut scope_mapping: HashMap<IdentifierId, Option<ScopeId>> = HashMap::new();
29 let mut merged_scopes = DisjointSet::<ScopeId>::new();
30
31 for (_block_id, block) in &func.body.blocks {
33 for &instr_id in &block.instructions {
34 let instr = &func.instructions[instr_id.0 as usize];
35 match &instr.value {
36 InstructionValue::MethodCall { property, .. } => {
37 let lvalue_scope =
38 env.identifiers[instr.lvalue.identifier.0 as usize].scope;
39 let property_scope =
40 env.identifiers[property.identifier.0 as usize].scope;
41
42 match (lvalue_scope, property_scope) {
43 (Some(lvalue_sid), Some(property_sid)) => {
44 merged_scopes.union(&[lvalue_sid, property_sid]);
46 }
47 (Some(lvalue_sid), None) => {
48 scope_mapping
51 .insert(property.identifier, Some(lvalue_sid));
52 }
53 (None, Some(_)) => {
54 scope_mapping.insert(property.identifier, None);
57 }
58 (None, None) => {
59 }
61 }
62 }
63 InstructionValue::FunctionExpression { lowered_func, .. }
64 | InstructionValue::ObjectMethod { lowered_func, .. } => {
65 let func_id = lowered_func.func;
67 let mut inner_func = std::mem::replace(
68 &mut env.functions[func_id.0 as usize],
69 react_compiler_ssa::enter_ssa::placeholder_function(),
70 );
71 align_method_call_scopes(&mut inner_func, env);
72 env.functions[func_id.0 as usize] = inner_func;
73 }
74 _ => {}
75 }
76 }
77 }
78
79 let mut range_updates: HashMap<ScopeId, (EvaluationOrder, EvaluationOrder)> = HashMap::new();
83
84 merged_scopes.for_each(|scope_id, root_id| {
85 if scope_id == root_id {
86 return;
87 }
88 let scope_range = env.scopes[scope_id.0 as usize].range.clone();
89 let root_range = env.scopes[root_id.0 as usize].range.clone();
90
91 let entry = range_updates
92 .entry(root_id)
93 .or_insert_with(|| (root_range.start, root_range.end));
94 entry.0 = EvaluationOrder(std::cmp::min(entry.0 .0, scope_range.start.0));
95 entry.1 = EvaluationOrder(std::cmp::max(entry.1 .0, scope_range.end.0));
96 });
97
98 let original_range_ids: HashMap<ScopeId, react_compiler_hir::MutableRangeId> = range_updates
100 .keys()
101 .map(|&root_id| {
102 let range_id = env.scopes[root_id.0 as usize].range.id;
103 (root_id, range_id)
104 })
105 .collect();
106
107 for (root_id, (new_start, new_end)) in &range_updates {
108 env.scopes[root_id.0 as usize].range.start = *new_start;
109 env.scopes[root_id.0 as usize].range.end = *new_end;
110 }
111
112 for ident in &mut env.identifiers {
115 if let Some(scope_id) = ident.scope {
116 if let Some(&orig_range_id) = original_range_ids.get(&scope_id) {
117 if ident.mutable_range.id == orig_range_id {
118 let new_range = &env.scopes[scope_id.0 as usize].range;
119 ident.mutable_range.start = new_range.start;
120 ident.mutable_range.end = new_range.end;
121 }
122 }
123 }
124 }
125
126 for (_block_id, block) in &func.body.blocks {
128 for &instr_id in &block.instructions {
129 let lvalue_id = func.instructions[instr_id.0 as usize].lvalue.identifier;
130
131 if let Some(mapped_scope) = scope_mapping.get(&lvalue_id) {
132 env.identifiers[lvalue_id.0 as usize].scope = *mapped_scope;
133 } else if let Some(current_scope) =
134 env.identifiers[lvalue_id.0 as usize].scope
135 {
136 if let Some(merged) = merged_scopes.find_opt(current_scope) {
138 env.identifiers[lvalue_id.0 as usize].scope = Some(merged);
139 }
140 }
141 }
142 }
143}