|
| 1 | +// Port of https://github.com/liufengyun/gestalt/blob/master/macros/src/main/scala/gestalt/macros/TypeToolbox.scala |
| 2 | +// using staging reflection |
| 3 | + |
| 4 | +import scala.quoted._ |
| 5 | +import scala.tasty._ |
| 6 | + |
| 7 | +object TypeToolbox { |
| 8 | + /** are the two types equal? */ |
| 9 | + inline def =:=[A, B]: Boolean = ~tpEqImpl('[A], '[B]) |
| 10 | + private def tpEqImpl[A, B](a: Type[A], b: Type[B])(implicit reflect: Reflection): Expr[Boolean] = { |
| 11 | + import reflect._ |
| 12 | + val res = a.reflect.tpe =:= b.reflect.tpe |
| 13 | + res.toExpr |
| 14 | + } |
| 15 | + |
| 16 | + /** is `tp1` a subtype of `tp2` */ |
| 17 | + inline def <:<[A, B]: Boolean = ~tpLEqImpl('[A], '[B]) |
| 18 | + private def tpLEqImpl[A, B](a: Type[A], b: Type[B])(implicit reflect: Reflection): Expr[Boolean] = { |
| 19 | + import reflect._ |
| 20 | + val res = a.reflect.tpe <:< b.reflect.tpe |
| 21 | + res.toExpr |
| 22 | + } |
| 23 | + |
| 24 | + /** type associated with the tree */ |
| 25 | + inline def typeOf[T, Expected](a: T): Boolean = ~typeOfImpl('(a), '[Expected]) |
| 26 | + private def typeOfImpl(a: Expr[_], expected: Type[_])(implicit reflect: Reflection): Expr[Boolean] = { |
| 27 | + import reflect._ |
| 28 | + val res = a.reflect.tpe =:= expected.reflect.tpe |
| 29 | + res.toExpr |
| 30 | + } |
| 31 | + |
| 32 | + /** does the type refer to a case class? */ |
| 33 | + inline def isCaseClass[A]: Boolean = ~isCaseClassImpl('[A]) |
| 34 | + private def isCaseClassImpl(tp: Type[_])(implicit reflect: Reflection): Expr[Boolean] = { |
| 35 | + import reflect._ |
| 36 | + val res = tp.reflect.symbol match { |
| 37 | + case IsClassSymbol(sym) => sym.flags.isCase |
| 38 | + case _ => false |
| 39 | + } |
| 40 | + res.toExpr |
| 41 | + } |
| 42 | + |
| 43 | + /** val fields of a case class Type -- only the ones declared in primary constructor */ |
| 44 | + inline def caseFields[T]: List[String] = ~caseFieldsImpl('[T]) |
| 45 | + private def caseFieldsImpl(tp: Type[_])(implicit reflect: Reflection): Expr[List[String]] = { |
| 46 | + import reflect._ |
| 47 | + val fields = tp.reflect.symbol.asClass.caseFields.map(_.name) |
| 48 | + fields.toExpr |
| 49 | + } |
| 50 | + |
| 51 | + inline def fieldIn[T](inline mem: String): String = ~fieldInImpl('[T], mem) |
| 52 | + private def fieldInImpl(t: Type[_], mem: String)(implicit reflect: Reflection): Expr[String] = { |
| 53 | + import reflect._ |
| 54 | + val field = t.reflect.symbol.asClass.field(mem) |
| 55 | + field.map(_.name).getOrElse("").toExpr |
| 56 | + } |
| 57 | + |
| 58 | + inline def fieldsIn[T]: Seq[String] = ~fieldsInImpl('[T]) |
| 59 | + private def fieldsInImpl(t: Type[_])(implicit reflect: Reflection): Expr[Seq[String]] = { |
| 60 | + import reflect._ |
| 61 | + val fields = t.reflect.symbol.asClass.fields |
| 62 | + fields.map(_.name).toList.toExpr |
| 63 | + } |
| 64 | + |
| 65 | + inline def methodIn[T](inline mem: String): Seq[String] = ~methodInImpl('[T], mem) |
| 66 | + private def methodInImpl(t: Type[_], mem: String)(implicit reflect: Reflection): Expr[Seq[String]] = { |
| 67 | + import reflect._ |
| 68 | + t.reflect.symbol.asClass.classMethod(mem).map(_.name).toExpr |
| 69 | + } |
| 70 | + |
| 71 | + inline def methodsIn[T]: Seq[String] = ~methodsInImpl('[T]) |
| 72 | + private def methodsInImpl(t: Type[_])(implicit reflect: Reflection): Expr[Seq[String]] = { |
| 73 | + import reflect._ |
| 74 | + t.reflect.symbol.asClass.classMethods.map(_.name).toExpr |
| 75 | + } |
| 76 | + |
| 77 | + inline def method[T](inline mem: String): Seq[String] = ~methodImpl('[T], mem) |
| 78 | + private def methodImpl(t: Type[_], mem: String)(implicit reflect: Reflection): Expr[Seq[String]] = { |
| 79 | + import reflect._ |
| 80 | + t.reflect.symbol.asClass.method(mem).map(_.name).toExpr |
| 81 | + } |
| 82 | + |
| 83 | + inline def methods[T]: Seq[String] = ~methodsImpl('[T]) |
| 84 | + private def methodsImpl(t: Type[_])(implicit reflect: Reflection): Expr[Seq[String]] = { |
| 85 | + import reflect._ |
| 86 | + t.reflect.symbol.asClass.methods.map(_.name).toExpr |
| 87 | + } |
| 88 | + |
| 89 | + inline def typeTag[T](x: T): String = ~typeTagImpl('[T]) |
| 90 | + private def typeTagImpl(tp: Type[_])(implicit reflect: Reflection): Expr[String] = { |
| 91 | + import reflect._ |
| 92 | + val res = tp.reflect.tpe.showCode |
| 93 | + res.toExpr |
| 94 | + } |
| 95 | + |
| 96 | + inline def companion[T1, T2]: Boolean = ~companionImpl('[T1], '[T2]) |
| 97 | + private def companionImpl(t1: Type[_], t2: Type[_])(implicit reflect: Reflection): Expr[Boolean] = { |
| 98 | + import reflect._ |
| 99 | + val res = t1.reflect.symbol.asClass.companionModule.contains(t2.reflect.symbol) |
| 100 | + res.toExpr |
| 101 | + } |
| 102 | + |
| 103 | + inline def companionName[T1]: String = ~companionNameImpl('[T1]) |
| 104 | + private def companionNameImpl(tp: Type[_])(implicit reflect: Reflection): Expr[String] = { |
| 105 | + import reflect._ |
| 106 | + val companionClassOpt = tp.reflect.symbol match { |
| 107 | + case IsClassSymbol(sym) => sym.companionClass |
| 108 | + case IsValSymbol(sym) => sym.companionClass |
| 109 | + case _ => None |
| 110 | + } |
| 111 | + companionClassOpt.map(_.fullName).getOrElse("").toExpr |
| 112 | + } |
| 113 | + |
| 114 | + // TODO add to the std lib |
| 115 | + private implicit def listIsLiftable[T: Type: Liftable]: Liftable[List[T]] = new Liftable { |
| 116 | + def toExpr(list: List[T]): Expr[List[T]] = list match { |
| 117 | + case x :: xs => '(~x.toExpr :: ~toExpr(xs)) |
| 118 | + case Nil => '(Nil) |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments