IR elements fixes & refactorings

This commit is contained in:
Dmitry Petrov
2016-08-05 15:41:07 +03:00
committed by Dmitry Petrov
parent 7879fb7084
commit 64abecf996
27 changed files with 250 additions and 228 deletions
@@ -41,7 +41,8 @@ abstract class IrDeclarationGeneratorBase(
// TODO create IrAnnotation's for each KtAnnotationEntry
}
fun generateMemberDeclaration(ktDeclaration: KtDeclaration, containingDeclaration: IrCompoundDeclaration) {
fun generateMemberDeclaration(ktDeclaration: KtDeclaration, containingDeclaration: IrCompoundDeclarationBase) {
// TODO visitor?
when (ktDeclaration) {
is KtNamedFunction ->
generateFunctionDeclaration(ktDeclaration, containingDeclaration)
@@ -56,47 +57,59 @@ abstract class IrDeclarationGeneratorBase(
private fun loc(ktElement: KtElement) = containingFile.getSourceLocationForElement(ktElement)
fun generateFunctionDeclaration(ktNamedFunction: KtNamedFunction, containingDeclaration: IrCompoundDeclaration) {
fun generateFunctionDeclaration(ktNamedFunction: KtNamedFunction, containingDeclaration: IrCompoundDeclarationBase) {
val sourceLocation = loc(ktNamedFunction)
val functionDescriptor = getOrFail(BindingContext.FUNCTION, ktNamedFunction) { "unresolved fun" }
val body = generateExpressionBody(ktNamedFunction.bodyExpression ?: TODO("function without body expression"))
val irFunction = IrFunctionImpl(sourceLocation, containingDeclaration, functionDescriptor, body)
containingDeclaration.addChildDeclaration(irFunction)
val irFunction = IrFunctionImpl(sourceLocation, functionDescriptor, body).apply { parent = containingDeclaration }
containingDeclaration.childDeclarations.add(irFunction)
}
fun generatePropertyDeclaration(ktProperty: KtProperty, containingDeclaration: IrCompoundDeclaration) {
fun generatePropertyDeclaration(ktProperty: KtProperty, containingDeclaration: IrCompoundDeclarationBase) {
val sourceLocation = loc(ktProperty)
val variableDescriptor = getOrFail(BindingContext.VARIABLE, ktProperty) { "unresolved property" }
val propertyDescriptor = variableDescriptor as? PropertyDescriptor ?: TODO("not a property?")
if (ktProperty.hasDelegate()) TODO("handle delegated property")
val initializer = ktProperty.initializer?.let { generateExpressionBody(it) }
val irProperty = IrSimplePropertyImpl(sourceLocation, containingDeclaration, propertyDescriptor, initializer)
containingDeclaration.addChildDeclaration(irProperty)
val irProperty = IrSimplePropertyImpl(sourceLocation, propertyDescriptor, initializer)
val irGetter: IrPropertyGetter? = ktProperty.getter?.let { ktGetter ->
val getterLocation = loc(ktGetter)
val accessorDescriptor = getOrFail(BindingContext.PROPERTY_ACCESSOR, ktGetter) { "unresolved getter" }
val getterDescriptor = accessorDescriptor as? PropertyGetterDescriptor ?: TODO("not a getter?")
val getterBody = generateExpressionBody(ktGetter.bodyExpression ?: TODO("default getter"))
IrPropertyGetterImpl(getterLocation, irProperty, getterDescriptor, getterBody)
IrPropertyGetterImpl(getterLocation, getterDescriptor, getterBody).apply {
parent = irProperty
getterBody.parent = this
}
}
val irSetter: IrPropertySetter? = ktProperty.setter?.let { ktSetter ->
val getterLocation = loc(ktSetter)
val accessorDescriptor = getOrFail(BindingContext.PROPERTY_ACCESSOR, ktSetter) { "unresolved setter" }
val setterDescriptor = accessorDescriptor as? PropertySetterDescriptor ?: TODO("not a setter?")
val getterBody = generateExpressionBody(ktSetter.bodyExpression ?: TODO("default setter"))
IrPropertySetterImpl(getterLocation, irProperty, setterDescriptor, getterBody)
val setterBody = generateExpressionBody(ktSetter.bodyExpression ?: TODO("default setter"))
IrPropertySetterImpl(getterLocation, setterDescriptor, setterBody).apply {
parent = irProperty
setterBody.parent = this
}
}
irProperty.initialize(irGetter, irSetter)
irProperty.apply {
parent = containingDeclaration
getter = irGetter
setter = irSetter
}
containingDeclaration.childDeclarations.add(irProperty)
}
fun generateExpressionBody(ktBody: KtExpression): IrBody {
fun generateExpressionBody(ktBody: KtExpression): IrBodyBase {
val sourceLocation = loc(ktBody)
val bodyExpression = irExpressionGenerator.generateExpression(ktBody)
return if (ktBody is KtBlockExpression)
bodyExpression
else
IrReturnExpressionImpl(sourceLocation, bodyExpression.type, bodyExpression)
val irExpression = irExpressionGenerator.generateExpression(ktBody)
val bodyExpression =
if (ktBody is KtBlockExpression)
irExpression
else
IrReturnExpressionImpl(sourceLocation, irExpression.type, irExpression).apply {
irExpression.parent = this
}
return IrExpressionBodyImpl(sourceLocation,bodyExpression)
}
}
@@ -26,11 +26,11 @@ import java.util.*
class IrElementFactory(val irModule: IrModule, val sourceManager: PsiSourceManager) {
val ktFileToIrFile = LinkedHashMap<KtFile, IrFile>()
fun createIrFile(ktFile: KtFile, descriptor: PackageFragmentDescriptor): IrFile {
fun createIrFile(ktFile: KtFile, descriptor: PackageFragmentDescriptor): IrFileImpl {
val fileEntry = sourceManager.getOrCreateFileEntry(ktFile)
val irFile = IrFileImpl(fileEntry.getRootSourceLocation(), irModule, fileEntry.getRecognizableName(), fileEntry, descriptor)
val irFile = IrFileImpl(fileEntry.getRootSourceLocation(), fileEntry.getRecognizableName(), fileEntry, descriptor)
irFile.parent = irModule
ktFileToIrFile.put(ktFile, irFile)
return irFile
}
}
@@ -25,27 +25,27 @@ import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluat
class IrExpressionGenerator(
override val context: IrGeneratorContext,
val containingFile: PsiSourceManager.PsiFileEntry
) : KtVisitor<IrExpression, Nothing?>(), IrGenerator {
) : KtVisitor<IrExpressionBase, Nothing?>(), IrGenerator {
fun generateExpression(ktExpression: KtExpression) = ktExpression.irExpr()
private fun KtElement.irExpr(): IrExpression = accept(this@IrExpressionGenerator, null)
private fun KtElement.irExpr(): IrExpressionBase = accept(this@IrExpressionGenerator, null)
private fun KtElement.loc() = this@IrExpressionGenerator.containingFile.getSourceLocationForElement(this)
private fun KtExpression.type() = getType(this) ?: TODO("no type for expression")
override fun visitExpression(expression: KtExpression, data: Nothing?): IrExpression =
override fun visitExpression(expression: KtExpression, data: Nothing?): IrExpressionBase =
IrDummyExpression(expression.loc(), expression.type())
override fun visitBlockExpression(expression: KtBlockExpression, data: Nothing?): IrExpression {
override fun visitBlockExpression(expression: KtBlockExpression, data: Nothing?): IrExpressionBase {
val irBlock = IrBlockExpressionImpl(expression.loc(), expression.type())
expression.statements.forEach { irBlock.addChildExpression(it.irExpr()) }
expression.statements.forEach { irBlock.childExpressions.add(it.irExpr()) }
return irBlock
}
override fun visitReturnExpression(expression: KtReturnExpression, data: Nothing?): IrExpression =
override fun visitReturnExpression(expression: KtReturnExpression, data: Nothing?): IrExpressionBase =
IrReturnExpressionImpl(expression.loc(), expression.type(),
expression.returnedExpression?.irExpr())
override fun visitConstantExpression(expression: KtConstantExpression, data: Nothing?): IrExpression {
override fun visitConstantExpression(expression: KtConstantExpression, data: Nothing?): IrExpressionBase {
val compileTimeConstant = ConstantExpressionEvaluator.getConstant(expression, context.bindingContext)
?: error("KtConstantExpression was not evaluated: ${expression.text}")
val constantValue = compileTimeConstant.toConstantValue(expression.type())
@@ -53,24 +53,24 @@ class IrExpressionGenerator(
return when (constantValue) {
is StringValue ->
IrStringLiteralImpl(expression.loc(), constantType, constantValue.value)
IrLiteralExpressionImpl.string(expression.loc(), constantType, constantValue.value)
is IntValue ->
IrIntLiteralImpl(expression.loc(), constantType, constantValue.value)
IrLiteralExpressionImpl.int(expression.loc(), constantType, constantValue.value)
else ->
TODO("handle other literal types: ${constantValue.type}")
}
}
override fun visitStringTemplateExpression(expression: KtStringTemplateExpression, data: Nothing?): IrExpression {
override fun visitStringTemplateExpression(expression: KtStringTemplateExpression, data: Nothing?): IrExpressionBase {
if (expression.entries.size == 1 && expression.entries[0] is KtLiteralStringTemplateEntry) {
return expression.entries[0].irExpr()
}
val irStringTemplate = IrStringTemplateExpressionImpl(expression.loc(), expression.type())
expression.entries.forEach { irStringTemplate.addStringTemplateEntry(it.irExpr()) }
val irStringTemplate = IrStringConcatenationExpressionImpl(expression.loc(), expression.type())
expression.entries.forEach { irStringTemplate.entries.add(it.irExpr()) }
return irStringTemplate
}
override fun visitLiteralStringTemplateEntry(entry: KtLiteralStringTemplateEntry, data: Nothing?): IrExpression =
IrStringLiteralImpl(entry.loc(), context.builtIns.stringType, entry.text)
override fun visitLiteralStringTemplateEntry(entry: KtLiteralStringTemplateEntry, data: Nothing?): IrExpressionBase =
IrLiteralExpressionImpl.string(entry.loc(), context.builtIns.stringType, entry.text)
}
@@ -16,13 +16,13 @@
package org.jetbrains.kotlin.psi2ir
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrFileImpl
import org.jetbrains.kotlin.psi.KtFile
class IrFileGenerator(
context: IrGeneratorContext,
val ktFile: KtFile,
override val irDeclaration: IrFile,
override val irDeclaration: IrFileImpl,
override val parent: IrModuleGenerator
) : IrDeclarationGeneratorBase(context, irDeclaration, parent, irDeclaration.fileEntry as PsiSourceManager.PsiFileEntry) {
fun generateFileContent() {
@@ -18,13 +18,13 @@ package org.jetbrains.kotlin.psi2ir
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.ir.declarations.IrModule
import org.jetbrains.kotlin.ir.declarations.IrModuleImpl
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.BindingContext
class IrGeneratorContext(
val inputFiles: List<KtFile>,
val irModule: IrModule,
val irModule: IrModuleImpl,
val bindingContext: BindingContext
) {
val moduleDescriptor: ModuleDescriptor get() = irModule.descriptor
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.psi2ir
import org.jetbrains.kotlin.ir.declarations.IrModule
import org.jetbrains.kotlin.ir.declarations.IrModuleImpl
import org.jetbrains.kotlin.resolve.BindingContext
class IrModuleGenerator(override val context: IrGeneratorContext) : IrDeclarationGenerator {
@@ -27,7 +28,7 @@ class IrModuleGenerator(override val context: IrGeneratorContext) : IrDeclaratio
for (ktFile in context.inputFiles) {
val packageFragmentDescriptor = getOrFail(BindingContext.FILE_TO_PACKAGE_FRAGMENT, ktFile) { "no package fragment for file" }
val irFile = context.irElementFactory.createIrFile(ktFile, packageFragmentDescriptor)
irDeclaration.addFile(irFile)
context.irModule.files.add(irFile)
val generator = IrFileGenerator(context, ktFile, irFile, this)
generator.generateFileContent()
}
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.ir
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
interface IrElement {
val parent: IrElement?
val sourceLocation: SourceLocation
fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R
fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D): Unit
@@ -16,8 +16,6 @@
package org.jetbrains.kotlin.ir
import org.jetbrains.kotlin.ir.SourceLocation
interface SourceLocationManager {
interface FileEntry {
fun getLineNumber(sourceLocation: SourceLocation): Int
@@ -0,0 +1,47 @@
/*
* 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.declarations
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrElementBase
import org.jetbrains.kotlin.ir.SourceLocation
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
interface IrBody : IrElement {
override val parent: IrDeclaration
}
interface IrExpressionBody : IrBody {
val expression: IrExpression
}
abstract class IrBodyBase(sourceLocation: SourceLocation): IrElementBase(sourceLocation), IrBody {
override lateinit var parent: IrDeclaration
}
class IrExpressionBodyImpl(
sourceLocation: SourceLocation,
override val expression: IrExpression
) : IrBodyBase(sourceLocation), IrExpressionBody {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitExpressionBody(this, data)
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
expression.accept(visitor, data)
}
}
@@ -26,9 +26,8 @@ interface IrClass : IrCompoundDeclaration {
class IrClassImpl(
sourceLocation: SourceLocation,
containingDeclaration: IrDeclaration,
override val descriptor: ClassDescriptor
) : IrCompoundDeclarationBase(sourceLocation, containingDeclaration), IrClass {
) : IrCompoundDeclarationBase(sourceLocation), IrClass {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitClass(this, data)
}
@@ -25,39 +25,33 @@ import java.util.*
interface IrDeclaration : IrElement {
val descriptor: DeclarationDescriptor
val containingDeclaration: IrDeclaration?
override val parent: IrDeclaration?
}
interface IrCompoundDeclaration : IrDeclaration {
val childDeclarations: List<IrDeclaration>
fun addChildDeclaration(child: IrDeclaration)
}
interface IrDeclarationNonRoot : IrDeclaration {
override val containingDeclaration: IrDeclaration
override val parent: IrDeclaration
}
abstract class IrDeclarationBase(
sourceLocation: SourceLocation,
override val containingDeclaration: IrDeclaration?
) : IrElementBase(sourceLocation), IrDeclaration
abstract class IrDeclarationBase(sourceLocation: SourceLocation) : IrElementBase(sourceLocation), IrDeclaration {
override var parent: IrDeclaration? = null
}
abstract class IrDeclarationNonRootBase(
sourceLocation: SourceLocation,
override val containingDeclaration: IrDeclaration
) : IrElementBase(sourceLocation), IrDeclarationNonRoot
abstract class IrDeclarationNonRootBase(sourceLocation: SourceLocation) : IrElementBase(sourceLocation), IrDeclarationNonRoot
abstract class IrCompoundDeclarationBase(
sourceLocation: SourceLocation,
containingDeclaration: IrDeclaration?
) : IrDeclarationBase(sourceLocation, containingDeclaration), IrCompoundDeclaration {
abstract class IrCompoundDeclarationBase(sourceLocation: SourceLocation) : IrDeclarationBase(sourceLocation), IrCompoundDeclaration {
override val childDeclarations: MutableList<IrDeclaration> = ArrayList()
override fun addChildDeclaration(child: IrDeclaration) {
childDeclarations.add(child)
}
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
childDeclarations.forEach { it.accept(visitor, data) }
}
}
val IrDeclaration.containingDeclaration: IrDeclaration?
get() = parent
val IrDeclarationNonRoot.continingDeclaration: IrDeclaration
get() = parent
@@ -29,11 +29,10 @@ interface IrFile : IrCompoundDeclaration {
class IrFileImpl(
sourceLocation: SourceLocation,
containingDeclaration: IrModule,
override val name: String,
override val fileEntry: SourceLocationManager.FileEntry,
override val descriptor: PackageFragmentDescriptor
) : IrCompoundDeclarationBase(sourceLocation, containingDeclaration), IrFile {
) : IrCompoundDeclarationBase(sourceLocation), IrFile {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitFile(this, data)
}
@@ -18,20 +18,14 @@ package org.jetbrains.kotlin.ir.declarations
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.ir.SourceLocation
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
interface IrBody : IrElement
interface IrFunction : IrDeclarationNonRoot {
override val descriptor: FunctionDescriptor
val body: IrBody
}
abstract class IrFunctionBase(
sourceLocation: SourceLocation,
containingDeclaration: IrDeclaration
) : IrDeclarationNonRootBase(sourceLocation, containingDeclaration), IrFunction {
abstract class IrFunctionBase(sourceLocation: SourceLocation) : IrDeclarationNonRootBase(sourceLocation), IrFunction {
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
body.accept(visitor, data)
}
@@ -39,10 +33,11 @@ abstract class IrFunctionBase(
class IrFunctionImpl(
sourceLocation: SourceLocation,
containingDeclaration: IrDeclaration,
override val descriptor: FunctionDescriptor,
override val body: IrBody
) : IrFunctionBase(sourceLocation, containingDeclaration) {
) : IrFunctionBase(sourceLocation) {
override lateinit var parent: IrDeclaration
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitFunction(this, data)
}
@@ -25,22 +25,12 @@ interface IrModule : IrCompoundDeclaration {
override val descriptor: ModuleDescriptor
val files: List<IrFile>
fun addFile(file: IrFile)
}
class IrModuleImpl(override val descriptor: ModuleDescriptor) : IrDeclarationBase(NO_LOCATION, null), IrModule {
class IrModuleImpl(override val descriptor: ModuleDescriptor) : IrDeclarationBase(NO_LOCATION), IrModule {
override val files: MutableList<IrFile> = ArrayList()
override val childDeclarations: List<IrDeclaration> get() = files
override fun addFile(file: IrFile) {
files.add(file)
}
override fun addChildDeclaration(child: IrDeclaration) {
if (child !is IrFile) throw IllegalArgumentException("Only IrFile can be contained in IrModule, got $child")
addFile(child)
}
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitModule(this, data)
@@ -36,9 +36,9 @@ interface IrDelegatedProperty : IrProperty {
abstract class IrPropertyBase(
sourceLocation: SourceLocation,
containingDeclaration: IrDeclaration,
override val descriptor: PropertyDescriptor
) : IrDeclarationNonRootBase(sourceLocation, containingDeclaration), IrProperty {
) : IrDeclarationNonRootBase(sourceLocation), IrProperty {
override lateinit var parent: IrDeclaration
override var getter: IrPropertyGetter? = null
override var setter: IrPropertySetter? = null
@@ -55,10 +55,9 @@ abstract class IrPropertyBase(
class IrSimplePropertyImpl(
sourceLocation: SourceLocation,
containingDeclaration: IrDeclaration,
descriptor: PropertyDescriptor,
override val valueInitializer: IrBody?
) : IrPropertyBase(sourceLocation, containingDeclaration, descriptor), IrSimpleProperty {
) : IrPropertyBase(sourceLocation, descriptor), IrSimpleProperty {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitSimpleProperty(this, data)
@@ -70,10 +69,9 @@ class IrSimplePropertyImpl(
class IrDelegatedPropertyImpl(
sourceLocation: SourceLocation,
containingDeclaration: IrDeclaration,
descriptor: PropertyDescriptor,
override val delegateInitializer: IrBody
) : IrPropertyBase(sourceLocation, containingDeclaration, descriptor), IrDelegatedProperty {
) : IrPropertyBase(sourceLocation, descriptor), IrDelegatedProperty {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitDelegatedProperty(this, data)
@@ -24,7 +24,7 @@ import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
interface IrPropertyAccessor : IrFunction {
override val descriptor: PropertyAccessorDescriptor
val property: IrProperty get() = containingDeclaration as IrProperty
override val parent: IrProperty
}
interface IrPropertyGetter : IrPropertyAccessor {
@@ -37,26 +37,25 @@ interface IrPropertySetter : IrPropertyAccessor {
abstract class IrPropertyAccessorBase(
sourceLocation: SourceLocation,
containingDeclaration: IrProperty,
override val body: IrBody
) : IrFunctionBase(sourceLocation, containingDeclaration), IrPropertyAccessor
) : IrFunctionBase(sourceLocation), IrPropertyAccessor {
override lateinit var parent: IrProperty
}
class IrPropertyGetterImpl(
sourceLocation: SourceLocation,
containingDeclaration: IrProperty,
override val descriptor: PropertyGetterDescriptor,
body: IrBody
) : IrPropertyAccessorBase(sourceLocation, containingDeclaration, body), IrPropertyGetter {
) : IrPropertyAccessorBase(sourceLocation, body), IrPropertyGetter {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitPropertyGetter(this, data)
}
class IrPropertySetterImpl(
sourceLocation: SourceLocation,
containingDeclaration: IrProperty,
override val descriptor: PropertySetterDescriptor,
body: IrBody
) : IrPropertyAccessorBase(sourceLocation, containingDeclaration, body), IrPropertySetter {
) : IrPropertyAccessorBase(sourceLocation, body), IrPropertySetter {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitPropertySetter(this, data)
}
@@ -24,7 +24,6 @@ import java.util.*
interface IrBlockExpression : IrExpression {
val childExpressions: List<IrExpression>
fun addChildExpression(childExpression: IrExpression)
}
class IrBlockExpressionImpl(
@@ -33,10 +32,6 @@ class IrBlockExpressionImpl(
) : IrExpressionBase(sourceLocation, type), IrBlockExpression {
override val childExpressions: MutableList<IrExpression> = ArrayList()
override fun addChildExpression(childExpression: IrExpression) {
childExpressions.add(childExpression)
}
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitBlockExpression(this, data)
@@ -17,11 +17,13 @@
package org.jetbrains.kotlin.ir.expressions
import org.jetbrains.kotlin.ir.SourceLocation
import org.jetbrains.kotlin.ir.declarations.IrBody
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.types.KotlinType
class IrDummyExpression(sourceLocation: SourceLocation, type: KotlinType) : IrExpressionBase(sourceLocation, type) {
class IrDummyExpression(
sourceLocation: SourceLocation,
type: KotlinType
) : IrExpressionBase(sourceLocation, type) {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitElement(this, data)
@@ -19,15 +19,16 @@ package org.jetbrains.kotlin.ir.expressions
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrElementBase
import org.jetbrains.kotlin.ir.SourceLocation
import org.jetbrains.kotlin.ir.declarations.IrBody
import org.jetbrains.kotlin.types.KotlinType
interface IrExpression : IrElement, IrBody {
interface IrExpression : IrElement {
val type: KotlinType
}
abstract class IrExpressionBase(
sourceLocation: SourceLocation,
override val type: KotlinType
) : IrElementBase(sourceLocation), IrExpression
) : IrElementBase(sourceLocation), IrExpression {
override lateinit var parent: IrElement
}
@@ -1,80 +0,0 @@
/*
* 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.expressions
import org.jetbrains.kotlin.ir.SourceLocation
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.types.KotlinType
interface IrLiteral : IrExpression
interface IrNullLiteral : IrLiteral
interface IrTrueLiteral : IrLiteral
interface IrFalseLiteral : IrLiteral
interface IrIntLiteral : IrLiteral {
val value: Int
}
interface IrLongLiteral : IrLiteral {
val value: Long
}
interface IrFloatLiteral : IrLiteral {
val value: Float
}
interface IrDoubleLiteral : IrLiteral {
val value: Double
}
interface IrCharLiteral : IrLiteral {
val value: Char
}
interface IrStringLiteral : IrLiteral {
val value: String
}
abstract class IrLiteralBase(
sourceLocation: SourceLocation,
type: KotlinType
) : IrExpressionBase(sourceLocation, type), IrLiteral {
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
// No children
}
}
class IrIntLiteralImpl(
sourceLocation: SourceLocation,
type: KotlinType,
override val value: Int
) : IrLiteralBase(sourceLocation, type), IrIntLiteral {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitIntLiteral(this, data)
}
class IrStringLiteralImpl(
sourceLocation: SourceLocation,
type: KotlinType,
override val value: String
) : IrLiteralBase(sourceLocation, type), IrStringLiteral {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitStringLiteral(this, data)
}
@@ -0,0 +1,68 @@
/*
* 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.expressions
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.SourceLocation
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.types.KotlinType
interface IrLiteralExpression<out T> : IrExpression {
val kind: IrLiteralKind<T>
val value: T
}
sealed class IrLiteralKind<out T>(val asString: kotlin.String) {
@Suppress("UNCHECKED_CAST")
fun valueOf(literal: IrLiteralExpression<*>) =
(literal as IrLiteralExpression<T>).value
object Null : IrLiteralKind<Nothing?>("Null")
object Boolean : IrLiteralKind<kotlin.Boolean>("Boolean")
object Byte : IrLiteralKind<kotlin.Byte>("Byte")
object Short : IrLiteralKind<kotlin.Short>("Short")
object Int : IrLiteralKind<kotlin.Int>("Int")
object Long : IrLiteralKind<kotlin.Long>("Long")
object String : IrLiteralKind<kotlin.String>("String")
object Float : IrLiteralKind<kotlin.Float>("Float")
object Double : IrLiteralKind<kotlin.Double>("Double")
override fun toString() = asString
}
class IrLiteralExpressionImpl<out T> (
sourceLocation: SourceLocation,
type: KotlinType,
override val kind: IrLiteralKind<T>,
override val value: T
) : IrExpressionBase(sourceLocation, type), IrLiteralExpression<T> {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitLiteral(this, data)
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
// No children
}
companion object {
fun string(sourceLocation: SourceLocation, type: KotlinType, value: String): IrLiteralExpressionImpl<String> =
IrLiteralExpressionImpl(sourceLocation, type, IrLiteralKind.String, value)
fun int(sourceLocation: SourceLocation, type: KotlinType, value: Int): IrLiteralExpressionImpl<Int> =
IrLiteralExpressionImpl(sourceLocation, type, IrLiteralKind.Int, value)
}
}
@@ -16,26 +16,22 @@
package org.jetbrains.kotlin.ir.expressions
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.SourceLocation
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.types.KotlinType
import java.util.*
interface IrStringTemplateExpression : IrExpression {
interface IrStringConcatenationExpression : IrExpression {
val entries: List<IrExpression>
fun addStringTemplateEntry(entry: IrExpression)
}
class IrStringTemplateExpressionImpl(
class IrStringConcatenationExpressionImpl(
sourceLocation: SourceLocation,
type: KotlinType
) : IrExpressionBase(sourceLocation, type), IrStringTemplateExpression {
) : IrExpressionBase(sourceLocation, type), IrStringConcatenationExpression {
override val entries: MutableList<IrExpression> = ArrayList()
override fun addStringTemplateEntry(entry: IrExpression) {
entries.add(entry)
}
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitStringTemplate(this, data)
@@ -47,14 +47,14 @@ class RenderIrElementVisitor : IrElementVisitor<String, Nothing?> {
override fun visitPropertySetter(declaration: IrPropertySetter, data: Nothing?): String =
"IrPropertySetter ${declaration.renderDescriptor()}"
override fun visitExpressionBody(body: IrExpressionBody, data: Nothing?): String =
"IrExpressionBody"
override fun visitExpression(expression: IrExpression, data: Nothing?): String =
"??? ${expression.javaClass.simpleName} type=${expression.renderType()}"
override fun visitStringLiteral(expression: IrStringLiteral, data: Nothing?): String =
"IrStringLiteral ${expression.value}"
override fun visitIntLiteral(expression: IrIntLiteral, data: Nothing?): String =
"IrIntLiteral type=${expression.renderType()} value=${expression.value}"
override fun <T> visitLiteral(expression: IrLiteralExpression<T>, data: Nothing?): String =
"IrLiteral ${expression.kind} type=${expression.renderType()} value='${expression.value}'"
override fun visitBlockExpression(expression: IrBlockExpression, data: Nothing?): String =
"IrBlockExpression type=${expression.renderType()}"
@@ -35,20 +35,12 @@ interface IrElementVisitor<out R, in D> {
fun visitSimpleProperty(declaration: IrSimpleProperty, data: D): R = visitProperty(declaration, data)
fun visitDelegatedProperty(declaration: IrDelegatedProperty, data: D): R = visitProperty(declaration, data)
fun visitBody(body: IrBody, data: D): R = visitElement(body, data)
fun visitExpressionBody(body: IrExpressionBody, data: D): R = visitBody(body, data)
fun visitExpression(expression: IrExpression, data: D): R = visitElement(expression, data)
fun visitLiteral(expression: IrLiteral, data: D): R = visitExpression(expression, data)
fun visitNullLiteral(expression: IrNullLiteral, data: D): R = visitLiteral(expression, data)
fun visitTrueLiteral(expression: IrTrueLiteral, data: D): R = visitLiteral(expression, data)
fun visitFalseLiteral(expression: IrFalseLiteral, data: D): R = visitLiteral(expression, data)
fun visitIntLiteral(expression: IrIntLiteral, data: D): R = visitLiteral(expression, data)
fun visitLongLiteral(expression: IrLongLiteral, data: D): R = visitLiteral(expression, data)
fun visitFloatLiteral(expression: IrFloatLiteral, data: D): R = visitLiteral(expression, data)
fun visitDoubleLiteral(expression: IrDoubleLiteral, data: D): R = visitLiteral(expression, data)
fun visitCharLiteral(expression: IrCharLiteral, data: D): R = visitLiteral(expression, data)
fun visitStringLiteral(expression: IrStringLiteral, data: D): R = visitLiteral(expression, data)
fun <T> visitLiteral(expression: IrLiteralExpression<T>, data: D): R = visitExpression(expression, data)
fun visitReturnExpression(expression: IrReturnExpression, data: D): R = visitExpression(expression, data)
fun visitBlockExpression(expression: IrBlockExpression, data: D): R = visitExpression(expression, data)
fun visitStringTemplate(expression: IrStringTemplateExpression, data: D) = visitExpression(expression, data)
fun visitStringTemplate(expression: IrStringConcatenationExpression, data: D) = visitExpression(expression, data)
}
+3 -1
View File
@@ -1,3 +1,5 @@
IrFile /boxOk.kt
IrFunction public fun box(): kotlin.String
??? IrDummyBody
IrExpressionBody
IrReturnExpression type=kotlin.String
IrLiteral String type=kotlin.String value='OK'
+18 -12
View File
@@ -1,21 +1,27 @@
IrFile /smoke.kt
IrFunction public fun testFun(): kotlin.String
IrBlockExpression type=kotlin.Nothing
IrReturnExpression type=kotlin.Nothing
IrStringLiteral OK
IrExpressionBody
IrBlockExpression type=kotlin.Nothing
IrReturnExpression type=kotlin.Nothing
IrLiteral String type=kotlin.String value='OK'
IrProperty public val testSimpleVal: kotlin.Int = 1
IrReturnExpression type=kotlin.Int
IrIntLiteral type=kotlin.Int value=1
IrExpressionBody
IrReturnExpression type=kotlin.Int
IrLiteral Int type=kotlin.Int value='1'
IrProperty public val testValWithGetter: kotlin.Int
IrPropertyGetter public fun <get-testValWithGetter>(): kotlin.Int
IrReturnExpression type=kotlin.Int
IrIntLiteral type=kotlin.Int value=42
IrExpressionBody
IrReturnExpression type=kotlin.Int
IrLiteral Int type=kotlin.Int value='42'
IrProperty public var testSimpleVar: kotlin.Int
IrReturnExpression type=kotlin.Int
IrIntLiteral type=kotlin.Int value=2
IrExpressionBody
IrReturnExpression type=kotlin.Int
IrLiteral Int type=kotlin.Int value='2'
IrProperty public var testVarWithAccessors: kotlin.Int
IrPropertyGetter public fun <get-testVarWithAccessors>(): kotlin.Int
IrReturnExpression type=kotlin.Int
IrIntLiteral type=kotlin.Int value=42
IrExpressionBody
IrReturnExpression type=kotlin.Int
IrLiteral Int type=kotlin.Int value='42'
IrPropertySetter public fun <set-testVarWithAccessors>(/*0*/ v: kotlin.Int): kotlin.Unit
IrBlockExpression type=kotlin.Unit
IrExpressionBody
IrBlockExpression type=kotlin.Unit
@@ -35,6 +35,12 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/ir/irText"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("boxOk.kt")
public void testBoxOk() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/boxOk.kt");
doTest(fileName);
}
@TestMetadata("smoke.kt")
public void testSmoke() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/smoke.kt");