Skip to content

Commit b29b236

Browse files
committed
Enable eliding function bodies in header mode.
^KT-78422
1 parent 07f4d3e commit b29b236

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/LightTreeRawFirDeclarationBuilder.kt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class LightTreeRawFirDeclarationBuilder(
9797
}
9898
IMPORT_LIST -> importList += convertImportDirectives(child)
9999
CLASS -> firDeclarationList += convertClass(child)
100-
FUN -> firDeclarationList += convertFunctionDeclaration(child) as FirDeclaration
100+
FUN -> firDeclarationList += convertFunctionDeclaration(child, headerCompilationMode) as FirDeclaration
101101
KtNodeTypes.PROPERTY -> firDeclarationList += convertPropertyDeclaration(child)
102102
TYPEALIAS -> firDeclarationList += convertTypeAlias(child)
103103
OBJECT_DECLARATION -> firDeclarationList += convertClass(child)
@@ -1923,7 +1923,7 @@ class LightTreeRawFirDeclarationBuilder(
19231923
/**
19241924
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseFunction
19251925
*/
1926-
fun convertFunctionDeclaration(functionDeclaration: LighterASTNode): FirStatement {
1926+
fun convertFunctionDeclaration(functionDeclaration: LighterASTNode, generateHeaders: Boolean = false): FirStatement {
19271927
var modifiers: ModifierList? = null
19281928
var identifier: String? = null
19291929
var valueParametersList: LighterASTNode? = null
@@ -1940,6 +1940,8 @@ class LightTreeRawFirDeclarationBuilder(
19401940
identifier = it.asText
19411941
}
19421942

1943+
var headerMode = generateHeaders
1944+
19431945
val isLocal = isCallableLocal(functionDeclaration) { getParent() }
19441946
val functionSource = functionDeclaration.toFirSourceElement()
19451947
val isAnonymousFunction = identifier == null && isLocal
@@ -1971,6 +1973,11 @@ class LightTreeRawFirDeclarationBuilder(
19711973

19721974
val calculatedModifiers = modifiers ?: ModifierList()
19731975

1976+
if (calculatedModifiers.hasInline()) {
1977+
// We need to disable header mode for inline functions.
1978+
headerMode = false
1979+
}
1980+
19741981
if (returnType == null) {
19751982
returnType =
19761983
if (block != null || !hasEqToken) implicitUnitType
@@ -2066,7 +2073,7 @@ class LightTreeRawFirDeclarationBuilder(
20662073

20672074
val allowLegacyContractDescription = outerContractDescription == null
20682075
val bodyWithContractDescription = withForcedLocalContext {
2069-
convertFunctionBody(block, expression, allowLegacyContractDescription)
2076+
convertFunctionBody(block, expression, allowLegacyContractDescription, headerMode)
20702077
}
20712078
this.body = bodyWithContractDescription.first
20722079
val contractDescription = outerContractDescription ?: bodyWithContractDescription.second
@@ -2103,11 +2110,12 @@ class LightTreeRawFirDeclarationBuilder(
21032110
private fun convertFunctionBody(
21042111
blockNode: LighterASTNode?,
21052112
expression: LighterASTNode?,
2106-
allowLegacyContractDescription: Boolean
2113+
allowLegacyContractDescription: Boolean,
2114+
generateHeaders: Boolean = false,
21072115
): Pair<FirBlock?, FirContractDescription?> {
21082116
return when {
21092117
blockNode != null -> {
2110-
val block = convertBlock(blockNode)
2118+
val block = convertBlock(blockNode) // We might be able to process only the contract statement in the body.
21112119
val contractDescription = runIf(allowLegacyContractDescription) {
21122120
val blockSource = block.source
21132121
val diagnostic = when {
@@ -2117,7 +2125,11 @@ class LightTreeRawFirDeclarationBuilder(
21172125
}
21182126
processLegacyContractDescription(block, diagnostic)
21192127
}
2120-
block to contractDescription
2128+
if (generateHeaders) {
2129+
null to contractDescription // We want to preserve the contract info when processing as headers.
2130+
} else {
2131+
block to contractDescription
2132+
}
21212133
}
21222134
expression != null -> FirSingleExpressionBlock(
21232135
expressionConverter.getAsFirExpression<FirExpression>(expression, "Function has no body (but should)").toReturn()

compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.codegen.state.JvmBackendConfig
2929
import org.jetbrains.kotlin.config.JVMConfigurationKeys
3030
import org.jetbrains.kotlin.config.LanguageFeature
3131
import org.jetbrains.kotlin.config.LanguageVersionSettings
32+
import org.jetbrains.kotlin.config.headerCompilation
3233
import org.jetbrains.kotlin.descriptors.CallableDescriptor
3334
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
3435
import org.jetbrains.kotlin.descriptors.VariableAccessorDescriptor
@@ -216,7 +217,7 @@ class ExpressionCodegen(
216217
mv.visitCode()
217218
val startLabel = markNewLabel()
218219
val info = BlockInfo()
219-
if (state.classBuilderMode.generateBodies) {
220+
if (state.classBuilderMode.generateBodies && !state.configuration.headerCompilation) {
220221
if (irFunction.isMultifileBridge()) {
221222
// Multifile bridges need to have line number 1 to be filtered out by the intellij debugging filters.
222223
mv.visitLineNumber(1, startLabel)

0 commit comments

Comments
 (0)