summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-12-04 16:53:46 +0000
committerPaul Phillips <paulp@improving.org>2010-12-04 16:53:46 +0000
commit4e3a930c04c9680be0e8d5d1114d0f0a3f8ea4a1 (patch)
treec6cf511f80cf5e996f2858d08e48582e1b65911c
parent72b2715324a8d44cce5beac6e5911574eeb3fb5b (diff)
downloadscala-4e3a930c04c9680be0e8d5d1114d0f0a3f8ea4a1.tar.gz
scala-4e3a930c04c9680be0e8d5d1114d0f0a3f8ea4a1.tar.bz2
scala-4e3a930c04c9680be0e8d5d1114d0f0a3f8ea4a1.zip
Segregated all the android related code in a ne...
Segregated all the android related code in a new file and integrated it with genjvm. Review by michelou.
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/GenAndroid.scala56
-rw-r--r--src/compiler/scala/tools/nsc/backend/jvm/GenJVM.scala36
-rw-r--r--src/compiler/scala/tools/nsc/symtab/Definitions.scala9
3 files changed, 62 insertions, 39 deletions
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/GenAndroid.scala b/src/compiler/scala/tools/nsc/backend/jvm/GenAndroid.scala
new file mode 100644
index 0000000000..e0a06fa076
--- /dev/null
+++ b/src/compiler/scala/tools/nsc/backend/jvm/GenAndroid.scala
@@ -0,0 +1,56 @@
+/* NSC -- new Scala compiler
+ * Copyright 2005-2010 LAMP/EPFL
+ */
+
+package scala.tools.nsc
+package backend.jvm
+
+import ch.epfl.lamp.fjbg._
+import symtab.Flags
+
+trait GenAndroid {
+ self: GenJVM =>
+
+ import global._
+ import icodes._
+ import opcodes._
+
+ private val fieldName = "CREATOR"
+ private lazy val AndroidParcelableInterface =
+ try definitions.getClass("android.os.Parcelable")
+ catch { case _: FatalError => NoSymbol }
+ private lazy val AndroidCreatorClass =
+ if (AndroidParcelableInterface == NoSymbol) NoSymbol
+ else definitions.getClass("android.os.Parcelable$Creator")
+
+ def isAndroidParcelableClass(sym: Symbol) = (
+ (AndroidParcelableInterface != NoSymbol) &&
+ (sym.info.parents contains AndroidParcelableInterface.tpe)
+ )
+
+ def addCreatorCode(codegen: BytecodeGenerator, block: BasicBlock) = {
+ import codegen._
+ val fieldSymbol = clasz.symbol.newValue(NoPosition, newTermName(fieldName))
+ .setFlag(Flags.STATIC | Flags.FINAL)
+ .setInfo(AndroidCreatorClass.tpe)
+ val methodSymbol = definitions.getMember(clasz.symbol.companionModule, fieldName)
+ clasz addField new IField(fieldSymbol)
+ block emit CALL_METHOD(methodSymbol, Static(false))
+ block emit STORE_FIELD(fieldSymbol, true)
+ }
+
+ def legacyAddCreatorCode(codegen: BytecodeGenerator, clinit: JExtendedCode) = {
+ import codegen._
+ val creatorType = javaType(AndroidCreatorClass)
+ jclass.addNewField(PublicStaticFinal,
+ fieldName,
+ creatorType)
+ val moduleName = javaName(clasz.symbol)+"$"
+ clinit.emitGETSTATIC(moduleName,
+ nme.MODULE_INSTANCE_FIELD.toString,
+ new JObjectType(moduleName))
+ clinit.emitINVOKEVIRTUAL(moduleName, fieldName,
+ new JMethodType(creatorType, Array()))
+ clinit.emitPUTSTATIC(jclass.getName(), fieldName, creatorType)
+ }
+}
diff --git a/src/compiler/scala/tools/nsc/backend/jvm/GenJVM.scala b/src/compiler/scala/tools/nsc/backend/jvm/GenJVM.scala
index 4fd59ba0b0..dfd9feafc2 100644
--- a/src/compiler/scala/tools/nsc/backend/jvm/GenJVM.scala
+++ b/src/compiler/scala/tools/nsc/backend/jvm/GenJVM.scala
@@ -26,7 +26,7 @@ import reflect.generic.{ PickleFormat, PickleBuffer }
* @version 1.0
*
*/
-abstract class GenJVM extends SubComponent {
+abstract class GenJVM extends SubComponent with GenAndroid {
val global: Global
import global._
import icodes._
@@ -36,7 +36,6 @@ abstract class GenJVM extends SubComponent {
AnyClass, ObjectClass, ThrowsClass, ThrowableClass, ClassfileAnnotationClass,
SerializableClass, StringClass, ClassClass, FunctionClass,
DeprecatedAttr, SerializableAttr, SerialVersionUIDAttr, VolatileAttr,
- AndroidParcelableInterface, AndroidCreatorClass,
TransientAttr, CloneableAttr, RemoteAttr,
getPrimitiveCompanion
}
@@ -206,8 +205,7 @@ abstract class GenJVM extends SubComponent {
val name = javaName(c.symbol)
serialVUID = None
isRemoteClass = false
- isParcelableClass = (AndroidParcelableInterface != NoSymbol) &&
- (parents contains AndroidParcelableInterface.tpe)
+ isParcelableClass = isAndroidParcelableClass(c.symbol)
if (parents.isEmpty)
parents = List(ObjectClass.tpe)
@@ -804,17 +802,8 @@ abstract class GenJVM extends SubComponent {
lastBlock emit STORE_FIELD(fieldSymbol, true)
}
- // add CREATOR code
- if (isParcelableClass) {
- val fieldName = "CREATOR"
- val fieldSymbol = clasz.symbol.newValue(NoPosition, newTermName(fieldName))
- .setFlag(Flags.STATIC | Flags.FINAL)
- .setInfo(AndroidCreatorClass.tpe)
- val methodSymbol = definitions.getMember(clasz.symbol.companionModule, "CREATOR")
- clasz addField new IField(fieldSymbol)
- lastBlock emit CALL_METHOD(methodSymbol, Static(false))
- lastBlock emit STORE_FIELD(fieldSymbol, true)
- }
+ if (isParcelableClass)
+ addCreatorCode(BytecodeGenerator.this, lastBlock)
if (clasz.bootstrapClass.isDefined) {
// emit bootstrap method install
@@ -848,20 +837,8 @@ abstract class GenJVM extends SubComponent {
clinit.emitPUTSTATIC(jclass.getName(), fieldName, JType.LONG)
}
- if (isParcelableClass) {
- val fieldName = "CREATOR"
- val creatorType = javaType(AndroidCreatorClass)
- jclass.addNewField(PublicStaticFinal,
- fieldName,
- creatorType)
- val moduleName = javaName(clasz.symbol)+"$"
- clinit.emitGETSTATIC(moduleName,
- nme.MODULE_INSTANCE_FIELD.toString,
- new JObjectType(moduleName))
- clinit.emitINVOKEVIRTUAL(moduleName, "CREATOR",
- new JMethodType(creatorType, Array()))
- clinit.emitPUTSTATIC(jclass.getName(), fieldName, creatorType)
- }
+ if (isParcelableClass)
+ legacyAddCreatorCode(BytecodeGenerator.this, clinit)
if (clasz.bootstrapClass.isDefined)
legacyEmitBootstrapMethodInstall(clinit)
@@ -1895,7 +1872,6 @@ abstract class GenJVM extends SubComponent {
(sym.isJavaDefined && sym.isNonBottomSubClass(ClassfileAnnotationClass))
}
-
def javaType(t: TypeKind): JType = (t: @unchecked) match {
case UNIT => JType.VOID
case BOOL => JType.BOOLEAN
diff --git a/src/compiler/scala/tools/nsc/symtab/Definitions.scala b/src/compiler/scala/tools/nsc/symtab/Definitions.scala
index f756d25895..87f88d1c5b 100644
--- a/src/compiler/scala/tools/nsc/symtab/Definitions.scala
+++ b/src/compiler/scala/tools/nsc/symtab/Definitions.scala
@@ -500,15 +500,6 @@ trait Definitions extends reflect.generic.StandardDefinitions {
attr
}
- // Android. I moved it into definitions because it threw an expensive
- // exception on every repl line.
- lazy val AndroidParcelableInterface =
- try getClass("android.os.Parcelable")
- catch { case _: FatalError => NoSymbol }
- lazy val AndroidCreatorClass =
- if (AndroidParcelableInterface == NoSymbol) NoSymbol
- else getClass("android.os.Parcelable$Creator")
-
def getModule(fullname: Name): Symbol =
getModuleOrClass(fullname.toTermName)