Skip to content

Commit

Permalink
Remove equality operator on CppElement
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed May 19, 2024
1 parent b00cdbf commit 151b9df
Show file tree
Hide file tree
Showing 17 changed files with 6 additions and 323 deletions.
1 change: 0 additions & 1 deletion src/CppAst.Tests/TestTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public void TestSimple()
};

var canonicalTypes = compilation.Typedefs.Select(x => x.GetCanonicalType()).Concat(compilation.Fields.Select(x => x.Type.GetCanonicalType())).ToList();
Assert.AreEqual(types, canonicalTypes);
Assert.AreEqual(types.Select(x => x.SizeOf), canonicalTypes.Select(x => x.SizeOf));
}
);
Expand Down
22 changes: 1 addition & 21 deletions src/CppAst/CppArrayType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace CppAst
/// <summary>
/// A C++ array (e.g int[5] or int[])
/// </summary>
public sealed class CppArrayType : CppTypeWithElementType, IEquatable<CppArrayType>
public sealed class CppArrayType : CppTypeWithElementType
{
/// <summary>
/// Constructor of a C++ array.
Expand All @@ -32,26 +32,6 @@ public override int SizeOf
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");
}

public bool Equals(CppArrayType other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return base.Equals(other) && Size == other.Size;
}

public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is CppArrayType other && Equals(other);
}

public override int GetHashCode()
{
unchecked
{
return (base.GetHashCode() * 397) ^ Size;
}
}

public override CppType GetCanonicalType()
{
var elementTypeCanonical = ElementType.GetCanonicalType();
Expand Down
31 changes: 0 additions & 31 deletions src/CppAst/CppClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@ public override string FullName
public bool IsAbstract { get; set; }


private bool Equals(CppClass other)
{
return base.Equals(other) && Equals(Parent, other.Parent) && Name.Equals(other.Name);
}

/// <inheritdoc />
public override int SizeOf { get; set; }

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

/// <inheritdoc />
public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is CppClass other && Equals(other);
}

/// <inheritdoc />
public override int GetHashCode()
{
unchecked
{
int hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ (Parent != null ? Parent.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ Name.GetHashCode();
foreach (var templateParameter in TemplateParameters)
{
hashCode = (hashCode * 397) ^ templateParameter.GetHashCode();
}
foreach (var templateArgument in TemplateSpecializedArguments)
{
hashCode = (hashCode * 397) ^ templateArgument.GetHashCode();
}
return hashCode;
}
}

/// <inheritdoc />
public override CppType GetCanonicalType()
{
Expand Down
5 changes: 5 additions & 0 deletions src/CppAst/CppElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See license.txt file in the project root for full license information.

using System;
using System.Runtime.CompilerServices;

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

public sealed override bool Equals(object obj) => ReferenceEquals(this, obj);

public sealed override int GetHashCode() => RuntimeHelpers.GetHashCode(this);

public string FullParentName
{
get
Expand Down
23 changes: 0 additions & 23 deletions src/CppAst/CppEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,36 +74,13 @@ public override string FullName

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

private bool Equals(CppEnum other)
{
return base.Equals(other) && Equals(Parent, other.Parent) && Equals(Name, other.Name);
}

/// <inheritdoc />
public override int SizeOf
{
get => IntegerType?.SizeOf ?? 0;
set => throw new InvalidOperationException("Cannot set the SizeOf an enum as it is determined only by the SizeOf of its underlying IntegerType");
}

/// <inheritdoc />
public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is CppEnum other && Equals(other);
}

/// <inheritdoc />
public override int GetHashCode()
{
unchecked
{
int hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ (Parent != null ? Parent.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Name != null ? Name.GetHashCode() : 0);
return hashCode;
}
}

/// <inheritdoc />
public override CppType GetCanonicalType() => IntegerType;

Expand Down
46 changes: 0 additions & 46 deletions src/CppAst/CppFunctionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,6 @@ public CppFunctionType(CppType returnType) : base(CppTypeKind.Function)
/// </summary>
public CppContainerList<CppParameter> Parameters { get; }

private bool Equals(CppFunctionType other)
{
if (base.Equals(other) && ReturnType.Equals(other.ReturnType))
{
if (Parameters.Count != other.Parameters.Count)
{
return false;
}

for (int i = 0; i < Parameters.Count; i++)
{
var fromType = Parameters[i].Type;
var otherType = other.Parameters[i].Type;
if (!fromType.Equals(otherType))
{
return false;
}
}

return true;
}

return false;
}

/// <inheritdoc />
public override int SizeOf
{
Expand All @@ -71,27 +46,6 @@ public override int SizeOf
set => throw new InvalidOperationException("This type does not support SizeOf");
}

/// <inheritdoc />
public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is CppFunctionType other && Equals(other);
}

/// <inheritdoc />
public override int GetHashCode()
{
unchecked
{
int hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ ReturnType.GetHashCode();
foreach (var parameter in Parameters)
{
hashCode = (hashCode * 397) ^ parameter.Type.GetHashCode();
}
return hashCode;
}
}

/// <inheritdoc />
public override IEnumerable<ICppDeclaration> Children() => Parameters;

Expand Down
21 changes: 0 additions & 21 deletions src/CppAst/CppNamespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,6 @@ public CppNamespace(string name)

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

protected bool Equals(CppNamespace other)
{
return Equals(Parent, other.Parent) && Name.Equals(other.Name);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((CppNamespace)obj);
}

public override int GetHashCode()
{
unchecked
{
return ((Parent != null ? Parent.GetHashCode() : 0) * 397) ^ (Name != null ? Name.GetHashCode() : 0);
}
}

public override string ToString()
{
return $"namespace {Name} {{...}}";
Expand Down
18 changes: 0 additions & 18 deletions src/CppAst/CppParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,6 @@ public CppParameter(CppType type, string name)
/// </summary>
public CppExpression InitExpression { get; set; }

private bool Equals(CppParameter other)
{
return Equals(Type, other.Type) && Equals(Name, other.Name);
}

public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is CppParameter other && Equals(other);
}

public override int GetHashCode()
{
unchecked
{
return ((Type != null ? Type.GetHashCode() : 0) * 397) ^ (Name != null ? Name.GetHashCode() : 0);
}
}

public override string ToString()
{
if (string.IsNullOrEmpty(Name))
Expand Down
15 changes: 0 additions & 15 deletions src/CppAst/CppPrimitiveType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,6 @@ public override int SizeOf
set => throw new InvalidOperationException("Cannot set the SizeOf of a primitive type");
}

/// <inheritdoc />
public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is CppPrimitiveType other && Equals(other);
}

