Skip to content

Commit 8949c7f

Browse files
felixmulderDuhemm
authored andcommitted
Fix ordering of specialized names and type parameterized apply
1 parent 1d0efe6 commit 8949c7f

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
@@ -969,9 +969,9 @@ class Definitions {
969969
lazy val ScalaNumericValueTypeList = List(
970970
ByteType, ShortType, CharType, IntType, LongType, FloatType, DoubleType)
971971

972-
private lazy val ScalaNumericValueTypes: collection.Set[TypeRef] = ScalaNumericValueTypeList.toSet
973-
private lazy val ScalaValueTypes: collection.Set[TypeRef] = ScalaNumericValueTypes + UnitType + BooleanType
974-
private lazy val ScalaBoxedTypes = ScalaValueTypes map (t => boxedTypes(t.name))
972+
lazy val ScalaNumericValueTypes: collection.Set[TypeRef] = ScalaNumericValueTypeList.toSet
973+
lazy val ScalaValueTypes: collection.Set[TypeRef] = ScalaNumericValueTypes + UnitType + BooleanType
974+
lazy val ScalaBoxedTypes = ScalaValueTypes map (t => boxedTypes(t.name))
975975

976976
val ScalaNumericValueClasses = new PerRun[collection.Set[Symbol]](implicit ctx => ScalaNumericValueTypes.map(_.symbol))
977977
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
@@ -186,7 +186,7 @@ object NameOps {
186186
/** Is a function name
187187
* - FunctionN for N >= 0
188188
*/
189-
def isPlainFunction: Boolean = functionArityFor(tpnme.Function) >= 0
189+
def isPlainFunction: Boolean = functionArityFor(str.Function) >= 0
190190

191191
/** Is a implicit function name
192192
* - ImplicitFunctionN for N >= 1
@@ -231,23 +231,24 @@ object NameOps {
231231
case nme.clone_ => nme.clone_
232232
}
233233

234-
def specializedFor(classTargs: List[Types.Type], classTargsNames: List[Name], methodTargs: List[Types.Type], methodTarsNames: List[Name])(implicit ctx: Context): name.ThisName = {
235-
236-
def typeToTag(tp: Types.Type): Name = {
237-
tp.classSymbol match {
238-
case t if t eq defn.IntClass => nme.specializedTypeNames.Int
239-
case t if t eq defn.BooleanClass => nme.specializedTypeNames.Boolean
240-
case t if t eq defn.ByteClass => nme.specializedTypeNames.Byte
241-
case t if t eq defn.LongClass => nme.specializedTypeNames.Long
242-
case t if t eq defn.ShortClass => nme.specializedTypeNames.Short
243-
case t if t eq defn.FloatClass => nme.specializedTypeNames.Float
244-
case t if t eq defn.UnitClass => nme.specializedTypeNames.Void
245-
case t if t eq defn.DoubleClass => nme.specializedTypeNames.Double
246-
case t if t eq defn.CharClass => nme.specializedTypeNames.Char
247-
case _ => nme.specializedTypeNames.Object
248-
}
234+
private def typeToTag(tp: Types.Type)(implicit ctx: Context): Name =
235+
tp.classSymbol match {
236+
case t if t eq defn.IntClass => nme.specializedTypeNames.Int
237+
case t if t eq defn.BooleanClass => nme.specializedTypeNames.Boolean
238+
case t if t eq defn.ByteClass => nme.specializedTypeNames.Byte
239+
case t if t eq defn.LongClass => nme.specializedTypeNames.Long
240+
case t if t eq defn.ShortClass => nme.specializedTypeNames.Short
241+
case t if t eq defn.FloatClass => nme.specializedTypeNames.Float
242+
case t if t eq defn.UnitClass => nme.specializedTypeNames.Void
243+
case t if t eq defn.DoubleClass => nme.specializedTypeNames.Double
244+
case t if t eq defn.CharClass => nme.specializedTypeNames.Char
245+
case _ => nme.specializedTypeNames.Object
249246
}
250247

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

@@ -256,6 +257,17 @@ object NameOps {
256257
classTags.fold(nme.EMPTY)(_ ++ _) ++ nme.specializedTypeNames.suffix)
257258
}
258259

260+
/** Use for specializing function names ONLY and use it if you are **not**
261+
* creating specialized name from type parameters. The order of names will
262+
* be:
263+
*
264+
* `<return type><first type><second type><...>`
265+
*/
266+
def specializedFunction(ret: Types.Type, args: List[Types.Type])(implicit ctx: Context): name.ThisName =
267+
name ++ nme.specializedTypeNames.prefix ++
268+
nme.specializedTypeNames.separator ++ typeToTag(ret) ++
269+
args.map(typeToTag).fold(nme.EMPTY)(_ ++ _) ++ nme.specializedTypeNames.suffix
270+
259271
/** If name length exceeds allowable limit, replace part of it by hash */
260272
def compactified(implicit ctx: Context): TermName = termName(compactify(name.toString))
261273

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)