@@ -23,7 +23,7 @@ class TreePrinter(privateMembers: Boolean = false, infixTypes: Boolean = false,
23
23
private final val CompiledCode = " ???"
24
24
25
25
// TODO use parameters
26
- private val sharedTypes = mutable.Map [( Addr , Boolean ) , String ]()
26
+ private val sharedTypes = mutable.Map [Addr , String ]()
27
27
private val sourceFiles = mutable.Buffer [String ]()
28
28
private var compilerOptions = CompilerOptions .Default
29
29
private var currentExtension = Option .empty[String ]
@@ -115,8 +115,8 @@ class TreePrinter(privateMembers: Boolean = false, infixTypes: Boolean = false,
115
115
116
116
val containsPackageObject = children match {
117
117
case Seq (
118
- Node2 (VALDEF , Seq (ScalaBytecodeConstants .PackageObjectClassName )),
119
- Node2 (TYPEDEF , Seq (ScalaBytecodeConstants .PackageObjectSingletonClassName )), _ : _*
118
+ Node2 (VALDEF , Seq (ScalaBytecodeConstants .PackageObjectClassName )),
119
+ Node2 (TYPEDEF , Seq (ScalaBytecodeConstants .PackageObjectSingletonClassName )), _ : _*
120
120
) => true // TODO use name type, not contents
121
121
case _ => false
122
122
}
@@ -134,8 +134,8 @@ class TreePrinter(privateMembers: Boolean = false, infixTypes: Boolean = false,
134
134
var delimiterRequired = false
135
135
children match {
136
136
case Seq (
137
- Node2 (VALDEF , Seq (name1)),
138
- tpe @ Node3 (TYPEDEF , Seq (name2), Seq (template, _ : _* )), _ : _*
137
+ Node2 (VALDEF , Seq (name1)),
138
+ tpe @ Node3 (TYPEDEF , Seq (name2), Seq (template, _ : _* )), _ : _*
139
139
// TODO: revert `if false`
140
140
) if name1.endsWith(ScalaBytecodeConstants .TopLevelDefinitionsClassNameSuffix ) &&
141
141
name2.endsWith(ScalaBytecodeConstants .TopLevelDefinitionsSingletonClassNameSuffix ) => // TODO use name type, not contents
@@ -284,15 +284,15 @@ class TreePrinter(privateMembers: Boolean = false, infixTypes: Boolean = false,
284
284
}
285
285
// TODO recursive textOf method, common syntactic sugar for FunctionN and TupleN
286
286
val parents = blockChildren.collect { // TODO rely on name kind
287
- case node if node.isTypeTree => textOf(node)
288
- case Node3 (APPLY , _, Seq (Node3 (SELECTin , _, Seq (Node3 (NEW , _, Seq (tpe, _ : _* )), _ : _* )), _ : _* )) => textOf(tpe)
289
- case Node3 (APPLY , _, Seq (Node3 (APPLY , _, Seq (Node3 (SELECTin , _, Seq (Node3 (NEW , _, Seq (tpe, _ : _* )), _ : _* )), _ : _* )), _ : _* )) => textOf(tpe)
290
- case Node3 (APPLY , _, Seq (Node3 (TYPEAPPLY , _, Seq (Node3 (SELECTin , _, Seq (Node3 (NEW , _, Seq (base @ Node1 (IDENTtpt ), _ : _* )), _ : _* )), arguments : _* )), _ : _* )) =>
291
- base.name + arguments.map(t => simple(textOfType(t))).mkString(" [" , " , " , " ]" )
292
- case Node3 (APPLY , _, Seq (Node3 (TYPEAPPLY , _, Seq (Node3 (SELECTin , _, Seq (Node3 (NEW , _, Seq (tpe, _ : _* )), _ : _* )), _ : _* )), _ : _* )) => textOf(tpe)
293
- case Node3 (APPLY , _, Seq (Node3 (APPLY , _, Seq (Node3 (TYPEAPPLY , _, Seq (Node3 (SELECTin , _, Seq (Node3 (NEW , _, Seq (tpe, _ : _* )), _ : _* )), _ : _* )), _ : _* )), _ : _* )) => textOf(tpe)
294
- }.filter(s => s.nonEmpty && s != " _root_.java.lang.Object" && s != " _root_.scala.runtime.EnumValue" &&
295
- ! (isInCaseClass && CommonQualifiedNames .isProductOrScalaSerializableCanonical(s)))
287
+ case node if node.isTypeTree => textOf(node)
288
+ case Node3 (APPLY , _, Seq (Node3 (SELECTin , _, Seq (Node3 (NEW , _, Seq (tpe, _ : _* )), _ : _* )), _ : _* )) => textOf(tpe)
289
+ case Node3 (APPLY , _, Seq (Node3 (APPLY , _, Seq (Node3 (SELECTin , _, Seq (Node3 (NEW , _, Seq (tpe, _ : _* )), _ : _* )), _ : _* )), _ : _* )) => textOf(tpe)
290
+ case Node3 (APPLY , _, Seq (Node3 (TYPEAPPLY , _, Seq (Node3 (SELECTin , _, Seq (Node3 (NEW , _, Seq (base @ Node1 (IDENTtpt ), _ : _* )), _ : _* )), arguments : _* )), _ : _* )) =>
291
+ base.name + arguments.map(t => simple(textOfType(t))).mkString(" [" , " , " , " ]" )
292
+ case Node3 (APPLY , _, Seq (Node3 (TYPEAPPLY , _, Seq (Node3 (SELECTin , _, Seq (Node3 (NEW , _, Seq (tpe, _ : _* )), _ : _* )), _ : _* )), _ : _* )) => textOf(tpe)
293
+ case Node3 (APPLY , _, Seq (Node3 (APPLY , _, Seq (Node3 (TYPEAPPLY , _, Seq (Node3 (SELECTin , _, Seq (Node3 (NEW , _, Seq (tpe, _ : _* )), _ : _* )), _ : _* )), _ : _* )), _ : _* )) => textOf(tpe)
294
+ }.filter(s => s.nonEmpty && s != " _root_.java.lang.Object" && s != " _root_.scala.runtime.EnumValue" &&
295
+ ! (isInCaseClass && CommonQualifiedNames .isProductOrScalaSerializableCanonical(s)))
296
296
.map(simple)
297
297
val isInGiven = definition.exists(it => isGivenObject0(it) || isGivenClass0(it))
298
298
val isInAnonymousGiven = isInGiven && definition.exists(_.name.startsWith(" given_" )) // TODO common method
@@ -526,27 +526,42 @@ class TreePrinter(privateMembers: Boolean = false, infixTypes: Boolean = false,
526
526
}
527
527
528
528
private def textOfType (node : Node , parens : Int = 0 )(using parent : Option [Node ] = None ): String = {
529
- val addTypeSuffix = parent.nonEmpty && node.is(TERMREF )
529
+ val withDotTypeSuffix =
530
+ parent.forall(_.is(SINGLETONtpt , APPLIEDtype ))
531
+ && node.is(TERMREF , TERMREFsymbol , TERMREFdirect , SELECT )
530
532
531
533
if (node.isSharedType) {
532
- sharedTypes.get((node.addr, addTypeSuffix)).foreach(return _)
534
+ val fromCache = sharedTypes.get(node.addr)
535
+ fromCache.foreach { cached =>
536
+ val res =
537
+ if (withDotTypeSuffix) cached + " .type"
538
+ else cached
539
+
540
+ return res
541
+ }
533
542
}
534
-
543
+
535
544
// TODO extract method
536
545
given Option [Node ] = Some (node)
537
546
val text = node match { // TODO proper settings
538
547
case Node3 (IDENTtpt , _, Seq (tail)) => textOfType(tail)
539
548
case Node3 (SINGLETONtpt , _, Seq (tail)) =>
540
549
val literal = textOfConstant(tail)
541
- if (literal.nonEmpty) literal else textOfType(tail) + (if (tail.is(TERMREF )) " " else " .type" )
550
+ if (literal.nonEmpty) literal
551
+ else {
552
+ val tailText = textOfType(tail)
553
+ tailText + (if (tail.is(THIS , QUALTHIS )) " .type" else " " )
554
+ }
542
555
case Node3 (TYPEREF , Seq (name), Seq (tail)) => textOfType(tail) + " ." + id(name)
543
556
case Node3 (TERMREF , Seq (name), Seq (tail)) =>
544
557
// TODO why there's "package" in some cases?
545
- if (name == ScalaBytecodeConstants .PackageObjectClassName || name.endsWith(ScalaBytecodeConstants .TopLevelDefinitionsClassNameSuffix ))
546
- textOfType(tail)
558
+ val tailText = textOfType(tail)
559
+ if (name == ScalaBytecodeConstants .PackageObjectClassName ||
560
+ name.endsWith(ScalaBytecodeConstants .TopLevelDefinitionsClassNameSuffix ))
561
+ tailText
547
562
else {
548
- textOfType(tail) + " ." + id(name) +
549
- (if (parent.forall(it => it.is( SINGLETONtpt ) || it.is( APPLIEDtype )) ) " .type" else " " ) // TODO Why there is sometimes no SINGLETONtpt? (add RHS?)
563
+ tailText + " ." + id(name) +
564
+ (if (withDotTypeSuffix ) " .type" else " " ) // TODO Why there is sometimes no SINGLETONtpt? (add RHS?)
550
565
}
551
566
case Node3 (THIS , _, Seq (tail)) =>
552
567
val qualifier = textOfType(tail)
@@ -578,14 +593,20 @@ class TreePrinter(privateMembers: Boolean = false, infixTypes: Boolean = false,
578
593
else {
579
594
// TODO rely on name kind
580
595
val part1 = if (prefix.isEmpty) id(name) else prefix + " ." + id(name)
581
- val part2 = if (parent.isEmpty && node.is( TERMREFsymbol , TERMREFdirect ) ) " .type" else " "
596
+ val part2 = if (withDotTypeSuffix ) " .type" else " "
582
597
part1 + part2
583
598
}
584
599
case Node3 (SELECTtpt | SELECT , Seq (name), Seq (tail)) =>
585
600
val selector = if (node.tag == SELECTtpt && node.children.headOption.exists(it => isTypeTreeTag(it.tag))) " #" else " ."
586
601
val qualifier = textOfType(tail)
587
602
val qualifierInParens = if (selector == " #" && tail.is(REFINEDtpt )) " (" + qualifier + " )" else qualifier
588
- if (qualifier.nonEmpty) qualifierInParens + selector + id(name) else id(name)
603
+
604
+ val res =
605
+ if (qualifier.nonEmpty) qualifierInParens + selector + id(name)
606
+ else id(name)
607
+
608
+ if (withDotTypeSuffix) res + " .type"
609
+ else res
589
610
case Node2 (TERMREFpkg | TYPEREFpkg , Seq (name)) => if (name == " _root_" ) name else " _root_." + name.split('.' ).map(id(_)).mkString(" ." )
590
611
case Node3 (APPLIEDtpt | APPLIEDtype , _, Seq (constructor, arguments : _* )) =>
591
612
val base = textOfType(constructor)
@@ -683,8 +704,8 @@ class TreePrinter(privateMembers: Boolean = false, infixTypes: Boolean = false,
683
704
case _ => // TODO exhaustive match
684
705
textOfConstant(node)
685
706
}
686
-
687
- sharedTypes.put(( node.addr, addTypeSuffix), text)
707
+
708
+ sharedTypes.put(node.addr, text.stripSuffix( " .type " ) )
688
709
text
689
710
}
690
711
@@ -714,16 +735,16 @@ class TreePrinter(privateMembers: Boolean = false, infixTypes: Boolean = false,
714
735
715
736
private def textOfArray (node : Node ): String = node match {
716
737
case Node3 (APPLY , _, Seq (
717
- Node3 (APPLY , _, Seq (
718
- Node3 (TYPEAPPLY , _, Seq (
719
- Node3 (SELECTin , Seq (" apply[...]" ), Seq (
720
- Node2 (TERMREF , Seq (" Array" )),
721
- _)),
722
- _)),
723
- Node3 (TYPED , _, Seq (
724
- Node3 (REPEATED , _, args),
725
- _)))),
726
- _)) => " _root_.scala.Array(" + args.map(textOfConstantOrArray).filter(_.nonEmpty).mkString(" , " ) + " )"
738
+ Node3 (APPLY , _, Seq (
739
+ Node3 (TYPEAPPLY , _, Seq (
740
+ Node3 (SELECTin , Seq (" apply[...]" ), Seq (
741
+ Node2 (TERMREF , Seq (" Array" )),
742
+ _)),
743
+ _)),
744
+ Node3 (TYPED , _, Seq (
745
+ Node3 (REPEATED , _, args),
746
+ _)))),
747
+ _)) => " _root_.scala.Array(" + args.map(textOfConstantOrArray).filter(_.nonEmpty).mkString(" , " ) + " )"
727
748
case _ => " "
728
749
}
729
750
@@ -1022,7 +1043,7 @@ class TreePrinter(privateMembers: Boolean = false, infixTypes: Boolean = false,
1022
1043
}
1023
1044
if (node.contains(PRIVATE ) && ! excluding(PRIVATE )) {
1024
1045
if (node.contains(LOCAL )) {
1025
- // sb += "private[this] " TODO Enable? (in Scala 3 it's almost always inferred)
1046
+ // sb += "private[this] " TODO Enable? (in Scala 3 it's almost always inferred)
1026
1047
sb ++= " private "
1027
1048
} else {
1028
1049
sb ++= " private "
0 commit comments