|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | + |
| 4 | +import ast.Trees._, ast.tpd, core._ |
| 5 | +import Contexts._, Types._, Decorators._, Symbols._, DenotTransformers._ |
| 6 | +import SymDenotations._, Scopes._, StdNames._, NameOps._, Names._ |
| 7 | +import MegaPhase.MiniPhase |
| 8 | + |
| 9 | +import scala.collection.mutable |
| 10 | + |
| 11 | + |
| 12 | +/** This phase synthesizes specialized methods for FunctionN, this is done |
| 13 | + * since there are no scala signatures in the bytecode for the specialized |
| 14 | + * methods. |
| 15 | + * |
| 16 | + * We know which specializations exist for the different arities, therefore we |
| 17 | + * can hardcode them. This should, however be removed once we're using a |
| 18 | + * different standard library. |
| 19 | + */ |
| 20 | +class SpecializeApplyMethods extends MiniPhase with InfoTransformer { |
| 21 | + import ast.tpd._ |
| 22 | + |
| 23 | + val phaseName = "specializeApplyMethods" |
| 24 | + |
| 25 | + override def isEnabled(using Context): Boolean = |
| 26 | + !ctx.settings.scalajs.value |
| 27 | + |
| 28 | + private def specApplySymbol(sym: Symbol, args: List[Type], ret: Type)(using Context): Symbol = { |
| 29 | + val name = nme.apply.specializedFunction(ret, args) |
| 30 | + // Create the symbol at the next phase, so that it is a valid member of the |
| 31 | + // corresponding function for all valid periods of its SymDenotations. |
| 32 | + // Otherwise, the valid period will offset by 1, which causes a stale symbol |
| 33 | + // in compiling stdlib. |
| 34 | + atNextPhase(newSymbol(sym, name, Flags.Method, MethodType(args, ret))) |
| 35 | + } |
| 36 | + |
| 37 | + private inline def specFun0(inline op: Type => Unit)(using Context): Unit = { |
| 38 | + for (r <- defn.Function0SpecializedReturnTypes) do |
| 39 | + op(r) |
| 40 | + } |
| 41 | + |
| 42 | + private inline def specFun1(inline op: (Type, Type) => Unit)(using Context): Unit = { |
| 43 | + for |
| 44 | + r <- defn.Function1SpecializedReturnTypes |
| 45 | + t1 <- defn.Function1SpecializedParamTypes |
| 46 | + do |
| 47 | + op(t1, r) |
| 48 | + } |
| 49 | + |
| 50 | + private inline def specFun2(inline op: (Type, Type, Type) => Unit)(using Context): Unit = { |
| 51 | + for |
| 52 | + r <- defn.Function2SpecializedReturnTypes |
| 53 | + t1 <- defn.Function2SpecializedParamTypes |
| 54 | + t2 <- defn.Function2SpecializedParamTypes |
| 55 | + do |
| 56 | + op(t1, t2, r) |
| 57 | + } |
| 58 | + |
| 59 | + override def infoMayChange(sym: Symbol)(using Context) = |
| 60 | + sym == defn.Function0 |
| 61 | + || sym == defn.Function1 |
| 62 | + || sym == defn.Function2 |
| 63 | + |
| 64 | + /** Add symbols for specialized methods to FunctionN */ |
| 65 | + override def transformInfo(tp: Type, sym: Symbol)(using Context) = tp match { |
| 66 | + case tp: ClassInfo => |
| 67 | + if sym == defn.Function0 then |
| 68 | + val scope = tp.decls.cloneScope |
| 69 | + specFun0 { r => scope.enter(specApplySymbol(sym, Nil, r)) } |
| 70 | + tp.derivedClassInfo(decls = scope) |
| 71 | + |
| 72 | + else if sym == defn.Function1 then |
| 73 | + val scope = tp.decls.cloneScope |
| 74 | + specFun1 { (t1, r) => scope.enter(specApplySymbol(sym, t1 :: Nil, r)) } |
| 75 | + tp.derivedClassInfo(decls = scope) |
| 76 | + |
| 77 | + else if sym == defn.Function2 then |
| 78 | + val scope = tp.decls.cloneScope |
| 79 | + specFun2 { (t1, t2, r) => scope.enter(specApplySymbol(sym, t1 :: t2 :: Nil, r)) } |
| 80 | + tp.derivedClassInfo(decls = scope) |
| 81 | + |
| 82 | + else tp |
| 83 | + |
| 84 | + case _ => tp |
| 85 | + } |
| 86 | + |
| 87 | + /** Create bridge methods for FunctionN with specialized applys */ |
| 88 | + override def transformTemplate(tree: Template)(using Context) = { |
| 89 | + val cls = tree.symbol.owner.asClass |
| 90 | + |
| 91 | + def synthesizeApply(names: collection.Set[TermName]): Tree = { |
| 92 | + val applyBuf = new mutable.ListBuffer[DefDef] |
| 93 | + names.foreach { name => |
| 94 | + val applySym = cls.info.decls.lookup(name) |
| 95 | + val ddef = DefDef( |
| 96 | + applySym.asTerm, |
| 97 | + { vparamss => |
| 98 | + This(cls) |
| 99 | + .select(nme.apply) |
| 100 | + .appliedToArgss(vparamss) |
| 101 | + .ensureConforms(applySym.info.finalResultType) |
| 102 | + } |
| 103 | + ) |
| 104 | + applyBuf += ddef |
| 105 | + } |
| 106 | + cpy.Template(tree)(body = tree.body ++ applyBuf) |
| 107 | + } |
| 108 | + |
| 109 | + if cls == defn.Function0 then |
| 110 | + synthesizeApply(defn.Function0SpecializedApplyNames) |
| 111 | + else if cls == defn.Function1 then |
| 112 | + synthesizeApply(defn.Function1SpecializedApplyNames) |
| 113 | + else if cls == defn.Function2 then |
| 114 | + synthesizeApply(defn.Function2SpecializedApplyNames) |
| 115 | + else |
| 116 | + tree |
| 117 | + } |
| 118 | +} |
0 commit comments