[Interop][Metadata] Mangle global properties names
This commit is contained in:
committed by
Sergey Bogolepov
parent
4ceb988246
commit
cd9812b504
+3
-14
@@ -331,17 +331,6 @@ abstract class KotlinFile(
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data class KotlinParameter(
|
// Try to use the provided name. If failed, mangle it with underscore and try again:
|
||||||
val name: String,
|
internal tailrec fun getTopLevelPropertyDeclarationName(scope: KotlinScope, name: String): String =
|
||||||
val type: KotlinType,
|
scope.declareProperty(name) ?: getTopLevelPropertyDeclarationName(scope, name + "_")
|
||||||
val isVararg: Boolean,
|
|
||||||
val annotations: List<String>
|
|
||||||
) {
|
|
||||||
fun render(scope: KotlinScope) = buildString {
|
|
||||||
annotations.forEach { append("$it ") }
|
|
||||||
if (isVararg) append("vararg ")
|
|
||||||
append(name.asSimpleName())
|
|
||||||
append(": ")
|
|
||||||
append(type.render(scope))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+3
-3
@@ -129,7 +129,7 @@ class StubIrDriver(
|
|||||||
GenerationMode.SOURCE_CODE -> {
|
GenerationMode.SOURCE_CODE -> {
|
||||||
emitSourceCode(outKtFile(), builderResult, bridgeBuilderResult)
|
emitSourceCode(outKtFile(), builderResult, bridgeBuilderResult)
|
||||||
}
|
}
|
||||||
GenerationMode.METADATA -> emitMetadata(builderResult, moduleName)
|
GenerationMode.METADATA -> emitMetadata(builderResult, moduleName, bridgeBuilderResult.kotlinFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,8 +142,8 @@ class StubIrDriver(
|
|||||||
return Result.SourceCode
|
return Result.SourceCode
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun emitMetadata(builderResult: StubIrBuilderResult, moduleName: String) =
|
private fun emitMetadata(builderResult: StubIrBuilderResult, moduleName: String, scope: KotlinScope) =
|
||||||
Result.Metadata(StubIrMetadataEmitter(context, builderResult, moduleName).emit())
|
Result.Metadata(StubIrMetadataEmitter(context, builderResult, moduleName, scope).emit())
|
||||||
|
|
||||||
private fun emitCFile(context: StubIrContext, cFile: Appendable, entryPoint: String?, nativeBridges: NativeBridges) {
|
private fun emitCFile(context: StubIrContext, cFile: Appendable, entryPoint: String?, nativeBridges: NativeBridges) {
|
||||||
val out = { it: String -> cFile.appendln(it) }
|
val out = { it: String -> cFile.appendln(it) }
|
||||||
|
|||||||
+20
-6
@@ -12,18 +12,20 @@ import org.jetbrains.kotlin.utils.addIfNotNull
|
|||||||
class StubIrMetadataEmitter(
|
class StubIrMetadataEmitter(
|
||||||
private val context: StubIrContext,
|
private val context: StubIrContext,
|
||||||
private val builderResult: StubIrBuilderResult,
|
private val builderResult: StubIrBuilderResult,
|
||||||
private val moduleName: String
|
private val moduleName: String,
|
||||||
|
private val scope: KotlinScope
|
||||||
) {
|
) {
|
||||||
fun emit(): KlibModuleMetadata {
|
fun emit(): KlibModuleMetadata {
|
||||||
val annotations = emptyList<KmAnnotation>()
|
val annotations = emptyList<KmAnnotation>()
|
||||||
val fragments = emitModuleFragments()
|
val fragments = emitModuleFragments(scope)
|
||||||
return KlibModuleMetadata(moduleName, fragments, annotations)
|
return KlibModuleMetadata(moduleName, fragments, annotations)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun emitModuleFragments(): List<KmModuleFragment> =
|
private fun emitModuleFragments(scope: KotlinScope): List<KmModuleFragment> =
|
||||||
ModuleMetadataEmitter(
|
ModuleMetadataEmitter(
|
||||||
context.configuration.pkgName,
|
context.configuration.pkgName,
|
||||||
builderResult.stubs
|
builderResult.stubs,
|
||||||
|
scope
|
||||||
).emit().let { kmModuleFragment ->
|
).emit().let { kmModuleFragment ->
|
||||||
// We need to create module fragment for each part of package name.
|
// We need to create module fragment for each part of package name.
|
||||||
val pkgName = context.configuration.pkgName
|
val pkgName = context.configuration.pkgName
|
||||||
@@ -43,7 +45,8 @@ class StubIrMetadataEmitter(
|
|||||||
*/
|
*/
|
||||||
internal class ModuleMetadataEmitter(
|
internal class ModuleMetadataEmitter(
|
||||||
private val packageFqName: String,
|
private val packageFqName: String,
|
||||||
private val module: SimpleStubContainer
|
private val module: SimpleStubContainer,
|
||||||
|
private val scope: KotlinScope
|
||||||
) {
|
) {
|
||||||
|
|
||||||
fun emit(): KmModuleFragment {
|
fun emit(): KmModuleFragment {
|
||||||
@@ -88,6 +91,16 @@ internal class ModuleMetadataEmitter(
|
|||||||
val typeParametersInterner: Interner<TypeParameterStub> = Interner()
|
val typeParametersInterner: Interner<TypeParameterStub> = Interner()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
private fun isTopLevelContainer(container: StubContainer?): Boolean =
|
||||||
|
container == null
|
||||||
|
|
||||||
|
private fun getPropertyNameInScope(originalName: String, container: StubContainer?): String =
|
||||||
|
if (isTopLevelContainer(container)) {
|
||||||
|
getTopLevelPropertyDeclarationName(scope, originalName)
|
||||||
|
} else {
|
||||||
|
originalName
|
||||||
|
}
|
||||||
|
|
||||||
private val visitor = object : StubIrVisitor<VisitingContext, Any> {
|
private val visitor = object : StubIrVisitor<VisitingContext, Any> {
|
||||||
|
|
||||||
override fun visitClass(element: ClassStub, data: VisitingContext): List<KmClass> {
|
override fun visitClass(element: ClassStub, data: VisitingContext): List<KmClass> {
|
||||||
@@ -146,7 +159,8 @@ internal class ModuleMetadataEmitter(
|
|||||||
|
|
||||||
override fun visitProperty(element: PropertyStub, data: VisitingContext) =
|
override fun visitProperty(element: PropertyStub, data: VisitingContext) =
|
||||||
with (MappingExtensions(data.typeParametersInterner)) {
|
with (MappingExtensions(data.typeParametersInterner)) {
|
||||||
KmProperty(element.flags, element.name, element.getterFlags, element.setterFlags).also { km ->
|
val name = getPropertyNameInScope(element.name, data.container)
|
||||||
|
KmProperty(element.flags, name, element.getterFlags, element.setterFlags).also { km ->
|
||||||
element.annotations.mapTo(km.annotations) { it.map() }
|
element.annotations.mapTo(km.annotations) { it.map() }
|
||||||
km.uniqId = data.uniqIds.uniqIdForProperty(element)
|
km.uniqId = data.uniqIds.uniqIdForProperty(element)
|
||||||
km.returnType = element.type.map()
|
km.returnType = element.type.map()
|
||||||
|
|||||||
-4
@@ -279,10 +279,6 @@ class StubIrTextEmitter(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to use the provided name. If failed, mangle it with underscore and try again:
|
|
||||||
private tailrec fun getTopLevelPropertyDeclarationName(scope: KotlinScope, name: String): String =
|
|
||||||
scope.declareProperty(name) ?: getTopLevelPropertyDeclarationName(scope, name + "_")
|
|
||||||
|
|
||||||
private fun emitProperty(element: PropertyStub, owner: StubContainer?) {
|
private fun emitProperty(element: PropertyStub, owner: StubContainer?) {
|
||||||
if (element in bridgeBuilderResult.excludedStubs) return
|
if (element in bridgeBuilderResult.excludedStubs) return
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user