prism 2.0.0-aarch64-mingw-ucrt

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (232) hide show
  1. checksums.yaml +7 -0
  2. data/BSDmakefile +58 -0
  3. data/CHANGELOG.md +881 -0
  4. data/CODE_OF_CONDUCT.md +76 -0
  5. data/CONTRIBUTING.md +58 -0
  6. data/LICENSE.md +7 -0
  7. data/Makefile +145 -0
  8. data/README.md +81 -0
  9. data/config.yml +4752 -0
  10. data/docs/build_system.md +119 -0
  11. data/docs/configuration.md +69 -0
  12. data/docs/cruby_compilation.md +27 -0
  13. data/docs/design.md +53 -0
  14. data/docs/encoding.md +121 -0
  15. data/docs/fuzzing.md +81 -0
  16. data/docs/heredocs.md +36 -0
  17. data/docs/javascript.md +133 -0
  18. data/docs/local_variable_depth.md +229 -0
  19. data/docs/mapping.md +117 -0
  20. data/docs/parser_translation.md +24 -0
  21. data/docs/parsing_rules.md +22 -0
  22. data/docs/releasing.md +147 -0
  23. data/docs/relocation.md +32 -0
  24. data/docs/ripper_translation.md +63 -0
  25. data/docs/ruby_api.md +45 -0
  26. data/docs/ruby_parser_translation.md +19 -0
  27. data/docs/serialization.md +244 -0
  28. data/docs/testing.md +72 -0
  29. data/ext/prism/api_node.c +7005 -0
  30. data/ext/prism/extconf.rb +126 -0
  31. data/ext/prism/extension.c +1768 -0
  32. data/ext/prism/extension.h +19 -0
  33. data/include/prism/arena.h +45 -0
  34. data/include/prism/arena.hpp +42 -0
  35. data/include/prism/ast.h +10381 -0
  36. data/include/prism/ast.hpp +641 -0
  37. data/include/prism/buffer.h +60 -0
  38. data/include/prism/buffer.hpp +55 -0
  39. data/include/prism/comments.h +72 -0
  40. data/include/prism/compiler/accel.h +26 -0
  41. data/include/prism/compiler/align.h +36 -0
  42. data/include/prism/compiler/assume.h +25 -0
  43. data/include/prism/compiler/branch.h +26 -0
  44. data/include/prism/compiler/bswap.h +46 -0
  45. data/include/prism/compiler/cplusplus.hpp +16 -0
  46. data/include/prism/compiler/endianness.h +34 -0
  47. data/include/prism/compiler/exceptions.hpp +17 -0
  48. data/include/prism/compiler/exported.h +24 -0
  49. data/include/prism/compiler/fallthrough.h +22 -0
  50. data/include/prism/compiler/filesystem.h +32 -0
  51. data/include/prism/compiler/flex_array.h +19 -0
  52. data/include/prism/compiler/force_inline.h +21 -0
  53. data/include/prism/compiler/format.h +27 -0
  54. data/include/prism/compiler/inline.h +17 -0
  55. data/include/prism/compiler/lifetimebound.hpp +25 -0
  56. data/include/prism/compiler/nodiscard.h +22 -0
  57. data/include/prism/compiler/nonnull.h +18 -0
  58. data/include/prism/compiler/unused.h +18 -0
  59. data/include/prism/constant_pool.h +106 -0
  60. data/include/prism/diagnostic.h +111 -0
  61. data/include/prism/errors_format.h +61 -0
  62. data/include/prism/excludes.h +29 -0
  63. data/include/prism/integer.h +41 -0
  64. data/include/prism/internal/allocator.h +68 -0
  65. data/include/prism/internal/allocator_debug.h +88 -0
  66. data/include/prism/internal/arena.h +108 -0
  67. data/include/prism/internal/bit.h +42 -0
  68. data/include/prism/internal/buffer.h +94 -0
  69. data/include/prism/internal/char.h +145 -0
  70. data/include/prism/internal/comments.h +53 -0
  71. data/include/prism/internal/constant_pool.h +152 -0
  72. data/include/prism/internal/diagnostic.h +389 -0
  73. data/include/prism/internal/encoding.h +242 -0
  74. data/include/prism/internal/integer.h +68 -0
  75. data/include/prism/internal/isinf.h +16 -0
  76. data/include/prism/internal/line_offset_list.h +34 -0
  77. data/include/prism/internal/list.h +62 -0
  78. data/include/prism/internal/magic_comments.h +23 -0
  79. data/include/prism/internal/memchr.h +15 -0
  80. data/include/prism/internal/node.h +42 -0
  81. data/include/prism/internal/options.h +232 -0
  82. data/include/prism/internal/parser.h +1004 -0
  83. data/include/prism/internal/regexp.h +26 -0
  84. data/include/prism/internal/serialize.h +39 -0
  85. data/include/prism/internal/source.h +72 -0
  86. data/include/prism/internal/static_literals.h +100 -0
  87. data/include/prism/internal/stringy.h +30 -0
  88. data/include/prism/internal/strncasecmp.h +18 -0
  89. data/include/prism/internal/strpbrk.h +33 -0
  90. data/include/prism/internal/tokens.h +11 -0
  91. data/include/prism/internal/xxh3.h +16 -0
  92. data/include/prism/json.h +40 -0
  93. data/include/prism/line_offset_list.h +69 -0
  94. data/include/prism/magic_comments.h +53 -0
  95. data/include/prism/node.h +139 -0
  96. data/include/prism/node.hpp +272 -0
  97. data/include/prism/node_type.hpp +38 -0
  98. data/include/prism/options.h +363 -0
  99. data/include/prism/options.hpp +43 -0
  100. data/include/prism/parser.h +433 -0
  101. data/include/prism/parser.hpp +274 -0
  102. data/include/prism/prettyprint.h +39 -0
  103. data/include/prism/serialize.h +123 -0
  104. data/include/prism/source.h +163 -0
  105. data/include/prism/stream.h +36 -0
  106. data/include/prism/string_query.h +82 -0
  107. data/include/prism/stringy.h +61 -0
  108. data/include/prism/stringy.hpp +31 -0
  109. data/include/prism/version.h +46 -0
  110. data/include/prism.h +123 -0
  111. data/include/prism.hpp +24 -0
  112. data/lib/prism/3.4/prism.so +0 -0
  113. data/lib/prism/4.0/prism.so +0 -0
  114. data/lib/prism/compiler.rb +815 -0
  115. data/lib/prism/desugar_compiler.rb +463 -0
  116. data/lib/prism/dispatcher.rb +2383 -0
  117. data/lib/prism/dot_visitor.rb +4848 -0
  118. data/lib/prism/dsl.rb +1369 -0
  119. data/lib/prism/ffi.rb +746 -0
  120. data/lib/prism/inspect_visitor.rb +2425 -0
  121. data/lib/prism/lex_compat.rb +953 -0
  122. data/lib/prism/mutation_compiler.rb +780 -0
  123. data/lib/prism/node.rb +33940 -0
  124. data/lib/prism/node_ext.rb +504 -0
  125. data/lib/prism/node_find.rb +215 -0
  126. data/lib/prism/parse_result/errors.rb +72 -0
  127. data/lib/prism/parse_result/newlines.rb +423 -0
  128. data/lib/prism/parse_result.rb +1236 -0
  129. data/lib/prism/pattern.rb +314 -0
  130. data/lib/prism/polyfill/append_as_bytes.rb +15 -0
  131. data/lib/prism/polyfill/byteindex.rb +13 -0
  132. data/lib/prism/polyfill/scan_byte.rb +14 -0
  133. data/lib/prism/polyfill/unpack1.rb +14 -0
  134. data/lib/prism/polyfill/warn.rb +36 -0
  135. data/lib/prism/reflection.rb +427 -0
  136. data/lib/prism/relocation.rb +567 -0
  137. data/lib/prism/serialize.rb +4589 -0
  138. data/lib/prism/string_query.rb +54 -0
  139. data/lib/prism/translation/parser/builder.rb +70 -0
  140. data/lib/prism/translation/parser/compiler.rb +2226 -0
  141. data/lib/prism/translation/parser/lexer.rb +769 -0
  142. data/lib/prism/translation/parser.rb +376 -0
  143. data/lib/prism/translation/parser_current.rb +26 -0
  144. data/lib/prism/translation/parser_versions.rb +36 -0
  145. data/lib/prism/translation/ripper/filter.rb +53 -0
  146. data/lib/prism/translation/ripper/lexer.rb +133 -0
  147. data/lib/prism/translation/ripper/sexp.rb +118 -0
  148. data/lib/prism/translation/ripper/shim.rb +7 -0
  149. data/lib/prism/translation/ripper.rb +4278 -0
  150. data/lib/prism/translation/ruby_parser.rb +1721 -0
  151. data/lib/prism/translation.rb +20 -0
  152. data/lib/prism/visitor.rb +1440 -0
  153. data/lib/prism.rb +168 -0
  154. data/prism.gemspec +251 -0
  155. data/rbi/generated/prism/compiler.rbi +489 -0
  156. data/rbi/generated/prism/desugar_compiler.rbi +306 -0
  157. data/rbi/generated/prism/dispatcher.rbi +1138 -0
  158. data/rbi/generated/prism/dot_visitor.rbi +618 -0
  159. data/rbi/generated/prism/dsl.rbi +751 -0
  160. data/rbi/generated/prism/inspect_visitor.rbi +503 -0
  161. data/rbi/generated/prism/lex_compat.rbi +162 -0
  162. data/rbi/generated/prism/mutation_compiler.rbi +464 -0
  163. data/rbi/generated/prism/node.rbi +15143 -0
  164. data/rbi/generated/prism/node_ext.rbi +259 -0
  165. data/rbi/generated/prism/node_find.rbi +79 -0
  166. data/rbi/generated/prism/parse_result/errors.rbi +21 -0
  167. data/rbi/generated/prism/parse_result/newlines.rbi +208 -0
  168. data/rbi/generated/prism/parse_result.rbi +776 -0
  169. data/rbi/generated/prism/pattern.rbi +134 -0
  170. data/rbi/generated/prism/reflection.rbi +89 -0
  171. data/rbi/generated/prism/relocation.rbi +329 -0
  172. data/rbi/generated/prism/serialize.rbi +185 -0
  173. data/rbi/generated/prism/string_query.rbi +43 -0
  174. data/rbi/generated/prism/translation.rbi +7 -0
  175. data/rbi/generated/prism/visitor.rbi +650 -0
  176. data/rbi/generated/prism.rbi +86 -0
  177. data/rbi/prism/translation/parser.rbi +11 -0
  178. data/rbi/prism/translation/parser_versions.rbi +28 -0
  179. data/rbi/prism/translation/ripper.rbi +15 -0
  180. data/rbi/rubyvm/node_find.rbi +14 -0
  181. data/sig/generated/prism/compiler.rbs +492 -0
  182. data/sig/generated/prism/desugar_compiler.rbs +305 -0
  183. data/sig/generated/prism/dispatcher.rbs +1292 -0
  184. data/sig/generated/prism/dot_visitor.rbs +635 -0
  185. data/sig/generated/prism/dsl.rbs +928 -0
  186. data/sig/generated/prism/inspect_visitor.rbs +509 -0
  187. data/sig/generated/prism/lex_compat.rbs +166 -0
  188. data/sig/generated/prism/mutation_compiler.rbs +464 -0
  189. data/sig/generated/prism/node.rbs +21632 -0
  190. data/sig/generated/prism/node_ext.rbs +290 -0
  191. data/sig/generated/prism/node_find.rbs +90 -0
  192. data/sig/generated/prism/parse_result/errors.rbs +22 -0
  193. data/sig/generated/prism/parse_result/newlines.rbs +223 -0
  194. data/sig/generated/prism/parse_result.rbs +871 -0
  195. data/sig/generated/prism/pattern.rbs +156 -0
  196. data/sig/generated/prism/reflection.rbs +101 -0
  197. data/sig/generated/prism/relocation.rbs +428 -0
  198. data/sig/generated/prism/serialize.rbs +200 -0
  199. data/sig/generated/prism/string_query.rbs +43 -0
  200. data/sig/generated/prism/translation.rbs +8 -0
  201. data/sig/generated/prism/visitor.rbs +1111 -0
  202. data/sig/generated/prism.rbs +84 -0
  203. data/sig/manifest.yaml +2 -0
  204. data/src/arena.c +117 -0
  205. data/src/buffer.c +389 -0
  206. data/src/char.c +289 -0
  207. data/src/comments.c +454 -0
  208. data/src/constant_pool.c +406 -0
  209. data/src/diagnostic.c +894 -0
  210. data/src/encoding.c +5345 -0
  211. data/src/errors_format.c +519 -0
  212. data/src/integer.c +681 -0
  213. data/src/json.c +5733 -0
  214. data/src/line_offset_list.c +100 -0
  215. data/src/list.c +24 -0
  216. data/src/memchr.c +37 -0
  217. data/src/node.c +4363 -0
  218. data/src/options.c +445 -0
  219. data/src/parser.c +371 -0
  220. data/src/prettyprint.c +9024 -0
  221. data/src/prism.c +23901 -0
  222. data/src/regexp.c +1705 -0
  223. data/src/serialize.c +2660 -0
  224. data/src/source.c +524 -0
  225. data/src/static_literals.c +685 -0
  226. data/src/string_query.c +180 -0
  227. data/src/stringy.c +61 -0
  228. data/src/strncasecmp.c +37 -0
  229. data/src/strpbrk.c +447 -0
  230. data/src/tokens.c +724 -0
  231. data/src/xxh3.c +292 -0
  232. metadata +275 -0
