@@ -558,32 +558,44 @@ object Symbols {
558558 /** If this is a top-level class, and if `-Yretain-trees` is set, return the TypeDef tree
559559 * for this class, otherwise EmptyTree. This will force the info of the class.
560560 */
561- def tree (implicit ctx : Context ): tpd.Tree /* tpd.TypeDef | tpd.EmptyTree */ = {
561+ def tree (implicit ctx : Context ): tpd.Tree /* tpd.TypeDef | tpd.EmptyTree */ = {
562+ import ast .Trees ._
563+ def findTree (tree : tpd.Tree ): Option [tpd.TypeDef ] = tree match {
564+ case PackageDef (_, stats) =>
565+ stats.flatMap(findTree).headOption
566+ case tree : tpd.TypeDef if tree.symbol == this =>
567+ Some (tree)
568+ case _ =>
569+ None
570+ }
571+ val t = unitTree
572+ if (t.isEmpty) t
573+ else findTree(tree).get
574+ }
575+
576+ /** If this is a top-level class, and if `-Yretain-trees` or `-YlinkOptimise` is set,
577+ * return the PackageDef tree for this class, otherwise EmptyTree.
578+ * This will force the info of the class.
579+ */
580+ def unitTree (implicit ctx : Context ): tpd.Tree /* tpd.PackageDef | tpd.TypeDef | tpd.EmptyTree */ = {
562581 denot.info
563582 // TODO: Consider storing this tree like we store lazy trees for inline functions
564583 if (unpickler != null && ! denot.isAbsent) {
565584 assert(myTree.isEmpty)
566-
567- import ast .Trees ._
568-
569- def findTree (tree : tpd.Tree ): Option [tpd.TypeDef ] = tree match {
570- case PackageDef (_, stats) =>
571- stats.flatMap(findTree).headOption
572- case tree : tpd.TypeDef if tree.symbol == this =>
573- Some (tree)
574- case _ =>
575- None
576- }
577- val List (unpickledTree) = unpickler.body(ctx.addMode(Mode .ReadPositions ))
585+ val body = unpickler.body(ctx.addMode(Mode .ReadPositions ))
586+ myTree = body.headOption.getOrElse(tpd.EmptyTree )
578587 unpickler = null
579-
580- myTree = findTree(unpickledTree).get
581588 }
582589 myTree
583590 }
584- private [dotc] var myTree : tpd.Tree = tpd.EmptyTree
591+ private var myTree : tpd.Tree /* tpd.PackageDef | tpd.TypeDef | tpd.EmptyTree */ = tpd.EmptyTree
585592 private [dotc] var unpickler : tasty.DottyUnpickler = _
586593
594+ private [dotc] def registerTree (tree : tpd.TypeDef )(implicit ctx : Context ): Unit = {
595+ if (ctx.settings.YretainTrees .value)
596+ myTree = tree
597+ }
598+
587599 /** The source or class file from which this class was generated, null if not applicable. */
588600 override def associatedFile (implicit ctx : Context ): AbstractFile =
589601 if (assocFile != null || (this .owner is PackageClass ) || this .isEffectiveRoot) assocFile
0 commit comments