Skip to content

Commit 5c2ad6b

Browse files
committed
Split the defintion of tasty reflect
1 parent a2a8d3f commit 5c2ad6b

28 files changed

+2228
-2016
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package dotty.tools.dotc.tastyreflect
2+
3+
import dotty.tools.dotc.ast.tpd
4+
5+
import scala.tasty.util.Show
6+
7+
trait CaseDefsImpl extends scala.tasty.reflect.CaseDefs { tasty: TastyImpl =>
8+
9+
type CaseDef = tpd.CaseDef
10+
11+
def CaseDefDeco(caseDef: CaseDef): CaseDefAPI = new CaseDefAPI {
12+
def show(implicit ctx: Context, s: Show[tasty.type]): String = s.showCaseDef(caseDef)
13+
def pattern(implicit ctx: Context): Pattern = caseDef.pat
14+
def guard(implicit ctx: Context): Option[Term] = optional(caseDef.guard)
15+
def rhs(implicit ctx: Context): Term = caseDef.body
16+
}
17+
18+
object CaseDef extends CaseDefExtractor {
19+
def unapply(x: CaseDef): Option[(Pattern, Option[Term], Term)] = x match {
20+
case x: tpd.CaseDef @unchecked =>
21+
Some(x.pat, optional(x.guard), x.body)
22+
case _ => None
23+
}
24+
}
25+
26+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package dotty.tools.dotc.tastyreflect
2+
3+
import dotty.tools.dotc.core.Constants
4+
5+
import scala.tasty.util.Show
6+
7+
trait ConstantsImpl extends scala.tasty.reflect.Constants { tasty: TastyImpl =>
8+
9+
type Constant = Constants.Constant
10+
11+
def ConstantDeco(const: Constant): ConstantAPI = new ConstantAPI {
12+
def show(implicit ctx: Context, s: Show[tasty.type]): String = s.showConstant(const)
13+
def value: Any = const.value
14+
}
15+
16+
object Constant extends ConstantModule {
17+
18+
object Unit extends UnitExtractor {
19+
def unapply(x: Constant): Boolean = x match {
20+
case x: Constants.Constant => x.tag == Constants.UnitTag
21+
case _ => false
22+
}
23+
}
24+
25+
object Null extends NullExtractor {
26+
def unapply(x: Constant): Boolean = x match {
27+
case x: Constants.Constant => x.tag == Constants.NullTag
28+
case _ => false
29+
}
30+
}
31+
32+
object Boolean extends BooleanExtractor {
33+
def unapply(x: Constant): Option[Boolean] = x match {
34+
case x: Constants.Constant if x.tag == Constants.BooleanTag => Some(x.booleanValue)
35+
case _ => None
36+
}
37+
}
38+
39+
object Byte extends ByteExtractor {
40+
def unapply(x: Constant): Option[Byte] = x match {
41+
case x: Constants.Constant if x.tag == Constants.ByteTag => Some(x.byteValue)
42+
case _ => None
43+
}
44+
}
45+
46+
object Short extends ShortExtractor {
47+
def unapply(x: Constant): Option[Short] = x match {
48+
case x: Constants.Constant if x.tag == Constants.ShortTag => Some(x.shortValue)
49+
case _ => None
50+
}
51+
}
52+
53+
object Char extends CharExtractor {
54+
def unapply(x: Constant): Option[Char] = x match {
55+
case x: Constants.Constant if x.tag == Constants.CharTag => Some(x.charValue)
56+
case _ => None
57+
}
58+
}
59+
60+
object Int extends IntExtractor {
61+
def unapply(x: Constant): Option[Int] = x match {
62+
case x: Constants.Constant if x.tag == Constants.IntTag => Some(x.intValue)
63+
case _ => None
64+
}
65+
}
66+
67+
object Long extends LongExtractor {
68+
def unapply(x: Constant): Option[Long] = x match {
69+
case x: Constants.Constant if x.tag == Constants.LongTag => Some(x.longValue)
70+
case _ => None
71+
}
72+
}
73+
74+
object Float extends FloatExtractor {
75+
def unapply(x: Constant): Option[Float] = x match {
76+
case x: Constants.Constant if x.tag == Constants.FloatTag => Some(x.floatValue)
77+
case _ => None
78+
}
79+
}
80+
81+
object Double extends DoubleExtractor {
82+
def unapply(x: Constant): Option[Double] = x match {
83+
case x: Constants.Constant if x.tag == Constants.DoubleTag => Some(x.doubleValue)
84+
case _ => None
85+
}
86+
}
87+
88+
object String extends StringExtractor {
89+
def unapply(x: Constant): Option[String] = x match {
90+
case x: Constants.Constant if x.tag == Constants.StringTag => Some(x.stringValue)
91+
case _ => None
92+
}
93+
}
94+
95+
object ClassTag extends ClassTagExtractor {
96+
def unapply(x: Constant): Option[Type] = x match {
97+
case x: Constants.Constant if x.tag == Constants.ClazzTag => Some(x.typeValue)
98+
case _ => None
99+
}
100+
}
101+
102+
object Symbol extends SymbolExtractor {
103+
def unapply(x: Constant): Option[scala.Symbol] = x match {
104+
case x: Constants.Constant if x.tag == Constants.ScalaSymbolTag => Some(x.scalaSymbolValue)
105+
case _ => None
106+
}
107+
}
108+
}
109+
110+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package dotty.tools.dotc.tastyreflect
2+
3+
import dotty.tools.dotc.core.Contexts
4+
import dotty.tools.dotc.tastyreflect.FromSymbol.definitionFromSym
5+
import dotty.tools.dotc.util.{Positions, SourcePosition}
6+
7+
trait ContextsImpl extends scala.tasty.reflect.Contexts { tasty: TastyImpl =>
8+
9+
type Context = Contexts.Context
10+
11+
val rootContext: Contexts.Context
12+
13+
def ContextDeco(ctx: Context): ContextAPI = new ContextAPI {
14+
def owner: Definition = definitionFromSym(ctx.owner)(ctx)
15+
16+
def source: java.nio.file.Path = ctx.compilationUnit.source.file.jpath
17+
}
18+
19+
def rootPosition: SourcePosition = SourcePosition(rootContext.source, Positions.NoPosition)
20+
21+
}

compiler/src/dotty/tools/dotc/tastyreflect/FlagSet.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotty.tools.dotc.tastyreflect
33
import dotty.tools.dotc.core.Flags
44
import dotty.tools.dotc.core.Flags._
55

6-
class FlagSet(flags: Flags.FlagSet) extends scala.tasty.FlagSet {
6+
class FlagSet(flags: Flags.FlagSet) extends scala.tasty.reflect.FlagSet {
77

88
def isProtected: Boolean = flags.is(Protected)
99
def isAbstract: Boolean = flags.is(Abstract)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dotty.tools.dotc.tastyreflect
2+
3+
import dotty.tools.dotc.ast.untpd
4+
5+
import dotty.tools.dotc.core.Decorators._
6+
7+
trait IdsImpl extends scala.tasty.reflect.Ids { tasty: TastyImpl =>
8+
9+
type Id = untpd.Ident
10+
11+
def IdDeco(id: Id): IdAPI = new IdAPI {
12+
def pos(implicit ctx: Context): Position = id.pos
13+
def name(implicit ctx: Context): String = id.name.toString
14+
}
15+
16+
object Id extends IdExtractor {
17+
def unapply(id: Id): Option[String] = Some(id.name.toString)
18+
}
19+
20+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package dotty.tools.dotc.tastyreflect
2+
3+
import dotty.tools.dotc.ast.{Trees, tpd}
4+
import dotty.tools.dotc.core.Decorators._
5+
import dotty.tools.dotc.core.Types
6+
7+
import scala.tasty.util.Show
8+
9+
trait PatternsImpl extends scala.tasty.reflect.Patterns { tasty: TastyImpl =>
10+
11+
// ----- Patterns -------------------------------------------------
12+
13+
type Pattern = tpd.Tree
14+
15+
def PatternDeco(pattern: Pattern): PatternAPI = new PatternAPI {
16+
def show(implicit ctx: Context, s: Show[tasty.type]): String = s.showPattern(pattern)
17+
def pos(implicit ctx: Context): Position = pattern.pos
18+
def tpe(implicit ctx: Context): Types.Type = pattern.tpe.stripTypeVar
19+
}
20+
21+
object Pattern extends PatternModule {
22+
23+
object Value extends ValueExtractor {
24+
def unapply(x: Pattern)(implicit ctx: Context): Option[Term] = x match {
25+
case lit: tpd.Literal @unchecked => Some(lit)
26+
case ref: tpd.RefTree @unchecked if ref.isTerm => Some(ref)
27+
case ths: tpd.This @unchecked => Some(ths)
28+
case _ => None
29+
}
30+
}
31+
32+
object Bind extends BindExtractor {
33+
def unapply(x: Pattern)(implicit ctx: Context): Option[(String, Pattern)] = x match {
34+
case x: tpd.Bind @unchecked if x.name.isTermName => Some(x.name.toString, x.body)
35+
case _ => None
36+
}
37+
}
38+
39+
object Unapply extends UnapplyExtractor {
40+
def unapply(x: Pattern)(implicit ctx: Context): Option[(Term, List[Term], List[Pattern])] = x match {
41+
case Trees.UnApply(fun, implicits, patterns) => Some((fun, implicits, effectivePatterns(patterns)))
42+
case Trees.Typed(Trees.UnApply(fun, implicits, patterns), _) => Some((fun, implicits, effectivePatterns(patterns)))
43+
case _ => None
44+
}
45+
private def effectivePatterns(patterns: List[Pattern]): List[Pattern] = patterns match {
46+
case patterns0 :+ Trees.SeqLiteral(elems, _) => patterns0 ::: elems
47+
case _ => patterns
48+
}
49+
}
50+
51+
object Alternative extends AlternativeExtractor {
52+
def unapply(x: Pattern)(implicit ctx: Context): Option[List[Pattern]] = x match {
53+
case x: tpd.Alternative @unchecked => Some(x.trees)
54+
case _ => None
55+
}
56+
}
57+
58+
object TypeTest extends TypeTestExtractor {
59+
def unapply(x: Pattern)(implicit ctx: Context): Option[TypeTree] = x match {
60+
case Trees.Typed(Trees.UnApply(_, _, _), _) => None
61+
case Trees.Typed(_, tpt) => Some(tpt)
62+
case _ => None
63+
}
64+
}
65+
66+
}
67+
68+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package dotty.tools.dotc.tastyreflect
2+
3+
import dotty.tools.dotc.util.SourcePosition
4+
5+
trait PositionsImpl extends scala.tasty.reflect.Positions { tasty: TastyImpl =>
6+
7+
type Position = SourcePosition
8+
9+
def PositionDeco(pos: Position): PositionAPI = new PositionAPI {
10+
def start: Int = pos.start
11+
def end: Int = pos.end
12+
13+
def sourceFile: java.nio.file.Path = pos.source.file.jpath
14+
15+
def startLine: Int = pos.startLine
16+
def endLine: Int = pos.endLine
17+
18+
def startColumn: Int = pos.startColumn
19+
def endColumn: Int = pos.endColumn
20+
}
21+
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dotty.tools.dotc.tastyreflect
2+
3+
import dotty.tools.dotc.core
4+
5+
trait SignaturesImpl extends scala.tasty.reflect.Signatures { tasty: TastyImpl =>
6+
7+
type Signature = core.Signature
8+
9+
object Signature extends SignatureExtractor {
10+
def unapply(x: Signature)(implicit ctx: Context): Option[(List[String], String)] = {
11+
Some((x.paramsSig.map(_.toString), x.resSig.toString))
12+
}
13+
}
14+
15+
def SignatureDeco(sig: Signature): SignatureAPI = new SignatureAPI {
16+
def paramSigs: List[String] = sig.paramsSig.map(_.toString)
17+
def resultSig: String = sig.resSig.toString
18+
}
19+
20+
}

0 commit comments

Comments
 (0)