All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dotty.tools.dotc.transform.SetRootTree.scala Maven / Gradle / Ivy

There is a newer version: 3.6.4-RC1-bin-20241220-0bfa1af-NIGHTLY
Show newest version
package dotty.tools.dotc.transform

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.core.Phases.Phase

/** Set the `rootTreeOrProvider` property of class symbols. */
class SetRootTree extends Phase {

  override val phaseName: String = SetRootTree.name

  override val description: String = SetRootTree.description

  override def isRunnable(using Context) =
    super.isRunnable && ctx.settings.YretainTrees.value

  // Check no needed. Does not transform trees
  override def isCheckable: Boolean = false

  override def run(using Context): Unit = {
    val tree = ctx.compilationUnit.tpdTree
    traverser.traverse(tree)
  }

  private def traverser = new tpd.TreeTraverser {
    override def traverse(tree: tpd.Tree)(using Context): Unit =
      tree match {
        case pkg: tpd.PackageDef =>
          traverseChildren(pkg)
        case td: tpd.TypeDef =>
          if (td.symbol.isClass) {
            val sym = td.symbol.asClass
            tpd.sliceTopLevel(ctx.compilationUnit.tpdTree, sym) match {
              case (pkg: tpd.PackageDef) :: Nil =>
                sym.rootTreeOrProvider = pkg
              case _ =>
                sym.rootTreeOrProvider = td
            }
          }
        case _ =>
          ()
      }
  }
}

object SetRootTree {
  val name: String = "SetRootTree"
  val description: String = "set the rootTreeOrProvider on class symbols"
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy