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.memoized
21 import com.google.devtools.ksp.processing.impl.KSNameImpl
22 import com.google.devtools.ksp.symbol.*
23 import org.jetbrains.kotlin.backend.common.serialization.findPackage
24 import org.jetbrains.kotlin.descriptors.ClassDescriptor
25 import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
26 import org.jetbrains.kotlin.descriptors.FunctionDescriptor
27 import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor
28 import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
29 import org.jetbrains.kotlin.resolve.descriptorUtil.parents
30 import org.jetbrains.kotlin.resolve.descriptorUtil.parentsWithSelf
31 import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
32 
33 abstract class KSDeclarationDescriptorImpl(private val descriptor: DeclarationDescriptor) : KSDeclaration {
34 
<lambda>null35     override val origin by lazy {
36         descriptor.origin
37     }
38 
39     override val containingFile: KSFile? = null
40 
41     override val location: Location = NonExistLocation
42 
<lambda>null43     override val annotations: Sequence<KSAnnotation> by lazy {
44         descriptor.annotations.asSequence().map { KSAnnotationDescriptorImpl.getCached(it, this) }.memoized()
45     }
46 
<lambda>null47     override val parentDeclaration: KSDeclaration? by lazy {
48         val containingDescriptor = descriptor.parents.first()
49         when (containingDescriptor) {
50             is ClassDescriptor -> KSClassDeclarationDescriptorImpl.getCached(containingDescriptor)
51             is FunctionDescriptor -> KSFunctionDeclarationDescriptorImpl.getCached(containingDescriptor)
52             else -> null
53         } as KSDeclaration?
54     }
55 
<lambda>null56     override val parent: KSNode? by lazy {
57         parentDeclaration
58     }
59 
<lambda>null60     override val packageName: KSName by lazy {
61         KSNameImpl.getCached(descriptor.findPackage().fqName.asString())
62     }
63 
<lambda>null64     override val qualifiedName: KSName by lazy {
65         KSNameImpl.getCached(descriptor.fqNameSafe.asString())
66     }
67 
<lambda>null68     override val simpleName: KSName by lazy {
69         KSNameImpl.getCached(descriptor.name.asString())
70     }
71 
toStringnull72     override fun toString(): String {
73         return this.simpleName.asString()
74     }
75 
76     override val docString = null
77 }
78 
79 val DeclarationDescriptor.origin: Origin
80     get() = if (parentsWithSelf.firstIsInstanceOrNull<JavaClassDescriptor>() != null) {
81         Origin.JAVA_LIB
82     } else {
83         Origin.KOTLIN_LIB
84     }
85