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.synthetic
19 
20 import com.google.devtools.ksp.KSObjectCache
21 import com.google.devtools.ksp.isPublic
22 import com.google.devtools.ksp.processing.impl.KSNameImpl
23 import com.google.devtools.ksp.processing.impl.ResolverImpl
24 import com.google.devtools.ksp.symbol.*
25 
26 class KSConstructorSyntheticImpl private constructor(val ksClassDeclaration: KSClassDeclaration) :
27     KSFunctionDeclaration,
28     KSDeclaration
29     by ksClassDeclaration {
30     companion object : KSObjectCache<KSClassDeclaration, KSConstructorSyntheticImpl>() {
getCachednull31         fun getCached(ksClassDeclaration: KSClassDeclaration) =
32             KSConstructorSyntheticImpl.cache.getOrPut(ksClassDeclaration) {
33                 KSConstructorSyntheticImpl(ksClassDeclaration)
34             }
35     }
36 
37     override val isAbstract: Boolean = false
38 
39     override val extensionReceiver: KSTypeReference? = null
40 
41     override val parameters: List<KSValueParameter> = emptyList()
42 
43     override val functionKind: FunctionKind = FunctionKind.MEMBER
44 
<lambda>null45     override val qualifiedName: KSName? by lazy {
46         KSNameImpl.getCached(ksClassDeclaration.qualifiedName?.asString()?.plus(".<init>") ?: "")
47     }
48 
<lambda>null49     override val simpleName: KSName by lazy {
50         KSNameImpl.getCached("<init>")
51     }
52 
53     override val typeParameters: List<KSTypeParameter> = emptyList()
54 
<lambda>null55     override val containingFile: KSFile? by lazy {
56         ksClassDeclaration.containingFile
57     }
58 
<lambda>null59     override val parentDeclaration: KSDeclaration? by lazy {
60         ksClassDeclaration
61     }
62 
<lambda>null63     override val parent: KSNode? by lazy {
64         parentDeclaration
65     }
66 
<lambda>null67     override val returnType: KSTypeReference by lazy {
68         KSTypeReferenceSyntheticImpl(
69             ksClassDeclaration.asStarProjectedType(), this
70         )
71     }
72 
73     override val annotations: Sequence<KSAnnotation> = emptySequence()
74 
75     override val isActual: Boolean = false
76 
77     override val isExpect: Boolean = false
78 
79     override val declarations: Sequence<KSDeclaration> = emptySequence()
80 
<lambda>null81     override val location: Location by lazy {
82         ksClassDeclaration.location
83     }
84 
<lambda>null85     override val modifiers: Set<Modifier> by lazy {
86         if (ksClassDeclaration.classKind == ClassKind.ENUM_CLASS) {
87             return@lazy setOf(Modifier.FINAL, Modifier.PRIVATE)
88         }
89         // add public if parent class is public
90         if (ksClassDeclaration.isPublic()) {
91             setOf(Modifier.FINAL, Modifier.PUBLIC)
92         } else {
93             setOf(Modifier.FINAL)
94         }
95     }
96 
97     override val origin: Origin = Origin.SYNTHETIC
98 
findOverrideenull99     override fun findOverridee(): KSFunctionDeclaration? = null
100 
101     override fun findActuals(): Sequence<KSDeclaration> {
102         return emptySequence()
103     }
104 
findExpectsnull105     override fun findExpects(): Sequence<KSDeclaration> {
106         return emptySequence()
107     }
108 
acceptnull109     override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
110         return visitor.visitFunctionDeclaration(this, data)
111     }
112 
toStringnull113     override fun toString(): String {
114         return "synthetic constructor for ${this.parentDeclaration}"
115     }
116 
asMemberOfnull117     override fun asMemberOf(containing: KSType): KSFunction =
118         ResolverImpl.instance!!.asMemberOf(this, containing)
119 }
120