Skip to content

Commit abfd398

Browse files
committed
Enable eliding function bodies in header mode.
^KT-78422
1 parent d5684a8 commit abfd398

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.fir.lightTree.converter
77

88
import com.intellij.lang.LighterASTNode
99
import com.intellij.psi.TokenType
10+
import com.intellij.util.containers.addIfNotNull
1011
import com.intellij.util.diff.FlyweightCapableTreeStructure
1112
import org.jetbrains.kotlin.*
1213
import org.jetbrains.kotlin.ElementTypeUtils.isExpression
@@ -1923,7 +1924,7 @@ class LightTreeRawFirDeclarationBuilder(
19231924
/**
19241925
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseFunction
19251926
*/
1926-
fun convertFunctionDeclaration(functionDeclaration: LighterASTNode): FirStatement {
1927+
fun convertFunctionDeclaration(functionDeclaration: LighterASTNode, generateHeaders: Boolean = false): FirStatement {
19271928
var modifiers: ModifierList? = null
19281929
var identifier: String? = null
19291930
var valueParametersList: LighterASTNode? = null
@@ -1940,6 +1941,8 @@ class LightTreeRawFirDeclarationBuilder(
19401941
identifier = it.asText
19411942
}
19421943

1944+
var headerMode = generateHeaders
1945+
19431946
val isLocal = isCallableLocal(functionDeclaration) { getParent() }
19441947
val functionSource = functionDeclaration.toFirSourceElement()
19451948
val isAnonymousFunction = identifier == null && isLocal
@@ -1971,6 +1974,11 @@ class LightTreeRawFirDeclarationBuilder(
19711974

19721975
val calculatedModifiers = modifiers ?: ModifierList()
19731976

1977+
if (calculatedModifiers.hasInline()) {
1978+
// We need to disable header mode for inline functions.
1979+
headerMode = false
1980+
}
1981+
19741982
if (returnType == null) {
19751983
returnType =
19761984
if (block != null || !hasEqToken) implicitUnitType
@@ -2066,7 +2074,7 @@ class LightTreeRawFirDeclarationBuilder(
20662074

20672075
val allowLegacyContractDescription = outerContractDescription == null
20682076
val bodyWithContractDescription = withForcedLocalContext {
2069-
convertFunctionBody(block, expression, allowLegacyContractDescription)
2077+
convertFunctionBody(block, expression, allowLegacyContractDescription, headerMode)
20702078
}
20712079
this.body = bodyWithContractDescription.first
20722080
val contractDescription = outerContractDescription ?: bodyWithContractDescription.second
@@ -2103,11 +2111,12 @@ class LightTreeRawFirDeclarationBuilder(
21032111
private fun convertFunctionBody(
21042112
blockNode: LighterASTNode?,
21052113
expression: LighterASTNode?,
2106-
allowLegacyContractDescription: Boolean
2114+
allowLegacyContractDescription: Boolean,
2115+
generateHeaders: Boolean = false,
21072116
): Pair<FirBlock?, FirContractDescription?> {
21082117
return when {
21092118
blockNode != null -> {
2110-
val block = convertBlock(blockNode)
2119+
val block = convertBlock(blockNode) // We might be able to process only the contract statement in the body.
21112120
val contractDescription = runIf(allowLegacyContractDescription) {
21122121
val blockSource = block.source
21132122
val diagnostic = when {
@@ -2117,7 +2126,11 @@ class LightTreeRawFirDeclarationBuilder(
21172126
}
21182127
processLegacyContractDescription(block, diagnostic)
21192128
}
2120-
block to contractDescription
2129+
if (generateHeaders) {
2130+
null to contractDescription // We want to preserve the contract info when processing as headers.
2131+
} else {
2132+
block to contractDescription
2133+
}
21212134
}
21222135
expression != null -> FirSingleExpressionBlock(
21232136
expressionConverter.getAsFirExpression<FirExpression>(expression, "Function has no body (but should)").toReturn()

0 commit comments

Comments
 (0)