1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Expression Analysis Helper Functions
//
// This module provides pure functions for analyzing expressions:
// - Reference detection (&x, &mut x)
// - Constant evaluation checking (compile-time evaluable expressions)
use crate::parser::{Expression, UnaryOp};
// =============================================================================
// Reference Detection
// =============================================================================
/// Check if an expression is a reference (&x or &mut x)
///
/// Returns true for both immutable and mutable references.
///
/// # Examples
/// ```
/// // &x → true
/// // &mut x → true
/// // !x → false
/// // x → false
/// ```
pub fn is_reference_expression(expr: &Expression) -> bool {
matches!(
expr,
Expression::Unary {
op: UnaryOp::Ref | UnaryOp::MutRef,
..
}
)
}
// =============================================================================
// Constant Evaluation Detection
// =============================================================================
/// Check if an expression can be evaluated at compile time
///
/// Returns true for literals and expressions composed entirely of const values.
///
/// # Examples
/// ```
/// // 42 → true
/// // "hello" → true
/// // 1 + 2 → true
/// // -5 → true
/// // x → false
/// // x + 1 → false
/// ```
pub fn is_const_evaluable(expr: &Expression) -> bool {
match expr {
// Literals are always const
Expression::Literal { .. } => true,
// Binary operations on const values are const
Expression::Binary { left, right, .. } => {
is_const_evaluable(left) && is_const_evaluable(right)
}
// Unary operations on const values are const
Expression::Unary { operand, .. } => is_const_evaluable(operand),
// Struct literals with const fields might be const
Expression::StructLiteral { fields, .. } => {
fields.iter().all(|(_, expr)| is_const_evaluable(expr))
}
// Map literals with const entries might be const
Expression::MapLiteral { pairs, .. } => pairs
.iter()
.all(|(key, val)| is_const_evaluable(key) && is_const_evaluable(val)),
// Array literals with const elements are const
Expression::Array { elements, .. } => elements.iter().all(|e| is_const_evaluable(e)),
// Tuple literals with const elements are const
Expression::Tuple { elements, .. } => elements.iter().all(|e| is_const_evaluable(e)),
// Everything else (identifiers, calls, field access, etc.) is not const
_ => false,
}
}
// =============================================================================
// Identifier usage analysis (struct literals, move semantics)
// =============================================================================
/// Count how many times each identifier appears across struct literal field expressions.
pub fn count_identifier_usages_in_fields(
fields: &[(String, &Expression<'_>)],
) -> std::collections::HashMap<String, usize> {
let mut counts = std::collections::HashMap::new();
for (_, expr) in fields {
accumulate_identifier_usages(expr, &mut counts);
}
counts
}
fn accumulate_identifier_usages(
expr: &Expression<'_>,
counts: &mut std::collections::HashMap<String, usize>,
) {
match expr {
Expression::Identifier { name, .. } => {
*counts.entry(name.clone()).or_default() += 1;
}
Expression::Binary { left, right, .. } => {
accumulate_identifier_usages(left, counts);
accumulate_identifier_usages(right, counts);
}
Expression::Unary { operand, .. } => accumulate_identifier_usages(operand, counts),
Expression::FieldAccess { object, .. } => accumulate_identifier_usages(object, counts),
Expression::Index { object, index, .. } => {
accumulate_identifier_usages(object, counts);
accumulate_identifier_usages(index, counts);
}
Expression::Call {
function,
arguments,
..
} => {
accumulate_identifier_usages(function, counts);
for (_, arg) in arguments {
accumulate_identifier_usages(arg, counts);
}
}
Expression::MethodCall {
object, arguments, ..
} => {
accumulate_identifier_usages(object, counts);
for (_, arg) in arguments {
accumulate_identifier_usages(arg, counts);
}
}
Expression::MacroInvocation { args, .. } => {
for arg in args {
accumulate_identifier_usages(arg, counts);
}
}
Expression::Block { statements, .. } => {
for stmt in statements {
if let crate::parser::Statement::Expression { expr, .. } = stmt {
accumulate_identifier_usages(expr, counts);
}
}
}
Expression::Array { elements, .. } | Expression::Tuple { elements, .. } => {
for e in elements {
accumulate_identifier_usages(e, counts);
}
}
Expression::StructLiteral { fields, .. } => {
for (_, e) in fields {
accumulate_identifier_usages(e, counts);
}
}
Expression::MapLiteral { pairs, .. } => {
for (k, v) in pairs {
accumulate_identifier_usages(k, counts);
accumulate_identifier_usages(v, counts);
}
}
Expression::Range { start, end, .. } => {
accumulate_identifier_usages(start, counts);
accumulate_identifier_usages(end, counts);
}
Expression::Cast { expr, .. } => accumulate_identifier_usages(expr, counts),
Expression::TryOp { expr, .. } | Expression::Await { expr, .. } => {
accumulate_identifier_usages(expr, counts);
}
Expression::ChannelSend { channel, value, .. } => {
accumulate_identifier_usages(channel, counts);
accumulate_identifier_usages(value, counts);
}
Expression::ChannelRecv { channel, .. } => accumulate_identifier_usages(channel, counts),
Expression::Closure { body, .. } => accumulate_identifier_usages(body, counts),
Expression::Literal { .. } => {}
}
}