Skip to content

Commit 144215d

Browse files
committed
wip
1 parent 270d8b6 commit 144215d

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-2
lines changed

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,11 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
15971597
&& level == 0
15981598
&& !hasInliningErrors =>
15991599
val expanded = expandMacro(res.args.head, tree.srcPos)
1600+
println(" ")
1601+
println(expanded)
1602+
println(expanded.show)
1603+
println(" ")
1604+
println(" ")
16001605
typedExpr(expanded) // Inline calls and constant fold code generated by the macro
16011606
case res =>
16021607
specializeEq(inlineIfNeeded(res))

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
229229

230230
object ClassDef extends ClassDefModule:
231231
def apply(cls: Symbol, constr: DefDef, parents: List[Tree], body: List[Statement]): ClassDef =
232-
tpd.ClassDefWithParents(cls, constr, parents, body)
232+
tpd.ClassDefWithParents(cls.asClass, constr, parents, body)
233233

234234
def copy(original: Tree)(name: String, constr: DefDef, parents: List[Tree], selfOpt: Option[ValDef], body: List[Statement]): ClassDef = {
235235
val dotc.ast.Trees.TypeDef(_, originalImpl: tpd.Template) = original
@@ -263,6 +263,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
263263

264264
object DefDef extends DefDefModule:
265265
def apply(symbol: Symbol, rhsFn: List[List[Tree]] => Option[Term]): DefDef =
266+
assert(symbol.isTerm, s"expected a term symbol but received $symbol")
266267
withDefaultPos(tpd.DefDef(symbol.asTerm, prefss =>
267268
xCheckMacroedOwners(xCheckMacroValidExpr(rhsFn(prefss)), symbol).getOrElse(tpd.EmptyTree)
268269
))
@@ -1807,7 +1808,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
18071808
(x.prefix, x.name.toString)
18081809
end TermRef
18091810

1810-
type TypeRef = dotc.core.Types.NamedType
1811+
type TypeRef = dotc.core.Types.TypeRef
18111812

18121813
object TypeRefTypeTest extends TypeTest[TypeRepr, TypeRef]:
18131814
def unapply(x: TypeRepr): Option[TypeRef & x.type] = x match
@@ -2457,6 +2458,20 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
24572458
def requiredModule(path: String): Symbol = dotc.core.Symbols.requiredModule(path)
24582459
def requiredMethod(path: String): Symbol = dotc.core.Symbols.requiredMethod(path)
24592460
def classSymbol(fullName: String): Symbol = dotc.core.Symbols.requiredClass(fullName)
2461+
2462+
def newClass(owner: Symbol, name: String, flags: Flags, parents: List[TypeRepr], decls: List[Symbol], selfInfo: Option[TypeRepr], privateWithin: Symbol): Symbol =
2463+
dotc.core.Symbols.newNormalizedClassSymbol(
2464+
owner,
2465+
name.toTypeName,
2466+
flags,
2467+
parents,
2468+
dotc.core.Scopes.newScopeWith(decls*),
2469+
selfInfo.getOrElse(Types.NoType),
2470+
privateWithin)
2471+
2472+
def newPrimaryConstructor(cls: Symbol, tpe: TypeRepr): Symbol =
2473+
dotc.core.Symbols.newSymbol(cls, nme.CONSTRUCTOR, dotc.core.Flags.Method, tpe, noSymbol)
2474+
24602475
def newMethod(owner: Symbol, name: String, tpe: TypeRepr): Symbol =
24612476
newMethod(owner, name, tpe, Flags.EmptyFlags, noSymbol)
24622477
def newMethod(owner: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol =

library/src/scala/quoted/Quotes.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3534,6 +3534,8 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
35343534
/** The class Symbol of a global class definition */
35353535
def classSymbol(fullName: String): Symbol
35363536

3537+
def newClass(owner: Symbol, name: String, flags: Flags, parents: List[TypeRepr], decls: List[Symbol], selfInfo: Option[TypeRepr], privateWithin: Symbol): Symbol
3538+
35373539
/** Generates a new method symbol with the given parent, name and type.
35383540
*
35393541
* This symbol starts without an accompanying definition.

tests/run-macros/ClassDef_apply.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import scala.quoted.*
2+
3+
inline def makeClass(inline name: String): Any = ${ makeClassExpr('name) }
4+
private def makeClassExpr(name: Expr[String])(using Quotes): Expr[Any] = {
5+
import quotes.reflect.*
6+
7+
val cls = Symbol.newClass(Symbol.spliceOwner, name.valueOrAbort, Flags.EmptyFlags, parents = List(TypeRepr.of[Object]), decls = Nil, selfInfo = None, privateWithin = Symbol.noSymbol)
8+
9+
val constr = DefDef(cls.primaryConstructor, argss => Some('{}.asTerm))
10+
val clsDef = ClassDef(cls, constr, parents = Nil, body = Nil)
11+
val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), Inferred(TypeRepr.of[Object]))
12+
Block(List(clsDef), newCls).asExpr
13+
// '{
14+
// class `name`()
15+
// new `name`()
16+
// }
17+
}
18+
19+
// Block(List(TypeDef(foo,Template(DefDef(<init>,List(List()),
20+
// TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class scala)),class Unit)],Literal(Constant(()))),List(),
21+
// ValDef(_,EmptyTree,EmptyTree),List()))),Apply(Select(New(TypeTree[TypeRef(NoPrefix,class foo)]),<init>),List()))
22+
// Block(List(TypeDef(foo,Template(DefDef(<init>,List(List()),
23+
// TypeTree[TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),Unit)],EmptyTree),List(Apply(Select(New(TypeTree[TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class java)),object lang),Object)]),<init>),List())),
24+
// ValDef(_,EmptyTree,EmptyTree),List()))),Typed(Apply(Select(New(Ident(foo)),<init>),List()),TypeTree[TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class java)),object lang),Object)]))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@main def Test: Unit = {
2+
val x = makeClass("foo")
3+
println(x.getClass)
4+
}

0 commit comments

Comments
 (0)