Skip to content

Commit 299e1af

Browse files
committed
Merge main into redsun82/swift-code-reorg
2 parents 0957f63 + 80ffd81 commit 299e1af

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+578
-324
lines changed

.github/workflows/swift-codegen.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ jobs:
1515
- uses: actions/checkout@v3
1616
- uses: ./.github/actions/fetch-codeql
1717
- uses: bazelbuild/setup-bazelisk@v2
18+
- uses: actions/setup-python@v3
19+
- uses: pre-commit/[email protected]
20+
name: Check that python code is properly formatted
21+
with:
22+
extra_args: autopep8 --all-files
1823
- name: Run unit tests
1924
run: |
2025
bazel test //swift/codegen/test --test_output=errors
21-
- name: Check that QL generated code was checked in
22-
run: |
23-
bazel run //swift/codegen
24-
git add swift
25-
git diff --exit-code HEAD
26+
- uses: pre-commit/[email protected]
27+
name: Check that QL generated code was checked in
28+
with:
29+
extra_args: swift-codegen --all-files
2630
- name: Generate C++ files
2731
run: |
2832
bazel run //swift/codegen:codegen -- --generate=trap,cpp --cpp-output=$PWD/swift-generated-cpp-files

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ repos:
1515
- id: clang-format
1616
files: ^swift/.*\.(h|c|cpp)$
1717

18+
- repo: https://github.com/pre-commit/mirrors-autopep8
19+
rev: v1.6.0
20+
hooks:
21+
- id: autopep8
22+
files: ^swift/codegen/.*\.py
23+
1824
- repo: local
1925
hooks:
2026
- id: codeql-format

java/kotlin-extractor/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ sourceSets {
3535
"utils/versions/v_1_5_21/*.kt",
3636
"utils/versions/v_1_5_31/*.kt",
3737
"utils/versions/v_1_6_10/*.kt",
38-
"utils/versions/v_1_7_0-RC/*.kt",
39-
// "utils/versions/v_1_6_20/*.kt",
38+
"utils/versions/v_1_6_20/*.kt",
39+
// "utils/versions/v_1_7_0/*.kt",
4040
]
4141
}
4242
}

java/kotlin-extractor/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
kotlin.code.style=official
2-
kotlinVersion=1.6.20
2+
kotlinVersion=1.7.0
33

44
GROUP=com.github.codeql
55
VERSION_NAME=0.0.1

java/kotlin-extractor/kotlin_plugin_versions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def version_string_to_tuple(version):
2121
m = re.match(r'([0-9]+)\.([0-9]+)\.([0-9]+)(.*)', version)
2222
return tuple([int(m.group(i)) for i in range(1, 4)] + [m.group(4)])
2323

24-
many_versions = [ '1.4.32', '1.5.0', '1.5.10', '1.5.21', '1.5.31', '1.6.10', '1.7.0-RC', '1.6.20' ]
24+
many_versions = [ '1.4.32', '1.5.0', '1.5.10', '1.5.21', '1.5.31', '1.6.10', '1.6.20', '1.7.0' ]
2525

2626
many_versions_tuples = [version_string_to_tuple(v) for v in many_versions]
2727

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,32 @@ import com.semmle.extractor.java.OdasaOutput
66
import com.semmle.util.data.StringDigestor
77
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
88
import org.jetbrains.kotlin.ir.declarations.*
9+
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
910
import org.jetbrains.kotlin.ir.util.isFileClass
1011
import org.jetbrains.kotlin.ir.util.packageFqName
1112
import org.jetbrains.kotlin.ir.util.parentClassOrNull
13+
import org.jetbrains.kotlin.name.FqName
1214
import java.io.File
1315
import java.util.ArrayList
1416
import java.util.HashSet
1517
import java.util.zip.GZIPOutputStream
1618

