mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-24 02:31:13 +08:00
321 lines
13 KiB
Rust
321 lines
13 KiB
Rust
//! 表达式 AST 节点的结构哈希。
|
||
|
||
use super::*;
|
||
|
||
pub(super) fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher, include_binding_names: bool) {
|
||
hash_match!(HashDomain::Expression, expr, h, {
|
||
Expression::BinaryExpression(bin) => {
|
||
std::mem::discriminant(&bin.operator).hash(h);
|
||
hash_expression(&bin.left, h, include_binding_names);
|
||
hash_expression(&bin.right, h, include_binding_names);
|
||
}
|
||
Expression::UnaryExpression(un) => {
|
||
std::mem::discriminant(&un.operator).hash(h);
|
||
hash_expression(&un.argument, h, include_binding_names);
|
||
}
|
||
Expression::CallExpression(call) => {
|
||
(call.arguments.len() as u32).hash(h);
|
||
hash_expression(&call.callee, h, include_binding_names);
|
||
// spread 实参改指令形态(CALL 变 CALL_SPREAD)与装载方式,逐位打形态标记。
|
||
for arg in &call.arguments {
|
||
match arg {
|
||
Argument::SpreadElement(sp) => {
|
||
1u8.hash(h);
|
||
hash_expression(&sp.argument, h, include_binding_names);
|
||
}
|
||
other => {
|
||
0u8.hash(h);
|
||
if let Some(expr) = other.as_expression() {
|
||
hash_expression(expr, h, include_binding_names);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
Expression::LogicalExpression(log) => {
|
||
std::mem::discriminant(&log.operator).hash(h);
|
||
hash_expression(&log.left, h, include_binding_names);
|
||
hash_expression(&log.right, h, include_binding_names);
|
||
}
|
||
Expression::ConditionalExpression(cond) => {
|
||
hash_expression(&cond.test, h, include_binding_names);
|
||
hash_expression(&cond.consequent, h, include_binding_names);
|
||
hash_expression(&cond.alternate, h, include_binding_names);
|
||
}
|
||
Expression::PrivateInExpression(pin) => {
|
||
pin.left.name.as_str().hash(h);
|
||
hash_expression(&pin.right, h, include_binding_names);
|
||
}
|
||
Expression::SequenceExpression(seq) => {
|
||
(seq.expressions.len() as u32).hash(h);
|
||
for e in &seq.expressions {
|
||
hash_expression(e, h, include_binding_names);
|
||
}
|
||
}
|
||
Expression::Identifier(ident) => {
|
||
if include_binding_names {
|
||
ident.name.as_str().hash(h);
|
||
}
|
||
}
|
||
Expression::NumericLiteral(num) => {
|
||
num.value.to_bits().hash(h);
|
||
}
|
||
Expression::StringLiteral(s) => {
|
||
s.value.hash(h);
|
||
}
|
||
Expression::BooleanLiteral(b) => {
|
||
b.value.hash(h);
|
||
}
|
||
Expression::AssignmentExpression(assign) => {
|
||
std::mem::discriminant(&assign.operator).hash(h);
|
||
// 全量分派:简单目标与数组/对象解构目标都计入,左值整体不得漏哈希。
|
||
target::hash_assignment_target(&assign.left, h, include_binding_names);
|
||
hash_expression(&assign.right, h, include_binding_names);
|
||
}
|
||
Expression::UpdateExpression(update) => {
|
||
std::mem::discriminant(&update.operator).hash(h);
|
||
update.prefix.hash(h);
|
||
target::hash_simple_assignment_target(&update.argument, h, include_binding_names);
|
||
}
|
||
Expression::TemplateLiteral(tl) => {
|
||
(tl.quasis.len() as u32).hash(h);
|
||
// 段值是字节码依赖(发射侧把 cooked 写入常量池);cooked 为 None 与
|
||
// Some(空串) 的池键都是空串,字节码相同,口径上合并。
|
||
for q in &tl.quasis {
|
||
q.value.cooked.as_deref().unwrap_or("").hash(h);
|
||
}
|
||
for expr in &tl.expressions {
|
||
hash_expression(expr, h, include_binding_names);
|
||
}
|
||
}
|
||
Expression::TaggedTemplateExpression(tt) => {
|
||
hash_expression(&tt.tag, h, include_binding_names);
|
||
(tt.quasi.quasis.len() as u32).hash(h);
|
||
// 发射侧同时消费 cooked 与 raw;cooked 为 None 时写标记字,与
|
||
// Some(空串) 的常量下标字不同,须保留 None 区分。
|
||
for q in &tt.quasi.quasis {
|
||
q.value.cooked.hash(h);
|
||
q.value.raw.hash(h);
|
||
}
|
||
for expr in &tt.quasi.expressions {
|
||
hash_expression(expr, h, include_binding_names);
|
||
}
|
||
}
|
||
Expression::ArrowFunctionExpression(arrow) => {
|
||
arrow.r#async.hash(h);
|
||
(arrow.params.items.len() as u32).hash(h);
|
||
if include_binding_names {
|
||
for param in &arrow.params.items {
|
||
hash_binding_pattern(¶m.pattern, h);
|
||
}
|
||
}
|
||
hash_function_body(&arrow.body, h, include_binding_names);
|
||
}
|
||
Expression::FunctionExpression(fe) => {
|
||
fe.r#async.hash(h);
|
||
fe.generator.hash(h);
|
||
(fe.params.items.len() as u32).hash(h);
|
||
if include_binding_names {
|
||
if let Some(id) = &fe.id {
|
||
id.name.as_str().hash(h);
|
||
}
|
||
for param in &fe.params.items {
|
||
hash_binding_pattern(¶m.pattern, h);
|
||
}
|
||
}
|
||
if let Some(body) = &fe.body {
|
||
hash_function_body(body, h, include_binding_names);
|
||
}
|
||
}
|
||
Expression::ClassExpression(class) => {
|
||
// 类表达式名与类声明名同口径:两种哈希粒度下恒计入(编译产物物化名)。
|
||
if let Some(id) = &class.id {
|
||
id.name.as_str().hash(h);
|
||
}
|
||
class.super_class.is_some().hash(h);
|
||
if let Some(super_class) = &class.super_class {
|
||
hash_expression(super_class, h, include_binding_names);
|
||
}
|
||
for element in &class.body.body {
|
||
class::hash_class_element(element, h, include_binding_names);
|
||
}
|
||
}
|
||
Expression::ObjectExpression(obj) => {
|
||
(obj.properties.len() as u32).hash(h);
|
||
for prop in &obj.properties {
|
||
property::hash_object_property_kind(prop, h, include_binding_names);
|
||
}
|
||
}
|
||
Expression::NewExpression(ne) => {
|
||
hash_expression(&ne.callee, h, include_binding_names);
|
||
(ne.arguments.len() as u32).hash(h);
|
||
// spread 实参改指令形态(NEW_EXPRESSION 变 NEW_EXPRESSION_SPREAD),逐位打形态标记。
|
||
for arg in &ne.arguments {
|
||
match arg {
|
||
Argument::SpreadElement(sp) => {
|
||
1u8.hash(h);
|
||
hash_expression(&sp.argument, h, include_binding_names);
|
||
}
|
||
other => {
|
||
0u8.hash(h);
|
||
if let Some(expr) = other.as_expression() {
|
||
hash_expression(expr, h, include_binding_names);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
Expression::PrivateFieldExpression(member) => {
|
||
hash_expression(&member.object, h, include_binding_names);
|
||
member.field.name.as_str().hash(h);
|
||
member.optional.hash(h);
|
||
}
|
||
Expression::StaticMemberExpression(member) => {
|
||
hash_expression(&member.object, h, include_binding_names);
|
||
member.property.name.as_str().hash(h);
|
||
member.optional.hash(h);
|
||
}
|
||
Expression::ComputedMemberExpression(member) => {
|
||
hash_expression(&member.object, h, include_binding_names);
|
||
hash_expression(&member.expression, h, include_binding_names);
|
||
member.optional.hash(h);
|
||
}
|
||
Expression::ChainExpression(chain) => {
|
||
hash_chain_element(&chain.expression, h, include_binding_names);
|
||
}
|
||
Expression::Super(_) => {}
|
||
Expression::NullLiteral(_) => {
|
||
1u8.hash(h);
|
||
}
|
||
Expression::ThisExpression(_) => {
|
||
2u8.hash(h);
|
||
}
|
||
Expression::BigIntLiteral(big) => {
|
||
big.value.hash(h);
|
||
}
|
||
Expression::RegExpLiteral(lit) => {
|
||
if let Some(raw) = &lit.raw {
|
||
raw.to_string().hash(h);
|
||
}
|
||
}
|
||
Expression::ArrayExpression(arr) => {
|
||
(arr.elements.len() as u32).hash(h);
|
||
// 元素形态(spread / 省略 / 值)逐位打标记:spread 源改变装载指令,
|
||
// 位置敏感,`[...a]` 与 `[a]` 不得同键。
|
||
for element in &arr.elements {
|
||
match element {
|
||
ArrayExpressionElement::SpreadElement(spread) => {
|
||
1u8.hash(h);
|
||
hash_expression(&spread.argument, h, include_binding_names);
|
||
}
|
||
ArrayExpressionElement::Elision(_) => {
|
||
2u8.hash(h);
|
||
}
|
||
other => {
|
||
0u8.hash(h);
|
||
if let Some(expr) = other.as_expression() {
|
||
hash_expression(expr, h, include_binding_names);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
Expression::MetaProperty(mp) => {
|
||
mp.meta.name.as_str().hash(h);
|
||
mp.property.name.as_str().hash(h);
|
||
}
|
||
Expression::AwaitExpression(ae) => {
|
||
hash_expression(&ae.argument, h, include_binding_names);
|
||
}
|
||
Expression::YieldExpression(ye) => {
|
||
ye.delegate.hash(h);
|
||
if let Some(arg) = &ye.argument {
|
||
hash_expression(arg, h, include_binding_names);
|
||
}
|
||
}
|
||
Expression::ImportExpression(ie) => {
|
||
hash_expression(&ie.source, h, include_binding_names);
|
||
if let Some(options) = &ie.options {
|
||
hash_expression(options, h, include_binding_names);
|
||
}
|
||
match &ie.phase {
|
||
Some(phase) => std::mem::discriminant(phase).hash(h),
|
||
None => 0u8.hash(h),
|
||
}
|
||
}
|
||
Expression::ParenthesizedExpression(p) => {
|
||
hash_expression(&p.expression, h, include_binding_names);
|
||
}
|
||
Expression::TSAsExpression(ts) => {
|
||
hash_expression(&ts.expression, h, include_binding_names);
|
||
}
|
||
Expression::TSSatisfiesExpression(ts) => {
|
||
hash_expression(&ts.expression, h, include_binding_names);
|
||
}
|
||
Expression::TSTypeAssertion(ts) => {
|
||
hash_expression(&ts.expression, h, include_binding_names);
|
||
}
|
||
Expression::TSNonNullExpression(ts) => {
|
||
hash_expression(&ts.expression, h, include_binding_names);
|
||
}
|
||
Expression::TSInstantiationExpression(ts) => {
|
||
hash_expression(&ts.expression, h, include_binding_names);
|
||
}
|
||
Expression::V8IntrinsicExpression(v8) => {
|
||
v8.name.name.as_str().hash(h);
|
||
(v8.arguments.len() as u32).hash(h);
|
||
for arg in &v8.arguments {
|
||
if let Some(expr) = arg.as_expression() {
|
||
hash_expression(expr, h, include_binding_names);
|
||
}
|
||
}
|
||
}
|
||
// JSX 变体在 JS 模式(SourceType::unambiguous)下不可达,落入兜底。
|
||
_ => {}
|
||
});
|
||
}
|
||
|
||
pub(super) fn hash_chain_element(element: &ChainElement, h: &mut rustc_hash::FxHasher, include_binding_names: bool) {
|
||
std::mem::discriminant(element).hash(h);
|
||
match element {
|
||
ChainElement::StaticMemberExpression(member) => {
|
||
hash_expression(&member.object, h, include_binding_names);
|
||
member.property.name.as_str().hash(h);
|
||
member.optional.hash(h);
|
||
}
|
||
ChainElement::ComputedMemberExpression(member) => {
|
||
hash_expression(&member.object, h, include_binding_names);
|
||
hash_expression(&member.expression, h, include_binding_names);
|
||
member.optional.hash(h);
|
||
}
|
||
ChainElement::PrivateFieldExpression(member) => {
|
||
hash_expression(&member.object, h, include_binding_names);
|
||
member.field.name.as_str().hash(h);
|
||
member.optional.hash(h);
|
||
}
|
||
ChainElement::CallExpression(call) => {
|
||
hash_expression(&call.callee, h, include_binding_names);
|
||
call.optional.hash(h);
|
||
(call.arguments.len() as u32).hash(h);
|
||
// spread 实参改指令形态(CALL_SPREAD),逐位打形态标记。
|
||
for arg in &call.arguments {
|
||
match arg {
|
||
Argument::SpreadElement(sp) => {
|
||
1u8.hash(h);
|
||
hash_expression(&sp.argument, h, include_binding_names);
|
||
}
|
||
other => {
|
||
0u8.hash(h);
|
||
if let Some(expr) = other.as_expression() {
|
||
hash_expression(expr, h, include_binding_names);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
ChainElement::TSNonNullExpression(expr) => {
|
||
hash_expression(&expr.expression, h, include_binding_names);
|
||
}
|
||
}
|
||
}
|