Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Commit

Permalink
Move KonanBuiltIns to a separate module (#1860)
Browse files Browse the repository at this point in the history
1. Separate (temporary) modules for the code to be moved to https://github.com/JetBrains/kotlin repo
2. KonanBuiltIns, functions for creation of module descriptors and interop descriptors and moved to "konan.descriptors" module 
3. Re-organizing KonanModuleOrigin and it's inheritors to better naming/understanding
  • Loading branch information
ddolovov committed Aug 14, 2018
1 parent 6064cc9 commit a4a235b
Show file tree
Hide file tree
Showing 45 changed files with 489 additions and 447 deletions.
3 changes: 3 additions & 0 deletions backend.native/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ dependencies {
compilerCompile kotlinNativeInterop['llvm'].configuration
compilerCompile kotlinNativeInterop['hash'].configuration
compilerCompile "org.jetbrains.kotlin:kotlin-native-shared:$konanVersion"
compilerCompile "org.jetbrains.kotlin:konan.descriptors:$konanVersion"
compilerCompile "org.jetbrains.kotlin:konan.metadata:$konanVersion"
compilerCompile "org.jetbrains.kotlin:konan.metadata.serializer:$konanVersion"

cli_bcCompile kotlinCompilerModule
cli_bcCompile "org.jetbrains.kotlin:kotlin-native-shared:$konanVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl
import org.jetbrains.kotlin.descriptors.impl.ReceiverParameterDescriptorImpl
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl
import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.SourceManager
import org.jetbrains.kotlin.ir.declarations.*
Expand All @@ -49,10 +50,14 @@ import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.builtins.konan.KonanBuiltIns
import org.jetbrains.kotlin.konan.target.CompilerOutputKind
import org.jetbrains.kotlin.metadata.KonanLinkData
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
import org.jetbrains.kotlin.resolve.descriptorUtil.module
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitClassReceiver
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor
import org.jetbrains.kotlin.serialization.deserialization.getName
Expand Down Expand Up @@ -280,6 +285,16 @@ internal class Context(config: KonanConfig) : KonanBackendContext(config) {
moduleDescriptor.builtIns as KonanBuiltIns
}

private val packageScope by lazy { builtIns.builtInsModule.getPackage(KonanFqNames.packageName).memberScope }

val nativePtr by lazy { packageScope.getContributedClassifier(NATIVE_PTR_NAME) as ClassDescriptor }
val nativePtrPlusLong by lazy { nativePtr.unsubstitutedMemberScope.getContributedFunctions("plus").single() }
val nativePtrToLong by lazy { nativePtr.unsubstitutedMemberScope.getContributedFunctions("toLong").single() }
val getNativeNullPtr by lazy { packageScope.getContributedFunctions("getNativeNullPtr").single() }
val immutableBinaryBlobOf by lazy {
builtIns.builtInsModule.getPackage(FqName("konan")).memberScope.getContributedFunctions("immutableBinaryBlobOf").single()
}

val specialDeclarationsFactory = SpecialDeclarationsFactory(this)

class LazyMember<T>(val initializer: Context.() -> T) {
Expand Down Expand Up @@ -350,7 +365,7 @@ internal class Context(config: KonanConfig) : KonanBackendContext(config) {
get() = ir.irModule.irBuiltins

val interopBuiltIns by lazy {
InteropBuiltIns(this.builtIns)
InteropBuiltIns(this.builtIns, nativePtr)
}

var llvmModule: LLVMModuleRef? = null
Expand Down Expand Up @@ -509,5 +524,13 @@ internal class Context(config: KonanConfig) : KonanBackendContext(config) {
val kind = config.configuration.get(KonanConfigKeys.PRODUCE)
kind == CompilerOutputKind.DYNAMIC || kind == CompilerOutputKind.STATIC
}

internal val stdlibModule
get() = this.builtIns.any.module
}

private fun MemberScope.getContributedClassifier(name: String) =
this.getContributedClassifier(Name.identifier(name), NoLookupLocation.FROM_BUILTINS)

private fun MemberScope.getContributedFunctions(name: String) =
this.getContributedFunctions(Name.identifier(name), NoLookupLocation.FROM_BUILTINS)
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ import org.jetbrains.kotlin.backend.konan.irasdescriptors.containsNull
import org.jetbrains.kotlin.backend.konan.irasdescriptors.fqNameSafe
import org.jetbrains.kotlin.builtins.PrimitiveType
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.konan.interop.InteropFqNames
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classifierOrFail
import org.jetbrains.kotlin.ir.types.makeNullable
import org.jetbrains.kotlin.ir.util.constructors
import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.name.ClassId
Expand Down Expand Up @@ -81,7 +84,7 @@ enum class KonanPrimitiveType(val classId: ClassId) {
LONG(PrimitiveType.LONG),
FLOAT(PrimitiveType.FLOAT),
DOUBLE(PrimitiveType.DOUBLE),
NON_NULL_NATIVE_PTR(ClassId.topLevel(KonanBuiltIns.FqNames.nonNullNativePtr.toSafe()))
NON_NULL_NATIVE_PTR(ClassId.topLevel(KonanFqNames.nonNullNativePtr.toSafe()))

;

Expand Down Expand Up @@ -204,8 +207,8 @@ internal abstract class InlineClassesSupport<Class : Any, Type : Any> {

private val implicitInlineClasses =
(KonanPrimitiveType.values().map { it.fqName } +
KonanBuiltIns.FqNames.nativePtr +
InteropBuiltIns.FqNames.cPointer).toSet()
KonanFqNames.nativePtr +
InteropFqNames.cPointer).toSet()

private enum class ValueClass(val fqName: FqNameUnsafe, val binaryType: BinaryType.Primitive) {

Expand Down Expand Up @@ -256,7 +259,7 @@ internal object KotlinTypeInlineClassesSupport : InlineClassesSupport<ClassDescr
override fun hasInlineModifier(clazz: ClassDescriptor): Boolean = clazz.isInline

override fun getNativePointedSuperclass(clazz: ClassDescriptor): ClassDescriptor? = clazz.getAllSuperClassifiers()
.firstOrNull { it.fqNameUnsafe == InteropBuiltIns.FqNames.nativePointed } as ClassDescriptor?
.firstOrNull { it.fqNameUnsafe == InteropFqNames.nativePointed } as ClassDescriptor?

override fun getInlinedClassUnderlyingType(clazz: ClassDescriptor): KotlinType =
clazz.unsubstitutedPrimaryConstructor!!.valueParameters.single().type
Expand Down Expand Up @@ -290,9 +293,9 @@ private object IrTypeInlineClassesSupport : InlineClassesSupport<IrClass, IrType
override fun hasInlineModifier(clazz: IrClass): Boolean = clazz.descriptor.isInline

override fun getNativePointedSuperclass(clazz: IrClass): IrClass? = clazz.getAllSuperClassifiers()
.firstOrNull { it.descriptor.fqNameUnsafe == InteropBuiltIns.FqNames.nativePointed }
.firstOrNull { it.fqNameSafe.toUnsafe() == InteropFqNames.nativePointed }

override fun getInlinedClassUnderlyingType(clazz: IrClass): IrType =
clazz.constructors.first { it.isPrimary }.valueParameters.single().type

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import org.jetbrains.kotlin.backend.konan.descriptors.ClassifierAliasingPackageF
import org.jetbrains.kotlin.backend.konan.descriptors.ExportedForwardDeclarationsPackageFragmentDescriptor
import org.jetbrains.kotlin.backend.konan.library.KonanLibraryReader
import org.jetbrains.kotlin.backend.konan.serialization.KonanPackageFragment
import org.jetbrains.kotlin.builtins.konan.KonanBuiltIns
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.konan.interop.InteropFqNames
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
Expand All @@ -50,32 +51,15 @@ fun createInteropLibrary(reader: KonanLibraryReader): InteropLibrary? {
return InteropLibraryImpl(FqName(pkg), exportForwardDeclarations)
}

private val cPointerName = "CPointer"
private val nativePointedName = "NativePointed"
internal class InteropBuiltIns(builtIns: KonanBuiltIns, vararg konanPrimitives: ClassDescriptor) {

internal class InteropBuiltIns(builtIns: KonanBuiltIns) {

object FqNames {
val packageName = FqName("kotlinx.cinterop")

val cPointer = packageName.child(Name.identifier(cPointerName)).toUnsafe()
val nativePointed = packageName.child(Name.identifier(nativePointedName)).toUnsafe()

val cNames = FqName("cnames")
val cNamesStructs = cNames.child(Name.identifier("structs"))

val objCNames = FqName("objcnames")
val objCNamesClasses = objCNames.child(Name.identifier("classes"))
val objCNamesProtocols = objCNames.child(Name.identifier("protocols"))
}

val packageScope = builtIns.builtInsModule.getPackage(FqNames.packageName).memberScope
val packageScope = builtIns.builtInsModule.getPackage(InteropFqNames.packageName).memberScope

val getPointerSize = packageScope.getContributedFunctions("getPointerSize").single()

val nativePointed = packageScope.getContributedClass(nativePointedName)
val nativePointed = packageScope.getContributedClass(InteropFqNames.nativePointedName)

val cPointer = this.packageScope.getContributedClass(cPointerName)
val cPointer = this.packageScope.getContributedClass(InteropFqNames.cPointerName)

val cPointerRawValue = cPointer.unsubstitutedMemberScope.getContributedVariables("rawValue").single()

Expand All @@ -102,11 +86,9 @@ internal class InteropBuiltIns(builtIns: KonanBuiltIns) {

val nativeMemUtils = packageScope.getContributedClass("nativeMemUtils")

private val primitives = listOf(
builtIns.byte, builtIns.short, builtIns.int, builtIns.long,
builtIns.float, builtIns.double,
builtIns.nativePtr
)
private val primitives = arrayOf(
arrayOf(builtIns.byte, builtIns.short, builtIns.int, builtIns.long, builtIns.float, builtIns.double),
konanPrimitives).flatten()

val readPrimitive = primitives.map {
nativeMemUtils.unsubstitutedMemberScope.getContributedFunctions("get" + it.name).single()
Expand Down Expand Up @@ -212,12 +194,10 @@ private class InteropLibraryImpl(
): List<PackageFragmentDescriptor> {
val interopPackageFragments = konanPackageFragments.filter { it.fqName == packageFqName }

val fqNames = InteropBuiltIns.FqNames

val result = mutableListOf<PackageFragmentDescriptor>()

// Allow references to forwarding declarations to be resolved into classifiers declared in this library:
listOf(fqNames.cNamesStructs, fqNames.objCNamesClasses, fqNames.objCNamesProtocols).mapTo(result) { fqName ->
listOf(InteropFqNames.cNamesStructs, InteropFqNames.objCNamesClasses, InteropFqNames.objCNamesProtocols).mapTo(result) { fqName ->
ClassifierAliasingPackageFragmentDescriptor(interopPackageFragments, module, fqName)
}
// TODO: use separate namespaces for structs, enums, Objective-C protocols etc.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.builtins.konan.KonanBuiltIns
import org.jetbrains.kotlin.name.Name

abstract internal class KonanBackendContext(val config: KonanConfig) : CommonBackendContext {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@
package org.jetbrains.kotlin.backend.konan

import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.backend.konan.descriptors.createForwardDeclarationsModule
import org.jetbrains.kotlin.backend.konan.library.*
import org.jetbrains.kotlin.backend.konan.library.impl.*
import org.jetbrains.kotlin.backend.konan.library.KonanLibraryReader
import org.jetbrains.kotlin.backend.konan.library.impl.LibraryReaderImpl
import org.jetbrains.kotlin.backend.konan.library.impl.purgeUnneeded
import org.jetbrains.kotlin.backend.konan.library.resolveImmediateLibraries
import org.jetbrains.kotlin.backend.konan.library.resolveLibrariesRecursive
import org.jetbrains.kotlin.backend.konan.library.withResolvedDependencies
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.cli.common.config.kotlinSourceRoots
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.*
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.STRONG_WARNING
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
import org.jetbrains.kotlin.descriptors.konan.interop.createForwardDeclarationsModule
import org.jetbrains.kotlin.konan.TempFiles
import org.jetbrains.kotlin.konan.file.File
import org.jetbrains.kotlin.konan.library.defaultResolver
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2010-2017 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.backend.konan

import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.FqNameUnsafe
import org.jetbrains.kotlin.name.Name

internal const val NATIVE_PTR_NAME = "NativePtr"

object KonanFqNames {

val packageName = FqName("konan.internal")
val nativePtr = packageName.child(Name.identifier(NATIVE_PTR_NAME)).toUnsafe()
val nonNullNativePtr = FqNameUnsafe("konan.internal.NonNullNativePtr")
val throws = FqName("konan.Throws")
}

/**
* Maximum number of parameters supported in function types (e.g. `FunctionXX`, `KFunctionXX`, `SuspendFunctionXX`).
*/
internal const val KONAN_FUNCTION_INTERFACES_MAX_PARAMETERS = 22
Loading

0 comments on commit a4a235b

Please sign in to comment.