1719
class ExternalDeclExtractor(val logger: FileLogger, val invocationTrapFile: String, val sourceFilePath: String, val primitiveTypeMapping: PrimitiveTypeMapping, val pluginContext: IrPluginContext, val globalExtensionState: KotlinExtractorGlobalState, val diagnosticTrapWriter: TrapWriter) {
1820

19-
val externalDeclsDone = HashSet<IrDeclaration>()
21+
val declBinaryNames = HashMap<IrDeclaration, String>()
22+
val externalDeclsDone = HashSet<Pair<String, String>>()
2023
val externalDeclWorkList = ArrayList<Pair<IrDeclaration, String>>()
2124

2225
val propertySignature = ";property"
2326
val fieldSignature = ";field"
2427

25-
fun extractLater(d: IrDeclaration, signature: String): Boolean {
28+
fun extractLater(d: IrDeclarationWithName, signature: String): Boolean {
2629
if (d !is IrClass && !isExternalFileClassMember(d)) {
2730
logger.errorElement("External declaration is neither a class, nor a top-level declaration", d)
2831
return false
2932
}
30-
val ret = externalDeclsDone.add(d)
33+
val declBinaryName = declBinaryNames.getOrPut(d) { getIrDeclBinaryName(d) }
34+
val ret = externalDeclsDone.add(Pair(declBinaryName, signature))
3135
if (ret) externalDeclWorkList.add(Pair(d, signature))
3236
return ret
3337
}

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

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ open class KotlinFileExtractor(
4141
globalExtensionState: KotlinExtractorGlobalState
4242
): KotlinUsesExtractor(logger, tw, dependencyCollector, externalClassExtractor, primitiveTypeMapping, pluginContext, globalExtensionState) {
4343

44-
inline fun <T> with(kind: String, element: IrElement, f: () -> T): T {
44+
private inline fun <T> with(kind: String, element: IrElement, f: () -> T): T {
4545
val name = when (element) {
4646
is IrFile -> element.name
4747
is IrDeclarationWithName -> element.name.asString()
@@ -86,6 +86,7 @@ open class KotlinFileExtractor(
8686
}
8787
}
8888

89+
@OptIn(ObsoleteDescriptorBasedAPI::class)
8990
private fun isFake(d: IrDeclarationWithVisibility): Boolean {
9091
val visibility = d.visibility
9192
if (visibility is DelegatedDescriptorVisibility && visibility.delegate == Visibilities.InvisibleFake) {
@@ -94,6 +95,9 @@ open class KotlinFileExtractor(
9495
if (d.isFakeOverride) {
9596
return true
9697
}
98+
if ((d as? IrFunction)?.descriptor?.isHiddenToOvercomeSignatureClash == true) {
99+
return true
100+
}
97101
return false
98102
}
99103

@@ -367,6 +371,27 @@ open class KotlinFileExtractor(
367371
tw.writeHasLocation(stmtId, locId)
368372
}
369373

374+
fun extractObinitFunction(c: IrClass, parentId: Label<out DbClassorinterface>) {
375+
// add method:
376+
val obinitLabel = getObinitLabel(c)
377+
val obinitId = tw.getLabelFor<DbMethod>(obinitLabel)
378+
val returnType = useType(pluginContext.irBuiltIns.unitType, TypeContext.RETURN)
379+
tw.writeMethods(obinitId, "<obinit>", "<obinit>()", returnType.javaResult.id, parentId, obinitId)
380+
tw.writeMethodsKotlinType(obinitId, returnType.kotlinResult.id)
381+
382+
val locId = tw.getLocation(c)
383+
tw.writeHasLocation(obinitId, locId)
384+
385+
addModifiers(obinitId, "private")
386+
387+
// add body:
388+
val blockId = tw.getFreshIdLabel<DbBlock>()
389+
tw.writeStmts_block(blockId, obinitId, 0, obinitId)
390+
tw.writeHasLocation(blockId, locId)
391+
392+
extractDeclInitializers(c.declarations, false) { Pair(blockId, obinitId) }
393+
}
394+
370395
fun extractClassSource(c: IrClass, extractDeclarations: Boolean, extractStaticInitializer: Boolean, extractPrivateMembers: Boolean, extractFunctionBodies: Boolean): Label<out DbClassorinterface> {
371396
with("class source", c) {
372397
DeclarationStackAdjuster(c).use {
@@ -421,6 +446,9 @@ open class KotlinFileExtractor(
421446
addModifiers(instance.id, "public", "static", "final")
422447
tw.writeClass_object(id.cast<DbClass>(), instance.id)
423448
}
449+
if (extractFunctionBodies && needsObinitFunction(c)) {
450+
extractObinitFunction(c, id)
451+
}
424452

425453
extractClassModifiers(c, id)
426454
extractClassSupertypes(c, id, inReceiverContext = true) // inReceiverContext = true is specified to force extraction of member prototypes of base types
@@ -2101,6 +2129,22 @@ open class KotlinFileExtractor(
21012129
enclosingStmt: Label<out DbStmt>
21022130
): Label<DbNewexpr> = extractNewExpr(useFunction<DbConstructor>(calledConstructor, constructorTypeArgs), constructedType, locId, parent, idx, callable, enclosingStmt)
21032131

2132+
private fun needsObinitFunction(c: IrClass) = c.primaryConstructor == null && c.constructors.count() > 1
2133+
2134+
private fun getObinitLabel(c: IrClass) = getFunctionLabel(
2135+
c,
2136+
null,
2137+
"<obinit>",
2138+
listOf(),
2139+
pluginContext.irBuiltIns.unitType,
2140+
null,
2141+
functionTypeParameters = listOf(),
2142+
classTypeArgsIncludingOuterClasses = listOf(),
2143+
overridesCollectionsMethod = false,
2144+
javaSignature = null,
2145+
addParameterWildcardsByDefault = false
2146+
)
2147+
21042148
private fun extractConstructorCall(
21052149
e: IrFunctionAccessExpression,
21062150
parent: Label<out DbExprparent>,
@@ -2192,7 +2236,7 @@ open class KotlinFileExtractor(
21922236
}
21932237
}
21942238

2195-
fun getStatementOriginOperator(origin: IrStatementOrigin?) = when (origin) {
2239+
private fun getStatementOriginOperator(origin: IrStatementOrigin?) = when (origin) {
21962240
IrStatementOrigin.PLUSEQ -> "plus"
21972241
IrStatementOrigin.MINUSEQ -> "minus"
21982242
IrStatementOrigin.MULTEQ -> "times"
@@ -2430,13 +2474,29 @@ open class KotlinFileExtractor(
24302474
loopIdMap.remove(e)
24312475
}
24322476
is IrInstanceInitializerCall -> {
2433-
val stmtParent = parent.stmt(e, callable)
24342477
val irConstructor = declarationStack.peek() as? IrConstructor
24352478
if (irConstructor == null) {
24362479
logger.errorElement("IrInstanceInitializerCall outside constructor", e)
24372480
return
24382481
}
2439-
extractInstanceInitializerBlock(stmtParent, irConstructor)
2482+
if (needsObinitFunction(irConstructor.parentAsClass)) {
2483+
val exprParent = parent.expr(e, callable)
2484+
val id = tw.getFreshIdLabel<DbMethodaccess>()
2485+
val type = useType(pluginContext.irBuiltIns.unitType)
2486+
val locId = tw.getLocation(e)
2487+
val methodLabel = getObinitLabel(irConstructor.parentAsClass)
2488+
val methodId = tw.getLabelFor<DbMethod>(methodLabel)
2489+
tw.writeExprs_methodaccess(id, type.javaResult.id, exprParent.parent, exprParent.idx)
2490+
tw.writeExprsKotlinType(id, type.kotlinResult.id)
2491+
tw.writeHasLocation(id, locId)
2492+
tw.writeCallableEnclosingExpr(id, callable)
2493+
tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt)
2494+
tw.writeCallableBinding(id, methodId)
2495+
}
2496+
else {
2497+
val stmtParent = parent.stmt(e, callable)
2498+
extractInstanceInitializerBlock(stmtParent, irConstructor)
2499+
}
24402500
}
24412501
is IrConstructorCall -> {
24422502
val exprParent = parent.expr(e, callable)

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

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ open class KotlinUsesExtractor(
190190
}
191191

192192
// The Kotlin compiler internal representation of Outer<A, B>.Inner<C, D>.InnerInner<E, F>.someFunction<G, H>.LocalClass<I, J> is LocalClass<I, J, G, H, E, F, C, D, A, B>. This function returns [A, B, C, D, E, F, G, H, I, J].
193-
fun orderTypeArgsLeftToRight(c: IrClass, argsIncludingOuterClasses: List<IrTypeArgument>?): List<IrTypeArgument>? {
193+
private fun orderTypeArgsLeftToRight(c: IrClass, argsIncludingOuterClasses: List<IrTypeArgument>?): List<IrTypeArgument>? {
194194
if(argsIncludingOuterClasses.isNullOrEmpty())
195195
return argsIncludingOuterClasses
196196
val ret = ArrayList<IrTypeArgument>()
@@ -237,7 +237,7 @@ open class KotlinUsesExtractor(
237237
return UseClassInstanceResult(classTypeResult, extractClass)
238238
}
239239

240-
fun isArray(t: IrSimpleType) = t.isBoxedArray || t.isPrimitiveArray()
240+
private fun isArray(t: IrSimpleType) = t.isBoxedArray || t.isPrimitiveArray()
241241

242242
fun extractClassLaterIfExternal(c: IrClass) {
243243
if (isExternalDeclaration(c)) {
@@ -1146,15 +1146,6 @@ open class KotlinUsesExtractor(
11461146
return res
11471147
}
11481148

1149-
fun <T: DbCallable> useFunctionCommon(f: IrFunction, label: String): Label<out T> {
1150-
val id: Label<T> = tw.getLabelFor(label)
1151-
if (isExternalDeclaration(f)) {
1152-
extractFunctionLaterIfExternalFileMember(f)
1153-
extractExternalEnclosingClassLater(f)
1154-
}
1155-
return id
1156-
}
1157-
11581149
// These are classes with Java equivalents, but whose methods don't all exist on those Java equivalents--
11591150
// for example, the numeric classes define arithmetic functions (Int.plus, Long.or and so on) that lower to
11601151
// primitive arithmetic on the JVM, but which we extract as calls to reflect the source syntax more closely.
@@ -1214,15 +1205,20 @@ open class KotlinUsesExtractor(
12141205
val ids = getLocallyVisibleFunctionLabels(f)
12151206
return ids.function.cast<T>()
12161207
} else {
1217-
val realFunction = kotlinFunctionToJavaEquivalent(f, noReplace)
1218-
return useFunctionCommon<T>(realFunction, getFunctionLabel(realFunction, classTypeArgsIncludingOuterClasses))
1208+
return useFunction(f, null, classTypeArgsIncludingOuterClasses, noReplace)
12191209
}
12201210
}
12211211

1222-
fun <T: DbCallable> useFunction(f: IrFunction, parentId: Label<out DbElement>, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>?, noReplace: Boolean = false) =
1223-
kotlinFunctionToJavaEquivalent(f, noReplace).let {
1224-
useFunctionCommon<T>(it, getFunctionLabel(it, parentId, classTypeArgsIncludingOuterClasses))
1212+
fun <T: DbCallable> useFunction(f: IrFunction, parentId: Label<out DbElement>?, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>?, noReplace: Boolean = false): Label<out T> {
1213+
val javaFun = kotlinFunctionToJavaEquivalent(f, noReplace)
1214+
val label = getFunctionLabel(javaFun, parentId, classTypeArgsIncludingOuterClasses)
1215+
val id: Label<T> = tw.getLabelFor(label)
1216+
if (isExternalDeclaration(javaFun)) {
1217+
extractFunctionLaterIfExternalFileMember(javaFun)
1218+
extractExternalEnclosingClassLater(javaFun)
12251219
}
1220+
return id
1221+
}
12261222

12271223
fun getTypeArgumentLabel(
12281224
arg: IrTypeArgument
@@ -1464,7 +1460,7 @@ open class KotlinUsesExtractor(
14641460
return t
14651461
}
14661462

1467-
fun eraseTypeParameter(t: IrTypeParameter) =
1463+
private fun eraseTypeParameter(t: IrTypeParameter) =
14681464
erase(t.superTypes[0])
14691465

14701466
/**

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package com.github.codeql
22

33
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
4+
import org.jetbrains.kotlin.builtins.StandardNames
45
import org.jetbrains.kotlin.ir.declarations.IrClass
6+
import org.jetbrains.kotlin.ir.declarations.IrPackageFragment
57
import org.jetbrains.kotlin.ir.types.IrSimpleType
6-
import org.jetbrains.kotlin.ir.types.IdSignatureValues
7-
import org.jetbrains.kotlin.ir.util.IdSignature
8+
import org.jetbrains.kotlin.ir.types.classOrNull
89
import org.jetbrains.kotlin.name.FqName
910

1011
class PrimitiveTypeMapping(val logger: Logger, val pluginContext: IrPluginContext) {
11-
fun getPrimitiveInfo(s: IrSimpleType) = mapping[s.classifier.signature]
12+
fun getPrimitiveInfo(s: IrSimpleType) =
13+
s.classOrNull?.let {
14+
if ((it.owner.parent as? IrPackageFragment)?.fqName == StandardNames.BUILT_INS_PACKAGE_FQ_NAME)
15+
mapping[it.owner.name]
16+
else
17+
null
18+
}
1219

1320
data class PrimitiveTypeInfo(
1421
val primitiveName: String?,
@@ -60,25 +67,25 @@ class PrimitiveTypeMapping(val logger: Logger, val pluginContext: IrPluginContex
6067
val javaLangVoid = findClass("java.lang.Void", kotlinNothing)
6168

6269
mapOf(
63-
IdSignatureValues._byte to PrimitiveTypeInfo("byte", true, javaLangByte, "kotlin", "Byte"),
64-
IdSignatureValues._short to PrimitiveTypeInfo("short", true, javaLangShort, "kotlin", "Short"),
65-
IdSignatureValues._int to PrimitiveTypeInfo("int", true, javaLangInteger, "kotlin", "Int"),
66-
IdSignatureValues._long to PrimitiveTypeInfo("long", true, javaLangLong, "kotlin", "Long"),
70+
StandardNames.FqNames._byte.shortName() to PrimitiveTypeInfo("byte", true, javaLangByte, "kotlin", "Byte"),
71+
StandardNames.FqNames._short.shortName() to PrimitiveTypeInfo("short", true, javaLangShort, "kotlin", "Short"),
72+
StandardNames.FqNames._int.shortName() to PrimitiveTypeInfo("int", true, javaLangInteger, "kotlin", "Int"),
73+
StandardNames.FqNames._long.shortName() to PrimitiveTypeInfo("long", true, javaLangLong, "kotlin", "Long"),
6774

68-
IdSignatureValues.uByte to PrimitiveTypeInfo("byte", true, kotlinUByte, "kotlin", "UByte"),
69-
IdSignatureValues.uShort to PrimitiveTypeInfo("short", true, kotlinUShort, "kotlin", "UShort"),
70-
IdSignatureValues.uInt to PrimitiveTypeInfo("int", true, kotlinUInt, "kotlin", "UInt"),
71-
IdSignatureValues.uLong to PrimitiveTypeInfo("long", true, kotlinULong, "kotlin", "ULong"),
75+
StandardNames.FqNames.uByteFqName.shortName() to PrimitiveTypeInfo("byte", true, kotlinUByte, "kotlin", "UByte"),
76+
StandardNames.FqNames.uShortFqName.shortName() to PrimitiveTypeInfo("short", true, kotlinUShort, "kotlin", "UShort"),
77+
StandardNames.FqNames.uIntFqName.shortName() to PrimitiveTypeInfo("int", true, kotlinUInt, "kotlin", "UInt"),
78+
StandardNames.FqNames.uLongFqName.shortName() to PrimitiveTypeInfo("long", true, kotlinULong, "kotlin", "ULong"),
7279

73-
IdSignatureValues._double to PrimitiveTypeInfo("double", true, javaLangDouble, "kotlin", "Double"),
74-
IdSignatureValues._float to PrimitiveTypeInfo("float", true, javaLangFloat, "kotlin", "Float"),
80+
StandardNames.FqNames._double.shortName() to PrimitiveTypeInfo("double", true, javaLangDouble, "kotlin", "Double"),
81+
StandardNames.FqNames._float.shortName() to PrimitiveTypeInfo("float", true, javaLangFloat, "kotlin", "Float"),
7582

76-
IdSignatureValues._boolean to PrimitiveTypeInfo("boolean", true, javaLangBoolean, "kotlin", "Boolean"),
83+
StandardNames.FqNames._boolean.shortName() to PrimitiveTypeInfo("boolean", true, javaLangBoolean, "kotlin", "Boolean"),
7784

78-
IdSignatureValues._char to PrimitiveTypeInfo("char", true, javaLangCharacter, "kotlin", "Char"),
85+
StandardNames.FqNames._char.shortName() to PrimitiveTypeInfo("char", true, javaLangCharacter, "kotlin", "Char"),
7986

80-
IdSignatureValues.unit to PrimitiveTypeInfo("void", false, kotlinUnit, "kotlin", "Unit"),
81-
IdSignatureValues.nothing to PrimitiveTypeInfo(null, true, javaLangVoid, "kotlin", "Nothing"),
87+
StandardNames.FqNames.unit.shortName() to PrimitiveTypeInfo("void", false, kotlinUnit, "kotlin", "Unit"),
88+
StandardNames.FqNames.nothing.shortName() to PrimitiveTypeInfo(null, true, javaLangVoid, "kotlin", "Nothing"),
8289
)
8390
}()
8491
}

java/kotlin-extractor/src/main/kotlin/utils/AutoCloseableUse.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ fun AutoCloseable?.closeFinallyAC(cause: Throwable?) = when {
4040
} catch (closeException: Throwable) {
4141
cause.addSuppressed(closeException)
4242
}
43-
}
43+
}

0 commit comments

Comments
 (0)