Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make opEquals generator easier to understand #485

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 39 additions & 32 deletions src/dparse/ast.d
Original file line number Diff line number Diff line change
Expand Up @@ -401,43 +401,50 @@ mixin template OpEquals(bool print = false)
{
override bool opEquals(Object other) const
{
static if (print)
pragma(msg, generateOpEquals!(typeof(this)));
mixin (generateOpEquals!(typeof(this)));
}
}

template generateOpEquals(T)
{
template opEqualsPart(p ...)
{
import std.traits : isSomeFunction, isDynamicArray;
import std.algorithm : among;

static if (p.length > 1)
{
enum opEqualsPart = opEqualsPart!(p[0 .. $/2]) ~ opEqualsPart!(p[$/2 .. $]);
}
else static if (p.length
&& !__traits(isDeprecated, __traits(getMember, T, p[0]))
&& !isSomeFunction!(typeof(__traits(getMember, T, p[0])))
&& !p[0].among("comment", "line", "column", "endLocation", "startLocation", "index", "dotLocation"))
if (auto obj = cast(typeof(this)) other)
{
static if (isDynamicArray!(typeof(__traits(getMember, T, p[0]))))
foreach (i, field; this.tupleof)
{
enum opEqualsPart = "\tif (obj." ~ p[0] ~ ".length != " ~ p[0] ~ ".length) return false;\n"
~ "\tforeach (i; 0 .. " ~ p[0] ~ ".length)\n"
~ "\t\tif (" ~ p[0] ~ "[i] != obj." ~ p[0] ~ "[i]) return false;\n";
if (typeof(this).tupleof[i].stringof.among(
"comment", "line", "column", "endLocation", "startLocation",
"index", "dotLocation"
))
continue;

if (field != obj.tupleof[i])
return false;
}
else
enum opEqualsPart = "\tif (obj." ~ p[0] ~ " != " ~ p[0] ~ ") return false;\n";
return true;
}
else
enum opEqualsPart = "";
return false;
}
enum generateOpEquals = "if (auto obj = cast(" ~ T.stringof ~ ") other){\n"
~ opEqualsPart!(__traits(derivedMembers, T))
~ "\treturn true;\n}\nreturn false;";
}

unittest
{
auto lhs = new AddExpression();
auto rhs = new AddExpression();
assert(lhs == rhs);
lhs.line = 4;
assert(lhs == rhs);
lhs.operator = tok!"-";
assert(lhs != rhs);
rhs.operator = tok!"-";
assert(lhs == rhs);
}

unittest
{
auto lhs = new AssertArguments();
auto rhs = new AssertArguments();
lhs.assertion = new AddExpression();
rhs.assertion = new AddExpression();
lhs.messageParts = [new NewExpression(), new AddExpression()];
rhs.messageParts = [new NewExpression(), new AddExpression()];
assert(lhs == rhs);
lhs.messageParts = [new NewExpression(), new AddExpression()];
rhs.messageParts = [new AddExpression(), new NewExpression()];
assert(lhs != rhs);
}

abstract class BaseNode : ASTNode
Expand Down