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.memoized 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.KSExpectActual 25 import com.google.devtools.ksp.symbol.KSName 26 import com.google.devtools.ksp.symbol.KSTypeParameter 27 import com.google.devtools.ksp.symbol.KSTypeReference 28 import com.google.devtools.ksp.symbol.KSVisitor 29 import com.google.devtools.ksp.symbol.Variance 30 import com.google.devtools.ksp.symbol.impl.synthetic.KSTypeReferenceSyntheticImpl 31 import org.jetbrains.kotlin.lexer.KtTokens 32 import org.jetbrains.kotlin.psi.KtTypeParameter 33 import org.jetbrains.kotlin.psi.KtTypeParameterListOwner 34 35 class KSTypeParameterImpl private constructor(val ktTypeParameter: KtTypeParameter) : 36 KSTypeParameter, 37 KSDeclarationImpl(ktTypeParameter), 38 KSExpectActual by KSExpectActualNoImpl() { 39 companion object : KSObjectCache<KtTypeParameter, KSTypeParameterImpl>() { getCachednull40 fun getCached(ktTypeParameter: KtTypeParameter) = 41 cache.getOrPut(ktTypeParameter) { KSTypeParameterImpl(ktTypeParameter) } 42 } 43 <lambda>null44 override val name: KSName by lazy { 45 KSNameImpl.getCached(ktTypeParameter.name!!) 46 } 47 <lambda>null48 override val isReified: Boolean by lazy { 49 ktTypeParameter.modifierList?.hasModifier(KtTokens.REIFIED_KEYWORD) ?: false 50 } 51 <lambda>null52 override val variance: Variance by lazy { 53 when { 54 ktTypeParameter.modifierList == null -> Variance.INVARIANT 55 ktTypeParameter.modifierList!!.hasModifier(KtTokens.OUT_KEYWORD) -> Variance.COVARIANT 56 ktTypeParameter.modifierList!!.hasModifier(KtTokens.IN_KEYWORD) -> Variance.CONTRAVARIANT 57 else -> Variance.INVARIANT 58 } 59 } 60 <lambda>null61 private val owner: KtTypeParameterListOwner by lazy { 62 (parentDeclaration as KSDeclarationImpl).ktDeclaration as KtTypeParameterListOwner 63 } 64 <lambda>null65 override val bounds: Sequence<KSTypeReference> by lazy { 66 val list = sequenceOf(ktTypeParameter.extendsBound) 67 list.plus( 68 owner.typeConstraints 69 .filter { 70 it.subjectTypeParameterName!!.getReferencedName() == ktTypeParameter.nameAsSafeName.asString() 71 } 72 .map { it.boundTypeReference } 73 ).filterNotNull().map { KSTypeReferenceImpl.getCached(it) }.ifEmpty { 74 sequenceOf( 75 KSTypeReferenceSyntheticImpl.getCached(ResolverImpl.instance!!.builtIns.anyType.makeNullable(), this) 76 ) 77 }.memoized() 78 } 79 <lambda>null80 override val qualifiedName: KSName? by lazy { 81 KSNameImpl.getCached("${this.parentDeclaration!!.qualifiedName!!.asString()}.${simpleName.asString()}") 82 } 83 <lambda>null84 override val simpleName: KSName by lazy { 85 KSNameImpl.getCached(ktTypeParameter.name ?: "_") 86 } 87 88 override val typeParameters: List<KSTypeParameter> = emptyList() 89 acceptnull90 override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R { 91 return visitor.visitTypeParameter(this, data) 92 } 93 } 94