| /* |
| * Copyright 2020 The Android Open Source Project |
| * |
| * 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 androidx.compose.compiler.plugins.kotlin.lower |
| |
| import org.jetbrains.kotlin.backend.common.ir.createImplicitParameterDeclarationWithWrappedDescriptor |
| import org.jetbrains.kotlin.descriptors.ClassKind |
| import org.jetbrains.kotlin.descriptors.Modality |
| import org.jetbrains.kotlin.descriptors.ModuleDescriptor |
| import org.jetbrains.kotlin.descriptors.impl.EmptyPackageFragmentDescriptor |
| import org.jetbrains.kotlin.ir.builders.declarations.addConstructor |
| import org.jetbrains.kotlin.ir.builders.declarations.addFunction |
| import org.jetbrains.kotlin.ir.builders.declarations.addTypeParameter |
| import org.jetbrains.kotlin.ir.builders.declarations.addValueParameter |
| import org.jetbrains.kotlin.ir.builders.declarations.buildClass |
| import org.jetbrains.kotlin.ir.declarations.IrClass |
| import org.jetbrains.kotlin.ir.declarations.IrPackageFragment |
| import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl |
| import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns |
| import org.jetbrains.kotlin.ir.symbols.IrClassSymbol |
| import org.jetbrains.kotlin.ir.symbols.impl.IrExternalPackageFragmentSymbolImpl |
| import org.jetbrains.kotlin.ir.types.defaultType |
| import org.jetbrains.kotlin.name.FqName |
| import org.jetbrains.kotlin.types.Variance |
| |
| class FakeJvmSymbols(val module: ModuleDescriptor, val irBuiltIns: IrBuiltIns) { |
| |
| fun getJvmFunctionClass(parameterCount: Int): IrClassSymbol = |
| jvmFunctionClasses(parameterCount) |
| |
| private val jvmFunctionClasses = { n: Int -> |
| createClass(FqName("kotlin.jvm.functions.Function$n"), ClassKind.INTERFACE) { klass -> |
| for (i in 1..n) { |
| klass.addTypeParameter("P$i", irBuiltIns.anyNType, Variance.IN_VARIANCE) |
| } |
| val returnType = klass.addTypeParameter("R", irBuiltIns.anyNType, Variance.OUT_VARIANCE) |
| |
| klass.addFunction("invoke", returnType.defaultType, Modality.ABSTRACT).apply { |
| for (i in 1..n) { |
| addValueParameter("p$i", klass.typeParameters[i - 1].defaultType) |
| } |
| } |
| } |
| } |
| |
| private fun createClass( |
| fqName: FqName, |
| classKind: ClassKind = ClassKind.CLASS, |
| block: (IrClass) -> Unit = {} |
| ): IrClassSymbol = |
| buildClass { |
| name = fqName.shortName() |
| kind = classKind |
| }.apply { |
| parent = createPackage(FqName(fqName.parent().asString())) |
| createImplicitParameterDeclarationWithWrappedDescriptor() |
| block(this) |
| }.symbol |
| |
| private fun createPackage(fqName: FqName): IrPackageFragment = |
| IrExternalPackageFragmentImpl( |
| IrExternalPackageFragmentSymbolImpl( |
| EmptyPackageFragmentDescriptor(module, fqName) |
| ), |
| fqName |
| ) |
| |
| val lambdaClass: IrClassSymbol = createClass(FqName("kotlin.jvm.internal.Lambda")) { klass -> |
| klass.addConstructor().apply { |
| addValueParameter("arity", irBuiltIns.intType) |
| } |
| } |
| } |