11package dotty .tools .dotc .core .tasty
22
3- import dotty .tools .dotc .ast .tpd
3+ import dotty .tools .dotc .ast .{ tpd , untpd }
44import dotty .tools .dotc .core .Comments .{Comment , CommentsContext , ContextDocstrings }
55import dotty .tools .dotc .core .Contexts ._
66
@@ -9,36 +9,36 @@ import TastyBuffer.{Addr, NoAddr}
99
1010import java .nio .charset .Charset
1111
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 ]) :
1313 private val buf = new TastyBuffer (5000 )
1414 pickler.newSection(" Comments" , buf)
1515
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)
2017
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" ))
2421 val length = bytes.length
2522 buf.writeAddr(addr)
2623 buf.writeNat(length)
2724 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)
4032 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