@@ -14,27 +14,23 @@ import scala.collection.mutable
14
14
class SpecializeFunctions extends MiniPhaseTransform with InfoTransformer {
15
15
import ast .tpd ._
16
16
val phaseName = " specializeFunctions"
17
+ override def runsAfter = Set (classOf [ElimByName ])
17
18
18
- /** Transforms the type to include decls for specialized applys and replace
19
- * the class parents with specialized versions.
20
- */
19
+ /** Transforms the type to include decls for specialized applys */
21
20
def transformInfo (tp : Type , sym : Symbol )(implicit ctx : Context ) = tp match {
22
- case tp : ClassInfo if ! sym.is(Flags .Package ) && (tp.decls ne EmptyScope ) => {
21
+ case tp : ClassInfo if ! sym.is(Flags .Package ) && (tp.decls ne EmptyScope ) =>
23
22
var newApplys = Map .empty[Name , Symbol ]
24
23
25
24
tp.parents.foreach { parent =>
26
25
List (0 , 1 , 2 , 3 ).foreach { arity =>
27
26
val func = defn.FunctionClass (arity)
28
- if (! parent.derivesFrom(func)) Nil
29
- else {
27
+ if (parent.derivesFrom(func)) {
30
28
val typeParams = tp.cls.typeRef.baseType(func).argInfos
31
29
val interface = specInterface(typeParams)
32
30
33
- if (interface.exists) {
34
- if (tp.decls.lookup(nme.apply).exists) {
35
- val specializedMethodName = nme.apply.specializedFunction(typeParams.last, typeParams.init)
36
- newApplys += (specializedMethodName -> interface)
37
- }
31
+ if (interface.exists && tp.decls.lookup(nme.apply).exists) {
32
+ val specializedMethodName = nme.apply.specializedFunction(typeParams.last, typeParams.init)
33
+ newApplys += (specializedMethodName -> interface)
38
34
}
39
35
}
40
36
}
@@ -45,7 +41,7 @@ class SpecializeFunctions extends MiniPhaseTransform with InfoTransformer {
45
41
ctx.newSymbol(
46
42
sym,
47
43
name,
48
- Flags .Override | Flags .Method ,
44
+ Flags .Override | Flags .Method | Flags . Synthetic ,
49
45
interface.info.decls.lookup(name).info
50
46
)
51
47
}
@@ -55,20 +51,18 @@ class SpecializeFunctions extends MiniPhaseTransform with InfoTransformer {
55
51
56
52
if (newApplys.isEmpty) tp
57
53
else tp.derivedClassInfo(decls = newDecls)
58
- }
59
54
60
55
case _ => tp
61
56
}
62
57
63
58
/** Transforms the `Template` of the classes to contain forwarders from the
64
- * generic applys to the specialized ones. Also replaces parents of the
65
- * class on the tree level and inserts the specialized applys in the
66
- * template body.
59
+ * generic applys to the specialized ones. Also inserts the specialized applys
60
+ * in the template body.
67
61
*/
68
62
override def transformTemplate (tree : Template )(implicit ctx : Context , info : TransformerInfo ) = {
69
63
val applyBuf = new mutable.ListBuffer [Tree ]
70
64
val newBody = tree.body.mapConserve {
71
- case dt : DefDef if dt.name == nme.apply && dt.vparamss.length == 1 => {
65
+ case dt : DefDef if dt.name == nme.apply && dt.vparamss.length == 1 =>
72
66
val specName = nme.apply.specializedFunction(
73
67
dt.tpe.widen.finalResultType,
74
68
dt.vparamss.head.map(_.symbol.info)
@@ -93,7 +87,7 @@ class SpecializeFunctions extends MiniPhaseTransform with InfoTransformer {
93
87
.appliedToArgs(dt.vparamss.head.map(vparam => ref(vparam.symbol)))
94
88
})
95
89
} else dt
96
- }
90
+
97
91
case x => x
98
92
}
99
93
@@ -105,31 +99,30 @@ class SpecializeFunctions extends MiniPhaseTransform with InfoTransformer {
105
99
/** Dispatch to specialized `apply`s in user code when available */
106
100
override def transformApply (tree : Apply )(implicit ctx : Context , info : TransformerInfo ) =
107
101
tree match {
108
- case app @ Apply (fun, args)
102
+ case Apply (fun, args)
109
103
if fun.symbol.name == nme.apply &&
110
104
fun.symbol.owner.derivesFrom(defn.FunctionClass (args.length))
111
- => {
105
+ =>
112
106
val params = (fun.tpe.widen.firstParamTypes :+ tree.tpe).map(_.widenSingleton.dealias)
113
107
val specializedApply = specializedName(nme.apply, params)
114
108
115
109
if (! params.exists(_.isInstanceOf [ExprType ]) && fun.symbol.owner.info.decls.lookup(specializedApply).exists) {
116
110
val newSel = fun match {
117
111
case Select (qual, _) =>
118
112
qual.select(specializedApply)
119
- case _ => {
113
+ case _ =>
120
114
(fun.tpe: @ unchecked) match {
121
115
case TermRef (prefix : ThisType , name) =>
122
116
tpd.This (prefix.cls).select(specializedApply)
123
117
case TermRef (prefix : NamedType , name) =>
124
118
tpd.ref(prefix).select(specializedApply)
125
119
}
126
- }
127
120
}
128
121
129
122
newSel.appliedToArgs(args)
130
123
}
131
124
else tree
132
- }
125
+
133
126
case _ => tree
134
127
}
135
128
0 commit comments