Skip to content

Commit 8952295

Browse files
authored
Merge pull request #2 from github/igfoo/lazyKt
Kotlin: Don't make a *Kt class unless we need one
2 parents b458217 + e7a07ae commit 8952295

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

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

+15-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import java.nio.file.Files
88
import java.nio.file.Paths
99
import java.text.SimpleDateFormat
1010
import java.util.Date
11+
import java.util.Optional
1112
import kotlin.system.exitProcess
1213
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
1314
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
@@ -167,8 +168,8 @@ fun doFile(logger: Logger, trapDir: File, srcDir: File, declaration: IrFile) {
167168
val tw = TrapWriter(fileLabel, trapFileBW, declaration.fileEntry)
168169
val id: Label<DbFile> = tw.getLabelFor(fileLabel)
169170
tw.writeFiles(id, filePath, basename, extension, 0)
170-
val fileExtractor = KotlinFileExtractor(logger, tw)
171-
fileExtractor.extractFile(id, declaration)
171+
val fileExtractor = KotlinFileExtractor(logger, tw, declaration)
172+
fileExtractor.extractFile(id)
172173
}
173174
}
174175

@@ -179,14 +180,16 @@ fun <T> fakeLabel(): Label<T> {
179180
return Label(0)
180181
}
181182

182-
class KotlinFileExtractor(val logger: Logger, val tw: TrapWriter) {
183-
fun extractFile(id: Label<DbFile>, f: IrFile) {
184-
val pkg = f.fqName.asString()
183+
class KotlinFileExtractor(val logger: Logger, val tw: TrapWriter, val file: IrFile) {
184+
val fileClass by lazy {
185+
extractFileClass(file)
186+
}
187+
188+
fun extractFile(id: Label<DbFile>) {
189+
val pkg = file.fqName.asString()
185190
val pkgId = extractPackage(pkg)
186191
tw.writeCupackage(id, pkgId)
187-
// TODO: This shouldn't really exist if there is nothing to go on it
188-
val fileClass = extractFileClass(f)
189-
f.declarations.map { extractDeclaration(it, fileClass) }
192+
file.declarations.map { extractDeclaration(it, Optional.empty()) }
190193
}
191194

192195
fun extractFileClass(f: IrFile): Label<out DbClass> {
@@ -233,11 +236,11 @@ class KotlinFileExtractor(val logger: Logger, val tw: TrapWriter) {
233236
return id
234237
}
235238

236-
fun extractDeclaration(declaration: IrDeclaration, parentid: Label<out DbReftype>) {
239+
fun extractDeclaration(declaration: IrDeclaration, optParentid: Optional<Label<out DbReftype>>) {
237240
when (declaration) {
238241
is IrClass -> extractClass(declaration)
239-
is IrFunction -> extractFunction(declaration, parentid)
240-
is IrProperty -> extractProperty(declaration, parentid)
242+
is IrFunction -> extractFunction(declaration, if (optParentid.isPresent()) optParentid.get() else fileClass)
243+
is IrProperty -> extractProperty(declaration, if (optParentid.isPresent()) optParentid.get() else fileClass)
241244
else -> logger.warn("Unrecognised IrDeclaration: " + declaration.javaClass)
242245
}
243246
}
@@ -309,7 +312,7 @@ class KotlinFileExtractor(val logger: Logger, val tw: TrapWriter) {
309312
val pkgId = extractPackage(pkg)
310313
tw.writeClasses(id, cls, pkgId, id)
311314
tw.writeHasLocation(id, locId)
312-
c.declarations.map { extractDeclaration(it, id) }
315+
c.declarations.map { extractDeclaration(it, Optional.of(id)) }
313316
return id
314317
}
315318

java/ql/test/kotlin/library-tests/classes/classes.expected

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
| classes.kt:0:0:0:0 | Any |
2-
| classes.kt:0:0:0:0 | ClassesKt |
32
| classes.kt:0:0:0:0 | Unit |
43
| classes.kt:2:1:2:18 | ClassOne |
54
| classes.kt:4:1:6:1 | ClassTwo |

java/ql/test/kotlin/library-tests/multiple_files/classes.expected

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
| file1.kt:0:0:0:0 | Any |
2-
| file1.kt:0:0:0:0 | File1Kt |
32
| file1.kt:2:1:2:16 | Class1 |
43
| file2.kt:0:0:0:0 | Any |
5-
| file2.kt:0:0:0:0 | File2Kt |
64
| file2.kt:2:1:2:16 | Class2 |
75
| file3.kt:0:0:0:0 | Any |
86
| file3.kt:0:0:0:0 | MyJvmName |

java/ql/test/kotlin/library-tests/types/types.expected

-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@
88
| file://:0:0:0:0 | short | PrimitiveType |
99
| file://:0:0:0:0 | string | ??? |
1010
| types.kt:0:0:0:0 | Any | Class |
11-
| types.kt:0:0:0:0 | TypesKt | Class |
1211
| types.kt:2:1:33:1 | Foo | Class |

0 commit comments

Comments
 (0)