|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | + |
| 4 | +import TreeTransforms.{ MiniPhaseTransform, TransformerInfo } |
| 5 | +import ast.Trees._, ast.tpd, core._ |
| 6 | +import Contexts.Context, Types._, Decorators._, Symbols._, DenotTransformers._ |
| 7 | +import SymDenotations._, Scopes._, StdNames._, NameOps._, Names._ |
| 8 | + |
| 9 | +/** This phase synthesizes specialized methods for FunctionN, this is done |
| 10 | + * since there are no scala signatures in the bytecode for the specialized |
| 11 | + * methods. |
| 12 | + * |
| 13 | + * We know which specializations exist for the different arities, therefore we |
| 14 | + * can hardcode them. This should, however be removed once we're using a |
| 15 | + * different standard library. |
| 16 | + */ |
| 17 | +class SpecializedApplyMethods extends MiniPhaseTransform with InfoTransformer { |
| 18 | + import ast.tpd._ |
| 19 | + |
| 20 | + val phaseName = "specializedApplyMethods" |
| 21 | + |
| 22 | + def transformInfo(tp: Type, sym: Symbol)(implicit ctx: Context) = tp match { |
| 23 | + case tp: ClassInfo if defn.isFunctionClass(sym) => { |
| 24 | + def specApply(ret: Type, args: List[Type])(implicit ctx: Context) = { |
| 25 | + val all = args :+ ret |
| 26 | + val name = nme.apply.specializedFor(all, all.map(_.typeSymbol.name), Nil, Nil) |
| 27 | + ctx.newSymbol(sym, name, Flags.Method, MethodType(args, ret)) |
| 28 | + } |
| 29 | + |
| 30 | + val newDecls = sym.name.functionArity match { |
| 31 | + case 0 => |
| 32 | + List( |
| 33 | + specApply(defn.UnitType, Nil), |
| 34 | + specApply(defn.ByteType, Nil), |
| 35 | + specApply(defn.ShortType, Nil), |
| 36 | + specApply(defn.IntType, Nil), |
| 37 | + specApply(defn.LongType, Nil), |
| 38 | + specApply(defn.CharType, Nil), |
| 39 | + specApply(defn.FloatType, Nil), |
| 40 | + specApply(defn.DoubleType, Nil), |
| 41 | + specApply(defn.BooleanType, Nil) |
| 42 | + ) |
| 43 | + .foldLeft(tp.decls.cloneScope){ (decls, sym) => decls.enter(sym); decls } |
| 44 | + |
| 45 | + case 1 => |
| 46 | + List( |
| 47 | + specApply(defn.UnitType, List(defn.IntType)), |
| 48 | + specApply(defn.IntType, List(defn.IntType)), |
| 49 | + specApply(defn.FloatType, List(defn.IntType)), |
| 50 | + specApply(defn.LongType, List(defn.IntType)), |
| 51 | + specApply(defn.DoubleType, List(defn.IntType)), |
| 52 | + specApply(defn.UnitType, List(defn.LongType)), |
| 53 | + specApply(defn.BooleanType, List(defn.LongType)), |
| 54 | + specApply(defn.IntType, List(defn.LongType)), |
| 55 | + specApply(defn.FloatType, List(defn.LongType)), |
| 56 | + specApply(defn.LongType, List(defn.LongType)), |
| 57 | + specApply(defn.DoubleType, List(defn.LongType)), |
| 58 | + specApply(defn.UnitType, List(defn.FloatType)), |
| 59 | + specApply(defn.BooleanType, List(defn.FloatType)), |
| 60 | + specApply(defn.IntType, List(defn.FloatType)), |
| 61 | + specApply(defn.FloatType, List(defn.FloatType)), |
| 62 | + specApply(defn.LongType, List(defn.FloatType)), |
| 63 | + specApply(defn.DoubleType, List(defn.FloatType)), |
| 64 | + specApply(defn.UnitType, List(defn.DoubleType)), |
| 65 | + specApply(defn.BooleanType, List(defn.DoubleType)), |
| 66 | + specApply(defn.IntType, List(defn.DoubleType)), |
| 67 | + specApply(defn.FloatType, List(defn.DoubleType)), |
| 68 | + specApply(defn.LongType, List(defn.DoubleType)), |
| 69 | + specApply(defn.DoubleType, List(defn.DoubleType)) |
| 70 | + ) |
| 71 | + .foldLeft(tp.decls.cloneScope){ (decls, sym) => decls.enter(sym); decls } |
| 72 | + |
| 73 | + case 2 => |
| 74 | + List( |
| 75 | + specApply(defn.UnitType, List(defn.IntType, defn.IntType)), |
| 76 | + specApply(defn.BooleanType, List(defn.IntType, defn.IntType)), |
| 77 | + specApply(defn.IntType, List(defn.IntType, defn.IntType)), |
| 78 | + specApply(defn.FloatType, List(defn.IntType, defn.IntType)), |
| 79 | + specApply(defn.LongType, List(defn.IntType, defn.IntType)), |
| 80 | + specApply(defn.DoubleType, List(defn.IntType, defn.IntType)), |
| 81 | + specApply(defn.UnitType, List(defn.IntType, defn.LongType)), |
| 82 | + specApply(defn.BooleanType, List(defn.IntType, defn.LongType)), |
| 83 | + specApply(defn.IntType, List(defn.IntType, defn.LongType)), |
| 84 | + specApply(defn.FloatType, List(defn.IntType, defn.LongType)), |
| 85 | + specApply(defn.LongType, List(defn.IntType, defn.LongType)), |
| 86 | + specApply(defn.DoubleType, List(defn.IntType, defn.LongType)), |
| 87 | + specApply(defn.UnitType, List(defn.IntType, defn.DoubleType)), |
| 88 | + specApply(defn.BooleanType, List(defn.IntType, defn.DoubleType)), |
| 89 | + specApply(defn.IntType, List(defn.IntType, defn.DoubleType)), |
| 90 | + specApply(defn.FloatType, List(defn.IntType, defn.DoubleType)), |
| 91 | + specApply(defn.LongType, List(defn.IntType, defn.DoubleType)), |
| 92 | + specApply(defn.DoubleType, List(defn.IntType, defn.DoubleType)), |
| 93 | + specApply(defn.UnitType, List(defn.LongType, defn.IntType)), |
| 94 | + specApply(defn.BooleanType, List(defn.LongType, defn.IntType)), |
| 95 | + specApply(defn.IntType, List(defn.LongType, defn.IntType)), |
| 96 | + specApply(defn.FloatType, List(defn.LongType, defn.IntType)), |
| 97 | + specApply(defn.LongType, List(defn.LongType, defn.IntType)), |
| 98 | + specApply(defn.DoubleType, List(defn.LongType, defn.IntType)), |
| 99 | + specApply(defn.UnitType, List(defn.LongType, defn.LongType)), |
| 100 | + specApply(defn.BooleanType, List(defn.LongType, defn.LongType)), |
| 101 | + specApply(defn.IntType, List(defn.LongType, defn.LongType)), |
| 102 | + specApply(defn.FloatType, List(defn.LongType, defn.LongType)), |
| 103 | + specApply(defn.LongType, List(defn.LongType, defn.LongType)), |
| 104 | + specApply(defn.DoubleType, List(defn.LongType, defn.LongType)), |
| 105 | + specApply(defn.UnitType, List(defn.LongType, defn.DoubleType)), |
| 106 | + specApply(defn.BooleanType, List(defn.LongType, defn.DoubleType)), |
| 107 | + specApply(defn.IntType, List(defn.LongType, defn.DoubleType)), |
| 108 | + specApply(defn.FloatType, List(defn.LongType, defn.DoubleType)), |
| 109 | + specApply(defn.LongType, List(defn.LongType, defn.DoubleType)), |
| 110 | + specApply(defn.DoubleType, List(defn.LongType, defn.DoubleType)), |
| 111 | + specApply(defn.UnitType, List(defn.DoubleType, defn.IntType)), |
| 112 | + specApply(defn.BooleanType, List(defn.DoubleType, defn.IntType)), |
| 113 | + specApply(defn.IntType, List(defn.DoubleType, defn.IntType)), |
| 114 | + specApply(defn.FloatType, List(defn.DoubleType, defn.IntType)), |
| 115 | + specApply(defn.LongType, List(defn.DoubleType, defn.IntType)), |
| 116 | + specApply(defn.DoubleType, List(defn.DoubleType, defn.IntType)), |
| 117 | + specApply(defn.UnitType, List(defn.DoubleType, defn.LongType)), |
| 118 | + specApply(defn.BooleanType, List(defn.DoubleType, defn.LongType)), |
| 119 | + specApply(defn.IntType, List(defn.DoubleType, defn.LongType)), |
| 120 | + specApply(defn.FloatType, List(defn.DoubleType, defn.LongType)), |
| 121 | + specApply(defn.LongType, List(defn.DoubleType, defn.LongType)), |
| 122 | + specApply(defn.DoubleType, List(defn.DoubleType, defn.LongType)), |
| 123 | + specApply(defn.UnitType, List(defn.DoubleType, defn.DoubleType)), |
| 124 | + specApply(defn.BooleanType, List(defn.DoubleType, defn.DoubleType)), |
| 125 | + specApply(defn.IntType, List(defn.DoubleType, defn.DoubleType)), |
| 126 | + specApply(defn.FloatType, List(defn.DoubleType, defn.DoubleType)), |
| 127 | + specApply(defn.LongType, List(defn.DoubleType, defn.DoubleType)), |
| 128 | + specApply(defn.DoubleType, List(defn.DoubleType, defn.DoubleType)) |
| 129 | + ) |
| 130 | + .foldLeft(tp.decls.cloneScope){ (decls, sym) => decls.enter(sym); decls } |
| 131 | + |
| 132 | + case _ => |
| 133 | + tp.decls |
| 134 | + } |
| 135 | + |
| 136 | + tp.derivedClassInfo(decls = newDecls) |
| 137 | + } |
| 138 | + case _ => tp |
| 139 | + } |
| 140 | +} |
0 commit comments