psi2ir: untie IR building utils from PSI

This commit is contained in:
Svyatoslav Scherbina
2017-01-10 12:29:29 +07:00
parent 4c3fb9a21f
commit feb0dee563
8 changed files with 87 additions and 51 deletions
@@ -16,42 +16,35 @@
package org.jetbrains.kotlin.psi2ir.builders
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrBlockBodyImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrBlockImpl
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi2ir.generators.Generator
import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
import org.jetbrains.kotlin.psi2ir.generators.GeneratorWithScope
import org.jetbrains.kotlin.psi2ir.generators.Scope
import org.jetbrains.kotlin.types.KotlinType
import java.util.*
abstract class IrBuilder(
override val context: GeneratorContext,
override val context: IrGeneratorContext,
var startOffset: Int,
var endOffset: Int
) : Generator
) : IrGenerator
abstract class IrBuilderWithScope(
context: GeneratorContext,
context: IrGeneratorContext,
override val scope: Scope,
startOffset: Int,
endOffset: Int
) : IrBuilder(context, startOffset, endOffset), GeneratorWithScope
) : IrBuilder(context, startOffset, endOffset), IrGeneratorWithScope
abstract class IrStatementsBuilder<out T : IrElement>(
context: GeneratorContext,
context: IrGeneratorContext,
scope: Scope,
startOffset: Int,
endOffset: Int
) : IrBuilderWithScope(context, scope, startOffset, endOffset), GeneratorWithScope {
) : IrBuilderWithScope(context, scope, startOffset, endOffset), IrGeneratorWithScope {
operator fun IrStatement.unaryPlus() {
addStatement(this)
}
@@ -61,7 +54,7 @@ abstract class IrStatementsBuilder<out T : IrElement>(
}
open class IrBlockBodyBuilder(
context: GeneratorContext,
context: IrGeneratorContext,
scope: Scope,
startOffset: Int,
endOffset: Int
@@ -83,7 +76,7 @@ open class IrBlockBodyBuilder(
}
class IrBlockBuilder(
context: GeneratorContext, scope: Scope,
context: IrGeneratorContext, scope: Scope,
startOffset: Int, endOffset: Int,
val origin: IrStatementOrigin? = null,
var resultType: KotlinType? = null
@@ -115,23 +108,20 @@ fun <T : IrBuilder> T.at(startOffset: Int, endOffset: Int): T {
return this
}
fun <T : IrBuilder> T.at(psiElement: PsiElement): T {
this.startOffset = psiElement.startOffset
this.endOffset = psiElement.endOffset
return this
}
inline fun GeneratorWithScope.irBlock(ktElement: KtElement? = null, origin: IrStatementOrigin? = null, resultType: KotlinType? = null,
body: IrBlockBuilder.() -> Unit
inline fun IrGeneratorWithScope.irBlock(startOffset: Int = UNDEFINED_OFFSET, endOffset: Int = UNDEFINED_OFFSET,
origin: IrStatementOrigin? = null, resultType: KotlinType? = null,
body: IrBlockBuilder.() -> Unit
): IrExpression =
IrBlockBuilder(context, scope,
ktElement?.startOffset ?: UNDEFINED_OFFSET,
ktElement?.endOffset ?: UNDEFINED_OFFSET,
startOffset,
endOffset,
origin, resultType
).block(body)
inline fun GeneratorWithScope.irBlockBody(ktElement: KtElement? = null, body: IrBlockBodyBuilder.() -> Unit) : IrBlockBody =
inline fun IrGeneratorWithScope.irBlockBody(startOffset: Int = UNDEFINED_OFFSET, endOffset: Int = UNDEFINED_OFFSET,
body: IrBlockBodyBuilder.() -> Unit
) : IrBlockBody =
IrBlockBodyBuilder(context, scope,
ktElement?.startOffset ?: UNDEFINED_OFFSET,
ktElement?.endOffset ?: UNDEFINED_OFFSET
).blockBody(body)
startOffset,
endOffset
).blockBody(body)
@@ -0,0 +1,33 @@
/*
* Copyright 2010-2017 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.builders
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.psi2ir.generators.Scope
interface IrGenerator {
val context: IrGeneratorContext
}
interface IrGeneratorWithScope : IrGenerator {
val scope: Scope
}
open class IrGeneratorContext(val irBuiltIns: IrBuiltIns) {
val builtIns: KotlinBuiltIns get() = irBuiltIns.builtIns
}
@@ -21,11 +21,10 @@ import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.declarations.impl.IrClassImpl
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
import org.jetbrains.kotlin.psi2ir.generators.Scope
class IrMemberFunctionBuilder(
context: GeneratorContext,
context: IrGeneratorContext,
val irClass: IrClassImpl,
val function: FunctionDescriptor,
val origin: IrDeclarationOrigin,
@@ -26,7 +26,6 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi2ir.builders.defineTemporary
import org.jetbrains.kotlin.psi2ir.builders.irBlock
import org.jetbrains.kotlin.psi2ir.builders.irGet
import org.jetbrains.kotlin.psi2ir.intermediate.*
import org.jetbrains.kotlin.resolve.BindingContext
@@ -32,7 +32,6 @@ import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPropertyDelegate
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi2ir.builders.irBlockBody
import org.jetbrains.kotlin.psi2ir.builders.irGet
import org.jetbrains.kotlin.psi2ir.builders.irReturn
import org.jetbrains.kotlin.psi2ir.intermediate.*
@@ -17,11 +17,16 @@
package org.jetbrains.kotlin.psi2ir.generators
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.expressions.IrBlockBody
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
import org.jetbrains.kotlin.ir.expressions.impl.IrErrorExpressionImpl
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi2ir.builders.*
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.types.KotlinType
@@ -30,12 +35,11 @@ import java.lang.AssertionError
import java.lang.RuntimeException
interface Generator {
val context: GeneratorContext
interface Generator : IrGenerator {
override val context: GeneratorContext
}
interface GeneratorWithScope : Generator {
val scope: Scope
interface GeneratorWithScope : Generator, IrGeneratorWithScope {
}
@@ -58,4 +62,17 @@ fun Generator.getResolvedCall(key: KtElement): ResolvedCall<out CallableDescript
key.getResolvedCall(context.bindingContext)
fun Generator.createDummyExpression(ktExpression: KtExpression, description: String): IrErrorExpressionImpl =
IrErrorExpressionImpl(ktExpression.startOffset, ktExpression.endOffset, getInferredTypeWithImplicitCastsOrFail(ktExpression), description)
IrErrorExpressionImpl(ktExpression.startOffset, ktExpression.endOffset, getInferredTypeWithImplicitCastsOrFail(ktExpression), description)
inline fun GeneratorWithScope.irBlock(ktElement: KtElement?,
origin: IrStatementOrigin? = null, resultType: KotlinType? = null,
body: IrBlockBuilder.() -> Unit
): IrExpression =
this.irBlock(ktElement?.startOffset ?: UNDEFINED_OFFSET,
ktElement?.endOffset ?: UNDEFINED_OFFSET,
origin, resultType, body)
inline fun GeneratorWithScope.irBlockBody(ktElement: KtElement?, body: IrBlockBodyBuilder.() -> Unit) : IrBlockBody =
this.irBlockBody(ktElement?.startOffset ?: UNDEFINED_OFFSET,
ktElement?.endOffset ?: UNDEFINED_OFFSET,
body)
@@ -16,23 +16,21 @@
package org.jetbrains.kotlin.psi2ir.generators
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.psi2ir.builders.IrGeneratorContext
import org.jetbrains.kotlin.resolve.BindingContext
class GeneratorContext(
val configuration: Psi2IrConfiguration,
val moduleDescriptor: ModuleDescriptor,
val bindingContext: BindingContext
) {
) : IrGeneratorContext(IrBuiltIns(moduleDescriptor.builtIns)) {
val sourceManager = PsiSourceManager()
val reflectionTypes = ReflectionTypes(moduleDescriptor)
val builtIns: KotlinBuiltIns get() = moduleDescriptor.builtIns
val irBuiltIns = IrBuiltIns(builtIns)
}
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.psi2ir.builders.IrGeneratorContext
fun primitiveOp1(startOffset: Int, endOffset: Int, primitiveOpDescriptor: CallableDescriptor, origin: IrStatementOrigin,
argument: IrExpression): IrExpression =
@@ -29,36 +30,36 @@ fun primitiveOp2(startOffset: Int, endOffset: Int, primitiveOpDescriptor: Callab
argument1: IrExpression, argument2: IrExpression): IrExpression =
IrBinaryPrimitiveImpl(startOffset, endOffset, origin, primitiveOpDescriptor, argument1, argument2)
fun GeneratorContext.constNull(startOffset: Int, endOffset: Int): IrExpression =
fun IrGeneratorContext.constNull(startOffset: Int, endOffset: Int): IrExpression =
IrConstImpl.constNull(startOffset, endOffset, builtIns.nullableNothingType)
fun GeneratorContext.equalsNull(startOffset: Int, endOffset: Int, argument: IrExpression): IrExpression =
fun IrGeneratorContext.equalsNull(startOffset: Int, endOffset: Int, argument: IrExpression): IrExpression =
primitiveOp2(startOffset, endOffset, irBuiltIns.eqeq, IrStatementOrigin.EQEQ,
argument, constNull(startOffset, endOffset))
fun GeneratorContext.eqeqeq(startOffset: Int, endOffset: Int, argument1: IrExpression, argument2: IrExpression): IrExpression =
fun IrGeneratorContext.eqeqeq(startOffset: Int, endOffset: Int, argument1: IrExpression, argument2: IrExpression): IrExpression =
primitiveOp2(startOffset, endOffset, irBuiltIns.eqeqeq, IrStatementOrigin.EQEQEQ, argument1, argument2)
fun GeneratorContext.throwNpe(startOffset: Int, endOffset: Int, origin: IrStatementOrigin): IrExpression =
fun IrGeneratorContext.throwNpe(startOffset: Int, endOffset: Int, origin: IrStatementOrigin): IrExpression =
IrNullaryPrimitiveImpl(startOffset, endOffset, origin, irBuiltIns.throwNpe)
// a || b == if (a) true else b
fun GeneratorContext.oror(startOffset: Int, endOffset: Int, a: IrExpression, b: IrExpression, origin: IrStatementOrigin = IrStatementOrigin.OROR): IrWhen =
fun IrGeneratorContext.oror(startOffset: Int, endOffset: Int, a: IrExpression, b: IrExpression, origin: IrStatementOrigin = IrStatementOrigin.OROR): IrWhen =
IrIfThenElseImpl(startOffset, endOffset, builtIns.booleanType,
a, IrConstImpl.constTrue(b.startOffset, b.endOffset, b.type), b,
origin)
fun GeneratorContext.oror(a: IrExpression, b: IrExpression, origin: IrStatementOrigin = IrStatementOrigin.OROR): IrWhen =
fun IrGeneratorContext.oror(a: IrExpression, b: IrExpression, origin: IrStatementOrigin = IrStatementOrigin.OROR): IrWhen =
oror(b.startOffset, b.endOffset, a, b, origin)
fun GeneratorContext.whenComma(a: IrExpression, b: IrExpression): IrWhen =
fun IrGeneratorContext.whenComma(a: IrExpression, b: IrExpression): IrWhen =
oror(a, b, IrStatementOrigin.WHEN_COMMA)
// a && b == if (a) b else false
fun GeneratorContext.andand(startOffset: Int, endOffset: Int, a: IrExpression, b: IrExpression, origin: IrStatementOrigin = IrStatementOrigin.ANDAND): IrWhen =
fun IrGeneratorContext.andand(startOffset: Int, endOffset: Int, a: IrExpression, b: IrExpression, origin: IrStatementOrigin = IrStatementOrigin.ANDAND): IrWhen =
IrIfThenElseImpl(startOffset, endOffset, builtIns.booleanType,
a, b, IrConstImpl.constFalse(b.startOffset, b.endOffset, b.type),
origin)
fun GeneratorContext.andand(a: IrExpression, b: IrExpression, origin: IrStatementOrigin = IrStatementOrigin.ANDAND): IrWhen =
fun IrGeneratorContext.andand(a: IrExpression, b: IrExpression, origin: IrStatementOrigin = IrStatementOrigin.ANDAND): IrWhen =
andand(b.startOffset, b.endOffset, a, b, origin)