[FIR] Add ability to create accessor to declaration attribute on symbol
This commit is contained in:
committed by
Space Team
parent
185e57e601
commit
ce8489a8a5
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.fir.plugin
|
||||
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.resolve.constructType
|
||||
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
|
||||
import org.jetbrains.kotlin.fir.symbols.constructStarProjectedType
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.ConeClassLikeLookupTagImpl
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
|
||||
import org.jetbrains.kotlin.fir.types.ConeClassLikeType
|
||||
import org.jetbrains.kotlin.fir.types.ConeTypeProjection
|
||||
import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
|
||||
public fun ClassId.createConeType(
|
||||
session: FirSession,
|
||||
typeArguments: Array<ConeTypeProjection> = emptyArray(),
|
||||
nullable: Boolean = false
|
||||
): ConeClassLikeType {
|
||||
val symbol = session.symbolProvider.getClassLikeSymbolByClassId(this) as? FirClassSymbol<*>
|
||||
|
||||
return when {
|
||||
symbol != null -> when {
|
||||
typeArguments.isEmpty() -> symbol.constructStarProjectedType(isNullable = nullable)
|
||||
else -> symbol.constructType(typeArguments, nullable)
|
||||
}
|
||||
else -> ConeClassLikeTypeImpl(
|
||||
ConeClassLikeLookupTagImpl(this),
|
||||
typeArguments,
|
||||
nullable
|
||||
)
|
||||
}
|
||||
}
|
||||
+4
@@ -85,5 +85,9 @@ inline fun <reified T : FirBasedSymbol<*>> FirSymbolProvider.getSymbolByTypeRef(
|
||||
return getSymbolByLookupTag(lookupTag) as? T
|
||||
}
|
||||
|
||||
fun FirSymbolProvider.getRegularClassSymbolByClassId(classId: ClassId): FirRegularClassSymbol? {
|
||||
return getClassLikeSymbolByClassId(classId) as? FirRegularClassSymbol
|
||||
}
|
||||
|
||||
val FirSession.symbolProvider: FirSymbolProvider by FirSession.sessionComponentAccessor()
|
||||
val FirSession.dependenciesSymbolProvider: FirSymbolProvider by FirSession.sessionComponentAccessor<FirDependenciesSymbolProvider>()
|
||||
|
||||
+16
@@ -5,6 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.fir.declarations
|
||||
|
||||
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
|
||||
import org.jetbrains.kotlin.fir.util.ConeTypeRegistry
|
||||
import org.jetbrains.kotlin.util.ArrayMap
|
||||
import org.jetbrains.kotlin.util.AttributeArrayOwner
|
||||
@@ -54,6 +55,11 @@ object FirDeclarationDataRegistry : ConeTypeRegistry<FirDeclarationDataKey, Any>
|
||||
return DeclarationDataAccessor(generateAnyNullableAccessor(kClass), kClass)
|
||||
}
|
||||
|
||||
fun <K : FirDeclarationDataKey> symbolAccessor(key: K): SymbolDataAccessor {
|
||||
val kClass = key::class
|
||||
return SymbolDataAccessor(generateAnyNullableAccessor(kClass), kClass)
|
||||
}
|
||||
|
||||
fun <K : FirDeclarationDataKey, V : Any> attributesAccessor(key: K): ReadWriteProperty<FirDeclarationAttributes, V?> {
|
||||
val kClass = key::class
|
||||
return AttributeDataAccessor(generateNullableAccessor(kClass), kClass)
|
||||
@@ -73,6 +79,16 @@ object FirDeclarationDataRegistry : ConeTypeRegistry<FirDeclarationDataKey, Any>
|
||||
}
|
||||
}
|
||||
|
||||
class SymbolDataAccessor(
|
||||
private val dataAccessor: NullableArrayMapAccessor<FirDeclarationDataKey, Any, *>,
|
||||
val key: KClass<out FirDeclarationDataKey>
|
||||
) {
|
||||
operator fun <V> getValue(thisRef: FirBasedSymbol<*>, property: KProperty<*>): V? {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return dataAccessor.getValue(thisRef.fir.attributes, property) as? V
|
||||
}
|
||||
}
|
||||
|
||||
private class AttributeDataAccessor<V : Any>(
|
||||
val dataAccessor: NullableArrayMapAccessor<FirDeclarationDataKey, Any, V>,
|
||||
val key: KClass<out FirDeclarationDataKey>
|
||||
|
||||
Reference in New Issue
Block a user