@@ -464,6 +464,15 @@ 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. A new class symbol can be created using `Symbol.newClass`.
470
+ * @param parents The parents trees class. The trees must align with the parent types of `cls`.
471
+ * Parents can be `TypeTree`s if they don't have term parameter,
472
+ * otherwise the can be `Term` containing the `New` applied to the parameters of the extended class.
473
+ * @param body List of members of the class. The members must align with the members of `cls`.
474
+ */
475
+ @ experimental def apply (cls : Symbol , parents : List [Tree /* Term | TypeTree */ ], body : List [Statement ]): ClassDef
467
476
def copy (original : Tree )(name : String , constr : DefDef , parents : List [Tree /* Term | TypeTree */ ], selfOpt : Option [ValDef ], body : List [Statement ]): ClassDef
468
477
def unapply (cdef : ClassDef ): (String , DefDef , List [Tree /* Term | TypeTree */ ], Option [ValDef ], List [Statement ])
469
478
}
@@ -1680,6 +1689,13 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
1680
1689
trait TypeTreeModule { this : TypeTree .type =>
1681
1690
/** Returns the tree of type or kind (TypeTree) of T */
1682
1691
def of [T <: AnyKind ](using Type [T ]): TypeTree
1692
+
1693
+ /** Returns a type tree reference to the symbol
1694
+ *
1695
+ * @param sym The type symbol for which we are creating a type tree reference.
1696
+ */
1697
+ @ experimental
1698
+ def apply (typeSymbol : Symbol ): TypeTree
1683
1699
}
1684
1700
1685
1701
/** Makes extension methods on `TypeTree` available without any imports */
@@ -2534,6 +2550,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
2534
2550
def typeSymbol : Symbol
2535
2551
def termSymbol : Symbol
2536
2552
def isSingleton : Boolean
2553
+
2554
+ /** The type of `member` as seen from prefix `self`.
2555
+ *
2556
+ * Also see `typeRef` and `termRef`
2557
+ */
2537
2558
def memberType (member : Symbol ): TypeRepr
2538
2559
2539
2560
/** The base classes of this type with the class itself as first element. */
@@ -3543,19 +3564,73 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3543
3564
/** The class Symbol of a global class definition */
3544
3565
def classSymbol (fullName : String ): Symbol
3545
3566
3567
+ /** Generates a new class symbol for a class with a parameterless constructor.
3568
+ *
3569
+ * Example usage:
3570
+ * ```
3571
+ * val name: String = "myClass"
3572
+ * val parents = List(TypeTree.of[Object], TypeTree.of[Foo])
3573
+ * def decls(cls: Symbol): List[Symbol] =
3574
+ * List(Symbol.newMethod(cls, "foo", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit])))
3575
+ *
3576
+ * val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = parents.map(_.tpe), decls, selfInfo = None)
3577
+ * val fooSym = cls.declaredMethod("foo").head
3578
+ *
3579
+ * val fooDef = DefDef(fooSym, argss => Some('{println(s"Calling foo")}.asTerm))
3580
+ * val clsDef = ClassDef(cls, parents, body = List(fooDef))
3581
+ * val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Foo])
3582
+ *
3583
+ * Block(List(clsDef), newCls).asExprOf[Foo]
3584
+ * ```
3585
+ * constructs the equivalent to
3586
+ * ```
3587
+ * '{
3588
+ * class myClass() extends Object with Foo {
3589
+ * def foo(): Unit = println("Calling foo")
3590
+ * }
3591
+ * new myClass(): Foo
3592
+ * }
3593
+ * ```
3594
+ *
3595
+ * @param parent The owner of the class
3596
+ * @param name The name of the class
3597
+ * @param parents The parent classes of the class. The first parent must not be a trait.
3598
+ * @param decls The member declarations of the class provided the symbol of this class
3599
+ * @param selfType The self type of the class if it has one
3600
+ *
3601
+ * This symbol starts without an accompanying definition.
3602
+ * It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3603
+ * this symbol to the ClassDef constructor.
3604
+ *
3605
+ * @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3606
+ * direct or indirect children of the reflection context's owner.
3607
+ */
3608
+ @ experimental def newClass (parent : Symbol , name : String , parents : List [TypeRepr ], decls : Symbol => List [Symbol ], selfType : Option [TypeRepr ]): Symbol
3609
+
3546
3610
/** Generates a new method symbol with the given parent, name and type.
3547
- *
3548
- * This symbol starts without an accompanying definition.
3549
- * It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3550
- * this symbol to the DefDef constructor.
3551
- *
3552
- * @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3553
- * direct or indirect children of the reflection context's owner.
3554
- */
3611
+ *
3612
+ * To define a member method of a class, use the `newMethod` within the `decls` function of `newClass`.
3613
+ *
3614
+ * @param parent The owner of the method
3615
+ * @param name The name of the method
3616
+ * @param tpe The type of the method (MethodType, PolyType, ByNameType)
3617
+ *
3618
+ * This symbol starts without an accompanying definition.
3619
+ * It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3620
+ * this symbol to the DefDef constructor.
3621
+ *
3622
+ * @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3623
+ * direct or indirect children of the reflection context's owner.
3624
+ */
3555
3625
def newMethod (parent : Symbol , name : String , tpe : TypeRepr ): Symbol
3556
3626
3557
3627
/** Works as the other newMethod, but with additional parameters.
3558
3628
*
3629
+ * To define a member method of a class, use the `newMethod` within the `decls` function of `newClass`.
3630
+ *
3631
+ * @param parent The owner of the method
3632
+ * @param name The name of the method
3633
+ * @param tpe The type of the method (MethodType, PolyType, ByNameType)
3559
3634
* @param flags extra flags to with which the symbol should be constructed
3560
3635
* @param privateWithin the symbol within which this new method symbol should be private. May be noSymbol.
3561
3636
*/
@@ -3569,6 +3644,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3569
3644
*
3570
3645
* Note: Also see reflect.let
3571
3646
*
3647
+ * @param parent The owner of the /var/lazy val
3648
+ * @param name The name of the val/var/lazy val
3649
+ * @param tpe The type of the val/var/lazy val
3572
3650
* @param flags extra flags to with which the symbol should be constructed
3573
3651
* @param privateWithin the symbol within which this new method symbol should be private. May be noSymbol.
3574
3652
* @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
@@ -3582,7 +3660,10 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3582
3660
* It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3583
3661
* this symbol to the BindDef constructor.
3584
3662
*
3663
+ * @param parent The owner of the binding
3664
+ * @param name The name of the binding
3585
3665
* @param flags extra flags to with which the symbol should be constructed
3666
+ * @param tpe The type of the binding
3586
3667
* @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3587
3668
* direct or indirect children of the reflection context's owner.
3588
3669
*/
@@ -3644,9 +3725,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3644
3725
*
3645
3726
* symbol.tree.tpe
3646
3727
*
3647
- * It should be replaced by the following code :
3728
+ * It should be replaced by one of the following:
3648
3729
*
3649
3730
* tp.memberType(symbol)
3731
+ * symbol.typeRef
3732
+ * symbol.termRef
3650
3733
*
3651
3734
*/
3652
3735
def tree : Tree
@@ -3817,6 +3900,19 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3817
3900
3818
3901
/** Case class or case object children of a sealed trait or cases of an `enum`. */
3819
3902
def children : List [Symbol ]
3903
+
3904
+ /** Type reference to the symbol usable in the scope of its owner.
3905
+ *
3906
+ * To get a reference to a symbol from a specific prefix `tp`, use `tp.memberType(symbol)` instead.
3907
+ *
3908
+ * @pre symbol.isType returns true
3909
+ */
3910
+ @ experimental
3911
+ def typeRef : TypeRef
3912
+
3913
+ /** Term reference to the symbol usable in the scope of its owner. */
3914
+ @ experimental
3915
+ def termRef : TermRef
3820
3916
end extension
3821
3917
}
3822
3918
0 commit comments