This phase rewrites calls to array constructors to newArray method in Dotty.runtime.Arrays module.
This phase augments Scala2 traits with implementation classes and with additional members needed for mixin composition.
This phase augments Scala2 traits with implementation classes and with additional members needed for mixin composition. These symbols would have been added between Unpickling and Mixin in the Scala2 pipeline. Specifcally, it adds
Furthermore, it expands the names of all private getters and setters as well as super accessors in the trait and makes them not-private.
A no-op transform that checks whether the compiled sources are re-entrant.
A no-op transform that checks whether the compiled sources are re-entrant. If -Ycheck:reentrant is set, the phase makes sure that there are no variables that are accessible from a global object. It excludes from checking paths that are labeled with one of the annotations
A transformer that check that requirements of Static fields\methods are implemented:
A transformer that check that requirements of Static fields\methods are implemented:
@static
2. The fields annotated with @static
should preceed any non-@static
fields.
This ensures that we do not introduce surprises for users in initialization order.
3. If a member foo
of an object C
is annotated with @static
,
the companion class C
is not allowed to define term members with name foo
.
4. If a member foo
of an object C
is annotated with @static
, the companion class C
is not allowed to inherit classes that define a term member with name foo
.
5. Only @static
methods and vals are supported in companions of traits.
Java8 supports those, but not vars, and JavaScript does not have interfaces at all.
6. @static
Lazy vals are currently unsupported.
Rewrite classOf
calls as follow:
Rewrite classOf
calls as follow:
For every primitive class C whose boxed class is called B: classOf[C] -> B.TYPE For every non-primitive class D: classOf[D] -> Literal(Constant(erasure(D)))
This transform
This transform
This transform makes sure that all private member selections from AndTypes are performed from the first component of AndType.
This transform makes sure that all private member selections from
AndTypes are performed from the first component of AndType.
This is needed for correctness of erasure. See tests/run/PrivateAnd.scala
Utility class for lazy values whose evaluation depends on a context.
Utility class for lazy values whose evaluation depends on a context. This should be used whenever the evaluation of a lazy expression depends on some context, but the value can be re-used afterwards with a different context.
A typical use case is a lazy val in a phase object which exists once per root context where the expression intiializing the lazy val depends only on the root context, but not any changes afterwards.
Drop Inlined nodes
This phase eliminates ExprTypes => T
as types of function parameters, and replaces them by
nullary function types.
This phase eliminates ExprTypes => T
as types of function parameters, and replaces them by
nullary function types. More precisely:
For the types of parameter symbols:
Note that => T
types are not eliminated in MethodTypes. This is done later at erasure.
Terms are rewritten as follows:
x ==> x.apply() if x is a parameter that had type => T
Arguments to call-by-name parameters are translated as follows. First, the argument is rewritten by the rules
e.apply() ==> e if e.apply() is an argument to a call-by-name parameter expr ==> () => expr if other expr is an argument to a call-by-name parameter
This makes the argument compatible with a parameter type of () => T, which will be the formal parameter type at erasure. But to be -Ycheckable until then, any argument ARG rewritten by the rules above is again wrapped in an application DummyApply(ARG) where
DummyApply: [T](() => T): T
is a synthetic method defined in Definitions. Erasure will later strip these DummyApply wrappers.
Note: This scheme to have inconsistent types between method types (whose formal types are still ExprTypes and parameter valdefs (which are now FunctionTypes) is not pretty. There are two other options which have been abandoned or not yet pursued.
Option 1: Transform => T to () => T also in method and function types. The problem with this is that is that it requires to look at every type, and this forces too much, causing Cyclic Reference errors. Abandoned for this reason.
Option 2: Merge ElimByName with erasure, or have it run immediately before. This has not been tried yet.
This phase erases ErasedValueType to their underlying type.
This phase erases ErasedValueType to their underlying type. It also removes the synthetic cast methods u2evt$ and evt2u$ which are no longer needed afterwards.
A transformer that removes repeated parameters (T*) from all types, replacing them with Seq types.
Replace This references to module classes in static methods by global identifiers to the corresponding modules.
Make private term members that are accessed from another class non-private by resetting the Private flag and expanding their name.
Make private term members that are accessed from another class non-private by resetting the Private flag and expanding their name.
Make private accessor in value class not-private. Ihis is necessary to unbox the value class when accessing it from separate compilation units
Also, make non-private any private parameter forwarders that forward to an inherited public or protected parameter accessor with the same name as the forwarder. This is necessary since private methods are not allowed to have the same name as inherited public ones.
See discussion in https://github.com/lampepfl/dotty/pull/784 and https://github.com/lampepfl/dotty/issues/783
Expand SAM closures that cannot be represented by the JVM as lambdas to anonymous classes.
Expand SAM closures that cannot be represented by the JVM as lambdas to anonymous classes. These fall into five categories
This phase adds outer accessors to classes and traits that need them.
This phase adds outer accessors to classes and traits that need them. Compared to Scala 2.x, it tries to minimize the set of classes that take outer accessors by scanning class implementations for outer references.
The following things are delayed until erasure and are performed by class OuterOps:
replacement of outer this by outer paths is done in Erasure. needs to run after pattern matcher as it can add outer checks and force creation of $outer
Transform references of the form
Transform references of the form
C.this.m
where C
is a class with explicit self type and C
is not a
subclass of the owner of m
to
C.this.asInstanceOf[S & C.this.type].m
where S
is the self type of C
.
See run/i789.scala for a test case why this is needed.
Also replaces idents referring to the self type with ThisTypes.
Perform Step 1 in the inline classes SIP: Creates extension methods for all methods in a value class, except parameter or super accessors, or constructors.
Perform Step 1 in the inline classes SIP: Creates extension methods for all methods in a value class, except parameter or super accessors, or constructors.
Additionally, for a value class V, let U be the underlying type after erasure. We add
to the companion module of V two cast methods:
def u2evt$(x0: U): ErasedValueType(V, U)
def evt2u$(x0: ErasedValueType(V, U)): U
The casts are used in Erasure to make it typecheck, they are then removed
in ElimErasedValueType.
This is different from the implementation of value classes in Scala 2
(see SIP-15) which uses asInstanceOf
which does not typecheck.
Finally, if the constructor of a value class is private pr protected it is widened to public.
The first tree transform
The first tree transform
Lift nested classes to toplevel
Provides methods to produce fully parameterized versions of instance methods,
where the this
of the enclosing class is abstracted out in an extra leading
$this
parameter and type parameters of the class become additional type
parameters of the fully parameterized method.
Provides methods to produce fully parameterized versions of instance methods,
where the this
of the enclosing class is abstracted out in an extra leading
$this
parameter and type parameters of the class become additional type
parameters of the fully parameterized method.
Example usage scenarios are:
Note that the methods lift out type parameters of the class containing the instance method, but not type parameters of enclosing classes. The fully instantiated method therefore needs to be put in a scope "close" to the original method, i.e. they need to share the same outer pointer. Examples of legal positions are: in the companion object, or as a local method inside the original method.
Note: The scheme does not handle yet methods where type parameter bounds depend on value parameters of the enclosing class, as in:
class C(val a: String) extends AnyVal { def foo[U <: a.type]: Unit = ... }
The expansion of method foo
would lead to
def foo$extension[U <: $this.a.type]($this: C): Unit = ...
which is not typable. Not clear yet what to do. Maybe allow PolyTypes to follow method parameters and translate to the following:
def foo$extension($this: C)[U <: $this.a.type]: Unit = ...
class-dependent-extension-method.scala in pending/pos.
Rewires closures to implement more specific types of Functions.
Rewrite getClass
calls as follow:
Rewrite getClass
calls as follow:
For every instance of primitive class C whose boxed class is called B: instanceC.getClass -> B.TYPE For every instance of non-primitive class D: instanceD.getClass -> instanceD.getClass
Performs the following rewritings for fields of a class:
Performs the following rewritings for fields of a class:
<mods> val x: T = e --> <mods> <stable> <accessor> def x: T = e <mods> var x: T = e --> <mods> <accessor> def x: T = e
<mods> val x: T --> <mods> <stable> <accessor> def x: T
<mods> lazy val x: T = e --> <mods> <accessor> lazy def x: T =e
<mods> var x: T --> <mods> <accessor> def x: T
<mods> non-static <module> val x$ = e --> <mods> <module> <accessor> def x$ = e
Omitted from the rewritings are
Furthermore, assignments to mutable vars are replaced by setter calls
p.x = e --> p.x_=(e)
No fields are generated yet. This is done later in phase Memoize.
Replace member references as follows:
Replace member references as follows:
- x != y
for != in class Any becomes !(x == y)
with == in class Any.
- x.##
for ## in NullClass becomes 0
- x.##
for ## in Any becomes calls to ScalaRunTime.hash,
using the most precise overload available
- x.getClass
for getClass in primitives becomes x.getClass
with getClass in class Object.
Implements partial evaluation of sc.isInstanceOf[Sel]
according to:
Implements partial evaluation of sc.isInstanceOf[Sel]
according to:
+-------------+----------------------------+----------------------------+------------------+ | Sel\sc | trait | class | final class | +-------------+----------------------------+----------------------------+------------------+ | trait | ? | ? | statically known | | class | ? | false if classes unrelated | statically known | | final class | false if classes unrelated | false if classes unrelated | statically known | +-------------+----------------------------+----------------------------+------------------+
This is a generalized solution to raising an error on unreachable match
cases and warnings on other statically known results of isInstanceOf
.
Steps taken:
Any
b) handleStaticallyKnown
c) falseIfUnrelated with scrutinee <:< selector
d) handleFalseUnrelated
e) leave as is (aka happens
)
3. Rewrite according to step taken in 2
This phase performs the necessary rewritings to eliminate classes and methods nested in other methods.
This phase performs the necessary rewritings to eliminate classes and methods nested in other methods. In detail:
A particularly tricky case are local traits. These cannot store free variables as field proxies, because LambdaLift runs after Mixin, so the fields cannot be expanded anymore. Instead, methods of local traits get free variables of the trait as additional proxy parameters. The difference between local classes and local traits is illustrated by the two rewritings below.
def f(x: Int) = { def f(x: Int) = new C(x).f2 class C { ==> class C(x$1: Int) { def f2 = x def f2 = x$1 } } new C().f2 }
def f(x: Int) = { def f(x: Int) = new C().f2(x) trait T { ==> trait T def f2 = x def f2(x$1: Int) = x$1 } } class C extends T class C extends T new C().f2 }
Lifts try's that might be executed on non-empty expression stacks to their own methods.
Lifts try's that might be executed on non-empty expression stacks to their own methods. I.e.
try body catch handler
is lifted to
{ def liftedTree$n() = try body catch handler; liftedTree$n() }
Rewrite calls
Rewrite calls
super[M].f(args)
where M is a Scala2 trait implemented by the current class to
M$class.f(this, args)
provided the implementation class M$class defines a corresponding function f
.
A base class for transforms.
A base class for transforms. A transform contains a compiler phase which applies a tree transformer.
Provides the implementations of all getters and setters, introducing fields to hold the value accessed by them.
Provides the implementations of all getters and setters, introducing fields to hold the value accessed by them. TODO: Make LazyVals a part of this phase?
<accessor> <stable> <mods> def x(): T = e --> private val x: T = e <accessor> <stable> <mods> def x(): T = x
<accessor> <mods> def x(): T = e --> private var x: T = e <accessor> <mods> def x(): T = x
<accessor> <mods> def x_=(y: T): Unit = () --> <accessor> <mods> def x_=(y: T): Unit = x = y
This phase performs the following transformations:
This phase performs the following transformations:
traitDefs
and transformSym
) Map every concrete trait getter<mods> def x(): T = expr
to the pair of definitions:
<mods> def x(): T protected def initial$x(): T = { stats; expr }
where stats
comprises all statements between either the start of the trait
or the previous field definition which are not definitions (i.e. are executed for
their side effects).
2. (done in traitDefs
) Make every concrete trait setter
<mods> def x_=(y: T) = ()
deferred by mapping it to
<mods> def x_=(y: T)
3. For a non-trait class C:
For every trait M directly implemented by the class (see SymUtils.mixin), in reverse linearization order, add the following definitions to C:
3.1 (done in traitInits
) For every parameter accessor <mods> def x(): T
in M,
in order of textual occurrence, add
<mods> def x() = e
where e
is the constructor argument in C that corresponds to x
. Issue
an error if no such argument exists.
3.2 (done in traitInits
) For every concrete trait getter <mods> def x(): T
in M
which is not a parameter accessor, in order of textual occurrence, produce the following:
3.2.1 If x
is also a member of C
, and M is a Dotty trait:
<mods> def x(): T = super[M].initial$x()
3.2.2 If x
is also a member of C
, and M is a Scala 2.x trait:
<mods> def x(): T = _
3.2.3 If x
is not a member of C
, and M is a Dotty trait:
super[M].initial$x()
3.2.4 If x
is not a member of C
, and M is a Scala2.x trait, nothing gets added.
3.3 (done in superCallOpt
) The call:
super[M].<init>
3.4 (done in setters
) For every concrete setter <mods> def x_=(y: T)
in M:
<mods> def x_=(y: T) = ()
4. (done in transformTemplate
and transformSym
) Drop all parameters from trait
constructors.
5. (done in transformSym
) Drop ParamAccessor flag from all parameter accessors in traits.
Conceptually, this is the second half of the previous mixin phase. It needs to run after erasure because it copies references to possibly private inner classes and objects into enclosing classes where they are not visible. This can only be done if all references are symbolic.
Move static methods from companion to the class itself
Implement non-local returns using NonLocalReturnControl exceptions.
1.
1. Widens all private[this] and protected[this] qualifiers to just private/protected 2. Sets PureInterface flag for traits that only have pure interface members and that do not have initialization code. A pure interface member is either an abstract or alias type definition or a deferred val or def.
For all parameter accessors
For all parameter accessors
val x: T = ...
if
(1) x is forwarded in the supercall to a parameter that's also named x
(2) the superclass parameter accessor for x
is accessible from the current class
change the accessor to
def x: T = super.x.asInstanceOf[T]
Do the same also if there are intermediate inaccessible parameter accessor forwarders. The aim of this transformation is to avoid redundant parameter accessor fields.
This transform eliminates patterns.
This transform eliminates patterns. Right now it's a dummy. Awaiting the real pattern matcher. elimRepeated is required TODO: outer tests are not generated yet.
This phase pickles trees
A macro transform that runs immediately after typer and that performs the following functions:
A macro transform that runs immediately after typer and that performs the following functions:
(1) Add super accessors and protected accessors (@see SuperAccessors)
(2) Convert parameter fields that have the same name as a corresponding public parameter field in a superclass to a forwarder to the superclass field (corresponding = super class field is initialized with subclass field) (@see ForwardParamAccessors)
(3) Add synthetic methods (@see SyntheticMethods)
(4) Check that New
nodes can be instantiated, and that annotations are valid
(5) Convert all trees representing types to TypeTrees.
(6) Check the bounds of AppliedTypeTrees
(7) Insert .package
for selections of package object members
(8) Replaces self references by name with this
(9) Adds SourceFile annotations to all top-level classes and objects
(10) Adds Child annotations to all sealed classes
(11) Minimizes call
fields of Inline
nodes to just point to the toplevel
class from which code was inlined.
The reason for making this a macro transform is that some functions (in particular super and protected accessors and instantiation checks) are naturally top-down and don't lend themselves to the bottom-up approach of a mini phase. The other two functions (forwarding param accessors and synthetic methods) only apply to templates and fit mini-phase or subfunction of a macro phase equally well. But taken by themselves they do not warrant their own group of miniphases before pickling.
This phase adds super accessors and method overrides where linearization differs from Java's rule for default methods in interfaces.
This phase adds super accessors and method overrides where linearization differs from Java's rule for default methods in interfaces. In particular:
For every trait M directly implemented by the class (see SymUtils.mixin), in reverse linearization order, add the following definitions to C:
3.1 (done in superAccessors
) For every superAccessor
<mods> def super$f[Ts](ps1)...(psN): U
in M:
<mods> def super$f[Ts](ps1)...(psN): U = super[S].f[Ts](ps1)...(psN)
where S
is the superclass of M
in the linearization of C
.
3.2 (done in methodOverrides
) For every method
<mods> def f[Ts](ps1)...(psN): U
in M that needs to be disambiguated:
<mods> def f[Ts](ps1)...(psN): U = super[M].f[Ts](ps1)...(psN)
A method in M needs to be disambiguated if it is concrete, not overridden in C, and if it overrides another concrete method.
This is the first part of what was the mixin phase. It is complemented by Mixin, which runs after erasure.
The preceding lambda lift and flatten phases move symbols to different scopes and rename them.
The preceding lambda lift and flatten phases move symbols to different scopes and rename them. This miniphase cleans up afterwards and makes sure that all class scopes contain the symbols defined in them.
Removes selects that would be compiled into GetStatic otherwise backend needs to be aware that some qualifiers need to be dropped.
Removes selects that would be compiled into GetStatic otherwise backend needs to be aware that some qualifiers need to be dropped. Similar transformation seems to be performed by flatten in nsc
A transformer that eliminates SeqLiteral's, transforming SeqLiteral(elems)
to an operation
equivalent to
A transformer that eliminates SeqLiteral's, transforming SeqLiteral(elems)
to an operation
equivalent to
JavaSeqLiteral(elems).toSeq
Instead of toSeq
, which takes an implicit, the appropriate "wrapArray" method
is called directly. The reason for this step is that JavaSeqLiterals, being arrays
keep a precise type after erasure, whereas SeqLiterals only get the erased type Seq
,
Distribute applications into Block and If nodes
This class performs the following functions:
This class performs the following functions:
(1) Adds super accessors for all super calls that either appear in a trait or have as a target a member of some outer class.
(2) Adds protected accessors if the access to the protected member happens in a class which is not a subclass of the member's owner.
It also checks that:
(1) Symbols accessed from super are not abstract, or are overridden by an abstract override.
(2) If a symbol accessed from super is defined in a real class (not a trait), there are no abstract members which override this member in Java's rules (see SI-4989; such an access would lead to illegal bytecode)
(3) Super calls do not go to some synthetic members of Any (see isDisallowed)
(4) Super calls do not go to synthetic field accessors
A decorator that provides methods on symbols that are needed in the transformer pipeline.
Synthetic method implementations for case classes, case objects, and value classes.
Synthetic method implementations for case classes, case objects, and value classes. Selectively added to case classes/objects, unless a non-default implementation already exists: def equals(other: Any): Boolean def hashCode(): Int def canEqual(other: Any): Boolean def toString(): String def productArity: Int def productPrefix: String Special handling: protected def readResolve(): AnyRef
Selectively added to value classes, unless a non-default implementation already exists:
def equals(other: Any): Boolean def hashCode(): Int
A Tail Rec Transformer
A Tail Rec Transformer
1.1
What it does:
Finds method calls in tail-position and replaces them with jumps. A call is in a tail-position if it is the last instruction to be executed in the body of a method. This is done by recursing over the trees that may contain calls in tail-position (trees that can't contain such calls are not transformed). However, they are not that many.
Self-recursive calls in tail-position are replaced by jumps to a label at the beginning of the method. As the JVM provides no way to jump from a method to another one, non-recursive calls in tail-position are not optimized.
A method call is self-recursive if it calls the current method and the method is final (otherwise, it could be a call to an overridden method in a subclass).
Recursive calls on a different instance are optimized. Since 'this' is not a local variable it s added as a label parameter.
This phase has been moved before pattern matching to catch more of the common cases of tail recursive functions. This means that more cases should be taken into account (like nested function, and pattern cases).
If a method contains self-recursive calls, a label is added to at the beginning of its body and the calls are replaced by jumps to that label.
In scalac, If the method had type parameters, the call must contain same parameters as type arguments. This is no longer case in dotc. In scalac, this is named tailCall but it does only provide optimization for self recursive functions, that's why it's renamed to tailrec
Run by -Ycheck option after a given phase, this class retypes all syntax trees and verifies that the type of each tree node so obtained conforms to the type found in the tree node.
Run by -Ycheck option after a given phase, this class retypes all syntax trees and verifies that the type of each tree node so obtained conforms to the type found in the tree node. It also performs the following checks:
Compiles the cases that can not be handled by primitive catch cases as a common pattern match.
Compiles the cases that can not be handled by primitive catch cases as a common pattern match.
The following code:
try {
}
catch {
}
catch {
This transform normalizes type tests and type casts, also replacing type tests with singleton argument type with reference equality check Any remaining type tests
This transform normalizes type tests and type casts, also replacing type tests with singleton argument type with reference equality check Any remaining type tests
Unfortunately this phase ended up being not Y-checkable unless types are erased. A cast to an ConstantType(3) or x.type cannot be rewritten before erasure.
A decorator that provides methods on types that are needed in the transformer pipeline.
This phase elides unnecessary value class allocations
This phase elides unnecessary value class allocations
For a value class V defined as: class V(val underlying: U) extends AnyVal we avoid unnecessary allocations: new V(u1) == new V(u2) => u1 == u2 (new V(u)).underlying() => u
This phase inlines calls to methods of value classes.
This phase inlines calls to methods of value classes.
A value class V after ExtensionMethods will look like: class V[A, B, ...](val underlying: U) extends AnyVal { def foo[T, S, ...](arg1: A1, arg2: A2, ...) = V.foo$extension[T, S, ..., A, B, ...](this)(arg1, arg2, ...)
... }
Let e have type V, if e is a stable prefix or if V does not have any class type parameter, then we can rewrite: e.foo[X, Y, ...](args) as: V.foo$extension[X, Y, ..., e.A, e.B, ...](e)(args) Otherwise, we need to evaluate e first: { val ev = e V.foo$extension[X, Y, ..., ev.A, ev.B, ...](ev)(args) }
This phase needs to be placed after phases which may introduce calls to value class methods (like PatternMatcher). This phase uses name mangling to find the correct extension method corresponding to a value class method (see ExtensionMethods.extensionMethod), therefore we choose to place it before phases which may perform their own name mangling on value class methods (like TypeSpecializer), this way VCInlineMethods does not need to have any knowledge of the name mangling done by other phases.
A module that can produce a kind of iterator (Cursor
),
which yields all pairs of overriding/overridden symbols
that are visible in some baseclass, unless there's a parent class
that already contains the same pairs.
A module that can produce a kind of iterator (Cursor
),
which yields all pairs of overriding/overridden symbols
that are visible in some baseclass, unless there's a parent class
that already contains the same pairs.
Adapted from the 2.9 version of OverridingPairs. The 2.10 version is IMO way too unwieldy to be maintained.
Methods that apply to user-defined value classes
This phase rewrites calls to array constructors to newArray method in Dotty.runtime.Arrays module.
It assummes that generic arrays have already been handled by typer(see Applications.convertNewGenericArray). Additionally it optimizes calls to scala.Array.ofDim functions by replacing them with calls to newArray with specific dimensions