-
Notifications
You must be signed in to change notification settings - Fork 3
/
Untyper.scala
164 lines (146 loc) · 6.27 KB
/
Untyper.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.thoughtworks.feature
import scala.reflect.api.Universe
import com.thoughtworks.Extractor._
/** A utility to convert [[scala.reflect.api.Universe.Type Type]] to [[scala.reflect.api.Universe.Tree Tree]].
*
* @note The primary intent of '''Untyper''' class is for some macro libraries in this [[https://github.com/ThoughtWorksInc/feature.scala feature.scala]] project,
* although it may be also useful for other projects.
*
* @author 杨博 (Yang Bo) <pop.atry@gmail.com>
*/
class Untyper[Universe <: Singleton with scala.reflect.api.Universe](val universe: Universe) {
import universe._
/** Returns the instance tree for given singleton type */
def singletonValue: PartialFunction[Type, Tree] = {
case ThisType(symbol) =>
q"$symbol.this"
case SingleType(NoPrefix, sym) =>
q"${sym.name.toTermName}"
case SingleType(pre, sym) if pre.typeSymbol.isPackage =>
q"$sym"
case SingleType(singletonValue.extract(pre), sym) =>
q"$pre.$sym"
case SuperType(singletonValue.extract(thisValue), ThisType(superSymbol)) =>
Super(thisValue, superSymbol.name.toTypeName)
case SingleType(untypeOption.extract(pre), sym) =>
SelectFromTypeTree(pre, sym.name.toTypeName)
}
def termSymbol: PartialFunction[Symbol, TermSymbol] = {
case symbol if symbol.isTerm => symbol.asTerm
}
def typeSymbol: PartialFunction[Symbol, TypeSymbol] = {
case symbol if symbol.isType => symbol.asType
}
def typeDefinitionSymbol(implicit tpe: Type): PartialFunction[TypeSymbol, (TypeName, Seq[Symbol], Type)] = {
case symbol if !symbol.isClass =>
val info = symbol.infoIn(tpe)
(symbol.name.toTypeName, info.typeParams, info.resultType)
}
def varDefinitionSymbol(implicit tpe: Type): PartialFunction[TermSymbol, (TermName, Type)] = {
case symbol if symbol.isVar =>
(symbol.name.toTermName, symbol.infoIn(tpe).resultType)
}
def valDefinitionSymbol(implicit tpe: Type): PartialFunction[TermSymbol, (TermName, Type)] = {
case symbol if symbol.isVal || symbol.isStable =>
(symbol.name.toTermName, symbol.infoIn(tpe).resultType)
}
def defDefinitionSymbol(
implicit tpe: Type): PartialFunction[TermSymbol, (TermName, Seq[Symbol], Seq[Seq[Symbol]], Type)] = {
case symbol if symbol.isMethod =>
val info = symbol.infoIn(tpe)
(symbol.name.toTermName, info.typeParams, info.paramLists, info.finalResultType)
}
private def typeDefinitionOption(symbol: TypeSymbol)(implicit tpe: Type): Option[TypeDef] = {
val flags = {
if (symbol.isCovariant) {
Flag.COVARIANT
} else if (symbol.isContravariant) {
Flag.CONTRAVARIANT
} else {
NoFlags
}
} | {
if (symbol.isParameter) {
Flag.PARAM
} else {
NoFlags
}
}
symbol match {
case typeDefinitionSymbol.extract(name,
typeDefinition.extract.forall(params),
TypeBounds(untypeOption.extract(upper), untypeOption.extract(lower))) =>
Some(q"$flags type $name[..$params] >: $upper <: $lower")
case typeDefinitionSymbol
.extract(name, typeDefinition.extract.forall(params: Seq[TypeDef]), untypeOption.extract(concreteType)) =>
Some(q"$flags type $name[..$params] = $concreteType")
case _ =>
None
}
}
def typeDefinition(implicit tpe: Type): PartialFunction[TypeSymbol, TypeDef] = {
scala.Function.unlift(typeDefinitionOption)
}
def definition(implicit tpe: Type): PartialFunction[Symbol, Tree] = {
case typeDefinition.extract(typeDef) => typeDef
case termDefinition.extract(termDef) => termDef
}
def termDefinitionOption(symbol: Symbol)(implicit tpe: Type): Option[Tree] = {
if (symbol.isTerm) {
symbol.asTerm match {
case varDefinitionSymbol.extract(name, untypeOption.extract(result)) =>
Some(q"var $name: $result")
case valDefinitionSymbol.extract(name, untypeOption.extract(result)) =>
val flags = if (symbol.isImplicit) {
Flag.IMPLICIT
} else {
NoFlags
}
Some(q"$flags val $name: $result")
case defDefinitionSymbol.extract(name,
typeDefinition.extract.forall(typeParams),
termDefinition.extract.forall.forall(params),
untypeOption.extract(result)) =>
Some(q"def $name[..$typeParams](...$params): $result")
case _ =>
None
}
} else {
None
}
}
def termDefinition(implicit tpe: Type): PartialFunction[Symbol, Tree] = {
scala.Function.unlift(termDefinitionOption)
}
protected def preprocess(tpe: Type): Type = tpe
def untypeOption: Type => Option[Tree] = { implicit tpe: Type =>
preprocess(tpe) match {
case ConstantType(value) =>
Some(Literal(value))
case singletonValue.extract(value) =>
Some(tq"$value.type")
case TypeRef(NoPrefix, sym, args) =>
Some(tq"${sym.name.toTypeName}[..${args.map(untype)}]")
case TypeRef(pre, sym, args) if pre.typeSymbol.isPackage =>
Some(tq"$sym[..${args.map(untype)}]")
case TypeRef(singletonValue.extract(pre), sym, args) =>
Some(tq"$pre.$sym[..${args.map(untype)}]")
case TypeRef(untypeOption.extract(pre), sym, args) =>
Some(tq"$pre#$sym[..${args.map(untype)}]")
case RefinedType(untypeOption.extract.forall(parents), decls) =>
Some(CompoundTypeTree(Template(parents.toList, noSelfType, decls.view.map(definition).toList))) // TODO: skip concret types
case PolyType(typeSymbol.extract.forall(typeDefinition.extract.forall(typeParams)),
untypeOption.extract(resultType)) =>
Some(tq"({type Λ$$[..$typeParams] = $resultType})#Λ$$")
case ExistentialType(definition.extract.forall(quantified), untypeOption.extract(underlying)) =>
Some(tq"$underlying forSome { ..$quantified }")
case AnnotatedType(annotations, untypeOption.extract(underlying)) =>
Some(annotations.foldLeft(underlying) { (tree, annotation) =>
Annotated(annotation.tree, tree)
})
case _ =>
None
}
}
def untype: PartialFunction[Type, Tree] = scala.Function.unlift(untypeOption)
}