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.kotlin
19 
20 import com.google.devtools.ksp.KSObjectCache
21 import com.google.devtools.ksp.symbol.KSAnnotation
22 import com.google.devtools.ksp.symbol.KSDeclaration
23 import com.google.devtools.ksp.symbol.KSNode
24 import com.google.devtools.ksp.symbol.KSReferenceElement
25 import com.google.devtools.ksp.symbol.KSType
26 import com.google.devtools.ksp.symbol.KSTypeReference
27 import com.google.devtools.ksp.symbol.KSVisitor
28 import com.google.devtools.ksp.symbol.Location
29 import com.google.devtools.ksp.symbol.Modifier
30 import com.google.devtools.ksp.symbol.NonExistLocation
31 import com.google.devtools.ksp.symbol.Origin
32 import com.google.devtools.ksp.symbol.impl.getInstanceForCurrentRound
33 
34 class KSTypeReferenceDeferredImpl private constructor(
35     private val resolver: () -> KSType,
36     override val parent: KSNode?
37 ) : KSTypeReference {
38     companion object : KSObjectCache<KSDeclaration, KSTypeReferenceDeferredImpl>() {
getCachednull39         fun getCached(parent: KSDeclaration, resolver: () -> KSType): KSTypeReferenceDeferredImpl {
40             val currentParent = parent.getInstanceForCurrentRound() as KSDeclaration
41             return cache
42                 .getOrPut(currentParent) { KSTypeReferenceDeferredImpl(resolver, currentParent) }
43         }
44     }
45 
46     override val origin = Origin.KOTLIN
47 
48     override val location: Location = NonExistLocation
49 
50     override val annotations: Sequence<KSAnnotation> = emptySequence()
51 
52     override val element: KSReferenceElement? = null
53 
54     override val modifiers: Set<Modifier> = emptySet()
55 
<lambda>null56     private val resolved: KSType by lazy {
57         resolver()
58     }
59 
resolvenull60     override fun resolve(): KSType = resolved
61 
62     override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
63         return visitor.visitTypeReference(this, data)
64     }
65 
toStringnull66     override fun toString(): String {
67         return resolved.toString()
68     }
69 }
70