lower_lang/arch.rs
1//! The archetype catalogue: the semantic forms a construct can be declared as.
2//!
3//! Each variant of [`Arch`] is one archetype from LexerSketch's semantic layer
4//! (ARCHITECTURE §5): binders, scopes, declarations, control, expressions,
5//! patterns, and types. Its fields are [`Pick`]s that say where each part of
6//! the construct sits among the node's children. Flags that a construct
7//! spells with keywords (`mut`, `async`, `static`, `pub`, `...`, `&`) are not
8//! fields: they are read from the node's direct children through the
9//! mapping's [modifier table](crate::MappingBuilder::modifier).
10
11use hir_lang::CaptureMode;
12
13use crate::mapping::Join;
14use crate::pick::Pick;
15use crate::template::TemplateId;
16
17/// How a record or variant's field layout is decided.
18///
19/// # Examples
20///
21/// ```
22/// use lower_lang::RecordShape;
23///
24/// assert_eq!(RecordShape::default(), RecordShape::Auto);
25/// ```
26#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
27pub enum RecordShape {
28 /// Named when any field has a name or there are no fields; tuple-like when
29 /// every field is unnamed.
30 #[default]
31 Auto,
32 /// `{ x: T }`.
33 Named,
34 /// `(T, U)`.
35 Tuple,
36 /// No fields at all (`struct S;`, a bare enum variant).
37 Unit,
38}
39
40/// A use of a registered template as a node's archetype, made by
41/// [`MappingBuilder::template_node`](crate::MappingBuilder::template_node).
42///
43/// The template's holes are bound, in declaration order, to picks stored in
44/// the mapping.
45///
46/// # Examples
47///
48/// ```
49/// use lower_lang::{templates, Arch, MappingBuilder, Pick};
50///
51/// let mut b = MappingBuilder::<u8>::new();
52/// let while_loop = b.template(templates::while_loop())?;
53/// b.template_node(4, while_loop, &[Pick::NthNode(0), Pick::NthNode(1), Pick::Absent]);
54/// let mapping = b.build()?;
55/// assert!(matches!(mapping.rule_for(&4), Some(Arch::Template(_))));
56/// # Ok::<(), lower_lang::MappingError>(())
57/// ```
58#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
59pub struct TemplateUse {
60 pub(crate) template: TemplateId,
61 pub(crate) first: u32,
62 pub(crate) len: u32,
63}
64
65impl TemplateUse {
66 /// The template this node instantiates.
67 ///
68 /// # Examples
69 ///
70 /// ```
71 /// use lower_lang::{templates, Arch, MappingBuilder, Pick};
72 ///
73 /// let mut b = MappingBuilder::<u8>::new();
74 /// let t = b.template(templates::logical_and())?;
75 /// b.template_node(1, t, &[Pick::Nth(0), Pick::Nth(2)]);
76 /// let m = b.build()?;
77 /// let Some(Arch::Template(used)) = m.rule_for(&1) else { unreachable!() };
78 /// assert_eq!(used.template(), t);
79 /// # Ok::<(), lower_lang::MappingError>(())
80 /// ```
81 #[must_use]
82 pub const fn template(&self) -> TemplateId {
83 self.template
84 }
85}
86
87/// A semantic archetype: what a syntax node *means*, and where its parts are.
88///
89/// A [`Mapping`](crate::Mapping) maps node kinds to archetypes. Lowering a node
90/// lowers each field in the context the archetype gives it (an `If`'s `cond`
91/// as an expression, a `Fn`'s `params` as parameters) and builds the HIR form.
92/// Fields marked *required* that a node lacks become error nodes plus a
93/// [`MissingField`](crate::ProblemKind::MissingField) problem; the others are
94/// simply left out. List fields take every match of their pick.
95///
96/// The modifiers each archetype reads from its direct children are listed
97/// with it (see [`Modifier`](crate::Modifier)).
98///
99/// # Examples
100///
101/// ```
102/// use lower_lang::{Arch, Pick};
103///
104/// // `if COND BLOCK (else (if_expr | BLOCK))?` with no field labels:
105/// let if_expr: Arch<u16> = Arch::If {
106/// cond: Pick::NthNode(0),
107/// then: Pick::NthNode(1),
108/// else_: Pick::NthNode(2),
109/// };
110/// assert!(matches!(if_expr, Arch::If { .. }));
111/// ```
112#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
113#[non_exhaustive]
114pub enum Arch<K> {
115 // ------------------------------------------------------------ structure
116 /// A module: the root of a file, or a nested module. Declarations among
117 /// `items` become the module's items; everything else (statements and
118 /// expressions) becomes its top-level body, in order (scripts, REPL
119 /// entries, conditional definitions).
120 Module {
121 /// The module's name (nested modules).
122 name: Pick<K>,
123 /// List: declarations and top-level statements.
124 items: Pick<K>,
125 },
126 /// A wrapper with one meaningful child (`expr`, a parenthesized group, a
127 /// statement wrapper): lowered as that child, in the same context.
128 Pass {
129 /// Required: the child.
130 inner: Pick<K>,
131 },
132 /// Several statements, items, or class members from one node
133 /// (`let a = 1, b = 2;`), spliced into the enclosing list.
134 Splice {
135 /// List: the parts, lowered in the enclosing list's context.
136 items: Pick<K>,
137 },
138 /// A construct the language has but does not lower yet: an error node
139 /// plus an [`Unsupported`](crate::ProblemKind::Unsupported) problem.
140 Unsupported,
141
142 // ------------------------------------------------- statements and binders
143 /// `let PAT: TY = INIT else ELSE;` — binds the pattern's names for the
144 /// following statements. Modifiers: `Mut` makes the binders mutable.
145 Let {
146 /// Required: the pattern (an identifier binds it).
147 pat: Pick<K>,
148 /// The annotation.
149 ty: Pick<K>,
150 /// The initializer.
151 init: Pick<K>,
152 /// The diverging `else` of let-else.
153 else_: Pick<K>,
154 /// Whether the binders are mutable without a `Mut` modifier (`var`,
155 /// JavaScript `let`) rather than only with one (`let` / `let mut`).
156 mutable: bool,
157 /// Function-scoped hoisting (JavaScript `var`): the name is declared
158 /// once at the top of the enclosing function (or module body) and this
159 /// statement becomes an assignment. Any other pattern (`var [a, b] =
160 /// x`) binds hygienic temporaries that are assigned to the hoisted
161 /// names right after it matches; a by-reference binding cannot be
162 /// hoisted this way and is reported
163 /// ([`CannotHoist`](crate::ProblemKind::CannotHoist)).
164 hoist: bool,
165 },
166 /// `EXPR;` — an expression evaluated for its effect (never a block tail).
167 ExprStmt {
168 /// Required: the expression.
169 expr: Pick<K>,
170 },
171 /// `defer BODY` — runs when the enclosing block exits.
172 Defer {
173 /// Required: the deferred expression.
174 body: Pick<K>,
175 },
176 /// `static $x = INIT;` — a function-static variable (PHP, C).
177 Static {
178 /// Required: the variable's name.
179 name: Pick<K>,
180 /// The annotation.
181 ty: Pick<K>,
182 /// The initializer.
183 init: Pick<K>,
184 },
185 /// `global $a, $b;` — locals aliasing globals, one statement per name.
186 GlobalDecl {
187 /// List: the names.
188 names: Pick<K>,
189 },
190 /// `use a::b as c;` / `use a::*;` — an import item. Modifiers: `Glob`.
191 Import {
192 /// Required: the imported path (a path node or one name).
193 path: Pick<K>,
194 /// The alias (`as c`).
195 alias: Pick<K>,
196 },
197 /// A use tree: `use a::{b, c::{d, e as f}, g::*};` (Rust), `use App\{Foo,
198 /// Bar as Baz};` (PHP group use). Flattened into one HIR import per leaf,
199 /// each path the concatenation of the prefixes above it. Modifiers:
200 /// visibility (on the outermost tree, for every import it makes), `Glob`
201 /// (a `*` leaf).
202 ///
203 /// The expected tree: a use-tree node holds an optional prefix `path` (a
204 /// path node or one name), then either nested `children` (use-tree nodes,
205 /// names, or paths; container nodes such as `{ ... }` splice) or, for a
206 /// leaf, an optional `alias`. A child spelled `self` (a token in the
207 /// [root table](crate::MappingBuilder::root) mapping to
208 /// `PathRoot::SelfModule`) imports the prefix itself. Leading root tokens
209 /// of the outermost prefix (`::`, `crate`, `super`) set the import's root.
210 /// The walk is iterative; nesting depth is bounded only by the tree.
211 UseTree {
212 /// The prefix (or, for a leaf, the whole path).
213 path: Pick<K>,
214 /// A leaf's alias (`as f`).
215 alias: Pick<K>,
216 /// List: the nested trees of a group.
217 children: Pick<K>,
218 },
219
220 // ------------------------------------------------------------ declarations
221 /// A function. Modifiers: visibility, `Async`, `Generator`, `Throws`,
222 /// `Unsafe`. In a class, interface, or impl it is a method.
223 Fn {
224 /// Required: the name.
225 name: Pick<K>,
226 /// List: generic parameters.
227 generics: Pick<K>,
228 /// List: parameters.
229 params: Pick<K>,
230 /// The declared return type.
231 ret: Pick<K>,
232 /// The declared thrown type.
233 throws: Pick<K>,
234 /// The body (absent for required, abstract, or foreign functions).
235 body: Pick<K>,
236 /// List: where-clause predicates.
237 where_: Pick<K>,
238 },
239 /// A record (struct) or union. Modifiers: visibility, `Union`.
240 Record {
241 /// Required: the name.
242 name: Pick<K>,
243 /// List: generic parameters.
244 generics: Pick<K>,
245 /// List: the fields.
246 fields: Pick<K>,
247 /// How the field layout is decided.
248 shape: RecordShape,
249 },
250 /// A sum (enum) with variants. Modifiers: visibility.
251 Sum {
252 /// Required: the name.
253 name: Pick<K>,
254 /// List: generic parameters.
255 generics: Pick<K>,
256 /// List: the variants.
257 variants: Pick<K>,
258 },
259 /// An interface or trait. Modifiers: visibility.
260 Interface {
261 /// Required: the name.
262 name: Pick<K>,
263 /// List: generic parameters.
264 generics: Pick<K>,
265 /// List: super-interfaces (types).
266 supers: Pick<K>,
267 /// List: members (methods, constants, associated types).
268 items: Pick<K>,
269 },
270 /// A class. Modifiers: visibility, `Abstract`, `Final`.
271 Class {
272 /// Required: the name.
273 name: Pick<K>,
274 /// List: generic parameters.
275 generics: Pick<K>,
276 /// List: base classes, in method-resolution order (types).
277 bases: Pick<K>,
278 /// List: implemented interfaces (types).
279 interfaces: Pick<K>,
280 /// List: members: fields, methods, constants, static fields, aliases,
281 /// mixin uses.
282 members: Pick<K>,
283 /// A mixin (PHP trait): never instantiated, used by classes.
284 mixin: bool,
285 },
286 /// An implementation block (`impl Tr for T { ... }`).
287 Impl {
288 /// List: generic parameters.
289 generics: Pick<K>,
290 /// The implemented interface (a type).
291 interface: Pick<K>,
292 /// Required: the implementing type.
293 self_ty: Pick<K>,
294 /// List: members.
295 items: Pick<K>,
296 },
297 /// A type alias. Modifiers: visibility.
298 Alias {
299 /// Required: the name.
300 name: Pick<K>,
301 /// List: generic parameters.
302 generics: Pick<K>,
303 /// Required: the aliased type.
304 ty: Pick<K>,
305 },
306 /// An associated type declared by an interface.
307 AssocType {
308 /// Required: the name.
309 name: Pick<K>,
310 /// List: bounds (types).
311 bounds: Pick<K>,
312 /// A default type.
313 default: Pick<K>,
314 },
315 /// A compile-time constant. Modifiers: visibility.
316 Const {
317 /// Required: the name.
318 name: Pick<K>,
319 /// The annotation.
320 ty: Pick<K>,
321 /// The value (absent only in an interface).
322 value: Pick<K>,
323 },
324 /// A global or static variable item. Modifiers: visibility, `Mut`.
325 GlobalVar {
326 /// Required: the name.
327 name: Pick<K>,
328 /// The annotation.
329 ty: Pick<K>,
330 /// The initializer.
331 init: Pick<K>,
332 /// Whether it is assignable without a `Mut` modifier.
333 mutable: bool,
334 },
335 /// A mixin use inside a class (PHP `use T, U { rules }`).
336 MixinUse {
337 /// List: the mixins (types).
338 mixins: Pick<K>,
339 /// List: conflict-resolution rules ([`MixinRule`](Arch::MixinRule)
340 /// nodes).
341 rules: Pick<K>,
342 },
343 /// One rule of a mixin use: `T::hello insteadof U, V;` or
344 /// `T::hello as protected greet;` / `hello as private;`. With
345 /// `insteadof` types it is an exclusion; otherwise an alias, with a new
346 /// name, a visibility modifier (`Public`, `Protected`, `Private`), or both.
347 /// A rule with neither is reported ([`MissingField`](crate::ProblemKind::MissingField)
348 /// `action`).
349 MixinRule {
350 /// The mixin the method comes from (`T` in `T::hello`), a type.
351 from: Pick<K>,
352 /// Required: the method's name.
353 method: Pick<K>,
354 /// List: the mixins it is taken instead of (types).
355 insteadof: Pick<K>,
356 /// The new name of an alias.
357 alias: Pick<K>,
358 },
359 /// A field of a record, variant, or class. Modifiers: visibility; in a
360 /// class, `Static` makes it a static (global) member instead.
361 FieldDecl {
362 /// The name (absent for a positional field).
363 name: Pick<K>,
364 /// The type.
365 ty: Pick<K>,
366 /// A default value.
367 default: Pick<K>,
368 },
369 /// A variant of a sum.
370 VariantDecl {
371 /// Required: the name.
372 name: Pick<K>,
373 /// List: the fields.
374 fields: Pick<K>,
375 /// An explicit discriminant.
376 discriminant: Pick<K>,
377 /// How the field layout is decided.
378 shape: RecordShape,
379 },
380 /// A generic parameter: a type parameter, a const parameter (with a
381 /// type), or a region (a lifetime-role token).
382 GenericParam {
383 /// Required: the name.
384 name: Pick<K>,
385 /// List: bounds (types).
386 bounds: Pick<K>,
387 /// The type of a const parameter.
388 ty: Pick<K>,
389 /// A default (a type, or a constant for a const parameter).
390 default: Pick<K>,
391 },
392 /// A where-clause predicate: `T: A + B`.
393 WherePred {
394 /// Required: the constrained type.
395 subject: Pick<K>,
396 /// List: the bounds (types).
397 bounds: Pick<K>,
398 },
399 /// A parameter. Modifiers: `Ref` (by reference, PHP `&$x`), `Mut`,
400 /// `Rest` (variadic), `RestNamed`, `NamedOnly`, `PositionalOnly`,
401 /// `Receiver`.
402 Param {
403 /// Required: the pattern (usually one name).
404 pat: Pick<K>,
405 /// The annotation.
406 ty: Pick<K>,
407 /// The default value.
408 default: Pick<K>,
409 },
410 /// An explicit closure capture (PHP `use ($x, &$y)`). Modifiers: `Ref`,
411 /// `Mut`.
412 Capture {
413 /// Required: the captured name.
414 name: Pick<K>,
415 },
416
417 // ------------------------------------------------------------- expressions
418 /// A path: `a::b::c`, `::a`, `self::a`, `super::a`, `Self::a`,
419 /// `<T as Tr>::a`. Leading tokens of `segments` whose kind is in the
420 /// mapping's [root table](crate::MappingBuilder::root) set the root.
421 Path {
422 /// Required (unless qualified): list of segments (names or `Segment`
423 /// nodes).
424 segments: Pick<K>,
425 /// A leading global marker (`::a`).
426 global: Pick<K>,
427 /// The qualified self type (`<T as ...>`).
428 qself: Pick<K>,
429 /// List: the trait segments of a qualified self (`<T as a::Tr>`).
430 qtrait: Pick<K>,
431 },
432 /// A path segment with generic arguments (`Vec::<i32>`).
433 Segment {
434 /// Required: the name.
435 name: Pick<K>,
436 /// List: generic arguments.
437 args: Pick<K>,
438 },
439 /// An associated-type binding in generic arguments: `Item = u8`.
440 AssocBinding {
441 /// Required: the associated type's name.
442 name: Pick<K>,
443 /// Required: its type.
444 ty: Pick<K>,
445 },
446 /// A prefix operator; the operator token's kind is looked up in the
447 /// prefix [operator table](crate::MappingBuilder::prefix_op).
448 Unary {
449 /// Required: the operator token.
450 op: Pick<K>,
451 /// Required: the operand.
452 operand: Pick<K>,
453 },
454 /// A binary operator; the token's kind is looked up in the binary
455 /// operator table. `&&`, `||`, `??`, assignment, and compound assignment
456 /// are operators too.
457 Binary {
458 /// Required: the left operand.
459 lhs: Pick<K>,
460 /// Required: the operator token.
461 op: Pick<K>,
462 /// Required: the right operand.
463 rhs: Pick<K>,
464 },
465 /// A postfix operator; looked up in the postfix operator table.
466 Postfix {
467 /// Required: the operand.
468 operand: Pick<K>,
469 /// Required: the operator token.
470 op: Pick<K>,
471 },
472 /// `TARGET = VALUE` or `TARGET op= VALUE`; a compound operator token is
473 /// looked up in the binary operator table.
474 Assign {
475 /// Required: the place.
476 target: Pick<K>,
477 /// The operator token (absent: plain assignment).
478 op: Pick<K>,
479 /// Required: the value.
480 value: Pick<K>,
481 },
482 /// `$a = &$b`: reference assignment (PHP).
483 RefAssign {
484 /// Required: the place that becomes an alias.
485 target: Pick<K>,
486 /// Required: the aliased place.
487 source: Pick<K>,
488 },
489 /// A call.
490 Call {
491 /// Required: the callee.
492 callee: Pick<K>,
493 /// List: arguments.
494 args: Pick<K>,
495 },
496 /// A call argument: named (`name: value`), spread (`...xs`, modifier
497 /// `Spread`; `**kw`, modifier `SpreadNamed`), or a place (`&$x`, modifier
498 /// `Ref`). Plain expressions in an argument list need no archetype.
499 Arg {
500 /// The argument's name.
501 name: Pick<K>,
502 /// Required: the value.
503 value: Pick<K>,
504 },
505 /// Member access, or a method call when `call` finds something:
506 /// `a.b`, `a.0`, `a.b(x)`.
507 Member {
508 /// Required: the accessed value.
509 base: Pick<K>,
510 /// Required: the member (a name, or a number for tuple fields).
511 member: Pick<K>,
512 /// List: method-call arguments.
513 args: Pick<K>,
514 /// The call marker (such as `(`); `Absent` means never a call.
515 call: Pick<K>,
516 },
517 /// A method call with explicit generic arguments: `x.f::<T>(a)`.
518 MethodCall {
519 /// Required: the receiver.
520 receiver: Pick<K>,
521 /// Required: the method name.
522 method: Pick<K>,
523 /// List: generic arguments.
524 generics: Pick<K>,
525 /// List: arguments.
526 args: Pick<K>,
527 },
528 /// `$o->$name` / `$o->$name(args)`: a member chosen at run time (Mox).
529 DynMember {
530 /// Required: the accessed value.
531 base: Pick<K>,
532 /// Required: the member name, an expression.
533 name: Pick<K>,
534 /// List: method-call arguments.
535 args: Pick<K>,
536 /// The call marker; `Absent` means never a call.
537 call: Pick<K>,
538 },
539 /// `base[index]`.
540 Index {
541 /// Required: the indexed value.
542 base: Pick<K>,
543 /// Required: the index.
544 index: Pick<K>,
545 },
546 /// `$$name`: the variable named at run time (Mox).
547 VarVar {
548 /// Required: the expression naming the variable.
549 inner: Pick<K>,
550 },
551 /// `$a[]`: append position (Mox); only a write target.
552 Append {
553 /// Required: the array.
554 base: Pick<K>,
555 },
556 /// `(a, b)`.
557 Tuple {
558 /// List: elements.
559 elems: Pick<K>,
560 },
561 /// `[a, b]`.
562 Array {
563 /// List: elements.
564 elems: Pick<K>,
565 },
566 /// An ordered map literal: `[k => v, w]` (PHP arrays). Elements that are
567 /// not `MapEntry` nodes take the next index.
568 Map {
569 /// List: entries.
570 entries: Pick<K>,
571 },
572 /// `key => value` inside a map literal.
573 MapEntry {
574 /// The key (absent: next index).
575 key: Pick<K>,
576 /// Required: the value.
577 value: Pick<K>,
578 },
579 /// `Path { x: 1, ..base }` or an anonymous record.
580 RecordLit {
581 /// The record type's path.
582 path: Pick<K>,
583 /// List: field initializers.
584 fields: Pick<K>,
585 /// The `..base` expression.
586 base: Pick<K>,
587 },
588 /// `name: value` (or the shorthand `name`) in a record literal.
589 FieldInit {
590 /// Required: the field name.
591 name: Pick<K>,
592 /// The value (absent: shorthand, the variable of the same name).
593 value: Pick<K>,
594 },
595 /// `[elem; count]`.
596 Repeat {
597 /// Required: the element.
598 elem: Pick<K>,
599 /// Required: the count.
600 count: Pick<K>,
601 },
602 /// `expr as Type`.
603 Cast {
604 /// Required: the converted value.
605 expr: Pick<K>,
606 /// Required: the target type.
607 ty: Pick<K>,
608 },
609 /// `&expr` / `&mut expr`. Modifiers: `Mut`.
610 Borrow {
611 /// Required: the borrowed place.
612 expr: Pick<K>,
613 },
614 /// `*expr`.
615 Deref {
616 /// Required: the dereferenced value.
617 inner: Pick<K>,
618 },
619 /// A block. Modifiers: `Unsafe`.
620 Block {
621 /// List: statements (and local items).
622 stmts: Pick<K>,
623 /// The tail expression (the block's value).
624 tail: Pick<K>,
625 /// A label (`'a: { ... }`).
626 label: Pick<K>,
627 /// Whether a final bare expression (not an expression statement)
628 /// becomes the tail.
629 tail_from_last: bool,
630 },
631 /// `if cond then else`.
632 If {
633 /// Required: the condition.
634 cond: Pick<K>,
635 /// Required: the value when true.
636 then: Pick<K>,
637 /// The value when false.
638 else_: Pick<K>,
639 },
640 /// `match scrutinee { arms }`.
641 Match {
642 /// Required: the matched value.
643 scrutinee: Pick<K>,
644 /// List: arms.
645 arms: Pick<K>,
646 },
647 /// A match arm: `pat if guard => body`.
648 Arm {
649 /// Required: the pattern.
650 pat: Pick<K>,
651 /// The guard.
652 guard: Pick<K>,
653 /// Required: the body.
654 body: Pick<K>,
655 },
656 /// `loop body` — HIR's one loop. `while`, `do-while`, C `for`, and
657 /// `for-each` are [templates](crate::templates).
658 Loop {
659 /// A label.
660 label: Pick<K>,
661 /// Required: the body.
662 body: Pick<K>,
663 },
664 /// `break 'label value`.
665 Break {
666 /// The target label.
667 label: Pick<K>,
668 /// The value.
669 value: Pick<K>,
670 },
671 /// `continue 'label`.
672 Continue {
673 /// The target label.
674 label: Pick<K>,
675 },
676 /// `return value`.
677 Return {
678 /// The value.
679 value: Pick<K>,
680 },
681 /// `throw value`.
682 Throw {
683 /// Required: the raised value.
684 value: Pick<K>,
685 },
686 /// `try body catch... finally...`.
687 Try {
688 /// Required: the protected body.
689 body: Pick<K>,
690 /// List: catch clauses (`Catch` or `Arm` nodes).
691 catches: Pick<K>,
692 /// The `finally` body.
693 finally: Pick<K>,
694 },
695 /// `catch (A | B $e) body`: a type test (or an alternative of several)
696 /// binding `name`. Without types it catches everything.
697 Catch {
698 /// List: the caught types.
699 types: Pick<K>,
700 /// The bound name.
701 name: Pick<K>,
702 /// Required: the handler.
703 body: Pick<K>,
704 },
705 /// `await e`.
706 Await {
707 /// Required: the awaited value.
708 inner: Pick<K>,
709 },
710 /// `yield v`, or `yield k => v` (PHP and Mox generators). A key without
711 /// a value yields `null` under that key.
712 Yield {
713 /// The explicit key.
714 key: Pick<K>,
715 /// The yielded value.
716 value: Pick<K>,
717 },
718 /// `yield from g`: delegate to an inner generator or iterable; evaluates
719 /// to its return value.
720 YieldFrom {
721 /// Required: the inner generator or iterable.
722 inner: Pick<K>,
723 },
724 /// `spawn e` — starts a task running the callable `e`.
725 Spawn {
726 /// Required: the callable.
727 inner: Pick<K>,
728 },
729 /// A closure. Modifiers: `Async`, `Generator`, `Throws`.
730 Closure {
731 /// List: parameters.
732 params: Pick<K>,
733 /// The return type.
734 ret: Pick<K>,
735 /// List: explicit captures (`Capture` nodes or names).
736 captures: Pick<K>,
737 /// Required: the body.
738 body: Pick<K>,
739 /// How outer variables are captured implicitly; `None` means only the
740 /// explicit captures are visible (PHP `function () use (...)`).
741 implicit: Option<CaptureMode>,
742 },
743 /// A node lowered through a template; see
744 /// [`MappingBuilder::template_node`](crate::MappingBuilder::template_node).
745 Template(TemplateUse),
746 /// An interpolated string: `"Hello {$name}!"`, `"$name"`, `"{$a['k']}"`,
747 /// and heredocs, as the lexer delivers them in segments.
748 ///
749 /// The expected tree: the string's node holds its pieces in order. Text
750 /// pieces are tokens with role [`TokenRole::StrPart`](crate::TokenRole::StrPart)
751 /// (no quotes; escapes decoded, adjacent pieces merged); every other
752 /// selected element is an embedded expression (`$name` as a variable
753 /// token, `{$a['k']}` as an index node; braces and quotes are skip kinds
754 /// or left out by the `parts` pick). The parts are joined by `join`:
755 /// one text piece alone is a string literal, no pieces the empty string,
756 /// and a lone expression is joined with an empty string first, so the
757 /// result is always a string conversion.
758 ///
759 /// With `close` (a heredoc), `close` picks the closing-marker token: the
760 /// whitespace before its identifier is removed from the start of every
761 /// line of the text pieces (PHP 7.3+), a line indented less (or starting
762 /// with an embedded expression) is reported
763 /// ([`InvalidLiteral`](crate::ProblemKind::InvalidLiteral)), and the
764 /// newline before the marker is dropped.
765 Interp {
766 /// List: the pieces, in order.
767 parts: Pick<K>,
768 /// A heredoc's closing-marker token.
769 close: Pick<K>,
770 /// How the pieces become one string.
771 join: Join,
772 },
773
774 // -------------------------------------------------------------- attributes
775 /// An attribute (`#[Route("/home", methods: GET)]`, `@inline`), attached
776 /// to the node it is a direct child of. Attribute nodes (and container
777 /// nodes holding them, such as PHP's `#[A, B]` group) are invisible to
778 /// every other pick of that node. HIR attributes are data, never code
779 /// (HIR spec §11): an argument value must be a literal or a name; anything
780 /// else is reported
781 /// ([`AttributeArgument`](crate::ProblemKind::AttributeArgument)) and left
782 /// out.
783 Attr {
784 /// Required: the attribute's name, kept as written (a qualified name
785 /// such as `\App\Route` keeps its separators).
786 name: Pick<K>,
787 /// List: the arguments ([`AttrArg`](Arch::AttrArg) nodes, or values).
788 args: Pick<K>,
789 },
790 /// `key: value` / `key = value` inside an attribute's arguments.
791 AttrArg {
792 /// The key.
793 key: Pick<K>,
794 /// The value (absent: a bare key).
795 value: Pick<K>,
796 },
797
798 // ---------------------------------------------------------------- patterns
799 /// `_` as a node.
800 WildPat,
801 /// `..` (or `rest @ ..`) inside a tuple, constructor, slice, or record
802 /// pattern.
803 RestPat {
804 /// A pattern binding the rest (slices only).
805 bind: Pick<K>,
806 },
807 /// `ref mut x @ sub`. Modifiers: `Ref`, `Mut`.
808 BindPat {
809 /// Required: the bound name.
810 name: Pick<K>,
811 /// A sub-pattern the value must also match.
812 sub: Pick<K>,
813 },
814 /// `(a, b, ..)`.
815 TuplePat {
816 /// List: element patterns (a `RestPat` marks `..`).
817 elems: Pick<K>,
818 },
819 /// `Some(x)`.
820 CtorPat {
821 /// Required: the constructor's path.
822 path: Pick<K>,
823 /// List: element patterns.
824 elems: Pick<K>,
825 },
826 /// `Point { x, y: 0, .. }`.
827 RecordPat {
828 /// The record's path (absent: an anonymous record or object).
829 path: Pick<K>,
830 /// List: field patterns.
831 fields: Pick<K>,
832 },
833 /// `name: pat` (or the shorthand `name`) in a record pattern.
834 FieldPat {
835 /// Required: the field name.
836 name: Pick<K>,
837 /// The pattern (absent: shorthand, binding the field's name).
838 pat: Pick<K>,
839 },
840 /// `a | b`.
841 OrPat {
842 /// List: alternatives.
843 alts: Pick<K>,
844 },
845 /// `lo..hi` / `lo..=hi`. Modifiers: `Inclusive`.
846 RangePat {
847 /// The lower bound.
848 lo: Pick<K>,
849 /// The upper bound.
850 hi: Pick<K>,
851 },
852 /// `&p` / `&mut p`. Modifiers: `Mut`.
853 RefPat {
854 /// Required: the inner pattern.
855 inner: Pick<K>,
856 },
857 /// `[a, b, rest @ .., z]`.
858 SlicePat {
859 /// List: element patterns (a `RestPat` marks `..`).
860 elems: Pick<K>,
861 },
862 /// `e: Type` / `Type e` — a type test, as in `catch` clauses.
863 TypeTestPat {
864 /// Required: the tested type.
865 ty: Pick<K>,
866 /// The pattern the value must then match.
867 pat: Pick<K>,
868 },
869
870 // ------------------------------------------------------------------- types
871 /// `(A, B)`.
872 TupleTy {
873 /// List: element types.
874 elems: Pick<K>,
875 },
876 /// `[T; N]`.
877 ArrayTy {
878 /// Required: the element type.
879 elem: Pick<K>,
880 /// Required: the length.
881 len: Pick<K>,
882 },
883 /// `[T]`.
884 SliceTy {
885 /// Required: the element type.
886 elem: Pick<K>,
887 },
888 /// `&'r mut T`. Modifiers: `Mut`.
889 RefTy {
890 /// Required: the referent type.
891 inner: Pick<K>,
892 /// An explicit region.
893 region: Pick<K>,
894 },
895 /// `*mut T` / `*const T`. Modifiers: `Mut`.
896 PtrTy {
897 /// Required: the pointee type.
898 inner: Pick<K>,
899 },
900 /// `T?`.
901 NullableTy {
902 /// Required: the inner type.
903 inner: Pick<K>,
904 },
905 /// `fn(A, B) -> R`.
906 FnTy {
907 /// List: parameter types.
908 params: Pick<K>,
909 /// The return type (absent: unit).
910 ret: Pick<K>,
911 },
912 /// `dyn A + B`.
913 ObjectTy {
914 /// List: bounds.
915 bounds: Pick<K>,
916 },
917 /// `impl A + B`.
918 ImplTy {
919 /// List: bounds.
920 bounds: Pick<K>,
921 },
922 /// `A | B | C` (PHP union types). Members are lowered in source order;
923 /// hir-lang's `finish` puts them in canonical form (flattened, sorted,
924 /// repeats dropped, `null` hoisted into `Nullable`, one member
925 /// collapsed). No members at all is a
926 /// [`MissingField`](crate::ProblemKind::MissingField) problem.
927 UnionTy {
928 /// List: the member types.
929 members: Pick<K>,
930 },
931 /// `A & B` (PHP intersection types), normalized like
932 /// [`UnionTy`](Self::UnionTy).
933 IntersectionTy {
934 /// List: the member types.
935 members: Pick<K>,
936 },
937}
938
939impl<K> Arch<K> {
940 /// `Pass { inner: Nth(n) }`: the common wrapper.
941 ///
942 /// # Examples
943 ///
944 /// ```
945 /// use lower_lang::{Arch, Pick};
946 ///
947 /// assert_eq!(Arch::<u8>::pass(1), Arch::Pass { inner: Pick::Nth(1) });
948 /// ```
949 #[must_use]
950 pub const fn pass(n: u16) -> Self {
951 Self::Pass {
952 inner: Pick::Nth(n),
953 }
954 }
955
956 /// The shape of lang-forge's operator nodes: `binary` is `lhs op rhs`.
957 ///
958 /// # Examples
959 ///
960 /// ```
961 /// use lower_lang::{Arch, Pick};
962 ///
963 /// let Arch::Binary { op, .. } = Arch::<u8>::binary() else { unreachable!() };
964 /// assert_eq!(op, Pick::Nth(1));
965 /// ```
966 #[must_use]
967 pub const fn binary() -> Self {
968 Self::Binary {
969 lhs: Pick::Nth(0),
970 op: Pick::Nth(1),
971 rhs: Pick::Nth(2),
972 }
973 }
974
975 /// The shape of lang-forge's `prefix` nodes: `op operand`.
976 ///
977 /// # Examples
978 ///
979 /// ```
980 /// use lower_lang::{Arch, Pick};
981 ///
982 /// let Arch::Unary { operand, .. } = Arch::<u8>::prefix() else { unreachable!() };
983 /// assert_eq!(operand, Pick::Nth(1));
984 /// ```
985 #[must_use]
986 pub const fn prefix() -> Self {
987 Self::Unary {
988 op: Pick::Nth(0),
989 operand: Pick::Nth(1),
990 }
991 }
992
993 /// The archetype's name, as problems and docs spell it.
994 ///
995 /// # Examples
996 ///
997 /// ```
998 /// use lower_lang::Arch;
999 ///
1000 /// assert_eq!(Arch::<u8>::binary().name(), "binary");
1001 /// assert_eq!(Arch::<u8>::WildPat.name(), "wildcard pattern");
1002 /// ```
1003 #[must_use]
1004 pub const fn name(&self) -> &'static str {
1005 match self {
1006 Self::Module { .. } => "module",
1007 Self::Pass { .. } => "pass",
1008 Self::Splice { .. } => "splice",
1009 Self::Unsupported => "unsupported",
1010 Self::Let { .. } => "let",
1011 Self::ExprStmt { .. } => "expression statement",
1012 Self::Defer { .. } => "defer",
1013 Self::Static { .. } => "static",
1014 Self::GlobalDecl { .. } => "global declaration",
1015 Self::Import { .. } => "import",
1016 Self::Fn { .. } => "function",
1017 Self::Record { .. } => "record",
1018 Self::Sum { .. } => "sum",
1019 Self::Interface { .. } => "interface",
1020 Self::Class { .. } => "class",
1021 Self::Impl { .. } => "impl",
1022 Self::Alias { .. } => "alias",
1023 Self::AssocType { .. } => "associated type",
1024 Self::Const { .. } => "constant",
1025 Self::GlobalVar { .. } => "global variable",
1026 Self::MixinUse { .. } => "mixin use",
1027 Self::MixinRule { .. } => "mixin rule",
1028 Self::UseTree { .. } => "use tree",
1029 Self::Interp { .. } => "interpolated string",
1030 Self::Attr { .. } => "attribute",
1031 Self::AttrArg { .. } => "attribute argument",
1032 Self::FieldDecl { .. } => "field",
1033 Self::VariantDecl { .. } => "variant",
1034 Self::GenericParam { .. } => "generic parameter",
1035 Self::WherePred { .. } => "where predicate",
1036 Self::Param { .. } => "parameter",
1037 Self::Capture { .. } => "capture",
1038 Self::Path { .. } => "path",
1039 Self::Segment { .. } => "path segment",
1040 Self::AssocBinding { .. } => "associated-type binding",
1041 Self::Unary { .. } => "unary",
1042 Self::Binary { .. } => "binary",
1043 Self::Postfix { .. } => "postfix",
1044 Self::Assign { .. } => "assignment",
1045 Self::RefAssign { .. } => "reference assignment",
1046 Self::Call { .. } => "call",
1047 Self::Arg { .. } => "argument",
1048 Self::Member { .. } => "member",
1049 Self::MethodCall { .. } => "method call",
1050 Self::DynMember { .. } => "dynamic member",
1051 Self::Index { .. } => "index",
1052 Self::VarVar { .. } => "variable variable",
1053 Self::Append { .. } => "append",
1054 Self::Tuple { .. } => "tuple",
1055 Self::Array { .. } => "array",
1056 Self::Map { .. } => "map",
1057 Self::MapEntry { .. } => "map entry",
1058 Self::RecordLit { .. } => "record literal",
1059 Self::FieldInit { .. } => "field initializer",
1060 Self::Repeat { .. } => "repeat",
1061 Self::Cast { .. } => "cast",
1062 Self::Borrow { .. } => "borrow",
1063 Self::Deref { .. } => "dereference",
1064 Self::Block { .. } => "block",
1065 Self::If { .. } => "if",
1066 Self::Match { .. } => "match",
1067 Self::Arm { .. } => "match arm",
1068 Self::Loop { .. } => "loop",
1069 Self::Break { .. } => "break",
1070 Self::Continue { .. } => "continue",
1071 Self::Return { .. } => "return",
1072 Self::Throw { .. } => "throw",
1073 Self::Try { .. } => "try",
1074 Self::Catch { .. } => "catch",
1075 Self::Await { .. } => "await",
1076 Self::Yield { .. } => "yield",
1077 Self::YieldFrom { .. } => "yield from",
1078 Self::Spawn { .. } => "spawn",
1079 Self::Closure { .. } => "closure",
1080 Self::Template(_) => "template",
1081 Self::WildPat => "wildcard pattern",
1082 Self::RestPat { .. } => "rest pattern",
1083 Self::BindPat { .. } => "binding pattern",
1084 Self::TuplePat { .. } => "tuple pattern",
1085 Self::CtorPat { .. } => "constructor pattern",
1086 Self::RecordPat { .. } => "record pattern",
1087 Self::FieldPat { .. } => "field pattern",
1088 Self::OrPat { .. } => "or-pattern",
1089 Self::RangePat { .. } => "range pattern",
1090 Self::RefPat { .. } => "reference pattern",
1091 Self::SlicePat { .. } => "slice pattern",
1092 Self::TypeTestPat { .. } => "type-test pattern",
1093 Self::TupleTy { .. } => "tuple type",
1094 Self::ArrayTy { .. } => "array type",
1095 Self::SliceTy { .. } => "slice type",
1096 Self::RefTy { .. } => "reference type",
1097 Self::PtrTy { .. } => "pointer type",
1098 Self::NullableTy { .. } => "nullable type",
1099 Self::FnTy { .. } => "function type",
1100 Self::ObjectTy { .. } => "object type",
1101 Self::ImplTy { .. } => "impl type",
1102 Self::UnionTy { .. } => "union type",
1103 Self::IntersectionTy { .. } => "intersection type",
1104 }
1105 }
1106
1107 /// The archetype's LSF2 name: the value of `arch = "…"` in
1108 /// `[semantics.bind]` (LSF2 §15.4), and the key
1109 /// [`MappingBuilder::modifier_in`](crate::MappingBuilder::modifier_in)
1110 /// takes. Archetypes LSF2 has no `arch` value for use the snake case of
1111 /// the variant (`mixin_rule`, `use_tree`, `attr_arg`, `template`).
1112 ///
1113 /// # Examples
1114 ///
1115 /// ```
1116 /// use lower_lang::{Arch, Pick};
1117 ///
1118 /// let param: Arch<u8> = Arch::Param { pat: Pick::Nth(0), ty: Pick::Absent, default: Pick::Absent };
1119 /// assert_eq!(param.lsf_name(), "param");
1120 /// assert_eq!(Arch::<u8>::binary().lsf_name(), "binary");
1121 /// ```
1122 #[must_use]
1123 pub const fn lsf_name(&self) -> &'static str {
1124 match self {
1125 Self::Module { .. } => "module",
1126 Self::Pass { .. } => "pass",
1127 Self::Splice { .. } => "splice",
1128 Self::Unsupported => "unsupported",
1129 Self::Let { .. } => "let",
1130 Self::ExprStmt { .. } => "expr_stmt",
1131 Self::Defer { .. } => "defer",
1132 Self::Static { .. } => "static",
1133 Self::GlobalDecl { .. } => "global_decl",
1134 Self::Import { .. } => "import",
1135 Self::Fn { .. } => "fn",
1136 Self::Record { .. } => "record",
1137 Self::Sum { .. } => "sum",
1138 Self::Interface { .. } => "interface",
1139 Self::Class { .. } => "class",
1140 Self::Impl { .. } => "impl",
1141 Self::Alias { .. } => "alias",
1142 Self::AssocType { .. } => "assoc_type",
1143 Self::Const { .. } => "const",
1144 Self::GlobalVar { .. } => "global_var",
1145 Self::MixinUse { .. } => "mixin_use",
1146 Self::MixinRule { .. } => "mixin_rule",
1147 Self::UseTree { .. } => "use_tree",
1148 Self::Interp { .. } => "interpolate",
1149 Self::Attr { .. } => "attribute",
1150 Self::AttrArg { .. } => "attr_arg",
1151 Self::FieldDecl { .. } => "field_decl",
1152 Self::VariantDecl { .. } => "variant_decl",
1153 Self::GenericParam { .. } => "generic_param",
1154 Self::WherePred { .. } => "where_pred",
1155 Self::Param { .. } => "param",
1156 Self::Capture { .. } => "capture",
1157 Self::Path { .. } => "path",
1158 Self::Segment { .. } => "segment",
1159 Self::AssocBinding { .. } => "assoc_binding",
1160 Self::Unary { .. } => "unary",
1161 Self::Binary { .. } => "binary",
1162 Self::Postfix { .. } => "postfix",
1163 Self::Assign { .. } => "assign",
1164 Self::RefAssign { .. } => "ref_assign",
1165 Self::Call { .. } => "call",
1166 Self::Arg { .. } => "arg",
1167 Self::Member { .. } => "member",
1168 Self::MethodCall { .. } => "method_call",
1169 Self::DynMember { .. } => "dyn_member",
1170 Self::Index { .. } => "index",
1171 Self::VarVar { .. } => "var_var",
1172 Self::Append { .. } => "append",
1173 Self::Tuple { .. } => "tuple",
1174 Self::Array { .. } => "array",
1175 Self::Map { .. } => "map",
1176 Self::MapEntry { .. } => "map_entry",
1177 Self::RecordLit { .. } => "record_lit",
1178 Self::FieldInit { .. } => "field_init",
1179 Self::Repeat { .. } => "repeat",
1180 Self::Cast { .. } => "cast",
1181 Self::Borrow { .. } => "borrow",
1182 Self::Deref { .. } => "deref",
1183 Self::Block { .. } => "block",
1184 Self::If { .. } => "if",
1185 Self::Match { .. } => "match",
1186 Self::Arm { .. } => "arm",
1187 Self::Loop { .. } => "loop",
1188 Self::Break { .. } => "break",
1189 Self::Continue { .. } => "continue",
1190 Self::Return { .. } => "return",
1191 Self::Throw { .. } => "throw",
1192 Self::Try { .. } => "try",
1193 Self::Catch { .. } => "catch",
1194 Self::Await { .. } => "await",
1195 Self::Yield { .. } => "yield",
1196 Self::YieldFrom { .. } => "yield_from",
1197 Self::Spawn { .. } => "spawn",
1198 Self::Closure { .. } => "closure",
1199 Self::Template(_) => "template",
1200 Self::WildPat => "wild_pat",
1201 Self::RestPat { .. } => "rest_pat",
1202 Self::BindPat { .. } => "bind_pat",
1203 Self::TuplePat { .. } => "tuple_pat",
1204 Self::CtorPat { .. } => "ctor_pat",
1205 Self::RecordPat { .. } => "record_pat",
1206 Self::FieldPat { .. } => "field_pat",
1207 Self::OrPat { .. } => "or_pat",
1208 Self::RangePat { .. } => "range_pat",
1209 Self::RefPat { .. } => "ref_pat",
1210 Self::SlicePat { .. } => "slice_pat",
1211 Self::TypeTestPat { .. } => "type_test_pat",
1212 Self::TupleTy { .. } => "tuple_ty",
1213 Self::ArrayTy { .. } => "array_ty",
1214 Self::SliceTy { .. } => "slice_ty",
1215 Self::RefTy { .. } => "ref_ty",
1216 Self::PtrTy { .. } => "ptr_ty",
1217 Self::NullableTy { .. } => "nullable_ty",
1218 Self::FnTy { .. } => "fn_ty",
1219 Self::ObjectTy { .. } => "object_ty",
1220 Self::ImplTy { .. } => "impl_ty",
1221 Self::UnionTy { .. } => "union_ty",
1222 Self::IntersectionTy { .. } => "intersection_ty",
1223 }
1224 }
1225}
1226
1227/// Every [`Arch::lsf_name`], for checking the archetype names a mapping is
1228/// given ([`modifier_in`](crate::MappingBuilder::modifier_in)). In variant
1229/// order; the unit test below checks it against every variant.
1230pub(crate) const LSF_NAMES: [&str; 98] = [
1231 "module",
1232 "pass",
1233 "splice",
1234 "unsupported",
1235 "let",
1236 "expr_stmt",
1237 "defer",
1238 "static",
1239 "global_decl",
1240 "import",
1241 "fn",
1242 "record",
1243 "sum",
1244 "interface",
1245 "class",
1246 "impl",
1247 "alias",
1248 "assoc_type",
1249 "const",
1250 "global_var",
1251 "mixin_use",
1252 "mixin_rule",
1253 "use_tree",
1254 "interpolate",
1255 "attribute",
1256 "attr_arg",
1257 "field_decl",
1258 "variant_decl",
1259 "generic_param",
1260 "where_pred",
1261 "param",
1262 "capture",
1263 "path",
1264 "segment",
1265 "assoc_binding",
1266 "unary",
1267 "binary",
1268 "postfix",
1269 "assign",
1270 "ref_assign",
1271 "call",
1272 "arg",
1273 "member",
1274 "method_call",
1275 "dyn_member",
1276 "index",
1277 "var_var",
1278 "append",
1279 "tuple",
1280 "array",
1281 "map",
1282 "map_entry",
1283 "record_lit",
1284 "field_init",
1285 "repeat",
1286 "cast",
1287 "borrow",
1288 "deref",
1289 "block",
1290 "if",
1291 "match",
1292 "arm",
1293 "loop",
1294 "break",
1295 "continue",
1296 "return",
1297 "throw",
1298 "try",
1299 "catch",
1300 "await",
1301 "yield",
1302 "yield_from",
1303 "spawn",
1304 "closure",
1305 "template",
1306 "wild_pat",
1307 "rest_pat",
1308 "bind_pat",
1309 "tuple_pat",
1310 "ctor_pat",
1311 "record_pat",
1312 "field_pat",
1313 "or_pat",
1314 "range_pat",
1315 "ref_pat",
1316 "slice_pat",
1317 "type_test_pat",
1318 "tuple_ty",
1319 "array_ty",
1320 "slice_ty",
1321 "ref_ty",
1322 "ptr_ty",
1323 "nullable_ty",
1324 "fn_ty",
1325 "object_ty",
1326 "impl_ty",
1327 "union_ty",
1328 "intersection_ty",
1329];
1330
1331/// The canonical (`'static`) spelling of an LSF2 archetype name, or `None`
1332/// when no archetype has that name.
1333pub(crate) fn lsf_name_of(name: &str) -> Option<&'static str> {
1334 LSF_NAMES.iter().copied().find(|n| *n == name)
1335}
1336
1337#[cfg(test)]
1338mod tests {
1339 use super::*;
1340 use crate::mapping::Join;
1341 use crate::mapping::NameRef;
1342 use crate::template::TemplateId;
1343
1344 /// One value of every variant, in declaration order.
1345 fn every_variant() -> alloc::vec::Vec<Arch<u8>> {
1346 let a = Pick::Absent;
1347 alloc::vec![
1348 Arch::Module { name: a, items: a },
1349 Arch::Pass { inner: a },
1350 Arch::Splice { items: a },
1351 Arch::Unsupported,
1352 Arch::Let {
1353 pat: a,
1354 ty: a,
1355 init: a,
1356 else_: a,
1357 mutable: false,
1358 hoist: false
1359 },
1360 Arch::ExprStmt { expr: a },
1361 Arch::Defer { body: a },
1362 Arch::Static {
1363 name: a,
1364 ty: a,
1365 init: a
1366 },
1367 Arch::GlobalDecl { names: a },
1368 Arch::Import { path: a, alias: a },
1369 Arch::Fn {
1370 name: a,
1371 generics: a,
1372 params: a,
1373 ret: a,
1374 throws: a,
1375 body: a,
1376 where_: a
1377 },
1378 Arch::Record {
1379 name: a,
1380 generics: a,
1381 fields: a,
1382 shape: RecordShape::Auto
1383 },
1384 Arch::Sum {
1385 name: a,
1386 generics: a,
1387 variants: a
1388 },
1389 Arch::Interface {
1390 name: a,
1391 generics: a,
1392 supers: a,
1393 items: a
1394 },
1395 Arch::Class {
1396 name: a,
1397 generics: a,
1398 bases: a,
1399 interfaces: a,
1400 members: a,
1401 mixin: false
1402 },
1403 Arch::Impl {
1404 generics: a,
1405 interface: a,
1406 self_ty: a,
1407 items: a
1408 },
1409 Arch::Alias {
1410 name: a,
1411 generics: a,
1412 ty: a
1413 },
1414 Arch::AssocType {
1415 name: a,
1416 bounds: a,
1417 default: a
1418 },
1419 Arch::Const {
1420 name: a,
1421 ty: a,
1422 value: a
1423 },
1424 Arch::GlobalVar {
1425 name: a,
1426 ty: a,
1427 init: a,
1428 mutable: false
1429 },
1430 Arch::MixinUse {
1431 mixins: a,
1432 rules: a
1433 },
1434 Arch::MixinRule {
1435 from: a,
1436 method: a,
1437 insteadof: a,
1438 alias: a
1439 },
1440 Arch::UseTree {
1441 path: a,
1442 alias: a,
1443 children: a
1444 },
1445 Arch::Interp {
1446 parts: a,
1447 close: a,
1448 join: Join::Call(NameRef(0))
1449 },
1450 Arch::Attr { name: a, args: a },
1451 Arch::AttrArg { key: a, value: a },
1452 Arch::FieldDecl {
1453 name: a,
1454 ty: a,
1455 default: a
1456 },
1457 Arch::VariantDecl {
1458 name: a,
1459 fields: a,
1460 discriminant: a,
1461 shape: RecordShape::Auto
1462 },
1463 Arch::GenericParam {
1464 name: a,
1465 bounds: a,
1466 ty: a,
1467 default: a
1468 },
1469 Arch::WherePred {
1470 subject: a,
1471 bounds: a
1472 },
1473 Arch::Param {
1474 pat: a,
1475 ty: a,
1476 default: a
1477 },
1478 Arch::Capture { name: a },
1479 Arch::Path {
1480 segments: a,
1481 global: a,
1482 qself: a,
1483 qtrait: a
1484 },
1485 Arch::Segment { name: a, args: a },
1486 Arch::AssocBinding { name: a, ty: a },
1487 Arch::Unary { op: a, operand: a },
1488 Arch::Binary {
1489 lhs: a,
1490 op: a,
1491 rhs: a
1492 },
1493 Arch::Postfix { operand: a, op: a },
1494 Arch::Assign {
1495 target: a,
1496 op: a,
1497 value: a
1498 },
1499 Arch::RefAssign {
1500 target: a,
1501 source: a
1502 },
1503 Arch::Call { callee: a, args: a },
1504 Arch::Arg { name: a, value: a },
1505 Arch::Member {
1506 base: a,
1507 member: a,
1508 args: a,
1509 call: a
1510 },
1511 Arch::MethodCall {
1512 receiver: a,
1513 method: a,
1514 generics: a,
1515 args: a
1516 },
1517 Arch::DynMember {
1518 base: a,
1519 name: a,
1520 args: a,
1521 call: a
1522 },
1523 Arch::Index { base: a, index: a },
1524 Arch::VarVar { inner: a },
1525 Arch::Append { base: a },
1526 Arch::Tuple { elems: a },
1527 Arch::Array { elems: a },
1528 Arch::Map { entries: a },
1529 Arch::MapEntry { key: a, value: a },
1530 Arch::RecordLit {
1531 path: a,
1532 fields: a,
1533 base: a
1534 },
1535 Arch::FieldInit { name: a, value: a },
1536 Arch::Repeat { elem: a, count: a },
1537 Arch::Cast { expr: a, ty: a },
1538 Arch::Borrow { expr: a },
1539 Arch::Deref { inner: a },
1540 Arch::Block {
1541 stmts: a,
1542 tail: a,
1543 label: a,
1544 tail_from_last: false
1545 },
1546 Arch::If {
1547 cond: a,
1548 then: a,
1549 else_: a
1550 },
1551 Arch::Match {
1552 scrutinee: a,
1553 arms: a
1554 },
1555 Arch::Arm {
1556 pat: a,
1557 guard: a,
1558 body: a
1559 },
1560 Arch::Loop { label: a, body: a },
1561 Arch::Break { label: a, value: a },
1562 Arch::Continue { label: a },
1563 Arch::Return { value: a },
1564 Arch::Throw { value: a },
1565 Arch::Try {
1566 body: a,
1567 catches: a,
1568 finally: a
1569 },
1570 Arch::Catch {
1571 types: a,
1572 name: a,
1573 body: a
1574 },
1575 Arch::Await { inner: a },
1576 Arch::Yield { key: a, value: a },
1577 Arch::YieldFrom { inner: a },
1578 Arch::Spawn { inner: a },
1579 Arch::Closure {
1580 params: a,
1581 ret: a,
1582 captures: a,
1583 body: a,
1584 implicit: None
1585 },
1586 Arch::Template(TemplateUse {
1587 template: TemplateId(0),
1588 first: 0,
1589 len: 0
1590 }),
1591 Arch::WildPat,
1592 Arch::RestPat { bind: a },
1593 Arch::BindPat { name: a, sub: a },
1594 Arch::TuplePat { elems: a },
1595 Arch::CtorPat { path: a, elems: a },
1596 Arch::RecordPat { path: a, fields: a },
1597 Arch::FieldPat { name: a, pat: a },
1598 Arch::OrPat { alts: a },
1599 Arch::RangePat { lo: a, hi: a },
1600 Arch::RefPat { inner: a },
1601 Arch::SlicePat { elems: a },
1602 Arch::TypeTestPat { ty: a, pat: a },
1603 Arch::TupleTy { elems: a },
1604 Arch::ArrayTy { elem: a, len: a },
1605 Arch::SliceTy { elem: a },
1606 Arch::RefTy {
1607 inner: a,
1608 region: a
1609 },
1610 Arch::PtrTy { inner: a },
1611 Arch::NullableTy { inner: a },
1612 Arch::FnTy { params: a, ret: a },
1613 Arch::ObjectTy { bounds: a },
1614 Arch::ImplTy { bounds: a },
1615 Arch::UnionTy { members: a },
1616 Arch::IntersectionTy { members: a },
1617 ]
1618 }
1619
1620 #[test]
1621 fn test_lsf_names_cover_every_variant_once() {
1622 let all = every_variant();
1623 assert_eq!(all.len(), LSF_NAMES.len());
1624 for (arch, name) in all.iter().zip(LSF_NAMES) {
1625 assert_eq!(arch.lsf_name(), name);
1626 assert_eq!(lsf_name_of(name), Some(name));
1627 }
1628 let mut sorted = LSF_NAMES;
1629 sorted.sort_unstable();
1630 assert!(sorted.windows(2).all(|w| w[0] != w[1]), "names are unique");
1631 assert_eq!(lsf_name_of("parameter"), None);
1632 assert_eq!(lsf_name_of(""), None);
1633 }
1634}