'ignoreErrors' as a PSI2IR configuration parameter.

This commit is contained in:
Dmitry Petrov
2016-09-05 09:01:59 +03:00
committed by Dmitry Petrov
parent ea83407dce
commit 535ddd639f
6 changed files with 42 additions and 12 deletions
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.psi2ir
class Psi2IrConfiguration(
val ignoreErrors: Boolean
)
@@ -24,9 +24,9 @@ import org.jetbrains.kotlin.psi2ir.generators.ModuleGenerator
import org.jetbrains.kotlin.psi2ir.transformations.insertImplicitCasts
import org.jetbrains.kotlin.resolve.BindingContext
class Psi2IrTranslator() {
class Psi2IrTranslator(val configuration: Psi2IrConfiguration) {
fun generateModule(moduleDescriptor: ModuleDescriptor, ktFiles: List<KtFile>, bindingContext: BindingContext): IrModule {
val context = GeneratorContext(moduleDescriptor, bindingContext)
val context = GeneratorContext(configuration, moduleDescriptor, bindingContext)
val irModule = ModuleGenerator(context).generateModule(ktFiles)
postprocess(irModule)
return irModule
@@ -25,13 +25,19 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.types.ErrorUtils
class ErrorExpressionGenerator(statementGenerator: StatementGenerator) : StatementGeneratorExtension(statementGenerator) {
private val ignoreErrors: Boolean get() = context.configuration.ignoreErrors
private inline fun generateErrorExpression(message: String? = null, body: () -> IrExpression) =
if (ignoreErrors) body() else throw RuntimeException(message)
fun generateErrorExpression(ktElement: KtElement, message: String?): IrExpression =
IrErrorExpressionImpl(ktElement.startOffset, ktElement.endOffset,
if (ktElement is KtExpression) getErrorExpressionType(ktElement) else ErrorUtils.createErrorType(""),
message ?: "")
generateErrorExpression {
IrErrorExpressionImpl(ktElement.startOffset, ktElement.endOffset,
if (ktElement is KtExpression) getErrorExpressionType(ktElement) else ErrorUtils.createErrorType(""),
message ?: "")
}
fun generateErrorCall(ktCall: KtCallExpression): IrExpression {
fun generateErrorCall(ktCall: KtCallExpression): IrExpression = generateErrorExpression {
val type = getErrorExpressionType(ktCall)
val irErrorCall = IrErrorCallExpressionImpl(ktCall.startOffset, ktCall.endOffset, type, "") // TODO problem description?
@@ -50,13 +56,13 @@ class ErrorExpressionGenerator(statementGenerator: StatementGenerator) : Stateme
irErrorCall.addArgument(statementGenerator.generateExpression(it.getArgumentExpression()))
}
return irErrorCall
irErrorCall
}
private fun getErrorExpressionType(ktExpression: KtExpression) =
getInferredTypeWithImplicitCasts(ktExpression) ?: ErrorUtils.createErrorType("")
fun generateErrorExpression(ktName: KtSimpleNameExpression): IrExpression {
fun generateErrorSimpleName(ktName: KtSimpleNameExpression): IrExpression = generateErrorExpression {
val type = getErrorExpressionType(ktName)
val irErrorCall = IrErrorCallExpressionImpl(ktName.startOffset, ktName.endOffset, type, "") // TODO problem description?
@@ -65,7 +71,7 @@ class ErrorExpressionGenerator(statementGenerator: StatementGenerator) : Stateme
else statementGenerator.generateExpression(ktParent.receiverExpression)
}
return irErrorCall
irErrorCall
}
}
@@ -20,10 +20,12 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.ReflectionTypes
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration
import org.jetbrains.kotlin.psi2ir.PsiSourceManager
import org.jetbrains.kotlin.resolve.BindingContext
class GeneratorContext(
val configuration: Psi2IrConfiguration,
val moduleDescriptor: ModuleDescriptor,
val bindingContext: BindingContext
) {
@@ -246,7 +246,7 @@ class StatementGenerator(
return generateExpressionForReferencedDescriptor(referenceTarget, expression, null)
}
return ErrorExpressionGenerator(this).generateErrorExpression(expression)
return ErrorExpressionGenerator(this).generateErrorSimpleName(expression)
}
private fun generateExpressionForReferencedDescriptor(
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.ir
import org.jetbrains.kotlin.codegen.CodegenTestCase
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration
import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator
import org.jetbrains.kotlin.resolve.AnalyzingUtils
import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil
@@ -41,7 +42,7 @@ abstract class AbstractIrGeneratorTestCase : CodegenTestCase() {
analysisResult.throwIfError()
AnalyzingUtils.throwExceptionOnErrors(analysisResult.bindingContext)
}
val psi2ir = Psi2IrTranslator()
val psi2ir = Psi2IrTranslator(Psi2IrConfiguration(ignoreErrors))
val irModule = psi2ir.generateModule(analysisResult.moduleDescriptor, myFiles.psiFiles, analysisResult.bindingContext)
val ktFiles = testFiles.filter { it.name.endsWith(".kt") }
return ktFiles.zip(irModule.files).toMap()