IR: dummy declarations for class/object and typealias

This commit is contained in:
Dmitry Petrov
2016-08-15 13:17:23 +03:00
committed by Dmitry Petrov
parent f903fb8ad3
commit 2707d33ca7
5 changed files with 67 additions and 15 deletions
@@ -45,19 +45,25 @@ abstract class IrDeclarationGeneratorBase(
fun generateMemberDeclaration(ktDeclaration: KtDeclaration) {
// TODO visitor?
when (ktDeclaration) {
is KtNamedFunction ->
generateFunctionDeclaration(ktDeclaration)
is KtProperty ->
generatePropertyDeclaration(ktDeclaration)
is KtClassOrObject -> {}
// TODO("classOrObject")
is KtTypeAlias -> {}
// TODO("typealias")
is KtNamedFunction -> generateFunctionDeclaration(ktDeclaration)
is KtProperty -> generatePropertyDeclaration(ktDeclaration)
is KtClassOrObject -> generateClassOrObjectDeclaration(ktDeclaration)
is KtTypeAlias -> generateTypeAliasDeclaration(ktDeclaration)
}
}
private fun generateClassOrObjectDeclaration(ktDeclaration: KtClassOrObject) {
IrDummyDeclaration(ktDeclaration.startOffset, ktDeclaration.endOffset, getOrFail(BindingContext.CLASS, ktDeclaration))
.register()
}
private fun generateTypeAliasDeclaration(ktDeclaration: KtTypeAlias) {
IrDummyDeclaration(ktDeclaration.startOffset, ktDeclaration.endOffset, getOrFail(BindingContext.TYPE_ALIAS, ktDeclaration))
.register()
}
fun generateFunctionDeclaration(ktNamedFunction: KtNamedFunction) {
val functionDescriptor = getOrFail(BindingContext.FUNCTION, ktNamedFunction) { "unresolved fun" }
val functionDescriptor = getOrFail(BindingContext.FUNCTION, ktNamedFunction)
val body = generateExpressionBody(functionDescriptor, ktNamedFunction.bodyExpression ?: TODO("function without body expression"))
declarationFactory.createFunction(ktNamedFunction, functionDescriptor, body).register()
}
@@ -68,13 +74,13 @@ abstract class IrDeclarationGeneratorBase(
val initializer = ktProperty.initializer?.let { generateExpressionBody(propertyDescriptor, it) }
val irProperty = declarationFactory.createSimpleProperty(ktProperty, propertyDescriptor, initializer).register()
ktProperty.getter?.let { ktGetter ->
val accessorDescriptor = getOrFail(BindingContext.PROPERTY_ACCESSOR, ktGetter) { "unresolved getter" }
val accessorDescriptor = getOrFail(BindingContext.PROPERTY_ACCESSOR, ktGetter)
val getterDescriptor = accessorDescriptor as? PropertyGetterDescriptor ?: TODO("not a getter?")
val getterBody = generateExpressionBody(getterDescriptor, ktGetter.bodyExpression ?: TODO("default getter"))
declarationFactory.createPropertyGetter(ktGetter, irProperty, getterDescriptor, getterBody).register()
}
ktProperty.setter?.let { ktSetter ->
val accessorDescriptor = getOrFail(BindingContext.PROPERTY_ACCESSOR, ktSetter) { "unresolved setter" }
val accessorDescriptor = getOrFail(BindingContext.PROPERTY_ACCESSOR, ktSetter)
val setterDescriptor = accessorDescriptor as? PropertySetterDescriptor ?: TODO("not a setter?")
val setterBody = generateExpressionBody(setterDescriptor, ktSetter.bodyExpression ?: TODO("default setter"))
declarationFactory.createPropertySetter(ktSetter, irProperty, setterDescriptor, setterBody).register()
@@ -82,7 +88,7 @@ abstract class IrDeclarationGeneratorBase(
}
private fun getPropertyDescriptor(ktProperty: KtProperty): PropertyDescriptor {
val variableDescriptor = getOrFail(BindingContext.VARIABLE, ktProperty) { "unresolved property" }
val variableDescriptor = getOrFail(BindingContext.VARIABLE, ktProperty)
val propertyDescriptor = variableDescriptor as? PropertyDescriptor ?: TODO("not a property?")
return propertyDescriptor
}
@@ -34,7 +34,8 @@ enum class IrDeclarationKind {
PROPERTY_SETTER,
PROPERTY,
VARIABLE,
CLASS
CLASS,
DUMMY;
}
enum class IrDeclarationOriginKind {
@@ -0,0 +1,42 @@
/*
* 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.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.throwNoSuchSlot
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
class IrDummyDeclaration(
startOffset: Int,
endOffset: Int,
override val descriptor: DeclarationDescriptor
) : IrDeclarationBase(startOffset, endOffset, IrDeclarationOriginKind.DEFINED) {
override val declarationKind: IrDeclarationKind get() = IrDeclarationKind.DUMMY
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R {
return visitor.visitDummyDeclaration(this, data)
}
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
// no children
}
override fun getChild(slot: Int): IrElement? = null
override fun replaceChild(slot: Int, newChild: IrElement) = throwNoSuchSlot(slot)
}
@@ -93,6 +93,9 @@ class RenderIrElementVisitor : IrElementVisitor<String, Nothing?> {
"SET_PROPERTY ${if (expression.isSafe) "?." else "."}${expression.descriptor.name}" +
"type=${expression.renderType()}"
override fun visitDummyDeclaration(declaration: IrDummyDeclaration, data: Nothing?): String =
"DUMMY ${declaration.descriptor.name}"
override fun visitDummyExpression(expression: IrDummyExpression, data: Nothing?): String =
"DUMMY ${expression.description} type=${expression.renderType()}"
@@ -35,6 +35,7 @@ interface IrElementVisitor<out R, in D> {
fun visitDelegatedProperty(declaration: IrDelegatedProperty, data: D): R = visitProperty(declaration, data)
fun visitLocalVariable(declaration: IrVariable, data: D) = visitDeclaration(declaration, data)
fun visitBody(body: IrBody, data: D): R = visitElement(body, data)
fun visitExpressionBody(body: IrExpressionBody, data: D): R = visitBody(body, data)
@@ -62,7 +63,6 @@ interface IrElementVisitor<out R, in D> {
fun visitTypeOperatorExpression(expression: IrTypeOperatorExpression, data: D) = visitExpression(expression, data)
// NB Use it only for testing purposes; will be removed as soon as all Kotlin expression types are covered
fun visitDummyDeclaration(declaration: IrDummyDeclaration, data: D) = visitDeclaration(declaration, data)
fun visitDummyExpression(expression: IrDummyExpression, data: D) = visitExpression(expression, data)
}