@@ -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
}
@@ -2534,6 +2543,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
2534
2543
def typeSymbol : Symbol
2535
2544
def termSymbol : Symbol
2536
2545
def isSingleton : Boolean
2546
+
2547
+ /** This type seen as if it were the type of a member of prefix type `self` declared in class `member.owner`.
2548
+ *
2549
+ * Also see `typeRef` and `termRef`
2550
+ */
2537
2551
def memberType (member : Symbol ): TypeRepr
2538
2552
2539
2553
/** The base classes of this type with the class itself as first element. */
@@ -3543,19 +3557,73 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3543
3557
/** The class Symbol of a global class definition */
3544
3558
def classSymbol (fullName : String ): Symbol
3545
3559
3560
+ /** Generates a new class symbol for a class with a parameterless constructor.
3561
+ *
3562
+ * Example usage:
3563
+ * ```
3564
+ * val name: String = "myClass"
3565
+ * val parents = List(TypeTree.of[Object], TypeTree.of[Foo])
3566
+ * def decls(cls: Symbol): List[Symbol] =
3567
+ * List(Symbol.newMethod(cls, "foo", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit])))
3568
+ *
3569
+ * val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = parents.map(_.tpe), decls, selfInfo = None)
3570
+ * val fooSym = cls.declaredMethod("foo").head
3571
+ *
3572
+ * val fooDef = DefDef(fooSym, argss => Some('{println(s"Calling foo")}.asTerm))
3573
+ * val clsDef = ClassDef(cls, parents, body = List(fooDef))
3574
+ * val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Foo])
3575
+ *
3576
+ * Block(List(clsDef), newCls).asExprOf[Foo]
3577
+ * ```
3578
+ * constructs the equivalent to
3579
+ * ```
3580
+ * '{
3581
+ * class myClass() extends Object with Foo {
3582
+ * def foo(): Unit = println("Calling foo")
3583
+ * }
3584
+ * new myClass(): Foo
3585
+ * }
3586
+ * ```
3587
+ *
3588
+ * @param parent The owner of the class
3589
+ * @param name The name of the class
3590
+ * @param parents The parent classes of the class. The first parent must not be a trait.
3591
+ * @param decls The member declarations of the class provided the symbol of this class
3592
+ * @param selfType The self type of the class if it has one
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 ClassDef 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
+ */
3601
+ @ experimental def newClass (parent : Symbol , name : String , parents : List [TypeRepr ], decls : Symbol => List [Symbol ], selfType : Option [TypeRepr ]): Symbol
3602
+
3546
3603
/** 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
- */
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)
3610
+ *
3611
+ * This symbol starts without an accompanying definition.
3612
+ * It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3613
+ * this symbol to the DefDef constructor.
3614
+ *
3615
+ * @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3616
+ * direct or indirect children of the reflection context's owner.
3617
+ */
3555
3618
def newMethod (parent : Symbol , name : String , tpe : TypeRepr ): Symbol
3556
3619
3557
3620
/** Works as the other newMethod, but with additional parameters.
3558
3621
*
3622
+ * To define a member method of a class, use the `newMethod` within the `decls` function of `newClass`.
3623
+ *
3624
+ * @param parent The owner of the method
3625
+ * @param name The name of the method
3626
+ * @param tpe The type of the method (MethodType, PolyType, ByNameType)
3559
3627
* @param flags extra flags to with which the symbol should be constructed
3560
3628
* @param privateWithin the symbol within which this new method symbol should be private. May be noSymbol.
3561
3629
*/
@@ -3569,6 +3637,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3569
3637
*
3570
3638
* Note: Also see reflect.let
3571
3639
*
3640
+ * @param parent The owner of the /var/lazy val
3641
+ * @param name The name of the val/var/lazy val
3642
+ * @param tpe The type of the val/var/lazy val
3572
3643
* @param flags extra flags to with which the symbol should be constructed
3573
3644
* @param privateWithin the symbol within which this new method symbol should be private. May be noSymbol.
3574
3645
* @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
@@ -3582,7 +3653,10 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3582
3653
* It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3583
3654
* this symbol to the BindDef constructor.
3584
3655
*
3656
+ * @param parent The owner of the binding
3657
+ * @param name The name of the binding
3585
3658
* @param flags extra flags to with which the symbol should be constructed
3659
+ * @param tpe The type of the binding
3586
3660
* @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3587
3661
* direct or indirect children of the reflection context's owner.
3588
3662
*/
@@ -3644,9 +3718,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3644
3718
*
3645
3719
* symbol.tree.tpe
3646
3720
*
3647
- * It should be replaced by the following code :
3721
+ * It should be replaced by one of the following:
3648
3722
*
3649
3723
* tp.memberType(symbol)
3724
+ * symbol.typeRef
3725
+ * symbol.termRef
3650
3726
*
3651
3727
*/
3652
3728
def tree : Tree
@@ -3817,6 +3893,19 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3817
3893
3818
3894
/** Case class or case object children of a sealed trait or cases of an `enum`. */
3819
3895
def children : List [Symbol ]
3896
+
3897
+ /** Type reference to the symbol usable in the scope of its owner.
3898
+ *
3899
+ * To get a reference to a symbol from a specific prefix `tp`, use `tp.memberType(symbol)` instead.
3900
+ *
3901
+ * @pre symbol.isType returns true
3902
+ */
3903
+ @ experimental
3904
+ def typeRef : TypeRef
3905
+
3906
+ /** Term reference to the symbol usable in the scope of its owner. */
3907
+ @ experimental
3908
+ def termRef : TermRef
3820
3909
end extension
3821
3910
}
3822
3911
0 commit comments