Support builtin properties in ConstantExpressionEvaluator
This commit is contained in:
committed by
Mikhail Glukhikh
parent
e062e32f95
commit
0b9c9a6047
+5
@@ -565,6 +565,11 @@ private class ConstantExpressionEvaluatorVisitor(
|
||||
return evaluateCall(calleeExpression, receiverExpression, expectedType)
|
||||
}
|
||||
|
||||
if (selectorExpression is JetSimpleNameExpression) {
|
||||
val result = evaluateCall(selectorExpression, expression.receiverExpression, expectedType)
|
||||
if (result != null) return result
|
||||
}
|
||||
|
||||
// MyEnum.A, Integer.MAX_VALUE
|
||||
if (selectorExpression != null) {
|
||||
return evaluate(selectorExpression, expectedType)
|
||||
|
||||
+1
@@ -98,6 +98,7 @@ internal val unaryOperations: HashMap<UnaryOperationKey<*>, Pair<Function1<Any?,
|
||||
unaryOperation(SHORT, "toLong", { a -> a.toLong() }, emptyUnaryFun),
|
||||
unaryOperation(SHORT, "toShort", { a -> a.toShort() }, emptyUnaryFun),
|
||||
unaryOperation(SHORT, "toString", { a -> a.toString() }, emptyUnaryFun),
|
||||
unaryOperation(STRING, "length", { a -> a.length() }, emptyUnaryFun),
|
||||
unaryOperation(STRING, "toString", { a -> a.toString() }, emptyUnaryFun)
|
||||
)
|
||||
|
||||
|
||||
+1
-1
@@ -35,4 +35,4 @@ val prop9 = "a" + "b"
|
||||
val prop10 = prop9 + "b"
|
||||
|
||||
// val prop11: 6
|
||||
val prop11 = "kotlin".length()
|
||||
val prop11 = "kotlin".length
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.jetbrains.kotlin.generators.evaluate
|
||||
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.generators.util.GeneratorsFileUtil
|
||||
@@ -46,7 +47,7 @@ fun generate(): String {
|
||||
p.println("/** This file is generated by org.jetbrains.kotlin.generators.evaluate:generate(). DO NOT MODIFY MANUALLY */")
|
||||
p.println()
|
||||
|
||||
val unaryOperationsMap = arrayListOf<Pair<String, List<JetType>>>()
|
||||
val unaryOperationsMap = arrayListOf<Triple<String, List<JetType>, Boolean>>()
|
||||
val binaryOperationsMap = arrayListOf<Pair<String, List<JetType>>>()
|
||||
|
||||
val builtIns = TargetPlatform.Default.builtIns
|
||||
@@ -57,13 +58,13 @@ fun generate(): String {
|
||||
for (descriptor in allPrimitiveTypes + builtIns.getString()) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val functions = descriptor.getMemberScope(listOf()).getDescriptors()
|
||||
.filter { it is FunctionDescriptor && !EXCLUDED_FUNCTIONS.contains(it.getName().asString()) } as List<FunctionDescriptor>
|
||||
.filter { it is CallableDescriptor && !EXCLUDED_FUNCTIONS.contains(it.getName().asString()) } as List<CallableDescriptor>
|
||||
|
||||
for (function in functions) {
|
||||
val parametersTypes = function.getParametersTypes()
|
||||
|
||||
when (parametersTypes.size()) {
|
||||
1 -> unaryOperationsMap.add(function.getName().asString() to parametersTypes)
|
||||
1 -> unaryOperationsMap.add(Triple(function.getName().asString(), parametersTypes, function is FunctionDescriptor))
|
||||
2 -> binaryOperationsMap.add(function.getName().asString() to parametersTypes)
|
||||
else -> throw IllegalStateException("Couldn't add following method from builtins to operations map: ${function.getName()} in class ${descriptor.getName()}")
|
||||
}
|
||||
@@ -79,13 +80,14 @@ fun generate(): String {
|
||||
|
||||
val unaryOperationsMapIterator = unaryOperationsMap.iterator()
|
||||
while (unaryOperationsMapIterator.hasNext()) {
|
||||
val (funcName, parameters) = unaryOperationsMapIterator.next()
|
||||
val (funcName, parameters, isFunction) = unaryOperationsMapIterator.next()
|
||||
val parenthesesOrBlank = "()" //if (isFunction) "()" else ""
|
||||
p.println(
|
||||
"unaryOperation(",
|
||||
parameters.map { it.asString() }.joinToString(", "),
|
||||
", ",
|
||||
"\"$funcName\"",
|
||||
", { a -> a.${funcName}() }, ",
|
||||
", { a -> a.$funcName$parenthesesOrBlank }, ",
|
||||
renderCheckUnaryOperation(funcName, parameters),
|
||||
")",
|
||||
if (unaryOperationsMapIterator.hasNext()) "," else ""
|
||||
@@ -159,7 +161,7 @@ private fun JetType.isIntegerType(): Boolean {
|
||||
}
|
||||
|
||||
|
||||
private fun FunctionDescriptor.getParametersTypes(): List<JetType> {
|
||||
private fun CallableDescriptor.getParametersTypes(): List<JetType> {
|
||||
val list = arrayListOf((getContainingDeclaration() as ClassDescriptor).getDefaultType())
|
||||
getValueParameters().map { it.getType() }.forEach {
|
||||
list.add(TypeUtils.makeNotNullable(it))
|
||||
|
||||
Reference in New Issue
Block a user