1 /*
2  * 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.binary
19 
20 import com.google.devtools.ksp.KSObjectCache
21 import com.google.devtools.ksp.hasBackingFieldWithBinaryClassSupport
22 import com.google.devtools.ksp.memoized
23 import com.google.devtools.ksp.processing.impl.ResolverImpl
24 import com.google.devtools.ksp.symbol.*
25 import com.google.devtools.ksp.symbol.impl.*
26 import com.google.devtools.ksp.toKSModifiers
27 import org.jetbrains.kotlin.descriptors.*
28 import org.jetbrains.org.objectweb.asm.Opcodes
29 
30 class KSPropertyDeclarationDescriptorImpl private constructor(val descriptor: PropertyDescriptor) :
31     KSPropertyDeclaration,
32     KSDeclarationDescriptorImpl(descriptor),
33     KSExpectActual by KSExpectActualDescriptorImpl(descriptor) {
34     companion object : KSObjectCache<PropertyDescriptor, KSPropertyDeclarationDescriptorImpl>() {
<lambda>null35         fun getCached(descriptor: PropertyDescriptor) = cache.getOrPut(descriptor) {
36             KSPropertyDeclarationDescriptorImpl(descriptor)
37         }
38     }
39 
<lambda>null40     override val extensionReceiver: KSTypeReference? by lazy {
41         if (descriptor.extensionReceiverParameter != null) {
42             KSTypeReferenceDescriptorImpl.getCached(descriptor.extensionReceiverParameter!!.type, origin, this)
43         } else {
44             null
45         }
46     }
47 
<lambda>null48     override val annotations: Sequence<KSAnnotation> by lazy {
49         // annotations on backing field will not visible in the property declaration so we query it directly to load
50         // its annotations as well.
51         val backingFieldAnnotations = descriptor.backingField?.annotations?.map {
52             KSAnnotationDescriptorImpl.getCached(it, this)
53         }.orEmpty()
54         (super.annotations + backingFieldAnnotations).memoized()
55     }
56 
<lambda>null57     override val isMutable: Boolean by lazy {
58         descriptor.isVar
59     }
60 
<lambda>null61     override val modifiers: Set<Modifier> by lazy {
62         val modifiers = mutableSetOf<Modifier>()
63         modifiers.addAll(descriptor.toKSModifiers())
64         if (descriptor.isConst) {
65             modifiers.add(Modifier.CONST)
66         }
67         if (descriptor.isLateInit) {
68             modifiers.add(Modifier.LATEINIT)
69         }
70 
71         if (this.origin == Origin.JAVA_LIB) {
72             if (this.jvmAccessFlag and Opcodes.ACC_TRANSIENT != 0)
73                 modifiers.add(Modifier.JAVA_TRANSIENT)
74             if (this.jvmAccessFlag and Opcodes.ACC_VOLATILE != 0)
75                 modifiers.add(Modifier.JAVA_VOLATILE)
76         }
77 
78         modifiers
79     }
80 
<lambda>null81     override val setter: KSPropertySetter? by lazy {
82         if (descriptor.setter != null) {
83             KSPropertySetterDescriptorImpl.getCached(descriptor.setter as PropertySetterDescriptor)
84         } else {
85             null
86         }
87     }
88 
<lambda>null89     override val getter: KSPropertyGetter? by lazy {
90         if (descriptor.getter != null) {
91             KSPropertyGetterDescriptorImpl.getCached(descriptor.getter as PropertyGetterDescriptor)
92         } else {
93             null
94         }
95     }
96 
<lambda>null97     override val typeParameters: List<KSTypeParameter> by lazy {
98         descriptor.typeParameters.map { KSTypeParameterDescriptorImpl.getCached(it) }
99     }
100 
<lambda>null101     override val type: KSTypeReference by lazy {
102         KSTypeReferenceDescriptorImpl.getCached(descriptor.type, origin, this)
103     }
104 
<lambda>null105     override val hasBackingField: Boolean by lazy {
106         descriptor.hasBackingFieldWithBinaryClassSupport()
107     }
108 
findOverrideenull109     override fun findOverridee(): KSPropertyDeclaration? {
110         val propertyDescriptor = ResolverImpl.instance!!.resolvePropertyDeclaration(this)
111         return propertyDescriptor?.findClosestOverridee()?.toKSPropertyDeclaration()
112     }
113 
isDelegatednull114     override fun isDelegated(): Boolean {
115         return (descriptor as? PropertyDescriptor)?.delegateField != null
116     }
117 
acceptnull118     override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
119         return visitor.visitPropertyDeclaration(this, data)
120     }
121 
asMemberOfnull122     override fun asMemberOf(containing: KSType): KSType =
123         ResolverImpl.instance!!.asMemberOf(this, containing)
124 }
125