Skip to content

Commit 7e8014e

Browse files
committed
Add paramSymss to TASTy reflect
1 parent 3fcfeb3 commit 7e8014e

File tree

6 files changed

+76
-0
lines changed

6 files changed

+76
-0
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
17491749
private def isMethod(sym: Symbol)(using ctx: Context): Boolean =
17501750
sym.isTerm && sym.is(Flags.Method) && !sym.isConstructor
17511751

1752+
def Symbol_paramSymss(self: Symbol)(using ctx: Context): (List[Symbol], List[List[Symbol]]) =
1753+
self.paramSymss
1754+
17521755
def Symbol_caseFields(self: Symbol)(using ctx: Context): List[Symbol] =
17531756
if (!self.isClass) Nil
17541757
else self.asClass.paramAccessors.collect {

library/src/scala/tasty/Reflection.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,12 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
22232223
def methods(using ctx: Context): List[Symbol] =
22242224
internal.Symbol_methods(sym)
22252225

2226+
/** A pair consistsing of type paremeter symbols and value parameter symbol lists
2227+
* of this method definition, or (Nil, Nil) for other symbols.
2228+
*/
2229+
def paramSymss(using ctx: Context): (List[Symbol], List[List[Symbol]]) =
2230+
internal.Symbol_paramSymss(sym)
2231+
22262232
/** Fields of a case class type -- only the ones declared in primary constructor */
22272233
def caseFields(using ctx: Context): List[Symbol] =
22282234
internal.Symbol_caseFields(sym)

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,11 @@ trait CompilerInterface {
13051305
/** Get all non-private methods declared or inherited */
13061306
def Symbol_methods(self: Symbol)(using ctx: Context): List[Symbol]
13071307

1308+
/** A pair consistsing of type paremeter symbols and value parameter symbol lists
1309+
* of this method definition, or (Nil, Nil) for other symbols.
1310+
*/
1311+
def Symbol_paramSymss(self: Symbol)(using ctx: Context): (List[Symbol], List[List[Symbol]])
1312+
13081313
/** Fields of a case class type -- only the ones declared in primary constructor */
13091314
def Symbol_caseFields(self: Symbol)(using ctx: Context): List[Symbol]
13101315

tests/run-macros/paramSymss.check

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
sym: Test$.a
2+
tparams: List()
3+
vparamss: List()
4+
5+
sym: Test$.b
6+
tparams: List()
7+
vparamss: List(List(Test$._$i))
8+
9+
sym: Test$.c
10+
tparams: List()
11+
vparamss: List(List(Test$._$x), List(Test$._$y), List(Test$._$z))
12+
13+
sym: Test$.d
14+
tparams: List(Test$._$T)
15+
vparamss: List()
16+
17+
sym: Test$.c
18+
tparams: List(Test$._$T, Test$._$U)
19+
vparamss: List(List(Test$._$x, Test$._$x2), List(Test$._$y), List(Test$._$z))
20+
21+
sym: Test.<init>
22+
tparams: List(Test._$T)
23+
vparamss: List(List(Test._$a))
24+
25+
sym: Test.<init>
26+
tparams: List(Test._$T)
27+
vparamss: List(List(Test._$a, Test._$b))
28+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.quoted._
2+
3+
inline def showParamSyms(inline x: Any): String =
4+
${ showParamSymsExpr('x) }
5+
6+
def showParamSymsExpr(using QuoteContext)(x: Expr[Any]): Expr[String] =
7+
val '{ $y: Any } = x // Drop Inlined not to access the symbol
8+
val sym = y.unseal.symbol
9+
val (tparams, vparamss) = sym.paramSymss
10+
Expr(
11+
s"""sym: ${sym.show}
12+
|tparams: ${tparams.map(_.show)}
13+
|vparamss: ${vparamss.map(_.map(_.show))}
14+
|""".stripMargin)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
object Test {
3+
def a: Unit = ()
4+
def b(i: Int): Unit = ()
5+
def c(x: Int)(y: String)(z: Boolean): Unit = ()
6+
def d[T]: Unit = ()
7+
def c[T, U](x: Int, x2: Int)(y: String)(z: Boolean): Unit = ()
8+
9+
def main(args: Array[String]) =
10+
println(showParamSyms(a))
11+
println(showParamSyms(b(1)))
12+
println(showParamSyms(c(2)("")(true)))
13+
println(showParamSyms(d[Int]))
14+
println(showParamSyms(c[Int, Char](3, 4)("")(true)))
15+
println(showParamSyms(new Test(1)))
16+
println(showParamSyms(new Test(1, 2)))
17+
}
18+
19+
class Test[T](a: T):
20+
def this(a: Int, b: T) = this(b)

0 commit comments

Comments
 (0)