From d23cab1dcff8c28d4817f3dccfefbc8cb58f017f Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Mon, 1 Dec 2014 16:58:53 +0100 Subject: Handle Arrays in backend. --- .../tools/backend/jvm/DottyBackendInterface.scala | 7 ++++++- src/dotty/tools/backend/jvm/scalaPrimitives.scala | 23 ++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/dotty/tools/backend/jvm/DottyBackendInterface.scala b/src/dotty/tools/backend/jvm/DottyBackendInterface.scala index eb82e010e..c4b86719d 100644 --- a/src/dotty/tools/backend/jvm/DottyBackendInterface.scala +++ b/src/dotty/tools/backend/jvm/DottyBackendInterface.scala @@ -134,12 +134,17 @@ class DottyBackendInterface()(implicit ctx: Context) extends BackendInterface{ (x, Erasure.Boxing.boxMethod(x.asClass)) }.toMap def unboxMethods: Map[Symbol, Symbol] = defn.ScalaValueClasses.map(x => (x, Erasure.Boxing.unboxMethod(x.asClass))).toMap + + private val mkArrayNames: Set[String] = Set("Byte", "Float", "Char", "Double", "Boolean", "Unit", "Long", "Int", "Short", "Ref") + + override lazy val syntheticArrayConstructors: Set[Symbol] = mkArrayNames.map(nm => ctx.requiredMethod(toDenot(defn.DottyArraysModule).moduleClass.asClass, s"new${nm}Array")) + def isBox(sym: Symbol): Boolean = Erasure.Boxing.isBox(sym) def isUnbox(sym: Symbol): Boolean = Erasure.Boxing.isUnbox(sym) val primitives: Primitives = new Primitives { val primitives = new DottyPrimitives(ctx) - def getPrimitive(methodSym: Symbol, reciever: Type): Int = primitives.getPrimitive(methodSym, reciever) + def getPrimitive(app: Apply, reciever: Type): Int = primitives.getPrimitive(app, reciever) def getPrimitive(sym: Symbol): Int = primitives.getPrimitive(sym) diff --git a/src/dotty/tools/backend/jvm/scalaPrimitives.scala b/src/dotty/tools/backend/jvm/scalaPrimitives.scala index 150e672c7..feb74faf5 100755 --- a/src/dotty/tools/backend/jvm/scalaPrimitives.scala +++ b/src/dotty/tools/backend/jvm/scalaPrimitives.scala @@ -7,8 +7,11 @@ package dotty.tools.dotc package backend.jvm import dotty.tools.backend.jvm.GenBCodePipeline +import dotty.tools.dotc.ast.Trees.{Select, Apply} +import dotty.tools.dotc.ast.tpd import dotty.tools.dotc.core.Names.TermName -import dotty.tools.dotc.core.Types.{ErrorType, Type} +import dotty.tools.dotc.core.StdNames._ +import dotty.tools.dotc.core.Types.{JavaArrayType, ErrorType, Type} import scala.collection.{ mutable, immutable } @@ -52,12 +55,22 @@ class DottyPrimitives(ctx: Context) { * @param tpe The type of the receiver object. It is used only for array * operations */ - def getPrimitive(fun: Symbol, tpe: Type)(implicit ctx: Context): Int = { + def getPrimitive(app: tpd.Apply, tpe: Type)(implicit ctx: Context): Int = { + val fun = app.fun.symbol val defn = ctx.definitions - val code = getPrimitive(fun) + val code = app.fun match { + case Select(_, nme.primitive.arrayLength) => + LENGTH + case Select(_, nme.primitive.arrayUpdate) => + UPDATE + case Select(_, nme.primitive.arrayApply) => + APPLY + case _ => getPrimitive(fun) + } def elementType: Type = tpe.widenDealias match { case defn.ArrayType(el) => el + case JavaArrayType(el) => el case _ => ctx.error(s"expected Array $tpe") ErrorType @@ -391,7 +404,9 @@ class DottyPrimitives(ctx: Context) { primitives.toMap } - def isPrimitive(sym: Symbol): Boolean = primitives contains sym + def isPrimitive(sym: Symbol): Boolean = { + (primitives contains sym) || sym == NoSymbol // the only trees that do not have a symbol assigned are array.{update,select,length} + } } -- cgit v1.2.3