git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: add proper hash support for map_key_t
24f73baAndy Arvanitis
pcc/include/map_key.hh+9−20
@@ -15,42 +15,31 @@
15#ifndef PS_MapKey_HH
15#ifndef PS_MapKey_HH
16#define PS_MapKey_HH
16#define PS_MapKey_HH
17
17
18+#include <utility>
18#include "hash.hh"
19#include "hash.hh"
19
20
20namespace PureScript {
21namespace PureScript {
21
22
22− struct map_key_t {
23+ class map_key_t : public std::pair<uint32_t, std::string> {
23−
24+ public:
24− const uint32_t hash;
25+ using std::pair<uint32_t, std::string>::pair;
25−
26− explicit constexpr map_key_t(const uint32_t hash) : hash(hash) {}
27
26
28 class hasher {
27 class hasher {
29 public:
28 public:
30− constexpr auto operator() (const map_key_t key) const -> size_t {
29+ auto operator() (const map_key_t& key) const -> size_t {
31− return key.hash;
30+ return key.first;
32 }
31 }
33 };
32 };
34
33
35 class equal {
34 class equal {
36 public:
35 public:
37− constexpr auto operator() (const map_key_t key1, const map_key_t key2) const -> bool {
36+ auto operator() (const map_key_t& key1, const map_key_t& key2) const -> bool {
38− return key1.hash == key2.hash;
37+ return key1.second == key2.second;
39 }
38 }
40 };
39 };
41 };
40 };
42
41
43− // Compile-time map keys (from strings)
42+ #define KEY(s) map_key_t(HASH(s), s)
44− //
45− template <uint32_t H>
46− struct _compile_time_key_ {
47− static constexpr map_key_t value = map_key_t(H);
48− };
49−
50− template <uint32_t H>
51− constexpr map_key_t _compile_time_key_<H>::value;
52−
53− #define KEY(s) _compile_time_key_<djb2(s)>::value
54
43
55} // namespace PureScript
44} // namespace PureScript
56
45
src/Language/PureScript/CodeGen/Cpp.hs+2−2
@@ -148,7 +148,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
148 | (f' : fs') <- flds = [CppReturn $ CppLambda [CppCaptureAll] (farg <$> [f'])
148 | (f' : fs') <- flds = [CppReturn $ CppLambda [CppCaptureAll] (farg <$> [f'])
149 (Just $ CppAny [])
149 (Just $ CppAny [])
150 (CppBlock $ fieldLambdas fs')]
150 (CppBlock $ fieldLambdas fs')]
151− | otherwise = [CppReturn (CppObjectLiteral ((ctorKey, CppStringLiteralHash name) : zip fields' (CppVar <$> fields')))]
151+ | otherwise = [CppReturn (CppObjectLiteral ((ctorKey, CppStringLiteral name) : zip fields' (CppVar <$> fields')))]
152
152
153 declToCpp _ ident val = do
153 declToCpp _ ident val = do
154 val' <- valueToCpp val
154 val' <- valueToCpp val
@@ -406,7 +406,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
406 ProductType -> cpps
406 ProductType -> cpps
407 SumType ->
407 SumType ->
408 [ CppIfElse (CppBinary Equal (CppIndexer (CppStringLiteral ctorKey) (CppVar varName))
408 [ CppIfElse (CppBinary Equal (CppIndexer (CppStringLiteral ctorKey) (CppVar varName))
409− (CppStringLiteralHash ctor'))
409+ (CppStringLiteral ctor'))
410 (CppBlock cpps)
410 (CppBlock cpps)
411 Nothing ]
411 Nothing ]
412 where
412 where
src/Language/PureScript/CodeGen/Cpp/AST.hs−4
@@ -249,10 +249,6 @@ data Cpp
249 --
249 --
250 | CppContinue
250 | CppContinue
251 -- |
251 -- |
252− -- A string literal compile-time hash
253− --
254− | CppStringLiteralHash String
255− -- |
256 -- Empty statement/expression
252 -- Empty statement/expression
257 --
253 --
258 | CppNoOp
254 | CppNoOp
src/Language/PureScript/Pretty/Cpp.hs−1
@@ -55,7 +55,6 @@ literals = mkPattern' match
55 match (CppNumericLiteral n) = return $ either show show n
55 match (CppNumericLiteral n) = return $ either show show n
56 match (CppStringLiteral s) | all isAscii s = return $ string s
56 match (CppStringLiteral s) | all isAscii s = return $ string s
57 match (CppStringLiteral s) = return $ "u8" ++ string s
57 match (CppStringLiteral s) = return $ "u8" ++ string s
58− match (CppStringLiteralHash s) = return $ "HASH" ++ parens (string s)
59 match (CppCharLiteral c) = return $ show c
58 match (CppCharLiteral c) = return $ show c
60 match (CppBooleanLiteral True) = return "true"
59 match (CppBooleanLiteral True) = return "true"
61 match (CppBooleanLiteral False) = return "false"
60 match (CppBooleanLiteral False) = return "false"