Skip to content

Commit 8d970a3

Browse files
committed
Don't extract private members of instantiated or external classes
This is both consistent with the Java extractor's behaviour, and prevents us from trying to refer to anonymous types (e.g. anonymous objects that directly initialize properties) out of scope.
1 parent fbae0f5 commit 8d970a3

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

java/kotlin-extractor/src/main/kotlin/ExternalDeclExtractor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ class ExternalDeclExtractor(val logger: FileLogger, val invocationTrapFile: Stri
9393
ftw.writeHasLocation(ftw.fileId, ftw.getWholeFileLocation())
9494
ftw.writeCupackage(ftw.fileId, pkgId)
9595

96-
fileExtractor.extractClassSource(irDecl, !irDecl.isFileClass, false)
96+
fileExtractor.extractClassSource(irDecl, extractDeclarations = !irDecl.isFileClass, extractStaticInitializer = false, extractPrivateMembers = false)
9797
} else {
98-
fileExtractor.extractDeclaration(irDecl)
98+
fileExtractor.extractDeclaration(irDecl, extractPrivateMembers = false)
9999
}
100100
}
101101

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ open class KotlinFileExtractor(
7474
}
7575
}
7676

77-
file.declarations.map { extractDeclaration(it) }
77+
file.declarations.map { extractDeclaration(it, extractPrivateMembers = true) }
7878
extractStaticInitializer(file, null)
7979
CommentExtractor(this, file, tw.fileId).extract()
8080
}
@@ -91,14 +91,23 @@ open class KotlinFileExtractor(
9191
return false
9292
}
9393

94-
fun extractDeclaration(declaration: IrDeclaration) {
94+
private fun shouldExtractDecl(declaration: IrDeclaration, extractPrivateMembers: Boolean) =
95+
extractPrivateMembers ||
96+
when(declaration) {
97+
is IrDeclarationWithVisibility -> declaration.visibility.let { it != DescriptorVisibilities.PRIVATE && it != DescriptorVisibilities.PRIVATE_TO_THIS }
98+
else -> true
99+
}
100+
101+
fun extractDeclaration(declaration: IrDeclaration, extractPrivateMembers: Boolean) {
95102
with("declaration", declaration) {
103+
if (!shouldExtractDecl(declaration, extractPrivateMembers))
104+
return
96105
when (declaration) {
97106
is IrClass -> {
98107
if (isExternalDeclaration(declaration)) {
99108
extractExternalClassLater(declaration)
100109
} else {
101-
extractClassSource(declaration, extractDeclarations = true, extractStaticInitializer = true)
110+
extractClassSource(declaration, extractDeclarations = true, extractStaticInitializer = true, extractPrivateMembers = extractPrivateMembers)
102111
}
103112
}
104113
is IrFunction -> {
@@ -320,7 +329,7 @@ open class KotlinFileExtractor(
320329

321330
// `argsIncludingOuterClasses` can be null to describe a raw generic type.
322331
// For non-generic types it will be zero-length list.
323-
fun extractMemberPrototypes(c: IrClass, argsIncludingOuterClasses: List<IrTypeArgument>?, id: Label<out DbClassorinterface>) {
332+
fun extractNonPrivateMemberPrototypes(c: IrClass, argsIncludingOuterClasses: List<IrTypeArgument>?, id: Label<out DbClassorinterface>) {
324333
with("member prototypes", c) {
325334
val typeParamSubstitution =
326335
when (argsIncludingOuterClasses) {
@@ -339,17 +348,19 @@ open class KotlinFileExtractor(
339348
}
340349

341350
c.declarations.map {
342-
when(it) {
343-
is IrFunction -> extractFunction(it, id, false, typeParamSubstitution, argsIncludingOuterClasses)
344-
is IrProperty -> extractProperty(it, id, false, typeParamSubstitution, argsIncludingOuterClasses)
345-
else -> {}
351+
if (shouldExtractDecl(it, false)) {
352+
when(it) {
353+
is IrFunction -> extractFunction(it, id, false, typeParamSubstitution, argsIncludingOuterClasses)
354+
is IrProperty -> extractProperty(it, id, false, typeParamSubstitution, argsIncludingOuterClasses)
355+
else -> {}
356+
}
346357
}
347358
}
348359
}
349360
}
350361

351362
private fun extractLocalTypeDeclStmt(c: IrClass, callable: Label<out DbCallable>, parent: Label<out DbStmtparent>, idx: Int) {
352-
val id = extractClassSource(c, extractDeclarations = true, extractStaticInitializer = true).cast<DbClass>()
363+
val id = extractClassSource(c, extractDeclarations = true, extractStaticInitializer = true, extractPrivateMembers = true).cast<DbClass>()
353364
extractLocalTypeDeclStmt(id, c, callable, parent, idx)
354365
}
355366

@@ -361,7 +372,7 @@ open class KotlinFileExtractor(
361372
tw.writeHasLocation(stmtId, locId)
362373
}
363374

364-
fun extractClassSource(c: IrClass, extractDeclarations: Boolean, extractStaticInitializer: Boolean): Label<out DbClassorinterface> {
375+
fun extractClassSource(c: IrClass, extractDeclarations: Boolean, extractStaticInitializer: Boolean, extractPrivateMembers: Boolean): Label<out DbClassorinterface> {
365376
with("class source", c) {
366377
DeclarationStackAdjuster(c).use {
367378

@@ -399,7 +410,7 @@ open class KotlinFileExtractor(
399410

400411
c.typeParameters.mapIndexed { idx, param -> extractTypeParameter(param, idx) }
401412
if (extractDeclarations) {
402-
c.declarations.map { extractDeclaration(it) }
413+
c.declarations.map { extractDeclaration(it, extractPrivateMembers) }
403414
if (extractStaticInitializer)
404415
extractStaticInitializer(c, id)
405416
}

java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ open class KotlinUsesExtractor(
410410
if (inReceiverContext && globalExtensionState.genericSpecialisationsExtracted.add(classLabelResult.classLabel)) {
411411
val supertypeMode = if (argsIncludingOuterClasses == null) ExtractSupertypesMode.Raw else ExtractSupertypesMode.Specialised(argsIncludingOuterClasses)
412412
extractorWithCSource.extractClassSupertypes(c, classLabel, supertypeMode, true)
413-
extractorWithCSource.extractMemberPrototypes(c, argsIncludingOuterClasses, classLabel)
413+
extractorWithCSource.extractNonPrivateMemberPrototypes(c, argsIncludingOuterClasses, classLabel)
414414
}
415415
}
416416

0 commit comments

Comments
 (0)