Skip to content

Commit 55053fd

Browse files
committed
Properly hashed types to optimize their storage in maps.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 4adc3d6 commit 55053fd

File tree

1 file changed

+63
-65
lines changed

1 file changed

+63
-65
lines changed

src/AST/Type.cs

Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public struct TypeQualifiers
5050
public bool IsConst;
5151
public bool IsVolatile;
5252
public bool IsRestrict;
53+
54+
public override int GetHashCode() =>
55+
IsConst.GetHashCode() ^ IsVolatile.GetHashCode() ^
56+
IsRestrict.GetHashCode();
5357
}
5458

5559
/// <summary>
@@ -101,10 +105,8 @@ public override bool Equals(object obj)
101105
return !(left == right);
102106
}
103107

104-
public override int GetHashCode()
105-
{
106-
return base.GetHashCode();
107-
}
108+
public override int GetHashCode() => Type == null ?
109+
Qualifiers.GetHashCode() : Type.GetHashCode() ^ Qualifiers.GetHashCode();
108110
}
109111

110112
/// <summary>
@@ -147,10 +149,7 @@ public override bool Equals(object obj)
147149
return Declaration.Equals(type.Declaration);
148150
}
149151

150-
public override int GetHashCode()
151-
{
152-
return base.GetHashCode();
153-
}
152+
public override int GetHashCode() => Declaration.GetHashCode();
154153
}
155154

156155
/// <summary>
@@ -220,7 +219,8 @@ public override bool Equals(object obj)
220219

221220
public override int GetHashCode()
222221
{
223-
return base.GetHashCode();
222+
return QualifiedType.GetHashCode() ^ SizeType.GetHashCode() ^
223+
Size.GetHashCode() ^ ElementSize.GetHashCode();
224224
}
225225
}
226226

@@ -285,10 +285,11 @@ public override bool Equals(object obj)
285285
return ReturnType.Equals(type.ReturnType) && Parameters.SequenceEqual(type.Parameters);
286286
}
287287

288-
public override int GetHashCode()
289-
{
290-
return base.GetHashCode();
291-
}
288+
public override int GetHashCode() =>
289+
Parameters.Aggregate(ReturnType.GetHashCode(),
290+
(p1, p2) => p1.GetHashCode() ^ p2.GetHashCode()) ^
291+
CallingConvention.GetHashCode() ^
292+
ExceptionSpecType.GetHashCode();
292293
}
293294

294295
/// <summary>
@@ -360,10 +361,8 @@ public override bool Equals(object obj)
360361
&& Modifier == type.Modifier;
361362
}
362363

363-
public override int GetHashCode()
364-
{
365-
return base.GetHashCode();
366-
}
364+
public override int GetHashCode() =>
365+
QualifiedPointee.GetHashCode() ^ Modifier.GetHashCode();
367366
}
368367

369368
/// <summary>
@@ -407,10 +406,7 @@ public override bool Equals(object obj)
407406
return QualifiedPointee.Equals(pointer.QualifiedPointee);
408407
}
409408

410-
public override int GetHashCode()
411-
{
412-
return base.GetHashCode();
413-
}
409+
public override int GetHashCode() => QualifiedPointee.GetHashCode();
414410
}
415411

416412
/// <summary>
@@ -451,10 +447,7 @@ public override bool Equals(object obj)
451447
return Declaration.Type.Equals(typedef == null ? obj : typedef.Declaration.Type);
452448
}
453449

454-
public override int GetHashCode()
455-
{
456-
return base.GetHashCode();
457-
}
450+
public override int GetHashCode() => Declaration.Type.GetHashCode();
458451
}
459452

460453
/// <summary>
@@ -508,10 +501,8 @@ public override bool Equals(object obj)
508501
&& Equivalent.Equals(attributed.Equivalent);
509502
}
510503

511-
public override int GetHashCode()
512-
{
513-
return base.GetHashCode();
514-
}
504+
public override int GetHashCode() =>
505+
Modified.GetHashCode() ^ Equivalent.GetHashCode();
515506
}
516507

517508
/// <summary>
@@ -530,12 +521,16 @@ public DecayedType()
530521
public DecayedType(DecayedType type)
531522
: base(type)
532523
{
533-
Decayed = new QualifiedType((Type) type.Decayed.Type.Clone(), type.Decayed.Qualifiers);
534-
Original = new QualifiedType((Type) type.Original.Type.Clone(), type.Original.Qualifiers);
535-
Pointee = new QualifiedType((Type) type.Pointee.Type.Clone(), type.Pointee.Qualifiers);
524+
Decayed = new QualifiedType((Type) type.Decayed.Type.Clone(),
525+
type.Decayed.Qualifiers);
526+
Original = new QualifiedType((Type) type.Original.Type.Clone(),
527+
type.Original.Qualifiers);
528+
Pointee = new QualifiedType((Type) type.Pointee.Type.Clone(),
529+
type.Pointee.Qualifiers);
536530
}
537531

