More fixes to C generator. (#1197)
This commit is contained in:
+6
-3
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.types.TypeUtils
|
|||||||
import org.jetbrains.kotlin.konan.target.*
|
import org.jetbrains.kotlin.konan.target.*
|
||||||
import org.jetbrains.kotlin.name.isChildOf
|
import org.jetbrains.kotlin.name.isChildOf
|
||||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||||
|
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
|
||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.module
|
import org.jetbrains.kotlin.resolve.descriptorUtil.module
|
||||||
|
|
||||||
private enum class ScopeKind {
|
private enum class ScopeKind {
|
||||||
@@ -171,14 +172,15 @@ private class ExportedElement(val kind: ElementKind,
|
|||||||
cname = "_konan_function_${owner.nextFunctionIndex()}"
|
cname = "_konan_function_${owner.nextFunctionIndex()}"
|
||||||
val llvmFunction = owner.codegen.llvmFunction(function)
|
val llvmFunction = owner.codegen.llvmFunction(function)
|
||||||
// If function is virtual, we need to resolve receiver properly.
|
// If function is virtual, we need to resolve receiver properly.
|
||||||
val bridge = if (!DescriptorUtils.isTopLevelDeclaration(function) && function.isOverridable) {
|
val bridge = if (!DescriptorUtils.isTopLevelDeclaration(function) && !function.isExtension &&
|
||||||
|
function.isOverridable) {
|
||||||
// We need LLVMGetElementType() as otherwise type is function pointer.
|
// We need LLVMGetElementType() as otherwise type is function pointer.
|
||||||
generateFunction(owner.codegen, LLVMGetElementType(llvmFunction.type)!!, cname) {
|
generateFunction(owner.codegen, LLVMGetElementType(llvmFunction.type)!!, cname) {
|
||||||
val receiver = param(0)
|
val receiver = param(0)
|
||||||
val numParams = LLVMCountParams(llvmFunction)
|
val numParams = LLVMCountParams(llvmFunction)
|
||||||
val args = (0 .. numParams - 1).map { index -> param(index) }
|
val args = (0 .. numParams - 1).map { index -> param(index) }
|
||||||
val callee = lookupVirtualImpl(receiver, function)
|
val callee = lookupVirtualImpl(receiver, function)
|
||||||
val result = callAtFunctionScope(callee, args, Lifetime.RETURN_VALUE)
|
val result = callAtFunctionScopeVerbatim(callee, args)
|
||||||
ret(result)
|
ret(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -488,7 +490,7 @@ internal class CAdapterGenerator(
|
|||||||
|
|
||||||
override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor, ignored: Void?): Boolean {
|
override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor, ignored: Void?): Boolean {
|
||||||
val fqName = descriptor.fqName
|
val fqName = descriptor.fqName
|
||||||
val name = if (fqName.isRoot) "root" else fqName.shortName().asString()
|
val name = if (fqName.isRoot) "root" else translateName(fqName.shortName().asString())
|
||||||
val packageScope = ExportedElementScope(ScopeKind.PACKAGE, name)
|
val packageScope = ExportedElementScope(ScopeKind.PACKAGE, name)
|
||||||
scopes.last().scopes += packageScope
|
scopes.last().scopes += packageScope
|
||||||
scopes.push(packageScope)
|
scopes.push(packageScope)
|
||||||
@@ -745,6 +747,7 @@ internal class CAdapterGenerator(
|
|||||||
fun translateName(name: String): String {
|
fun translateName(name: String): String {
|
||||||
return when {
|
return when {
|
||||||
simpleNameMapping.contains(name) -> simpleNameMapping[name]!!
|
simpleNameMapping.contains(name) -> simpleNameMapping[name]!!
|
||||||
|
cKeywords.contains(name) -> "${name}_"
|
||||||
else -> name
|
else -> name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-2
@@ -245,10 +245,15 @@ internal class FunctionGenerationContext(val function: LLVMValueRef,
|
|||||||
lifetime: Lifetime) =
|
lifetime: Lifetime) =
|
||||||
call(llvmFunction, args, lifetime, { cleanupLandingpad })
|
call(llvmFunction, args, lifetime, { cleanupLandingpad })
|
||||||
|
|
||||||
|
|
||||||
|
fun callAtFunctionScopeVerbatim(llvmFunction: LLVMValueRef, args: List<LLVMValueRef>) =
|
||||||
|
call(llvmFunction, args, Lifetime.IRRELEVANT, { cleanupLandingpad }, true)
|
||||||
|
|
||||||
fun call(llvmFunction: LLVMValueRef, args: List<LLVMValueRef>,
|
fun call(llvmFunction: LLVMValueRef, args: List<LLVMValueRef>,
|
||||||
resultLifetime: Lifetime = Lifetime.IRRELEVANT,
|
resultLifetime: Lifetime = Lifetime.IRRELEVANT,
|
||||||
lazyLandingpad: () -> LLVMBasicBlockRef? = { null }): LLVMValueRef {
|
lazyLandingpad: () -> LLVMBasicBlockRef? = { null },
|
||||||
val callArgs = if (!isObjectReturn(llvmFunction.type)) {
|
verbatim: Boolean = false): LLVMValueRef {
|
||||||
|
val callArgs = if (verbatim || !isObjectReturn(llvmFunction.type)) {
|
||||||
args
|
args
|
||||||
} else {
|
} else {
|
||||||
// If function returns an object - create slot for the returned value or give local arena.
|
// If function returns an object - create slot for the returned value or give local arena.
|
||||||
|
|||||||
Reference in New Issue
Block a user