-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Implement getClass #734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Implement getClass #734
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package dotty.tools.dotc | ||
package transform | ||
|
||
import ast.tpd | ||
import core.Contexts.Context | ||
import core.StdNames.nme | ||
import core.Symbols.TermSymbol | ||
import core.Phases.Phase | ||
import TreeTransforms.{MiniPhaseTransform, TransformerInfo} | ||
|
||
/** Rewrite `getClass` calls as follow: | ||
* | ||
* For every instance of primitive class C whose boxed class is called B: | ||
* instanceC.getClass -> B.TYPE | ||
* For every instance of non-primitive class D: | ||
* instanceD.getClass -> instanceD.getClass | ||
*/ | ||
class GetClass extends MiniPhaseTransform { | ||
import tpd._ | ||
|
||
override def phaseName: String = "getClass" | ||
|
||
override def runsAfter: Set[Class[_ <: Phase]] = Set(classOf[Erasure]) | ||
|
||
override def transformApply(tree: Apply)(implicit ctx: Context, info: TransformerInfo): Tree = { | ||
import ast.Trees._ | ||
|
||
tree match { | ||
case Apply(Select(qual, nme.getClass_), Nil) => | ||
val defn = ctx.definitions | ||
val claz = qual.tpe.classSymbol | ||
|
||
def TYPE(module: TermSymbol) = ref(module).select(nme.TYPE_).ensureConforms(tree.tpe) | ||
claz match { | ||
case defn.BooleanClass => TYPE(defn.BoxedBooleanModule) | ||
case defn.ByteClass => TYPE(defn.BoxedByteModule) | ||
case defn.ShortClass => TYPE(defn.BoxedShortModule) | ||
case defn.CharClass => TYPE(defn.BoxedCharModule) | ||
case defn.IntClass => TYPE(defn.BoxedIntModule) | ||
case defn.LongClass => TYPE(defn.BoxedLongModule) | ||
case defn.FloatClass => TYPE(defn.BoxedFloatModule) | ||
case defn.DoubleClass => TYPE(defn.BoxedDoubleModule) | ||
case defn.UnitClass => TYPE(defn.BoxedVoidModule) | ||
case _ => tree | ||
} | ||
case _ => tree | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Value types: | ||
void | ||
boolean | ||
byte | ||
short | ||
char | ||
int | ||
long | ||
float | ||
double | ||
|
||
Class types: | ||
class SomeClass | ||
class ValueClass | ||
class scala.collection.immutable.$colon$colon | ||
class scala.Tuple2 | ||
|
||
Arrays: | ||
class [Lscala.runtime.BoxedUnit; | ||
class [I | ||
class [D | ||
class [Lscala.collection.immutable.List; | ||
|
||
Functions: | ||
class Test$$$Lambda$1 | ||
class Test$$$Lambda$2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class ValueClass(val i: Integer) extends AnyVal | ||
class SomeClass | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
val cls: Predef.Class[_] = new SomeClass().getClass | ||
val valCls: Predef.Class[_] = new ValueClass(1).getClass | ||
val iCls: Class[Int] = 1.getClass | ||
val f1: Function2[Int, Int, Unit] = (a: Int, b: Int) => println(a + b) | ||
val f2: Function1[Int, Boolean] = (a: Int) => a % 2 == 0 | ||
|
||
println("Value types:") | ||
println(().getClass) | ||
println(true.getClass) | ||
println(1.asInstanceOf[Byte].getClass) | ||
println(1.asInstanceOf[Short].getClass) | ||
println('a'.getClass) | ||
println(1.getClass) | ||
println(1L.getClass) | ||
println(1f.getClass) | ||
println(1d.getClass) | ||
|
||
println("\nClass types:") | ||
println(new SomeClass().getClass) | ||
println(new ValueClass(1).getClass) | ||
println(List(Array(1f)).getClass) | ||
println(("a", Map(1 -> "b")).getClass) | ||
|
||
println("\nArrays:") | ||
println(Array(()).getClass) | ||
println(Array(1).getClass) | ||
println(Array(1d).getClass) | ||
println(Array(List("1")).getClass) | ||
|
||
println("\nFunctions:") | ||
// FunctionN.getClass.toString has form of "class Test$$$Lambda$N/1349414238", | ||
// but number (1349414238) depends on environment | ||
println(f1.getClass.toString.takeWhile(_ != '/')) | ||
println(f2.getClass.toString.takeWhile(_ != '/')) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some types don't have symbols before erasure. I'd add
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought that order in
Compiler
is enough for phases ordering. Why do we explicitly needrunsAfter
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is enough.
We have a checks, before building compiler pipeline that ensures that those
runsAfter
are followed. This is very handy if someone wants to play around with phase ordering, as it makes dependencies explicit.