538-
public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals = new TypeQualifiers())
532+
public override T Visit<T>(ITypeVisitor<T> visitor,
533+
TypeQualifiers quals = new TypeQualifiers())
539534
{
540535
return visitor.VisitDecayedType(this, quals);
541536
}
@@ -553,10 +548,9 @@ public override bool Equals(object obj)
553548
return Original.Equals(decay.Original);
554549
}
555550

556-
public override int GetHashCode()
557-
{
558-
return base.GetHashCode();
559-
}
551+
public override int GetHashCode() =>
552+
Decayed.GetHashCode() ^ Original.GetHashCode() ^
553+
Pointee.GetHashCode();
560554
}
561555

562556
/// <summary>
@@ -724,10 +718,10 @@ public override bool Equals(object obj)
724718
(Desugared.Type != null && Desugared == type.Desugared));
725719
}
726720

727-
public override int GetHashCode()
728-
{
729-
return base.GetHashCode();
730-
}
721+
public override int GetHashCode() =>
722+
Arguments.Aggregate(Template.GetHashCode(),
723+
(a1, a2) => a1.GetHashCode() ^ a2.GetHashCode()) ^
724+
Desugared.GetHashCode();
731725
}
732726

733727
/// <summary>
@@ -778,10 +772,9 @@ public override bool Equals(object obj)
778772
Desugared == type.Desugared;
779773
}
780774

781-
public override int GetHashCode()
782-
{
783-
return base.GetHashCode();
784-
}
775+
public override int GetHashCode() =>
776+
Arguments.Aggregate(Desugared.GetHashCode(),
777+
(a1, a2) => a1.GetHashCode() ^ a2.GetHashCode());
785778
}
786779

787780
/// <summary>
@@ -834,10 +827,12 @@ public override bool Equals(object obj)
834827
&& IsParameterPack == type.IsParameterPack;
835828
}
836829

837-
public override int GetHashCode()
838-
{
839-
return base.GetHashCode();
840-
}
830+
public override int GetHashCode() =>
831+
Parameter == null ?
832+
Depth.GetHashCode() ^ Index.GetHashCode() ^
833+
IsParameterPack.GetHashCode() :
834+
Parameter.GetHashCode() ^ Depth.GetHashCode() ^
835+
Index.GetHashCode() ^ IsParameterPack.GetHashCode();
841836
}
842837

843838
/// <summary>
@@ -879,10 +874,7 @@ public override bool Equals(object obj)
879874
return Replacement.Equals(type.Replacement);
880875
}
881876

882-
public override int GetHashCode()
883-
{
884-
return base.GetHashCode();
885-
}
877+
public override int GetHashCode() => Replacement.GetHashCode();
886878
}
887879

888880
/// <summary>
@@ -933,10 +925,11 @@ public override bool Equals(object obj)
933925
&& Class.Equals(type.Class);
934926
}
935927

936-
public override int GetHashCode()
937-
{
938-
return base.GetHashCode();
939-
}
928+
public override int GetHashCode() =>
929+
TemplateSpecialization != null && Class != null
930+
? TemplateSpecialization.GetHashCode() ^ Class.GetHashCode()
931+
: TemplateSpecialization != null ? TemplateSpecialization.GetHashCode()
932+
: Class.GetHashCode();
940933
}
941934

942935
/// <summary>
@@ -967,6 +960,9 @@ public override object Clone()
967960
{
968961
return new DependentNameType(this);
969962
}
963+
964+
public override int GetHashCode() =>
965+
Qualifier.GetHashCode() ^ Identifier.GetHashCode();
970966
}
971967

972968
/// <summary>
@@ -1010,10 +1006,7 @@ public override bool Equals(object obj)
10101006
return Type == type.Type;
10111007
}
10121008

1013-
public override int GetHashCode()
1014-
{
1015-
return base.GetHashCode();
1016-
}
1009+
public override int GetHashCode() => Type.GetHashCode();
10171010
}
10181011

10191012
public class PackExpansionType : Type
@@ -1063,6 +1056,9 @@ public override object Clone()
10631056
{
10641057
return new UnaryTransformType(this);
10651058
}
1059+
1060+
public override int GetHashCode() =>
1061+
Desugared.GetHashCode() ^ BaseType.GetHashCode();
10661062
}
10671063

10681064
public class VectorType : Type
@@ -1088,6 +1084,9 @@ public override object Clone()
10881084
{
10891085
return new VectorType(this);
10901086
}
1087+
1088+
public override int GetHashCode() =>
1089+
ElementType.GetHashCode() ^ NumElements.GetHashCode();
10911090
}
10921091

10931092
public class UnsupportedType : Type
@@ -1117,6 +1116,8 @@ public override object Clone()
11171116
{
11181117
return new UnsupportedType(this);
11191118
}
1119+
1120+
public override int GetHashCode() => Description.GetHashCode();
11201121
}
11211122

11221123
public class CustomType : UnsupportedType
@@ -1223,10 +1224,7 @@ public override bool Equals(object obj)
12231224
return Type == type.Type;
12241225
}
12251226

1226-
public override int GetHashCode()
1227-
{
1228-
return base.GetHashCode();
1229-
}
1227+
public override int GetHashCode() => Type.GetHashCode();
12301228
}
12311229

12321230
#endregion

0 commit comments

Comments
 (0)