scalascalacscala-compiler

How to compile output of a compiler phase?


Using -Xprint flag in scalac we get output of different compiler phases, for example given the following Foo.scala

object Foo {
  val x = 42
}

then scalac -Xprint:jvm Foo.scala outputs

package <empty> {
  object Foo extends Object {
    <static> private[this] val x: Int = _;
    <stable> <accessor> def x(): Int = Foo.this.x;
    def <init>(): Foo.type = {
      Foo.super.<init>();
      Foo.this.x = 42;
      ()
    }
  }
}

How to compile the phase itself, that is, say we have source file jvmphase.scala like so

package <empty> { ...

containing the phase source code instead of the original vanilla Scala source code, then how to achieve something similar to scalac jvmphase.scala?


Solution

  • "compiling output of a compiler phase" sounds strange. Literally compiler compiles source file. Output of a compiler phase is not a source any more (although scalac -Xprint:... tries to print it similarly). For example jvm phase is after erasure phase. And it's not clear what you expect to get while "compiling output of a compiler phase" besides result of compilation of the original source.

    If you want to make changes between phases maybe you should create compiler plugin.

    https://docs.scala-lang.org/overviews/plugins/index.html

    https://dotty.epfl.ch/docs/reference/changed-features/compiler-plugins.html