[FIR] Allows use DSL for building FIR declarations from any FirExtension

This commit is contained in:
Dmitriy Novozhilov
2023-02-01 10:47:57 +02:00
committed by Space Team
parent 1248ee12d6
commit a967a242c7
4 changed files with 27 additions and 19 deletions
@@ -18,6 +18,7 @@ import org.jetbrains.kotlin.fir.declarations.builder.buildRegularClass
import org.jetbrains.kotlin.fir.declarations.origin
import org.jetbrains.kotlin.fir.declarations.utils.isLocal
import org.jetbrains.kotlin.fir.extensions.FirDeclarationGenerationExtension
import org.jetbrains.kotlin.fir.extensions.FirExtension
import org.jetbrains.kotlin.fir.moduleData
import org.jetbrains.kotlin.fir.scopes.kotlinScopeProvider
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
@@ -99,12 +100,13 @@ public class ClassBuildingContext(
/**
* Creates top-level class with given [classId]
* All declarations in class should be generated using methods from [FirDeclarationGenerationExtension]
* Generation of top-level classes with [FirDeclarationsForMetadataProviderExtension] is prohibited
*
* If no supertypes added then [kotlin.Any] supertype will be added automatically
*
* Created class won't have a constructor; constructor can be added separately with [createConstructor] function
*/
public fun FirDeclarationGenerationExtension.createTopLevelClass(
public fun FirExtension.createTopLevelClass(
classId: ClassId,
key: GeneratedDeclarationKey,
classKind: ClassKind = ClassKind.CLASS,
@@ -115,7 +117,10 @@ public fun FirDeclarationGenerationExtension.createTopLevelClass(
/**
* Creates nested class for [owner] class with name [name]
* All declarations in class should be generated using methods from [FirDeclarationGenerationExtension]
* If class is generated in [FirDeclarationGenerationExtension], all its declarations should be generated
* using methods from [FirDeclarationGenerationExtension]
* If class is generated in [FirDeclarationsForMetadataProviderExtension] all its declarations should be manually added right to
* FIR node of created class
*
* If no supertypes added then [kotlin.Any] supertype will be added automatically
*
@@ -123,7 +128,7 @@ public fun FirDeclarationGenerationExtension.createTopLevelClass(
*
* By default, the class is only nested; to create an inner class, create nested class and add inner status via status()
*/
public fun FirDeclarationGenerationExtension.createNestedClass(
public fun FirExtension.createNestedClass(
owner: FirClassSymbol<*>,
name: Name,
key: GeneratedDeclarationKey,
@@ -135,13 +140,16 @@ public fun FirDeclarationGenerationExtension.createNestedClass(
/**
* Creates companion object for [owner] class
* All declarations in class should be generated using methods from [FirDeclarationGenerationExtension]
* If class is generated in [FirDeclarationGenerationExtension], all its declarations should be generated
* using methods from [FirDeclarationGenerationExtension]
* If class is generated in [FirDeclarationsForMetadataProviderExtension] all its declarations should be manually added right to
* FIR node of created class
*
* If no supertypes added then [kotlin.Any] supertype will be added automatically
*
* Created class won't have a constructor; constructor can be added separately with [createDefaultPrivateConstructor] function
*/
public fun FirDeclarationGenerationExtension.createCompanionObject(
public fun FirExtension.createCompanionObject(
owner: FirClassSymbol<*>,
key: GeneratedDeclarationKey,
config: ClassBuildingContext.() -> Unit = {}
@@ -18,7 +18,7 @@ import org.jetbrains.kotlin.fir.declarations.origin
import org.jetbrains.kotlin.fir.declarations.utils.isInner
import org.jetbrains.kotlin.fir.expressions.buildResolvedArgumentList
import org.jetbrains.kotlin.fir.expressions.builder.buildDelegatedConstructorCall
import org.jetbrains.kotlin.fir.extensions.FirDeclarationGenerationExtension
import org.jetbrains.kotlin.fir.extensions.FirExtension
import org.jetbrains.kotlin.fir.getContainingClassLookupTag
import org.jetbrains.kotlin.fir.moduleData
import org.jetbrains.kotlin.fir.references.builder.buildResolvedNamedReference
@@ -115,7 +115,7 @@ public class ConstructorBuildingContext(
*
* If you want to create custom delegated constructor call please do it in the [IrGenerationExtension]
*/
public fun FirDeclarationGenerationExtension.createConstructor(
public fun FirExtension.createConstructor(
owner: FirClassSymbol<*>,
key: GeneratedDeclarationKey,
isPrimary: Boolean = false,
@@ -140,7 +140,7 @@ public fun FirDeclarationGenerationExtension.createConstructor(
*
* If you want to create custom delegated constructor call please do it in the [IrGenerationExtension]
*/
public fun FirDeclarationGenerationExtension.createDefaultPrivateConstructor(
public fun FirExtension.createDefaultPrivateConstructor(
owner: FirClassSymbol<*>,
key: GeneratedDeclarationKey,
generateDelegatedNoArgConstructorCall: Boolean = true
@@ -150,7 +150,7 @@ public fun FirDeclarationGenerationExtension.createDefaultPrivateConstructor(
}
}
context(FirDeclarationGenerationExtension)
context(FirExtension)
private fun FirConstructor.generateNoArgDelegatingConstructorCall() {
val owner = returnTypeRef.coneType.toSymbol(session) as? FirClassSymbol<*>
requireNotNull(owner)
@@ -15,7 +15,7 @@ import org.jetbrains.kotlin.fir.declarations.builder.buildReceiverParameter
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyBackingField
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyGetter
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertySetter
import org.jetbrains.kotlin.fir.extensions.FirDeclarationGenerationExtension
import org.jetbrains.kotlin.fir.extensions.FirExtension
import org.jetbrains.kotlin.fir.moduleData
import org.jetbrains.kotlin.fir.resolve.defaultType
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
@@ -117,7 +117,7 @@ public class PropertyBuildingContext(
/**
* Creates a member property for [owner] class with [returnType] return type
*/
public fun FirDeclarationGenerationExtension.createMemberProperty(
public fun FirExtension.createMemberProperty(
owner: FirClassSymbol<*>,
key: GeneratedDeclarationKey,
name: Name,
@@ -133,7 +133,7 @@ public fun FirDeclarationGenerationExtension.createMemberProperty(
* Creates a member property for [owner] class with return type provided by [returnTypeProvider]
* Use this overload when those types use type parameters of constructed property
*/
public fun FirDeclarationGenerationExtension.createMemberProperty(
public fun FirExtension.createMemberProperty(
owner: FirClassSymbol<*>,
key: GeneratedDeclarationKey,
name: Name,
@@ -152,7 +152,7 @@ public fun FirDeclarationGenerationExtension.createMemberProperty(
* If you create top-level extension property don't forget to set [hasBackingField] to false,
* since such properties never have backing fields
*/
public fun FirDeclarationGenerationExtension.createTopLevelProperty(
public fun FirExtension.createTopLevelProperty(
key: GeneratedDeclarationKey,
callableId: CallableId,
returnType: ConeKotlinType,
@@ -171,7 +171,7 @@ public fun FirDeclarationGenerationExtension.createTopLevelProperty(
*
* Use this overload when those types use type parameters of constructed property
*/
public fun FirDeclarationGenerationExtension.createTopLevelProperty(
public fun FirExtension.createTopLevelProperty(
key: GeneratedDeclarationKey,
callableId: CallableId,
returnTypeProvider: (List<FirTypeParameterRef>) -> ConeKotlinType,
@@ -13,7 +13,7 @@ import org.jetbrains.kotlin.fir.declarations.FirTypeParameter
import org.jetbrains.kotlin.fir.declarations.builder.buildReceiverParameter
import org.jetbrains.kotlin.fir.declarations.builder.buildSimpleFunction
import org.jetbrains.kotlin.fir.declarations.origin
import org.jetbrains.kotlin.fir.extensions.FirDeclarationGenerationExtension
import org.jetbrains.kotlin.fir.extensions.FirExtension
import org.jetbrains.kotlin.fir.moduleData
import org.jetbrains.kotlin.fir.resolve.defaultType
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
@@ -89,7 +89,7 @@ public class SimpleFunctionBuildingContext(
*
* Type and value parameters can be configured with [config] builder lambda
*/
public fun FirDeclarationGenerationExtension.createMemberFunction(
public fun FirExtension.createMemberFunction(
owner: FirClassSymbol<*>,
key: GeneratedDeclarationKey,
name: Name,
@@ -105,7 +105,7 @@ public fun FirDeclarationGenerationExtension.createMemberFunction(
*
* Type and value parameters can be configured with [config] builder lambda
*/
public fun FirDeclarationGenerationExtension.createMemberFunction(
public fun FirExtension.createMemberFunction(
owner: FirClassSymbol<*>,
key: GeneratedDeclarationKey,
name: Name,
@@ -121,7 +121,7 @@ public fun FirDeclarationGenerationExtension.createMemberFunction(
*
* Type and value parameters can be configured with [config] builder lambda
*/
public fun FirDeclarationGenerationExtension.createTopLevelFunction(
public fun FirExtension.createTopLevelFunction(
key: GeneratedDeclarationKey,
callableId: CallableId,
returnType: ConeKotlinType,
@@ -136,7 +136,7 @@ public fun FirDeclarationGenerationExtension.createTopLevelFunction(
*
* Type and value parameters can be configured with [config] builder lambda
*/
public fun FirDeclarationGenerationExtension.createTopLevelFunction(
public fun FirExtension.createTopLevelFunction(
key: GeneratedDeclarationKey,
callableId: CallableId,
returnTypeProvider: (List<FirTypeParameter>) -> ConeKotlinType,