Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin: extract annotations #11258

Open
wants to merge 40 commits into
base: main
Choose a base branch
from

Conversation

smowton
Copy link
Contributor

@smowton smowton commented Nov 14, 2022

This establishes annotation extraction on a par with the Java extractor (annotations for classes/interfaces, value parameters, fields, callables, and enum entries, but noteworthily not for type parameters or type usages, which require database schema work.) Not annotating type usages particularly means that we don't yet need to worry about nullability annotations.

Wrinkles:

  • Certain meta-annotations require translation between Kotlin and JVM universes; the code for this is largely lifted with attribution from the Kotlin compiler.
  • Repeatable annotations are particularly difficult, requiring a synthetic container class when specified in Kotlin.
  • All annotation expressions require full global identifiers, because they can be extracted when seen from outside as an external dependency.

@smowton smowton requested review from a team as code owners Nov 14, 2022
@smowton smowton added the no-change-note-required This PR does not need a change note label Nov 14, 2022
java/kotlin-extractor/src/main/kotlin/utils/ClassNames.kt Outdated Show resolved Hide resolved
logger.warnElement("Expected container class to have a primary constructor", containerClass)
return null
} else {
return IrConstructorCallImpl.fromSymbolOwner(containerClass.defaultType, containerConstructor.symbol).apply {
Copy link
Contributor

@igfoo igfoo Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generating IR makes me nervous. I wonder whether it might be simpler to directly extract the list of annotations, rather than making an IR representation and extracting that?

@@ -4101,6 +4451,163 @@ open class KotlinFileExtractor(
extractExpressionExpr(loop.condition, callable, id, 0, id)
}

@Suppress("UNCHECKED_CAST")
private fun <T : DbExpr> exprIdOrFresh(id: Label<out DbExpr>?) = id as? Label<T> ?: tw.getFreshIdLabel()
Copy link
Contributor

@igfoo igfoo Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can avoid the warning suppression here by using something like id?.cast() ?: tw.getFreshIdLabel()

tw.writeExprs_stringliteral(id, type.javaResult.id, parent, idx)
tw.writeExprsKotlinType(id, type.kotlinResult.id)
extractExprContext(id, locId, enclosingCallable, enclosingStmt)
tw.writeNamestrings(toQuotedLiteral(v.toString()), v.toString(), id)
Copy link
Contributor

@tamasvajk tamasvajk Nov 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change results in a lot of changes to expected files. Would it make sense to do this in a separate PR to make the diff cleaner?

Copy link
Contributor Author

@smowton smowton Nov 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll do that

@@ -450,7 +464,406 @@ open class KotlinFileExtractor(
extractDeclInitializers(c.declarations, false) { Pair(blockId, obinitId) }
}

val jvmStaticFqName = FqName("kotlin.jvm.JvmStatic")
// Adapted from RepeatedAnnotationLowering.kt
Copy link
Contributor

@tamasvajk tamasvajk Nov 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is already quite large, can we easily move these adapted functions into a separate file?

tamasvajk and others added 23 commits Nov 16, 2022
This gives all annotations and expressions derived from them globally-significant and stable names, enabling the Kotlin and Java extractors to see the same annotation without introducing database conflicts.
Note however this includes extracting annotations for external types, unlike the situation for function bodies.
@smowton smowton force-pushed the smowton/feature/kotlin-annotation-extraction branch from a719a86 to 37e3ecd Compare Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Java Kotlin no-change-note-required This PR does not need a change note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants