Add initial type attributes infra

This commit is contained in:
Irene Dea
2021-10-08 17:05:11 -07:00
committed by Dmitriy Novozhilov
parent 62bde2d686
commit fad3c1f2f6
23 changed files with 545 additions and 30 deletions
@@ -0,0 +1,37 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.extensions
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.types.*
interface TypeAttributeTranslatorExtension : TypeAttributesTranslator
class TypeAttributeTranslators(project: Project) {
val translators: List<TypeAttributesTranslator> =
getInstances(project) + DefaultTypeAttributesTranslator
fun toAttributes(annotations: Annotations): TypeAttributes {
val translated = translators.mapNotNull { translator ->
translator.toAttributes(annotations)
}.flatten()
return TypeAttributes.create(translated)
}
fun toAnnotations(attributes: TypeAttributes): Annotations {
val translated = translators.mapNotNull { translator ->
translator.toAnnotations(attributes)
}.flatten()
return Annotations.create(translated)
}
companion object :
ProjectExtensionDescriptor<TypeAttributeTranslatorExtension>(
"org.jetbrains.kotlin.extensions.typeAttribute",
TypeAttributeTranslatorExtension::class.java
)
}
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.descriptors.annotations.composeAnnotations
import org.jetbrains.kotlin.descriptors.impl.VariableDescriptorImpl
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.diagnostics.Errors.*
import org.jetbrains.kotlin.extensions.TypeAttributeTranslators
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*
@@ -69,7 +70,8 @@ class TypeResolver(
private val identifierChecker: IdentifierChecker,
private val platformToKotlinClassMapper: PlatformToKotlinClassMapper,
private val languageVersionSettings: LanguageVersionSettings,
private val upperBoundChecker: UpperBoundChecker
private val upperBoundChecker: UpperBoundChecker,
private val typeAttributeTranslators: TypeAttributeTranslators
) {
private val isNonParenthesizedAnnotationsOnFunctionalTypesEnabled =
languageVersionSettings.getFeatureSupport(LanguageFeature.NonParenthesizedAnnotationsOnFunctionalTypes) == LanguageFeature.State.ENABLED
@@ -521,7 +523,7 @@ class TypeResolver(
ErrorUtils.createErrorType("?")
else
KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope(
annotations,
typeAttributeTranslators.toAttributes(annotations),
typeParameter.typeConstructor,
listOf(),
false,
@@ -602,7 +604,8 @@ class TypeResolver(
" but ${collectedArgumentAsTypeProjections.size} instead of ${parameters.size} found in ${element.text}"
}
val resultingType = KotlinTypeFactory.simpleNotNullType(annotations, classDescriptor, arguments)
val resultingType =
KotlinTypeFactory.simpleNotNullType(typeAttributeTranslators.toAttributes(annotations), classDescriptor, arguments)
// We create flexible types by convention here
// This is not intended to be used in normal users' environments, only for tests and debugger etc
@@ -699,7 +702,12 @@ class TypeResolver(
}
return if (c.abbreviated) {
val abbreviatedType = KotlinTypeFactory.simpleType(annotations, descriptor.typeConstructor, arguments, false)
val abbreviatedType = KotlinTypeFactory.simpleType(
typeAttributeTranslators.toAttributes(annotations),
descriptor.typeConstructor,
arguments,
false
)
type(abbreviatedType)
} else {
val typeAliasExpansion = TypeAliasExpansion.create(null, descriptor, arguments)
@@ -56,6 +56,8 @@ class TypeTemplate(
) {
override fun replaceAnnotations(newAnnotations: Annotations) = this
override fun replaceAttributes(newAttributes: TypeAttributes) = this
override fun makeNullableAsSpecified(newNullability: Boolean) = TypeTemplate(typeVariable, builderInferenceData, newNullability)
override val delegate: SimpleType