@@ -464,6 +464,13 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
464
464
465
465
/** Methods of the module object `val ClassDef` */
466
466
trait ClassDefModule { this : ClassDef .type =>
467
+ /** Create a class definition tree
468
+ *
469
+ * @param cls The class symbol
470
+ * @param parents The parents trees class. The trees must align with the parent types of `cls`.
471
+ * @param body List of members of the class. The members must align with the members of `cls`.
472
+ */
473
+ @ experimental def apply (cls : Symbol , parents : List [Tree /* Term | TypeTree */ ], body : List [Statement ]): ClassDef
467
474
def copy (original : Tree )(name : String , constr : DefDef , parents : List [Tree /* Term | TypeTree */ ], selfOpt : Option [ValDef ], body : List [Statement ]): ClassDef
468
475
def unapply (cdef : ClassDef ): (String , DefDef , List [Tree /* Term | TypeTree */ ], Option [ValDef ], List [Statement ])
469
476
}
@@ -3533,19 +3540,73 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3533
3540
/** The class Symbol of a global class definition */
3534
3541
def classSymbol (fullName : String ): Symbol
3535
3542
3543
+ /** Generates a new class symbol for a class with a parameterless constructor.
3544
+ *
3545
+ * Example usage:
3546
+ * ```scala
3547
+ * val name: String = ...
3548
+ * val parents = List(TypeTree.of[Object], TypeTree.of[Foo])
3549
+ * def decls(cls: Symbol): List[Symbol] =
3550
+ * List(Symbol.newMethod(cls, "foo", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit])))
3551
+ *
3552
+ * val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = parents.map(_.tpe), decls, selfInfo = None)
3553
+ * val fooSym = cls.declaredMethod("foo").head
3554
+ *
3555
+ * val fooDef = DefDef(fooSym, argss => Some('{println(s"Calling foo")}.asTerm))
3556
+ * val clsDef = ClassDef(cls, parents, body = List(fooDef))
3557
+ * val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Foo])
3558
+ *
3559
+ * Block(List(clsDef), newCls).asExprOf[Foo]
3560
+ * ```
3561
+ * constructs the equivalent to
3562
+ * ```scala
3563
+ * '{
3564
+ * class `name`() extends Object with Foo {
3565
+ * def foo(): Unit = println("Calling foo")
3566
+ * }
3567
+ * new `name`(): Foo
3568
+ * }
3569
+ * ```
3570
+ *
3571
+ * @param parent The owner of the class
3572
+ * @param name The name of the class
3573
+ * @param parents The parent classes of the class. The first parent must not be a trait.
3574
+ * @param decls The member declarations of the class provided the symbol of this class
3575
+ * @param selfType The self type of the class if it has one
3576
+ *
3577
+ * This symbol starts without an accompanying definition.
3578
+ * It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3579
+ * this symbol to the ClassDef constructor.
3580
+ *
3581
+ * @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3582
+ * direct or indirect children of the reflection context's owner.
3583
+ */
3584
+ @ experimental def newClass (parent : Symbol , name : String , parents : List [TypeRepr ], decls : Symbol => List [Symbol ], selfType : Option [TypeRepr ]): Symbol
3585
+
3536
3586
/** Generates a new method symbol with the given parent, name and type.
3537
- *
3538
- * This symbol starts without an accompanying definition.
3539
- * It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3540
- * this symbol to the DefDef constructor.
3541
- *
3542
- * @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3543
- * direct or indirect children of the reflection context's owner.
3544
- */
3587
+ *
3588
+ * To define a member method of a class, use the `newMethod` within the `decls` function of `newClass`.
3589
+ *
3590
+ * @param parent The owner of the method
3591
+ * @param name The name of the method
3592
+ * @param tpe The type of the method (MethodType, PolyType, ByNameType)
3593
+ *
3594
+ * This symbol starts without an accompanying definition.
3595
+ * It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3596
+ * this symbol to the DefDef constructor.
3597
+ *
3598
+ * @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3599
+ * direct or indirect children of the reflection context's owner.
3600
+ */
3545
3601
def newMethod (parent : Symbol , name : String , tpe : TypeRepr ): Symbol
3546
3602
3547
3603
/** Works as the other newMethod, but with additional parameters.
3548
3604
*
3605
+ * To define a member method of a class, use the `newMethod` within the `decls` function of `newClass`.
3606
+ *
3607
+ * @param parent The owner of the method
3608
+ * @param name The name of the method
3609
+ * @param tpe The type of the method (MethodType, PolyType, ByNameType)
3549
3610
* @param flags extra flags to with which the symbol should be constructed
3550
3611
* @param privateWithin the symbol within which this new method symbol should be private. May be noSymbol.
3551
3612
*/
@@ -3559,6 +3620,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3559
3620
*
3560
3621
* Note: Also see reflect.let
3561
3622
*
3623
+ * @param parent The owner of the /var/lazy val
3624
+ * @param name The name of the val/var/lazy val
3625
+ * @param tpe The type of the val/var/lazy val
3562
3626
* @param flags extra flags to with which the symbol should be constructed
3563
3627
* @param privateWithin the symbol within which this new method symbol should be private. May be noSymbol.
3564
3628
* @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
@@ -3572,7 +3636,10 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3572
3636
* It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3573
3637
* this symbol to the BindDef constructor.
3574
3638
*
3639
+ * @param parent The owner of the binding
3640
+ * @param name The name of the binding
3575
3641
* @param flags extra flags to with which the symbol should be constructed
3642
+ * @param tpe The type of the binding
3576
3643
* @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3577
3644
* direct or indirect children of the reflection context's owner.
3578
3645
*/
@@ -3807,6 +3874,14 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3807
3874
3808
3875
/** Case class or case object children of a sealed trait or cases of an `enum`. */
3809
3876
def children : List [Symbol ]
3877
+
3878
+ /** Type reference to the symbol usable in the scope of its owner. */
3879
+ @ experimental
3880
+ def typeRef : TypeRef
3881
+
3882
+ /** Term reference to the symbol usable in the scope of its owner. */
3883
+ @ experimental
3884
+ def termRef : TermRef
3810
3885
end extension
3811
3886
}
3812
3887
0 commit comments