[FIR] Build raw FIR for code fragments

This commit is contained in:
Yan Zhulanow
2023-06-14 16:09:35 +09:00
parent b7921a401b
commit 8f798119cc
@@ -1088,23 +1088,28 @@ open class PsiRawFirBuilder(
aliasSource = importDirective.alias?.nameIdentifier?.toFirSourceElement()
}
}
for (declaration in file.declarations) {
declarations += when (declaration) {
is KtScript -> {
require(file.declarations.size == 1) { "Expect the script to be the only declaration in the file $name" }
convertScript(declaration, this.name) {
for (configurator in baseSession.extensionService.scriptConfigurators) {
with(configurator) { configure(this@buildFile) }
if (file is KtCodeFragment) {
declarations += convertCodeFragment(file)
} else {
for (declaration in file.declarations) {
declarations += when (declaration) {
is KtScript -> {
require(file.declarations.size == 1) { "Expect the script to be the only declaration in the file $name" }
convertScript(declaration, this.name) {
for (configurator in baseSession.extensionService.scriptConfigurators) {
with(configurator) { configure(this@buildFile) }
}
}
}
is KtDestructuringDeclaration -> buildErrorTopLevelDestructuringDeclaration(declaration.toFirSourceElement())
else -> declaration.convert()
}
is KtDestructuringDeclaration -> buildErrorTopLevelDestructuringDeclaration(declaration.toFirSourceElement())
else -> declaration.convert()
}
}
for (danglingModifierList in file.danglingModifierLists) {
declarations += buildErrorTopLevelDeclarationForDanglingModifierList(danglingModifierList)
for (danglingModifierList in file.danglingModifierLists) {
declarations += buildErrorTopLevelDeclarationForDanglingModifierList(danglingModifierList)
}
}
}
}
@@ -1157,6 +1162,20 @@ open class PsiRawFirBuilder(
}
}
private fun convertCodeFragment(file: KtCodeFragment): FirCodeFragment {
return buildCodeFragment {
source = file.toFirSourceElement()
moduleData = baseModuleData
origin = FirDeclarationOrigin.Source
symbol = FirCodeFragmentSymbol()
block = when (file) {
is KtExpressionCodeFragment -> file.getContentElement()?.toFirBlock() ?: buildEmptyExpressionBlock()
is KtBlockCodeFragment -> configureBlockWithoutBuilding(file.getContentElement()).build()
else -> error("Unexpected code fragment type: " + file::class.java)
}
}
}
// TODO: if no original FirScript is passed here, the invalid script could be constructed, consider throwing an error
override fun visitScript(script: KtScript, data: FirElement?): FirElement {
val ktFile = script.containingKtFile