FIR: unify common special names scattered around the code base

This commit is contained in:
Tianyu Geng
2021-07-28 14:25:24 -07:00
committed by teamcityserver
parent 263b876e6e
commit 684ef871ee
42 changed files with 161 additions and 145 deletions
@@ -14,7 +14,7 @@ data class CallableId(
private val pathToLocal: FqName? = null
) {
private companion object {
val LOCAL_NAME = Name.special("<local>")
val LOCAL_NAME = SpecialNames.LOCAL
val PACKAGE_FQ_NAME_FOR_LOCAL = FqName.topLevel(LOCAL_NAME)
}
@@ -16,8 +16,6 @@
package org.jetbrains.kotlin.name
import java.util.*
object NameUtils {
private val SANITIZE_AS_JAVA_INVALID_CHARACTERS = "[^\\p{L}\\p{Digit}]".toRegex()
@@ -52,5 +50,5 @@ object NameUtils {
Name.identifier(NameUtils.getPackagePartClassNamePrefix(filePath.substringAfterLast('/').substringBeforeLast('.')))
@JvmStatic
fun hasName(name: Name) = name != SpecialNames.NO_NAME_PROVIDED && name != SpecialNames.ANONYMOUS_FUNCTION
fun hasName(name: Name) = name != SpecialNames.NO_NAME_PROVIDED && name != SpecialNames.ANONYMOUS
}
@@ -1,53 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.name;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class SpecialNames {
public static final Name NO_NAME_PROVIDED = Name.special("<no name provided>");
public static final Name ROOT_PACKAGE = Name.special("<root package>");
public static final Name UNDERSCORE_FOR_UNUSED_VAR = Name.special("<unused var>");
public static final Name DEFAULT_NAME_FOR_COMPANION_OBJECT = Name.identifier("Companion");
// This name is used as a key for the case when something has no name _due to a syntactic error_
// Example: fun (x: Int) = 5
// There's no name for this function in the PSI
// The name contains a GUID to avoid clashes, if a clash happens, it's not a big deal: the code does not compile anyway
public static final Name SAFE_IDENTIFIER_FOR_NO_NAME = Name.identifier("no_name_in_PSI_3d19d79d_1ba9_4cd0_b7f5_b46aa3cd5d40");
public static final String ANONYMOUS = "<anonymous>";
public static final Name ANONYMOUS_FUNCTION = Name.special(ANONYMOUS);
@NotNull
public static Name safeIdentifier(@Nullable Name name) {
return name != null && !name.isSpecial() ? name : SAFE_IDENTIFIER_FOR_NO_NAME;
}
@NotNull
public static Name safeIdentifier(@Nullable String name) {
return safeIdentifier(name == null ? null : Name.identifier(name));
}
public static boolean isSafeIdentifier(@NotNull Name name) {
return !name.asString().isEmpty() && !name.isSpecial();
}
private SpecialNames() {}
}
@@ -0,0 +1,74 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.name
object SpecialNames {
@JvmField
val NO_NAME_PROVIDED = Name.special("<no name provided>")
@JvmField
val ROOT_PACKAGE = Name.special("<root package>")
@JvmField
val DEFAULT_NAME_FOR_COMPANION_OBJECT = Name.identifier("Companion")
// This name is used as a key for the case when something has no name _due to a syntactic error_
// Example: fun (x: Int) = 5
// There's no name for this function in the PSI
// The name contains a GUID to avoid clashes, if a clash happens, it's not a big deal: the code does not compile anyway
@JvmField
val SAFE_IDENTIFIER_FOR_NO_NAME = Name.identifier("no_name_in_PSI_3d19d79d_1ba9_4cd0_b7f5_b46aa3cd5d40")
const val ANONYMOUS_STRING = "<anonymous>"
@JvmField
val ANONYMOUS = Name.special(ANONYMOUS_STRING)
@JvmField
val UNARY = Name.special("<unary>")
@JvmField
val THIS = Name.special("<this>")
@JvmField
val INIT = Name.special("<init>")
@JvmField
val ITERATOR = Name.special("<iterator>")
@JvmField
val DESTRUCT = Name.special("<destruct>")
@JvmField
val LOCAL = Name.special("<local>")
@JvmField
val UNDERSCORE_FOR_UNUSED_VAR = Name.special("<unused var>")
@JvmStatic
fun safeIdentifier(name: Name?): Name {
return if (name != null && !name.isSpecial) name else SAFE_IDENTIFIER_FOR_NO_NAME
}
@JvmStatic
fun safeIdentifier(name: String?): Name {
return safeIdentifier(if (name == null) null else Name.identifier(name))
}
fun isSafeIdentifier(name: Name): Boolean {
return name.asString().isNotEmpty() && !name.isSpecial
}
}
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader
import org.jetbrains.kotlin.load.kotlin.header.ReadKotlinClassHeaderAnnotationVisitor
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.resolve.constants.ClassLiteralValue
import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType
import java.lang.reflect.Constructor
@@ -115,7 +116,7 @@ private object ReflectClassStructure {
private fun loadConstructorAnnotations(klass: Class<*>, memberVisitor: KotlinJvmBinaryClass.MemberVisitor) {
for (constructor in klass.declaredConstructors) {
val visitor = memberVisitor.visitMethod(Name.special("<init>"), SignatureSerializer.constructorDesc(constructor)) ?: continue
val visitor = memberVisitor.visitMethod(SpecialNames.INIT, SignatureSerializer.constructorDesc(constructor)) ?: continue
for (annotation in constructor.declaredAnnotations) {
processAnnotation(visitor, annotation)
@@ -20,7 +20,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.name.SpecialNames;
import org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.kotlin.types.TypeSubstitutor;
@@ -31,10 +31,9 @@ import java.util.Collections;
import java.util.List;
public abstract class AbstractReceiverParameterDescriptor extends DeclarationDescriptorImpl implements ReceiverParameterDescriptor {
private static final Name RECEIVER_PARAMETER_NAME = Name.special("<this>");
public AbstractReceiverParameterDescriptor(@NotNull Annotations annotations) {
super(annotations, RECEIVER_PARAMETER_NAME);
super(annotations, SpecialNames.THIS);
}
@Nullable
@@ -21,6 +21,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.name.SpecialNames;
import org.jetbrains.kotlin.types.TypeSubstitutor;
import java.util.Collection;
@@ -31,8 +32,6 @@ public class ClassConstructorDescriptorImpl extends FunctionDescriptorImpl imple
protected final boolean isPrimary;
private static final Name NAME = Name.special("<init>");
protected ClassConstructorDescriptorImpl(
@NotNull ClassDescriptor containingDeclaration,
@Nullable ConstructorDescriptor original,
@@ -41,7 +40,7 @@ public class ClassConstructorDescriptorImpl extends FunctionDescriptorImpl imple
@NotNull Kind kind,
@NotNull SourceElement source
) {
super(containingDeclaration, original, annotations, NAME, kind, source);
super(containingDeclaration, original, annotations, SpecialNames.INIT, kind, source);
this.isPrimary = isPrimary;
}
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor.Kind
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.resolve.DescriptorFactory
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.storage.getValue
@@ -56,7 +57,7 @@ class TypeAliasConstructorDescriptorImpl private constructor(
kind: Kind,
source: SourceElement
) : TypeAliasConstructorDescriptor,
FunctionDescriptorImpl(typeAliasDescriptor, original, annotations, Name.special("<init>"), kind, source) {
FunctionDescriptorImpl(typeAliasDescriptor, original, annotations, SpecialNames.INIT, kind, source) {
init {
isActual = typeAliasDescriptor.isActual
@@ -292,7 +292,7 @@ public class DescriptorUtils {
@SuppressWarnings("unused")
public static boolean isAnonymousFunction(@NotNull DeclarationDescriptor descriptor) {
return descriptor instanceof SimpleFunctionDescriptor &&
descriptor.getName().equals(SpecialNames.ANONYMOUS_FUNCTION);
descriptor.getName().equals(SpecialNames.ANONYMOUS);
}
public static boolean isNonCompanionObject(@Nullable DeclarationDescriptor descriptor) {