Skip to content

Commit 0cd20b4

Browse files
committed
Add CookComments miniphase
This miniphase is used to cook the comments when `-Ycook-comments` is set. This phase is used by the compiler, the REPL, Dotty IDE and Dottydoc.
1 parent a082b5a commit 0cd20b4

File tree

11 files changed

+45
-46
lines changed

11 files changed

+45
-46
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ class Compiler {
5959
protected def transformPhases: List[List[Phase]] =
6060
List(new FirstTransform, // Some transformations to put trees into a canonical form
6161
new CheckReentrant, // Internal use only: Check that compiled program has no data races involving global vars
62-
new ElimPackagePrefixes) :: // Eliminate references to package prefixes in Select nodes
62+
new ElimPackagePrefixes, // Eliminate references to package prefixes in Select nodes
63+
new CookComments) :: // Cook the comments: expand variables, doc, etc.
6364
List(new CheckStatic, // Check restrictions that apply to @static members
6465
new ElimRepeated, // Rewrite vararg parameters and arguments
6566
new ExpandSAMs, // Expand single abstract method closures to anonymous classes

compiler/src/dotty/tools/dotc/fromtasty/ReadTastyTreesFromClasses.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ class ReadTastyTreesFromClasses extends FrontEnd {
4545
else {
4646
val unit = mkCompilationUnit(cls, cls.tree, forceTrees = true)
4747
unit.pickled += (cls -> unpickler.unpickler.bytes)
48-
if (ctx.settings.YcookComments.value) {
49-
ctx.typer.cookComments(cls.tree, cls)
50-
}
5148
Some(unit)
5249
}
5350
case tree: Tree[_] =>

compiler/src/dotty/tools/dotc/interactive/InteractiveCompiler.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class InteractiveCompiler extends Compiler {
1313
// This could be improved by reporting errors back to the IDE
1414
// after each phase group instead of waiting for the pipeline to finish.
1515
override def phases: List[List[Phase]] = List(
16-
List(new FrontEnd)
16+
List(new FrontEnd),
17+
List(new transform.CookComments)
1718
)
1819
}

compiler/src/dotty/tools/dotc/interactive/InteractiveDriver.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class InteractiveDriver(val settings: List[String]) extends Driver {
3333
private val myInitCtx: Context = {
3434
val rootCtx = initCtx.fresh.addMode(Mode.ReadPositions).addMode(Mode.Interactive).addMode(Mode.ReadComments)
3535
rootCtx.setSetting(rootCtx.settings.YretainTrees, true)
36+
rootCtx.setSetting(rootCtx.settings.YcookComments, true)
3637
val ctx = setup(settings.toArray, rootCtx)._2
3738
ctx.initialize()(ctx)
3839
ctx
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dotty.tools.dotc.transform
2+
3+
import dotty.tools.dotc.ast.tpd
4+
import dotty.tools.dotc.core.Contexts.Context
5+
import dotty.tools.dotc.typer.Docstrings
6+
7+
class CookComments extends MegaPhase.MiniPhase {
8+
override def phaseName = "cookComments"
9+
10+
override def transformTypeDef(tree: tpd.TypeDef)(implicit ctx: Context): tpd.Tree = {
11+
if (ctx.settings.YcookComments.value && tree.isClassDef) {
12+
val cls = tree.symbol
13+
val cookingCtx = ctx.localContext(tree, cls).setNewScope
14+
val template = tree.rhs.asInstanceOf[tpd.Template]
15+
val owner = template.self.symbol.orElse(cls)
16+
17+
template.body.foreach { stat =>
18+
Docstrings.cookComment(stat.symbol, owner)(cookingCtx)
19+
}
20+
21+
Docstrings.cookComment(cls, cls)(cookingCtx)
22+
}
23+
24+
tree
25+
26+
}
27+
28+
}

compiler/src/dotty/tools/dotc/typer/Docstrings.scala

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,7 @@ import core._
66
import Contexts._, Symbols._, Decorators._, Comments._
77
import ast.tpd
88

9-
trait Docstrings { self: Typer =>
10-
11-
/**
12-
* Expands or cooks the documentation for all members of `cdef`.
13-
*
14-
* @see Docstrings#cookComment
15-
*/
16-
def cookComments(cdef: tpd.Tree, cls: ClassSymbol)(implicit ctx: Context): Unit = {
17-
// val cls = cdef.symbol
18-
val cookingCtx = ctx.localContext(cdef, cls).setNewScope
19-
cls.info.allMembers.foreach { member =>
20-
cookComment(member.symbol, cls)(cookingCtx)
21-
}
22-
cookComment(cls, cls)(cookingCtx)
23-
}
9+
object Docstrings {
2410

2511
/**
2612
* Expands or cooks the documentation for `sym` in class `owner`.
@@ -47,8 +33,8 @@ trait Docstrings { self: Typer =>
4733
case _ =>
4834
expandComment(sym).map { expanded =>
4935
val typedUsecases = expanded.usecases.map { usecase =>
50-
enterSymbol(createSymbol(usecase.untpdCode))
51-
typedStats(usecase.untpdCode :: Nil, owner) match {
36+
ctx.typer.enterSymbol(ctx.typer.createSymbol(usecase.untpdCode))
37+
ctx.typer.typedStats(usecase.untpdCode :: Nil, owner) match {
5238
case List(df: tpd.DefDef) =>
5339
usecase.typed(df)
5440
case _ =>

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ class Typer extends Namer
8585
with Implicits
8686
with Inferencing
8787
with Dynamic
88-
with Checking
89-
with Docstrings {
88+
with Checking {
9089

9190
import Typer._
9291
import tpd.{cpy => _, _}
@@ -1592,12 +1591,6 @@ class Typer extends Namer
15921591

15931592
if (ctx.settings.YretainTrees.value) cls.treeOrProvider = cdef1
15941593

1595-
// Expand comments and type usecases if `-Ycook-comments` is set.
1596-
if (ctx.settings.YcookComments.value) {
1597-
cookComments(cdef1, cls)
1598-
}
1599-
1600-
16011594
cdef1
16021595

16031596
// todo later: check that

compiler/src/dotty/tools/repl/ReplCompiler.scala

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import dotty.tools.dotc.core.StdNames._
1414
import dotty.tools.dotc.core.Symbols._
1515
import dotty.tools.dotc.reporting.diagnostic.messages
1616
import dotty.tools.dotc.transform.PostTyper
17-
import dotty.tools.dotc.typer.{FrontEnd, ImportInfo, Typer}
17+
import dotty.tools.dotc.typer.ImportInfo
1818
import dotty.tools.dotc.util.Positions._
1919
import dotty.tools.dotc.util.SourceFile
2020
import dotty.tools.dotc.{CompilationUnit, Compiler, Run}
@@ -195,13 +195,11 @@ class ReplCompiler extends Compiler {
195195
if (stat.tpe.isError) stat.tpe.show
196196
else {
197197
val symbols = extractSymbols(stat)
198-
val typer = new Typer()
199198
val doc = for {
200199
sym <- symbols
201-
owner = sym.owner
202-
cookingCtx = ctx.withOwner(owner)
203-
cooked <- typer.cookComment(sym, owner)(cookingCtx)
204-
body <- cooked.expandedBody
200+
docCtx <- ctx.docCtx
201+
comment <- docCtx.docstring(sym)
202+
body <- comment.expandedBody
205203
} yield body
206204

207205
if (doc.hasNext) doc.next()

compiler/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class ReplDriver(settings: Array[String],
6565
/** Create a fresh and initialized context with IDE mode enabled */
6666
private[this] def initialCtx = {
6767
val rootCtx = initCtx.fresh.addMode(Mode.ReadPositions | Mode.Interactive | Mode.ReadComments)
68+
rootCtx.setSetting(rootCtx.settings.YcookComments, true)
6869
val ictx = setup(settings, rootCtx)._2
6970
ictx.base.initialize()(ictx)
7071
ictx

doc-tool/src/dotty/tools/dottydoc/DocCompiler.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import dotc.core.Mode
99
import dotc.{Compiler, Run}
1010

1111
import dotty.tools.dotc.fromtasty.{ReadTastyTreesFromClasses, TASTYRun}
12+
import dotty.tools.dotc.transform.CookComments
1213

1314
/** Custom Compiler with phases for the documentation tool
1415
*
@@ -41,6 +42,7 @@ class DocCompiler extends Compiler {
4142
Nil
4243

4344
override protected def transformPhases: List[List[Phase]] =
45+
List(new CookComments) ::
4446
List(new DocImplicitsPhase) ::
4547
List(new DocASTPhase) ::
4648
List(DocMiniTransformations(new UsecasePhase,

language-server/src/dotty/tools/languageserver/DottyLanguageServer.scala

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -368,16 +368,7 @@ class DottyLanguageServer extends LanguageServer
368368
if (tpw == NoType) null // null here indicates that no response should be sent
369369
else {
370370
val symbol = Interactive.enclosingSourceSymbol(trees, pos)
371-
val docComment = ctx.docCtx.flatMap(_.docstring(symbol)).map {
372-
case comment if !comment.isExpanded =>
373-
val typer = new Typer()
374-
val owner = symbol.owner
375-
val cookingCtx = ctx.withOwner(owner)
376-
typer.cookComment(symbol, owner)(cookingCtx)
377-
ctx.docCtx.get.docstring(symbol).get
378-
case comment =>
379-
comment
380-
}
371+
val docComment = ctx.docCtx.flatMap(_.docstring(symbol))
381372
val content = hoverContent(tpw.show, docComment)
382373
new Hover(content, null)
383374
}

0 commit comments

Comments
 (0)