Skip to content

Commit 8d4454b

Browse files
committed
stuff
1 parent 51e90e8 commit 8d4454b

File tree

7 files changed

+102
-141
lines changed

7 files changed

+102
-141
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ add_library(hk_objects OBJECT
122122
"${CMAKE_CURRENT_SOURCE_DIR}/src/utility/datum.cpp"
123123
"${CMAKE_CURRENT_SOURCE_DIR}/src/utility/datum.hpp"
124124
"${CMAKE_CURRENT_SOURCE_DIR}/src/utility/defer.hpp"
125-
"${CMAKE_CURRENT_SOURCE_DIR}/src/utility/enum_variant.hpp"
126125
"${CMAKE_CURRENT_SOURCE_DIR}/src/utility/fixed_fifo.hpp"
127126
"${CMAKE_CURRENT_SOURCE_DIR}/src/utility/fixed_string.hpp"
128127
"${CMAKE_CURRENT_SOURCE_DIR}/src/utility/fqname.cpp"
@@ -201,6 +200,7 @@ if(BUILD_TESTING)
201200
"${CMAKE_CURRENT_SOURCE_DIR}/src/repository/repository_tests.cpp"
202201
"${CMAKE_CURRENT_SOURCE_DIR}/src/tokenizer/tokenizer_semicolon_tests.cpp"
203202
"${CMAKE_CURRENT_SOURCE_DIR}/src/tokenizer/tokenizer_tests.cpp"
203+
"${CMAKE_CURRENT_SOURCE_DIR}/src/utility/defer_tests.cpp"
204204
"${CMAKE_CURRENT_SOURCE_DIR}/src/utility/git_tests.cpp"
205205
"${CMAKE_CURRENT_SOURCE_DIR}/src/utility/fqname_tests.cpp"
206206
"${CMAKE_CURRENT_SOURCE_DIR}/src/utility/logic_tests.cpp"

doc/language/value_categories.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Value Categories
2+
3+
4+
## Move properties
5+
6+
Simple `class` and `enum` types have the following move-properties by default:
7+
8+
* memset: An object can be initialized by setting the members to pre-declared values.
9+
* memcopy: An object can be copied/moved using a memory copy.
10+
* memmove: An object can be moved by copying memory then clearing the old.
11+
* memswap: An object can be swapped/moved by swapping memory.
12+
13+
When a destructor is added to a type, the `memcopy` and `memmove` properties are disabled. `memswap` would
14+
almost always still work correctly.
15+
16+
If a default-constructor is added to the type, the `memset` property is disabled.
17+
18+
If you added a `__swap__()` function, `memswap` property is disabled

src/ast/module_declaration_node.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "top_declaration_node.hpp"
55
#include "utility/fqname.hpp"
66
#include "utility/semantic_version.hpp"
7-
#include "utility/enum_variant.hpp"
87
#include <string>
98
#include <filesystem>
109

src/utility/defer.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,39 @@
44

55
#include <concepts>
66
#include <functional>
7+
#include <cassert>
78

89
namespace hk {
910

11+
/** Defer execution till destruction.
12+
*
13+
* Delay execution to the end of a function/block; useful for cleaning non-scoped variables
14+
* with older C-style APIs.
15+
*/
1016
class defer {
1117
public:
18+
/** If not cancelled, execute the deferred callable object.
19+
*/
1220
~defer()
1321
{
1422
if (_f) {
1523
_f();
1624
}
1725
}
1826

27+
/** Construct with a callable object.
28+
*/
1929
[[nodiscard]] defer(std::function<void()> && arg) : _f(std::move(arg)) {}
30+
31+
/** Construct with a callable object.
32+
*/
2033
[[nodiscard]] defer(std::function<void()> const& arg) : _f(arg) {}
2134

35+
/** Cancel execution of the deferred execution.
36+
*/
2237
void cancel()
2338
{
39+
assert(_f != nullptr);
2440
_f = nullptr;
2541
}
2642

src/utility/defer_tests.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
#include "defer.hpp"
3+
#include <hikotest/hikotest.hpp>
4+
5+
TEST_SUITE(defer_suite)
6+
{
7+
TEST_CASE(simple) {
8+
int i = 1;
9+
10+
{
11+
auto const d1 = hk::defer([&]{
12+
i += 8;
13+
});
14+
15+
auto const d2 = hk::defer([&]{
16+
i *= 3;
17+
});
18+
}
19+
20+
// If executed in the wrong order i == 27.
21+
REQUIRE(i == 11);
22+
}
23+
};

src/utility/enum_variant.hpp

Lines changed: 0 additions & 118 deletions
This file was deleted.

src/utility/logic.hpp

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,47 @@
44
#include <cstdint>
55
#include <utility>
66
#include <exception>
7+
#include <string_view>
78

89
namespace hk {
910

11+
/** Logic type.
12+
*
13+
* This is a logic-type a smaller version of VHDL's std_logic.
14+
*
15+
*/
1016
enum class logic : uint8_t {
17+
/** False */
1118
F,
19+
/** True */
1220
T,
21+
/** Error */
1322
X,
23+
/** Don't Care (third state) */
1424
_,
1525
};
1626

17-
template<size_t N>
18-
[[nodiscard]] constexpr auto make_logic_truth_table(char const *str)
27+
/** Calculate a truth-table and pack it into a integer.
28+
*
29+
* @param str A set of values represented by '0', '1', 'X', '-'. The left
30+
* most character will places in the least significant 2 bits.
31+
*/
32+
[[nodiscard]] constexpr uint32_t make_logic_truth_table(std::string_view str)
1933
{
20-
using int_type = std::conditional_t<N == 16, uint32_t, uint8_t>;
21-
auto r = int_type{};
34+
auto r = uint32_t{0};
2235

23-
for (auto i = N; i != 0; --i) {
36+
for (auto i = str.size(); i != 0; --i) {
2437
r <<= 2;
2538
switch (str[i - 1]) {
26-
case 'F':
2739
case '0':
2840
r |= std::to_underlying(logic::F);
2941
break;
30-
case 'T':
3142
case '1':
3243
r |= std::to_underlying(logic::T);
3344
break;
3445
case 'X':
3546
r |= std::to_underlying(logic::X);
3647
break;
37-
case '_':
3848
case '-':
3949
r |= std::to_underlying(logic::_);
4050
break;
@@ -45,11 +55,25 @@ template<size_t N>
4555
return r;
4656
}
4757

48-
struct logic_binary_truth_table {
58+
/** Create a truth table for logic functions.
59+
*
60+
*/
61+
struct logic_truth_table {
4962
uint32_t _table;
5063

51-
[[nodiscard]] constexpr logic_binary_truth_table(char const str[17]) : _table(make_logic_truth_table<16>(str)) {}
52-
64+
/** Create a truth table.
65+
*
66+
* @param str A set of values represented by '0', '1', 'X', '-'. The left
67+
* most character will places in the least significant 2 bits.
68+
*/
69+
[[nodiscard]] constexpr logic_truth_table(std::string_view str) : _table(make_logic_truth_table(str)) {}
70+
71+
/** Extract a value from the table.
72+
*
73+
* @param lhs Index on the minor axis.
74+
* @param rhs Index on the major axis.
75+
* @return The value at the given coordinates
76+
*/
5377
[[nodiscard]] constexpr logic operator[](logic lhs, logic rhs) const noexcept
5478
{
5579
auto tmp = _table;
@@ -58,13 +82,12 @@ struct logic_binary_truth_table {
5882
tmp &= 0b11;
5983
return static_cast<logic>(tmp);
6084
}
61-
};
62-
63-
struct logic_unary_truth_table {
64-
uint8_t _table;
65-
66-
[[nodiscard]] constexpr logic_unary_truth_table(char const str[5]) : _table(make_logic_truth_table<4>(str)) {}
6785

86+
/** Extract a value from the table.
87+
*
88+
* @param lhs Index in the table.
89+
* @return The value at the given index
90+
*/
6891
[[nodiscard]] constexpr logic operator[](logic rhs) const noexcept
6992
{
7093
auto tmp = _table;
@@ -107,7 +130,7 @@ struct logic_unary_truth_table {
107130
*/
108131
[[nodiscard]] constexpr logic operator|(logic lhs, logic rhs) noexcept
109132
{
110-
constexpr auto tt = logic_binary_truth_table{"01X-11X1XXXX-1X-"};
133+
constexpr auto tt = logic_truth_table{"01X-11X1XXXX-1X-"};
111134
return tt[lhs, rhs];
112135
}
113136

@@ -122,7 +145,7 @@ struct logic_unary_truth_table {
122145
*/
123146
[[nodiscard]] constexpr logic operator&(logic lhs, logic rhs) noexcept
124147
{
125-
constexpr auto tt = logic_binary_truth_table{"00X001X-XXXX0-X-"};
148+
constexpr auto tt = logic_truth_table{"00X001X-XXXX0-X-"};
126149
return tt[lhs, rhs];
127150
}
128151

@@ -137,7 +160,7 @@ struct logic_unary_truth_table {
137160
*/
138161
[[nodiscard]] constexpr logic operator^(logic lhs, logic rhs) noexcept
139162
{
140-
constexpr auto tt = logic_binary_truth_table{"01X-10X-XXXX--X-"};
163+
constexpr auto tt = logic_truth_table{"01X-10X-XXXX--X-"};
141164
return tt[lhs, rhs];
142165
}
143166

@@ -149,7 +172,7 @@ struct logic_unary_truth_table {
149172
*/
150173
[[nodiscard]] constexpr logic operator~(logic rhs) noexcept
151174
{
152-
constexpr auto tt = logic_unary_truth_table{"10X-"};
175+
constexpr auto tt = logic_truth_table{"10X-"};
153176
return tt[rhs];
154177
}
155178

0 commit comments

Comments
 (0)