McpScriptConfigurator

internal class McpScriptConfigurator(session: FirSession, val lowered: Boolean) : FirScriptConfiguratorExtension

Configures an execute_code snippet as a FirScript whose whole body is the body of one lambda, invoked on the spot: ({ body })(), the inside of a run { }. SnippetParser parses the text under function-body rules and wraps all of it in a single script initializer, which the stock light-tree converter turns into one block of local declarations and statements; this moves that block into the lambda. There, data flow runs from each statement into the next and a var is a local — the two things a smart cast needs, and neither of which a script's class-body top level gives it.

The reshaping is for a lowered script, one that goes on through fir2ir to the backend. Analysis leaves the body the initializer it parses to: LL FIR rebuilds every body it resolves from PSI, which holds no lambda, and rebuilds a result property's value only out of an initializer that is a single expression. An initializer is a data-flow root as well; it is fir2ir that needs the lambda.

Constructors

Link copied to clipboard
constructor(session: FirSession, lowered: Boolean)

Properties

Link copied to clipboard
override val extensionType: KClass<out FirExtension>
Link copied to clipboard
private val lowered: Boolean
Link copied to clipboard
override val name: FirExtensionPointName
Link copied to clipboard
val session: FirSession

Functions

Link copied to clipboard
open override fun accepts(sourceFile: KtSourceFile?, scriptSource: KtSourceElement): Boolean
Link copied to clipboard
open override fun FirScriptBuilder.configure(sourceFile: KtSourceFile?, context: Context<*>)
Link copied to clipboard
open override fun FirScriptBuilder.configureContainingFile(fileBuilder: FirFileBuilder)
Link copied to clipboard
private fun invokedOnTheSpot(body: FirBlock, source: KtSourceElement): FirFunctionCall

({ body })() — the body of a lambda, as in run { }, invoked on the spot. A lambda is a root for the data-flow analysis: what decides whether a var a closure writes may still smart-cast. A property initializer is no root, so it would let such a smart cast through; the anonymous initializer the body arrives in is one, but fir2ir flattens it into the script's top level, where every local variable becomes a property. The backend inlines a lambda invoked on the spot, so the body still runs straight in the constructor.

Link copied to clipboard
open fun FirDeclarationPredicateRegistrar.registerPredicates()
Link copied to clipboard
private fun resultProperty(value: FirExpression, source: KtSourceElement, packageFqName: FqName): FirProperty

The property holding the snippet's value: value's, the body's last expression under the type it resolved to. A body ending on a statement makes that Unit, and fir2ir then emits the invocation as a plain statement and no property at all — so a snippet has a result exactly when it ends on a value, with nothing here deciding which.