Translator

dotty.tools.dotc.transform.PatternMatcher.Translator
class Translator(resultType: Type, thisPhase: MiniPhase)(using x$3: Context)

The pattern matching translator. Its general structure is a pipeline:

Match tree ---matchPlan---> Plan ---optimize---> Plan ---emit---> Tree

The pipeline consists of three steps:

  • build a plan, using methods matchPlan, caseDefPlan, patternPlan.
  • optimize the plan, using methods listed in optimization,
  • emit the translated tree, using methods emit, collectSwitchCases, emitSwitchCases, and emitCondition.

A plan represents the underlying decision graph. It consists of tests, let bindings, labeled blocks, return from said labeled blocks and code blocks. It's represented by its own data type. Plans are optimized by merging common tests and eliminating dead code.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Type members

Classlikes

case class EqualTest(tree: Tree) extends Test

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
class Test
class Object
trait Matchable
class Any
Show all
case object GuardTest extends Test

Attributes

Supertypes
trait Singleton
trait Product
trait Mirror
trait Serializable
trait Product
trait Equals
class Test
class Object
trait Matchable
class Any
Show all
Self type
GuardTest.type
case class LabeledPlan(sym: TermSymbol, var expr: Plan) extends Plan

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
class Plan
class Object
trait Matchable
class Any
Show all
case class LengthTest(len: Int, exact: Boolean) extends Test

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
class Test
class Object
trait Matchable
class Any
Show all
case class LetPlan(sym: TermSymbol, var body: Plan) extends Plan

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
class Plan
class Object
trait Matchable
class Any
Show all
case object NonEmptyTest extends Test

Attributes

Supertypes
trait Singleton
trait Product
trait Mirror
trait Serializable
trait Product
trait Equals
class Test
class Object
trait Matchable
class Any
Show all
Self type
case object NonNullTest extends Test

Attributes

Supertypes
trait Singleton
trait Product
trait Mirror
trait Serializable
trait Product
trait Equals
class Test
class Object
trait Matchable
class Any
Show all
Self type
sealed abstract class Plan

The different kinds of plans

The different kinds of plans

Attributes

Supertypes
class Object
trait Matchable
class Any
Known subtypes
class LabeledPlan
class LetPlan
class ResultPlan
class ReturnPlan
class SeqPlan
class TestPlan
Show all
class PlanTransform extends Plan => Plan

A superclass for plan transforms

A superclass for plan transforms

Attributes

Supertypes
trait Plan => Plan
class Object
trait Matchable
class Any
case class ResultPlan(var tree: Tree) extends Plan

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
class Plan
class Object
trait Matchable
class Any
Show all
case class ReturnPlan(var label: TermSymbol) extends Plan

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
class Plan
class Object
trait Matchable
class Any
Show all
case class SeqPlan(var head: Plan, var tail: Plan) extends Plan

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
class Plan
class Object
trait Matchable
class Any
Show all
sealed abstract class Test

The different kinds of tests

The different kinds of tests

Attributes

Supertypes
class Object
trait Matchable
class Any
Known subtypes
class EqualTest
object GuardTest
class LengthTest
object NonEmptyTest
object NonNullTest
class TypeTest
Show all
case class TestPlan(test: Test, var scrutinee: Tree, span: Span, var onSuccess: Plan) extends Plan

Attributes

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
class Plan
class Object
trait Matchable
class Any
Show all
object TestPlan

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
TestPlan.type
case class TypeTest(tpt: Tree, trusted: Boolean) extends Test

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
class Test
class Object
trait Matchable
class Any
Show all

Value members

Concrete methods

def mergeTests(plan: Plan): Plan

Merge identical consecutive tests.

Merge identical consecutive tests.

When we have the following shape:

if (testA) plan1 if (testA) plan2 nextPlan?

transform it to

if (testA) { plan1 plan2 } nextPlan?

Similarly, when we have equivalent let bindings:

let x1 = rhs1 in plan1 let x2 = rhs2 in plan2 nextPlan?

and rhs1 and rhs2 are equivalent, transform it to

let x1 = rhs1 in { plan1 plan2[x1/x2] }

where plan2[x1/x2] means substituting x1 for x2 in plan2.

There are some tricks to "ignore" non-patmat-generated let bindings, i.e., captures written in the source code, while identifying common subplans.

Attributes

def show(plan: Plan): String

Pretty-print plan; used for debugging

Pretty-print plan; used for debugging

Attributes

def translateMatch(tree: Match): Tree

Translate pattern match to sequence of tests.

Translate pattern match to sequence of tests.

Attributes

Concrete fields