@@ -460,6 +460,15 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
460
460
461
461
/** Methods of the module object `val ClassDef` */
462
462
trait ClassDefModule { this : ClassDef .type =>
463
+ /** Create a class definition tree
464
+ *
465
+ * @param cls The class symbol. A new class symbol can be created using `Symbol.newClass`.
466
+ * @param parents The parents trees class. The trees must align with the parent types of `cls`.
467
+ * Parents can be `TypeTree`s if they don't have term parameter,
468
+ * otherwise the can be `Term` containing the `New` applied to the parameters of the extended class.
469
+ * @param body List of members of the class. The members must align with the members of `cls`.
470
+ */
471
+ @ experimental def apply (cls : Symbol , parents : List [Tree /* Term | TypeTree */ ], body : List [Statement ]): ClassDef
463
472
def copy (original : Tree )(name : String , constr : DefDef , parents : List [Tree /* Term | TypeTree */ ], selfOpt : Option [ValDef ], body : List [Statement ]): ClassDef
464
473
def unapply (cdef : ClassDef ): (String , DefDef , List [Tree /* Term | TypeTree */ ], Option [ValDef ], List [Statement ])
465
474
}
@@ -1719,6 +1728,13 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
1719
1728
trait TypeTreeModule { this : TypeTree .type =>
1720
1729
/** Returns the tree of type or kind (TypeTree) of T */
1721
1730
def of [T <: AnyKind ](using Type [T ]): TypeTree
1731
+
1732
+ /** Returns a type tree reference to the symbol
1733
+ *
1734
+ * @param sym The type symbol for which we are creating a type tree reference.
1735
+ */
1736
+ @ experimental
1737
+ def ref (typeSymbol : Symbol ): TypeTree
1722
1738
}
1723
1739
1724
1740
/** Makes extension methods on `TypeTree` available without any imports */
@@ -2571,6 +2587,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
2571
2587
def typeSymbol : Symbol
2572
2588
def termSymbol : Symbol
2573
2589
def isSingleton : Boolean
2590
+
2591
+ /** The type of `member` as seen from prefix `self`.
2592
+ *
2593
+ * Also see `typeRef` and `termRef`
2594
+ */
2574
2595
def memberType (member : Symbol ): TypeRepr
2575
2596
2576
2597
/** The base classes of this type with the class itself as first element. */
@@ -3578,19 +3599,73 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3578
3599
/** The class Symbol of a global class definition */
3579
3600
def classSymbol (fullName : String ): Symbol
3580
3601
3602
+ /** Generates a new class symbol for a class with a parameterless constructor.
3603
+ *
3604
+ * Example usage:
3605
+ * ```
3606
+ * val name: String = "myClass"
3607
+ * val parents = List(TypeTree.of[Object], TypeTree.of[Foo])
3608
+ * def decls(cls: Symbol): List[Symbol] =
3609
+ * List(Symbol.newMethod(cls, "foo", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit])))
3610
+ *
3611
+ * val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = parents.map(_.tpe), decls, selfInfo = None)
3612
+ * val fooSym = cls.declaredMethod("foo").head
3613
+ *
3614
+ * val fooDef = DefDef(fooSym, argss => Some('{println(s"Calling foo")}.asTerm))
3615
+ * val clsDef = ClassDef(cls, parents, body = List(fooDef))
3616
+ * val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Foo])
3617
+ *
3618
+ * Block(List(clsDef), newCls).asExprOf[Foo]
3619
+ * ```
3620
+ * constructs the equivalent to
3621
+ * ```
3622
+ * '{
3623
+ * class myClass() extends Object with Foo {
3624
+ * def foo(): Unit = println("Calling foo")
3625
+ * }
3626
+ * new myClass(): Foo
3627
+ * }
3628
+ * ```
3629
+ *
3630
+ * @param parent The owner of the class
3631
+ * @param name The name of the class
3632
+ * @param parents The parent classes of the class. The first parent must not be a trait.
3633
+ * @param decls The member declarations of the class provided the symbol of this class
3634
+ * @param selfType The self type of the class if it has one
3635
+ *
3636
+ * This symbol starts without an accompanying definition.
3637
+ * It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3638
+ * this symbol to the ClassDef constructor.
3639
+ *
3640
+ * @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3641
+ * direct or indirect children of the reflection context's owner.
3642
+ */
3643
+ @ experimental def newClass (parent : Symbol , name : String , parents : List [TypeRepr ], decls : Symbol => List [Symbol ], selfType : Option [TypeRepr ]): Symbol
3644
+
3581
3645
/** Generates a new method symbol with the given parent, name and type.
3582
- *
3583
- * This symbol starts without an accompanying definition.
3584
- * It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3585
- * this symbol to the DefDef constructor.
3586
- *
3587
- * @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3588
- * direct or indirect children of the reflection context's owner.
3589
- */
3646
+ *
3647
+ * To define a member method of a class, use the `newMethod` within the `decls` function of `newClass`.
3648
+ *
3649
+ * @param parent The owner of the method
3650
+ * @param name The name of the method
3651
+ * @param tpe The type of the method (MethodType, PolyType, ByNameType)
3652
+ *
3653
+ * This symbol starts without an accompanying definition.
3654
+ * It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3655
+ * this symbol to the DefDef constructor.
3656
+ *
3657
+ * @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3658
+ * direct or indirect children of the reflection context's owner.
3659
+ */
3590
3660
def newMethod (parent : Symbol , name : String , tpe : TypeRepr ): Symbol
3591
3661
3592
3662
/** Works as the other newMethod, but with additional parameters.
3593
3663
*
3664
+ * To define a member method of a class, use the `newMethod` within the `decls` function of `newClass`.
3665
+ *
3666
+ * @param parent The owner of the method
3667
+ * @param name The name of the method
3668
+ * @param tpe The type of the method (MethodType, PolyType, ByNameType)
3594
3669
* @param flags extra flags to with which the symbol should be constructed
3595
3670
* @param privateWithin the symbol within which this new method symbol should be private. May be noSymbol.
3596
3671
*/
@@ -3604,6 +3679,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3604
3679
*
3605
3680
* Note: Also see reflect.let
3606
3681
*
3682
+ * @param parent The owner of the val/var/lazy val
3683
+ * @param name The name of the val/var/lazy val
3684
+ * @param tpe The type of the val/var/lazy val
3607
3685
* @param flags extra flags to with which the symbol should be constructed
3608
3686
* @param privateWithin the symbol within which this new method symbol should be private. May be noSymbol.
3609
3687
* @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
@@ -3617,7 +3695,10 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3617
3695
* It is the meta-programmer's responsibility to provide exactly one corresponding definition by passing
3618
3696
* this symbol to the BindDef constructor.
3619
3697
*
3698
+ * @param parent The owner of the binding
3699
+ * @param name The name of the binding
3620
3700
* @param flags extra flags to with which the symbol should be constructed
3701
+ * @param tpe The type of the binding
3621
3702
* @note As a macro can only splice code into the point at which it is expanded, all generated symbols must be
3622
3703
* direct or indirect children of the reflection context's owner.
3623
3704
*/
@@ -3679,9 +3760,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3679
3760
*
3680
3761
* symbol.tree.tpe
3681
3762
*
3682
- * It should be replaced by the following code :
3763
+ * It should be replaced by one of the following:
3683
3764
*
3684
3765
* tp.memberType(symbol)
3766
+ * symbol.typeRef
3767
+ * symbol.termRef
3685
3768
*
3686
3769
*/
3687
3770
def tree : Tree
@@ -3880,6 +3963,19 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3880
3963
@ experimental
3881
3964
def asQuotes : Nested
3882
3965
3966
+ /** Type reference to the symbol usable in the scope of its owner.
3967
+ *
3968
+ * To get a reference to a symbol from a specific prefix `tp`, use `tp.select(symbol)` instead.
3969
+ * @see TypeReprMethods.select
3970
+ *
3971
+ * @pre symbol.isType returns true
3972
+ */
3973
+ @ experimental
3974
+ def typeRef : TypeRef
3975
+
3976
+ /** Term reference to the symbol usable in the scope of its owner. */
3977
+ @ experimental
3978
+ def termRef : TermRef
3883
3979
end extension
3884
3980
}
3885
3981
0 commit comments