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.getDocString
21 import com.google.devtools.ksp.isConstructor
22 import com.google.devtools.ksp.memoized
23 import com.google.devtools.ksp.processing.impl.KSNameImpl
24 import com.google.devtools.ksp.symbol.*
25 import com.google.devtools.ksp.symbol.impl.findParentAnnotated
26 import com.google.devtools.ksp.symbol.impl.findParentDeclaration
27 import com.google.devtools.ksp.symbol.impl.toLocation
28 import com.google.devtools.ksp.toKSModifiers
29 import org.jetbrains.kotlin.psi.*
30 
31 abstract class KSDeclarationImpl(val ktDeclaration: KtDeclaration) : KSDeclaration {
32     override val origin: Origin = Origin.KOTLIN
33 
<lambda>null34     override val location: Location by lazy {
35         ktDeclaration.toLocation()
36     }
37 
<lambda>null38     override val simpleName: KSName by lazy {
39         KSNameImpl.getCached(ktDeclaration.name!!)
40     }
41 
<lambda>null42     override val qualifiedName: KSName? by lazy {
43         (ktDeclaration as? KtNamedDeclaration)?.fqName?.let { KSNameImpl.getCached(it.asString()) }
44     }
45 
<lambda>null46     override val annotations: Sequence<KSAnnotation> by lazy {
47         ktDeclaration.annotationEntries.asSequence().map { KSAnnotationImpl.getCached(it) }.memoized()
48     }
49 
<lambda>null50     override val modifiers: Set<Modifier> by lazy {
51         // we do not check for JVM_STATIC here intentionally as it actually means static in parent class,
52         // not in this class.
53         // see: https://github.com/google/ksp/issues/378
54         if (this is KSFunctionDeclaration && this.isConstructor() &&
55             (this.parentDeclaration as? KSClassDeclaration)?.classKind == ClassKind.ENUM_CLASS
56         ) {
57             setOf(Modifier.FINAL, Modifier.PRIVATE)
58         } else {
59             ktDeclaration.toKSModifiers()
60         }
61     }
62 
<lambda>null63     override val containingFile: KSFile? by lazy {
64         KSFileImpl.getCached(ktDeclaration.containingKtFile)
65     }
66 
<lambda>null67     override val packageName: KSName by lazy {
68         this.containingFile!!.packageName
69     }
70 
<lambda>null71     override val typeParameters: List<KSTypeParameter> by lazy {
72         (ktDeclaration as? KtTypeParameterListOwner)?.let {
73             it.typeParameters.map { KSTypeParameterImpl.getCached(it) }
74         } ?: emptyList()
75     }
76 
<lambda>null77     override val parentDeclaration: KSDeclaration? by lazy {
78         ktDeclaration.findParentDeclaration()
79     }
80 
<lambda>null81     override val parent: KSNode? by lazy {
82         ktDeclaration.findParentAnnotated()
83     }
84 
toStringnull85     override fun toString(): String {
86         return this.simpleName.asString()
87     }
88 
<lambda>null89     internal val originalAnnotations: List<KSAnnotation> by lazy {
90         ktDeclaration.annotationEntries.map { KSAnnotationImpl.getCached(it) }
91     }
92 
<lambda>null93     override val docString by lazy {
94         ktDeclaration.getDocString()
95     }
96 }
97