| @@ -35,7 +35,7 @@ use std::collections::HashMap; |
| 35 | 35 | use tilc_hir::body::{self as hb, Body, ExprId, LocalId, PatId}; |
| 36 | 36 | use tilc_hir::{DefId, DefKind, Hir, TypeBody}; |
| 37 | 37 | use tilc_span::{FileId, TextRange}; |
| 38 | | −use tilc_types::{BodyResults, CapArg, Constraint, Evidence, Ty, TypeckOutput}; |
| 38 | +use tilc_types::{BodyResults, CapArg, Constraint, Evidence, Ty, TypeckOutput, WellKnown}; |
| 39 | 39 | |
| 40 | 40 | use crate::ir::*; |
| 41 | 41 | |
| @@ -127,6 +127,8 @@ pub struct Lowerer<'a> { |
| 127 | 127 | fn_ids: HashMap<DefId, FnId>, |
| 128 | 128 | dict_fns: Vec<FnId>, |
| 129 | 129 | effect_ids: HashMap<DefId, EffectId>, |
| 130 | + /// The std items lowering treats specially. |
| 131 | + known: WellKnown, |
| 130 | 132 | stubs: HashMap<DefId, FnId>, |
| 131 | 133 | // The function body being lowered, and its file. |
| 132 | 134 | body: Option<&'a Body>, |
| @@ -148,6 +150,7 @@ pub fn lower(hir: &Hir, typeck: &TypeckOutput) -> Program { |
| 148 | 150 | fn_ids: HashMap::new(), |
| 149 | 151 | dict_fns: Vec::new(), |
| 150 | 152 | effect_ids: HashMap::new(), |
| 153 | + known: WellKnown::find(hir), |
| 151 | 154 | stubs: HashMap::new(), |
| 152 | 155 | body: None, |
| 153 | 156 | results: None, |
| @@ -241,9 +244,17 @@ impl<'a> Lowerer<'a> { |
| 241 | 244 | (hir.def(*op).name.clone(), kind) |
| 242 | 245 | }) |
| 243 | 246 | .collect(); |
| 247 | + let builtin = [ |
| 248 | + (self.known.proc, BuiltinEffect::Proc), |
| 249 | + (self.known.st, BuiltinEffect::St), |
| 250 | + (self.known.alloc, BuiltinEffect::Alloc), |
| 251 | + ] |
| 252 | + .into_iter() |
| 253 | + .find_map(|(def, builtin)| (def == Some(id)).then_some(builtin)); |
| 244 | 254 | self.program.effects.push(EffectDecl { |
| 245 | 255 | name: qualified(hir, id), |
| 246 | 256 | ops, |
| 257 | + builtin, |
| 247 | 258 | }); |
| 248 | 259 | self.effect_ids |
| 249 | 260 | .insert(id, self.program.effects.len() as EffectId - 1); |
| @@ -836,14 +847,11 @@ impl<'a> Lowerer<'a> { |
| 836 | 847 | clauses, |
| 837 | 848 | } => self.handle(*handled, clauses), |
| 838 | 849 | hb::Expr::Region { body: region_body } => { |
| 839 | | − let effect = |name: &str| { |
| 840 | | − self.hir |
| 841 | | − .module_by_path("std/ref") |
| 842 | | − .and_then(|m| self.hir.exports[m.index()].types.get(name)) |
| 843 | | − .and_then(|d| self.effect_ids.get(d).copied()) |
| 844 | | − }; |
| 845 | | − let effects: Vec<EffectId> = |
| 846 | | − ["alloc", "st"].into_iter().filter_map(effect).collect(); |
| 850 | + let effects: Vec<EffectId> = [self.known.alloc, self.known.st] |
| 851 | + .into_iter() |
| 852 | + .flatten() |
| 853 | + .filter_map(|d| self.effect_ids.get(&d).copied()) |
| 854 | + .collect(); |
| 847 | 855 | let binder = self.binder(*region_body); |
| 848 | 856 | let body = self.lambda(&binder, &[], None, *region_body); |
| 849 | 857 | self.emit(Value::Region { effects, body }) |