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.IdKeyTriple
21 import com.google.devtools.ksp.KSObjectCache
22 import com.google.devtools.ksp.memoized
23 import com.google.devtools.ksp.symbol.KSAnnotation
24 import com.google.devtools.ksp.symbol.KSNode
25 import com.google.devtools.ksp.symbol.KSReferenceElement
26 import com.google.devtools.ksp.symbol.KSType
27 import com.google.devtools.ksp.symbol.KSTypeReference
28 import com.google.devtools.ksp.symbol.KSVisitor
29 import com.google.devtools.ksp.symbol.Location
30 import com.google.devtools.ksp.symbol.Modifier
31 import com.google.devtools.ksp.symbol.NonExistLocation
32 import com.google.devtools.ksp.symbol.Origin
33 import com.google.devtools.ksp.symbol.impl.kotlin.getKSTypeCached
34 import org.jetbrains.kotlin.builtins.isSuspendFunctionTypeOrSubtype
35 import org.jetbrains.kotlin.types.AbbreviatedType
36 import org.jetbrains.kotlin.types.KotlinType
37 
38 class KSTypeReferenceDescriptorImpl private constructor(
39     val kotlinType: KotlinType,
40     override val origin: Origin,
41     override val parent: KSNode?
42 ) : KSTypeReference {
43     companion object : KSObjectCache<IdKeyTriple<KotlinType, Origin, KSNode?>, KSTypeReferenceDescriptorImpl>() {
getCachednull44         fun getCached(kotlinType: KotlinType, origin: Origin, parent: KSNode?) = cache
45             .getOrPut(IdKeyTriple(kotlinType, origin, parent)) {
46                 KSTypeReferenceDescriptorImpl(kotlinType, origin, parent)
47             }
48     }
49 
KotlinTypenull50     private fun KotlinType.findAlias(): KotlinType =
51         (kotlinType as? AbbreviatedType)?.abbreviation ?: this
52 
53     override val location: Location = NonExistLocation
54 
55     override val element: KSReferenceElement by lazy {
56         KSClassifierReferenceDescriptorImpl.getCached(kotlinType.findAlias(), origin, this)
57     }
58 
<lambda>null59     override val annotations: Sequence<KSAnnotation> by lazy {
60         kotlinType.annotations.asSequence().map { KSAnnotationDescriptorImpl.getCached(it, this) }.memoized()
61     }
62 
<lambda>null63     override val modifiers: Set<Modifier> by lazy {
64         if (kotlinType.isSuspendFunctionTypeOrSubtype) {
65             setOf(Modifier.SUSPEND)
66         } else {
67             emptySet<Modifier>()
68         }
69     }
70 
resolvenull71     override fun resolve(): KSType {
72         return getKSTypeCached(kotlinType)
73     }
74 
acceptnull75     override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
76         return visitor.visitTypeReference(this, data)
77     }
78 
toStringnull79     override fun toString(): String {
80         return element.toString()
81     }
82 }
83