|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | + |
| 4 | +import core._ |
| 5 | +import Constants.Constant |
| 6 | +import Contexts.Context |
| 7 | +import Decorators._ |
| 8 | +import Flags._ |
| 9 | +import ast.Trees._ |
| 10 | +import Definitions._ |
| 11 | +import DenotTransformers._ |
| 12 | +import StdNames._ |
| 13 | +import Symbols._ |
| 14 | +import MegaPhase._ |
| 15 | +import Types._ |
| 16 | +import dotty.tools.dotc.ast.tpd |
| 17 | + |
| 18 | + |
| 19 | +import scala.annotation.tailrec |
| 20 | + |
| 21 | +/** TODO |
| 22 | + */ |
| 23 | +class GenericTuples extends MiniPhase with IdentityDenotTransformer { |
| 24 | + import tpd._ |
| 25 | + |
| 26 | + def phaseName: String = "genericTuples" |
| 27 | + |
| 28 | + override def transformApply(tree: tpd.Apply)(implicit ctx: Context): tpd.Tree = { |
| 29 | + if (tree.symbol == defn.Tuple_cons) transformTupleCons(tree) |
| 30 | + else super.transformApply(tree) |
| 31 | + } |
| 32 | + |
| 33 | +// override def transformTypeApply(tree: tpd.TypeApply)(implicit ctx: Context): tpd.Tree = { |
| 34 | +// if (tree.symbol == defn.NonEmptyTuple_tail) transformTupleTail(tree) |
| 35 | +// else super.transformTypeApply(tree) |
| 36 | +// } |
| 37 | + |
| 38 | + private def transformTupleCons(tree: tpd.Apply)(implicit ctx: Context): Tree = { |
| 39 | + val TypeApply(Select(qual, _), headType :: tailType :: Nil) = tree.fun |
| 40 | + tupleTypes(tree.tpe) match { |
| 41 | + case Some(tpes) => |
| 42 | + val size = tpes.size |
| 43 | + if (size <= 5) { |
| 44 | + // val t = tail.asInstanceOf[TupleN[...]] |
| 45 | + // TupleN+1(head, t._1, ..., t._n) |
| 46 | + val tailType = |
| 47 | + if (size == 1) defn.UnitType |
| 48 | + else defn.TupleType(size - 1).appliedTo(tpes.tail) |
| 49 | + evalOnce(Typed(qual, TypeTree(tailType))) { tup => |
| 50 | + val elements = tree.args.head :: (0 until size - 1).map(i => tup.select(nme.selectorName(i))).toList |
| 51 | + knownTupleFromElements(tpes, elements) |
| 52 | + } |
| 53 | + } else { |
| 54 | + // val it = Iterator.single(head) ++ tail.asInstanceOf[Product].productIterator |
| 55 | + // TupleN(it.next(), ..., it.next()) |
| 56 | + val fullIterator = ref(defn.DynamicTuple_consIterator).appliedToArgs(tree.args.head :: qual :: Nil) |
| 57 | + evalOnce(fullIterator) { it => |
| 58 | + knownTupleFromIterator(tpes.length, it).asInstance(tree.tpe) |
| 59 | + } |
| 60 | + } |
| 61 | + case _ => |
| 62 | + // DynamicTuple.dynamic_*:(tail, head) |
| 63 | + ref(defn.DynamicTupleModule).select("dynamic_*:".toTermName).appliedToTypeTrees(tailType :: headType :: Nil).appliedToArgs(qual :: tree.args).asInstance(tree.tpe) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | +// private def transformTupleTail(tree: tpd.TypeApply)(implicit ctx: Context): Tree = { |
| 68 | +// val Select(tup, _) = tree.fun |
| 69 | +// tupleTypes(tree.args.head.tpe) match { // TODO tupleBoundedTypes |
| 70 | +// case Some(tpes) => |
| 71 | +// val size = tpes.size |
| 72 | +// if (size <= 5) { |
| 73 | +// // val t = tup.asInstanceOf[TupleN[...]] |
| 74 | +// // TupleN-1(t._2, ..., t._n) |
| 75 | +// evalOnce(tup.asInstance(defn.TupleType(size).appliedTo(tpes))) { tup => |
| 76 | +// val elements = (1 until size).map(i => tup.select(nme.selectorName(i))).toList |
| 77 | +// knownTupleFromElements(tpes.tail, elements) |
| 78 | +// } |
| 79 | +// } else { |
| 80 | +// // val it = this.asInstanceOf[Product].productIterator |
| 81 | +// // it.next() |
| 82 | +// // TupleN(it.next(), ..., it.next()) |
| 83 | +// evalOnce(tup.asInstance(defn.ProductType).select(nme.productIterator)) { it => |
| 84 | +// Block( |
| 85 | +// it.select(nme.next).ensureApplied :: Nil, |
| 86 | +// knownTupleFromIterator(size - 1, it).asInstance(tree.tpe) |
| 87 | +// ) |
| 88 | +// } |
| 89 | +// } |
| 90 | +// case None => |
| 91 | +// // DynamicTuple.dynamicTail(tup) |
| 92 | +// ref(defn.DynamicTuple_dynamicTail).appliedToType(tree.tpe).appliedToArgs(tup :: Nil) |
| 93 | +// } |
| 94 | +// } |
| 95 | + |
| 96 | + /** Create a TupleN (1 <= N < 23) from the elements */ |
| 97 | + private def knownTupleFromElements(tpes: List[Type], elements: List[Tree])(implicit ctx: Context) = { |
| 98 | + val size = elements.size |
| 99 | + assert(0 < size && size <= Definitions.MaxTupleArity) |
| 100 | + val tupleModule = defn.TupleType(size).classSymbol.companionModule |
| 101 | + ref(tupleModule).select(nme.apply).appliedToTypes(tpes).appliedToArgs(elements) |
| 102 | + } |
| 103 | + |
| 104 | + private def knownTupleFromIterator(size: Int, it: Tree)(implicit ctx: Context): Tree = { |
| 105 | + if (size == 0) { |
| 106 | + // Unit for empty tuple |
| 107 | + Literal(Constant(())) // TODO should this code be here? Or assert(size > specializedSize) |
| 108 | + } |
| 109 | + else if (size <= Definitions.MaxTupleArity) { |
| 110 | + // TupleN(it.next(), ..., it.next()) |
| 111 | + |
| 112 | + // TODO outline this code for the 22 alternatives (or less, may not need the smallest ones)? |
| 113 | + // This would yield smaller bytecode at the cost of an extra (easily JIT inlinable) call. |
| 114 | + // def dynamicTupleN(it: Iterator[Any]): TupleN[Any, ..., Any] = Tuple(it.next(), ..., it.next()) |
| 115 | + val tpes = List.fill(size)(defn.AnyType) |
| 116 | + val elements = (0 until size).map(_ => it.select(nme.next)).toList |
| 117 | + knownTupleFromElements(tpes, elements) |
| 118 | + } else { |
| 119 | + // TupleXXL.fromIterator(it) |
| 120 | + ref(defn.TupleXXL_fromIterator).appliedTo(it) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private def tupleTypes(tp: Type)(implicit ctx: Context): Option[List[Type]] = { |
| 125 | + @tailrec def rec(tp: Type, acc: List[Type]): Option[List[Type]] = tp match { |
| 126 | + case tp: AppliedType if defn.PairClass == tp.classSymbol => rec(tp.args(1), tp.args(0) :: acc) |
| 127 | + case tp: AppliedType if defn.isTupleClass(tp.tycon.classSymbol) => Some(acc.reverse ::: tp.args) |
| 128 | + case tp if tp.classSymbol == defn.UnitClass => Some(acc.reverse) |
| 129 | + case _ => None |
| 130 | + } |
| 131 | + rec(tp.stripTypeVar, Nil) |
| 132 | + } |
| 133 | +} |
0 commit comments