1
1
package dotty .tools .dotc .core .tasty
2
2
3
- import dotty .tools .dotc .ast .tpd
3
+ import dotty .tools .dotc .ast .{ tpd , untpd }
4
4
import dotty .tools .dotc .core .Comments .{Comment , CommentsContext , ContextDocstrings }
5
5
import dotty .tools .dotc .core .Contexts ._
6
6
@@ -9,36 +9,36 @@ import TastyBuffer.{Addr, NoAddr}
9
9
10
10
import java .nio .charset .Charset
11
11
12
- class CommentPickler (pickler : TastyPickler , addrOfTree : tpd.Tree => Addr )( using Context ) {
12
+ class CommentPickler (pickler : TastyPickler , addrOfTree : tpd.Tree => Addr , docString : untpd. MemberDef => Option [ Comment ]) :
13
13
private val buf = new TastyBuffer (5000 )
14
14
pickler.newSection(" Comments" , buf)
15
15
16
- def pickleComment (root : tpd.Tree ): Unit = {
17
- assert(ctx.docCtx.isDefined, " Trying to pickle comments, but there's no `docCtx`." )
18
- new Traverser (ctx.docCtx.get).traverse(root)
19
- }
16
+ def pickleComment (root : tpd.Tree ): Unit = traverse(root)
20
17
21
- def pickleComment (addr : Addr , comment : Option [ Comment ] ): Unit = comment match {
22
- case Some (cmt) if addr != NoAddr =>
23
- val bytes = cmt .raw.getBytes(Charset .forName(" UTF-8" ))
18
+ private def pickleComment (addr : Addr , comment : Comment ): Unit =
19
+ if addr != NoAddr then
20
+ val bytes = comment .raw.getBytes(Charset .forName(" UTF-8" ))
24
21
val length = bytes.length
25
22
buf.writeAddr(addr)
26
23
buf.writeNat(length)
27
24
buf.writeBytes(bytes, length)
28
- buf.writeLongInt(cmt.span.coords)
29
- case other =>
30
- ()
31
- }
32
-
33
- private class Traverser (docCtx : ContextDocstrings ) extends tpd.TreeTraverser {
34
- override def traverse (tree : tpd.Tree )(using Context ): Unit =
35
- tree match {
36
- case md : tpd.MemberDef =>
37
- val comment = docCtx.docstring(md.symbol)
38
- pickleComment(addrOfTree(md), comment)
39
- traverseChildren(md)
25
+ buf.writeLongInt(comment.span.coords)
26
+
27
+ private def traverse (x : Any ): Unit = x match
28
+ case x : untpd.Tree @ unchecked =>
29
+ x match
30
+ case x : tpd.MemberDef @ unchecked => // at this point all MembderDefs are t(y)p(e)d.
31
+ for comment <- docString(x) do pickleComment(addrOfTree(x), comment)
40
32
case _ =>
41
- traverseChildren(tree)
42
- }
43
- }
44
- }
33
+ val limit = x.productArity
34
+ var n = 0
35
+ while n < limit do
36
+ traverse(x.productElement(n))
37
+ n += 1
38
+ case y :: ys =>
39
+ traverse(y)
40
+ traverse(ys)
41
+ case _ =>
42
+
43
+ end CommentPickler
44
+
0 commit comments