ExpressionCodegenExtension extracted

This commit is contained in:
Andrey Breslav
2014-09-30 09:43:56 +04:00
committed by Yan Zhulanow
parent e10d8a6fe0
commit 5811b13ce7
8 changed files with 76 additions and 14 deletions
-1
View File
@@ -15,6 +15,5 @@
<orderEntry type="module" module-name="serialization.jvm" />
<orderEntry type="module" module-name="backend-common" exported="" />
<orderEntry type="module" module-name="util" />
<orderEntry type="module" module-name="android-compiler-plugin" />
</component>
</module>
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.codegen.extensions
import org.jetbrains.jet.extensions.ProjectExtensionDescriptor
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall
import org.jetbrains.jet.codegen.StackValue
import org.jetbrains.jet.codegen.state.JetTypeMapper
import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
public trait ExpressionCodegenExtension {
class object : ProjectExtensionDescriptor<ExpressionCodegenExtension>("org.jetbrains.kotlin.expressionCodegenExtension", javaClass<ExpressionCodegenExtension>())
public class Context(
public val typeMapper: JetTypeMapper,
public val v: InstructionAdapter
)
// return null if not applicable
public fun apply(receiver: StackValue, resolvedCall: ResolvedCall<*>, c: ExpressionCodegenExtension.Context): StackValue?
}
@@ -30,6 +30,7 @@ import kotlin.KotlinPackage;
import kotlin.Unit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.codegen.extensions.ExpressionCodegenExtension;
import org.jetbrains.kotlin.backend.common.CodegenUtil;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.codegen.binding.CalculatedClosure;
@@ -1888,20 +1889,10 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
if (descriptor instanceof PropertyDescriptor) {
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor;
JetFile file = DescriptorToSourceUtils.getContainingFile(propertyDescriptor);
if (file != null) {
String androidPackage = file.getUserData(ANDROID_USER_PACKAGE);
if (androidPackage != null) {
for (ExpressionCodegenExtension extension : ExpressionCodegenExtension.OBJECT$.getInstances(state.getProject())) {
StackValue result = extension.apply(receiver, resolvedCall, new ExpressionCodegenExtension.Context(typeMapper, v));
Type retType = typeMapper.mapType(propertyDescriptor.getReturnType());
v.load(0, Type.getType("Landroid/app/Activity;"));
v.getstatic(androidPackage.replace(".", "/") + "/R$id",
propertyDescriptor.getName().asString(), "I");
v.invokevirtual("android/app/Activity", "findViewById", "(I)" + "Landroid/view/View;", false);
v.checkcast(retType);
return StackValue.onStack(retType);
}
if (result != null) return result;
}
boolean directToField =
@@ -56,6 +56,7 @@ import kotlin.Function1;
import kotlin.Unit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;
import org.jetbrains.jet.codegen.extensions.ExpressionCodegenExtension;
import org.jetbrains.jet.extensions.ExternalDeclarationsProvider;
import org.jetbrains.kotlin.asJava.JavaElementFinder;
import org.jetbrains.kotlin.asJava.KotlinLightClassForPackage;
@@ -302,6 +303,7 @@ public class JetCoreEnvironment {
}
ExternalDeclarationsProvider.OBJECT$.registerExtensionPoint(project);
ExpressionCodegenExtension.OBJECT$.registerExtensionPoint(project);
}
private static void registerProjectExtensionPoints(ExtensionsArea area) {
+3
View File
@@ -11,5 +11,8 @@
<extensionPoint name="externalDeclarationsProvider"
interface="org.jetbrains.jet.extensions.ExternalDeclarationsProvider"
area="IDEA_PROJECT"/>
<extensionPoint name="expressionCodegenExtension"
interface="org.jetbrains.jet.codegen.extensions.ExpressionCodegenExtension"
area="IDEA_PROJECT"/>
</extensionPoints>
</idea-plugin>
@@ -12,6 +12,7 @@
<orderEntry type="library" name="kotlin-runtime" level="project" />
<orderEntry type="module" module-name="cli" />
<orderEntry type="module" module-name="util" />
<orderEntry type="module" module-name="backend" scope="PROVIDED" />
</component>
</module>
@@ -29,6 +29,13 @@ import org.jetbrains.jet.lang.resolve.android.*
import com.intellij.openapi.components.ServiceManager
import org.jetbrains.jet.utils.emptyOrSingletonList
import com.intellij.openapi.project.Project
import org.jetbrains.jet.codegen.extensions.ExpressionCodegenExtension
import org.jetbrains.jet.lang.descriptors.CallableDescriptor
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall
import org.jetbrains.jet.codegen.StackValue
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor
import org.jetbrains.org.objectweb.asm.Type
import org.jetbrains.jet.lang.resolve.DescriptorToSourceUtils
public object AndroidConfigurationKeys {
@@ -65,6 +72,28 @@ public class AndroidDeclarationsProvider(private val project: Project) : Externa
}
}
public class AndroidExpressionCodegen : ExpressionCodegenExtension {
override fun apply(receiver: StackValue, resolvedCall: ResolvedCall<*>, c: ExpressionCodegenExtension.Context): StackValue? {
if (resolvedCall.getResultingDescriptor() !is PropertyDescriptor) return null
val propertyDescriptor = resolvedCall.getResultingDescriptor() as PropertyDescriptor
val file = DescriptorToSourceUtils.getContainingFile(propertyDescriptor)
if (file == null) return null
val androidPackage = file.getUserData<String>(AndroidConst.ANDROID_USER_PACKAGE)
if (androidPackage == null) return null
val retType = c.typeMapper.mapType(propertyDescriptor.getReturnType()!!)
receiver.put(Type.getType("Landroid/app/Activity;"), c.v)
c.v.getstatic(androidPackage.replace(".", "/") + "/R\$id", propertyDescriptor.getName().asString(), "I")
c.v.invokevirtual("android/app/Activity", "findViewById", "(I)" + "Landroid/view/View;", false)
c.v.checkcast(retType)
return StackValue.onStack(retType)
}
}
public class AndroidComponentRegistrar : ComponentRegistrar {
public override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
@@ -73,5 +102,6 @@ public class AndroidComponentRegistrar : ComponentRegistrar {
project.registerService(javaClass<AndroidUIXmlProcessor>(), CliAndroidUIXmlProcessor(project, androidResPath, androidManifest))
ExternalDeclarationsProvider.registerExtension(project, AndroidDeclarationsProvider(project))
ExpressionCodegenExtension.registerExtension(project, AndroidExpressionCodegen())
}
}
@@ -18,5 +18,6 @@
<extensions defaultExtensionNs="org.jetbrains.kotlin">
<externalDeclarationsProvider implementation="org.jetbrains.kotlin.android.AndroidDeclarationsProvider"/>
<expressionCodegenExtension implementation="org.jetbrains.kotlin.android.AndroidExpressionCodegen"/>
</extensions>
</idea-plugin>