[Wasm] Port WasmImport/WasmExport checker to K2 (KT-56849)

This commit is contained in:
Svyatoslav Kuzmich
2023-11-14 12:24:10 +00:00
committed by Space Team
parent 28895a2613
commit 1c230c8f27
14 changed files with 205 additions and 128 deletions
@@ -30,8 +30,10 @@ public interface ErrorsWasm {
DiagnosticFactory0<PsiElement> WASM_IMPORT_ON_NON_EXTERNAL_DECLARATION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> WASM_IMPORT_EXPORT_PARAMETER_DEFAULT_VALUE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> WASM_IMPORT_EXPORT_VARARG_PARAMETER = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, KotlinType> WASM_IMPORT_EXPORT_UNSUPPORTED_PARAMETER_TYPE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, KotlinType> WASM_IMPORT_EXPORT_UNSUPPORTED_RETURN_TYPE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, KotlinType> WASM_IMPORT_EXPORT_UNSUPPORTED_PARAMETER_TYPE =
DiagnosticFactory1.create(ERROR, PositioningStrategies.DECLARATION_SIGNATURE_OR_DEFAULT);
DiagnosticFactory1<PsiElement, KotlinType> WASM_IMPORT_EXPORT_UNSUPPORTED_RETURN_TYPE =
DiagnosticFactory1.create(ERROR, PositioningStrategies.DECLARATION_SIGNATURE_OR_DEFAULT);
DiagnosticFactory0<PsiElement> WRONG_JS_FUN_TARGET = DiagnosticFactory0.create(ERROR);
@@ -14,16 +14,18 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.checkers.DeclarationChecker
import org.jetbrains.kotlin.resolve.checkers.DeclarationCheckerContext
import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyExternal
import org.jetbrains.kotlin.resolve.source.getPsi
import org.jetbrains.kotlin.wasm.util.hasValidJsCodeBody
// TODO: Implement in K2: KT-56849
abstract class WasmExportAnnotationChecker(val checkJsInterop: Boolean) : DeclarationChecker {
private val wasmExportFqName = FqName("kotlin.wasm.WasmExport")
private val jsExportFqName = FqName("kotlin.js.JsExport")
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
if (descriptor !is FunctionDescriptor) return
if (!descriptor.annotations.hasAnnotation(wasmExportFqName)) return
val wasmExport = descriptor.annotations.findAnnotation(wasmExportFqName) ?: return
val wasmExportPsi = wasmExport.source.getPsi() ?: declaration
val trace = context.trace
val bindingContext = trace.bindingContext
@@ -36,13 +38,11 @@ abstract class WasmExportAnnotationChecker(val checkJsInterop: Boolean) : Declar
}
if (descriptor.isEffectivelyExternal() || (checkJsInterop && descriptor.hasValidJsCodeBody(bindingContext))) {
val reportOn = descriptor.findPsi() ?: declaration
trace.report(ErrorsWasm.WASM_EXPORT_ON_EXTERNAL_DECLARATION.on(reportOn))
trace.report(ErrorsWasm.WASM_EXPORT_ON_EXTERNAL_DECLARATION.on(wasmExportPsi))
}
if (!DescriptorUtils.isTopLevelDeclaration(descriptor)) {
val reportOn = descriptor.findPsi() ?: declaration
trace.report(ErrorsWasm.NESTED_WASM_EXPORT.on(reportOn))
trace.report(ErrorsWasm.NESTED_WASM_EXPORT.on(wasmExportPsi))
}
WasmImportAnnotationChecker.checkSignatureIsPrimitive(descriptor, trace, declaration)
@@ -32,7 +32,6 @@ import org.jetbrains.kotlin.types.typeUtil.isBoolean
import org.jetbrains.kotlin.types.typeUtil.isPrimitiveNumberType
import org.jetbrains.kotlin.types.typeUtil.isUnit
// TODO: Implement in K2: KT-56849
object WasmImportAnnotationChecker : DeclarationChecker {
private val wasmImportFqName = FqName("kotlin.wasm.WasmImport")