Skip to content

Commit 151b9df

Browse files
committed
Remove equality operator on CppElement
1 parent b00cdbf commit 151b9df

17 files changed

+6
-323
lines changed

src/CppAst.Tests/TestTypes.cs

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public void TestSimple()
5050
};
5151

5252
var canonicalTypes = compilation.Typedefs.Select(x => x.GetCanonicalType()).Concat(compilation.Fields.Select(x => x.Type.GetCanonicalType())).ToList();
53-
Assert.AreEqual(types, canonicalTypes);
5453
Assert.AreEqual(types.Select(x => x.SizeOf), canonicalTypes.Select(x => x.SizeOf));
5554
}
5655
);

src/CppAst/CppArrayType.cs

+1-21
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace CppAst
99
/// <summary>
1010
/// A C++ array (e.g int[5] or int[])
1111
/// </summary>
12-
public sealed class CppArrayType : CppTypeWithElementType, IEquatable<CppArrayType>
12+
public sealed class CppArrayType : CppTypeWithElementType
1313
{
1414
/// <summary>
1515
/// Constructor of a C++ array.
@@ -32,26 +32,6 @@ public override int SizeOf
3232
set => throw new InvalidOperationException("Cannot set the SizeOf an array type. The SizeOf is calculated by the SizeOf its ElementType and the number of elements in the fixed array");
3333
}
3434

