Add symbol & IR builders to JS backend

This commit is contained in:
Roman Artemev
2018-04-18 22:26:45 +03:00
parent c1a4aeb67f
commit 09c54f1da3
2 changed files with 167 additions and 0 deletions
@@ -0,0 +1,110 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.ir.backend.js.descriptors
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.*
import org.jetbrains.kotlin.ir.descriptors.IrTemporaryVariableDescriptorImpl
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrVariableSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.createFunctionSymbol
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.KotlinType
object JsSymbolBuilder {
fun buildValueParameter(
containingSymbol: IrSimpleFunctionSymbol,
index: Int,
type: KotlinType,
name: String? = null,
hasDefault: Boolean = false
) =
IrValueParameterSymbolImpl(
ValueParameterDescriptorImpl(
containingSymbol.descriptor,
null,
index,
Annotations.EMPTY,
Name.identifier(name ?: "param$index"),
type,
hasDefault,
false,
false,
null,
SourceElement.NO_SOURCE
)
)
fun buildSimpleFunction(
containingDeclaration: DeclarationDescriptor,
name: String,
annotations: Annotations = Annotations.EMPTY,
kind: CallableMemberDescriptor.Kind = CallableMemberDescriptor.Kind.SYNTHESIZED,
source: SourceElement = SourceElement.NO_SOURCE
) = IrSimpleFunctionSymbolImpl(
SimpleFunctionDescriptorImpl.create(
containingDeclaration,
annotations,
Name.identifier(name),
kind,
source
)
)
fun copyFunctionSymbol(symbol: IrFunctionSymbol, newName: String) = IrSimpleFunctionSymbolImpl(
SimpleFunctionDescriptorImpl.create(
symbol.descriptor.containingDeclaration,
symbol.descriptor.annotations,
Name.identifier(newName),
symbol.descriptor.kind,
symbol.descriptor.source
)
)
fun buildVar(
containingSymbol: IrFunctionSymbol,
name: String,
type: KotlinType,
mutable: Boolean = false
) = IrVariableSymbolImpl(
LocalVariableDescriptor(
containingSymbol.descriptor,
Annotations.EMPTY,
Name.identifier(name),
type,
mutable,
false,
SourceElement.NO_SOURCE
)
)
}
fun <T : TypeParameterDescriptor> IrSimpleFunctionSymbol.initialize(
receiverParameterType: KotlinType? = null,
dispatchParameterDescriptor: ReceiverParameterDescriptor? = null,
typeParameters: List<T> = emptyList(),
valueParameters: List<ValueParameterDescriptor> = emptyList(),
type: KotlinType? = null,
modality: Modality = Modality.FINAL,
visibility: Visibility = Visibilities.LOCAL
) = this.apply {
(descriptor as FunctionDescriptorImpl).initialize(
receiverParameterType,
dispatchParameterDescriptor,
typeParameters,
valueParameters,
type,
modality,
visibility
)
}
@@ -0,0 +1,57 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.ir.backend.js.ir
import org.jetbrains.kotlin.backend.common.ir.Ir
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOriginImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrVariableImpl
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
import org.jetbrains.kotlin.types.KotlinType
object JsIrBuilder {
object SYNTHESIZED_STATEMENT : IrStatementOriginImpl("SYNTHESIZED_STATEMENT")
object SYNTHESIZED_DECLARATION : IrDeclarationOriginImpl("SYNTHESIZED_DECLARATION")
fun buildCall(target: IrFunctionSymbol, type: KotlinType? = null, typeArguments: Map<TypeParameterDescriptor, KotlinType>? = null) =
IrCallImpl(
UNDEFINED_OFFSET,
UNDEFINED_OFFSET,
type ?: target.descriptor.returnType!!,
target,
target.descriptor,
typeArguments,
SYNTHESIZED_STATEMENT
)
fun buildReturn(targetSymbol: IrFunctionSymbol, value: IrExpression) =
IrReturnImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, targetSymbol, value)
fun buildValueParameter(symbol: IrValueParameterSymbol) =
IrValueParameterImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, SYNTHESIZED_DECLARATION, symbol)
fun buildFunction(symbol: IrSimpleFunctionSymbol) = IrFunctionImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, SYNTHESIZED_DECLARATION, symbol)
fun buildGetValue(symbol: IrValueSymbol) = IrGetValueImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, symbol, SYNTHESIZED_STATEMENT)
fun buildBlockBody() = IrBlockBodyImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET)
fun buildBlockBody(stmts: List<IrStatement>) = IrBlockBodyImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, stmts)
fun buildFunctionReference(type: KotlinType, symbol: IrFunctionSymbol) =
IrFunctionReferenceImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, type, symbol, symbol.descriptor)
fun buildVar(symbol: IrVariableSymbol) = IrVariableImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, SYNTHESIZED_DECLARATION, symbol)
}