backend: untie IR builders from psi2ir generators

This commit is contained in:
Svyatoslav Scherbina
2016-12-23 14:27:02 +07:00
committed by SvyatoslavScherbina
parent fa95d4d2a8
commit 9054a5eb5e
3 changed files with 46 additions and 21 deletions
@@ -0,0 +1,35 @@
/*
* 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.ir.builders
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
// TODO: rename Generator* to IrGenerator* when contributing back to Kotlin.
interface Generator {
val context: GeneratorContext
}
interface GeneratorWithScope : Generator {
val scope: Scope
}
interface GeneratorContext {
val irBuiltIns: IrBuiltIns
val builtIns: KotlinBuiltIns get() = irBuiltIns.builtIns
}
@@ -16,19 +16,12 @@
package org.jetbrains.kotlin.ir.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.types.KotlinType
import java.util.*
@@ -50,7 +43,7 @@ abstract class IrStatementsBuilder<out T : IrElement>(
scope: Scope,
startOffset: Int,
endOffset: Int
) : IrBuilderWithScope(context, scope, startOffset, endOffset), GeneratorWithScope {
) : IrBuilderWithScope(context, scope, startOffset, endOffset) {
operator fun IrStatement.unaryPlus() {
addStatement(this)
}
@@ -114,23 +107,21 @@ 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,
inline fun GeneratorWithScope.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 GeneratorWithScope.irBlockBody(startOffset: Int = UNDEFINED_OFFSET, endOffset: Int = UNDEFINED_OFFSET,
body: IrBlockBodyBuilder.() -> Unit
) : IrBlockBody =
IrBlockBodyBuilder(context, scope,
ktElement?.startOffset ?: UNDEFINED_OFFSET,
ktElement?.endOffset ?: UNDEFINED_OFFSET
startOffset,
endOffset
).blockBody(body)
@@ -21,7 +21,6 @@ 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
class IrMemberFunctionBuilder(
context: GeneratorContext,