@@ -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
}
@@ -1737,6 +1746,13 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
1737
1746
trait TypeTreeModule { this : TypeTree .type =>
1738
1747
/** Returns the tree of type or kind (TypeTree) of T */
1739
1748
def of [T <: AnyKind ](using Type [T ]): TypeTree
1749
+
1750
+ /** Returns a type tree reference to the symbol
1751
+ *
1752
+ * @param sym The type symbol for which we are creating a type tree reference.
1753
+ */
1754
+ @ experimental
1755
+ def ref (typeSymbol : Symbol ): TypeTree
1740
1756
}
1741
1757
1742
1758
/** Makes extension methods on `TypeTree` available without any imports */
@@ -2591,6 +2607,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
2591
2607
def typeSymbol : Symbol
2592
2608
def termSymbol : Symbol
2593
2609
def isSingleton : Boolean
2610
+
2611
+ /** The type of `member` as seen from prefix `self`.
2612
+ *
2613
+ * Also see `typeRef` and `termRef`
2614
+ */
2594
2615
def memberType (member : Symbol ): TypeRepr
2595
2616
2596
2617
/** The base classes of this type with the class itself as first element. */
@@ -3600,19 +3621,73 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3600
3621
/** The class Symbol of a global class definition */
3601
3622
def classSymbol (fullName : String ): Symbol
3602
3623
3624
+ /** Generates a new class symbol for a class with a parameterless constructor.
3625
+ *
3626
+ * Example usage:
3627
+ * ```
3628
+ * val name: String = "myClass"
3629
+ * val parents = List(TypeTree.of[Object], TypeTree.of[Foo])
3630
+ * def decls(cls: Symbol): List[Symbol] =
3631
+ * List(Symbol.newMethod(cls, "foo", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit])))
3632
+ *
3633
+ * val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = parents.map(_.tpe), decls, selfInfo = None)
3634
+ * val fooSym = cls.declaredMethod("foo").head
3635
+ *
3636
+ * val fooDef = DefDef(fooSym, argss => Some('{println(s"Calling foo")}.asTerm))
3637
+ * val clsDef = ClassDef(cls, parents, body = List(fooDef))
3638
+ * val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Foo])
3639
+ *
3640
+ * Block(List(clsDef), newCls).asExprOf[Foo]
3641
+ * ```
3642
+ * constructs the equivalent to
3643
+ * ```
3644
+ * '{
3645
+ * class myClass() extends Object with Foo {
3646
+ * def foo(): Unit = println("Calling foo")
3647
+ * }
3648
+ * new myClass(): Foo
3649
+ * }
3650
+ * ```
3651
+ *
3652
+ * @param parent The owner of the class
3653
+ * @param name The name of the class
3654
+ * @param parents The parent classes of the class. The first parent must not be a trait.
3655
+ * @param decls The member declarations of the class provided the symbol of this class
3656
+ * @param selfType The self type of the class if it has one
3657
+ *
3658
+ * This symbol starts without an accompanying definition.
3659
+ * It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3660
+ * this symbol to the ClassDef constructor.
3661
+ *
3662
+ * @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3663
+ * direct or indirect children of the reflection context's owner.
3664
+ */
3665
+ @ experimental def newClass (parent : Symbol , name : String , parents : List [TypeRepr ], decls : Symbol => List [Symbol ], selfType : Option [TypeRepr ]): Symbol
3666
+
3603
3667
/** Generates a new method symbol with the given parent, name and type.
3604
- *
3605
- * This symbol starts without an accompanying definition.
3606
- * It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3607
- * this symbol to the DefDef constructor.
3608
- *
3609
- * @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3610
- * direct or indirect children of the reflection context's owner.
3611
- */
3668
+ *
3669
+ * To define a member method of a class, use the `newMethod` within the `decls` function of `newClass`.
3670
+ *
3671
+ * @param parent The owner of the method
3672
+ * @param name The name of the method
3673
+ * @param tpe The type of the method (MethodType, PolyType, ByNameType)
3674
+ *
3675
+ * This symbol starts without an accompanying definition.
3676
+ * It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3677
+ * this symbol to the DefDef constructor.
3678
+ *
3679
+ * @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3680
+ * direct or indirect children of the reflection context's owner.
3681
+ */
3612
3682
def newMethod (parent : Symbol , name : String , tpe : TypeRepr ): Symbol
3613
3683
3614
3684
/** Works as the other newMethod, but with additional parameters.
3615
3685
*
3686
+ * To define a member method of a class, use the `newMethod` within the `decls` function of `newClass`.
3687
+ *
3688
+ * @param parent The owner of the method
3689
+ * @param name The name of the method
3690
+ * @param tpe The type of the method (MethodType, PolyType, ByNameType)
3616
3691
* @param flags extra flags to with which the symbol should be constructed
3617
3692
* @param privateWithin the symbol within which this new method symbol should be private. May be noSymbol.
3618
3693
*/
@@ -3626,6 +3701,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3626
3701
*
3627
3702
* Note: Also see reflect.let
3628
3703
*
3704
+ * @param parent The owner of the /var/lazy val
3705
+ * @param name The name of the val/var/lazy val
3706
+ * @param tpe The type of the val/var/lazy val
3629
3707
* @param flags extra flags to with which the symbol should be constructed
3630
3708
* @param privateWithin the symbol within which this new method symbol should be private. May be noSymbol.
3631
3709
* @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
@@ -3639,7 +3717,10 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3639
3717
* It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3640
3718
* this symbol to the BindDef constructor.
3641
3719
*
3720
+ * @param parent The owner of the binding
3721
+ * @param name The name of the binding
3642
3722
* @param flags extra flags to with which the symbol should be constructed
3723
+ * @param tpe The type of the binding
3643
3724
* @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3644
3725
* direct or indirect children of the reflection context's owner.
3645
3726
*/
@@ -3701,9 +3782,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3701
3782
*
3702
3783
* symbol.tree.tpe
3703
3784
*
3704
- * It should be replaced by the following code :
3785
+ * It should be replaced by one of the following:
3705
3786
*
3706
3787
* tp.memberType(symbol)
3788
+ * symbol.typeRef
3789
+ * symbol.termRef
3707
3790
*
3708
3791
*/
3709
3792
def tree : Tree
@@ -3903,6 +3986,19 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3903
3986
@ experimental
3904
3987
def asQuotes : Nested
3905
3988
3989
+ /** Type reference to the symbol usable in the scope of its owner.
3990
+ *
3991
+ * To get a reference to a symbol from a specific prefix `tp`, use `tp.select(symbol)` instead.
3992
+ * @see TypeReprMethods.select
3993
+ *
3994
+ * @pre symbol.isType returns true
3995
+ */
3996
+ @ experimental
3997
+ def typeRef : TypeRef
3998
+
3999
+ /** Term reference to the symbol usable in the scope of its owner. */
4000
+ @ experimental
4001
+ def termRef : TermRef
3906
4002
end extension
3907
4003
}
3908
4004
0 commit comments