@@ -82,6 +82,29 @@ sealed class MaybeConstant {
8282 },
8383 ),
8484 ),
85+ EnumConstantSyntax (
86+ value: final value,
87+ : final definitionIndex,
88+ : final index,
89+ : final name,
90+ ) =>
91+ EnumConstant (
92+ definition: context.definitions[definitionIndex],
93+ index: index,
94+ name: name,
95+ fields: (value ?? {}).map (
96+ (key, index) {
97+ final constant = context.constants[index];
98+ if (constant is ! Constant ) {
99+ throw FormatException (
100+ 'Enum constant field $key at index $index is not '
101+ 'a constant' ,
102+ );
103+ }
104+ return MapEntry (key, constant);
105+ },
106+ ),
107+ ),
85108 RecordConstantSyntax (: final positional, : final named) => RecordConstant (
86109 positional: (positional ?? const []).map ((i) {
87110 final constant = context.constants[i];
@@ -493,6 +516,87 @@ final class InstanceConstant extends Constant {
493516 }
494517}
495518
519+ /// A constant enum value.
520+ final class EnumConstant extends Constant {
521+ /// The definition of the enum class of this value.
522+ final Definition definition;
523+
524+ /// The index of the enum member.
525+ final int index;
526+
527+ /// The name of the enum member.
528+ final String name;
529+
530+ /// The fields of this instance, mapped from field name to [Constant] value.
531+ ///
532+ /// This includes additional fields from enhanced enums.
533+ final Map <String , Constant > fields;
534+
535+ /// Creates an [EnumConstant] object with the given [definition] , [index] ,
536+ /// [name] , and [fields] .
537+ const EnumConstant ({
538+ required this .definition,
539+ required this .index,
540+ required this .name,
541+ this .fields = const {},
542+ });
543+
544+ @override
545+ EnumConstantSyntax _toSyntax (SerializationContext context) =>
546+ EnumConstantSyntax (
547+ definitionIndex: context.definitions[definition]! ,
548+ index: index,
549+ name: name,
550+ value: fields.isNotEmpty
551+ ? fields.map (
552+ (key, value) => MapEntry (key, context.constants[value]! ),
553+ )
554+ : null ,
555+ );
556+
557+ @override
558+ bool operator == (Object other) {
559+ if (identical (this , other)) return true ;
560+
561+ return other is EnumConstant &&
562+ other.definition == definition &&
563+ other.index == index &&
564+ other.name == name &&
565+ deepEquals (other.fields, fields);
566+ }
567+
568+ @override
569+ int get hashCode => Object .hash (definition, index, name, deepHash (fields));
570+
571+ @override
572+ String toString () =>
573+ 'EnumConstant($definition , index: $index , name: $name , fields: {'
574+ '${fields .entries .map ((e ) => '${e .key }: ${e .value }' ).join (', ' )}})' ;
575+
576+ @override
577+ bool _semanticEqualsInternal (
578+ MaybeConstant other,
579+ bool allowPromotionOfUnsupported,
580+ ) {
581+ if (other is ! EnumConstant ) return false ;
582+ // ignore: invalid_use_of_visible_for_testing_member
583+ if (! definition.semanticEquals (other.definition)) return false ;
584+ if (index != other.index || name != other.name) return false ;
585+ if (fields.length != other.fields.length) return false ;
586+ for (final entry in fields.entries) {
587+ final otherField = other.fields[entry.key];
588+ if (otherField == null ||
589+ ! entry.value.semanticEquals (
590+ otherField,
591+ allowPromotionOfUnsupported: allowPromotionOfUnsupported,
592+ )) {
593+ return false ;
594+ }
595+ }
596+ return true ;
597+ }
598+ }
599+
496600/// A constant record value.
497601final class RecordConstant extends Constant {
498602 /// The positional fields of this record.
0 commit comments