Skip to content

Commit 391c576

Browse files
committed
Add spliced names
This allow naming val/def in quoted code with computed names
1 parent 0b10433 commit 391c576

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package scala.internal.quoted
2+
3+
class splicedName(name: String) extends scala.annotation.Annotation

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,8 @@ trait Printers
679679
if (vdef.symbol.flags.is(Flags.Mutable)) this += highlightKeyword("var ")
680680
else this += highlightKeyword("val ")
681681

682-
this += highlightValDef(name) += ": "
682+
val name1 = splicedName(vdef.symbol).getOrElse(name)
683+
this += highlightValDef(name1) += ": "
683684
printTypeTree(tpt)
684685
rhs match {
685686
case Some(tree) =>
@@ -714,7 +715,8 @@ trait Printers
714715

715716
printProtectedOrPrivate(ddef)
716717

717-
this += highlightKeyword("def ") += highlightValDef((if (isConstructor) "this" else name))
718+
val name1: String = if (isConstructor) "this" else splicedName(ddef.symbol).getOrElse(name)
719+
this += highlightKeyword("def ") += highlightValDef(name1)
718720
printTargsDefs(targs.zip(targs))
719721
val it = argss.iterator
720722
while (it.hasNext)
@@ -734,8 +736,11 @@ trait Printers
734736
case Ident("_") =>
735737
this += "_"
736738

737-
case IsTerm(tree @ Ident(_)) =>
738-
printType(tree.tpe)
739+
case IsIdent(tree) =>
740+
splicedName(tree.symbol) match {
741+
case Some(name) => this += name
742+
case _ => printType(tree.tpe)
743+
}
739744

740745
case Select(qual, name) =>
741746
printQualTree(qual)
@@ -1637,12 +1642,15 @@ trait Printers
16371642

16381643
def printAnnotation(annot: Term)(given elideThis: Option[Symbol]): Buffer = {
16391644
val Annotation(ref, args) = annot
1640-
this += "@"
1641-
printTypeTree(ref)
1642-
if (args.isEmpty)
1643-
this
1644-
else
1645-
inParens(printTrees(args, ", "))
1645+
if (annot.symbol.owner.fullName == "scala.internal.quoted.splicedName") this
1646+
else {
1647+
this += "@"
1648+
printTypeTree(ref)
1649+
if (args.isEmpty)
1650+
this
1651+
else
1652+
inParens(printTrees(args, ", "))
1653+
}
16461654
}
16471655

16481656
def printDefAnnotations(definition: Definition)(given elideThis: Option[Symbol]): Buffer = {
@@ -1809,6 +1817,13 @@ trait Printers
18091817
private def escapedString(str: String): String = str flatMap escapedChar
18101818
}
18111819

1820+
private def splicedName(sym: Symbol)(given Context): Option[String] = {
1821+
sym.annots.find(_.symbol.owner.fullName == "scala.internal.quoted.splicedName").map {
1822+
case Apply(_, Literal(Constant(c: String)) :: Nil) => c
1823+
case Apply(_, Inlined(_, _, Literal(Constant(c: String))) :: Nil) => c
1824+
}
1825+
}
1826+
18121827
private object SpecialOp {
18131828
def unapply(arg: Tree)(given ctx: Context): Option[(String, List[Term])] = arg match {
18141829
case IsTerm(arg @ Apply(fn, args)) =>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
((x1: scala.Double) => x1.*({
2+
val x2: scala.Double = x1.*(x1)
3+
val x4: scala.Double = x2.*(x2)
4+
x4.*({
5+
val x8: scala.Double = x4.*(x4)
6+
x8.*({
7+
val x16: scala.Double = x8.*(x8)
8+
val x32: scala.Double = x16.*(x16)
9+
val x64: scala.Double = x32.*(x32)
10+
x64
11+
})
12+
})
13+
}))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.quoted._
2+
import scala.quoted.staging._
3+
import scala.reflect.ClassTag
4+
5+
object Test {
6+
given Toolbox = Toolbox.make(getClass.getClassLoader)
7+
def main(args: Array[String]): Unit = withQuoteContext {
8+
println(powerCode(77).show)
9+
}
10+
11+
def powerCode(n: Long)(given QuoteContext): Expr[Double => Double] =
12+
'{ x1 => ${powerCode(n, 2, 'x1)} }
13+
14+
def powerCode(n: Long, idx: Int, x: Expr[Double])(given QuoteContext): Expr[Double] =
15+
if (n == 0) '{1.0}
16+
else if (n == 1) x
17+
else if (n % 2 == 0) '{ @scala.internal.quoted.splicedName(${Expr("x" + idx)}) val y = $x * $x; ${powerCode(n / 2, idx * 2, '{y})} }
18+
else '{ $x * ${powerCode(n - 1, idx, x)} }
19+
20+
}

0 commit comments

Comments
 (0)