Skip to content

Commit d32d4af

Browse files
committed
Add reflect ClassDef.apply
1 parent 8922c88 commit d32d4af

File tree

19 files changed

+303
-2
lines changed

19 files changed

+303
-2
lines changed

compiler/src/dotty/tools/dotc/core/Symbols.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ object Symbols {
569569
def complete(denot: SymDenotation)(using Context): Unit = {
570570
val cls = denot.asClass.classSymbol
571571
val decls = newScope
572-
denot.info = ClassInfo(owner.thisType, cls, parentTypes.map(_.dealias), decls)
572+
denot.info = ClassInfo(owner.thisType, cls, parentTypes.map(_.dealias), decls, selfInfo)
573573
}
574574
}
575575
newClassSymbol(owner, name, flags, completer, privateWithin, coord, assocFile)

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
227227
end ClassDefTypeTest
228228

229229
object ClassDef extends ClassDefModule:
230+
def apply(cls: Symbol, parents: List[Tree], body: List[Statement]): ClassDef =
231+
val untpdCtr = untpd.DefDef(nme.CONSTRUCTOR, Nil, tpd.TypeTree(dotc.core.Symbols.defn.UnitClass.typeRef), untpd.EmptyTree)
232+
val ctr = ctx.typeAssigner.assignType(untpdCtr, cls.primaryConstructor)
233+
tpd.ClassDefWithParents(cls.asClass, ctr, parents, body)
234+
230235
def copy(original: Tree)(name: String, constr: DefDef, parents: List[Tree], selfOpt: Option[ValDef], body: List[Statement]): ClassDef = {
231236
val dotc.ast.Trees.TypeDef(_, originalImpl: tpd.Template) = original
232237
tpd.cpy.TypeDef(original)(name.toTypeName, tpd.cpy.Template(originalImpl)(constr, parents, derived = Nil, selfOpt.getOrElse(tpd.EmptyValDef), body))
@@ -259,6 +264,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
259264

260265
object DefDef extends DefDefModule:
261266
def apply(symbol: Symbol, rhsFn: List[List[Tree]] => Option[Term]): DefDef =
267+
assert(symbol.isTerm, s"expected a term symbol but received $symbol")
262268
withDefaultPos(tpd.DefDef(symbol.asTerm, prefss =>
263269
xCheckMacroedOwners(xCheckMacroValidExpr(rhsFn(prefss)), symbol).getOrElse(tpd.EmptyTree)
264270
))
@@ -1803,7 +1809,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
18031809
(x.prefix, x.name.toString)
18041810
end TermRef
18051811

1806-
type TypeRef = dotc.core.Types.NamedType
1812+
type TypeRef = dotc.core.Types.TypeRef
18071813

18081814
object TypeRefTypeTest extends TypeTest[TypeRepr, TypeRef]:
18091815
def unapply(x: TypeRepr): Option[TypeRef & x.type] = x match
@@ -2453,6 +2459,20 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
24532459
def requiredModule(path: String): Symbol = dotc.core.Symbols.requiredModule(path)
24542460
def requiredMethod(path: String): Symbol = dotc.core.Symbols.requiredMethod(path)
24552461
def classSymbol(fullName: String): Symbol = dotc.core.Symbols.requiredClass(fullName)
2462+
2463+
def newClass(owner: Symbol, name: String, parents: List[TypeRepr], decls: Symbol => List[Symbol], selfInfo: Option[TypeRepr]): Symbol =
2464+
assert(parents.nonEmpty && !parents.head.typeSymbol.is(dotc.core.Flags.Trait), "First parent must be a class")
2465+
val cls = dotc.core.Symbols.newNormalizedClassSymbol(
2466+
owner,
2467+
name.toTypeName,
2468+
dotc.core.Flags.EmptyFlags,
2469+
parents,
2470+
selfInfo.getOrElse(Types.NoType),
2471+
dotc.core.Symbols.NoSymbol)
2472+
cls.enter(dotc.core.Symbols.newConstructor(cls, dotc.core.Flags.Synthetic, Nil, Nil))
2473+
for sym <- decls(cls) do cls.enter(sym)
2474+
cls
2475+
24562476
def newMethod(owner: Symbol, name: String, tpe: TypeRepr): Symbol =
24572477
newMethod(owner, name, tpe, Flags.EmptyFlags, noSymbol)
24582478
def newMethod(owner: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol =
@@ -2621,6 +2641,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
26212641
def companionClass: Symbol = self.denot.companionClass
26222642
def companionModule: Symbol = self.denot.companionModule
26232643
def children: List[Symbol] = self.denot.children
2644+
def typeRef: TypeRef = self.denot.typeRef
2645+
def termRef: TermRef = self.denot.termRef
26242646

26252647
def show(using printer: Printer[Symbol]): String = printer.show(self)
26262648

library/src/scala/quoted/Quotes.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
464464

465465
/** Methods of the module object `val ClassDef` */
466466
trait ClassDefModule { this: ClassDef.type =>
467+
@experimental def apply(cls: Symbol, parents: List[Tree /* Term | TypeTree */], body: List[Statement]): ClassDef
467468
def copy(original: Tree)(name: String, constr: DefDef, parents: List[Tree /* Term | TypeTree */], selfOpt: Option[ValDef], body: List[Statement]): ClassDef
468469
def unapply(cdef: ClassDef): (String, DefDef, List[Tree /* Term | TypeTree */], Option[ValDef], List[Statement])
469470
}
@@ -3533,6 +3534,8 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
35333534
/** The class Symbol of a global class definition */
35343535
def classSymbol(fullName: String): Symbol
35353536

3537+
@experimental def newClass(owner: Symbol, name: String, parents: List[TypeRepr], decls: Symbol => List[Symbol], selfInfo: Option[TypeRepr]): Symbol
3538+
35363539
/** Generates a new method symbol with the given parent, name and type.
35373540
*
35383541
* This symbol starts without an accompanying definition.
@@ -3807,6 +3810,14 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
38073810

38083811
/** Case class or case object children of a sealed trait or cases of an `enum`. */
38093812
def children: List[Symbol]
3813+
3814+
/** Type reference to the symbol */
3815+
@experimental
3816+
def typeRef: TypeRef
3817+
3818+
/** Term reference to the symbol */
3819+
@experimental
3820+
def termRef: TermRef
38103821
end extension
38113822
}
38123823

project/MiMaFilters.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ object MiMaFilters {
1717
ProblemFilters.exclude[MissingClassProblem]("scala.compiletime.ops.long$"),
1818
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#CompilationInfoModule.XmacroSettings"),
1919
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#CompilationInfoModule.XmacroSettings"),
20+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#ClassDefModule.apply"),
21+
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#ClassDefModule.apply"),
22+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolModule.newClass"),
23+
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolModule.newClass"),
24+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.typeRef"),
25+
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.typeRef"),
26+
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.termRef"),
27+
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.termRef"),
2028

2129
// Private to the compiler - needed for forward binary compatibility
2230
ProblemFilters.exclude[MissingClassProblem]("scala.annotation.since")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
-- Error: tests/neg-macros/newClassExtendsNoParents/Test_2.scala:1:25 --------------------------------------------------
3+
1 |def test: Any = makeClass("foo") // error
4+
| ^^^^^^^^^^^^^^^^
5+
| Exception occurred while executing macro expansion.
6+
| java.lang.AssertionError: assertion failed: First parent must be a class
7+
| at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
8+
| at scala.quoted.runtime.impl.QuotesImpl$reflect$Symbol$.newClass(QuotesImpl.scala:2464)
9+
| at scala.quoted.runtime.impl.QuotesImpl$reflect$Symbol$.newClass(QuotesImpl.scala:2463)
10+
| at Macro_1$package$.makeClassExpr(Macro_1.scala:11)
11+
| at Macro_1$package$.inline$makeClassExpr(Macro_1.scala:4)
12+
|
13+
|---------------------------------------------------------------------------------------------------------------------
14+
|Inline stack trace
15+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
16+
|This location contains code that was inlined from Macro_1.scala:3
17+
3 |inline def makeClass(inline name: String): Any = ${ makeClassExpr('name) }
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
19+
---------------------------------------------------------------------------------------------------------------------
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import scala.quoted.*
2+
3+
inline def makeClass(inline name: String): Any = ${ makeClassExpr('name) }
4+
private def makeClassExpr(nameExpr: Expr[String])(using Quotes): Expr[Any] = {
5+
import quotes.reflect.*
6+
7+
val name = nameExpr.valueOrAbort
8+
val parents = List.empty[Tree] // BUG: first parent is not a class
9+
def decls(cls: Symbol): List[Symbol] = Nil
10+
11+
val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = Nil, decls, selfInfo = None)
12+
val clsDef = ClassDef(cls, parents, body = List())
13+
val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Object])
14+
15+
Block(List(clsDef), newCls).asExpr
16+
17+
// '{
18+
// class `name`() {
19+
// def foo(): Unit = println("Calling `name`.foo")
20+
// }
21+
// new `name`()
22+
// }
23+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def test: Any = makeClass("foo") // error
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
-- Error: tests/neg-macros/newClassExtendsOnlyTrait/Test_2.scala:1:25 --------------------------------------------------
3+
1 |def test: Foo = makeClass("foo") // error
4+
| ^^^^^^^^^^^^^^^^
5+
| Exception occurred while executing macro expansion.
6+
| java.lang.AssertionError: assertion failed: First parent must be a class
7+
| at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
8+
| at scala.quoted.runtime.impl.QuotesImpl$reflect$Symbol$.newClass(QuotesImpl.scala:2464)
9+
| at scala.quoted.runtime.impl.QuotesImpl$reflect$Symbol$.newClass(QuotesImpl.scala:2463)
10+
| at Macro_1$package$.makeClassExpr(Macro_1.scala:12)
11+
| at Macro_1$package$.inline$makeClassExpr(Macro_1.scala:4)
12+
|
13+
|---------------------------------------------------------------------------------------------------------------------
14+
|Inline stack trace
15+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
16+
|This location contains code that was inlined from Macro_1.scala:3
17+
3 |inline def makeClass(inline name: String): Foo = ${ makeClassExpr('name) }
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
19+
---------------------------------------------------------------------------------------------------------------------
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import scala.quoted.*
2+
3+
inline def makeClass(inline name: String): Foo = ${ makeClassExpr('name) }
4+
private def makeClassExpr(nameExpr: Expr[String])(using Quotes): Expr[Foo] = {
5+
import quotes.reflect.*
6+
7+
val name = nameExpr.valueOrAbort
8+
val parents = List(TypeTree.of[Foo]) // BUG: first parent is not a class
9+
def decls(cls: Symbol): List[Symbol] =
10+
List(Symbol.newMethod(cls, "foo", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit])))
11+
12+
val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = parents.map(_.tpe), decls, selfInfo = None)
13+
val fooSym = cls.declaredMethod("foo").head
14+
15+
val fooDef = DefDef(fooSym, argss => Some('{println(s"Calling ${$nameExpr}.foo")}.asTerm))
16+
val clsDef = ClassDef(cls, parents, body = List(fooDef))
17+
val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Foo])
18+
19+
Block(List(clsDef), newCls).asExprOf[Foo]
20+
21+
// '{
22+
// class `name`() extends Foo {
23+
// def foo(): Unit = println("Calling `name`.foo")
24+
// }
25+
// new `name`()
26+
// }
27+
}
28+
29+
trait Foo {
30+
def foo(): Unit
31+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def test: Foo = makeClass("foo") // error

tests/run-macros/newClass.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Constructing foo
2+
class Test_2$package$foo$1
3+
Constructing bar
4+
class Test_2$package$bar$1
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import scala.quoted.*
2+
3+
inline def makeClass(inline name: String): Any = ${ makeClassExpr('name) }
4+
private def makeClassExpr(nameExpr: Expr[String])(using Quotes): Expr[Any] = {
5+
import quotes.reflect.*
6+
7+
val name = nameExpr.valueOrAbort
8+
val parents = List(TypeTree.of[Object])
9+
def decls(cls: Symbol): List[Symbol] = Nil
10+
11+
val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = parents.map(_.tpe), decls, selfInfo = None)
12+
13+
val clsDef = ClassDef(cls, parents, body = List('{println(s"Constructing ${$nameExpr}")}.asTerm))
14+
val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Object])
15+
16+
Block(List(clsDef), newCls).asExpr
17+
// '{
18+
// class `name`() { println("Constructing `name`") }
19+
// new `name`()
20+
// }
21+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@main def Test: Unit = {
2+
val foo = makeClass("foo")
3+
println(foo.getClass)
4+
val bar = makeClass("bar")
5+
println(bar.getClass)
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Calling foo.foo
2+
class Test_2$package$foo$1
3+
Calling bar.foo
4+
class Test_2$package$bar$1
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import scala.quoted.*
2+
3+
inline def makeClass(inline name: String): Foo = ${ makeClassExpr('name) }
4+
private def makeClassExpr(nameExpr: Expr[String])(using Quotes): Expr[Foo] = {
5+
import quotes.reflect.*
6+
7+
val name = nameExpr.valueOrAbort
8+
val parents = List(TypeTree.of[Object], TypeTree.of[Foo])
9+
def decls(cls: Symbol): List[Symbol] =
10+
List(Symbol.newMethod(cls, "foo", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit])))
11+
12+
val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = parents.map(_.tpe), decls, selfInfo = None)
13+
val fooSym = cls.declaredMethod("foo").head
14+
15+
val fooDef = DefDef(fooSym, argss => Some('{println(s"Calling ${$nameExpr}.foo")}.asTerm))
16+
val clsDef = ClassDef(cls, parents, body = List(fooDef))
17+
val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Foo])
18+
19+
Block(List(clsDef), newCls).asExprOf[Foo]
20+
21+
// '{
22+
// class `name`() extends Object, Foo {
23+
// def foo(): Unit = println("Calling `name`.foo")
24+
// }
25+
// new `name`()
26+
// }
27+
}
28+
29+
trait Foo {
30+
def foo(): Unit
31+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@main def Test: Unit = {
2+
val foo: Foo = makeClass("foo")
3+
foo.foo()
4+
println(foo.getClass)
5+
val bar: Foo = makeClass("bar")
6+
bar.foo()
7+
println(bar.getClass)
8+
}

tests/run-macros/newClassSelf.check

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Calling Bar.bar
2+
Calling Foo.foo
3+
Calling Bar.bar
4+
class Test_2$package$A$1
5+
Calling Bar.bar
6+
Calling Foo.foo
7+
Calling Bar.bar
8+
class Test_2$package$B$1
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import scala.quoted.*
2+
3+
inline def makeClass(inline name: String): Bar = ${ makeClassExpr('name) }
4+
private def makeClassExpr(nameExpr: Expr[String])(using Quotes): Expr[Bar] = {
5+
import quotes.reflect.*
6+
val name = nameExpr.valueOrAbort
7+
val fooDef = makeFoo()
8+
val fooBarDef = makeFooBar(name, fooDef.symbol)
9+
val newCls = makeNewFooBar(fooBarDef.symbol)
10+
11+
Block(List(fooDef, fooBarDef), newCls).asExprOf[Bar]
12+
// '{
13+
// class Foo { self: Bar =>
14+
// def foo(): Unit = bar()
15+
// }
16+
// class `name`() extends Foo with Bar
17+
// new `name`()
18+
// }
19+
}
20+
21+
/** Generate
22+
* ```
23+
* class Foo { self: Bar =>
24+
* def foo(): Unit = bar()
25+
* }
26+
* ```
27+
*/
28+
def makeFoo(using Quotes)(): quotes.reflect.ClassDef = {
29+
import quotes.reflect.*
30+
val parents = List(TypeTree.of[Object])
31+
def decls(cls: Symbol): List[Symbol] =
32+
List(Symbol.newMethod(cls, "foo", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit])))
33+
34+
val cls = Symbol.newClass(Symbol.spliceOwner, "Foo", parents = parents.map(_.tpe), decls, selfInfo = Some(TypeRepr.of[Bar]))
35+
val fooSym = cls.declaredMethod("foo").head
36+
val barSym = Symbol.classSymbol("Bar").declaredMethod("bar").head
37+
38+
def fooRhs(args: List[List[Tree]]): Option[Term] =
39+
val barCall = This(cls).select(barSym).appliedToNone.asExprOf[Unit]
40+
Some('{ println("Calling Foo.foo"); $barCall }.asTerm)
41+
42+
val fooDef = DefDef(fooSym, fooRhs)
43+
ClassDef(cls, parents, body = List(fooDef))
44+
}
45+
46+
/** Generate
47+
* ```
48+
* class `name`() extends Foo with Bar
49+
* ```
50+
*/
51+
def makeFooBar(using Quotes)(name: String, fooCls: quotes.reflect.Symbol): quotes.reflect.ClassDef = {
52+
import quotes.reflect.*
53+
val parents = List(Inferred(fooCls.typeRef), TypeTree.of[Bar])
54+
def decls(cls: Symbol): List[Symbol] = Nil
55+
val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = parents.map(_.tpe), decls, selfInfo = None)
56+
ClassDef(cls, parents, body = Nil)
57+
}
58+
59+
/** Generate
60+
* ```
61+
* new `name`()
62+
* ```
63+
*/
64+
def makeNewFooBar(using Quotes)(fooBarCls: quotes.reflect.Symbol): quotes.reflect.Term = {
65+
import quotes.reflect.*
66+
Typed(Apply(Select(New(TypeIdent(fooBarCls)), fooBarCls.primaryConstructor), Nil), TypeTree.of[Bar])
67+
}
68+
69+
trait Bar {
70+
def bar(): Unit = println("Calling Bar.bar")
71+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@main def Test: Unit = {
2+
val a: Bar = makeClass("A")
3+
a.bar()
4+
callFoo(a)
5+
println(a.getClass)
6+
val b: Bar = makeClass("B")
7+
b.bar()
8+
callFoo(b)
9+
println(b.getClass)
10+
}
11+
12+
def callFoo(x: Any): Unit =
13+
x.getClass.getMethod("foo").invoke(x)

0 commit comments

Comments
 (0)