Skip to content

Commit 662631d

Browse files
committed
Fix ordering of specialized names and type parameterized apply
1 parent a206d77 commit 662631d

File tree

3 files changed

+58
-133
lines changed

3 files changed

+58
-133
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -884,9 +884,9 @@ class Definitions {
884884
lazy val ScalaNumericValueTypeList = List(
885885
ByteType, ShortType, CharType, IntType, LongType, FloatType, DoubleType)
886886

887-
private lazy val ScalaNumericValueTypes: collection.Set[TypeRef] = ScalaNumericValueTypeList.toSet
888-
private lazy val ScalaValueTypes: collection.Set[TypeRef] = ScalaNumericValueTypes + UnitType + BooleanType
889-
private lazy val ScalaBoxedTypes = ScalaValueTypes map (t => boxedTypes(t.name))
887+
lazy val ScalaNumericValueTypes: collection.Set[TypeRef] = ScalaNumericValueTypeList.toSet
888+
lazy val ScalaValueTypes: collection.Set[TypeRef] = ScalaNumericValueTypes + UnitType + BooleanType
889+
lazy val ScalaBoxedTypes = ScalaValueTypes map (t => boxedTypes(t.name))
890890

891891
val ScalaNumericValueClasses = new PerRun[collection.Set[Symbol]](implicit ctx => ScalaNumericValueTypes.map(_.symbol))
892892
val ScalaValueClasses = new PerRun[collection.Set[Symbol]](implicit ctx => ScalaValueTypes.map(_.symbol))

compiler/src/dotty/tools/dotc/core/NameOps.scala

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ object NameOps {
171171
/** Is a function name
172172
* - FunctionN for N >= 0
173173
*/
174-
def isPlainFunction: Boolean = functionArityFor(tpnme.Function) >= 0
174+
def isPlainFunction: Boolean = functionArityFor(str.Function) >= 0
175175

176176
/** Is a implicit function name
177177
* - ImplicitFunctionN for N >= 0
@@ -213,23 +213,24 @@ object NameOps {
213213
case nme.clone_ => nme.clone_
214214
}
215215

216-
def specializedFor(classTargs: List[Types.Type], classTargsNames: List[Name], methodTargs: List[Types.Type], methodTarsNames: List[Name])(implicit ctx: Context): name.ThisName = {
217-
218-
def typeToTag(tp: Types.Type): Name = {
219-
tp.classSymbol match {
220-
case t if t eq defn.IntClass => nme.specializedTypeNames.Int
221-
case t if t eq defn.BooleanClass => nme.specializedTypeNames.Boolean
222-
case t if t eq defn.ByteClass => nme.specializedTypeNames.Byte
223-
case t if t eq defn.LongClass => nme.specializedTypeNames.Long
224-
case t if t eq defn.ShortClass => nme.specializedTypeNames.Short
225-
case t if t eq defn.FloatClass => nme.specializedTypeNames.Float
226-
case t if t eq defn.UnitClass => nme.specializedTypeNames.Void
227-
case t if t eq defn.DoubleClass => nme.specializedTypeNames.Double
228-
case t if t eq defn.CharClass => nme.specializedTypeNames.Char
229-
case _ => nme.specializedTypeNames.Object
230-
}
216+
private def typeToTag(tp: Types.Type)(implicit ctx: Context): Name =
217+
tp.classSymbol match {
218+
case t if t eq defn.IntClass => nme.specializedTypeNames.Int
219+
case t if t eq defn.BooleanClass => nme.specializedTypeNames.Boolean
220+
case t if t eq defn.ByteClass => nme.specializedTypeNames.Byte
221+
case t if t eq defn.LongClass => nme.specializedTypeNames.Long
222+
case t if t eq defn.ShortClass => nme.specializedTypeNames.Short
223+
case t if t eq defn.FloatClass => nme.specializedTypeNames.Float
224+
case t if t eq defn.UnitClass => nme.specializedTypeNames.Void
225+
case t if t eq defn.DoubleClass => nme.specializedTypeNames.Double
226+
case t if t eq defn.CharClass => nme.specializedTypeNames.Char
227+
case _ => nme.specializedTypeNames.Object
231228
}
232229

230+
/** This method is to be used on **type parameters** from a class, since
231+
* this method does sorting based on their names
232+
*/
233+
def specializedFor(classTargs: List[Types.Type], classTargsNames: List[Name], methodTargs: List[Types.Type], methodTarsNames: List[Name])(implicit ctx: Context): name.ThisName = {
233234
val methodTags: Seq[Name] = (methodTargs zip methodTarsNames).sortBy(_._2).map(x => typeToTag(x._1))
234235
val classTags: Seq[Name] = (classTargs zip classTargsNames).sortBy(_._2).map(x => typeToTag(x._1))
235236

@@ -238,6 +239,17 @@ object NameOps {
238239
classTags.fold(nme.EMPTY)(_ ++ _) ++ nme.specializedTypeNames.suffix)
239240
}
240241

242+
/** Use for specializing function names ONLY and use it if you are **not**
243+
* creating specialized name from type parameters. The order of names will
244+
* be:
245+
*
246+
* `<return type><first type><second type><...>`
247+
*/
248+
def specializedFunction(ret: Types.Type, args: List[Types.Type])(implicit ctx: Context): name.ThisName =
249+
name ++ nme.specializedTypeNames.prefix ++
250+
nme.specializedTypeNames.separator ++ typeToTag(ret) ++
251+
args.map(typeToTag).fold(nme.EMPTY)(_ ++ _) ++ nme.specializedTypeNames.suffix
252+
241253
/** If name length exceeds allowable limit, replace part of it by hash */
242254
def compactified(implicit ctx: Context): TermName = termName(compactify(name.toString))
243255

compiler/src/dotty/tools/dotc/transform/SpecializedApplyMethods.scala

Lines changed: 27 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import ast.Trees._, ast.tpd, core._
66
import Contexts.Context, Types._, Decorators._, Symbols._, DenotTransformers._
77
import SymDenotations._, Scopes._, StdNames._, NameOps._, Names._
88

9-
import scala.reflect.internal.util.Collections
10-
119
/** This phase synthesizes specialized methods for FunctionN, this is done
1210
* since there are no scala signatures in the bytecode for the specialized
1311
* methods.
@@ -29,107 +27,29 @@ class SpecializedApplyMethods extends MiniPhaseTransform with InfoTransformer {
2927
private[this] var func2: Symbol = _
3028

3129
private def init()(implicit ctx: Context): Unit = if (func0Applys eq null) {
32-
def specApply(sym: Symbol, ret: Type, args: List[Type])(implicit ctx: Context) = {
33-
val all = args :+ ret
34-
val name = nme.apply.specializedFor(all, all.map(_.typeSymbol.name), Nil, Nil)
30+
val definitions = ctx.definitions
31+
import definitions._
32+
33+
def specApply(sym: Symbol, args: List[Type], ret: Type)(implicit ctx: Context) = {
34+
val name = nme.apply.specializedFunction(ret, args)
3535
ctx.newSymbol(sym, name, Flags.Method, MethodType(args, ret))
3636
}
3737

38-
func0 = defn.FunctionClass(0)
39-
func0Applys = List(
40-
specApply(func0, defn.UnitType, Nil),
41-
specApply(func0, defn.ByteType, Nil),
42-
specApply(func0, defn.ShortType, Nil),
43-
specApply(func0, defn.IntType, Nil),
44-
specApply(func0, defn.LongType, Nil),
45-
specApply(func0, defn.CharType, Nil),
46-
specApply(func0, defn.FloatType, Nil),
47-
specApply(func0, defn.DoubleType, Nil),
48-
specApply(func0, defn.BooleanType, Nil)
49-
)
50-
func1 = defn.FunctionClass(1)
51-
func1Applys = List(
52-
specApply(func1, defn.UnitType, List(defn.IntType)),
53-
specApply(func1, defn.IntType, List(defn.IntType)),
54-
specApply(func1, defn.FloatType, List(defn.IntType)),
55-
specApply(func1, defn.LongType, List(defn.IntType)),
56-
specApply(func1, defn.DoubleType, List(defn.IntType)),
57-
specApply(func1, defn.UnitType, List(defn.LongType)),
58-
specApply(func1, defn.BooleanType, List(defn.LongType)),
59-
specApply(func1, defn.IntType, List(defn.LongType)),
60-
specApply(func1, defn.FloatType, List(defn.LongType)),
61-
specApply(func1, defn.LongType, List(defn.LongType)),
62-
specApply(func1, defn.DoubleType, List(defn.LongType)),
63-
specApply(func1, defn.UnitType, List(defn.FloatType)),
64-
specApply(func1, defn.BooleanType, List(defn.FloatType)),
65-
specApply(func1, defn.IntType, List(defn.FloatType)),
66-
specApply(func1, defn.FloatType, List(defn.FloatType)),
67-
specApply(func1, defn.LongType, List(defn.FloatType)),
68-
specApply(func1, defn.DoubleType, List(defn.FloatType)),
69-
specApply(func1, defn.UnitType, List(defn.DoubleType)),
70-
specApply(func1, defn.BooleanType, List(defn.DoubleType)),
71-
specApply(func1, defn.IntType, List(defn.DoubleType)),
72-
specApply(func1, defn.FloatType, List(defn.DoubleType)),
73-
specApply(func1, defn.LongType, List(defn.DoubleType)),
74-
specApply(func1, defn.DoubleType, List(defn.DoubleType))
75-
)
38+
func0 = FunctionClass(0)
39+
func0Applys = for (r <- ScalaValueTypes.toList) yield specApply(func0, Nil, r)
40+
41+
func1 = FunctionClass(1)
42+
func1Applys = for {
43+
r <- List(UnitType, BooleanType, IntType, FloatType, LongType, DoubleType)
44+
t1 <- List(IntType, LongType, FloatType, DoubleType)
45+
} yield specApply(func1, List(t1), r)
46+
7647
func2 = defn.FunctionClass(2)
77-
func2Applys = List(
78-
specApply(func2, defn.UnitType, List(defn.IntType, defn.IntType)),
79-
specApply(func2, defn.BooleanType, List(defn.IntType, defn.IntType)),
80-
specApply(func2, defn.IntType, List(defn.IntType, defn.IntType)),
81-
specApply(func2, defn.FloatType, List(defn.IntType, defn.IntType)),
82-
specApply(func2, defn.LongType, List(defn.IntType, defn.IntType)),
83-
specApply(func2, defn.DoubleType, List(defn.IntType, defn.IntType)),
84-
specApply(func2, defn.UnitType, List(defn.IntType, defn.LongType)),
85-
specApply(func2, defn.BooleanType, List(defn.IntType, defn.LongType)),
86-
specApply(func2, defn.IntType, List(defn.IntType, defn.LongType)),
87-
specApply(func2, defn.FloatType, List(defn.IntType, defn.LongType)),
88-
specApply(func2, defn.LongType, List(defn.IntType, defn.LongType)),
89-
specApply(func2, defn.DoubleType, List(defn.IntType, defn.LongType)),
90-
specApply(func2, defn.UnitType, List(defn.IntType, defn.DoubleType)),
91-
specApply(func2, defn.BooleanType, List(defn.IntType, defn.DoubleType)),
92-
specApply(func2, defn.IntType, List(defn.IntType, defn.DoubleType)),
93-
specApply(func2, defn.FloatType, List(defn.IntType, defn.DoubleType)),
94-
specApply(func2, defn.LongType, List(defn.IntType, defn.DoubleType)),
95-
specApply(func2, defn.DoubleType, List(defn.IntType, defn.DoubleType)),
96-
specApply(func2, defn.UnitType, List(defn.LongType, defn.IntType)),
97-
specApply(func2, defn.BooleanType, List(defn.LongType, defn.IntType)),
98-
specApply(func2, defn.IntType, List(defn.LongType, defn.IntType)),
99-
specApply(func2, defn.FloatType, List(defn.LongType, defn.IntType)),
100-
specApply(func2, defn.LongType, List(defn.LongType, defn.IntType)),
101-
specApply(func2, defn.DoubleType, List(defn.LongType, defn.IntType)),
102-
specApply(func2, defn.UnitType, List(defn.LongType, defn.LongType)),
103-
specApply(func2, defn.BooleanType, List(defn.LongType, defn.LongType)),
104-
specApply(func2, defn.IntType, List(defn.LongType, defn.LongType)),
105-
specApply(func2, defn.FloatType, List(defn.LongType, defn.LongType)),
106-
specApply(func2, defn.LongType, List(defn.LongType, defn.LongType)),
107-
specApply(func2, defn.DoubleType, List(defn.LongType, defn.LongType)),
108-
specApply(func2, defn.UnitType, List(defn.LongType, defn.DoubleType)),
109-
specApply(func2, defn.BooleanType, List(defn.LongType, defn.DoubleType)),
110-
specApply(func2, defn.IntType, List(defn.LongType, defn.DoubleType)),
111-
specApply(func2, defn.FloatType, List(defn.LongType, defn.DoubleType)),
112-
specApply(func2, defn.LongType, List(defn.LongType, defn.DoubleType)),
113-
specApply(func2, defn.DoubleType, List(defn.LongType, defn.DoubleType)),
114-
specApply(func2, defn.UnitType, List(defn.DoubleType, defn.IntType)),
115-
specApply(func2, defn.BooleanType, List(defn.DoubleType, defn.IntType)),
116-
specApply(func2, defn.IntType, List(defn.DoubleType, defn.IntType)),
117-
specApply(func2, defn.FloatType, List(defn.DoubleType, defn.IntType)),
118-
specApply(func2, defn.LongType, List(defn.DoubleType, defn.IntType)),
119-
specApply(func2, defn.DoubleType, List(defn.DoubleType, defn.IntType)),
120-
specApply(func2, defn.UnitType, List(defn.DoubleType, defn.LongType)),
121-
specApply(func2, defn.BooleanType, List(defn.DoubleType, defn.LongType)),
122-
specApply(func2, defn.IntType, List(defn.DoubleType, defn.LongType)),
123-
specApply(func2, defn.FloatType, List(defn.DoubleType, defn.LongType)),
124-
specApply(func2, defn.LongType, List(defn.DoubleType, defn.LongType)),
125-
specApply(func2, defn.DoubleType, List(defn.DoubleType, defn.LongType)),
126-
specApply(func2, defn.UnitType, List(defn.DoubleType, defn.DoubleType)),
127-
specApply(func2, defn.BooleanType, List(defn.DoubleType, defn.DoubleType)),
128-
specApply(func2, defn.IntType, List(defn.DoubleType, defn.DoubleType)),
129-
specApply(func2, defn.FloatType, List(defn.DoubleType, defn.DoubleType)),
130-
specApply(func2, defn.LongType, List(defn.DoubleType, defn.DoubleType)),
131-
specApply(func2, defn.DoubleType, List(defn.DoubleType, defn.DoubleType))
132-
)
48+
func2Applys = for {
49+
r <- List(UnitType, BooleanType, IntType, FloatType, LongType, DoubleType)
50+
t1 <- List(IntType, LongType, DoubleType)
51+
t2 <- List(IntType, LongType, DoubleType)
52+
} yield specApply(func2, List(t1, t2), r)
13353
}
13454

13555
/** Add symbols for specialized methods to FunctionN */
@@ -164,20 +84,13 @@ class SpecializedApplyMethods extends MiniPhaseTransform with InfoTransformer {
16484
else Nil
16585

16686
if (additionalSymbols eq Nil) tree
167-
else {
168-
val newBody: List[Tree] = tree.body ++ additionalSymbols.map { applySym =>
169-
polyDefDef(applySym.asTerm, tparams => vparamss => {
170-
val prefix = This(owner.asClass).select(nme.apply).appliedToTypes(vparamss.head.map(_.tpe))
171-
val argTypess = prefix.tpe.widen.paramTypess
172-
173-
val argss = Collections.map2(vparamss, argTypess) { (vparams, argTypes) =>
174-
Collections.map2(vparams, argTypes) { (vparam, argType) => vparam.ensureConforms(argType) }
175-
}
176-
prefix.appliedToArgss(argss).ensureConforms(applySym.info.finalResultType)
177-
})
178-
}
179-
180-
cpy.Template(tree)(body = newBody)
181-
}
87+
else cpy.Template(tree)(body = tree.body ++ additionalSymbols.map { apply =>
88+
DefDef(apply.asTerm, { vparamss =>
89+
This(owner.asClass)
90+
.select(nme.apply)
91+
.appliedToArgss(vparamss)
92+
.ensureConforms(apply.info.finalResultType)
93+
})
94+
})
18295
}
18396
}

0 commit comments

Comments
 (0)