Skip to content

Commit 7481149

Browse files
committed
Handle Arrays in backend.
1 parent f93b568 commit 7481149

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/dotty/tools/backend/jvm/DottyBackendInterface.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,17 @@ class DottyBackendInterface()(implicit ctx: Context) extends BackendInterface{
134134
(x, Erasure.Boxing.boxMethod(x.asClass))
135135
}.toMap
136136
def unboxMethods: Map[Symbol, Symbol] = defn.ScalaValueClasses.map(x => (x, Erasure.Boxing.unboxMethod(x.asClass))).toMap
137+
138+
private val mkArrayNames: Set[String] = Set("Byte", "Float", "Char", "Double", "Boolean", "Unit", "Long", "Int", "Short", "Ref")
139+
140+
override lazy val syntheticArrayConstructors: Set[Symbol] = mkArrayNames.map(nm => ctx.requiredMethod(toDenot(defn.DottyArraysModule).moduleClass.asClass, s"new${nm}Array"))
141+
137142
def isBox(sym: Symbol): Boolean = Erasure.Boxing.isBox(sym)
138143
def isUnbox(sym: Symbol): Boolean = Erasure.Boxing.isUnbox(sym)
139144

140145
val primitives: Primitives = new Primitives {
141146
val primitives = new DottyPrimitives(ctx)
142-
def getPrimitive(methodSym: Symbol, reciever: Type): Int = primitives.getPrimitive(methodSym, reciever)
147+
def getPrimitive(app: Apply, reciever: Type): Int = primitives.getPrimitive(app, reciever)
143148

144149
def getPrimitive(sym: Symbol): Int = primitives.getPrimitive(sym)
145150

src/dotty/tools/backend/jvm/scalaPrimitives.scala

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ package dotty.tools.dotc
77
package backend.jvm
88

99
import dotty.tools.backend.jvm.GenBCodePipeline
10+
import dotty.tools.dotc.ast.Trees.{Select, Apply}
11+
import dotty.tools.dotc.ast.tpd
1012
import dotty.tools.dotc.core.Names.TermName
11-
import dotty.tools.dotc.core.Types.{ErrorType, Type}
13+
import dotty.tools.dotc.core.StdNames._
14+
import dotty.tools.dotc.core.Types.{JavaArrayType, ErrorType, Type}
1215

1316
import scala.collection.{ mutable, immutable }
1417

@@ -52,12 +55,22 @@ class DottyPrimitives(ctx: Context) {
5255
* @param tpe The type of the receiver object. It is used only for array
5356
* operations
5457
*/
55-
def getPrimitive(fun: Symbol, tpe: Type)(implicit ctx: Context): Int = {
58+
def getPrimitive(app: tpd.Apply, tpe: Type)(implicit ctx: Context): Int = {
59+
val fun = app.fun.symbol
5660
val defn = ctx.definitions
57-
val code = getPrimitive(fun)
61+
val code = app.fun match {
62+
case Select(_, nme.primitive.arrayLength) =>
63+
LENGTH
64+
case Select(_, nme.primitive.arrayUpdate) =>
65+
UPDATE
66+
case Select(_, nme.primitive.arrayApply) =>
67+
APPLY
68+
case _ => getPrimitive(fun)
69+
}
5870

5971
def elementType: Type = tpe.widenDealias match {
6072
case defn.ArrayType(el) => el
73+
case JavaArrayType(el) => el
6174
case _ =>
6275
ctx.error(s"expected Array $tpe")
6376
ErrorType
@@ -391,7 +404,9 @@ class DottyPrimitives(ctx: Context) {
391404
primitives.toMap
392405
}
393406

394-
def isPrimitive(sym: Symbol): Boolean = primitives contains sym
407+
def isPrimitive(sym: Symbol): Boolean = {
408+
(primitives contains sym) || sym == NoSymbol // the only trees that do not have a symbol assigned are array.{update,select,length}
409+
}
395410

396411
}
397412

tests/pos/Arrays.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object Arrays{
2+
def main(a: Array[String]) = {
3+
val stringArr = new Array[String](1)
4+
stringArr(0) = "foo"
5+
val intArr = new Array[Int](1)
6+
stringArr(0) = intArr(0).toString
7+
}
8+
}

0 commit comments

Comments
 (0)