Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c446b0e

Browse files
tamasvajkigfoo
authored andcommitted
Move anonymous class and local function label generation to KotlinUsesExtractor
1 parent 6dbf278 commit c446b0e

3 files changed

Lines changed: 57 additions & 60 deletions

File tree

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,6 @@ open class KotlinFileExtractor(
125125
return id
126126
}
127127

128-
private val anonymousTypeMapping: MutableMap<IrClass, TypeResults> = mutableMapOf()
129-
130-
fun useAnonymousClass(c: IrClass): TypeResults {
131-
var res = anonymousTypeMapping[c]
132-
if (res == null) {
133-
val javaResult = TypeResult(tw.getFreshIdLabel<DbClass>(), "", "")
134-
val kotlinResult = TypeResult(tw.getFreshIdLabel<DbKt_notnull_type>(), "", "")
135-
tw.writeKt_notnull_types(kotlinResult.id, javaResult.id)
136-
res = TypeResults(javaResult, kotlinResult)
137-
anonymousTypeMapping[c] = res
138-
}
139-
140-
return res
141-
}
142-
143128
private fun extractAnonymousClassStmt(c: IrClass, callable: Label<out DbCallable>, parent: Label<out DbStmtparent>, idx: Int) {
144129
@Suppress("UNCHECKED_CAST")
145130
val id = extractClassSource(c) as Label<out DbClass>
@@ -1768,31 +1753,6 @@ open class KotlinFileExtractor(
17681753
private val IrType.isAnonymous: Boolean
17691754
get() = ((this as? IrSimpleType)?.classifier?.owner as? IrClass)?.isAnonymousObject ?: false
17701755

1771-
1772-
private val generatedLocalFunctionTypeMapping: MutableMap<IrFunction, LocalFunctionLabels> = mutableMapOf()
1773-
1774-
data class LocalFunctionLabels(val type: TypeResults, val constructor: Label<DbConstructor>, val function: Label<DbMethod>)
1775-
1776-
fun getLocalFunctionLabels(f: IrFunction): LocalFunctionLabels {
1777-
if (!f.isLocalFunction()){
1778-
logger.warnElement(Severity.ErrorSevere, "Extracting a non-local function as a local one", f)
1779-
}
1780-
1781-
var res = generatedLocalFunctionTypeMapping[f]
1782-
if (res == null) {
1783-
val javaResult = TypeResult(tw.getFreshIdLabel<DbClass>(), "", "")
1784-
val kotlinResult = TypeResult(tw.getFreshIdLabel<DbKt_notnull_type>(), "", "")
1785-
tw.writeKt_notnull_types(kotlinResult.id, javaResult.id)
1786-
res = LocalFunctionLabels(
1787-
TypeResults(javaResult, kotlinResult),
1788-
tw.getFreshIdLabel(),
1789-
tw.getFreshIdLabel())
1790-
generatedLocalFunctionTypeMapping[f] = res
1791-
}
1792-
1793-
return res
1794-
}
1795-
17961756
fun extractGeneratedClass(localFunction: IrFunction, superTypes: List<IrType>) : Label<out DbClass> {
17971757
val ids = getLocalFunctionLabels(localFunction)
17981758

java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,6 @@ open class KotlinUsesExtractor(
6666
?.let { pluginContext.referenceClass(it.asSingleFqName()) }
6767
?.owner
6868

69-
private fun withSourceFile(clsFile: IrFile): KotlinFileExtractor {
70-
if (this is KotlinSourceFileExtractor && this.file == clsFile) {
71-
return this
72-
}
73-
74-
val newTrapWriter = tw.makeSourceFileTrapWriter(clsFile, false)
75-
val newLogger = FileLogger(logger.logCounter, newTrapWriter)
76-
return KotlinSourceFileExtractor(newLogger, newTrapWriter, clsFile, externalClassExtractor, primitiveTypeMapping, pluginContext)
77-
}
78-
7969
/**
8070
* Gets a KotlinFileExtractor based on this one, except it attributes locations to the file that declares the given class.
8171
*/
@@ -86,9 +76,15 @@ open class KotlinUsesExtractor(
8676
val newTrapWriter = tw.makeFileTrapWriter(getIrClassBinaryPath(cls))
8777
val newLogger = FileLogger(logger.logCounter, newTrapWriter)
8878
return KotlinFileExtractor(newLogger, newTrapWriter, dependencyCollector, externalClassExtractor, primitiveTypeMapping, pluginContext)
89-
} else {
90-
return withSourceFile(clsFile)
9179
}
80+
81+
if (this is KotlinSourceFileExtractor && this.file == clsFile) {
82+
return this
83+
}
84+
85+
val newTrapWriter = tw.makeSourceFileTrapWriter(clsFile, false)
86+
val newLogger = FileLogger(logger.logCounter, newTrapWriter)
87+
return KotlinSourceFileExtractor(newLogger, newTrapWriter, clsFile, externalClassExtractor, primitiveTypeMapping, pluginContext)
9288
}
9389

9490
fun useClassInstance(c: IrClass, typeArgs: List<IrTypeArgument>): UseClassInstanceResult {
@@ -158,6 +154,21 @@ open class KotlinUsesExtractor(
158154
classLabelResult.shortName)
159155
}
160156

157+
private val anonymousTypeMapping: MutableMap<IrClass, TypeResults> = mutableMapOf()
158+
159+
fun useAnonymousClass(c: IrClass): TypeResults {
160+
var res = anonymousTypeMapping[c]
161+
if (res == null) {
162+
val javaResult = TypeResult(tw.getFreshIdLabel<DbClass>(), "", "")
163+
val kotlinResult = TypeResult(tw.getFreshIdLabel<DbKt_notnull_type>(), "", "")
164+
tw.writeKt_notnull_types(kotlinResult.id, javaResult.id)
165+
res = TypeResults(javaResult, kotlinResult)
166+
anonymousTypeMapping[c] = res
167+
}
168+
169+
return res
170+
}
171+
161172
fun useSimpleTypeClass(c: IrClass, args: List<IrTypeArgument>, hasQuestionMark: Boolean): TypeResults {
162173
if (c.isAnonymousObject) {
163174
if (args.isNotEmpty()) {
@@ -167,7 +178,7 @@ open class KotlinUsesExtractor(
167178
logger.warn(Severity.ErrorHigh, "Unexpected nullable anonymous class")
168179
}
169180

170-
return withSourceFile(c.fileOrNull!!).useAnonymousClass(c)
181+
return useAnonymousClass(c)
171182
}
172183

173184
val classInstanceResult = useClassInstance(c, args)
@@ -453,9 +464,33 @@ class X {
453464
this.origin != IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA
454465
}
455466

467+
private val generatedLocalFunctionTypeMapping: MutableMap<IrFunction, LocalFunctionLabels> = mutableMapOf()
468+
469+
data class LocalFunctionLabels(val type: TypeResults, val constructor: Label<DbConstructor>, val function: Label<DbMethod>)
470+
471+
fun getLocalFunctionLabels(f: IrFunction): LocalFunctionLabels {
472+
if (!f.isLocalFunction()){
473+
logger.warn(Severity.ErrorSevere, "Extracting a non-local function as a local one")
474+
}
475+
476+
var res = generatedLocalFunctionTypeMapping[f]
477+
if (res == null) {
478+
val javaResult = TypeResult(tw.getFreshIdLabel<DbClass>(), "", "")
479+
val kotlinResult = TypeResult(tw.getFreshIdLabel<DbKt_notnull_type>(), "", "")
480+
tw.writeKt_notnull_types(kotlinResult.id, javaResult.id)
481+
res = LocalFunctionLabels(
482+
TypeResults(javaResult, kotlinResult),
483+
tw.getFreshIdLabel(),
484+
tw.getFreshIdLabel())
485+
generatedLocalFunctionTypeMapping[f] = res
486+
}
487+
488+
return res
489+
}
490+
456491
fun <T: DbCallable> useFunction(f: IrFunction): Label<out T> {
457492
if (f.isLocalFunction()) {
458-
val ids = withSourceFile(f.fileOrNull!!).getLocalFunctionLabels(f)
493+
val ids = getLocalFunctionLabels(f)
459494
@Suppress("UNCHECKED_CAST")
460495
return ids.function as Label<out T>
461496
}
@@ -559,7 +594,7 @@ class X {
559594
fun useClassSource(c: IrClass): Label<out DbClassorinterface> {
560595
if (c.isAnonymousObject) {
561596
@Suppress("UNCHECKED_CAST")
562-
return withSourceFile(c.fileOrNull!!).useAnonymousClass(c).javaResult.id as Label<DbClass>
597+
return useAnonymousClass(c).javaResult.id as Label<DbClass>
563598
}
564599

565600
// For source classes, the label doesn't include and type arguments

java/kotlin-extractor/src/main/kotlin/TrapWriter.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ open class TrapWriter (protected val lm: TrapLabelManager, private val bw: Buffe
6868
return maybeLabel
6969
}
7070
}
71+
72+
fun <T> getFreshIdLabel(): Label<T> {
73+
val label: Label<T> = lm.getFreshLabel()
74+
writeTrap("$label = *\n")
75+
return label
76+
}
77+
7178
/**
7279
* It is not easy to assign keys to local variables, so they get
7380
* given `*` IDs. However, the same variable may be referred to
@@ -216,11 +223,6 @@ open class FileTrapWriter (
216223
// user knows where it came from.
217224
return "file://$filePath"
218225
}
219-
fun <T> getFreshIdLabel(): Label<T> {
220-
val label: Label<T> = lm.getFreshLabel()
221-
writeTrap("$label = *\n")
222-
return label
223-
}
224226
}
225227

226228
/**

0 commit comments

Comments
 (0)