Skip to content

Commit

Permalink
AK+Everywhere: Replace DistinctNumeric bool parameters with named ones
Browse files Browse the repository at this point in the history
This means that rather than this:

```
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false,
    false, true, FunctionAddress);
```

We now have this:
```
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, FunctionAddress, Arithmetic,
    Comparison, Increment);
```

Which is a lot more readable. :^)

Co-authored-by: Ali Mohammad Pur <[email protected]>
  • Loading branch information
AtkinsSJ and alimpfard committed Nov 11, 2022
1 parent 24c0a6e commit c33eae2
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 92 deletions.
13 changes: 6 additions & 7 deletions AK/ArbitrarySizedEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,12 @@ struct ArbitrarySizedEnum : public T {
}
};

#define AK_MAKE_ARBITRARY_SIZED_ENUM(EnumName, T, ...) \
namespace EnumName { \
using EnumName = ArbitrarySizedEnum<DistinctNumeric<T, struct __##EnumName##Tag, \
false, true, false, false, false, false>>; \
using Type = EnumName; \
using UnderlyingType = T; \
inline constexpr static EnumName __VA_ARGS__; \
#define AK_MAKE_ARBITRARY_SIZED_ENUM(EnumName, T, ...) \
namespace EnumName { \
using EnumName = ArbitrarySizedEnum<DistinctNumeric<T, struct __##EnumName##Tag, AK::DistinctNumericFeature::Comparison>>; \
using Type = EnumName; \
using UnderlyingType = T; \
inline constexpr static EnumName __VA_ARGS__; \
}

}
Expand Down
166 changes: 104 additions & 62 deletions AK/DistinctNumeric.h

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions Tests/AK/TestDistinctNumeric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ForType {
public:
static void check_size()
{
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(T, false, false, false, false, false, false, TheNumeric);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(T, TheNumeric);
EXPECT_EQ(sizeof(T), sizeof(TheNumeric));
}
};
Expand All @@ -34,14 +34,14 @@ TEST_CASE(check_size)
ForType<double>::check_size();
}

AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, false, false, false, false, false, false, BareNumeric);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, true, false, false, false, false, false, IncrNumeric);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, false, true, false, false, false, false, CmpNumeric);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, false, false, true, false, false, false, BoolNumeric);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, false, false, false, true, false, false, FlagsNumeric);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, false, false, false, false, true, false, ShiftNumeric);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, false, false, false, false, false, true, ArithNumeric);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, true, true, true, true, true, true, GeneralNumeric);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, BareNumeric);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, IncrNumeric, Increment);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, CmpNumeric, Comparison);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, BoolNumeric, CastToBool);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, FlagsNumeric, Flags);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, ShiftNumeric, Shift);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, ArithNumeric, Arithmetic);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(int, GeneralNumeric, Arithmetic, CastToBool, Comparison, Flags, Increment, Shift);

TEST_CASE(address_identity)
{
Expand Down Expand Up @@ -230,21 +230,21 @@ TEST_CASE(negative_incr)
{
BareNumeric a = 12;
a++;
// error: static assertion failed: 'a++' is only available for DistinctNumeric types with 'Incr'.
// error: static assertion failed: 'a++' is only available for DistinctNumeric types with 'Increment'.
}

TEST_CASE(negative_cmp)
{
BareNumeric a = 12;
[[maybe_unused]] auto res = (a < a);
// error: static assertion failed: 'a<b' is only available for DistinctNumeric types with 'Cmp'.
// error: static assertion failed: 'a<b' is only available for DistinctNumeric types with 'Comparison'.
}

TEST_CASE(negative_bool)
{
BareNumeric a = 12;
[[maybe_unused]] auto res = !a;
// error: static assertion failed: '!a', 'a&&b', 'a||b' and similar operators are only available for DistinctNumeric types with 'Bool'.
// error: static assertion failed: '!a', 'a&&b', 'a||b' and similar operators are only available for DistinctNumeric types with 'CastToBool'.
}

TEST_CASE(negative_flags)
Expand All @@ -265,7 +265,7 @@ TEST_CASE(negative_arith)
{
BareNumeric a = 12;
[[maybe_unused]] auto res = (a + a);
// error: static assertion failed: 'a+b' is only available for DistinctNumeric types with 'Arith'.
// error: static assertion failed: 'a+b' is only available for DistinctNumeric types with 'Arithmetic'.
}

TEST_CASE(negative_incompatible)
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibJS/Bytecode/IdentifierTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace JS::Bytecode {

AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(size_t, false, true, false, false, false, false, IdentifierTableIndex);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(size_t, IdentifierTableIndex, Comparison);

class IdentifierTable {
AK_MAKE_NONMOVABLE(IdentifierTable);
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibJS/Bytecode/StringTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace JS::Bytecode {

AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(size_t, false, true, false, false, false, false, StringTableIndex);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(size_t, StringTableIndex, Comparison);

class StringTable {
AK_MAKE_NONMOVABLE(StringTable);
Expand Down
14 changes: 7 additions & 7 deletions Userland/Libraries/LibWasm/AbstractMachine/AbstractMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ struct LinkError {
Vector<OtherErrors> other_errors;
};

AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, FunctionAddress);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, ExternAddress);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, TableAddress);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, GlobalAddress);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, ElementAddress);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, DataAddress);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, MemoryAddress);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, FunctionAddress, Arithmetic, Comparison, Increment);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, ExternAddress, Arithmetic, Comparison, Increment);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, TableAddress, Arithmetic, Comparison, Increment);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, GlobalAddress, Arithmetic, Comparison, Increment);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, ElementAddress, Arithmetic, Comparison, Increment);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, DataAddress, Arithmetic, Comparison, Increment);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, MemoryAddress, Arithmetic, Comparison, Increment);

// FIXME: These should probably be made generic/virtual if/when we decide to do something more
// fancy than just a dumb interpreter.
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWasm/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ AK_TYPEDEF_DISTINCT_ORDERED_ID(size_t, LocalIndex);
AK_TYPEDEF_DISTINCT_ORDERED_ID(size_t, GlobalIndex);
AK_TYPEDEF_DISTINCT_ORDERED_ID(size_t, LabelIndex);
AK_TYPEDEF_DISTINCT_ORDERED_ID(size_t, DataIndex);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, true, false, true, InstructionPointer);
AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, InstructionPointer, Arithmetic, Comparison, Flags, Increment);

ParseError with_eof_check(InputStream const& stream, ParseError error_if_not_eof);

Expand Down

0 comments on commit c33eae2

Please sign in to comment.