[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(
|
||||
val name: String,
|
||||
val type: KotlinType,
|
||||
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))
|
||||
}
|
||||
}
|
||||
// Try to use the provided name. If failed, mangle it with underscore and try again:
|
||||
internal tailrec fun getTopLevelPropertyDeclarationName(scope: KotlinScope, name: String): String =
|
||||
scope.declareProperty(name) ?: getTopLevelPropertyDeclarationName(scope, name + "_")
|
||||
+3
-3
@@ -129,7 +129,7 @@ class StubIrDriver(
|
||||
GenerationMode.SOURCE_CODE -> {
|
||||
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
|
||||
}
|
||||
|
||||
private fun emitMetadata(builderResult: StubIrBuilderResult, moduleName: String) =
|
||||
Result.Metadata(StubIrMetadataEmitter(context, builderResult, moduleName).emit())
|
||||
private fun emitMetadata(builderResult: StubIrBuilderResult, moduleName: String, scope: KotlinScope) =
|
||||
Result.Metadata(StubIrMetadataEmitter(context, builderResult, moduleName, scope).emit())
|
||||
|
||||
private fun emitCFile(context: StubIrContext, cFile: Appendable, entryPoint: String?, nativeBridges: NativeBridges) {
|
||||
val out = { it: String -> cFile.appendln(it) }
|
||||
|
||||
+20
-6
@@ -12,18 +12,20 @@ import org.jetbrains.kotlin.utils.addIfNotNull
|
||||
class StubIrMetadataEmitter(
|
||||
private val context: StubIrContext,
|
||||
private val builderResult: StubIrBuilderResult,
|
||||
private val moduleName: String
|
||||
private val moduleName: String,
|
||||
private val scope: KotlinScope
|
||||
) {
|
||||
fun emit(): KlibModuleMetadata {
|
||||
val annotations = emptyList<KmAnnotation>()
|
||||
val fragments = emitModuleFragments()
|
||||
val fragments = emitModuleFragments(scope)
|
||||
return KlibModuleMetadata(moduleName, fragments, annotations)
|
||||
}
|
||||
|
||||
private fun emitModuleFragments(): List<KmModuleFragment> =
|
||||
private fun emitModuleFragments(scope: KotlinScope): List<KmModuleFragment> =
|
||||
ModuleMetadataEmitter(
|
||||
context.configuration.pkgName,
|
||||
builderResult.stubs
|
||||
builderResult.stubs,
|
||||
scope
|
||||
).emit().let { kmModuleFragment ->
|
||||
// We need to create module fragment for each part of package name.
|
||||
val pkgName = context.configuration.pkgName
|
||||
@@ -43,7 +45,8 @@ class StubIrMetadataEmitter(
|
||||
*/
|
||||
internal class ModuleMetadataEmitter(
|
||||
private val packageFqName: String,
|
||||
private val module: SimpleStubContainer
|
||||
private val module: SimpleStubContainer,
|
||||
private val scope: KotlinScope
|
||||
) {
|
||||
|
||||
fun emit(): KmModuleFragment {
|
||||
@@ -88,6 +91,16 @@ internal class ModuleMetadataEmitter(
|
||||
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> {
|
||||
|
||||
override fun visitClass(element: ClassStub, data: VisitingContext): List<KmClass> {
|
||||
@@ -146,7 +159,8 @@ internal class ModuleMetadataEmitter(
|
||||
|
||||
override fun visitProperty(element: PropertyStub, data: VisitingContext) =
|
||||
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() }
|
||||
km.uniqId = data.uniqIds.uniqIdForProperty(element)
|
||||
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?) {
|
||||
if (element in bridgeBuilderResult.excludedStubs) return
|
||||
|
||||
|
||||
Reference in New Issue
Block a user