@@ -0,0 +1,60 @@
1
+ /**
2
+ * @file buffer.h
3
+ *
4
+ * A wrapper around a contiguous block of allocated memory.
5
+ */
6
+ #ifndef PRISM_BUFFER_H
7
+ #define PRISM_BUFFER_H
8
+
9
+ #include "prism/compiler/exported.h"
10
+ #include "prism/compiler/nodiscard.h"
11
+ #include "prism/compiler/nonnull.h"
12
+
13
+ #include <stddef.h>
14
+
15
+ #ifdef __cplusplus
16
+ extern "C" {
17
+ #endif
18
+
19
+ /**
20
+ * A wrapper around a contiguous block of allocated memory.
21
+ */
22
+ typedef struct pm_buffer_t pm_buffer_t;
23
+
24
+ /**
25
+ * Allocate and initialize a new buffer. If the buffer cannot be allocated, this
26
+ * function will abort the process.
27
+ *
28
+ * @returns A pointer to the initialized buffer. The caller is responsible for
29
+ * freeing the buffer with pm_buffer_free.
30
+ */
31
+ PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_buffer_t * pm_buffer_new(void);
32
+
33
+ /**
34
+ * Free both the memory held by the buffer and the buffer itself.
35
+ *
36
+ * @param buffer The buffer to free.
37
+ */
38
+ PRISM_EXPORTED_FUNCTION void pm_buffer_free(pm_buffer_t *buffer) PRISM_NONNULL(1);
39
+
40
+ /**
41
+ * Return the value of the buffer.
42
+ *
43
+ * @param buffer The buffer to get the value of.
44
+ * @returns The value of the buffer.
45
+ */
46
+ PRISM_EXPORTED_FUNCTION char * pm_buffer_value(const pm_buffer_t *buffer) PRISM_NONNULL(1);
47
+
48
+ /**
49
+ * Return the length of the buffer.
50
+ *
51
+ * @param buffer The buffer to get the length of.
52
+ * @returns The length of the buffer.
53
+ */
54
+ PRISM_EXPORTED_FUNCTION size_t pm_buffer_length(const pm_buffer_t *buffer) PRISM_NONNULL(1);
55
+
56
+ #ifdef __cplusplus
57
+ }
58
+ #endif
59
+
60
+ #endif
@@ -0,0 +1,55 @@
1
+ /**
2
+ * @file buffer.hpp
3
+ *
4
+ * Ownership of buffers from C++, and access to their contents.
5
+ */
6
+ #ifndef PRISM_BUFFER_HPP
7
+ #define PRISM_BUFFER_HPP
8
+
9
+ #include "prism/compiler/cplusplus.hpp"
10
+ #include "prism/compiler/lifetimebound.hpp"
11
+
12
+ #include "prism/buffer.h"
13
+
14
+ #include <memory>
15
+ #include <string_view>
16
+
17
+ namespace prism {
18
+
19
+ /** Frees a buffer with pm_buffer_free. */
20
+ struct buffer_deleter {
21
+ /**
22
+ * Frees the given buffer.
23
+ *
24
+ * @param buffer The buffer to free.
25
+ */
26
+ void operator()(pm_buffer_t *buffer) const noexcept { pm_buffer_free(buffer); }
27
+ };
28
+
29
+ /** An owned buffer. */
30
+ using unique_buffer = std::unique_ptr<pm_buffer_t, buffer_deleter>;
31
+
32
+ /**
33
+ * Allocate a new empty buffer. Allocation failure aborts, so this never throws.
34
+ *
35
+ * @return A unique_buffer managing the newly allocated buffer.
36
+ */
37
+ [[nodiscard]] inline unique_buffer
38
+ make_buffer() noexcept {
39
+ return unique_buffer(pm_buffer_new());
40
+ }
41
+
42
+ /**
43
+ * The bytes written to the given buffer so far.
44
+ *
45
+ * @param buffer The buffer to view.
46
+ * @return A string_view of the contents of the buffer.
47
+ */
48
+ [[nodiscard]] inline std::string_view
49
+ view(const pm_buffer_t *buffer PRISM_LIFETIMEBOUND) noexcept {
50
+ return std::string_view(pm_buffer_value(buffer), pm_buffer_length(buffer));
51
+ }
52
+
53
+ }
54
+
55
+ #endif
@@ -0,0 +1,72 @@
1
+ /**
2
+ * @file comments.h
3
+ *
4
+ * Types and functions related to comments found during parsing.
5
+ */
6
+ #ifndef PRISM_COMMENTS_H
7
+ #define PRISM_COMMENTS_H
8
+
9
+ #include "prism/compiler/exported.h"
10
+ #include "prism/compiler/nodiscard.h"
11
+ #include "prism/compiler/nonnull.h"
12
+
13
+ #include "prism/ast.h"
14
+
15
+ #include <stddef.h>
16
+
17
+ #ifdef __cplusplus
18
+ extern "C" {
19
+ #endif
20
+
21
+ /** This is the type of a comment that we've found while parsing. */
22
+ typedef enum {
23
+ PM_COMMENT_INLINE,
24
+ PM_COMMENT_EMBDOC
25
+ } pm_comment_type_t;
26
+
27
+ /** An opaque pointer to a comment found while parsing. */
28
+ typedef struct pm_comment_t pm_comment_t;
29
+
30
+ /**
31
+ * The comments owned by a node, as a slice of the comment attachments of the
32
+ * parser that produced it.
33
+ */
34
+ typedef struct {
35
+ /** The index of the first attachment owned by the node. */
36
+ uint32_t index;
37
+
38
+ /** The number of attachments owned by the node. */
39
+ uint32_t length;
40
+ } pm_comment_range_t;
41
+
42
+ /**
43
+ * Returns the location associated with the given comment.
44
+ *
45
+ * @param comment the comment whose location we want to get
46
+ * @returns the location associated with the given comment
47
+ */
48
+ PRISM_EXPORTED_FUNCTION pm_location_t pm_comment_location(const pm_comment_t *comment) PRISM_NONNULL(1);
49
+
50
+ /**
51
+ * Returns the type associated with the given comment.
52
+ *
53
+ * @param comment the comment whose type we want to get
54
+ * @returns the type associated with the given comment. This can either be
55
+ * PM_COMMENT_INLINE or PM_COMMENT_EMBDOC.
56
+ */
57
+ PRISM_EXPORTED_FUNCTION pm_comment_type_t pm_comment_type(const pm_comment_t *comment) PRISM_NONNULL(1);
58
+
59
+ /**
60
+ * Returns the comment that follows the given comment among the comments
61
+ * associated with the parser that found it.
62
+ *
63
+ * @param comment the comment whose successor we want to get
64
+ * @returns the next comment, or NULL if the given comment is the last one
65
+ */
66
+ PRISM_EXPORTED_FUNCTION const pm_comment_t * pm_comment_next(const pm_comment_t *comment) PRISM_NONNULL(1);
67
+
68
+ #ifdef __cplusplus
69
+ }
70
+ #endif
71
+
72
+ #endif
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @file compiler/accel.h
3
+ */
4
+ #ifndef PRISM_COMPILER_ACCEL_H
5
+ #define PRISM_COMPILER_ACCEL_H
6
+
7
+ #include "prism/compiler/endianness.h"
8
+
9
+ /**
10
+ * Platform detection for SIMD/fast-path implementations. At most one of these
11
+ * macros is defined, selecting the best available vectorization strategy.
12
+ *
13
+ * Selection is made at compile time with no runtime CPU detection, and
14
+ * precompiled binaries run on machines other than the one that built them. So
15
+ * each strategy relies only on instructions that the architecture baseline
16
+ * guarantees: NEON is mandatory on AArch64 and SSE2 is mandatory on x86-64.
17
+ */
18
+ #if (defined(__aarch64__) && defined(__ARM_NEON)) || (defined(_MSC_VER) && defined(_M_ARM64))
19
+ # define PRISM_HAS_NEON
20
+ #elif (defined(__x86_64__) && defined(__SSE2__)) || (defined(_MSC_VER) && defined(_M_X64))
21
+ # define PRISM_HAS_SSE2
22
+ #elif defined(PRISM_LITTLE_ENDIAN_COMPTIME)
23
+ # define PRISM_HAS_SWAR
24
+ #endif
25
+
26
+ #endif
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @file compiler/align.h
3
+ */
4
+ #ifndef PRISM_COMPILER_ALIGN_H
5
+ #define PRISM_COMPILER_ALIGN_H
6
+
7
+ /**
8
+ * Compiler-agnostic macros for specifying alignment of types and variables.
9
+ */
10
+ #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L /* C11 or later */
11
+ /** Specify alignment for a type or variable. */
12
+ #define PRISM_ALIGNAS _Alignas
13
+
14
+ /** Get the alignment requirement of a type. */
15
+ #define PRISM_ALIGNOF _Alignof
16
+ #elif defined(__GNUC__) || defined(__clang__)
17
+ /** Specify alignment for a type or variable. */
18
+ #define PRISM_ALIGNAS(size) __attribute__((aligned(size)))
19
+
20
+ /** Get the alignment requirement of a type. */
21
+ #define PRISM_ALIGNOF(type) __alignof__(type)
22
+ #elif defined(_MSC_VER)
23
+ /** Specify alignment for a type or variable. */
24
+ #define PRISM_ALIGNAS(size) __declspec(align(size))
25
+
26
+ /** Get the alignment requirement of a type. */
27
+ #define PRISM_ALIGNOF(type) __alignof(type)
28
+ #else
29
+ /** Void because this platform does not support specifying alignment. */
30
+ #define PRISM_ALIGNAS(size)
31
+
32
+ /** Fallback to sizeof as alignment requirement of a type. */
33
+ #define PRISM_ALIGNOF(type) sizeof(type)
34
+ #endif
35
+
36
+ #endif
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @file compiler/assume.h
3
+ */
4
+ #ifndef PRISM_COMPILER_ASSUME_H
5
+ #define PRISM_COMPILER_ASSUME_H
6
+
7
+ /**
8
+ * Tell the compiler that the given expression can be assumed to be true. Unlike
9
+ * assert, this emits no runtime check — it only feeds the optimizer's value
10
+ * range analysis so it can prune impossible paths. Use it to communicate an
11
+ * invariant the caller guarantees but that the compiler cannot otherwise prove.
12
+ */
13
+ #if defined(__clang__)
14
+ #define PRISM_ASSUME(expr_) __builtin_assume(expr_)
15
+ #elif defined(_MSC_VER)
16
+ #define PRISM_ASSUME(expr_) __assume(expr_)
17
+ #elif defined(__GNUC__) && (__GNUC__ >= 13)
18
+ #define PRISM_ASSUME(expr_) __attribute__((assume(expr_)))
19
+ #elif defined(__GNUC__)
20
+ #define PRISM_ASSUME(expr_) ((expr_) ? (void) 0 : __builtin_unreachable())
21
+ #else
22
+ #define PRISM_ASSUME(expr_) ((void) 0)
23
+ #endif
24
+
25
+ #endif
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @file compiler/branch.h
3
+ */
4
+ #ifndef PRISM_COMPILER_BRANCH_H
5
+ #define PRISM_COMPILER_BRANCH_H
6
+
7
+ /**
8
+ * Branch prediction hints. These tell the compiler which side of a branch to
9
+ * predicate the fast path through, so it can lay out instructions to favour
10
+ * that side and steer the static branch predictor.
11
+ */
12
+ #if defined(__GNUC__) || defined(__clang__)
13
+ /** The compiler should predicate that this branch will be taken. */
14
+ #define PRISM_LIKELY(x) __builtin_expect(!!(x), 1)
15
+
16
+ /** The compiler should predicate that this branch will not be taken. */
17
+ #define PRISM_UNLIKELY(x) __builtin_expect(!!(x), 0)
18
+ #else
19
+ /** Void because this platform does not support branch prediction hints. */
20
+ #define PRISM_LIKELY(x) (x)
21
+
22
+ /** Void because this platform does not support branch prediction hints. */
23
+ #define PRISM_UNLIKELY(x) (x)
24
+ #endif
25
+
26
+ #endif
@@ -0,0 +1,46 @@
1
+ /**
2
+ * @file compiler/bswap.h
3
+ */
4
+ #ifndef PRISM_COMPILER_BSWAP_H
5
+ #define PRISM_COMPILER_BSWAP_H
6
+
7
+ /**
8
+ * Byte swap utilities for 32-bit and 64-bit integers.
9
+ */
10
+ #if defined(__GNUC__) || defined(__clang__)
11
+ #define pm_bswap32(x_) __builtin_bswap32((x_))
12
+ #define pm_bswap64(x_) __builtin_bswap64((x_))
13
+ #elif defined(_MSC_VER)
14
+ #include <stdlib.h>
15
+
16
+ #define pm_bswap32(x_) _byteswap_ulong((x_))
17
+ #define pm_bswap64(x_) _byteswap_uint64((x_))
18
+ #else
19
+ #include <stdint.h>
20
+
21
+ static uint32_t
22
+ pm_bswap32(uint32_t x) {
23
+ return (
24
+ ((x >> 24) & 0x000000FFUL) |
25
+ ((x << 24) & 0xFF000000UL) |
26
+ ((x >> 8) & 0x0000FF00UL) |
27
+ ((x << 8) & 0x00FF0000UL)
28
+ );
29
+ }
30
+
31
+ static uint64_t
32
+ pm_bswap64(uint64_t x) {
33
+ return (
34
+ ((x >> 56) & 0x00000000000000FFULL) |
35
+ ((x << 56) & 0xFF00000000000000ULL) |
36
+ ((x >> 40) & 0x000000000000FF00ULL) |
37
+ ((x << 40) & 0x00FF000000000000ULL) |
38
+ ((x >> 24) & 0x0000000000FF0000ULL) |
39
+ ((x << 24) & 0x0000FF0000000000ULL) |
40
+ ((x >> 8) & 0x00000000FF000000ULL) |
41
+ ((x << 8) & 0x000000FF00000000ULL)
42
+ );
43
+ }
44
+ #endif
45
+
46
+ #endif
@@ -0,0 +1,16 @@
1
+ /**
2
+ * @file compiler/cplusplus.hpp
3
+ */
4
+ #ifndef PRISM_COMPILER_CPLUSPLUS_HPP
5
+ #define PRISM_COMPILER_CPLUSPLUS_HPP
6
+
7
+ /**
8
+ * The C++ interface relies on C++17 for inline variables, std::string_view,
9
+ * std::void_t, and std::is_invocable_r_v. MSVC reports __cplusplus as 199711L
10
+ * unless /Zc:__cplusplus is passed, so its own _MSVC_LANG is checked as well.
11
+ */
12
+ #if !defined(__cplusplus) || ((__cplusplus < 201703L) && (!defined(_MSVC_LANG) || (_MSVC_LANG < 201703L)))
13
+ # error "the prism C++ interface requires C++17 or later"
14
+ #endif
15
+
16
+ #endif
@@ -0,0 +1,34 @@
1
+ /**
2
+ * @file compiler/endianness.h
3
+ */
4
+ #ifndef PRISM_COMPILER_ENDIANNESS_H
5
+ #define PRISM_COMPILER_ENDIANNESS_H
6
+
7
+ /**
8
+ * Detect endianness at compile-time through the use of predefined macros.
9
+ */
10
+ #if defined(_WIN32) || defined(__LITTLE_ENDIAN__) || (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
11
+ #define PRISM_LITTLE_ENDIAN_COMPTIME
12
+ #endif
13
+
14
+ /**
15
+ * Determine endianness at runtime, either through predefined macros or through
16
+ * a runtime check that is usually folded.
17
+ */
18
+ #if defined(PRISM_LITTLE_ENDIAN_COMPTIME)
19
+ #define PRISM_LITTLE_ENDIAN_RUNTIME 1
20
+ #elif defined(__BIG_ENDIAN__) || (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
21
+ #define PRISM_LITTLE_ENDIAN_RUNTIME 0
22
+ #else
23
+ #include <stdint.h>
24
+
25
+ static inline int
26
+ pm_little_endian(void) {
27
+ const union { uint32_t u; uint8_t c[4]; } one = { 1 };
28
+ return one.c[0];
29
+ }
30
+
31
+ #define PRISM_LITTLE_ENDIAN_RUNTIME pm_little_endian()
32
+ #endif
33
+
34
+ #endif
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @file compiler/exceptions.hpp
3
+ */
4
+ #ifndef PRISM_COMPILER_EXCEPTIONS_HPP
5
+ #define PRISM_COMPILER_EXCEPTIONS_HPP
6
+
7
+ /**
8
+ * Defined when C++ exceptions are enabled, so that code which catches them can
9
+ * still compile in builds that disable them, such as with -fno-exceptions.
10
+ * __cpp_exceptions is the standard feature test macro, and older versions of
11
+ * GCC and MSVC define __EXCEPTIONS and _CPPUNWIND instead.
12
+ */
13
+ #if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)
14
+ # define PRISM_HAS_EXCEPTIONS
15
+ #endif
16
+
17
+ #endif
@@ -0,0 +1,24 @@
1
+ /**
2
+ * @file compiler/exported.h
3
+ */
4
+ #ifndef PRISM_COMPILER_EXPORTED_H
5
+ #define PRISM_COMPILER_EXPORTED_H
6
+
7
+ /**
8
+ * By default, we compile with -fvisibility=hidden. When this is enabled, we
9
+ * need to mark certain functions as being publically-visible. This macro does
10
+ * that in a compiler-agnostic way.
11
+ */
12
+ #ifndef PRISM_EXPORTED_FUNCTION
13
+ # ifdef PRISM_EXPORT_SYMBOLS
14
+ # ifdef _WIN32
15
+ # define PRISM_EXPORTED_FUNCTION __declspec(dllexport) extern
16
+ # else
17
+ # define PRISM_EXPORTED_FUNCTION __attribute__((__visibility__("default"))) extern
18
+ # endif
19
+ # else
20
+ # define PRISM_EXPORTED_FUNCTION
21
+ # endif
22
+ #endif
23
+
24
+ #endif
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @file compiler/fallthrough.h
3
+ */
4
+ #ifndef PRISM_COMPILER_FALLTHROUGH_H
5
+ #define PRISM_COMPILER_FALLTHROUGH_H
6
+
7
+ /**
8
+ * We use -Wimplicit-fallthrough to guard potentially unintended fall-through
9
+ * between cases of a switch. Use PRISM_FALLTHROUGH to explicitly annotate cases
10
+ * where the fallthrough is intentional.
11
+ */
12
+ #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L /* C23 or later */
13
+ #define PRISM_FALLTHROUGH [[fallthrough]];
14
+ #elif defined(__GNUC__) || defined(__clang__)
15
+ #define PRISM_FALLTHROUGH __attribute__((fallthrough));
16
+ #elif defined(_MSC_VER)
17
+ #define PRISM_FALLTHROUGH __fallthrough;
18
+ #else
19
+ #define PRISM_FALLTHROUGH
20
+ #endif
21
+
22
+ #endif
@@ -0,0 +1,32 @@
1
+ /**
2
+ * @file compiler/filesystem.h
3
+ *
4
+ * Platform detection for mmap and filesystem support.
5
+ */
6
+ #ifndef PRISM_COMPILER_FILESYSTEM_H
7
+ #define PRISM_COMPILER_FILESYSTEM_H
8
+
9
+ /**
10
+ * In general, libc for embedded systems does not support memory-mapped files.
11
+ * If the target platform is POSIX or Windows, we can map a file in memory and
12
+ * read it in a more efficient manner.
13
+ */
14
+ #ifdef _WIN32
15
+ # define PRISM_HAS_MMAP
16
+ #else
17
+ # include <unistd.h>
18
+ # ifdef _POSIX_MAPPED_FILES
19
+ # define PRISM_HAS_MMAP
20
+ # endif
21
+ #endif
22
+
23
+ /**
24
+ * If PRISM_HAS_NO_FILESYSTEM is defined, then we want to exclude all filesystem
25
+ * related code from the library. All filesystem related code should be guarded
26
+ * by PRISM_HAS_FILESYSTEM.
27
+ */
28
+ #ifndef PRISM_HAS_NO_FILESYSTEM
29
+ # define PRISM_HAS_FILESYSTEM
30
+ #endif
31
+
32
+ #endif
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @file compiler/flex_array.h
3
+ */
4
+ #ifndef PRISM_COMPILER_FLEX_ARRAY_H
5
+ #define PRISM_COMPILER_FLEX_ARRAY_H
6
+
7
+ /**
8
+ * A macro for helper define a flexible array member. C99 supports `data[]`, GCC
9
+ * supports `data[0]` as an extension, and older compilers require `data[1]`.
10
+ */
11
+ #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
12
+ #define PM_FLEX_ARRAY_LENGTH /* data[] */
13
+ #elif defined(__GNUC__) && !defined(__STRICT_ANSI__)
14
+ #define PM_FLEX_ARRAY_LENGTH 0 /* data[0] */
15
+ #else
16
+ #define PM_FLEX_ARRAY_LENGTH 1 /* data[1] */
17
+ #endif
18
+
19
+ #endif
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @file compiler/force_inline.h
3
+ */
4
+ #ifndef PRISM_COMPILER_FORCE_INLINE_H
5
+ #define PRISM_COMPILER_FORCE_INLINE_H
6
+
7
+ #include "prism/compiler/inline.h"
8
+
9
+ /**
10
+ * Force a function to be inlined at every call site. Use sparingly — only for
11
+ * small, hot functions where the compiler's heuristics fail to inline.
12
+ */
13
+ #if defined(_MSC_VER)
14
+ # define PRISM_FORCE_INLINE __forceinline
15
+ #elif defined(__GNUC__) || defined(__clang__)
16
+ # define PRISM_FORCE_INLINE PRISM_INLINE __attribute__((always_inline))
17
+ #else
18
+ # define PRISM_FORCE_INLINE PRISM_INLINE
19
+ #endif
20
+
21
+ #endif
@@ -0,0 +1,27 @@
1
+ /**
2
+ * @file compiler/format.h
3
+ */
4
+ #ifndef PRISM_COMPILER_FORMAT_H
5
+ #define PRISM_COMPILER_FORMAT_H
6
+
7
+ #include <stdio.h>
8
+
9
+ /**
10
+ * Certain compilers support specifying that a function accepts variadic
11
+ * parameters that look like printf format strings to provide a better developer
12
+ * experience when someone is using the function. This macro does that in a
13
+ * compiler-agnostic way.
14
+ */
15
+ #if defined(__GNUC__)
16
+ # if defined(__MINGW_PRINTF_FORMAT)
17
+ # define PRISM_ATTRIBUTE_FORMAT(fmt_idx_, arg_idx_) __attribute__((format(__MINGW_PRINTF_FORMAT, fmt_idx_, arg_idx_)))
18
+ # else
19
+ # define PRISM_ATTRIBUTE_FORMAT(fmt_idx_, arg_idx_) __attribute__((format(printf, fmt_idx_, arg_idx_)))
20
+ # endif
21
+ #elif defined(__clang__)
22
+ # define PRISM_ATTRIBUTE_FORMAT(fmt_idx_, arg_idx_) __attribute__((__format__(__printf__, fmt_idx_, arg_idx_)))
23
+ #else
24
+ # define PRISM_ATTRIBUTE_FORMAT(fmt_idx_, arg_idx_)
25
+ #endif
26
+
27
+ #endif
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @file compiler/inline.h
3
+ */
4
+ #ifndef PRISM_COMPILER_INLINE_H
5
+ #define PRISM_COMPILER_INLINE_H
6
+
7
+ /**
8
+ * Old Visual Studio versions do not support the inline keyword, so we need to
9
+ * define it to be __inline.
10
+ */
11
+ #if defined(_MSC_VER) && !defined(inline)
12
+ # define PRISM_INLINE __inline
13
+ #else
14
+ # define PRISM_INLINE inline
15
+ #endif
16
+
17
+ #endif
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @file compiler/lifetimebound.hpp
3
+ */
4
+ #ifndef PRISM_COMPILER_LIFETIMEBOUND_HPP
5
+ #define PRISM_COMPILER_LIFETIMEBOUND_HPP
6
+
7
+ /**
8
+ * Mark a parameter that the return value points into, so that the compiler
9
+ * warns if a temporary is passed and the return value outlives it. If it is
10
+ * placed after the qualifiers of a member function, it marks the object
11
+ * instead.
12
+ */
13
+ #if defined(__has_cpp_attribute)
14
+ # if __has_cpp_attribute(clang::lifetimebound)
15
+ # define PRISM_LIFETIMEBOUND [[clang::lifetimebound]]
16
+ # elif __has_cpp_attribute(msvc::lifetimebound)
17
+ # define PRISM_LIFETIMEBOUND [[msvc::lifetimebound]]
18
+ # endif
19
+ #endif
20
+
21
+ #ifndef PRISM_LIFETIMEBOUND
22
+ # define PRISM_LIFETIMEBOUND
23
+ #endif
24
+
25
+ #endif
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @file compiler/nodiscard.h
3
+ */
4
+ #ifndef PRISM_COMPILER_NODISCARD_H
5
+ #define PRISM_COMPILER_NODISCARD_H
6
+
7
+ /**
8
+ * Mark the return value of a function as important so that the compiler warns
9
+ * if a caller ignores it. This is useful for functions that return error codes
10
+ * or allocated resources that must be freed.
11
+ */
12
+ #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
13
+ # define PRISM_NODISCARD [[nodiscard]]
14
+ #elif defined(__GNUC__) || defined(__clang__)
15
+ # define PRISM_NODISCARD __attribute__((__warn_unused_result__))
16
+ #elif defined(_MSC_VER)
17
+ # define PRISM_NODISCARD _Check_return_
18
+ #else
19
+ # define PRISM_NODISCARD
20
+ #endif
21
+
22
+ #endif
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @file compiler/nonnull.h
3
+ */
4
+ #ifndef PRISM_COMPILER_NONNULL_H
5
+ #define PRISM_COMPILER_NONNULL_H
6
+
7
+ /**
8
+ * Mark the parameters of a function as non-null. This allows the compiler to
9
+ * warn if a caller passes NULL for a parameter that should never be NULL. The
10
+ * arguments are the 1-based indices of the parameters.
11
+ */
12
+ #if defined(__GNUC__) || defined(__clang__)
13
+ # define PRISM_NONNULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
14
+ #else
15
+ # define PRISM_NONNULL(...)
16
+ #endif
17
+
18
+ #endif
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @file compiler/unused.h
3
+ */
4
+ #ifndef PRISM_COMPILER_UNUSED_H
5
+ #define PRISM_COMPILER_UNUSED_H
6
+
7
+ /**
8
+ * GCC will warn if you specify a function or parameter that is unused at
9
+ * runtime. This macro allows you to mark a function or parameter as unused in a
10
+ * compiler-agnostic way.
11
+ */
12
+ #if defined(__GNUC__)
13
+ # define PRISM_UNUSED __attribute__((unused))
14
+ #else
15
+ # define PRISM_UNUSED
16
+ #endif
17
+
18
+ #endif