Skip to content

Commit

Permalink
Value classes: Add isValue property to class descriptors
Browse files Browse the repository at this point in the history
Reuse isInline flag in proto and IR.
Check metadata version on deserialization.
  • Loading branch information
ilmirus committed Nov 27, 2020
1 parent 8eff3a6 commit 361ed11
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class SyntheticClassOrObjectDescriptor(
override fun isExpect() = false
override fun isActual() = false
override fun isFun() = false
override fun isValue() = false

override fun getCompanionObjectDescriptor(): ClassDescriptorWithResolutionScopes? = null
override fun getTypeConstructor(): TypeConstructor = typeConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public class LazyClassDescriptor extends ClassDescriptorBase implements ClassDes
private final boolean isExpect;
private final boolean isActual;
private final boolean isFun;
private final boolean isValue;

private final Annotations annotations;
private final Annotations danglingAnnotations;
Expand Down Expand Up @@ -160,6 +161,7 @@ public LazyClassDescriptor(
containingDeclaration instanceof ClassDescriptor && ((ClassDescriptor) containingDeclaration).isExpect();

this.isFun = modifierList != null && PsiUtilsKt.hasFunModifier(modifierList);
this.isValue = modifierList != null && PsiUtilsKt.hasValueModifier(modifierList);

// Annotation entries are taken from both own annotations (if any) and object literal annotations (if any)
List<KtAnnotationEntry> annotationEntries = new ArrayList<>();
Expand Down Expand Up @@ -552,6 +554,11 @@ public boolean isFun() {
return isFun;
}

@Override
public boolean isValue() {
return isValue;
}

@NotNull
@Override
public Annotations getAnnotations() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,9 @@ open class IrBasedClassDescriptor(owner: IrClass) : ClassDescriptor, IrBasedDecl

override fun isFun() = owner.isFun

// In IR, inline and value are synonyms
override fun isValue() = owner.isInline

override fun getThisAsReceiverParameter() = owner.thisReceiver?.toIrBasedDescriptor() as ReceiverParameterDescriptor

override fun getUnsubstitutedPrimaryConstructor() =
Expand Down Expand Up @@ -704,6 +707,8 @@ open class IrBasedEnumEntryDescriptor(owner: IrEnumEntry) : ClassDescriptor, IrB

override fun isFun() = false

override fun isValue() = false

override fun getThisAsReceiverParameter() = (owner.parent as IrClass).toIrBasedDescriptor().thisAsReceiverParameter

override fun getUnsubstitutedPrimaryConstructor(): ClassConstructorDescriptor? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ open class WrappedClassDescriptor : ClassDescriptor, WrappedDeclarationDescripto

override fun isFun() = owner.isFun

override fun isValue() = owner.isInline

override fun getThisAsReceiverParameter() = owner.thisReceiver?.descriptor as ReceiverParameterDescriptor

override fun getUnsubstitutedPrimaryConstructor() =
Expand Down Expand Up @@ -682,6 +684,8 @@ open class WrappedScriptDescriptor : ScriptDescriptor, WrappedDeclarationDescrip

override fun isFun() = false

override fun isValue() = false

override fun getThisAsReceiverParameter() = owner.thisReceiver.descriptor as ReceiverParameterDescriptor

override fun getUnsubstitutedPrimaryConstructor() = TODO()
Expand Down Expand Up @@ -822,6 +826,8 @@ open class WrappedEnumEntryDescriptor : ClassDescriptor, WrappedDeclarationDescr

override fun isFun() = false

override fun isValue() = false

override fun getThisAsReceiverParameter() = (owner.parent as IrClass).descriptor.thisAsReceiverParameter

override fun getUnsubstitutedPrimaryConstructor(): ClassConstructorDescriptor? {
Expand Down
2 changes: 2 additions & 0 deletions compiler/psi/src/org/jetbrains/kotlin/psi/psiUtil/psiUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ fun KtModifierList.hasSuspendModifier() = hasModifier(KtTokens.SUSPEND_KEYWORD)

fun KtModifierList.hasFunModifier() = hasModifier(KtTokens.FUN_KEYWORD)

fun KtModifierList.hasValueModifier() = hasModifier(KtTokens.VALUE_KEYWORD)

fun ASTNode.children() = generateSequence(firstChildNode) { node -> node.treeNext }
fun ASTNode.parents() = generateSequence(treeParent) { node -> node.treeParent }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class LazyJavaClassDescriptor(
override fun isExpect() = false
override fun isActual() = false
override fun isFun() = false
override fun isValue() = false

private val typeConstructor = LazyJavaClassTypeConstructor()
override fun getTypeConstructor(): TypeConstructor = typeConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class FunctionClassDescriptor(
override fun isData() = false
override fun isInline() = false
override fun isFun() = false
override fun isValue() = false
override fun isExpect() = false
override fun isActual() = false
override fun isExternal() = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public interface ClassDescriptor extends ClassifierDescriptorWithTypeParameters,

boolean isFun();

boolean isValue();

@NotNull
ReceiverParameterDescriptor getThisAsReceiverParameter();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class NotFoundClasses(private val storageManager: StorageManager, private val mo
override fun isData() = false
override fun isInline() = false
override fun isFun() = false
override fun isValue() = false
override fun isExpect() = false
override fun isActual() = false
override fun isExternal() = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ public boolean isFun() {
return false;
}

@Override
public boolean isValue() {
return false;
}

@Override
public boolean isInner() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public boolean isInline() {
return false;
}

@Override
public boolean isValue() {
return false;
}

@Override
public boolean isFun() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ public boolean isFun() {
return original.isFun();
}

@Override
public boolean isValue() {
return original.isValue();
}

@Override
public boolean isExternal() {
return original.isExternal();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public boolean isFun() {
return false;
}

@Override
public boolean isValue() {
return false;
}

@Override
public boolean isCompanionObject() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class DeserializedClassDescriptor(

override fun isData() = Flags.IS_DATA.get(classProto.flags)

override fun isInline() = Flags.IS_INLINE_CLASS.get(classProto.flags)
override fun isInline() = Flags.IS_INLINE_CLASS.get(classProto.flags) && metadataVersion.isAtMost(1, 4, 1)

override fun isExpect() = Flags.IS_EXPECT_CLASS.get(classProto.flags)

Expand All @@ -108,6 +108,8 @@ class DeserializedClassDescriptor(

override fun isFun() = Flags.IS_FUN_INTERFACE.get(classProto.flags)

override fun isValue() = Flags.IS_INLINE_CLASS.get(classProto.flags) && metadataVersion.isAtLeast(1, 4, 2)

override fun getUnsubstitutedMemberScope(kotlinTypeRefiner: KotlinTypeRefiner): MemberScope =
memberScopeHolder.getScope(kotlinTypeRefiner)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ abstract class BinaryVersion(private vararg val numbers: Int) {
return this.patch >= patch
}

fun isAtMost(version: BinaryVersion): Boolean =
isAtMost(version.major, version.minor, version.patch)

fun isAtMost(major: Int, minor: Int, patch: Int): Boolean {
if (this.major < major) return true
if (this.major > major) return false

if (this.minor < minor) return true
if (this.minor > minor) return false

return this.patch <= patch
}

override fun toString(): String {
val versions = toArray().takeWhile { it != UNKNOWN }
return if (versions.isEmpty()) "unknown" else versions.joinToString(".")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class CommonizedClassDescriptor(
override fun isExpect() = isExpect
override fun isActual() = isActual
override fun isFun() = false // TODO: modifier "fun" should be accessible from here too
override fun isValue() = false // TODO: modifier "value" should be accessible from here too

override fun getUnsubstitutedMemberScope(kotlinTypeRefiner: KotlinTypeRefiner): CommonizedMemberScope {
check(kotlinTypeRefiner == KotlinTypeRefiner.Default) {
Expand Down

0 comments on commit 361ed11

Please sign in to comment.