Skip to content

Commit 5b2834c

Browse files
committed
Restrict type access extraction to classes extracted from source
Type accesses have anonymous IDs, so we mimic the Java extractor and extract them only when seeing a class in a .kt source file.
1 parent 0a788bc commit 5b2834c

File tree

6 files changed

+170
-74
lines changed

6 files changed

+170
-74
lines changed

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ open class KotlinFileExtractor(
208208
is IrField -> {
209209
val parentId = useDeclarationParent(getFieldParent(declaration), false)?.cast<DbReftype>()
210210
if (parentId != null) {
211-
extractField(declaration, parentId)
211+
extractField(declaration, parentId, extractFunctionBodies)
212212
}
213213
Unit
214214
}
@@ -480,21 +480,22 @@ open class KotlinFileExtractor(
480480
extractDeclInitializers(c.declarations, false) { Pair(blockId, obinitId) }
481481
}
482482

483-
private fun extractAnnotations(annotations: List<IrConstructorCall>, parent: Label<out DbExprparent>) {
483+
private fun extractAnnotations(annotations: List<IrConstructorCall>, parent: Label<out DbExprparent>, extractEnumTypeAccesses: Boolean) {
484484
val groupedAnnotations = metaAnnotationSupport.groupRepeatableAnnotations(annotations)
485485
for ((idx, constructorCall: IrConstructorCall) in groupedAnnotations.sortedBy { v -> v.type.classFqName?.asString() }.withIndex()) {
486-
extractAnnotation(constructorCall, parent, idx)
486+
extractAnnotation(constructorCall, parent, idx, extractEnumTypeAccesses)
487487
}
488488
}
489489

490-
private fun extractAnnotations(c: IrAnnotationContainer, parent: Label<out DbExprparent>) {
491-
extractAnnotations(c.annotations, parent)
490+
private fun extractAnnotations(c: IrAnnotationContainer, parent: Label<out DbExprparent>, extractEnumTypeAccesses: Boolean) {
491+
extractAnnotations(c.annotations, parent, extractEnumTypeAccesses)
492492
}
493493

494494
private fun extractAnnotation(
495495
constructorCall: IrConstructorCall,
496496
parent: Label<out DbExprparent>,
497497
idx: Int,
498+
extractEnumTypeAccesses: Boolean,
498499
contextLabel: String? = null
499500
): Label<out DbExpr> {
500501
// Erase the type here because the JVM lowering erases the annotation type, and so the Java extractor will see it in erased form.
@@ -518,7 +519,7 @@ open class KotlinFileExtractor(
518519
logger.warnElement("Expected annotation property to define a getter", prop)
519520
} else {
520521
val getterId = useFunction<DbMethod>(getter)
521-
val exprId = extractAnnotationValueExpression(v, id, i, "{${getterId}}", getter.returnType)
522+
val exprId = extractAnnotationValueExpression(v, id, i, "{${getterId}}", getter.returnType, extractEnumTypeAccesses)
522523
if (exprId != null) {
523524
tw.writeAnnotValue(id, getterId, exprId)
524525
}
@@ -532,7 +533,8 @@ open class KotlinFileExtractor(
532533
parent: Label<out DbExprparent>,
533534
idx: Int,
534535
contextLabel: String,
535-
contextType: IrType?): Label<out DbExpr>? {
536+
contextType: IrType?,
537+
extractEnumTypeAccesses: Boolean): Label<out DbExpr>? {
536538

537539
fun exprId() = tw.getLabelFor<DbExpr>("@\"annotationExpr;{$parent};$idx\"")
538540

@@ -541,15 +543,15 @@ open class KotlinFileExtractor(
541543
extractConstant(v, parent, idx, null, null, overrideId = exprId())
542544
}
543545
is IrGetEnumValue -> {
544-
extractEnumValue(v, parent, idx, null, null, overrideId = exprId())
546+
extractEnumValue(v, parent, idx, null, null, extractTypeAccess = extractEnumTypeAccesses, overrideId = exprId())
545547
}
546548
is IrClassReference -> {
547549
val classRefId = exprId()
548550
val typeAccessId = tw.getLabelFor<DbUnannotatedtypeaccess>("@\"annotationExpr;{$classRefId};0\"")
549551
extractClassReference(v, parent, idx, null, null, overrideId = classRefId, typeAccessOverrideId = typeAccessId, useJavaLangClassType = true)
550552
}
551553
is IrConstructorCall -> {
552-
extractAnnotation(v, parent, idx, contextLabel)
554+
extractAnnotation(v, parent, idx, extractEnumTypeAccesses, contextLabel)
553555
}
554556
is IrVararg -> {
555557
tw.getLabelFor<DbArrayinit>("@\"annotationarray;{${parent}};$contextLabel\"").also { arrayId ->
@@ -573,7 +575,7 @@ open class KotlinFileExtractor(
573575
null
574576
}
575577
}
576-
extractAnnotationValueExpression(argExpr, arrayId, index, "child;$index", null)
578+
extractAnnotationValueExpression(argExpr, arrayId, index, "child;$index", null, extractEnumTypeAccesses)
577579
} }
578580
}
579581
}
@@ -682,11 +684,11 @@ open class KotlinFileExtractor(
682684

683685
val additionalAnnotations =
684686
if (c.kind == ClassKind.ANNOTATION_CLASS && c.origin != IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB)
685-
metaAnnotationSupport.generateJavaMetaAnnotations(c)
687+
metaAnnotationSupport.generateJavaMetaAnnotations(c, extractFunctionBodies)
686688
else
687689
listOf()
688690

689-
extractAnnotations(c.annotations + additionalAnnotations, id)
691+
extractAnnotations(c.annotations + additionalAnnotations, id, extractFunctionBodies)
690692

691693
if (extractFunctionBodies && !c.isAnonymousObject && !c.isLocal)
692694
externalClassExtractor.writeStubTrapFile(c)
@@ -878,7 +880,7 @@ open class KotlinFileExtractor(
878880
extractTypeAccessRecursive(substitutedType, location, id, -1)
879881
}
880882
val syntheticParameterNames = isUnderscoreParameter(vp) || ((vp.parent as? IrFunction)?.let { hasSynthesizedParameterNames(it) } ?: true)
881-
extractAnnotations(vp, id)
883+
extractAnnotations(vp, id, extractTypeAccess)
882884
return extractValueParameter(id, substitutedType, vp.name.asString(), location, parent, idx, useValueParameter(vp, parentSourceDeclaration), syntheticParameterNames, vp.isVararg, vp.isNoinline, vp.isCrossinline)
883885
}
884886
}
@@ -1419,7 +1421,7 @@ open class KotlinFileExtractor(
14191421
linesOfCode?.linesOfCodeInDeclaration(f, id)
14201422

14211423
if (extractAnnotations)
1422-
extractAnnotations(f, id)
1424+
extractAnnotations(f, id, extractMethodAndParameterTypeAccesses)
14231425

14241426
return id
14251427
}
@@ -1432,13 +1434,13 @@ open class KotlinFileExtractor(
14321434
&& f.symbol !is IrConstructorSymbol // not a constructor
14331435
}
14341436

1435-
private fun extractField(f: IrField, parentId: Label<out DbReftype>): Label<out DbField> {
1437+
private fun extractField(f: IrField, parentId: Label<out DbReftype>, extractAnnotationEnumTypeAccesses: Boolean): Label<out DbField> {
14361438
with("field", f) {
14371439
DeclarationStackAdjuster(f).use {
14381440
val fNameSuffix = getExtensionReceiverType(f)?.let { it.classFqName?.asString()?.replace(".", "$$") } ?: ""
14391441
val extractType = if (isAnnotationClassField(f)) kClassToJavaClass(f.type) else f.type
14401442
val id = useField(f)
1441-
extractAnnotations(f, id)
1443+
extractAnnotations(f, id, extractAnnotationEnumTypeAccesses)
14421444
return extractField(id, "${f.name.asString()}$fNameSuffix", extractType, parentId, tw.getLocation(f), f.visibility, f, isExternalDeclaration(f), f.isFinal, isDirectlyExposedCompanionObjectField(f))
14431445
}
14441446
}
@@ -1522,7 +1524,7 @@ open class KotlinFileExtractor(
15221524
if (bf != null && extractBackingField) {
15231525
val fieldParentId = useDeclarationParent(getFieldParent(bf), false)
15241526
if (fieldParentId != null) {
1525-
val fieldId = extractField(bf, fieldParentId.cast())
1527+
val fieldId = extractField(bf, fieldParentId.cast(), extractFunctionBodies)
15261528
tw.writeKtPropertyBackingFields(id, fieldId)
15271529
if (p.isDelegated) {
15281530
tw.writeKtPropertyDelegates(id, fieldId)
@@ -1578,7 +1580,7 @@ open class KotlinFileExtractor(
15781580
extractDeclaration(it, extractPrivateMembers, extractFunctionBodies, extractAnnotations = true)
15791581
}
15801582

1581-
extractAnnotations(ee, id)
1583+
extractAnnotations(ee, id, extractFunctionBodies)
15821584
}
15831585
}
15841586
}
@@ -4240,6 +4242,7 @@ open class KotlinFileExtractor(
42404242
idx: Int,
42414243
enclosingCallable: Label<out DbCallable>?,
42424244
enclosingStmt: Label<out DbStmt>?,
4245+
extractTypeAccess: Boolean = true,
42434246
overrideId: Label<out DbExpr>? = null
42444247
) =
42454248
exprIdOrFresh<DbVaraccess>(overrideId).also { id ->
@@ -4254,7 +4257,8 @@ open class KotlinFileExtractor(
42544257
val vId = useEnumEntry(owner)
42554258
tw.writeVariableBinding(id, vId)
42564259

4257-
extractStaticTypeAccessQualifier(owner, id, locId, enclosingCallable, enclosingStmt)
4260+
if (extractTypeAccess)
4261+
extractStaticTypeAccessQualifier(owner, id, locId, enclosingCallable, enclosingStmt)
42584262

42594263
}
42604264
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ class MetaAnnotationSupport(private val logger: FileLogger, private val pluginCo
352352
}
353353

354354
// Adapted from AdditionalClassAnnotationLowering.kt
355-
private fun generateRepeatableAnnotation(irClass: IrClass): IrConstructorCall? {
355+
private fun generateRepeatableAnnotation(irClass: IrClass, extractAnnotationTypeAccesses: Boolean): IrConstructorCall? {
356356
if (!irClass.hasAnnotation(StandardNames.FqNames.repeatable) ||
357357
irClass.hasAnnotation(JvmAnnotationNames.REPEATABLE_ANNOTATION)
358358
) return null
@@ -361,7 +361,7 @@ class MetaAnnotationSupport(private val logger: FileLogger, private val pluginCo
361361

362362
val containerClass = getOrCreateSyntheticRepeatableAnnotationContainer(irClass)
363363
// Whenever a repeatable annotation with a Kotlin-synthesised container is extracted, extract the synthetic container to the same trap file.
364-
extractor.extractClassSource(containerClass, extractDeclarations = true, extractStaticInitializer = true, extractPrivateMembers = true, extractFunctionBodies = true)
364+
extractor.extractClassSource(containerClass, extractDeclarations = true, extractStaticInitializer = true, extractPrivateMembers = true, extractFunctionBodies = extractAnnotationTypeAccesses)
365365

366366
val containerReference = IrClassReferenceImpl(
367367
UNDEFINED_OFFSET, UNDEFINED_OFFSET, pluginContext.irBuiltIns.kClassClass.typeWith(containerClass.defaultType),
@@ -389,8 +389,8 @@ class MetaAnnotationSupport(private val logger: FileLogger, private val pluginCo
389389
)
390390
}
391391

392-
fun generateJavaMetaAnnotations(c: IrClass) =
392+
fun generateJavaMetaAnnotations(c: IrClass, extractAnnotationTypeAccesses: Boolean) =
393393
// This is essentially AdditionalClassAnnotationLowering adapted to run outside the backend.
394-
listOfNotNull(generateTargetAnnotation(c), generateRetentionAnnotation(c), generateRepeatableAnnotation(c), generateDocumentedAnnotation(c))
394+
listOfNotNull(generateTargetAnnotation(c), generateRetentionAnnotation(c), generateRepeatableAnnotation(c, extractAnnotationTypeAccesses), generateDocumentedAnnotation(c))
395395
}
396396

0 commit comments

Comments
 (0)