aboutsummaryrefslogtreecommitdiff
path: root/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSExpectActualImpl.kt
diff options
context:
space:
mode:
Diffstat (limited to 'compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSExpectActualImpl.kt')
-rw-r--r--compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSExpectActualImpl.kt64
1 files changed, 64 insertions, 0 deletions
diff --git a/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSExpectActualImpl.kt b/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSExpectActualImpl.kt
new file mode 100644
index 00000000..18c3f9c5
--- /dev/null
+++ b/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/kotlin/KSExpectActualImpl.kt
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2020 Google LLC
+ * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.devtools.ksp.symbol.impl.kotlin
+
+import com.google.devtools.ksp.processing.impl.ResolverImpl
+import com.google.devtools.ksp.processing.impl.findActualsInKSDeclaration
+import com.google.devtools.ksp.processing.impl.findExpectsInKSDeclaration
+import com.google.devtools.ksp.symbol.KSDeclaration
+import com.google.devtools.ksp.symbol.KSExpectActual
+import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
+import org.jetbrains.kotlin.psi.KtDeclaration
+import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
+import org.jetbrains.kotlin.psi.psiUtil.hasActualModifier
+import org.jetbrains.kotlin.psi.psiUtil.hasExpectModifier
+
+class KSExpectActualImpl(val declaration: KtDeclaration) : KSExpectActual {
+ /**
+ * "All actual declarations that match any part of an expected declaration need to be marked as actual."
+ */
+ override val isActual: Boolean = declaration.hasActualModifier()
+
+ private fun KtDeclaration.isExpect(): Boolean = hasExpectModifier() || containingClassOrObject?.isExpect() == true
+
+ override val isExpect: Boolean = declaration.isExpect()
+
+ private val expects: Sequence<KSDeclaration> by lazy {
+ descriptor?.findExpectsInKSDeclaration() ?: emptySequence()
+ }
+
+ override fun findExpects(): Sequence<KSDeclaration> {
+ if (!isActual)
+ return emptySequence()
+ return expects
+ }
+
+ private val actuals: Sequence<KSDeclaration> by lazy {
+ descriptor?.findActualsInKSDeclaration() ?: emptySequence()
+ }
+
+ override fun findActuals(): Sequence<KSDeclaration> {
+ if (!isExpect)
+ return emptySequence()
+ return actuals
+ }
+
+ private val descriptor: DeclarationDescriptor? by lazy {
+ ResolverImpl.instance!!.resolveDeclaration(declaration)
+ }
+}