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.isPrivate
22 import com.google.devtools.ksp.processing.impl.ResolverImpl
23 import com.google.devtools.ksp.symbol.*
24 import com.google.devtools.ksp.symbol.impl.*
25 import com.google.devtools.ksp.symbol.impl.synthetic.KSPropertyGetterSyntheticImpl
26 import com.google.devtools.ksp.symbol.impl.synthetic.KSPropertySetterSyntheticImpl
27 import org.jetbrains.kotlin.psi.KtParameter
28 
29 class KSPropertyDeclarationParameterImpl private constructor(val ktParameter: KtParameter) :
30     KSPropertyDeclaration,
31     KSDeclarationImpl(ktParameter),
32     KSExpectActual by KSExpectActualImpl(ktParameter) {
33     companion object : KSObjectCache<KtParameter, KSPropertyDeclarationParameterImpl>() {
34         fun getCached(ktParameter: KtParameter) = cache.getOrPut(ktParameter) {
35             KSPropertyDeclarationParameterImpl(ktParameter)
36         }
37     }
38 
39     override val annotations: Sequence<KSAnnotation> by lazy {
40         ktParameter.filterUseSiteTargetAnnotations().map { KSAnnotationImpl.getCached(it) }
41             .filterNot { valueParameterAnnotation ->
42                 valueParameterAnnotation.useSiteTarget == AnnotationUseSiteTarget.PARAM ||
43                     (
44                         valueParameterAnnotation.annotationType.resolve()
45                             .declaration.annotations.any { metaAnnotation ->
46                                 metaAnnotation.annotationType.resolve().declaration.qualifiedName
47                                     ?.asString() == "kotlin.annotation.Target" &&
48                                     (metaAnnotation.arguments.singleOrNull()?.value as? ArrayList<*>)?.any {
49                                     (it as? KSType)?.declaration?.qualifiedName
50                                         ?.asString() == "kotlin.annotation.AnnotationTarget.VALUE_PARAMETER"
51                                 } ?: false
52                             } && valueParameterAnnotation.useSiteTarget == null
53                         )
54             }
55     }
56 
57     override val parentDeclaration: KSDeclaration? by lazy {
58         ktParameter.findParentDeclaration()!!.parentDeclaration
59     }
60 
61     override val hasBackingField: Boolean
62         get() = true
63 
64     override val extensionReceiver: KSTypeReference? = null
65 
66     override val isMutable: Boolean by lazy {
67         ktParameter.isMutable
68     }
69 
70     override val getter: KSPropertyGetter? by lazy {
71         if (this.isPrivate()) {
72             null
73         } else {
74             KSPropertyGetterSyntheticImpl.getCached(this)
75         }
76     }
77 
78     override val setter: KSPropertySetter? by lazy {
79         if (ktParameter.isMutable && !this.isPrivate()) {
80             KSPropertySetterSyntheticImpl.getCached(this)
81         } else {
82             null
83         }
84     }
85 
86     override val type: KSTypeReference by lazy {
87         if (ktParameter.typeReference != null) {
88             KSTypeReferenceImpl.getCached(ktParameter.typeReference!!)
89         } else {
90             throw IllegalStateException("properties in parameter must have explicit type")
91         }
92     }
93 
94     override fun isDelegated(): Boolean = false
95 
96     override fun findOverridee(): KSPropertyDeclaration? {
97         return ResolverImpl.instance!!.resolvePropertyDeclaration(this)?.original
98             ?.findClosestOverridee()?.toKSPropertyDeclaration()
99     }
100 
101     override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
102         return visitor.visitPropertyDeclaration(this, data)
103     }
104 
105     override fun asMemberOf(containing: KSType): KSType =
106         ResolverImpl.instance!!.asMemberOf(this, containing)
107 }
108