|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | + |
| 4 | +import core._ |
| 5 | +import TreeTransforms._ |
| 6 | +import Contexts.Context |
| 7 | +import Flags._ |
| 8 | +import SymUtils._ |
| 9 | +import Symbols._ |
| 10 | +import SymDenotations._ |
| 11 | +import Types._ |
| 12 | +import Decorators._ |
| 13 | +import DenotTransformers._ |
| 14 | +import StdNames._ |
| 15 | +import NameOps._ |
| 16 | +import ast.Trees._ |
| 17 | +import dotty.tools.dotc.ast.tpd |
| 18 | +import util.Positions._ |
| 19 | +import Names._ |
| 20 | +import collection.mutable |
| 21 | +import ResolveSuper._ |
| 22 | + |
| 23 | + |
| 24 | +/** This phase rewrites calls to array constructors to newArray and newGenericArray methods |
| 25 | + * in Dotty.runtime.Arrays module. |
| 26 | + * |
| 27 | + * Additionally it optimizes calls to scala.Array.ofDim functions |
| 28 | + * |
| 29 | + */ |
| 30 | +class ArrayConstructors extends MiniPhaseTransform { thisTransform => |
| 31 | + import ast.tpd._ |
| 32 | + |
| 33 | + override def phaseName: String = "arrayConstructors" |
| 34 | + |
| 35 | + override def transformApply(tree: tpd.Apply)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { |
| 36 | + def rewrite(elemType: Type, dims: List[Tree]) = |
| 37 | + tpd.newArray(elemType, tree.tpe, tree.pos, JavaSeqLiteral(dims, TypeTree(defn.IntClass.typeRef)).asInstanceOf[JavaSeqLiteral]) |
| 38 | + |
| 39 | + if (tree.fun.symbol eq defn.ArrayConstructor) { |
| 40 | + tree.fun match { |
| 41 | + case TypeApply(tycon, targ :: Nil) => |
| 42 | + rewrite(targ.tpe, tree.args) |
| 43 | + case _ => |
| 44 | + ??? |
| 45 | + } |
| 46 | + } else if ((tree.fun.symbol.maybeOwner eq defn.ArrayModule) && (tree.fun.symbol.name eq nme.ofDim) && !tree.tpe.isInstanceOf[MethodicType]) { |
| 47 | + |
| 48 | + tree.fun match { |
| 49 | + case Apply(TypeApply(t: Ident, targ), dims) => |
| 50 | + rewrite(targ.head.tpe, dims) |
| 51 | + case Apply(TypeApply(t: Select, targ), dims) => |
| 52 | + Block(t.qualifier :: Nil, rewrite(targ.head.tpe, dims)) |
| 53 | + case _ => tree |
| 54 | + } |
| 55 | + |
| 56 | + } else tree |
| 57 | + } |
| 58 | +} |
| 59 | + |
0 commit comments