Pass BindingContext to CodegenUtil.getActualDeclarations

Will be used in a subsequent commit. Also rename getActualDeclarations
-> getDeclarationsToGenerate
This commit is contained in:
Alexander Udalov
2018-06-25 14:55:43 +02:00
parent ba6da7c40a
commit 4e217b180a
7 changed files with 27 additions and 18 deletions
@@ -167,9 +167,13 @@ object CodegenUtil {
return KtPsiFactory(project, markGenerated = false).createExpression(fakeFunctionCall) as KtCallExpression return KtPsiFactory(project, markGenerated = false).createExpression(fakeFunctionCall) as KtCallExpression
} }
/**
* Returns declarations in the given [file] which should be generated by the back-end. This includes all declarations
* minus all expected declarations.
*/
@JvmStatic @JvmStatic
fun getActualDeclarations(file: KtFile): List<KtDeclaration> = fun getDeclarationsToGenerate(file: KtFile, bindingContext: BindingContext): List<KtDeclaration> =
file.declarations.filterNot(KtDeclaration::hasExpectModifier) file.declarations.filterNot(KtDeclaration::hasExpectModifier)
@JvmStatic @JvmStatic
fun findExpectedFunctionForActual(descriptor: FunctionDescriptor): FunctionDescriptor? { fun findExpectedFunctionForActual(descriptor: FunctionDescriptor): FunctionDescriptor? {
@@ -85,7 +85,7 @@ class MultifileClassCodegenImpl(
private val partInternalNamesSorted = run { private val partInternalNamesSorted = run {
val partInternalNamesSet = hashSetOf<String>() val partInternalNamesSet = hashSetOf<String>()
for (file in files) { for (file in files) {
if (file.hasDeclarationsForPartClass()) { if (file.hasDeclarationsForPartClass(state.bindingContext)) {
partInternalNamesSet.add(JvmFileClassUtil.getFileClassInternalName(file)) partInternalNamesSet.add(JvmFileClassUtil.getFileClassInternalName(file))
} }
} }
@@ -122,7 +122,7 @@ class MultifileClassCodegenImpl(
val singleSourceFile = val singleSourceFile =
if (previouslyCompiledCallables.isEmpty()) if (previouslyCompiledCallables.isEmpty())
files.singleOrNull { it.hasDeclarationsForPartClass() } files.singleOrNull { it.hasDeclarationsForPartClass(state.bindingContext) }
else else
null null
@@ -209,7 +209,9 @@ class MultifileClassCodegenImpl(
generateNonPartClassDeclarations(file, partContext) generateNonPartClassDeclarations(file, partContext)
if (!state.generateDeclaredClassFilter.shouldGeneratePackagePart(file) || !file.hasDeclarationsForPartClass()) return if (!state.generateDeclaredClassFilter.shouldGeneratePackagePart(file) ||
!file.hasDeclarationsForPartClass(state.bindingContext)
) return
packagePartRegistry.addPart(partType.internalName, facadeClassType.internalName) packagePartRegistry.addPart(partType.internalName, facadeClassType.internalName)
@@ -243,7 +245,7 @@ class MultifileClassCodegenImpl(
private fun addDelegateGenerationTasksForDeclarationsInFile(file: KtFile, packageFragment: PackageFragmentDescriptor, partType: Type) { private fun addDelegateGenerationTasksForDeclarationsInFile(file: KtFile, packageFragment: PackageFragmentDescriptor, partType: Type) {
val facadeContext = state.rootContext.intoMultifileClass(packageFragment, facadeClassType, partType) val facadeContext = state.rootContext.intoMultifileClass(packageFragment, facadeClassType, partType)
val memberCodegen = createCodegenForDelegatesInMultifileFacade(facadeContext) val memberCodegen = createCodegenForDelegatesInMultifileFacade(facadeContext)
for (declaration in CodegenUtil.getActualDeclarations(file)) { for (declaration in CodegenUtil.getDeclarationsToGenerate(file, state.bindingContext)) {
if (declaration is KtNamedFunction || declaration is KtProperty || declaration is KtTypeAlias) { if (declaration is KtNamedFunction || declaration is KtProperty || declaration is KtTypeAlias) {
val descriptor = state.bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, declaration) val descriptor = state.bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, declaration)
if (descriptor !is MemberDescriptor) { if (descriptor !is MemberDescriptor) {
@@ -375,8 +377,8 @@ class MultifileClassCodegenImpl(
return fragments.firstOrNull() return fragments.firstOrNull()
} }
private fun KtFile.hasDeclarationsForPartClass() = private fun KtFile.hasDeclarationsForPartClass(bindingContext: BindingContext) =
CodegenUtil.getActualDeclarations(this).any { it is KtProperty || it is KtFunction || it is KtTypeAlias } CodegenUtil.getDeclarationsToGenerate(this, bindingContext).any { it is KtProperty || it is KtFunction || it is KtTypeAlias }
private fun getCompiledPackageFragment( private fun getCompiledPackageFragment(
facadeFqName: FqName, state: GenerationState facadeFqName: FqName, state: GenerationState
@@ -132,7 +132,7 @@ class MultifileClassPartCodegen(
} }
override fun generateBody() { override fun generateBody() {
for (declaration in CodegenUtil.getActualDeclarations(element)) { for (declaration in CodegenUtil.getDeclarationsToGenerate(element, state.bindingContext)) {
if (declaration is KtNamedFunction || declaration is KtProperty || declaration is KtTypeAlias) { if (declaration is KtNamedFunction || declaration is KtProperty || declaration is KtTypeAlias) {
genSimpleMember(declaration) genSimpleMember(declaration)
} }
@@ -98,7 +98,7 @@ public class PackageCodegenImpl implements PackageCodegen {
List<KtClassOrObject> classOrObjects = new ArrayList<>(); List<KtClassOrObject> classOrObjects = new ArrayList<>();
for (KtDeclaration declaration : CodegenUtil.getActualDeclarations(file)) { for (KtDeclaration declaration : CodegenUtil.getDeclarationsToGenerate(file, state.getBindingContext())) {
if (isFilePartDeclaration(declaration)) { if (isFilePartDeclaration(declaration)) {
generatePackagePart = true; generatePackagePart = true;
} }
@@ -90,7 +90,7 @@ public class PackagePartCodegen extends MemberCodegen<KtFile> {
@Override @Override
protected void generateBody() { protected void generateBody() {
for (KtDeclaration declaration : CodegenUtil.getActualDeclarations(element)) { for (KtDeclaration declaration : CodegenUtil.getDeclarationsToGenerate(element, state.getBindingContext())) {
if (declaration instanceof KtNamedFunction || declaration instanceof KtProperty || declaration instanceof KtTypeAlias) { if (declaration instanceof KtNamedFunction || declaration instanceof KtProperty || declaration instanceof KtTypeAlias) {
genSimpleMember(declaration); genSimpleMember(declaration);
} }
@@ -123,7 +123,8 @@ public class PackagePartCodegen extends MemberCodegen<KtFile> {
@NotNull Type packagePartType @NotNull Type packagePartType
) { ) {
BindingContext bindingContext = codegen.bindingContext; BindingContext bindingContext = codegen.bindingContext;
List<DeclarationDescriptor> members = CollectionsKt.mapNotNull(CodegenUtil.getActualDeclarations(codegen.element), declaration -> { List<KtDeclaration> allDeclarations = CodegenUtil.getDeclarationsToGenerate(codegen.element, bindingContext);
List<DeclarationDescriptor> members = CollectionsKt.mapNotNull(allDeclarations, declaration -> {
if (declaration instanceof KtNamedFunction) { if (declaration instanceof KtNamedFunction) {
return bindingContext.get(BindingContext.FUNCTION, declaration); return bindingContext.get(BindingContext.FUNCTION, declaration);
} }
@@ -193,8 +193,10 @@ public class SamWrapperCodegen {
@NotNull KtFile containingFile, @NotNull KtFile containingFile,
CallableMemberDescriptor contextDescriptor CallableMemberDescriptor contextDescriptor
) { ) {
boolean hasPackagePartClass = boolean hasPackagePartClass = CollectionsKt.any(
CollectionsKt.any(CodegenUtil.getActualDeclarations(containingFile), PackageCodegenImpl::isFilePartDeclaration); CodegenUtil.getDeclarationsToGenerate(containingFile, state.getBindingContext()),
PackageCodegenImpl::isFilePartDeclaration
);
FqName filePartFqName = JvmFileClassUtil.getFileClassInfoNoResolve(containingFile).getFileClassFqName(); FqName filePartFqName = JvmFileClassUtil.getFileClassInfoNoResolve(containingFile).getFileClassFqName();
FqName outermostOwner; FqName outermostOwner;
@@ -12,6 +12,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.backend.common.CodegenUtil; import org.jetbrains.kotlin.backend.common.CodegenUtil;
import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil; import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil;
import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.test.InTextDirectivesUtils; import org.jetbrains.kotlin.test.InTextDirectivesUtils;
import org.jetbrains.kotlin.utils.ExceptionUtilsKt; import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
@@ -87,7 +88,7 @@ public abstract class AbstractBlackBoxCodegenTest extends CodegenTestCase {
// If there are many files, the first 'box(): String' function will be executed. // If there are many files, the first 'box(): String' function will be executed.
GeneratedClassLoader generatedClassLoader = generateAndCreateClassLoader(); GeneratedClassLoader generatedClassLoader = generateAndCreateClassLoader();
for (KtFile firstFile : myFiles.getPsiFiles()) { for (KtFile firstFile : myFiles.getPsiFiles()) {
String className = getFacadeFqName(firstFile); String className = getFacadeFqName(firstFile, classFileFactory.getGenerationState().getBindingContext());
if (className == null) continue; if (className == null) continue;
Class<?> aClass = getGeneratedClass(generatedClassLoader, className); Class<?> aClass = getGeneratedClass(generatedClassLoader, className);
try { try {
@@ -108,10 +109,9 @@ public abstract class AbstractBlackBoxCodegenTest extends CodegenTestCase {
fail("Can't find box method!"); fail("Can't find box method!");
} }
@Nullable @Nullable
private static String getFacadeFqName(@NotNull KtFile firstFile) { private static String getFacadeFqName(@NotNull KtFile firstFile, @NotNull BindingContext bindingContext) {
for (KtDeclaration declaration : CodegenUtil.getActualDeclarations(firstFile)) { for (KtDeclaration declaration : CodegenUtil.getDeclarationsToGenerate(firstFile, bindingContext)) {
if (declaration instanceof KtProperty || declaration instanceof KtNamedFunction || declaration instanceof KtTypeAlias) { if (declaration instanceof KtProperty || declaration instanceof KtNamedFunction || declaration instanceof KtTypeAlias) {
return JvmFileClassUtil.getFileClassInfoNoResolve(firstFile).getFacadeClassFqName().asString(); return JvmFileClassUtil.getFileClassInfoNoResolve(firstFile).getFacadeClassFqName().asString();
} }