Introduce initial version of FIR Java type enhancement

Java type enhancement is performed by a special scope kind
Java FIR dump was added for multiplatform tests to look at enhancements
Overrides, J2K mapping, special cases does not work yet

Related to KT-29937
This commit is contained in:
Mikhail Glukhikh
2019-02-15 10:36:57 +03:00
parent 060bd1b464
commit f31faafd72
56 changed files with 2042 additions and 336 deletions
@@ -56,6 +56,10 @@ class FirRenderer(builder: StringBuilder) : FirVisitorVoid() {
printer.popIndent()
}
fun newLine() {
println()
}
override fun visitElement(element: FirElement) {
element.acceptChildren(this)
}
@@ -214,22 +218,32 @@ class FirRenderer(builder: StringBuilder) : FirVisitorVoid() {
}
private fun FirDeclarationContainer.renderDeclarations() {
renderInBraces {
for (declaration in declarations) {
declaration.accept(this@FirRenderer)
println()
}
}
}
fun renderInBraces(f: () -> Unit) {
println(" {")
pushIndent()
for (declaration in declarations) {
declaration.accept(this@FirRenderer)
println()
}
f()
popIndent()
println("}")
}
override fun visitRegularClass(regularClass: FirRegularClass) {
visitMemberDeclaration(regularClass)
fun renderSupertypes(regularClass: FirRegularClass) {
if (regularClass.superTypeRefs.isNotEmpty()) {
print(" : ")
regularClass.superTypeRefs.renderSeparated()
}
}
override fun visitRegularClass(regularClass: FirRegularClass) {
visitMemberDeclaration(regularClass)
renderSupertypes(regularClass)
regularClass.renderDeclarations()
}
@@ -345,14 +359,12 @@ class FirRenderer(builder: StringBuilder) : FirVisitorVoid() {
}
override fun visitBlock(block: FirBlock) {
println(" {")
pushIndent()
for (statement in block.statements) {
statement.accept(this)
println()
renderInBraces {
for (statement in block.statements) {
statement.accept(this)
println()
}
}
popIndent()
println("}")
}
override fun visitTypeAlias(typeAlias: FirTypeAlias) {
@@ -657,8 +669,10 @@ class FirRenderer(builder: StringBuilder) : FirVisitorVoid() {
buildString {
append("ft<")
append(lowerBound.asString())
append(lowerBound.nullability.suffix)
append(", ")
append(upperBound.asString())
append(upperBound.nullability.suffix)
append(">")
}
}
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.fir
import org.jetbrains.kotlin.analyzer.ModuleInfo
import org.jetbrains.kotlin.utils.Jsr305State
import kotlin.reflect.KClass
interface FirSession {
@@ -13,6 +14,8 @@ interface FirSession {
val sessionProvider: FirSessionProvider? get() = null
val jsr305State: Jsr305State? get() = null
val components: Map<KClass<*>, Any>
fun <T : Any> getService(kclass: KClass<T>): T =
@@ -29,6 +29,30 @@ fun <T : FirElement, D> MutableList<T>.transformInplace(transformer: FirTransfor
}
}
fun <T : FirElement, D> MutableList<T>.transformInplaceWithBeforeOperation(
transformer: FirTransformer<D>, data: D, operation: (T, Int) -> Unit
) {
val iterator = this.listIterator()
var index = 0
while (iterator.hasNext()) {
val next = iterator.next()
operation(next, index++)
val result = next.transform<T, D>(transformer, data)
if (result.isSingle) {
iterator.set(result.single)
} else {
val resultIterator = result.list.listIterator()
if (!resultIterator.hasNext()) {
iterator.remove()
} else {
iterator.set(resultIterator.next())
}
while (resultIterator.hasNext()) {
iterator.add(resultIterator.next())
}
}
}
}
fun <T : FirElement, D> T.transformSingle(transformer: FirTransformer<D>, data: D): T {
return this.transform<T, D>(transformer, data).single
@@ -7,8 +7,11 @@ package org.jetbrains.kotlin.fir.expressions
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.fir.BaseTransformedType
import org.jetbrains.kotlin.fir.types.ConeClassLikeType
import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.visitors.FirVisitor
import org.jetbrains.kotlin.name.FqName
@BaseTransformedType
interface FirAnnotationCall : FirCall {
@@ -24,4 +27,7 @@ interface FirAnnotationCall : FirCall {
annotationTypeRef.accept(visitor, data)
super.acceptChildren(visitor, data)
}
}
}
val FirAnnotationCall.resolvedFqName: FqName?
get() = ((annotationTypeRef as? FirResolvedTypeRef)?.type as? ConeClassLikeType)?.lookupTag?.classId?.asSingleFqName()
@@ -5,9 +5,19 @@
package org.jetbrains.kotlin.fir.expressions
import org.jetbrains.kotlin.fir.FirResolvedCallableReference
import org.jetbrains.kotlin.fir.symbols.ConeCallableSymbol
import org.jetbrains.kotlin.fir.visitors.FirVisitor
interface FirExpression : FirStatement {
override fun <R, D> accept(visitor: FirVisitor<R, D>, data: D): R =
visitor.visitExpression(this, data)
}
fun FirExpression.toResolvedCallableReference(): FirResolvedCallableReference? {
return (this as? FirQualifiedAccess)?.calleeReference as? FirResolvedCallableReference
}
fun FirExpression.toResolvedCallableSymbol(): ConeCallableSymbol? {
return toResolvedCallableReference()?.callableSymbol
}