@@ -407,12 +407,12 @@ object Trees {
407
407
}
408
408
409
409
/** A ValDef or DefDef tree */
410
- abstract class ValOrDefDef [+ T <: Untyped ](implicit @ constructorOnly src : SourceFile ) extends MemberDef [T ] with WithLazyField [ Tree [ T ]] {
410
+ abstract class ValOrDefDef [+ T <: Untyped ](implicit @ constructorOnly src : SourceFile ) extends MemberDef [T ], WithLazyFields {
411
411
type ThisTree [+ T <: Untyped ] <: ValOrDefDef [T ]
412
412
def name : TermName
413
413
def tpt : Tree [T ]
414
- def unforcedRhs : LazyTree [T ] = unforced
415
- def rhs (using Context ): Tree [T ] = forceIfLazy
414
+ def unforcedRhs : LazyTree [T ]
415
+ def rhs (using Context ): Tree [T ]
416
416
}
417
417
418
418
trait ValOrTypeDef [+ T <: Untyped ] extends MemberDef [T ]:
@@ -808,8 +808,10 @@ object Trees {
808
808
extends ValOrDefDef [T ], ValOrTypeDef [T ] {
809
809
type ThisTree [+ T <: Untyped ] = ValDef [T ]
810
810
assert(isEmpty || (tpt ne genericEmptyTree))
811
- def unforced : LazyTree [T ] = preRhs
812
- protected def force (x : Tree [T @ uncheckedVariance]): Unit = preRhs = x
811
+
812
+ def unforcedRhs : LazyTree [T ] = preRhs
813
+ def forceFields ()(using Context ): Unit = preRhs = force(preRhs)
814
+ def rhs (using Context ): Tree [T ] = { forceFields(); preRhs.asInstanceOf [Tree [T ]] }
813
815
}
814
816
815
817
/** mods def name[tparams](vparams_1)...(vparams_n): tpt = rhs */
@@ -818,8 +820,10 @@ object Trees {
818
820
extends ValOrDefDef [T ] {
819
821
type ThisTree [+ T <: Untyped ] = DefDef [T ]
820
822
assert(tpt ne genericEmptyTree)
821
- def unforced : LazyTree [T ] = preRhs
822
- protected def force (x : Tree [T @ uncheckedVariance]): Unit = preRhs = x
823
+
824
+ def unforcedRhs : LazyTree [T ] = preRhs
825
+ def forceFields ()(using Context ): Unit = preRhs = force(preRhs)
826
+ def rhs (using Context ): Tree [T ] = { forceFields(); preRhs.asInstanceOf [Tree [T ]] }
823
827
824
828
def leadingTypeParams (using Context ): List [TypeDef [T ]] = paramss match
825
829
case (tparams @ (tparam : TypeDef [_]) :: _) :: _ => tparams.asInstanceOf [List [TypeDef [T ]]]
@@ -855,16 +859,20 @@ object Trees {
855
859
* if this is of class untpd.DerivingTemplate.
856
860
* Typed templates only have parents.
857
861
*/
858
- case class Template [+ T <: Untyped ] private [ast] (constr : DefDef [T ], parentsOrDerived : List [ Tree [ T ] ], self : ValDef [T ], private var preBody : LazyTreeList [T ])(implicit @ constructorOnly src : SourceFile )
859
- extends DefTree [T ] with WithLazyField [ List [ Tree [ T ]]] {
862
+ case class Template [+ T <: Untyped ] private [ast] (constr : DefDef [T ], private var preParentsOrDerived : LazyTreeList [ T ], self : ValDef [T ], private var preBody : LazyTreeList [T ])(implicit @ constructorOnly src : SourceFile )
863
+ extends DefTree [T ] with WithLazyFields {
860
864
type ThisTree [+ T <: Untyped ] = Template [T ]
861
- def unforcedBody : LazyTreeList [T ] = unforced
862
- def unforced : LazyTreeList [T ] = preBody
863
- protected def force (x : List [Tree [T @ uncheckedVariance]]): Unit = preBody = x
864
- def body (using Context ): List [Tree [T ]] = forceIfLazy
865
865
866
- def parents : List [Tree [T ]] = parentsOrDerived // overridden by DerivingTemplate
867
- def derived : List [untpd.Tree ] = Nil // overridden by DerivingTemplate
866
+ def forceFields ()(using Context ): Unit =
867
+ preParentsOrDerived = force(preParentsOrDerived)
868
+ preBody = force(preBody)
869
+
870
+ def unforcedBody : LazyTreeList [T ] = preBody
871
+ def body (using Context ): List [Tree [T ]] = { forceFields(); preBody.asInstanceOf [List [Tree [T ]]] }
872
+ def parentsOrDerived (using Context ): List [Tree [T ]] = { forceFields(); preParentsOrDerived.asInstanceOf [List [Tree [T ]]] }
873
+
874
+ def parents (using Context ): List [Tree [T ]] = parentsOrDerived // overridden by DerivingTemplate
875
+ def derived : List [untpd.Tree ] = Nil // overridden by DerivingTemplate
868
876
}
869
877
870
878
@@ -1008,30 +1016,27 @@ object Trees {
1008
1016
1009
1017
// ----- Lazy trees and tree sequences
1010
1018
1011
- /** A tree that can have a lazy field
1012
- * The field is represented by some private `var` which is
1013
- * accessed by `unforced` and `force`. Forcing the field will
1014
- * set the `var` to the underlying value.
1015
- */
1016
- trait WithLazyField [+ T <: AnyRef ] {
1017
- def unforced : T | Lazy [T ]
1018
- protected def force (x : T @ uncheckedVariance): Unit
1019
- def forceIfLazy (using Context ): T = unforced match {
1020
- case lzy : Lazy [T @ unchecked] =>
1021
- val x = lzy.complete
1022
- force(x)
1023
- x
1024
- case x : T @ unchecked => x
1025
- }
1026
- }
1027
-
1028
1019
/** A base trait for lazy tree fields.
1029
1020
* These can be instantiated with Lazy instances which
1030
1021
* can delay tree construction until the field is first demanded.
1031
1022
*/
1032
- trait Lazy [+ T <: AnyRef ] {
1023
+ trait Lazy [+ T <: AnyRef ]:
1033
1024
def complete (using Context ): T
1034
- }
1025
+
1026
+ /** A tree that can have a lazy fields.
1027
+ * Such fields are variables of type `T | Lazy[T]`, for some tyope `T`.
1028
+ */
1029
+ trait WithLazyFields :
1030
+
1031
+ /** If `x` is lazy, computes the underlying value */
1032
+ protected def force [T <: AnyRef ](x : T | Lazy [T ])(using Context ): T = x match
1033
+ case x : Lazy [T ] @ unchecked => x.complete
1034
+ case x : T @ unchecked => x
1035
+
1036
+ /** Assigns all lazy fields their underlying non-lazy value. */
1037
+ def forceFields ()(using Context ): Unit
1038
+
1039
+ end WithLazyFields
1035
1040
1036
1041
// ----- Generic Tree Instances, inherited from `tpt` and `untpd`.
1037
1042
@@ -1355,7 +1360,7 @@ object Trees {
1355
1360
DefDef (tree : Tree )(name, paramss, tpt, rhs)
1356
1361
def TypeDef (tree : TypeDef )(name : TypeName = tree.name, rhs : Tree = tree.rhs)(using Context ): TypeDef =
1357
1362
TypeDef (tree : Tree )(name, rhs)
1358
- def Template (tree : Template )(constr : DefDef = tree.constr, parents : List [Tree ] = tree.parents, derived : List [untpd.Tree ] = tree.derived, self : ValDef = tree.self, body : LazyTreeList = tree.unforcedBody)( using Context ): Template =
1363
+ def Template (tree : Template )(using Context )( constr : DefDef = tree.constr, parents : List [Tree ] = tree.parents, derived : List [untpd.Tree ] = tree.derived, self : ValDef = tree.self, body : LazyTreeList = tree.unforcedBody): Template =
1359
1364
Template (tree : Tree )(constr, parents, derived, self, body)
1360
1365
def Hole (tree : Hole )(isTerm : Boolean = tree.isTerm, idx : Int = tree.idx, args : List [Tree ] = tree.args, content : Tree = tree.content, tpt : Tree = tree.tpt)(using Context ): Hole =
1361
1366
Hole (tree : Tree )(isTerm, idx, args, content, tpt)
@@ -1618,8 +1623,8 @@ object Trees {
1618
1623
inContext(localCtx(tree)) {
1619
1624
this (x, rhs)
1620
1625
}
1621
- case tree @ Template (constr, parents , self, _) if tree.derived.isEmpty =>
1622
- this (this (this (this (x, constr), parents), self), tree.body)
1626
+ case tree @ Template (constr, _ , self, _) if tree.derived.isEmpty =>
1627
+ this (this (this (this (x, constr), tree. parents), self), tree.body)
1623
1628
case Import (expr, _) =>
1624
1629
this (x, expr)
1625
1630
case Export (expr, _) =>
0 commit comments