@@ -6,6 +6,7 @@ import 'dart:math';
66
77import 'package:meta/meta.dart' ;
88
9+ import 'canonicalization_context.dart' ;
910import 'definition.dart' ;
1011import 'helper.dart' ;
1112import 'serialization_context.dart' ;
@@ -27,6 +28,9 @@ sealed class MaybeConstant {
2728 /// `hashCode` because it is implicitly covered by the content-based hash.
2829 int get _size;
2930
31+ /// Canonicalizes this [MaybeConstant] .
32+ MaybeConstant _canonicalizeChildren (CanonicalizationContext context);
33+
3034 /// Compares this [MaybeConstant] with [other] for semantic equality.
3135 ///
3236 /// If [allowPromotionOfUnsupported] is true, an [UnsupportedConstant] in
@@ -171,6 +175,9 @@ final class NonConstant extends MaybeConstant {
171175 @override
172176 int get _size => 1 ;
173177
178+ @override
179+ MaybeConstant _canonicalizeChildren (CanonicalizationContext context) => this ;
180+
174181 @override
175182 int get hashCode => 0x4e6f6e43 ;
176183
@@ -196,6 +203,9 @@ abstract class Constant extends MaybeConstant {
196203 /// Creates a [Constant] object.
197204 const Constant ();
198205
206+ @override
207+ Constant _canonicalizeChildren (CanonicalizationContext context);
208+
199209 @override
200210 @visibleForTesting
201211 bool semanticEquals (
@@ -233,6 +243,9 @@ final class NullConstant extends Constant {
233243 @override
234244 int get _size => 1 ;
235245
246+ @override
247+ Constant _canonicalizeChildren (CanonicalizationContext context) => this ;
248+
236249 @override
237250 int get hashCode => 0x4e756c6c ;
238251
@@ -268,6 +281,9 @@ final class UnsupportedConstant extends Constant {
268281 @override
269282 int get _size => 1 ;
270283
284+ @override
285+ Constant _canonicalizeChildren (CanonicalizationContext context) => this ;
286+
271287 @override
272288 int get hashCode => message.hashCode;
273289
@@ -303,6 +319,9 @@ final class BoolConstant extends Constant {
303319 @override
304320 int get _size => 1 ;
305321
322+ @override
323+ Constant _canonicalizeChildren (CanonicalizationContext context) => this ;
324+
306325 @override
307326 bool operator == (Object other) =>
308327 other is BoolConstant && other.value == value;
@@ -338,6 +357,9 @@ final class IntConstant extends Constant {
338357 @override
339358 int get _size => 1 ;
340359
360+ @override
361+ Constant _canonicalizeChildren (CanonicalizationContext context) => this ;
362+
341363 @override
342364 bool operator == (Object other) =>
343365 other is IntConstant && other.value == value;
@@ -373,6 +395,9 @@ final class StringConstant extends Constant {
373395 @override
374396 int get _size => 1 ;
375397
398+ @override
399+ Constant _canonicalizeChildren (CanonicalizationContext context) => this ;
400+
376401 @override
377402 bool operator == (Object other) =>
378403 other is StringConstant && other.value == value;
@@ -413,6 +438,9 @@ final class SymbolConstant extends Constant {
413438 @override
414439 int get _size => 1 ;
415440
441+ @override
442+ Constant _canonicalizeChildren (CanonicalizationContext context) => this ;
443+
416444 @override
417445 bool operator == (Object other) =>
418446 other is SymbolConstant &&
@@ -466,6 +494,12 @@ final class ListConstant extends Constant {
466494 return 1 + size;
467495 });
468496
497+ @override
498+ Constant _canonicalizeChildren (CanonicalizationContext context) =>
499+ ListConstant (
500+ value.map ((c) => context.canonicalizeConstant (c) as Constant ).toList (),
501+ );
502+
469503 @override
470504 bool operator == (Object other) {
471505 if (identical (this , other)) return true ;
@@ -537,6 +571,19 @@ final class MapConstant extends Constant {
537571 return 1 + size;
538572 });
539573
574+ @override
575+ Constant _canonicalizeChildren (CanonicalizationContext context) =>
576+ MapConstant (
577+ entries
578+ .map (
579+ (e) => MapEntry (
580+ context.canonicalizeConstant (e.key) as Constant ,
581+ context.canonicalizeConstant (e.value) as Constant ,
582+ ),
583+ )
584+ .toList (),
585+ );
586+
540587 @override
541588 bool operator == (Object other) {
542589 if (identical (this , other)) return true ;
@@ -655,6 +702,20 @@ final class InstanceConstant extends Constant {
655702 return 1 + size;
656703 });
657704
705+ @override
706+ Constant _canonicalizeChildren (CanonicalizationContext context) =>
707+ InstanceConstant (
708+ definition: context.canonicalizeDefinition (definition),
709+ fields: Map .fromEntries (
710+ fields.entries.map (
711+ (e) => MapEntry (
712+ e.key,
713+ context.canonicalizeConstant (e.value) as Constant ,
714+ ),
715+ ),
716+ ),
717+ );
718+
658719 @override
659720 String toString () =>
660721 'InstanceConstant($definition , {'
@@ -757,6 +818,22 @@ final class EnumConstant extends Constant {
757818 return 1 + size;
758819 });
759820
821+ @override
822+ Constant _canonicalizeChildren (CanonicalizationContext context) =>
823+ EnumConstant (
824+ definition: context.canonicalizeDefinition (definition),
825+ index: index,
826+ name: name,
827+ fields: Map .fromEntries (
828+ fields.entries.map (
829+ (e) => MapEntry (
830+ e.key,
831+ context.canonicalizeConstant (e.value) as Constant ,
832+ ),
833+ ),
834+ ),
835+ );
836+
760837 @override
761838 String toString () =>
762839 'EnumConstant($definition , index: $index , name: $name , fields: {'
@@ -852,6 +929,22 @@ final class RecordConstant extends Constant {
852929 return 1 + size;
853930 });
854931
932+ @override
933+ Constant _canonicalizeChildren (CanonicalizationContext context) =>
934+ RecordConstant (
935+ positional: positional
936+ .map ((c) => context.canonicalizeConstant (c) as Constant )
937+ .toList (),
938+ named: Map .fromEntries (
939+ named.entries.map (
940+ (e) => MapEntry (
941+ e.key,
942+ context.canonicalizeConstant (e.value) as Constant ,
943+ ),
944+ ),
945+ ),
946+ );
947+
855948 @override
856949 String toString () =>
857950 'RecordConstant('
@@ -896,6 +989,9 @@ final class RecordConstant extends Constant {
896989extension MaybeConstantProtected on MaybeConstant {
897990 ConstantSyntax toSyntax (SerializationContext context) => _toSyntax (context);
898991
992+ MaybeConstant canonicalizeChildren (CanonicalizationContext context) =>
993+ _canonicalizeChildren (context);
994+
899995 static MaybeConstant fromSyntax (
900996 ConstantSyntax syntax,
901997 DeserializationContext context,
0 commit comments