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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//! Lowering for operator and call expressions: binary/unary ops, references,
//! method calls, plus shared helpers for inferring closure-typed argument
//! shapes.
use crate::ast::{BinaryOperator, Expr, PrimitiveType, UnaryOperator};
use crate::error::CompilerError;
use crate::ir::lower::IrLowerer;
use crate::ir::{IrExpr, ResolvedType};
impl IrLowerer<'_> {
pub(super) fn lower_binary_op_expr(
&mut self,
left: &Expr,
op: BinaryOperator,
right: &Expr,
) -> IrExpr {
let left_ir = self.lower_expr(left);
let right_ir = self.lower_expr(right);
let ty = match op {
BinaryOperator::Eq
| BinaryOperator::Ne
| BinaryOperator::Lt
| BinaryOperator::Le
| BinaryOperator::Gt
| BinaryOperator::Ge
| BinaryOperator::And
| BinaryOperator::Or => ResolvedType::Primitive(PrimitiveType::Boolean),
BinaryOperator::Add
| BinaryOperator::Sub
| BinaryOperator::Mul
| BinaryOperator::Div
| BinaryOperator::Mod => left_ir.ty().clone(),
BinaryOperator::Range => ResolvedType::Range(Box::new(left_ir.ty().clone())),
};
IrExpr::BinaryOp {
left: Box::new(left_ir),
op,
right: Box::new(right_ir),
ty,
span: self.current_ir_span(),
}
}
pub(super) fn lower_unary_op_expr(&mut self, op: UnaryOperator, operand: &Expr) -> IrExpr {
let operand_ir = self.lower_expr(operand);
let ty = match op {
UnaryOperator::Not => ResolvedType::Primitive(PrimitiveType::Boolean),
UnaryOperator::Neg => operand_ir.ty().clone(),
};
IrExpr::UnaryOp {
op,
operand: Box::new(operand_ir),
ty,
span: self.current_ir_span(),
}
}
pub(super) fn lower_reference(&mut self, path: &[crate::ast::Ident]) -> IrExpr {
let path_strs: Vec<String> = path.iter().map(|i| i.name.clone()).collect();
// Check for self.field pattern — bounds verified by len() == 2 check
#[expect(
clippy::indexing_slicing,
reason = "len == 2 check above guarantees indices 0 and 1"
)]
if path_strs.len() == 2 && path_strs[0] == "self" {
let field_name = &path_strs[1];
let ty = self.resolve_self_field_type(field_name);
return IrExpr::SelfFieldRef {
field: field_name.clone(),
field_idx: crate::ir::FieldIdx(0),
ty,
span: self.current_ir_span(),
};
}
// Check for bare "self" in impl context — bounds verified by len() == 1 check
#[expect(
clippy::indexing_slicing,
reason = "len == 1 check above guarantees index 0"
)]
if path_strs.len() == 1 && path_strs[0] == "self" {
if let Some(impl_name) = self.current_impl_struct.clone() {
let ty = self.resolve_impl_self_type(&impl_name);
return IrExpr::Reference {
path: path_strs,
target: crate::ir::ReferenceTarget::Unresolved,
ty,
span: self.current_ir_span(),
};
}
}
// Check for module-level let binding reference
if path_strs.len() == 1 {
#[expect(
clippy::indexing_slicing,
reason = "len == 1 check above guarantees index 0"
)]
let name = &path_strs[0];
if let Some(let_type) = self.symbols.get_let_type(name).map(str::to_string) {
// prefer the simple-name resolution; fall
// back to the value's known type for composite type
// strings the helper can't reparse (closures, tuples,
// arrays, etc.). The let was previously lowered, so its
// resolved type is already cached on the IR side.
if let Some(ty) = self.string_to_resolved_type(&let_type) {
return IrExpr::LetRef {
name: name.clone(),
binding_id: crate::ir::BindingId(0),
ty,
span: self.current_ir_span(),
};
}
let ty = self
.module
.lets
.iter()
.find(|l| l.name == *name)
.map_or_else(|| ResolvedType::Error, |l| l.value.ty().clone());
return IrExpr::LetRef {
name: name.clone(),
binding_id: crate::ir::BindingId(0),
ty,
span: self.current_ir_span(),
};
}
}
// Resolve the root of the path. Try, in order:
// 1. a local binding (function param, `self`, closure capture),
// 2. a module-level `let` — needed for multi-segment paths like
// `sample.tags` where the single-segment LetRef branch above
// doesn't apply.
// For multi-segment paths, walk each subsequent segment as a field
// access so `u.x.y` resolves to `y`'s actual field type. A root
// segment that resolves to nothing is a real unresolved reference;
// surface it as `UndefinedReference` and return `Error`.
let root = path_strs.first().and_then(|n| {
self.lookup_local_binding(n).cloned().or_else(|| {
self.module
.lets
.iter()
.find(|l| l.name == *n)
.map(|l| l.value.ty().clone())
})
});
let ty = if let Some(root_ty) = root {
let mut current = root_ty;
for seg in path_strs.iter().skip(1) {
current = self.resolve_field_type(¤t, seg);
}
current
} else {
let span = path.first().map_or(self.current_span, |i| i.span);
self.errors.push(CompilerError::UndefinedReference {
name: path_strs.join("."),
span,
});
ResolvedType::Error
};
IrExpr::Reference {
path: path_strs,
target: crate::ir::ReferenceTarget::Unresolved,
ty,
span: self.current_ir_span(),
}
}
pub(super) fn lower_method_call(
&mut self,
receiver: &Expr,
method_name: &str,
args: &[(Option<crate::ast::Ident>, Expr)],
) -> IrExpr {
let receiver_ir = self.lower_expr(receiver);
// same idea as the function-call path — pull the
// method's expected param types so closure-literal arguments
// get their `x` typed against what the method expects.
let expected_param_tys = self.lookup_method_param_types(receiver_ir.ty(), method_name);
let lowered_args: Vec<(Option<String>, IrExpr)> = args
.iter()
.enumerate()
.map(|(i, (label, expr))| {
let saved_closure = self.expected_closure_type.take();
self.expected_closure_type =
Self::expected_arg_closure_ty(&expected_param_tys, i, label.as_ref());
let lowered = self.lower_expr(expr);
self.expected_closure_type = saved_closure;
(label.as_ref().map(|l| l.name.clone()), lowered)
})
.collect();
let ty = self.resolve_method_return_type(receiver_ir.ty(), method_name);
let dispatch = self.resolve_dispatch_kind(receiver_ir.ty(), method_name);
IrExpr::MethodCall {
receiver: Box::new(receiver_ir),
method: method_name.to_string(),
method_idx: crate::ir::MethodIdx(0),
args: lowered_args,
dispatch,
ty,
span: self.current_ir_span(),
}
}
/// find the IR function with the given name and
/// return its parameter list as `(param_name, param_ty)` pairs. The
/// caller uses the list to seed `expected_closure_type` for each
/// argument before lowering. Returns an empty vec when the function
/// isn't yet in the IR (forward reference) — in that case we fall
/// back to `Unknown` for closure-literal params, same as before.
pub(super) fn lookup_function_param_types(&self, fn_name: &str) -> Vec<(String, ResolvedType)> {
// Match the same module-aware resolution `find_function_in_scope`
// uses so a call from inside `mod foo { fn caller() { add(...) } }`
// gets `foo::add`'s declared params (not a same-named top-level
// function's, which lexical scoping says doesn't apply here).
let f = if self.current_module_prefix.is_empty() {
self.module.functions.iter().find(|f| f.name == fn_name)
} else {
let qualified = format!("{}::{}", self.current_module_prefix, fn_name);
self.module
.functions
.iter()
.find(|f| f.name == qualified)
.or_else(|| self.module.functions.iter().find(|f| f.name == fn_name))
};
f.map(|f| {
f.params
.iter()
.filter_map(|p| p.ty.as_ref().map(|t| (p.name.clone(), t.clone())))
.collect()
})
.unwrap_or_default()
}
/// pick the expected parameter type for arg
/// position `i`, preferring name match (for named args like
/// `apply(callback: x -> x + 1)`) and falling back to positional
/// index. Returns `Some(ty)` only when the matched parameter is a
/// `Closure { .. }` — non-closure expected types don't influence
/// closure-literal lowering.
pub(super) fn expected_arg_closure_ty(
expected: &[(String, ResolvedType)],
i: usize,
name: Option<&crate::ast::Ident>,
) -> Option<ResolvedType> {
let candidate = name.map_or_else(
|| expected.get(i).map(|(_, t)| t.clone()),
|n| {
expected
.iter()
.find(|(pname, _)| pname == &n.name)
.map(|(_, t)| t.clone())
},
);
candidate.filter(|t| matches!(t, ResolvedType::Closure { .. }))
}
/// locate the impl method matching
/// `(receiver_ty, method_name)` and return its non-self parameter
/// list as `(name, type)` pairs. The caller uses these to seed
/// `expected_closure_type` for closure-literal arguments. Returns
/// an empty vec when the method can't be resolved (forward
/// reference, generic dispatch via trait, etc.) — in that case the
/// arg-lowering falls back to `Unknown` for closure-literal params.
pub(super) fn lookup_method_param_types(
&self,
receiver_ty: &ResolvedType,
method_name: &str,
) -> Vec<(String, ResolvedType)> {
let target = match receiver_ty {
ResolvedType::Generic { base, .. } => match base {
crate::ir::GenericBase::Struct(id) => Some(crate::ir::ImplTarget::Struct(*id)),
crate::ir::GenericBase::Enum(id) => Some(crate::ir::ImplTarget::Enum(*id)),
// A generic trait base can't be a method-call
// receiver (FormaLang has no dynamic dispatch). Phase
// E2 rejects trait values; this branch is here only
// to keep the match exhaustive.
crate::ir::GenericBase::Trait(_) => None,
},
ResolvedType::Struct(id) => Some(crate::ir::ImplTarget::Struct(*id)),
ResolvedType::Enum(id) => Some(crate::ir::ImplTarget::Enum(*id)),
ResolvedType::Primitive(_)
| ResolvedType::Trait(_)
| ResolvedType::Array(_)
| ResolvedType::Range(_)
| ResolvedType::Optional(_)
| ResolvedType::Tuple(_)
| ResolvedType::TypeParam(_)
| ResolvedType::External { .. }
| ResolvedType::Dictionary { .. }
| ResolvedType::Closure { .. }
| ResolvedType::Error => None,
};
let Some(target) = target else {
return Vec::new();
};
for impl_block in &self.module.impls {
if impl_block.target != target {
continue;
}
if let Some(func) = impl_block.functions.iter().find(|f| f.name == method_name) {
return func
.params
.iter()
.filter(|p| p.name != "self")
.filter_map(|p| p.ty.as_ref().map(|t| (p.name.clone(), t.clone())))
.collect();
}
}
Vec::new()
}
}