Skip to main content

lower_lang/lower/
mod.rs

1//! The lowering engine: an explicit-stack machine over the syntax tree.
2//!
3//! Lowering is recursive in nature (an expression contains expressions) and
4//! hir-lang's builder is bottom-up (children before parents), so the engine
5//! runs a work stack of tasks: *lower this element in this context* and
6//! *finish the innermost open frame*. Entering a node resolves its fields'
7//! picks, opens a frame, and schedules one task per selected child; every
8//! task leaves exactly one value on the value stack, so when the frame
9//! finishes its fields' values sit in order above the frame's base. Nothing
10//! recurses on the call stack, so any tree depth is safe.
11
12mod enter;
13mod finish;
14mod token;
15mod tpl;
16
17use alloc::collections::BTreeMap;
18use alloc::string::String;
19use alloc::vec::Vec;
20
21use hir_lang::{
22    Arg, ArgKind, BinderId, BinderKind, Builder, Capture, Effects, Expr, ExprId, FieldInit,
23    FieldPat, GenericArg, GenericParam, Hir, HirError, Item, ItemId, ItemKind, List, MapEntry,
24    Name, NodeRef, Ns, Origin, Param, ParamKind, Pat, PatId, Path, PathId, Res, Segment, Site,
25    Span, Stmt, StmtId, Ty, TyId, UnitId, VariantId, WherePred,
26};
27use intern_lang::{Interner, Symbol};
28use syntax_lang::{Element, Node, TokenKind};
29
30use crate::arch::Arch;
31use crate::mapping::{EffectMode, Mapping, OpSpec, TokenRole};
32use crate::pick::{self, Kid, Labeler, Pick};
33use crate::problem::{Context, LowerError, Problem, ProblemKind};
34use crate::template::TemplateId;
35
36/// A range of the run's attribute pool: (start, len).
37pub(crate) type AttrRange = (u32, u32);
38
39/// Limits on what one lowering may do. Untrusted trees and templates cannot
40/// exceed them; exceeding one is a reported problem or error, never a hang.
41///
42/// `#[non_exhaustive]`: start from [`Budget::default`] and assign fields.
43///
44/// # Examples
45///
46/// ```
47/// use lower_lang::Budget;
48///
49/// let mut budget = Budget::default();
50/// budget.max_nodes = 1_000;
51/// assert_eq!(budget.max_expansion_depth, 64);
52/// ```
53#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
54#[non_exhaustive]
55pub struct Budget {
56    /// The most HIR nodes (and list entries) one lowering may build, copies
57    /// for repeated template holes included; past it lowering stops with
58    /// [`LowerError::NodeBudget`]. Default `1 << 24` (16,777,216).
59    pub max_nodes: u64,
60    /// The deepest chain of template applications
61    /// ([`TemplateBuilder::apply`](crate::TemplateBuilder::apply)); a deeper
62    /// one becomes an error node and an
63    /// [`ExpansionTooDeep`](ProblemKind::ExpansionTooDeep) problem.
64    /// Default 64.
65    pub max_expansion_depth: u32,
66}
67
68impl Default for Budget {
69    fn default() -> Self {
70        Self {
71            max_nodes: 1 << 24,
72            max_expansion_depth: 64,
73        }
74    }
75}
76
77/// The result of lowering: a valid HIR unit and every problem found.
78///
79/// The HIR is valid even when there are problems: each broken construct is an
80/// HIR error form, and the rest of the unit is lowered normally.
81///
82/// # Examples
83///
84/// See [`lower`](crate::lower).
85#[derive(Clone, Debug)]
86#[non_exhaustive]
87pub struct Lowered {
88    /// The validated HIR.
89    pub hir: Hir,
90    /// Every problem, in source order (span start, then discovery).
91    pub problems: Vec<Problem>,
92}
93
94impl Lowered {
95    /// Whether any problem was found.
96    ///
97    /// # Examples
98    ///
99    /// ```
100    /// use lower_lang::{lower, Arch, MappingBuilder, Pick};
101    /// use syntax_lang::Node;
102    ///
103    /// # #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
104    /// # struct K(u8);
105    /// # impl syntax_lang::TokenKind for K {}
106    /// let mut b = MappingBuilder::new();
107    /// b.node(K(0), Arch::Module { name: Pick::Absent, items: Pick::Nodes });
108    /// let mapping = b.build()?;
109    /// let tree = Node::new(K(0), vec![]);
110    /// let mut names = intern_lang::Interner::new();
111    /// let out = lower(&tree, "", &mapping, &mut names).expect("within budget");
112    /// assert!(!out.has_problems());
113    /// # Ok::<(), lower_lang::MappingError>(())
114    /// ```
115    #[must_use]
116    pub fn has_problems(&self) -> bool {
117        !self.problems.is_empty()
118    }
119}
120
121/// Lowers syntax trees with one mapping: the configured path, for a budget,
122/// a compilation unit other than 0, or host-provided field labels.
123///
124/// # Examples
125///
126/// ```
127/// use hir_lang::UnitId;
128/// use lower_lang::{Arch, Budget, Lowerer, MappingBuilder, Pick};
129/// use syntax_lang::Node;
130///
131/// # #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
132/// # struct K(u8);
133/// # impl syntax_lang::TokenKind for K {}
134/// let mut b = MappingBuilder::new();
135/// b.node(K(0), Arch::Module { name: Pick::Absent, items: Pick::Nodes });
136/// let mapping = b.build()?;
137/// let tree = Node::new(K(0), vec![]);
138/// let mut names = intern_lang::Interner::new();
139/// let out = Lowerer::new(&mapping)
140///     .with_unit(UnitId::new(3))
141///     .with_budget(Budget::default())
142///     .lower(&tree, "", &mut names)
143///     .expect("within budget");
144/// assert_eq!(out.hir.unit(), UnitId::new(3));
145/// # Ok::<(), lower_lang::MappingError>(())
146/// ```
147pub struct Lowerer<'m, K> {
148    mapping: &'m Mapping<K>,
149    budget: Budget,
150    unit: UnitId,
151    labeler: Option<&'m dyn Labeler<K>>,
152}
153
154impl<'m, K> Lowerer<'m, K>
155where
156    K: Copy + Ord + TokenKind,
157{
158    /// A lowerer for `mapping`, with the default budget, unit 0, and no field
159    /// labels.
160    ///
161    /// # Examples
162    ///
163    /// See [`Lowerer`].
164    #[must_use]
165    pub fn new(mapping: &'m Mapping<K>) -> Self {
166        Self {
167            mapping,
168            budget: Budget::default(),
169            unit: UnitId::default(),
170            labeler: None,
171        }
172    }
173
174    /// Sets the budget.
175    ///
176    /// # Examples
177    ///
178    /// See [`Lowerer`].
179    #[must_use]
180    pub fn with_budget(mut self, budget: Budget) -> Self {
181        self.budget = budget;
182        self
183    }
184
185    /// Sets the compilation unit the HIR is built for.
186    ///
187    /// # Examples
188    ///
189    /// See [`Lowerer`].
190    #[must_use]
191    pub fn with_unit(mut self, unit: UnitId) -> Self {
192        self.unit = unit;
193        self
194    }
195
196    /// Uses `labeler` for [`Pick::Label`](crate::Pick::Label) fields.
197    ///
198    /// # Examples
199    ///
200    /// ```
201    /// use lower_lang::{Labeler, Lowerer, MappingBuilder};
202    /// use syntax_lang::Node;
203    ///
204    /// struct NoFields;
205    /// # #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
206    /// # struct K(u8);
207    /// # impl syntax_lang::TokenKind for K {}
208    /// impl Labeler<K> for NoFields {
209    ///     fn label(&self, _: &Node<K>, _: usize) -> Option<u16> { None }
210    /// }
211    /// let mapping = MappingBuilder::<K>::new().build()?;
212    /// let _lowerer = Lowerer::new(&mapping).with_labeler(&NoFields);
213    /// # Ok::<(), lower_lang::MappingError>(())
214    /// ```
215    #[must_use]
216    pub fn with_labeler(mut self, labeler: &'m dyn Labeler<K>) -> Self {
217        self.labeler = Some(labeler);
218        self
219    }
220
221    /// Lowers `tree` (parsed from `source`) into a validated HIR unit, with
222    /// names interned in `names`.
223    ///
224    /// # Errors
225    ///
226    /// Only for exhausted resources: [`LowerError::NodeBudget`],
227    /// [`LowerError::SymbolSpaceExhausted`], or [`LowerError::Hir`] when
228    /// hir-lang's arenas overflow. Problems in the code are never errors:
229    /// they are in [`Lowered::problems`].
230    ///
231    /// # Examples
232    ///
233    /// See [`Lowerer`].
234    pub fn lower(
235        &self,
236        tree: &Node<K>,
237        source: &str,
238        names: &mut Interner,
239    ) -> Result<Lowered, LowerError> {
240        Run::new(self, source, names)?.run(tree)
241    }
242}
243
244// ------------------------------------------------------------------ machine
245
246/// Where a pattern's binders are bound.
247#[derive(Clone, Copy, Debug, PartialEq, Eq)]
248pub(crate) struct PatSite {
249    /// A parameter (binders of kind `Param`) rather than a local.
250    pub(crate) param: bool,
251    /// The binders are mutable.
252    pub(crate) mutable: bool,
253    /// A `match` arm, where bare identifiers follow the language's
254    /// [`IdentPatterns`](crate::IdentPatterns) policy.
255    pub(crate) arm: bool,
256    /// A range bound: bare identifiers are constants, not bindings.
257    pub(crate) bound: bool,
258    /// The binders belong to the enclosing function: the pattern binds
259    /// hygienic temporaries that the consumer assigns to hoisted names.
260    pub(crate) persist: bool,
261}
262
263impl PatSite {
264    pub(crate) const LOCAL: Self = Self {
265        param: false,
266        mutable: false,
267        arm: false,
268        bound: false,
269        persist: false,
270    };
271    pub(crate) const ARM: Self = Self {
272        param: false,
273        mutable: false,
274        arm: true,
275        bound: false,
276        persist: false,
277    };
278}
279
280/// A function-scoped binding made by a pattern: the temporary the pattern
281/// binds, and the hoisted name it is assigned to.
282#[derive(Clone, Copy, Debug)]
283pub(crate) struct Persist {
284    pub(crate) tmp: BinderId,
285    pub(crate) sym: Symbol,
286    pub(crate) span: Span,
287}
288
289/// One piece of an interpolated string under construction.
290#[derive(Clone, Copy, Debug)]
291pub(crate) enum Piece {
292    /// Decoded text.
293    Text(hir_lang::TextRef, Span),
294    /// The next value of the frame's expression field.
295    Expr,
296}
297
298/// The context an element is lowered in, and so the kind of value it leaves.
299#[derive(Clone, Copy, Debug, PartialEq, Eq)]
300pub(crate) enum Want {
301    /// The root module.
302    Root,
303    /// An expression.
304    Expr,
305    /// An element of a block or module body: a statement, an expression, or
306    /// an item.
307    Stmt,
308    /// A member of a class, interface, or impl: an item or a field.
309    Member,
310    /// A pattern.
311    Pat(PatSite),
312    /// A type.
313    Ty,
314    /// A path in a namespace.
315    Path(Ns),
316    /// A path segment.
317    Segment,
318    /// A parameter.
319    Param,
320    /// A call argument.
321    Arg,
322    /// A match arm or catch clause.
323    Arm,
324    /// An explicit capture.
325    Capture,
326    /// A field definition.
327    Field,
328    /// A mixin rule.
329    Rule,
330    /// A sum variant.
331    Variant,
332    /// A generic parameter.
333    Generic,
334    /// A generic argument.
335    GArg,
336    /// A where-clause predicate.
337    Where,
338    /// A map-literal entry.
339    Entry,
340    /// A record-literal field initializer.
341    Init,
342    /// A record-pattern field.
343    FieldPat(PatSite),
344}
345
346impl Want {
347    pub(crate) const fn context(self) -> Context {
348        match self {
349            Self::Root | Self::Stmt => Context::Statement,
350            Self::Expr => Context::Expression,
351            Self::Member => Context::Member,
352            Self::Pat(_) | Self::FieldPat(_) => Context::Pattern,
353            Self::Ty => Context::Type,
354            Self::Path(_) => Context::Path,
355            Self::Segment => Context::Segment,
356            Self::Param => Context::Parameter,
357            Self::Arg => Context::Argument,
358            Self::Arm => Context::Arm,
359            Self::Capture => Context::Capture,
360            Self::Field => Context::Field,
361            Self::Rule => Context::Member,
362            Self::Variant => Context::Variant,
363            Self::Generic | Self::GArg | Self::Where => Context::Generic,
364            Self::Entry | Self::Init => Context::Entry,
365        }
366    }
367
368    /// Whether an expression's value adapts to this context.
369    pub(crate) const fn takes_expr(self) -> bool {
370        matches!(
371            self,
372            Self::Expr | Self::Stmt | Self::Arg | Self::Entry | Self::GArg
373        )
374    }
375}
376
377/// The value one lowered element leaves on the value stack.
378#[derive(Clone, Copy, Debug)]
379pub(crate) enum Val {
380    /// Nothing: an absent element, or a list element that failed and was
381    /// dropped (its problem already reported).
382    Err,
383    Expr(ExprId),
384    Stmt(StmtId),
385    Item(ItemId),
386    /// Several values from one element (a `Splice`, `global $a, $b`): a run of
387    /// the spill stack. Spill entries are never themselves spills.
388    Spill(u32, u32),
389    Pat(PatId),
390    /// A `..` marker in a pattern list, with its optional binding.
391    Rest(Option<PatId>),
392    Ty(TyId),
393    Path(PathId),
394    Seg(Segment),
395    /// A parameter, created as a node by the enclosing function so that list
396    /// markers can still adjust its kind; with its attributes.
397    Param(Param, Span, AttrRange),
398    /// A `*` or `/` marker in a parameter list.
399    Marker(ParamKind),
400    Arg(Arg),
401    Arm(hir_lang::Arm),
402    Cap(Capture),
403    /// A field definition, and whether it has a name.
404    Field(hir_lang::FieldId, bool),
405    Variant(VariantId),
406    Generic(GenericParam),
407    GArg(GenericArg),
408    Where(WherePred),
409    Entry(MapEntry),
410    Init(FieldInit),
411    FieldPat(FieldPat),
412    Rule(hir_lang::MixinRule),
413}
414
415/// A value on the value stack, with the span of the element it came from and
416/// whether it is a place (for place arguments).
417#[derive(Clone, Copy, Debug)]
418pub(crate) struct Slot {
419    pub(crate) v: Val,
420    pub(crate) span: Span,
421    pub(crate) place: bool,
422}
423
424/// A unit of work.
425#[derive(Clone, Copy)]
426pub(crate) enum Task<'t, K> {
427    /// Lower this element in this context.
428    Elem(&'t Element<K>, Want),
429    /// Lower this node in this context (the root).
430    Node(&'t Node<K>, Want),
431    /// Finish the innermost open frame.
432    Finish,
433    /// Record where the persistent-binding log stands (between the fields of
434    /// a template node, so each pattern hole's bindings are known).
435    Mark,
436}
437
438/// What a frame builds when it finishes.
439#[derive(Clone, Copy, Debug)]
440pub(crate) enum Build {
441    /// The frame's archetype, in its context.
442    Arch,
443    /// An operator node lowered through an operator spec.
444    Op(OpSpec),
445    /// A template instantiation.
446    Template {
447        id: TemplateId,
448        label: Option<BinderId>,
449        nodes_at: u64,
450    },
451    /// A synthetic module around a root that is not a module.
452    RootWrap,
453    /// `var x = init` hoisted: an assignment to the hoisted name.
454    HoistedLet { name: Symbol, name_span: Span },
455}
456
457/// An open construct whose fields are being lowered.
458#[derive(Clone, Copy, Debug)]
459pub(crate) struct Frame<K> {
460    pub(crate) arch: Arch<K>,
461    pub(crate) build: Build,
462    pub(crate) want: Want,
463    pub(crate) span: Span,
464    pub(crate) vals_base: usize,
465    pub(crate) counts_base: usize,
466    pub(crate) nfields: usize,
467    pub(crate) spill_base: usize,
468    pub(crate) label_mark: usize,
469    pub(crate) mods: u32,
470    pub(crate) fx: bool,
471    pub(crate) or: bool,
472    /// Names read synchronously at entry (declaration names, aliases,
473    /// members, labels).
474    pub(crate) name: Option<(Symbol, Span)>,
475    /// A second synchronous name (a mixin rule's alias).
476    pub(crate) alias: Option<(Symbol, Span)>,
477    /// A binder created or found at entry (a label, a pattern's binder).
478    pub(crate) binder: Option<BinderId>,
479    /// A tuple-field index (`a.0`).
480    pub(crate) index: Option<u32>,
481    /// An archetype-specific flag: a member access is a call, a generic
482    /// parameter is a region.
483    pub(crate) flag: bool,
484    /// A path's root, read from its leading tokens.
485    pub(crate) path_root: hir_lang::PathRoot,
486    /// The node's attributes in the run's pool.
487    pub(crate) attrs: AttrRange,
488    /// An archetype-specific range: an interpolation's pieces, or the
489    /// persistent-binding log mark of a hoisting `let`.
490    pub(crate) aux: (u32, u32),
491}
492
493/// A function, closure, or module body under construction: its effects and
494/// its hoisted declarations.
495#[derive(Clone, Copy, Debug)]
496pub(crate) struct Fx {
497    pub(crate) declared: Effects,
498    pub(crate) inferred: Effects,
499    pub(crate) hoist_base: usize,
500    pub(crate) decl_base: usize,
501}
502
503/// One lowering run.
504pub(crate) struct Run<'a, 't, K> {
505    pub(crate) m: &'a Mapping<K>,
506    pub(crate) src: &'t str,
507    pub(crate) labeler: Option<&'a dyn Labeler<K>>,
508    pub(crate) names: &'a mut Interner,
509    pub(crate) b: Builder,
510    pub(crate) nodes: u64,
511    pub(crate) budget: Budget,
512    pub(crate) syms: Vec<Symbol>,
513    pub(crate) empty: Symbol,
514    pub(crate) sym_failed: bool,
515    pub(crate) tasks: Vec<Task<'t, K>>,
516    pub(crate) vals: Vec<Slot>,
517    pub(crate) frames: Vec<Frame<K>>,
518    pub(crate) counts: Vec<usize>,
519    pub(crate) spill: Vec<Slot>,
520    pub(crate) kids: Vec<Kid<'t, K>>,
521    pub(crate) kids2: Vec<Kid<'t, K>>,
522    pub(crate) sel: Vec<&'t Element<K>>,
523    pub(crate) labels: BTreeMap<Symbol, Vec<BinderId>>,
524    pub(crate) label_log: Vec<Symbol>,
525    pub(crate) fx: Vec<Fx>,
526    pub(crate) hoist: Vec<(Symbol, Span)>,
527    pub(crate) declared: Vec<Symbol>,
528    pub(crate) or_depth: u32,
529    pub(crate) or_map: BTreeMap<Symbol, BinderId>,
530    pub(crate) problems: Vec<Problem>,
531    pub(crate) text: Vec<u8>,
532    pub(crate) scratch: String,
533    pub(crate) bufs: Vec<Vec<Slot>>,
534    pub(crate) tpl_scratch: tpl::TplScratch,
535    pub(crate) lists: Lists,
536    pub(crate) tfields: Vec<(Pick<K>, Want, bool)>,
537    /// Attribute elements of the node being entered.
538    pub(crate) attr_elems: Vec<&'t Element<K>>,
539    /// Attributes lowered so far, referenced by ranges.
540    pub(crate) attr_pool: Vec<hir_lang::Attr>,
541    /// The attributes of the node being entered, until a frame takes them.
542    pub(crate) pending_attrs: AttrRange,
543    /// Function-scoped bindings not yet assigned by their consumer.
544    pub(crate) persist: Vec<Persist>,
545    /// (frame depth, persist log length) at each `Task::Mark`.
546    pub(crate) persist_marks: Vec<(u32, u32)>,
547    /// Pieces of the interpolated strings being lowered.
548    pub(crate) pieces: Vec<Piece>,
549    /// The expansion name of hoisting temporaries (`hoist`).
550    pub(crate) hoist_sym: Symbol,
551}
552
553/// Typed scratch lists for building HIR lists without allocating per node.
554#[derive(Default)]
555pub(crate) struct Lists {
556    pub(crate) exprs: Vec<ExprId>,
557    pub(crate) tys: Vec<TyId>,
558    pub(crate) args: Vec<Arg>,
559    pub(crate) arms: Vec<hir_lang::Arm>,
560    pub(crate) stmts: Vec<StmtId>,
561    pub(crate) params: Vec<hir_lang::ParamId>,
562    pub(crate) gargs: Vec<GenericArg>,
563}
564
565impl<'a, 't, K> Run<'a, 't, K>
566where
567    K: Copy + Ord + TokenKind,
568{
569    fn new(
570        cfg: &Lowerer<'a, K>,
571        src: &'t str,
572        names: &'a mut Interner,
573    ) -> Result<Self, LowerError> {
574        let empty = names
575            .try_intern("")
576            .map_err(|_| LowerError::SymbolSpaceExhausted)?;
577        let hoist_sym = names
578            .try_intern("hoist")
579            .map_err(|_| LowerError::SymbolSpaceExhausted)?;
580        let mut syms = Vec::with_capacity(cfg.mapping.strings.len());
581        for s in &cfg.mapping.strings {
582            syms.push(
583                names
584                    .try_intern(s)
585                    .map_err(|_| LowerError::SymbolSpaceExhausted)?,
586            );
587        }
588        Ok(Self {
589            m: cfg.mapping,
590            src,
591            labeler: cfg.labeler,
592            names,
593            b: Builder::for_unit(cfg.unit),
594            nodes: 0,
595            budget: cfg.budget,
596            syms,
597            empty,
598            sym_failed: false,
599            tasks: Vec::new(),
600            vals: Vec::new(),
601            frames: Vec::new(),
602            counts: Vec::new(),
603            spill: Vec::new(),
604            kids: Vec::new(),
605            kids2: Vec::new(),
606            sel: Vec::new(),
607            labels: BTreeMap::new(),
608            label_log: Vec::new(),
609            fx: Vec::new(),
610            hoist: Vec::new(),
611            declared: Vec::new(),
612            or_depth: 0,
613            or_map: BTreeMap::new(),
614            problems: Vec::new(),
615            text: Vec::new(),
616            scratch: String::new(),
617            bufs: Vec::new(),
618            tpl_scratch: tpl::TplScratch::default(),
619            lists: Lists::default(),
620            tfields: Vec::new(),
621            attr_elems: Vec::new(),
622            attr_pool: Vec::new(),
623            pending_attrs: (0, 0),
624            persist: Vec::new(),
625            persist_marks: Vec::new(),
626            pieces: Vec::new(),
627            hoist_sym,
628        })
629    }
630
631    fn run(mut self, tree: &'t Node<K>) -> Result<Lowered, LowerError> {
632        self.tasks.push(Task::Node(tree, Want::Root));
633        while let Some(task) = self.tasks.pop() {
634            match task {
635                Task::Elem(Element::Node(n), want) => self.lower_node(n, want),
636                Task::Elem(Element::Token(t), want) => self.lower_token(t, want),
637                Task::Node(n, want) => self.lower_node(n, want),
638                Task::Finish => self.finish(),
639                Task::Mark => {
640                    let depth = u32::try_from(self.frames.len()).unwrap_or(u32::MAX);
641                    let len = u32::try_from(self.persist.len()).unwrap_or(u32::MAX);
642                    self.persist_marks.push((depth, len));
643                }
644            }
645            if self.nodes > self.budget.max_nodes {
646                return Err(LowerError::NodeBudget {
647                    limit: self.budget.max_nodes,
648                });
649            }
650            if self.sym_failed {
651                return Err(LowerError::SymbolSpaceExhausted);
652            }
653        }
654        let root = match self.vals.pop().map(|s| s.v) {
655            Some(Val::Item(root)) => root,
656            _ => {
657                self.b.set_origin(Origin::new(tree.span()));
658                self.b.module(None, &[])
659            }
660        };
661        let (hir, errors) = self.b.finish_lenient(root).map_err(LowerError::Hir)?;
662        let mut problems = self.problems;
663        problems.reserve(errors.len());
664        for e in errors {
665            let span = hir_span(&hir, &e);
666            problems.push(Problem {
667                kind: ProblemKind::Hir(e),
668                span,
669            });
670        }
671        problems.sort_by_key(|p| p.span.start());
672        Ok(Lowered { hir, problems })
673    }
674
675    // ------------------------------------------------------------- dispatch
676
677    fn lower_node(&mut self, node: &'t Node<K>, want: Want) {
678        let span = node.span();
679        if self.m.is_error(node.kind()) {
680            self.report(ProblemKind::SyntaxError, span);
681            let v = self.error_val(want, span);
682            self.push(v, span);
683            return;
684        }
685        let mut attrs = core::mem::take(&mut self.attr_elems);
686        pick::collect_kids(node, self.labeler, self.m, &mut self.kids, Some(&mut attrs));
687        self.pending_attrs = (0, 0);
688        if !attrs.is_empty() {
689            self.pending_attrs = self.lower_attrs(&attrs);
690        }
691        self.attr_elems = attrs;
692        let (start, end) = self.m.rule_range(node.kind());
693        let mut chosen = None;
694        for &(_, rule) in self.m.by_kind.get(start..end).unwrap_or(&[]) {
695            let Some(rule) = self.m.rules.get(rule as usize) else {
696                continue;
697            };
698            if rule.when.is_absent() || pick::single(rule.when, &self.kids, self.m).is_some() {
699                chosen = Some(rule.arch);
700                break;
701            }
702        }
703        match chosen {
704            Some(arch) => self.enter(node, arch, want),
705            None => {
706                self.report(ProblemKind::Unmapped, span);
707                let v = self.error_val(want, span);
708                self.push(v, span);
709            }
710        }
711        // A construct that opened no frame (a wrapper, a capture, ...) has
712        // no node to carry its attributes.
713        if self.pending_attrs.1 > 0 {
714            self.pending_attrs = (0, 0);
715            self.report(ProblemKind::MisplacedAttribute, span);
716        }
717    }
718
719    /// Leaves a value for the element at `span`.
720    pub(crate) fn push(&mut self, v: Val, span: Span) {
721        self.vals.push(Slot {
722            v,
723            span,
724            place: false,
725        });
726    }
727
728    /// Leaves a value that may be a place.
729    pub(crate) fn push_place(&mut self, v: Val, span: Span, place: bool) {
730        self.vals.push(Slot { v, span, place });
731    }
732
733    // -------------------------------------------------------------- frames
734
735    /// Opens a frame for `node` and schedules its fields: each `(pick, want,
736    /// many)` is resolved against the node's children (already in
737    /// `self.kids`), and one task per selected child is pushed.
738    pub(crate) fn plan(&mut self, mut frame: Frame<K>, fields: &[(Pick<K>, Want, bool)]) {
739        let counts_base = self.counts.len();
740        self.sel.clear();
741        for &(p, _, many) in fields {
742            let before = self.sel.len();
743            if many {
744                pick::list(p, &self.kids, self.m, &mut self.sel);
745            } else if let Some(e) = pick::single(p, &self.kids, self.m) {
746                self.sel.push(e);
747            }
748            self.counts.push(self.sel.len() - before);
749        }
750        self.drop_shared_children(fields, counts_base);
751        frame.counts_base = counts_base;
752        frame.nfields = fields.len();
753        frame.vals_base = self.vals.len();
754        frame.spill_base = self.spill.len();
755        self.open(frame);
756        self.tasks.push(Task::Finish);
757        let mut end = self.sel.len();
758        for (i, &(_, want, _)) in fields.iter().enumerate().rev() {
759            let n = self.counts.get(counts_base + i).copied().unwrap_or(0);
760            let start = end.saturating_sub(n);
761            if let Some(slice) = self.sel.get(start..end) {
762                for &e in slice.iter().rev() {
763                    self.tasks.push(Task::Elem(e, want));
764                }
765            }
766            end = start;
767        }
768        self.sel.clear();
769    }
770
771    /// A child selected by an earlier field is not lowered again for a later
772    /// single-valued field: that field is left absent and the conflict is
773    /// reported. Positional picks alias this way in half-typed code (a
774    /// missing condition makes "the first node" the body).
775    fn drop_shared_children(&mut self, fields: &[(Pick<K>, Want, bool)], counts_base: usize) {
776        let mut offset = 0;
777        for (i, &(_, _, many)) in fields.iter().enumerate() {
778            let c = self.counts.get(counts_base + i).copied().unwrap_or(0);
779            if !many && c == 1 {
780                if let Some(&e) = self.sel.get(offset) {
781                    let earlier = self.sel.get(..offset).unwrap_or(&[]);
782                    if earlier.iter().any(|&x| core::ptr::eq(x, e)) {
783                        let _ = self.sel.remove(offset);
784                        if let Some(slot) = self.counts.get_mut(counts_base + i) {
785                            *slot = 0;
786                        }
787                        self.report(ProblemKind::AmbiguousChild, e.span());
788                        continue;
789                    }
790                }
791            }
792            offset += c;
793        }
794    }
795
796    /// Opens a frame like [`plan`](Self::plan), and records the
797    /// persistent-binding log before each field and after the last (template
798    /// nodes whose pattern holes bind function-scoped names).
799    pub(crate) fn plan_marked(&mut self, mut frame: Frame<K>, fields: &[(Pick<K>, Want, bool)]) {
800        let counts_base = self.counts.len();
801        self.sel.clear();
802        for &(p, _, many) in fields {
803            let before = self.sel.len();
804            if many {
805                pick::list(p, &self.kids, self.m, &mut self.sel);
806            } else if let Some(e) = pick::single(p, &self.kids, self.m) {
807                self.sel.push(e);
808            }
809            self.counts.push(self.sel.len() - before);
810        }
811        self.drop_shared_children(fields, counts_base);
812        frame.counts_base = counts_base;
813        frame.nfields = fields.len();
814        frame.vals_base = self.vals.len();
815        frame.spill_base = self.spill.len();
816        self.open(frame);
817        self.tasks.push(Task::Finish);
818        self.tasks.push(Task::Mark);
819        let mut end = self.sel.len();
820        for i in (0..fields.len()).rev() {
821            let n = self.counts.get(counts_base + i).copied().unwrap_or(0);
822            let start = end.saturating_sub(n);
823            if let Some(slice) = self.sel.get(start..end) {
824                let want = fields.get(i).map_or(Want::Expr, |f| f.1);
825                for &e in slice.iter().rev() {
826                    self.tasks.push(Task::Elem(e, want));
827                }
828            }
829            self.tasks.push(Task::Mark);
830            end = start;
831        }
832        self.sel.clear();
833    }
834
835    /// A frame for `node` with nothing scheduled yet.
836    pub(crate) fn frame(&self, arch: Arch<K>, want: Want, span: Span) -> Frame<K> {
837        Frame {
838            arch,
839            build: Build::Arch,
840            want,
841            span,
842            vals_base: 0,
843            counts_base: 0,
844            nfields: 0,
845            spill_base: 0,
846            label_mark: self.label_log.len(),
847            mods: 0,
848            fx: false,
849            or: false,
850            name: None,
851            alias: None,
852            binder: None,
853            index: None,
854            flag: false,
855            path_root: hir_lang::PathRoot::Relative,
856            attrs: (0, 0),
857            aux: (0, 0),
858        }
859    }
860
861    /// Pushes an open frame; it takes the attributes of the node being
862    /// entered, which it attaches to its value when it finishes.
863    pub(crate) fn open(&mut self, mut frame: Frame<K>) {
864        frame.attrs = core::mem::take(&mut self.pending_attrs);
865        self.frames.push(frame);
866    }
867
868    /// The value range of field `i` of `f`.
869    pub(crate) fn field_range(&self, f: &Frame<K>, i: usize) -> (usize, usize) {
870        let counts = self
871            .counts
872            .get(f.counts_base..f.counts_base + f.nfields)
873            .unwrap_or(&[]);
874        let start = f.vals_base + counts.iter().take(i).sum::<usize>();
875        let len = counts.get(i).copied().unwrap_or(0);
876        (start, start + len)
877    }
878
879    /// The first value of field `i` (or `Val::Err` when absent).
880    pub(crate) fn one(&self, f: &Frame<K>, i: usize) -> Val {
881        self.slot(f, i).map_or(Val::Err, |s| s.v)
882    }
883
884    /// The first slot of field `i`.
885    pub(crate) fn slot(&self, f: &Frame<K>, i: usize) -> Option<Slot> {
886        let (s, e) = self.field_range(f, i);
887        if s < e {
888            self.vals.get(s).copied()
889        } else {
890            None
891        }
892    }
893
894    /// Whether field `i` selected an element at all (even one that failed).
895    pub(crate) fn present(&self, f: &Frame<K>, i: usize) -> bool {
896        let (s, e) = self.field_range(f, i);
897        s < e
898    }
899
900    /// The values of field `i`, spills flattened, into a pooled buffer.
901    pub(crate) fn many(&mut self, f: &Frame<K>, i: usize) -> Vec<Slot> {
902        let mut out = self.bufs.pop().unwrap_or_default();
903        out.clear();
904        let (s, e) = self.field_range(f, i);
905        for j in s..e {
906            match self.vals.get(j).copied() {
907                Some(Slot {
908                    v: Val::Spill(ss, sl),
909                    ..
910                }) => {
911                    let (ss, sl) = (ss as usize, sl as usize);
912                    if let Some(run) = self.spill.get(ss..ss.saturating_add(sl)) {
913                        out.extend_from_slice(run);
914                    }
915                }
916                Some(Slot { v: Val::Err, .. }) | None => {}
917                Some(slot) => out.push(slot),
918            }
919        }
920        out
921    }
922
923    /// Returns a buffer from [`many`](Self::many) to the pool.
924    pub(crate) fn give(&mut self, mut buf: Vec<Slot>) {
925        buf.clear();
926        self.bufs.push(buf);
927    }
928
929    // ------------------------------------------------------------- problems
930
931    pub(crate) fn report(&mut self, kind: ProblemKind, span: Span) {
932        self.problems.push(Problem { kind, span });
933    }
934
935    /// Reports a missing required field and returns an error expression.
936    pub(crate) fn missing_expr(&mut self, field: &'static str, span: Span) -> ExprId {
937        self.report(ProblemKind::MissingField { field }, span);
938        self.err_expr(span)
939    }
940
941    // ------------------------------------------------------- node creation
942
943    pub(crate) fn at(&mut self, span: Span) {
944        self.b.set_origin(Origin::new(span));
945    }
946
947    pub(crate) fn ex(&mut self, e: Expr) -> ExprId {
948        self.nodes += 1;
949        self.b.expr(e)
950    }
951
952    pub(crate) fn st(&mut self, s: Stmt) -> StmtId {
953        self.nodes += 1;
954        self.b.stmt(s)
955    }
956
957    pub(crate) fn pt(&mut self, p: Pat) -> PatId {
958        self.nodes += 1;
959        self.b.pat(p)
960    }
961
962    pub(crate) fn ty(&mut self, t: Ty) -> TyId {
963        self.nodes += 1;
964        self.b.ty(t)
965    }
966
967    pub(crate) fn it(&mut self, i: Item) -> ItemId {
968        self.nodes += 1;
969        self.b.item(i)
970    }
971
972    pub(crate) fn path(&mut self, p: Path) -> PathId {
973        self.nodes += 1;
974        self.b.path(p)
975    }
976
977    pub(crate) fn binder(&mut self, name: Name, kind: BinderKind, mutable: bool) -> BinderId {
978        self.nodes += 1;
979        self.b
980            .binder(hir_lang::Binder::new(name, kind).with_mutable(mutable))
981    }
982
983    pub(crate) fn list<T: hir_lang::Pooled>(&mut self, items: &[T]) -> List<T> {
984        self.nodes += items.len() as u64;
985        self.b.list(items)
986    }
987
988    pub(crate) fn err_expr(&mut self, span: Span) -> ExprId {
989        self.at(span);
990        self.ex(Expr::Err)
991    }
992
993    /// A one-segment unresolved path to a source name.
994    pub(crate) fn name_path(&mut self, sym: Symbol, span: Span, ns: Ns) -> PathId {
995        self.at(span);
996        let seg = Segment::new(Name::new(sym), Origin::new(span));
997        let segs = self.list(&[seg]);
998        self.path(Path::new(segs, ns))
999    }
1000
1001    pub(crate) fn name_expr(&mut self, sym: Symbol, span: Span) -> ExprId {
1002        let p = self.name_path(sym, span, Ns::Value);
1003        self.ex(Expr::Path(p))
1004    }
1005
1006    /// The error value for a context: an error node where the context keeps
1007    /// positions (expressions, statements, patterns, parameters, arguments),
1008    /// nothing where a list can simply lose the element.
1009    pub(crate) fn error_val(&mut self, want: Want, span: Span) -> Val {
1010        self.at(span);
1011        match want {
1012            Want::Root => {
1013                // The root must stay a module for hir-lang to accept the unit.
1014                self.nodes += 1;
1015                Val::Item(self.b.module(None, &[]))
1016            }
1017            Want::Member => Val::Item(self.it(Item::new(None, ItemKind::Err))),
1018            Want::Expr => Val::Expr(self.ex(Expr::Err)),
1019            Want::Stmt => Val::Stmt(self.st(Stmt::Err)),
1020            Want::Pat(_) => Val::Pat(self.pt(Pat::Err)),
1021            Want::Ty => Val::Ty(self.ty(Ty::Err)),
1022            Want::Path(_) => {
1023                let mut p = Path::new(List::EMPTY, Ns::Value);
1024                p.res = Res::Err;
1025                Val::Path(self.path(p))
1026            }
1027            Want::Param => {
1028                let pat = self.pt(Pat::Err);
1029                Val::Param(Param::new(pat), span, (0, 0))
1030            }
1031            Want::Arg => {
1032                let e = self.ex(Expr::Err);
1033                Val::Arg(Arg::positional(e))
1034            }
1035            Want::Entry => {
1036                let e = self.ex(Expr::Err);
1037                Val::Entry(MapEntry {
1038                    key: None,
1039                    value: e,
1040                })
1041            }
1042            Want::GArg => {
1043                let t = self.ty(Ty::Err);
1044                Val::GArg(GenericArg::Ty(t))
1045            }
1046            Want::Segment
1047            | Want::Arm
1048            | Want::Capture
1049            | Want::Field
1050            | Want::Variant
1051            | Want::Generic
1052            | Want::Where
1053            | Want::Init
1054            | Want::Rule
1055            | Want::FieldPat(_) => Val::Err,
1056        }
1057    }
1058
1059    /// Reports that a construct cannot appear in `want` and leaves its error
1060    /// value.
1061    pub(crate) fn not_here(&mut self, want: Want, span: Span) {
1062        self.report(
1063            ProblemKind::NotAllowedHere {
1064                expected: want.context(),
1065            },
1066            span,
1067        );
1068        let v = self.error_val(want, span);
1069        self.push(v, span);
1070    }
1071
1072    /// Converts an expression to the value a context takes.
1073    pub(crate) fn adapt_expr(&mut self, e: ExprId, place: bool, want: Want, span: Span) -> Val {
1074        match want {
1075            Want::Expr | Want::Stmt => Val::Expr(e),
1076            Want::Arg => Val::Arg(Arg {
1077                kind: ArgKind::Positional,
1078                value: e,
1079                place: place && self.m.policy.place_args == crate::PlaceArgs::Places,
1080            }),
1081            Want::Entry => Val::Entry(MapEntry {
1082                key: None,
1083                value: e,
1084            }),
1085            Want::GArg => Val::GArg(GenericArg::Const(e)),
1086            _ => {
1087                // Unreachable by construction (`enter` checks the context
1088                // first); keep the expression attached by wrapping it in an
1089                // error-free statement where possible.
1090                self.report(
1091                    ProblemKind::NotAllowedHere {
1092                        expected: want.context(),
1093                    },
1094                    span,
1095                );
1096                self.error_val(want, span)
1097            }
1098        }
1099    }
1100
1101    // ---------------------------------------------------------------- text
1102
1103    /// The source text of `span`, or `""` (and a problem) when the span lies
1104    /// outside the source.
1105    pub(crate) fn text_of(&mut self, span: Span) -> &'t str {
1106        let (s, e) = (span.start().to_usize(), span.end().to_usize());
1107        match self.src.get(s..e) {
1108            Some(t) => t,
1109            None => {
1110                self.report(ProblemKind::BadSpan, span);
1111                ""
1112            }
1113        }
1114    }
1115
1116    pub(crate) fn intern(&mut self, s: &str) -> Symbol {
1117        match self.names.try_intern(s) {
1118            Ok(sym) => sym,
1119            Err(_) => {
1120                self.sym_failed = true;
1121                self.empty
1122            }
1123        }
1124    }
1125
1126    /// The mapping string `i` (texts of templates, extern names) as a symbol.
1127    pub(crate) fn sym(&self, i: u32) -> Symbol {
1128        self.syms.get(i as usize).copied().unwrap_or(self.empty)
1129    }
1130
1131    /// The name an element spells: a name-like token (sigil stripped), or a
1132    /// `Pass` wrapper around one. Other tokens count by their text.
1133    pub(crate) fn name_of(&mut self, elem: &'t Element<K>) -> Option<(Symbol, Span)> {
1134        let mut cur = elem;
1135        loop {
1136            match cur {
1137                Element::Token(t) => {
1138                    let span = t.span();
1139                    let strip = match self.m.role_for(t.kind()) {
1140                        Some(TokenRole::Name) | None => 0,
1141                        Some(
1142                            TokenRole::Var { sigil }
1143                            | TokenRole::Label { sigil }
1144                            | TokenRole::Lifetime { sigil },
1145                        ) => usize::from(sigil),
1146                        Some(_) => return None,
1147                    };
1148                    let text = self.text_of(span);
1149                    let name = text.get(strip..).unwrap_or("");
1150                    return Some((self.intern(name), span));
1151                }
1152                Element::Node(n) => {
1153                    pick::collect_kids(n, self.labeler, self.m, &mut self.kids2, None);
1154                    let arch = self.m.rule_for(n.kind()).copied();
1155                    let inner = match arch {
1156                        Some(Arch::Pass { inner } | Arch::Segment { name: inner, .. }) => {
1157                            pick::single(inner, &self.kids2, self.m)
1158                        }
1159                        Some(Arch::Path { segments, .. }) => {
1160                            let mut out = Vec::new();
1161                            pick::list(segments, &self.kids2, self.m, &mut out);
1162                            if out.len() == 1 {
1163                                out.first().copied()
1164                            } else {
1165                                None
1166                            }
1167                        }
1168                        _ => None,
1169                    };
1170                    cur = inner?;
1171                }
1172            }
1173        }
1174    }
1175
1176    /// The modifiers among the node's direct significant children (and the
1177    /// tokens directly inside container children).
1178    pub(crate) fn mods(&self) -> u32 {
1179        if self.m.modifiers.is_empty() {
1180            return 0;
1181        }
1182        let mut bits = 0;
1183        for kid in &self.kids {
1184            match kid.elem {
1185                Element::Token(t) => bits |= crate::mapping::modifier_bits(self.m, t.kind()),
1186                Element::Node(n) => {
1187                    if crate::pick::KindSets::is_container(self.m, n.kind()) {
1188                        for c in n.child_tokens() {
1189                            bits |= crate::mapping::modifier_bits(self.m, c.kind());
1190                        }
1191                    }
1192                }
1193            }
1194        }
1195        bits
1196    }
1197
1198    // --------------------------------------------------------------- scopes
1199
1200    pub(crate) fn push_label(&mut self, sym: Symbol, b: BinderId) {
1201        self.labels.entry(sym).or_default().push(b);
1202        self.label_log.push(sym);
1203    }
1204
1205    pub(crate) fn pop_labels(&mut self, mark: usize) {
1206        while self.label_log.len() > mark {
1207            if let Some(sym) = self.label_log.pop() {
1208                if let Some(stack) = self.labels.get_mut(&sym) {
1209                    let _ = stack.pop();
1210                }
1211            }
1212        }
1213    }
1214
1215    pub(crate) fn find_label(&self, sym: Symbol) -> Option<BinderId> {
1216        self.labels.get(&sym).and_then(|s| s.last().copied())
1217    }
1218
1219    pub(crate) fn push_fx(&mut self, declared: Effects) {
1220        self.fx.push(Fx {
1221            declared,
1222            inferred: Effects::NONE,
1223            hoist_base: self.hoist.len(),
1224            decl_base: self.declared.len(),
1225        });
1226    }
1227
1228    /// Marks the innermost function-like frame as doing `effect`.
1229    pub(crate) fn infer(&mut self, effect: Effects) {
1230        if let Some(top) = self.fx.last_mut() {
1231            top.inferred = top.inferred.union(effect);
1232        }
1233    }
1234
1235    pub(crate) fn hoist_name(&mut self, sym: Symbol, span: Span) {
1236        if !self.fx.is_empty() {
1237            self.hoist.push((sym, span));
1238        }
1239    }
1240
1241    pub(crate) fn declare_name(&mut self, sym: Symbol) {
1242        if !self.fx.is_empty() {
1243            self.declared.push(sym);
1244        }
1245    }
1246
1247    /// Closes the innermost function-like frame: its effects, and the `let`
1248    /// statements for its hoisted names (deduplicated, minus the names a
1249    /// parameter, `global`, or `static` declares), in first-use order.
1250    pub(crate) fn pop_fx(&mut self, span: Span) -> (Effects, Vec<StmtId>) {
1251        let Some(fx) = self.fx.pop() else {
1252            return (Effects::NONE, Vec::new());
1253        };
1254        let policy = self.m.policy;
1255        let mut effects = fx.declared;
1256        for (mode, flag) in [
1257            (policy.throws, Effects::THROWS),
1258            (policy.yields, Effects::YIELD),
1259            (policy.asyncs, Effects::ASYNC),
1260        ] {
1261            let add = match mode {
1262                EffectMode::Declared => false,
1263                EffectMode::Inferred => fx.inferred.contains(flag),
1264                EffectMode::Always => true,
1265            };
1266            if add {
1267                effects = effects.union(flag);
1268            }
1269        }
1270        let mut stmts = Vec::new();
1271        if self.hoist.len() > fx.hoist_base {
1272            let mut names: Vec<(Symbol, Span)> = self.hoist.split_off(fx.hoist_base);
1273            let mut declared: Vec<Symbol> = self.declared.split_off(fx.decl_base);
1274            declared.sort();
1275            names.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.start().cmp(&b.1.start())));
1276            names.dedup_by(|later, first| later.0 == first.0);
1277            names.retain(|(s, _)| declared.binary_search(s).is_err());
1278            names.sort_by_key(|(_, sp)| sp.start());
1279            for (sym, sp) in names {
1280                self.at(sp);
1281                let b = self.binder(Name::new(sym), BinderKind::Local, true);
1282                let pat = self.pt(Pat::Bind {
1283                    binder: b,
1284                    mode: hir_lang::BindMode::Value,
1285                    sub: None,
1286                });
1287                stmts.push(self.st(Stmt::Let {
1288                    pat,
1289                    ty: None,
1290                    init: None,
1291                    else_: None,
1292                }));
1293            }
1294            self.at(span);
1295        } else {
1296            self.hoist.truncate(fx.hoist_base);
1297            self.declared.truncate(fx.decl_base);
1298        }
1299        (effects, stmts)
1300    }
1301
1302    // ----------------------------------------------- function-scoped binding
1303
1304    /// The temporary a function-scoped pattern binding binds: marked with a
1305    /// fresh `Desugar` expansion (so no user name can capture it), logged for
1306    /// its consumer, and its name hoisted to the enclosing function.
1307    pub(crate) fn persist_binder(&mut self, sym: Symbol, span: Span) -> BinderId {
1308        let expn = self.b.expansion(hir_lang::Expansion {
1309            kind: hir_lang::ExpnKind::Desugar,
1310            name: self.hoist_sym,
1311            call_site: span,
1312            parent: hir_lang::ExpnId::ROOT,
1313            def_site: hir_lang::ExpnId::ROOT,
1314        });
1315        self.at(span);
1316        let tmp = self.binder(Name::marked(sym, expn), BinderKind::Local, true);
1317        self.hoist_name(sym, span);
1318        self.persist.push(Persist { tmp, sym, span });
1319        tmp
1320    }
1321
1322    /// `name = tmp;` for each binding, in order.
1323    pub(crate) fn persist_assigns(&mut self, binds: &[Persist], out: &mut Vec<StmtId>) {
1324        for p in binds {
1325            let target = self.name_expr(p.sym, p.span);
1326            self.at(p.span);
1327            let value = self.b.use_binder(p.tmp);
1328            self.nodes += 2;
1329            let assign = self.ex(Expr::Assign {
1330                target,
1331                op: None,
1332                value,
1333            });
1334            out.push(self.st(Stmt::Expr(assign)));
1335        }
1336    }
1337
1338    /// `{ name = tmp; ...; body }`, or `body` without bindings.
1339    pub(crate) fn with_assigns(&mut self, binds: &[Persist], body: ExprId, span: Span) -> ExprId {
1340        if binds.is_empty() {
1341            return body;
1342        }
1343        let mut stmts = Vec::with_capacity(binds.len());
1344        self.persist_assigns(binds, &mut stmts);
1345        self.at(span);
1346        let list = self.list(&stmts);
1347        self.ex(Expr::Block(hir_lang::Block {
1348            stmts: list,
1349            tail: Some(body),
1350            label: None,
1351            is_unsafe: false,
1352        }))
1353    }
1354
1355    // ----------------------------------------------------------- attributes
1356
1357    /// Lowers attribute nodes (data only: literal and name arguments) into the
1358    /// pool; returns their range.
1359    fn lower_attrs(&mut self, elems: &[&'t Element<K>]) -> AttrRange {
1360        let start = u32::try_from(self.attr_pool.len()).unwrap_or(u32::MAX);
1361        let mut kids = Vec::new();
1362        let mut arg_kids = Vec::new();
1363        let mut args_sel: Vec<&'t Element<K>> = Vec::new();
1364        for &e in elems {
1365            let Element::Node(n) = e else { continue };
1366            let Some(Arch::Attr { name, args }) = self.m.rule_for(n.kind()).copied() else {
1367                continue;
1368            };
1369            pick::collect_kids(n, self.labeler, self.m, &mut kids, None);
1370            let Some(name_elem) = pick::single(name, &kids, self.m) else {
1371                self.report(ProblemKind::MissingField { field: "name" }, e.span());
1372                continue;
1373            };
1374            let name_span = name_elem.span();
1375            let sym = match name_elem {
1376                Element::Token(_) => self.name_of(name_elem).map(|(s, _)| s),
1377                Element::Node(_) => {
1378                    let text = self.text_of(name_span).trim();
1379                    Some(self.intern(text))
1380                }
1381            }
1382            .unwrap_or(self.empty);
1383            args_sel.clear();
1384            pick::list(args, &kids, self.m, &mut args_sel);
1385            let mut out_args: Vec<hir_lang::AttrArg> = Vec::new();
1386            for &a in &args_sel {
1387                let arg_rule = match a {
1388                    Element::Node(an) => self.m.rule_for(an.kind()).copied(),
1389                    Element::Token(_) => None,
1390                };
1391                if let (Some(Arch::AttrArg { key, value }), Element::Node(an)) = (arg_rule, a) {
1392                    pick::collect_kids(an, self.labeler, self.m, &mut arg_kids, None);
1393                    let key = pick::single(key, &arg_kids, self.m)
1394                        .and_then(|k| self.name_of(k))
1395                        .map(|(s, sp)| hir_lang::Ident::new(s, sp));
1396                    let value = pick::single(value, &arg_kids, self.m);
1397                    let value = match value {
1398                        Some(v) => match self.attr_value(v) {
1399                            Some(v) => Some(v),
1400                            None => continue,
1401                        },
1402                        None => None,
1403                    };
1404                    if key.is_none() && value.is_none() {
1405                        continue;
1406                    }
1407                    out_args.push(hir_lang::AttrArg { key, value });
1408                } else if let Some(v) = self.attr_value(a) {
1409                    out_args.push(hir_lang::AttrArg {
1410                        key: None,
1411                        value: Some(v),
1412                    });
1413                }
1414            }
1415            let list = self.list(&out_args);
1416            self.attr_pool.push(hir_lang::Attr {
1417                name: hir_lang::Ident::new(sym, name_span),
1418                args: list,
1419            });
1420        }
1421        let end = u32::try_from(self.attr_pool.len()).unwrap_or(u32::MAX);
1422        (start, end.saturating_sub(start))
1423    }
1424
1425    /// An attribute argument value: a literal or a name; anything else is
1426    /// reported and left out.
1427    fn attr_value(&mut self, e: &'t Element<K>) -> Option<hir_lang::AttrValue> {
1428        match e {
1429            Element::Token(t) => {
1430                let span = t.span();
1431                match self.m.role_for(t.kind()) {
1432                    Some(TokenRole::Name | TokenRole::Var { .. }) => {
1433                        let (sym, _) = self.token_name(span, TokenRole::Name);
1434                        Some(hir_lang::AttrValue::Name(hir_lang::Ident::new(sym, span)))
1435                    }
1436                    Some(
1437                        role @ (TokenRole::Int
1438                        | TokenRole::Float
1439                        | TokenRole::Number
1440                        | TokenRole::Str(_)
1441                        | TokenRole::RawStr
1442                        | TokenRole::Heredoc(_)
1443                        | TokenRole::Char(_)
1444                        | TokenRole::Bytes(_)
1445                        | TokenRole::True
1446                        | TokenRole::False
1447                        | TokenRole::Null),
1448                    ) => match self.token_lit(role, span, false) {
1449                        token::Decoded::Lit(l) => Some(hir_lang::AttrValue::Lit(l)),
1450                        token::Decoded::Bad => None,
1451                    },
1452                    _ => {
1453                        self.report(ProblemKind::AttributeArgument, span);
1454                        None
1455                    }
1456                }
1457            }
1458            Element::Node(n) => {
1459                let span = n.span();
1460                match self.m.rule_for(n.kind()).copied() {
1461                    Some(Arch::Path { .. } | Arch::Segment { .. }) => {
1462                        let text = self.text_of(span).trim();
1463                        let sym = self.intern(text);
1464                        Some(hir_lang::AttrValue::Name(hir_lang::Ident::new(sym, span)))
1465                    }
1466                    Some(Arch::Pass { .. }) => match self.name_of(e) {
1467                        Some((sym, sp)) => {
1468                            Some(hir_lang::AttrValue::Name(hir_lang::Ident::new(sym, sp)))
1469                        }
1470                        None => {
1471                            self.report(ProblemKind::AttributeArgument, span);
1472                            None
1473                        }
1474                    },
1475                    Some(Arch::Unary { op, operand }) => {
1476                        let mut kids = Vec::new();
1477                        pick::collect_kids(n, self.labeler, self.m, &mut kids, None);
1478                        let op_tok = pick::single(op, &kids, self.m);
1479                        let neg = match op_tok {
1480                            Some(Element::Token(t)) => {
1481                                let text = if self.m.has_op_text() {
1482                                    self.text_of(t.span())
1483                                } else {
1484                                    ""
1485                                };
1486                                matches!(
1487                                    self.m.op(crate::mapping::Fixity::Prefix, t.kind(), text),
1488                                    Some(OpSpec::Op(hir_lang::OpKind::Neg))
1489                                )
1490                            }
1491                            _ => false,
1492                        };
1493                        let num = match pick::single(operand, &kids, self.m) {
1494                            Some(Element::Token(t)) => Some(*t),
1495                            _ => None,
1496                        };
1497                        match (
1498                            neg,
1499                            num.and_then(|t| self.m.role_for(t.kind()).map(|r| (t, r))),
1500                        ) {
1501                            (
1502                                true,
1503                                Some((
1504                                    t,
1505                                    role @ (TokenRole::Int | TokenRole::Float | TokenRole::Number),
1506                                )),
1507                            ) => match self.token_lit(role, t.span(), true) {
1508                                token::Decoded::Lit(l) => Some(hir_lang::AttrValue::Lit(l)),
1509                                token::Decoded::Bad => None,
1510                            },
1511                            _ => {
1512                                self.report(ProblemKind::AttributeArgument, span);
1513                                None
1514                            }
1515                        }
1516                    }
1517                    _ => {
1518                        self.report(ProblemKind::AttributeArgument, span);
1519                        None
1520                    }
1521                }
1522            }
1523        }
1524    }
1525
1526    /// Attaches a frame's attributes to the node its value is.
1527    pub(crate) fn attach_attrs(&mut self, v: &mut Val, attrs: AttrRange, span: Span) {
1528        let (s, l) = (attrs.0 as usize, attrs.1 as usize);
1529        let Some(list) = self.attr_pool.get(s..s.saturating_add(l)) else {
1530            return;
1531        };
1532        let list: Vec<hir_lang::Attr> = list.to_vec();
1533        let node = match *v {
1534            Val::Item(i) => Some(NodeRef::Item(i)),
1535            Val::Expr(e) => Some(NodeRef::Expr(e)),
1536            Val::Stmt(st) => Some(NodeRef::Stmt(st)),
1537            Val::Pat(p) => Some(NodeRef::Pat(p)),
1538            Val::Ty(t) => Some(NodeRef::Ty(t)),
1539            Val::Path(p) => Some(NodeRef::Path(p)),
1540            Val::Field(f, _) => Some(NodeRef::Field(f)),
1541            Val::Variant(var) => Some(NodeRef::Variant(var)),
1542            Val::Arg(a) => Some(NodeRef::Expr(a.value)),
1543            Val::Param(p, sp, _) => {
1544                *v = Val::Param(p, sp, attrs);
1545                return;
1546            }
1547            Val::Spill(ss, sl) => {
1548                let (ss, sl) = (ss as usize, sl as usize);
1549                let nodes: Vec<NodeRef> = self
1550                    .spill
1551                    .get(ss..ss.saturating_add(sl))
1552                    .unwrap_or(&[])
1553                    .iter()
1554                    .filter_map(|slot| match slot.v {
1555                        Val::Item(i) => Some(NodeRef::Item(i)),
1556                        Val::Stmt(st) => Some(NodeRef::Stmt(st)),
1557                        Val::Expr(e) => Some(NodeRef::Expr(e)),
1558                        _ => None,
1559                    })
1560                    .collect();
1561                for n in nodes {
1562                    self.b.attach(n, &list);
1563                }
1564                return;
1565            }
1566            _ => None,
1567        };
1568        match node {
1569            Some(n) => self.b.attach(n, &list),
1570            None => self.report(ProblemKind::MisplacedAttribute, span),
1571        }
1572    }
1573
1574    /// `body`, or `{ hoisted lets; body }` when there are hoisted names.
1575    pub(crate) fn wrap_hoisted(&mut self, hoisted: &[StmtId], body: ExprId, span: Span) -> ExprId {
1576        if hoisted.is_empty() {
1577            return body;
1578        }
1579        self.at(span);
1580        let stmts = self.list(hoisted);
1581        self.ex(Expr::Block(hir_lang::Block {
1582            stmts,
1583            tail: Some(body),
1584            label: None,
1585            is_unsafe: false,
1586        }))
1587    }
1588}
1589
1590/// The span a HIR validation problem is about: its node's origin, or its
1591/// binder's or expansion's.
1592fn hir_span(hir: &Hir, e: &HirError) -> Span {
1593    match e.node() {
1594        Some(n) => hir.origin(n).span,
1595        None => match e {
1596            HirError::BinderBoundTwice { binder, .. }
1597            | HirError::BinderKind { binder, .. }
1598            | HirError::DuplicateBinding { binder, .. } => hir.binder_origin(*binder).span,
1599            HirError::Dangling {
1600                site: Site::Binder(b),
1601                ..
1602            } => hir.binder_origin(*b).span,
1603            HirError::Dangling {
1604                site: Site::Expansion(x),
1605                ..
1606            }
1607            | HirError::ExpansionOrder { expn: x } => {
1608                hir.expansion(*x).map_or(Span::empty(0), |x| x.call_site)
1609            }
1610            _ => hir.origin(NodeRef::Item(hir.root())).span,
1611        },
1612    }
1613}