Factory methods used by TreeMakers to make the actual trees.
Factory methods used by TreeMakers to make the actual trees.
We have two modes in which to emit trees: optimized (the default) and pure (aka "virtualized": match is parametric in its monad).
Segregating this super hacky CPS code.
Optimize and analyze matches based on their TreeMaker-representation.
Optimize and analyze matches based on their TreeMaker-representation.
The patmat translation doesn't rely on this, so it could be disabled in principle.
Translate typed Trees that represent pattern matches into the patternmatching IR, defined by TreeMakers.
Translate our IR (TreeMakers) into actual Scala Trees using the factory methods in MatchCodeGen.
Translate our IR (TreeMakers) into actual Scala Trees using the factory methods in MatchCodeGen.
The IR is mostly concerned with sequencing, substitution, and rendering all necessary conditions, mostly agnostic to whether we're in optimized/pure (virtualized) mode.
An extractor returns: F1, F2, ..., Fi, opt[Seq[E] or E*] A case matches: P1, P2, ..., Pj, opt[Seq[E]] Put together: P1/F1, P2/F2, ...
An extractor returns: F1, F2, ..., Fi, opt[Seq[E] or E*] A case matches: P1, P2, ..., Pj, opt[Seq[E]] Put together: P1/F1, P2/F2, ... Pi/Fi, Pi+1/E, Pi+2/E, ... Pj/E, opt[Seq[E]]
Here Pm/Fi is the last pattern to match the fixed arity section.
productArity: the value of i, i.e. the number of non-sequence types in the extractor nonStarArity: the value of j, i.e. the number of non-star patterns in the case definition elementArity: j - i, i.e. the number of non-star patterns which must match sequence elements starArity: 1 or 0 based on whether there is a star (sequence-absorbing) pattern totalArity: nonStarArity + starArity, i.e. the number of patterns in the case definition
Note that productArity is a function only of the extractor, and nonStar/star/totalArity are all functions of the patterns. The key value for aligning and typing the patterns is elementArity, as it is derived from both sets of information.
Translate pattern matching.
Translate pattern matching.
Either into optimized if/then/else's, or virtualized as method calls (these methods form a zero-plus monad), similar in spirit to how for-comprehensions are compiled.
For each case, express all patterns as extractor calls, guards as 0-ary extractors, and sequence them using flatMap
(lifting the body of the case into the monad using one
).
Cases are combined into a pattern match using the orElse
combinator (the implicit failure case is expressed using the monad's zero
).
TODO:
(longer-term) TODO:
This is scalac-specific logic layered on top of the scalac-agnostic "matching products to patterns" logic defined in PatternExpander.
Solve pattern matcher exhaustivity problem via DPLL.