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.processing.impl.KSNameImpl
22 import com.google.devtools.ksp.symbol.*
23 import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
24 import org.jetbrains.kotlin.resolve.calls.components.hasDefaultValue
25 import org.jetbrains.kotlin.resolve.calls.components.isVararg
26 
27 class KSValueParameterDescriptorImpl private constructor(
28     val descriptor: ValueParameterDescriptor,
29     override val parent: KSNode?
30 ) : KSValueParameter {
31     companion object : KSObjectCache<Pair<ValueParameterDescriptor, KSNode?>, KSValueParameterDescriptorImpl>() {
getCachednull32         fun getCached(descriptor: ValueParameterDescriptor, parent: KSNode?) = cache
33             .getOrPut(Pair(descriptor, parent)) { KSValueParameterDescriptorImpl(descriptor, parent) }
34     }
35 
<lambda>null36     override val origin by lazy {
37         descriptor.origin
38     }
39 
40     override val location: Location = NonExistLocation
41 
<lambda>null42     override val annotations: Sequence<KSAnnotation> by lazy {
43         descriptor.annotations.asSequence().map { KSAnnotationDescriptorImpl.getCached(it, this) }
44     }
45 
46     override val isCrossInline: Boolean = descriptor.isCrossinline
47 
48     override val isNoInline: Boolean = descriptor.isNoinline
49 
50     override val isVararg: Boolean = descriptor.isVararg
51 
52     override val isVal: Boolean = !descriptor.isVar
53 
54     override val isVar: Boolean = descriptor.isVar
55 
<lambda>null56     override val name: KSName? by lazy {
57         KSNameImpl.getCached(descriptor.name.asString())
58     }
59 
<lambda>null60     override val type: KSTypeReference by lazy {
61         // Descriptor wraps vararg with Array<>, to align with the actual behavior in source.
62         if (isVararg) {
63             KSTypeReferenceDescriptorImpl.getCached(descriptor.varargElementType!!, origin, this)
64         } else {
65             KSTypeReferenceDescriptorImpl.getCached(descriptor.type, origin, this)
66         }
67     }
68 
69     override val hasDefault: Boolean = descriptor.hasDefaultValue()
70 
acceptnull71     override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
72         return visitor.visitValueParameter(this, data)
73     }
74 
toStringnull75     override fun toString(): String {
76         return name?.asString() ?: "_"
77     }
78 }
79