1mod 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
36pub(crate) type AttrRange = (u32, u32);
38
39#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
54#[non_exhaustive]
55pub struct Budget {
56 pub max_nodes: u64,
60 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#[derive(Clone, Debug)]
86#[non_exhaustive]
87pub struct Lowered {
88 pub hir: Hir,
90 pub problems: Vec<Problem>,
92}
93
94impl Lowered {
95 #[must_use]
116 pub fn has_problems(&self) -> bool {
117 !self.problems.is_empty()
118 }
119}
120
121pub 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 #[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 #[must_use]
180 pub fn with_budget(mut self, budget: Budget) -> Self {
181 self.budget = budget;
182 self
183 }
184
185 #[must_use]
191 pub fn with_unit(mut self, unit: UnitId) -> Self {
192 self.unit = unit;
193 self
194 }
195
196 #[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 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#[derive(Clone, Copy, Debug, PartialEq, Eq)]
248pub(crate) struct PatSite {
249 pub(crate) param: bool,
251 pub(crate) mutable: bool,
253 pub(crate) arm: bool,
256 pub(crate) bound: bool,
258 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#[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#[derive(Clone, Copy, Debug)]
291pub(crate) enum Piece {
292 Text(hir_lang::TextRef, Span),
294 Expr,
296}
297
298#[derive(Clone, Copy, Debug, PartialEq, Eq)]
300pub(crate) enum Want {
301 Root,
303 Expr,
305 Stmt,
308 Member,
310 Pat(PatSite),
312 Ty,
314 Path(Ns),
316 Segment,
318 Param,
320 Arg,
322 Arm,
324 Capture,
326 Field,
328 Rule,
330 Variant,
332 Generic,
334 GArg,
336 Where,
338 Entry,
340 Init,
342 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 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#[derive(Clone, Copy, Debug)]
379pub(crate) enum Val {
380 Err,
383 Expr(ExprId),
384 Stmt(StmtId),
385 Item(ItemId),
386 Spill(u32, u32),
389 Pat(PatId),
390 Rest(Option<PatId>),
392 Ty(TyId),
393 Path(PathId),
394 Seg(Segment),
395 Param(Param, Span, AttrRange),
398 Marker(ParamKind),
400 Arg(Arg),
401 Arm(hir_lang::Arm),
402 Cap(Capture),
403 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#[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#[derive(Clone, Copy)]
426pub(crate) enum Task<'t, K> {
427 Elem(&'t Element<K>, Want),
429 Node(&'t Node<K>, Want),
431 Finish,
433 Mark,
436}
437
438#[derive(Clone, Copy, Debug)]
440pub(crate) enum Build {
441 Arch,
443 Op(OpSpec),
445 Template {
447 id: TemplateId,
448 label: Option<BinderId>,
449 nodes_at: u64,
450 },
451 RootWrap,
453 HoistedLet { name: Symbol, name_span: Span },
455}
456
457#[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 pub(crate) name: Option<(Symbol, Span)>,
475 pub(crate) alias: Option<(Symbol, Span)>,
477 pub(crate) binder: Option<BinderId>,
479 pub(crate) index: Option<u32>,
481 pub(crate) flag: bool,
484 pub(crate) path_root: hir_lang::PathRoot,
486 pub(crate) attrs: AttrRange,
488 pub(crate) aux: (u32, u32),
491}
492
493#[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
503pub(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 pub(crate) attr_elems: Vec<&'t Element<K>>,
539 pub(crate) attr_pool: Vec<hir_lang::Attr>,
541 pub(crate) pending_attrs: AttrRange,
543 pub(crate) persist: Vec<Persist>,
545 pub(crate) persist_marks: Vec<(u32, u32)>,
547 pub(crate) pieces: Vec<Piece>,
549 pub(crate) hoist_sym: Symbol,
551}
552
553#[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 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 if self.pending_attrs.1 > 0 {
714 self.pending_attrs = (0, 0);
715 self.report(ProblemKind::MisplacedAttribute, span);
716 }
717 }
718
719 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 pub(crate) fn push_place(&mut self, v: Val, span: Span, place: bool) {
730 self.vals.push(Slot { v, span, place });
731 }
732
733 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 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 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 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 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 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 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 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 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 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 pub(crate) fn give(&mut self, mut buf: Vec<Slot>) {
925 buf.clear();
926 self.bufs.push(buf);
927 }
928
929 pub(crate) fn report(&mut self, kind: ProblemKind, span: Span) {
932 self.problems.push(Problem { kind, span });
933 }
934
935 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 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 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 pub(crate) fn error_val(&mut self, want: Want, span: Span) -> Val {
1010 self.at(span);
1011 match want {
1012 Want::Root => {
1013 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 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 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 self.report(
1091 ProblemKind::NotAllowedHere {
1092 expected: want.context(),
1093 },
1094 span,
1095 );
1096 self.error_val(want, span)
1097 }
1098 }
1099 }
1100
1101 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 pub(crate) fn sym(&self, i: u32) -> Symbol {
1128 self.syms.get(i as usize).copied().unwrap_or(self.empty)
1129 }
1130
1131 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 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 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 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 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 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 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 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 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 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 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 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
1590fn 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}