|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | + |
| 4 | +import ast.Trees._ |
| 5 | +import core.Constants.Constant |
| 6 | +import core.Contexts.Context |
| 7 | +import core.Definitions.MaxImplementedTupleArity |
| 8 | +import core.StdNames._ |
| 9 | +import core.Symbols._ |
| 10 | +import core.Types._ |
| 11 | +import transform.TreeTransforms.{MiniPhaseTransform, TransformerInfo} |
| 12 | + |
| 13 | +/** Local rewrites for tuple expressions. Nested apply and unapply trees coming |
| 14 | + * from desugaring into single apply/unapply nodes on DottyTupleN/LargeTuple. |
| 15 | + */ |
| 16 | +class TupleRewrites extends MiniPhaseTransform { |
| 17 | + import ast.tpd._ |
| 18 | + import TupleRewrites._ |
| 19 | + |
| 20 | + def phaseName: String = "tupleRewrites" |
| 21 | + |
| 22 | + override def checkPostCondition(tree: Tree)(implicit ctx: Context): Unit = |
| 23 | + tree match { |
| 24 | + case Select(ident, _) if ident.symbol == defn.TupleConsModule && start != end => |
| 25 | + assert(false, s"Reference to TupleCons comming from desugaring survived tupleRewrites.") |
| 26 | + case _ => () |
| 27 | + } |
| 28 | + |
| 29 | + /** Rewrites `TupleCons(a, TupleCons(b, ..., TNit))` to implementation specific constructors. |
| 30 | + * |
| 31 | + * Below `MaxImplementedTupleArity`, they become `DottyTuple$i(a, b, ...)`. |
| 32 | + * Above `MaxImplementedTupleArity`, they become `LargeTuple(Array.apply(a, b, ...)`. |
| 33 | + * |
| 34 | + * Note that because of bottom up traversal, the transformation of a tuple constructor of size `N` |
| 35 | + * will go thought this transformation `N` times, thus generating `N` `TupleCons(a, opt)` where `opt` |
| 36 | + * is the optimized transformation for previous arity. |
| 37 | + */ |
| 38 | + override def transformApply(tree: Apply)(implicit ctx: Context, info: TransformerInfo): Tree = { |
| 39 | + // Matches a tree with shape `TupleCons.apply(head, tail)` where `tail` itself a tuple |
| 40 | + // with statically known lenght (Unit, DottyTuple1, DottyTuple2...). */ |
| 41 | + object TupleApplies { |
| 42 | + def unapply(tree: Apply)(implicit ctx: Context): Option[(List[Tree], TupleType)] = |
| 43 | + tree match { |
| 44 | + case Apply(TypeApply(Select(ident, nme.apply), fstTpe :: _), head :: tail :: Nil) |
| 45 | + if ident.symbol == defn.TupleConsModule => |
| 46 | + tail match { |
| 47 | + case Literal(Constant(())) => |
| 48 | + Some((head :: Nil, UnfoldedTupleType(fstTpe.tpe :: Nil))) |
| 49 | + |
| 50 | + case Typed(Apply(TypeApply(Select(tailIdent, nme.apply), tailTpes), args), tt) |
| 51 | + if defn.DottyTupleNModule contains tailIdent.symbol => |
| 52 | + Some((head :: args, UnfoldedTupleType(fstTpe.tpe :: tailTpes.map(_.tpe)))) |
| 53 | + |
| 54 | + case Typed(Apply(TypeApply(Select(tailIdent, nme.wrap), tailTpes), SeqLiteral(args, _) :: Nil), _) |
| 55 | + if tailIdent.symbol == defn.LargeTupleModule => |
| 56 | + val foldedTailType = defn.TupleConsType.safeAppliedTo(tailTpes.map(_.tpe)) |
| 57 | + Some((head :: args, FoldedTupleType(fstTpe.tpe, foldedTailType))) |
| 58 | + |
| 59 | + case _ => None |
| 60 | + } |
| 61 | + case _ => None |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + tree match { |
| 66 | + case TupleApplies(args, types) => |
| 67 | + val arity = args.length |
| 68 | + val newSelect = |
| 69 | + if (arity <= MaxImplementedTupleArity) |
| 70 | + ref(defn.DottyTupleNType(arity).classSymbol.companionModule) // DottyTuple${arity}(args) |
| 71 | + .select(nme.apply) |
| 72 | + .appliedToTypes(types.unfolded.types) |
| 73 | + .appliedToArgs(args) |
| 74 | + else |
| 75 | + ref(defn.LargeTupleType.classSymbol.companionModule) // LargeTuple.wrap(args) |
| 76 | + .select(nme.wrap) |
| 77 | + .appliedToTypes(types.folded.asArgumentList) |
| 78 | + .appliedTo(SeqLiteral(args, ref(defn.AnyType))) |
| 79 | + Typed(newSelect, TypeTree(tree.tpe)) |
| 80 | + case _ => tree |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Matches a tree with shape `TupleCons.unapply(head, tail)` where `tail` |
| 85 | + // itself a tuple with statically known length (`Unit`, `DottyTuple1`, |
| 86 | + // `DottyTuple2`...). Extracts the trees and types corresponding to each |
| 87 | + // tuple element. |
| 88 | + // |
| 89 | + // Note that the hlist representation contains more type information than |
| 90 | + // the scala.tupleN one, indeed, with C <: B one could write the following: |
| 91 | + // |
| 92 | + // TupleCons[A, TupleCons[B, Unit]](a, TupleCons[C, Unit](c, ())) |
| 93 | + // |
| 94 | + // This transformation keeps the more precise type, such that the example |
| 95 | + // above is rewritten to scala.Tuple2[A, C](a, b) (using C and not B). |
| 96 | + private object TupleUnapplies { |
| 97 | + def unapply(tree: UnApply)(implicit ctx: Context): Option[(List[Tree], TupleType)] = |
| 98 | + tree match { |
| 99 | + case UnApply(TypeApply(Select(selectIndent, nme.unapply), fstTpe :: _), Nil, fstPat :: sndPat :: Nil) |
| 100 | + if selectIndent.symbol == defn.TupleConsModule => |
| 101 | + sndPat match { |
| 102 | + case Literal(Constant(())) => |
| 103 | + Some((List(fstPat), UnfoldedTupleType(fstTpe.tpe :: Nil))) |
| 104 | + |
| 105 | + case UnApply(TypeApply(Select(ident, nme.unapply), tailTpes), Nil, tailPats) |
| 106 | + if defn.DottyTupleNModule contains ident.symbol => |
| 107 | + Some((fstPat :: tailPats, UnfoldedTupleType(fstTpe.tpe :: tailTpes.map(_.tpe)))) |
| 108 | + |
| 109 | + case UnApply(TypeApply(Select(ident, nme.unapplySeq), tailTpes), Nil, tailPat) |
| 110 | + if ident.symbol == defn.TupleUnapplySeqModule => |
| 111 | + val foldedTailType = defn.TupleConsType.safeAppliedTo(tailTpes.map(_.tpe)) |
| 112 | + Some((fstPat :: tailPat, FoldedTupleType(fstTpe.tpe, foldedTailType))) |
| 113 | + |
| 114 | + case Typed(UnApply(TypeApply(Select(ident, nme.unapply), tailTpes), Nil, tailPats), _) |
| 115 | + if defn.DottyTupleNModule contains ident.symbol => |
| 116 | + Some((fstPat :: tailPats, UnfoldedTupleType(fstTpe.tpe :: tailTpes.map(_.tpe)))) |
| 117 | + |
| 118 | + case Typed(UnApply(TypeApply(Select(ident, nme.unapplySeq), tailTpes), Nil, tailPats), _) |
| 119 | + if ident.symbol == defn.TupleUnapplySeqModule => |
| 120 | + val foldedTailType = defn.TupleConsType.safeAppliedTo(tailTpes.map(_.tpe)) |
| 121 | + Some((fstPat :: tailPats, FoldedTupleType(fstTpe.tpe, foldedTailType))) |
| 122 | + |
| 123 | + case _ => None |
| 124 | + } |
| 125 | + case _ => None |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** Rewrites `TupleCons.unapply(a, TupleCons.unapply(b, ..., TNit))` to implementation specific extractors. |
| 130 | + * |
| 131 | + * Below `MaxImplementedTupleArity`, they become `DottyTuple$i.unapply(a, b, ...)`. |
| 132 | + * Above `MaxImplementedTupleArity`, they become `TupleUnapplySeq.unapply(a, b, ...)`. |
| 133 | + * |
| 134 | + * Similarly to `transformApply`, size `N` extractors will pass `N` times thought this transformation. |
| 135 | + */ |
| 136 | + override def transformUnApply(tree: UnApply)(implicit ctx: Context, info: TransformerInfo): Tree = |
| 137 | + tree match { |
| 138 | + case TupleUnapplies(patterns, types) => |
| 139 | + transformUnApplyPatterns(tree, patterns, types) |
| 140 | + case _ => tree |
| 141 | + } |
| 142 | + |
| 143 | + /** Same then `transformUnApply` for unapply wrapped in Typed trees. */ |
| 144 | + override def transformTyped(tree: Typed)(implicit ctx: Context, info: TransformerInfo): Tree = |
| 145 | + tree match { |
| 146 | + case Typed(TupleUnapplies(patterns, types), tpe) => |
| 147 | + val unapply = transformUnApplyPatterns(tree, patterns, types) |
| 148 | + Typed(unapply, tpe) |
| 149 | + case _ => tree |
| 150 | + } |
| 151 | + |
| 152 | + // Create an `UnApply` tree from a list of patters, used in both transformUnApply and transformTyped. |
| 153 | + private def transformUnApplyPatterns(tree: Tree, patterns: List[Tree], types: TupleType)(implicit ctx: Context): UnApply = { |
| 154 | + val arity = patterns.length |
| 155 | + if (arity <= MaxImplementedTupleArity) { |
| 156 | + val unfoldedTypes: List[Type] = types.unfolded.types |
| 157 | + val refinedType = defn.TupleNType(arity).safeAppliedTo(unfoldedTypes) |
| 158 | + val newCall = // DottyTuple${arity}.unapply(patterns) |
| 159 | + ref(defn.DottyTupleNType(arity).classSymbol.companionModule) |
| 160 | + .select(nme.unapply) |
| 161 | + .appliedToTypes(unfoldedTypes) |
| 162 | + UnApply(fun = newCall, implicits = Nil, patterns = patterns, proto = refinedType) |
| 163 | + } else { |
| 164 | + val newCall = // TupleUnapplySeq.unapplySeq(patterns) |
| 165 | + ref(defn.TupleUnapplySeqType.classSymbol.companionModule) |
| 166 | + .select(nme.unapplySeq) |
| 167 | + .appliedToTypes(types.folded.asArgumentList) |
| 168 | + UnApply(fun = newCall, implicits = Nil, patterns = patterns, proto = tree.tpe) |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | +} |
| 173 | + |
| 174 | +object TupleRewrites { |
| 175 | + /** Helper to go back and forth between representations of tuple types. |
| 176 | + * `.folded` is `head :: tail :: Nil` where tail is a big hlist type |
| 177 | + * `.unfolded` is a flat list of every type in the tuple. |
| 178 | + */ |
| 179 | + sealed trait TupleType { |
| 180 | + def folded(implicit ctx: Context): FoldedTupleType |
| 181 | + def unfolded(implicit ctx: Context): UnfoldedTupleType |
| 182 | + } |
| 183 | + |
| 184 | + case class FoldedTupleType(head: Type, tail: Type) extends TupleType { |
| 185 | + def asArgumentList: List[Type] = head :: tail :: Nil |
| 186 | + |
| 187 | + def asTupleConsType(implicit ctx: Context): Type = defn.TupleConsType.safeAppliedTo(head :: tail :: Nil) |
| 188 | + |
| 189 | + def folded(implicit ctx: Context): FoldedTupleType = this |
| 190 | + |
| 191 | + def unfolded(implicit ctx: Context): UnfoldedTupleType = { |
| 192 | + def unfold(acc: List[Type], next: Type): List[Type] = next match { |
| 193 | + case RefinedType(RefinedType(_, _, TypeAlias(headType)), _, TypeAlias(tailType)) => |
| 194 | + unfold(headType :: acc, tailType) |
| 195 | + case _ => acc |
| 196 | + } |
| 197 | + UnfoldedTupleType(unfold(List(head), tail).reverse) |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + case class UnfoldedTupleType(types: List[Type]) extends TupleType { |
| 202 | + def unfolded(implicit ctx: Context): UnfoldedTupleType = this |
| 203 | + |
| 204 | + def folded(implicit ctx: Context): FoldedTupleType = |
| 205 | + FoldedTupleType( |
| 206 | + types.head, |
| 207 | + types.tail |
| 208 | + .map(t => TypeAlias(t, variance = 1)) |
| 209 | + .reverse |
| 210 | + .foldLeft[Type](defn.UnitType) { |
| 211 | + case (acc, el) => defn.TupleConsType.safeAppliedTo(el :: acc :: Nil) |
| 212 | + } |
| 213 | + ) |
| 214 | + } |
| 215 | +} |
0 commit comments