1 /*
<lambda>null2  * Copyright 2020 Google LLC
3  * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.google.devtools.ksp.symbol.impl.kotlin
19 
20 import com.google.devtools.ksp.KSObjectCache
21 import com.google.devtools.ksp.processing.impl.ResolverImpl
22 import com.google.devtools.ksp.symbol.*
23 import com.google.devtools.ksp.symbol.impl.*
24 import com.google.devtools.ksp.symbol.impl.binary.KSPropertyGetterDescriptorImpl
25 import com.google.devtools.ksp.symbol.impl.binary.KSPropertySetterDescriptorImpl
26 import org.jetbrains.kotlin.descriptors.PropertyDescriptor
27 import org.jetbrains.kotlin.descriptors.VariableDescriptorWithAccessors
28 import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
29 import org.jetbrains.kotlin.lexer.KtTokens
30 import org.jetbrains.kotlin.psi.KtAnnotated
31 import org.jetbrains.kotlin.psi.KtAnnotationEntry
32 import org.jetbrains.kotlin.psi.KtProperty
33 import org.jetbrains.kotlin.psi.psiUtil.isExtensionDeclaration
34 import org.jetbrains.kotlin.resolve.BindingContext
35 
36 class KSPropertyDeclarationImpl private constructor(val ktProperty: KtProperty) :
37     KSPropertyDeclaration,
38     KSDeclarationImpl(ktProperty),
39     KSExpectActual by KSExpectActualImpl(ktProperty) {
40     companion object : KSObjectCache<KtProperty, KSPropertyDeclarationImpl>() {
41         fun getCached(ktProperty: KtProperty) = cache.getOrPut(ktProperty) { KSPropertyDeclarationImpl(ktProperty) }
42     }
43 
44     private val propertyDescriptor by lazy {
45         ResolverImpl.instance!!.resolveDeclaration(ktProperty) as? PropertyDescriptor
46     }
47 
48     override val annotations: Sequence<KSAnnotation> by lazy {
49         ktProperty.filterUseSiteTargetAnnotations().map { KSAnnotationImpl.getCached(it) }
50     }
51 
52     override val extensionReceiver: KSTypeReference? by lazy {
53         if (ktProperty.isExtensionDeclaration()) {
54             KSTypeReferenceImpl.getCached(ktProperty.receiverTypeReference!!)
55         } else {
56             null
57         }
58     }
59 
60     override val isMutable: Boolean by lazy {
61         ktProperty.isVar
62     }
63 
64     override val hasBackingField: Boolean by lazy {
65         // taken from: https://github.com/JetBrains/kotlin/blob/master/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightMembersCreator.kt#L104
66         when {
67             ktProperty.initializer != null -> true
68             ktProperty.hasModifier(KtTokens.LATEINIT_KEYWORD) -> true
69             else -> {
70                 val context = ResolverImpl.instance!!.bindingTrace.bindingContext
71                 val descriptor = ResolverImpl.instance!!.resolveDeclaration(ktProperty)
72                 descriptor is PropertyDescriptor && context[BindingContext.BACKING_FIELD_REQUIRED, descriptor] == true
73             }
74         }
75     }
76 
77     override val getter: KSPropertyGetter? by lazy {
78         ktProperty.getter?.let {
79             KSPropertyGetterImpl.getCached(it)
80         } ?: propertyDescriptor?.getter?.let {
81             KSPropertyGetterDescriptorImpl.getCached(it)
82         }
83     }
84 
85     override val setter: KSPropertySetter? by lazy {
86         ktProperty.setter?.let {
87             KSPropertySetterImpl.getCached(it)
88         } ?: propertyDescriptor?.setter?.let {
89             KSPropertySetterDescriptorImpl.getCached(it)
90         }
91     }
92 
93     override val type: KSTypeReference by lazy {
94         if (ktProperty.typeReference != null) {
95             KSTypeReferenceImpl.getCached(ktProperty.typeReference!!)
96         } else {
97             KSTypeReferenceDeferredImpl.getCached(this) {
98                 val desc = propertyDescriptor as? VariableDescriptorWithAccessors
99                 if (desc == null) {
100                     KSErrorType
101                 } else {
102                     getKSTypeCached(desc.type)
103                 }
104             }
105         }
106     }
107 
108     override fun isDelegated(): Boolean = ktProperty.hasDelegate()
109 
110     override fun findOverridee(): KSPropertyDeclaration? {
111         return propertyDescriptor?.findClosestOverridee()?.toKSPropertyDeclaration()
112     }
113 
114     override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
115         return visitor.visitPropertyDeclaration(this, data)
116     }
117 
118     override fun asMemberOf(containing: KSType): KSType =
119         ResolverImpl.instance!!.asMemberOf(this, containing)
120 }
121 
filterUseSiteTargetAnnotationsnull122 internal fun KtAnnotated.filterUseSiteTargetAnnotations(): Sequence<KtAnnotationEntry> {
123     return this.annotationEntries.asSequence().filter { property ->
124         property.useSiteTarget?.getAnnotationUseSiteTarget()?.let {
125             it != AnnotationUseSiteTarget.PROPERTY_GETTER && it != AnnotationUseSiteTarget.PROPERTY_SETTER &&
126                 it != AnnotationUseSiteTarget.SETTER_PARAMETER && it != AnnotationUseSiteTarget.CONSTRUCTOR_PARAMETER
127         } ?: true
128     }
129 }
130