@@ -12,11 +12,16 @@ import org.jetbrains.kotlin.ir.declarations.path
1212import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
1313import org.jetbrains.kotlin.ir.declarations.IrClass
1414import org.jetbrains.kotlin.ir.declarations.IrDeclaration
15+ import org.jetbrains.kotlin.ir.declarations.IrDeclarationParent
1516import org.jetbrains.kotlin.ir.declarations.IrFile
17+ import org.jetbrains.kotlin.ir.declarations.IrFunction
18+ import org.jetbrains.kotlin.ir.declarations.IrValueParameter
1619import org.jetbrains.kotlin.ir.util.dump
1720import org.jetbrains.kotlin.ir.util.packageFqName
1821import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
1922import org.jetbrains.kotlin.ir.IrFileEntry
23+ import org.jetbrains.kotlin.ir.types.IrType
24+ import org.jetbrains.kotlin.ir.types.IrSimpleType
2025
2126class KotlinExtractorExtension (private val tests : List <String >) : IrGenerationExtension {
2227 override fun generate (moduleFragment : IrModuleFragment , pluginContext : IrPluginContext ) {
@@ -96,37 +101,100 @@ fun extractFile(trapDir: File, srcDir: File, declaration: IrFile) {
96101 val pkg = declaration.fqName.asString()
97102 val pkgId = fileExtractor.extractPackage(pkg)
98103 tw.writeCupackage(id, pkgId)
99- declaration.declarations.map { fileExtractor.extractDeclaration(it) }
104+ declaration.declarations.map { fileExtractor.extractDeclaration(it, pkgId ) }
100105 }
101106}
102107
103108class KotlinFileExtractor (val tw : TrapWriter ) {
104- fun extractPackage (pkg : String ): Label <DbPackage > {
109+ fun usePackage (pkg : String ): Label <out DbPackage > {
110+ return extractPackage(pkg)
111+ }
112+
113+ fun extractPackage (pkg : String ): Label <out DbPackage > {
105114 val pkgLabel = " @\" package;$pkg \" "
106115 val id: Label <DbPackage > = tw.getIdFor(pkgLabel, {
107116 tw.writePackages(it, pkg)
108117 })
109118 return id
110119 }
111120
112- fun extractDeclaration (declaration : IrDeclaration ) {
121+ fun extractDeclaration (declaration : IrDeclaration , parentid : Label < out DbPackage_or_reftype > ) {
113122 when (declaration) {
114123 is IrClass -> extractClass(declaration)
124+ is IrFunction -> extractFunction(declaration, parentid)
115125 else -> extractorBug(" Unrecognised IrDeclaration: " + declaration.javaClass)
116126 }
117127 }
118128
119- fun extractClass (declaration : IrClass ) {
120- val id: Label <DbClass > = tw.getFreshId()
121- val locId = tw.getLocation(declaration.startOffset, declaration.endOffset)
122- val pkg = declaration.packageFqName?.asString() ? : " "
123- val cls = declaration.name.asString()
129+ @Suppress(" UNUSED_PARAMETER" )
130+ fun useSimpleType (c : IrSimpleType ): Label <out DbPrimitive > {
131+ // TODO: This shouldn't assume all SimpleType's are Int
132+ val label = " @\" type;int\" "
133+ val id: Label <DbPrimitive > = tw.getIdFor(label, {
134+ tw.writePrimitives(it, " int" )
135+ })
136+ return id
137+ }
138+
139+ fun useClass (c : IrClass ): Label <out DbClass > {
140+ val pkg = c.packageFqName?.asString() ? : " "
141+ val cls = c.name.asString()
124142 val qualClassName = if (pkg.isEmpty()) cls else " $pkg .$cls "
143+ val label = " @\" class;$qualClassName \" "
144+ val id: Label <DbClass > = tw.getIdFor(label)
145+ return id
146+ }
147+
148+ fun extractClass (c : IrClass ) {
149+ val id = useClass(c)
150+ val locId = tw.getLocation(c.startOffset, c.endOffset)
151+ val pkg = c.packageFqName?.asString() ? : " "
152+ val cls = c.name.asString()
125153 val pkgId = extractPackage(pkg)
126- tw.writeTrap(" $id = @\" class;$qualClassName \"\n " )
127154 tw.writeClasses(id, cls, pkgId, id)
128155 tw.writeHasLocation(id, locId)
129- declaration.declarations.map { extractDeclaration(it) }
156+ c.declarations.map { extractDeclaration(it, id) }
157+ }
158+
159+ fun useType (t : IrType ): Label <out DbType > {
160+ when (t) {
161+ is IrSimpleType -> return useSimpleType(t)
162+ is IrClass -> return useClass(t)
163+ else -> {
164+ extractorBug(" Unrecognised IrType: " + t.javaClass)
165+ return Label (0 )
166+ }
167+ }
168+ }
169+
170+ fun useDeclarationParent (dp : IrDeclarationParent ): Label <out DbPackage_or_reftype > {
171+ when (dp) {
172+ is IrFile -> return usePackage(dp.fqName.asString())
173+ is IrClass -> return useClass(dp)
174+ else -> {
175+ extractorBug(" Unrecognised IrDeclarationParent: " + dp.javaClass)
176+ return Label (0 )
177+ }
178+ }
179+ }
180+
181+ fun useFunction (f : IrFunction ): Label <out DbMethod > {
182+ val paramTypeIds = f.valueParameters.joinToString() { " {${useType(it.type).toString()} }" }
183+ val returnTypeId = useType(f.returnType)
184+ val parentId = useDeclarationParent(f.parent)
185+ val label = " @\" callable;{$parentId }.${f.name.asString()} ($paramTypeIds ){$returnTypeId }\" "
186+ val id: Label <DbMethod > = tw.getIdFor(label)
187+ return id
188+ }
189+
190+ fun extractFunction (f : IrFunction , parentid : Label <out DbPackage_or_reftype >) {
191+ val id = useFunction(f)
192+ val locId = tw.getLocation(f.startOffset, f.endOffset)
193+ val signature = " TODO"
194+ val returnTypeId = useType(f.returnType)
195+ tw.writeMethods(id, f.name.asString(), signature, returnTypeId, parentid, id)
196+ tw.writeHasLocation(id, locId)
130197 }
198+
131199}
132200
0 commit comments