new scope and deserialization

This commit is contained in:
Michael Bogdanov
2015-08-17 14:30:42 +03:00
parent 7f2973dc7d
commit ebb1629285
6 changed files with 120 additions and 10 deletions
@@ -167,6 +167,10 @@ public class FunctionCodegen {
v.getSerializationBindings().put(IMPL_CLASS_NAME_FOR_CALLABLE, functionDescriptor, shortNameByAsmType(ownerType));
}
else {
if (owner instanceof PackageContext) {
Type ownerType = ((PackageContext) owner).getPackagePartType();
v.getSerializationBindings().put(IMPL_CLASS_NAME_FOR_CALLABLE, functionDescriptor, shortNameByAsmType(ownerType));
}
v.getSerializationBindings().put(METHOD_FOR_FUNCTION, functionDescriptor, asmMethod);
}
@@ -115,6 +115,10 @@ public class PropertyCodegen {
v.getSerializationBindings().put(IMPL_CLASS_NAME_FOR_CALLABLE, descriptor, shortNameByAsmType(ownerType));
}
else {
if (context instanceof PackageContext) {
Type ownerType = ((PackageContext) context).getPackagePartType();
v.getSerializationBindings().put(IMPL_CLASS_NAME_FOR_CALLABLE, descriptor, shortNameByAsmType(ownerType));
}
assert declaration != null : "Declaration is null for different context: " + context;
boolean hasBackingField = hasBackingField(declaration, descriptor);
@@ -48,17 +48,43 @@ public class LazyJavaPackageScope(
public val kotlinBinaryClass: KotlinJvmBinaryClass?
= c.components.kotlinClassFinder.findKotlinClass(PackageClassUtils.getPackageClassId(packageFragment.fqName))
public val kotlinBinaryClasses: List<KotlinJvmBinaryClass>
init {
val pakage = jPackage.getFqName().asString().replace('.', '/')
val files = containingDeclaration.packageMapper.findPackageMembers(pakage)
val packageClassId = PackageClassUtils.getPackageClassId(packageFragment.fqName).packageFqName
val notFound = arrayListOf<String>()
val classFiles = files.map {
val classId = ClassId(packageClassId, Name.identifierNoValidate(it.substringAfterLast("/")))
val findKotlinClass = c.components.kotlinClassFinder.findKotlinClass(classId)
if (findKotlinClass == null) {
notFound.add("$classId")
}
findKotlinClass
}
kotlinBinaryClasses = classFiles.filterNotNull()
if (kotlinBinaryClasses.size() != classFiles.size()) {
println("package: $pakage")
println("files: " + files.join())
println("not found: " + notFound.join())
//assert(kotlinBinaryClasses.size() == classFiles.size(), "${kotlinBinaryClasses.size()} != ${classFiles.size()}")
}
}
private val deserializedPackageScope = c.storageManager.createLazyValue {
val kotlinBinaryClass = kotlinBinaryClass
if (kotlinBinaryClass == null)
if (kotlinBinaryClasses.isEmpty())
JetScope.Empty
else {
val pakage = jPackage.getFqName().asString()
val files = containingDeclaration.packageMapper.findPackageMembers(pakage.replace("\\.", "/"))
// println("package:" + pakage)
// println(files.join())
val jetScope = c.components.deserializedDescriptorResolver.createKotlinPackageScope(packageFragment, kotlinBinaryClass) ?: JetScope.Empty
jetScope
//if(!kotlinBinaryClasses.isEmpty()) {
c.components.deserializedDescriptorResolver.createKotlinNewPackageScope(packageFragment, kotlinBinaryClasses)
//print("deserializing " + packageFragment.getName())
//}
// val jetScope = c.deserializedDescriptorResolver.createKotlinPackageScope(packageFragment, kotlinBinaryClass) ?: JetScope.Empty
// jetScope
}
}
@@ -29,15 +29,17 @@ import org.jetbrains.kotlin.serialization.PackageData;
import org.jetbrains.kotlin.serialization.deserialization.ClassDataProvider;
import org.jetbrains.kotlin.serialization.deserialization.DeserializationComponents;
import org.jetbrains.kotlin.serialization.deserialization.ErrorReporter;
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedNewPackageMemberScope;
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPackageMemberScope;
import org.jetbrains.kotlin.serialization.jvm.JvmProtoBufUtil;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader.Kind.CLASS;
import static org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader.Kind.PACKAGE_FACADE;
public final class DeserializedDescriptorResolver {
private final ErrorReporter errorReporter;
@@ -67,7 +69,8 @@ public final class DeserializedDescriptorResolver {
@Nullable
public JetScope createKotlinPackageScope(@NotNull PackageFragmentDescriptor descriptor, @NotNull KotlinJvmBinaryClass kotlinClass) {
String[] data = readData(kotlinClass, PACKAGE_FACADE);
//TODO add assertion from readData(kotlinClass, CLASS);
String[] data = kotlinClass.getClassHeader().getAnnotationData();
if (data != null) {
//all classes are included in java scope
PackageData packageData = JvmProtoBufUtil.readPackageDataFrom(data);
@@ -84,6 +87,17 @@ public final class DeserializedDescriptorResolver {
return null;
}
@NotNull
public JetScope createKotlinNewPackageScope(@NotNull PackageFragmentDescriptor descriptor, @NotNull List<KotlinJvmBinaryClass> packageParts) {
List<JetScope> list = new ArrayList<JetScope>();
for (KotlinJvmBinaryClass callable : packageParts) {
JetScope scope = createKotlinPackageScope(descriptor, callable);
assert scope != null : "Can't create scope for " + callable;
list.add(scope);
}
return new DeserializedNewPackageMemberScope(descriptor, list);
}
@Nullable
public String[] readData(@NotNull KotlinJvmBinaryClass kotlinClass, @NotNull KotlinClassHeader.Kind expectedKind) {
KotlinClassHeader header = kotlinClass.getClassHeader();
@@ -0,0 +1,62 @@
/*
* Copyright 2010-2015 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.kotlin.serialization.deserialization.descriptors
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.JetScope
import org.jetbrains.kotlin.resolve.scopes.JetScopeImpl
import org.jetbrains.kotlin.utils.Printer
public open class DeserializedNewPackageMemberScope(
val packageDescriptor: PackageFragmentDescriptor,
val membersList: List<JetScope>
) : JetScopeImpl() {
override fun getFunctions(name: Name, location: LookupLocation): Collection<FunctionDescriptor> = membersList.flatMap { it.getFunctions(name, location) }
override fun getProperties(name: Name, location: LookupLocation): Collection<VariableDescriptor> = membersList.flatMap { it.getProperties(name, location) }
override fun getClassifier(name: Name, location: LookupLocation) = membersList.asSequence().map { it.getClassifier(name, location) }.filterNotNull().firstOrNull()
override fun getContainingDeclaration() = packageDescriptor
override fun getImplicitReceiversHierarchy(): List<ReceiverParameterDescriptor> {
return listOf()
}
override fun getOwnDeclaredDescriptors() = getAllDescriptors()
override fun printScopeStructure(p: Printer) {
p.println(javaClass.getSimpleName(), " {")
p.pushIndent()
p.println("containingDeclaration = " + getContainingDeclaration())
p.popIndent()
p.println("}")
}
override fun getDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean)
= membersList.flatMap { it.getDescriptors(kindFilter, nameFilter) }
}
@@ -69,7 +69,7 @@ public fun isKotlinInternalCompiledFile(file: VirtualFile): Boolean {
return true
}
val header = KotlinBinaryClassCache.getKotlinBinaryClass(file)?.getClassHeader() ?: return false
return header.kind == KotlinClassHeader.Kind.SYNTHETIC_CLASS ||
return (header.kind == KotlinClassHeader.Kind.SYNTHETIC_CLASS && header.syntheticClassKind != KotlinSyntheticClass.Kind.PACKAGE_PART) ||
(header.kind == KotlinClassHeader.Kind.CLASS && header.classKind != null && header.classKind != KotlinClass.Kind.CLASS)
}