35-
public bool Equals(CppArrayType other)
36-
{
37-
if (ReferenceEquals(null, other)) return false;
38-
if (ReferenceEquals(this, other)) return true;
39-
return base.Equals(other) && Size == other.Size;
40-
}
41-
42-
public override bool Equals(object obj)
43-
{
44-
return ReferenceEquals(this, obj) || obj is CppArrayType other && Equals(other);
45-
}
46-
47-
public override int GetHashCode()
48-
{
49-
unchecked
50-
{
51-
return (base.GetHashCode() * 397) ^ Size;
52-
}
53-
}
54-
5535
public override CppType GetCanonicalType()
5636
{
5737
var elementTypeCanonical = ElementType.GetCanonicalType();

src/CppAst/CppClass.cs

-31
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,6 @@ public override string FullName
163163
public bool IsAbstract { get; set; }
164164

165165

166-
private bool Equals(CppClass other)
167-
{
168-
return base.Equals(other) && Equals(Parent, other.Parent) && Name.Equals(other.Name);
169-
}
170-
171166
/// <inheritdoc />
172167
public override int SizeOf { get; set; }
173168

@@ -176,32 +171,6 @@ private bool Equals(CppClass other)
176171
/// </summary>
177172
public int AlignOf { get; set; }
178173

179-
/// <inheritdoc />
180-
public override bool Equals(object obj)
181-
{
182-
return ReferenceEquals(this, obj) || obj is CppClass other && Equals(other);
183-
}
184-
185-
/// <inheritdoc />
186-
public override int GetHashCode()
187-
{
188-
unchecked
189-
{
190-
int hashCode = base.GetHashCode();
191-
hashCode = (hashCode * 397) ^ (Parent != null ? Parent.GetHashCode() : 0);
192-
hashCode = (hashCode * 397) ^ Name.GetHashCode();
193-
foreach (var templateParameter in TemplateParameters)
194-
{
195-
hashCode = (hashCode * 397) ^ templateParameter.GetHashCode();
196-
}
197-
foreach (var templateArgument in TemplateSpecializedArguments)
198-
{
199-
hashCode = (hashCode * 397) ^ templateArgument.GetHashCode();
200-
}
201-
return hashCode;
202-
}
203-
}
204-
205174
/// <inheritdoc />
206175
public override CppType GetCanonicalType()
207176
{

src/CppAst/CppElement.cs

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See license.txt file in the project root for full license information.
44

55
using System;
6+
using System.Runtime.CompilerServices;
67

78
namespace CppAst
89
{
@@ -21,6 +22,10 @@ public abstract class CppElement : ICppElement
2122
/// </summary>
2223
public ICppContainer Parent { get; internal set; }
2324

25+
public sealed override bool Equals(object obj) => ReferenceEquals(this, obj);
26+
27+
public sealed override int GetHashCode() => RuntimeHelpers.GetHashCode(this);
28+
2429
public string FullParentName
2530
{
2631
get

src/CppAst/CppEnum.cs

-23
Original file line numberDiff line numberDiff line change
@@ -74,36 +74,13 @@ public override string FullName
7474

7575
public MetaAttributeMap MetaAttributes { get; private set; } = new MetaAttributeMap();
7676

77-
private bool Equals(CppEnum other)
78-
{
79-
return base.Equals(other) && Equals(Parent, other.Parent) && Equals(Name, other.Name);
80-
}
81-
8277
/// <inheritdoc />
8378
public override int SizeOf
8479
{
8580
get => IntegerType?.SizeOf ?? 0;
8681
set => throw new InvalidOperationException("Cannot set the SizeOf an enum as it is determined only by the SizeOf of its underlying IntegerType");
8782
}
8883

89-
/// <inheritdoc />
90-
public override bool Equals(object obj)
91-
{
92-
return ReferenceEquals(this, obj) || obj is CppEnum other && Equals(other);
93-
}
94-
95-
/// <inheritdoc />
96-
public override int GetHashCode()
97-
{
98-
unchecked
99-
{
100-
int hashCode = base.GetHashCode();
101-
hashCode = (hashCode * 397) ^ (Parent != null ? Parent.GetHashCode() : 0);
102-
hashCode = (hashCode * 397) ^ (Name != null ? Name.GetHashCode() : 0);
103-
return hashCode;
104-
}
105-
}
106-
10784
/// <inheritdoc />
10885
public override CppType GetCanonicalType() => IntegerType;
10986

src/CppAst/CppFunctionType.cs

-46
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,6 @@ public CppFunctionType(CppType returnType) : base(CppTypeKind.Function)
3838
/// </summary>
3939
public CppContainerList<CppParameter> Parameters { get; }
4040

41-
private bool Equals(CppFunctionType other)
42-
{
43-
if (base.Equals(other) && ReturnType.Equals(other.ReturnType))
44-
{
45-
if (Parameters.Count != other.Parameters.Count)
46-
{
47-
return false;
48-
}
49-
50-
for (int i = 0; i < Parameters.Count; i++)
51-
{
52-
var fromType = Parameters[i].Type;
53-
var otherType = other.Parameters[i].Type;
54-
if (!fromType.Equals(otherType))
55-
{
56-
return false;
57-
}
58-
}
59-
60-
return true;
61-
}
62-
63-
return false;
64-
}
65-
6641
/// <inheritdoc />
6742
public override int SizeOf
6843
{
@@ -71,27 +46,6 @@ public override int SizeOf
7146
set => throw new InvalidOperationException("This type does not support SizeOf");
7247
}
7348

74-
/// <inheritdoc />
75-
public override bool Equals(object obj)
76-
{
77-
return ReferenceEquals(this, obj) || obj is CppFunctionType other && Equals(other);
78-
}
79-
80-
/// <inheritdoc />
81-
public override int GetHashCode()
82-
{
83-
unchecked
84-
{
85-
int hashCode = base.GetHashCode();
86-
hashCode = (hashCode * 397) ^ ReturnType.GetHashCode();
87-
foreach (var parameter in Parameters)
88-
{
89-
hashCode = (hashCode * 397) ^ parameter.Type.GetHashCode();
90-
}
91-
return hashCode;
92-
}
93-
}
94-
9549
/// <inheritdoc />
9650
public override IEnumerable<ICppDeclaration> Children() => Parameters;
9751

src/CppAst/CppNamespace.cs

-21
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,6 @@ public CppNamespace(string name)
6464

6565
public MetaAttributeMap MetaAttributes { get; private set; } = new MetaAttributeMap();
6666

67-
protected bool Equals(CppNamespace other)
68-
{
69-
return Equals(Parent, other.Parent) && Name.Equals(other.Name);
70-
}
71-
72-
public override bool Equals(object obj)
73-
{
74-
if (ReferenceEquals(null, obj)) return false;
75-
if (ReferenceEquals(this, obj)) return true;
76-
if (obj.GetType() != this.GetType()) return false;
77-
return Equals((CppNamespace)obj);
78-
}
79-
80-
public override int GetHashCode()
81-
{
82-
unchecked
83-
{
84-
return ((Parent != null ? Parent.GetHashCode() : 0) * 397) ^ (Name != null ? Name.GetHashCode() : 0);
85-
}
86-
}
87-
8867
public override string ToString()
8968
{
9069
return $"namespace {Name} {{...}}";

src/CppAst/CppParameter.cs

-18
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,6 @@ public CppParameter(CppType type, string name)
4242
/// </summary>
4343
public CppExpression InitExpression { get; set; }
4444

45-
private bool Equals(CppParameter other)
46-
{
47-
return Equals(Type, other.Type) && Equals(Name, other.Name);
48-
}
49-
50-
public override bool Equals(object obj)
51-
{
52-
return ReferenceEquals(this, obj) || obj is CppParameter other && Equals(other);
53-
}
54-
55-
public override int GetHashCode()
56-
{
57-
unchecked
58-
{
59-
return ((Type != null ? Type.GetHashCode() : 0) * 397) ^ (Name != null ? Name.GetHashCode() : 0);
60-
}
61-
}
62-
6345
public override string ToString()
6446
{
6547
if (string.IsNullOrEmpty(Name))

src/CppAst/CppPrimitiveType.cs

-15
Original file line numberDiff line numberDiff line change
@@ -213,21 +213,6 @@ public override int SizeOf
213213
set => throw new InvalidOperationException("Cannot set the SizeOf of a primitive type");
214214
}
215215

216-
/// <inheritdoc />
217-
public override bool Equals(object obj)
218-
{
219-
return ReferenceEquals(this, obj) || obj is CppPrimitiveType other && Equals(other);
220-
}
221-
222-
/// <inheritdoc />
223-
public override int GetHashCode()
224-
{
225-
unchecked
226-
{
227-
return (base.GetHashCode() * 397) ^ (int)Kind;
228-
}
229-
}
230-
231216
/// <inheritdoc />
232217
public override CppType GetCanonicalType()
233218
{

src/CppAst/CppQualifiedType.cs

-20
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,6 @@ public CppQualifiedType(CppTypeQualifier qualifier, CppType elementType) : base(
2525
/// </summary>
2626
public CppTypeQualifier Qualifier { get; }
2727

28-
private bool Equals(CppQualifiedType other)
29-
{
30-
return base.Equals(other) && Qualifier == other.Qualifier;
31-
}
32-
33-
/// <inheritdoc />
34-
public override bool Equals(object obj)
35-
{
36-
return ReferenceEquals(this, obj) || obj is CppQualifiedType other && Equals(other);
37-
}
38-
39-
/// <inheritdoc />
40-
public override int GetHashCode()
41-
{
42-
unchecked
43-
{
44-
return (base.GetHashCode() * 397) ^ (int)Qualifier;
45-
}
46-
}
47-
4828
/// <inheritdoc />
4929
public override CppType GetCanonicalType()
5030
{

src/CppAst/CppTemplateArgument.cs

-16
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,6 @@ public override int SizeOf
7777
set => throw new InvalidOperationException("This type does not support SizeOf");
7878
}
7979

80-
/// <inheritdoc />
81-
public override bool Equals(object obj)
82-
{
83-
return ReferenceEquals(this, obj) || obj is CppTemplateArgument other && Equals(other);
84-
}
85-
86-
/// <inheritdoc />
87-
public override int GetHashCode()
88-
{
89-
unchecked
90-
{
91-
return (base.GetHashCode() * 397) ^ SourceParam.GetHashCode() ^ ArgString.GetHashCode();
92-
}
93-
}
94-
95-
9680
/// <inheritdoc />
9781
public override CppType GetCanonicalType() => this;
9882

src/CppAst/CppTemplateParameterNonType.cs

-15
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,6 @@ public override int SizeOf
4141
set => throw new InvalidOperationException("This type does not support SizeOf");
4242
}
4343

44-
/// <inheritdoc />
45-
public override bool Equals(object obj)
46-
{
47-
return ReferenceEquals(this, obj) || obj is CppTemplateParameterNonType other && Equals(other);
48-
}
49-
50-
/// <inheritdoc />
51-
public override int GetHashCode()
52-
{
53-
unchecked
54-
{
55-
return (base.GetHashCode() * 397) ^ Name.GetHashCode();
56-
}
57-
}
58-
5944
/// <inheritdoc />
6045
public override CppType GetCanonicalType() => this;
6146

src/CppAst/CppTemplateParameterType.cs

-15
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,6 @@ public override int SizeOf
3737
set => throw new InvalidOperationException("This type does not support SizeOf");
3838
}
3939

40-
/// <inheritdoc />
41-
public override bool Equals(object obj)
42-
{
43-
return ReferenceEquals(this, obj) || obj is CppTemplateParameterType other && Equals(other);
44-
}
45-
46-
/// <inheritdoc />
47-
public override int GetHashCode()
48-
{
49-
unchecked
50-
{
51-
return (base.GetHashCode() * 397) ^ Name.GetHashCode();
52-
}
53-
}
54-
5540
/// <inheritdoc />
5641
public override CppType GetCanonicalType() => this;
5742

src/CppAst/CppType.cs

-18
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,6 @@ protected CppType(CppTypeKind typeKind)
2525

2626
public abstract int SizeOf { get; set; }
2727

28-
protected bool Equals(CppType other)
29-
{
30-
return TypeKind == other.TypeKind;
31-
}
32-
33-
public override bool Equals(object obj)
34-
{
35-
if (ReferenceEquals(null, obj)) return false;
36-
if (ReferenceEquals(this, obj)) return true;
37-
return obj is CppType type && Equals(type);
38-
}
39-
40-
/// <inheritdoc />
41-
public override int GetHashCode()
42-
{
43-
return (int)TypeKind;
44-
}
45-
4628
/// <summary>
4729
/// Gets the canonical type of this type instance.
4830
/// </summary>

0 commit comments

Comments
 (0)