Skip to content

Commit b08d746

Browse files
Merge pull request #8536 from dotty-staging/use-indentation-syntax-in-staging-and-inspertor-internals
Use indentation syntax in Staging/Inspector internals
2 parents ff100f4 + 34fb2fa commit b08d746

File tree

5 files changed

+81
-76
lines changed

5 files changed

+81
-76
lines changed

staging/src/scala/quoted/staging/QuoteCompiler.scala

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import scala.quoted.{Expr, QuoteContext, Type}
2929
/** Compiler that takes the contents of a quoted expression `expr` and produces
3030
* a class file with `class ' { def apply: Object = expr }`.
3131
*/
32-
private class QuoteCompiler extends Compiler {
32+
private class QuoteCompiler extends Compiler:
3333

3434
/** Either `Left` with name of the classfile generated or `Right` with the value contained in the expression */
3535
private[this] var result: Either[String, Any] = null
@@ -40,15 +40,14 @@ private class QuoteCompiler extends Compiler {
4040
override protected def picklerPhases: List[List[Phase]] =
4141
List(List(new ReifyQuotes))
4242

43-
override def newRun(implicit ctx: Context): ExprRun = {
43+
override def newRun(implicit ctx: Context): ExprRun =
4444
reset()
4545
new ExprRun(this, ctx.addMode(Mode.ReadPositions))
46-
}
4746

4847
def outputClassName: TypeName = "Generated$Code$From$Quoted".toTypeName
4948

5049
/** Frontend that receives a scala.quoted.Expr or scala.quoted.Type as input */
51-
class QuotedFrontend extends Phase {
50+
class QuotedFrontend extends Phase:
5251
import tpd._
5352

5453
def phaseName: String = "quotedFrontend"
@@ -68,14 +67,14 @@ private class QuoteCompiler extends Compiler {
6867
cls.enter(unitCtx.newDefaultConstructor(cls), EmptyScope)
6968
val meth = unitCtx.newSymbol(cls, nme.apply, Method, ExprType(defn.AnyType), coord = pos).entered
7069

71-
val quoted = {
70+
val quoted =
7271
given Context = unitCtx.withOwner(meth)
7372
val qctx = dotty.tools.dotc.quoted.QuoteContext()
7473
val quoted = PickledQuotes.quotedExprToTree(exprUnit.exprBuilder.apply(qctx))
7574
checkEscapedVariables(quoted, meth)
76-
}
75+
end quoted
7776

78-
getLiteral(quoted) match {
77+
getLiteral(quoted) match
7978
case Some(value) =>
8079
result = Right(value)
8180
None // Stop copilation here we already have the result
@@ -86,28 +85,29 @@ private class QuoteCompiler extends Compiler {
8685
val source = SourceFile.virtual("<quoted.Expr>", "")
8786
result = Left(outputClassName.toString)
8887
Some(CompilationUnit(source, tree, forceTrees = true))
89-
}
9088
}
9189

9290
/** Get the literal value if this tree only contains a literal tree */
93-
@tailrec private def getLiteral(tree: Tree): Option[Any] = tree match {
94-
case Literal(lit) => Some(lit.value)
95-
case Block(Nil, expr) => getLiteral(expr)
96-
case Inlined(_, Nil, expr) => getLiteral(expr)
97-
case _ => None
98-
}
91+
@tailrec private def getLiteral(tree: Tree): Option[Any] =
92+
tree match
93+
case Literal(lit) => Some(lit.value)
94+
case Block(Nil, expr) => getLiteral(expr)
95+
case Inlined(_, Nil, expr) => getLiteral(expr)
96+
case _ => None
9997

10098
def run(implicit ctx: Context): Unit = unsupported("run")
101-
}
10299

103-
class ExprRun(comp: QuoteCompiler, ictx: Context) extends Run(comp, ictx) {
100+
end QuotedFrontend
101+
102+
class ExprRun(comp: QuoteCompiler, ictx: Context) extends Run(comp, ictx):
104103
/** Unpickle and optionally compile the expression.
105104
* Returns either `Left` with name of the classfile generated or `Right` with the value contained in the expression.
106105
*/
107-
def compileExpr(exprBuilder: QuoteContext => Expr[_]): Either[String, Any] = {
106+
def compileExpr(exprBuilder: QuoteContext => Expr[_]): Either[String, Any] =
108107
val units = new ExprCompilationUnit(exprBuilder) :: Nil
109108
compileUnits(units)
110109
result
111-
}
112-
}
113-
}
110+
111+
end ExprRun
112+
113+
end QuoteCompiler

staging/src/scala/quoted/staging/QuoteDriver.scala

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,26 @@ import scala.annotation.tailrec
1818
*
1919
* @param appClassloader classloader of the application that generated the quotes
2020
*/
21-
private class QuoteDriver(appClassloader: ClassLoader) extends Driver {
21+
private class QuoteDriver(appClassloader: ClassLoader) extends Driver:
2222
import tpd._
2323

2424
private[this] val contextBase: ContextBase = new ContextBase
2525

26-
def run[T](exprBuilder: QuoteContext => Expr[T], settings: Toolbox.Settings): T = {
27-
val outDir: AbstractFile = settings.outDir match {
28-
case Some(out) =>
29-
val dir = Directory(out)
30-
dir.createDirectory()
31-
new PlainDirectory(Directory(out))
32-
case None =>
33-
new VirtualDirectory("<quote compilation output>")
34-
}
26+
def run[T](exprBuilder: QuoteContext => Expr[T], settings: Toolbox.Settings): T =
27+
val outDir: AbstractFile =
28+
settings.outDir match
29+
case Some(out) =>
30+
val dir = Directory(out)
31+
dir.createDirectory()
32+
new PlainDirectory(Directory(out))
33+
case None =>
34+
new VirtualDirectory("<quote compilation output>")
35+
end outDir
3536

3637
val (_, ctx0: Context) = setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)
3738
val ctx = setToolboxSettings(ctx0.fresh.setSetting(ctx0.settings.outputDir, outDir), settings)
3839

39-
new QuoteCompiler().newRun(ctx).compileExpr(exprBuilder) match {
40+
new QuoteCompiler().newRun(ctx).compileExpr(exprBuilder) match
4041
case Right(value) =>
4142
value.asInstanceOf[T]
4243

@@ -50,21 +51,19 @@ private class QuoteDriver(appClassloader: ClassLoader) extends Driver {
5051
val inst = clazz.getConstructor().newInstance()
5152

5253
method.invoke(inst).asInstanceOf[T]
53-
}
54-
}
54+
end match
5555

56-
override def initCtx: Context = {
56+
end run
57+
58+
override def initCtx: Context =
5759
val ictx = contextBase.initialCtx
5860
ictx.settings.classpath.update(ClasspathFromClassloader(appClassloader))(ictx)
5961
ictx
60-
}
6162

62-
private def setToolboxSettings(ctx: FreshContext, settings: Toolbox.Settings): ctx.type = {
63+
private def setToolboxSettings(ctx: FreshContext, settings: Toolbox.Settings): ctx.type =
6364
ctx.setSetting(ctx.settings.YshowRawQuoteTrees, settings.showRawTree)
6465
// An error in the generated code is a bug in the compiler
6566
// Setting the throwing reporter however will report any exception
6667
ctx.setReporter(new ThrowingReporter(ctx.reporter))
67-
}
68-
69-
}
7068

69+
end QuoteDriver

staging/src/scala/quoted/staging/Toolbox.scala

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ package staging
44
import scala.annotation.implicitNotFound
55

66
@implicitNotFound("Could not find implicit scala.quoted.staging.Toolbox.\n\nDefault toolbox can be instantiated with:\n `given scala.quoted.staging.Toolbox = scala.quoted.staging.Toolbox.make(getClass.getClassLoader)`\n\n")
7-
trait Toolbox {
7+
trait Toolbox:
88
def run[T](expr: QuoteContext => Expr[T]): T
9-
}
109

11-
object Toolbox {
10+
object Toolbox:
1211

1312
/** Create a new instance of the toolbox using the the classloader of the application.
1413
*
@@ -22,28 +21,30 @@ object Toolbox {
2221
* @param settings toolbox settings
2322
* @return A new instance of the toolbox
2423
*/
25-
def make(appClassloader: ClassLoader)(implicit settings: Settings): Toolbox = new Toolbox {
24+
def make(appClassloader: ClassLoader)(implicit settings: Settings): Toolbox =
25+
new Toolbox:
2626

27-
private[this] val driver: QuoteDriver = new QuoteDriver(appClassloader)
27+
private[this] val driver: QuoteDriver = new QuoteDriver(appClassloader)
2828

29-
private[this] var running = false
29+
private[this] var running = false
3030

31-
def run[T](exprBuilder: QuoteContext => Expr[T]): T = synchronized {
32-
try {
33-
if (running) // detected nested run
34-
throw new ScopeException("Cannot call `scala.quoted.staging.run(...)` within a another `run(...)`")
35-
running = true
36-
driver.run(exprBuilder, settings)
37-
} finally {
38-
running = false
31+
def run[T](exprBuilder: QuoteContext => Expr[T]): T = synchronized {
32+
try
33+
if (running) // detected nested run
34+
throw new ScopeException("Cannot call `scala.quoted.staging.run(...)` within a another `run(...)`")
35+
running = true
36+
driver.run(exprBuilder, settings)
37+
finally
38+
running = false
39+
end try
3940
}
40-
}
41-
}
41+
42+
end new
4243

4344
/** Setting of the Toolbox instance. */
4445
case class Settings private (outDir: Option[String], showRawTree: Boolean, compilerArgs: List[String])
4546

46-
object Settings {
47+
object Settings:
4748

4849
implicit def default: Settings = make()
4950

@@ -58,6 +59,7 @@ object Toolbox {
5859
compilerArgs: List[String] = Nil
5960
): Settings =
6061
new Settings(outDir, showRawTree, compilerArgs)
61-
}
6262

63-
}
63+
end Settings
64+
65+
end Toolbox

staging/src/scala/quoted/staging/staging.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package scala.quoted
22

3-
package object staging {
3+
package object staging:
44

55
/** Evaluate the contents of this expression and return the result.
66
* It provides a new QuoteContext that is only valid within the scope the argument.
@@ -32,7 +32,7 @@ package object staging {
3232
* This method should not be called in a context where there is already has a `QuoteContext`
3333
* such as within a `run` or a `withQuoteContext`.
3434
*/
35-
def withQuoteContext[T](thunk: QuoteContext ?=> T)(using toolbox: Toolbox): T = {
35+
def withQuoteContext[T](thunk: QuoteContext ?=> T)(using toolbox: Toolbox): T =
3636
val noResult = new Object
3737
var result: T = noResult.asInstanceOf[T]
3838
def dummyRun(using QuoteContext): Expr[Unit] = {
@@ -42,6 +42,6 @@ package object staging {
4242
toolbox.run(dummyRun(using _))
4343
assert(result != noResult) // toolbox.run should have thrown an exception
4444
result
45-
}
45+
end withQuoteContext
4646

47-
}
47+
end staging

tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import dotty.tools.dotc.util.ClasspathFromClassloader
1414

1515
import java.io.File.pathSeparator
1616

17-
trait TastyInspector { self =>
17+
trait TastyInspector:
18+
self =>
1819

1920
/** Process a TASTy file using TASTy reflect */
2021
protected def processCompilationUnit(reflect: Reflection)(root: reflect.Tree): Unit
@@ -24,44 +25,47 @@ trait TastyInspector { self =>
2425
* @param classpath Classpath where the classes are located
2526
* @param classes classes to be inspected
2627
*/
27-
def inspect(classpath: String, classes: List[String]): Unit = {
28+
def inspect(classpath: String, classes: List[String]): Unit =
2829
if (classes.isEmpty)
2930
throw new IllegalArgumentException("Parameter classes should no be empty")
3031

31-
class InspectorDriver extends Driver {
32+
class InspectorDriver extends Driver:
3233
override protected def newCompiler(implicit ctx: Context): Compiler = new TastyFromClass
33-
}
3434

35-
class TastyFromClass extends TASTYCompiler {
35+
class TastyFromClass extends TASTYCompiler:
36+
3637
override protected def frontendPhases: List[List[Phase]] =
3738
List(new ReadTasty) :: // Load classes from tasty
3839
Nil
3940

4041
override protected def picklerPhases: List[List[Phase]] = Nil
42+
4143
override protected def transformPhases: List[List[Phase]] = Nil
4244

4345
override protected def backendPhases: List[List[Phase]] =
4446
List(new TastyInspectorPhase) :: // Print all loaded classes
4547
Nil
4648

47-
override def newRun(implicit ctx: Context): Run = {
49+
override def newRun(implicit ctx: Context): Run =
4850
reset()
4951
new TASTYRun(this, ctx.fresh.addMode(Mode.ReadPositions).addMode(Mode.ReadComments))
50-
}
51-
}
5252

53-
class TastyInspectorPhase extends Phase {
53+
end TastyFromClass
54+
55+
class TastyInspectorPhase extends Phase:
56+
5457
override def phaseName: String = "tastyInspector"
5558

56-
override def run(implicit ctx: Context): Unit = {
59+
override def run(implicit ctx: Context): Unit =
5760
val reflect = ReflectionImpl(ctx)
5861
self.processCompilationUnit(reflect)(ctx.compilationUnit.tpdTree.asInstanceOf[reflect.Tree])
59-
}
60-
}
62+
63+
end TastyInspectorPhase
6164

6265
val currentClasspath = ClasspathFromClassloader(getClass.getClassLoader)
6366
val args = "-from-tasty" :: "-Yretain-trees" :: "-classpath" :: s"$classpath$pathSeparator$currentClasspath" :: classes
6467
(new InspectorDriver).process(args.toArray)
65-
}
68+
end inspect
69+
6670

67-
}
71+
end TastyInspector

0 commit comments

Comments
 (0)