Skip to content

Commit 5c48dfc

Browse files
committed
Print vars, protected and private
1 parent c0b0ab9 commit 5c48dfc

File tree

3 files changed

+71
-15
lines changed

3 files changed

+71
-15
lines changed

library/src/scala/tasty/util/ShowSourceCode.scala

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
190190
if (flags.isImplicit) this += "implicit "
191191
if (flags.isOverride) this += "override "
192192

193+
printProtectedOrPrivate(vdef)
194+
193195
if (flags.isLazy) this += "lazy "
194196
if (vdef.flags.isMutable) this += "var "
195197
else this += "val "
@@ -220,12 +222,14 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
220222
case ddef @ DefDef(name, targs, argss, tpt, rhs) =>
221223
printDefAnnotations(ddef)
222224

225+
val isConstructor = name == "<init>"
226+
223227
val flags = ddef.flags
224228
if (flags.isImplicit) this += "implicit "
225229
if (flags.isInline) this += "inline "
226230
if (flags.isOverride) this += "override "
227231

228-
val isConstructor = name == "<init>"
232+
printProtectedOrPrivate(ddef)
229233

230234
this += "def " += (if (isConstructor) "this" else name)
231235
printTargsDefs(targs)
@@ -595,9 +599,9 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
595599

596600
def printSeparated(list: List[ValDef]): Unit = list match {
597601
case Nil =>
598-
case x :: Nil => printArgDef(x)
602+
case x :: Nil => printParamDef(x)
599603
case x :: xs =>
600-
printArgDef(x)
604+
printParamDef(x)
601605
this += ", "
602606
printSeparated(xs)
603607
}
@@ -619,15 +623,24 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
619623
this
620624
}
621625

622-
def printArgDef(arg: ValDef): Unit = {
626+
def printParamDef(arg: ValDef): Unit = {
623627
val ValDef(name, tpt, rhs) = arg
624628
arg.owner match {
625629
case DefDef("<init>", _, _, _, _) =>
626630
val ClassDef(_, _, _, _, body) = arg.owner.owner
627631
body.collectFirst {
628632
case vdef @ ValDef(`name`, _, _) if vdef.flags.isParamAccessor =>
629-
if (!vdef.flags.isLocal && !vdef.flags.isCaseAcessor)
630-
this += "val " // TODO `var`s
633+
if (!vdef.flags.isLocal) {
634+
var printedPrefix = false
635+
if (vdef.flags.isOverride) {
636+
this += "override "
637+
printedPrefix = true
638+
}
639+
printedPrefix |= printProtectedOrPrivate(vdef)
640+
if (vdef.flags.isMutable) this += "var "
641+
else if (!printedPrefix && vdef.flags.isCaseAcessor) this // val not explicitly needed
642+
else this += "val "
643+
}
631644
}
632645
case _ =>
633646
}
@@ -896,15 +909,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
896909

897910
case Type.ThisType(tp) =>
898911
tp match {
899-
case Type.SymRef(cdef @ ClassDef(name, _, _, _, _), prefix) if !cdef.flags.isObject =>
900-
def printPrefix(prefix: TypeOrBounds): Unit = prefix match {
901-
case Type.SymRef(ClassDef(name, _, _, _, _), prefix2) =>
902-
printPrefix(prefix2)
903-
this += name += "."
904-
case _ =>
905-
}
906-
printPrefix(prefix)
907-
this += name
912+
case Type.SymRef(cdef @ ClassDef(_, _, _, _, _), _) if !cdef.flags.isObject =>
913+
printFullClassName(tp)
908914
this += ".this"
909915
case _ => printType(tp)
910916
}
@@ -1053,6 +1059,48 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
10531059
printType(hi)
10541060
}
10551061

1062+
def printProtectedOrPrivate(definition: Definition): Boolean = {
1063+
var printedPrefix = false
1064+
def printWithin(within: Type) = within match {
1065+
case Type.SymRef(PackageDef(name, _), _) => this += name
1066+
case _ => printFullClassName(within)
1067+
}
1068+
if (definition.flags.isProtected) {
1069+
this += "protected"
1070+
definition.protectedWithin match {
1071+
case Some(within) =>
1072+
this += "["
1073+
printWithin(within)
1074+
this += "] "
1075+
case _ =>
1076+
this += " "
1077+
}
1078+
printedPrefix = true
1079+
} else {
1080+
definition.privateWithin match {
1081+
case Some(within) =>
1082+
this += "private["
1083+
printWithin(within)
1084+
this += "] "
1085+
printedPrefix = true
1086+
case _ =>
1087+
}
1088+
}
1089+
printedPrefix
1090+
}
1091+
1092+
def printFullClassName(tp: TypeOrBounds): Unit = {
1093+
def printClassPrefix(prefix: TypeOrBounds): Unit = prefix match {
1094+
case Type.SymRef(ClassDef(name, _, _, _, _), prefix2) =>
1095+
printClassPrefix(prefix2)
1096+
this += name += "."
1097+
case _ =>
1098+
}
1099+
val Type.SymRef(ClassDef(name, _, _, _, _), prefix) = tp
1100+
printClassPrefix(prefix)
1101+
this += name
1102+
}
1103+
10561104
def +=(x: Boolean): this.type = { sb.append(x); this }
10571105
def +=(x: Byte): this.type = { sb.append(x); this }
10581106
def +=(x: Short): this.type = { sb.append(x); this }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** Decompiled from out/posTestFromTasty/pos/simpleConstructor/A.class */
2+
class A(a: scala.Int, val b: scala.Int, var c: scala.Int)
3+
/** Decompiled from out/posTestFromTasty/pos/simpleConstructor/B.class */
4+
class B(protected val x: scala.Int, protected[B] val y: scala.Int, private[B] val z: scala.Int)

tests/pos/simpleConstructor.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
class A(a: Int, val b: Int, var c: Int)
3+
4+
class B(protected val x: Int, protected[B] val y: Int, private[B] val z: Int)

0 commit comments

Comments
 (0)