/// <inheritdoc />
public override int GetHashCode()
{
unchecked
{
return (base.GetHashCode() * 397) ^ (int)Kind;
}
}

/// <inheritdoc />
public override CppType GetCanonicalType()
{
Expand Down
20 changes: 0 additions & 20 deletions src/CppAst/CppQualifiedType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,6 @@ public CppQualifiedType(CppTypeQualifier qualifier, CppType elementType) : base(
/// </summary>
public CppTypeQualifier Qualifier { get; }

private bool Equals(CppQualifiedType other)
{
return base.Equals(other) && Qualifier == other.Qualifier;
}

/// <inheritdoc />
public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is CppQualifiedType other && Equals(other);
}

/// <inheritdoc />
public override int GetHashCode()
{
unchecked
{
return (base.GetHashCode() * 397) ^ (int)Qualifier;
}
}

/// <inheritdoc />
public override CppType GetCanonicalType()
{
Expand Down
16 changes: 0 additions & 16 deletions src/CppAst/CppTemplateArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,6 @@ public override int SizeOf
set => throw new InvalidOperationException("This type does not support SizeOf");
}

/// <inheritdoc />
public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is CppTemplateArgument other && Equals(other);
}

/// <inheritdoc />
public override int GetHashCode()
{
unchecked
{
return (base.GetHashCode() * 397) ^ SourceParam.GetHashCode() ^ ArgString.GetHashCode();
}
}


/// <inheritdoc />
public override CppType GetCanonicalType() => this;

Expand Down
15 changes: 0 additions & 15 deletions src/CppAst/CppTemplateParameterNonType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,6 @@ public override int SizeOf
set => throw new InvalidOperationException("This type does not support SizeOf");
}

/// <inheritdoc />
public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is CppTemplateParameterNonType other && Equals(other);
}

/// <inheritdoc />
public override int GetHashCode()
{
unchecked
{
return (base.GetHashCode() * 397) ^ Name.GetHashCode();
}
}

/// <inheritdoc />
public override CppType GetCanonicalType() => this;

Expand Down
15 changes: 0 additions & 15 deletions src/CppAst/CppTemplateParameterType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,6 @@ public override int SizeOf
set => throw new InvalidOperationException("This type does not support SizeOf");
}

/// <inheritdoc />
public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is CppTemplateParameterType other && Equals(other);
}

/// <inheritdoc />
public override int GetHashCode()
{
unchecked
{
return (base.GetHashCode() * 397) ^ Name.GetHashCode();
}
}

/// <inheritdoc />
public override CppType GetCanonicalType() => this;

Expand Down
18 changes: 0 additions & 18 deletions src/CppAst/CppType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,6 @@ protected CppType(CppTypeKind typeKind)

public abstract int SizeOf { get; set; }

protected bool Equals(CppType other)
{
return TypeKind == other.TypeKind;
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj is CppType type && Equals(type);
}

/// <inheritdoc />
public override int GetHashCode()
{
return (int)TypeKind;
}

/// <summary>
/// Gets the canonical type of this type instance.
/// </summary>
Expand Down
Loading

0 comments on commit 151b9df

Please sign in to comment.