prism 1.9.0 → 2.0.0.rc2

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 (244) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +88 -0
  3. data/CODE_OF_CONDUCT.md +1 -1
  4. data/LICENSE.md +1 -1
  5. data/Makefile +41 -12
  6. data/README.md +35 -97
  7. data/config.yml +174 -161
  8. data/docs/build_system.md +2 -2
  9. data/docs/configuration.md +5 -4
  10. data/docs/encoding.md +2 -2
  11. data/docs/fuzzing.md +4 -11
  12. data/docs/javascript.md +19 -4
  13. data/docs/mapping.md +1 -1
  14. data/docs/releasing.md +77 -5
  15. data/docs/relocation.md +2 -4
  16. data/docs/serialization.md +22 -24
  17. data/docs/testing.md +46 -29
  18. data/ext/prism/api_node.c +1461 -1401
  19. data/ext/prism/extconf.rb +3 -4
  20. data/ext/prism/extension.c +796 -422
  21. data/ext/prism/extension.h +3 -3
  22. data/include/prism/arena.h +37 -0
  23. data/include/prism/ast.h +2727 -610
  24. data/include/prism/buffer.h +52 -0
  25. data/include/prism/comments.h +55 -0
  26. data/include/prism/compiler/accel.h +26 -0
  27. data/include/prism/compiler/align.h +36 -0
  28. data/include/prism/compiler/assume.h +25 -0
  29. data/include/prism/compiler/bswap.h +46 -0
  30. data/include/prism/compiler/endianness.h +34 -0
  31. data/include/prism/compiler/exported.h +24 -0
  32. data/include/prism/compiler/fallthrough.h +22 -0
  33. data/include/prism/compiler/filesystem.h +32 -0
  34. data/include/prism/compiler/flex_array.h +19 -0
  35. data/include/prism/compiler/force_inline.h +21 -0
  36. data/include/prism/compiler/format.h +25 -0
  37. data/include/prism/compiler/inline.h +17 -0
  38. data/include/prism/compiler/nodiscard.h +22 -0
  39. data/include/prism/compiler/nonnull.h +18 -0
  40. data/include/prism/compiler/unused.h +18 -0
  41. data/include/prism/constant_pool.h +81 -0
  42. data/include/prism/diagnostic.h +38 -403
  43. data/include/prism/errors_format.h +53 -0
  44. data/include/prism/excludes.h +29 -0
  45. data/include/prism/integer.h +41 -0
  46. data/include/prism/internal/allocator.h +68 -0
  47. data/include/prism/internal/allocator_debug.h +88 -0
  48. data/include/prism/internal/arena.h +108 -0
  49. data/include/prism/internal/bit.h +42 -0
  50. data/include/prism/internal/buffer.h +94 -0
  51. data/include/prism/internal/char.h +145 -0
  52. data/include/prism/internal/comments.h +53 -0
  53. data/include/prism/internal/constant_pool.h +152 -0
  54. data/include/prism/internal/diagnostic.h +389 -0
  55. data/include/prism/{encoding.h → internal/encoding.h} +27 -68
  56. data/include/prism/internal/integer.h +68 -0
  57. data/include/prism/internal/isinf.h +16 -0
  58. data/include/prism/internal/line_offset_list.h +34 -0
  59. data/include/prism/{util/pm_list.h → internal/list.h} +11 -52
  60. data/include/prism/internal/magic_comments.h +23 -0
  61. data/include/prism/internal/memchr.h +15 -0
  62. data/include/prism/internal/node.h +38 -0
  63. data/include/prism/internal/options.h +232 -0
  64. data/include/prism/internal/parser.h +1004 -0
  65. data/include/prism/internal/regexp.h +26 -0
  66. data/include/prism/internal/serialize.h +39 -0
  67. data/include/prism/internal/source.h +72 -0
  68. data/include/prism/{static_literals.h → internal/static_literals.h} +26 -47
  69. data/include/prism/internal/stringy.h +35 -0
  70. data/include/prism/{util/pm_strncasecmp.h → internal/strncasecmp.h} +3 -17
  71. data/include/prism/{util/pm_strpbrk.h → internal/strpbrk.h} +7 -20
  72. data/include/prism/internal/tokens.h +11 -0
  73. data/include/prism/internal/xxh3.h +16 -0
  74. data/include/prism/json.h +32 -0
  75. data/include/prism/line_offset_list.h +61 -0
  76. data/include/prism/magic_comments.h +35 -0
  77. data/include/prism/node.h +18 -53
  78. data/include/prism/options.h +146 -280
  79. data/include/prism/parser.h +356 -867
  80. data/include/prism/prettyprint.h +6 -9
  81. data/include/prism/serialize.h +115 -0
  82. data/include/prism/source.h +155 -0
  83. data/include/prism/stream.h +28 -0
  84. data/include/prism/string_query.h +74 -0
  85. data/include/prism/stringy.h +72 -0
  86. data/include/prism/version.h +12 -3
  87. data/include/prism.h +43 -320
  88. data/lib/prism/compiler.rb +319 -305
  89. data/lib/prism/desugar_compiler.rb +105 -34
  90. data/lib/prism/dispatcher.rb +646 -473
  91. data/lib/prism/dot_visitor.rb +592 -511
  92. data/lib/prism/dsl.rb +391 -25
  93. data/lib/prism/ffi.rb +258 -90
  94. data/lib/prism/inspect_visitor.rb +357 -325
  95. data/lib/prism/lex_compat.rb +254 -212
  96. data/lib/prism/mutation_compiler.rb +313 -305
  97. data/lib/prism/node.rb +20966 -6992
  98. data/lib/prism/node_ext.rb +180 -187
  99. data/lib/prism/node_find.rb +215 -0
  100. data/lib/prism/parse_result/errors.rb +7 -1
  101. data/lib/prism/parse_result/newlines.rb +276 -8
  102. data/lib/prism/parse_result.rb +468 -139
  103. data/lib/prism/pattern.rb +65 -20
  104. data/lib/prism/reflection.rb +23 -12
  105. data/lib/prism/relocation.rb +149 -87
  106. data/lib/prism/serialize.rb +3460 -539
  107. data/lib/prism/string_query.rb +24 -1
  108. data/lib/prism/translation/parser/builder.rb +10 -2
  109. data/lib/prism/translation/parser/compiler.rb +44 -52
  110. data/lib/prism/translation/parser/lexer.rb +41 -92
  111. data/lib/prism/translation/parser.rb +1 -1
  112. data/lib/prism/translation/parser_current.rb +1 -1
  113. data/lib/prism/translation/ripper/lexer.rb +8 -10
  114. data/lib/prism/translation/ripper/sexp.rb +2 -10
  115. data/lib/prism/translation/ripper/shim.rb +2 -0
  116. data/lib/prism/translation/ripper.rb +967 -209
  117. data/lib/prism/translation/ruby_parser.rb +73 -316
  118. data/lib/prism/translation.rb +2 -0
  119. data/lib/prism/visitor.rb +632 -5
  120. data/lib/prism.rb +79 -9
  121. data/prism.gemspec +128 -62
  122. data/rbi/generated/prism/compiler.rbi +489 -0
  123. data/rbi/generated/prism/desugar_compiler.rbi +306 -0
  124. data/rbi/generated/prism/dispatcher.rbi +1138 -0
  125. data/rbi/generated/prism/dot_visitor.rbi +618 -0
  126. data/rbi/generated/prism/dsl.rbi +751 -0
  127. data/rbi/generated/prism/inspect_visitor.rbi +503 -0
  128. data/rbi/generated/prism/lex_compat.rbi +162 -0
  129. data/rbi/generated/prism/mutation_compiler.rbi +464 -0
  130. data/rbi/generated/prism/node.rbi +14407 -0
  131. data/rbi/generated/prism/node_ext.rbi +259 -0
  132. data/rbi/generated/prism/node_find.rbi +79 -0
  133. data/rbi/generated/prism/parse_result/errors.rbi +21 -0
  134. data/rbi/generated/prism/parse_result/newlines.rbi +208 -0
  135. data/rbi/generated/prism/parse_result.rbi +776 -0
  136. data/rbi/generated/prism/pattern.rbi +134 -0
  137. data/rbi/generated/prism/reflection.rbi +89 -0
  138. data/rbi/generated/prism/relocation.rbi +329 -0
  139. data/rbi/generated/prism/serialize.rbi +184 -0
  140. data/rbi/generated/prism/string_query.rbi +43 -0
  141. data/rbi/generated/prism/translation.rbi +7 -0
  142. data/rbi/generated/prism/visitor.rbi +650 -0
  143. data/rbi/generated/prism.rbi +86 -0
  144. data/rbi/prism/translation/parser_versions.rbi +5 -0
  145. data/rbi/rubyvm/node_find.rbi +14 -0
  146. data/sig/generated/prism/compiler.rbs +492 -0
  147. data/sig/generated/prism/desugar_compiler.rbs +305 -0
  148. data/sig/generated/prism/dispatcher.rbs +1292 -0
  149. data/sig/generated/prism/dot_visitor.rbs +635 -0
  150. data/sig/generated/prism/dsl.rbs +928 -0
  151. data/sig/generated/prism/inspect_visitor.rbs +509 -0
  152. data/sig/generated/prism/lex_compat.rbs +166 -0
  153. data/sig/{prism → generated/prism}/mutation_compiler.rbs +309 -4
  154. data/sig/generated/prism/node.rbs +20896 -0
  155. data/sig/generated/prism/node_ext.rbs +290 -0
  156. data/sig/generated/prism/node_find.rbs +90 -0
  157. data/sig/generated/prism/parse_result/errors.rbs +22 -0
  158. data/sig/generated/prism/parse_result/newlines.rbs +223 -0
  159. data/sig/generated/prism/parse_result.rbs +871 -0
  160. data/sig/generated/prism/pattern.rbs +156 -0
  161. data/sig/generated/prism/reflection.rbs +101 -0
  162. data/sig/generated/prism/relocation.rbs +428 -0
  163. data/sig/generated/prism/serialize.rbs +198 -0
  164. data/sig/generated/prism/string_query.rbs +43 -0
  165. data/sig/generated/prism/translation.rbs +8 -0
  166. data/sig/generated/prism/visitor.rbs +1111 -0
  167. data/sig/generated/prism.rbs +84 -0
  168. data/sig/manifest.yaml +2 -0
  169. data/src/arena.c +117 -0
  170. data/src/{util/pm_buffer.c → buffer.c} +53 -21
  171. data/src/{util/pm_char.c → char.c} +28 -57
  172. data/src/comments.c +454 -0
  173. data/src/constant_pool.c +406 -0
  174. data/src/diagnostic.c +97 -65
  175. data/src/encoding.c +8 -3
  176. data/src/errors_format.c +519 -0
  177. data/src/{util/pm_integer.c → integer.c} +33 -22
  178. data/src/json.c +5733 -0
  179. data/src/line_offset_list.c +100 -0
  180. data/src/list.c +24 -0
  181. data/src/{util/pm_memchr.c → memchr.c} +8 -6
  182. data/src/node.c +3504 -7674
  183. data/src/options.c +162 -49
  184. data/src/parser.c +367 -0
  185. data/src/prettyprint.c +421 -354
  186. data/src/prism.c +8402 -7161
  187. data/src/regexp.c +961 -45
  188. data/src/serialize.c +460 -424
  189. data/src/source.c +518 -0
  190. data/src/static_literals.c +111 -43
  191. data/src/string_query.c +180 -0
  192. data/src/stringy.c +91 -0
  193. data/src/{util/pm_strncasecmp.c → strncasecmp.c} +5 -4
  194. data/src/strpbrk.c +447 -0
  195. data/src/{token_type.c → tokens.c} +39 -18
  196. data/src/xxh3.c +292 -0
  197. metadata +128 -63
  198. data/ext/prism/api_pack.c +0 -276
  199. data/include/prism/defines.h +0 -260
  200. data/include/prism/pack.h +0 -163
  201. data/include/prism/regexp.h +0 -43
  202. data/include/prism/util/pm_buffer.h +0 -236
  203. data/include/prism/util/pm_char.h +0 -204
  204. data/include/prism/util/pm_constant_pool.h +0 -218
  205. data/include/prism/util/pm_integer.h +0 -130
  206. data/include/prism/util/pm_memchr.h +0 -29
  207. data/include/prism/util/pm_newline_list.h +0 -113
  208. data/include/prism/util/pm_string.h +0 -200
  209. data/lib/prism/pack.rb +0 -230
  210. data/lib/prism/parse_result/comments.rb +0 -188
  211. data/rbi/prism/compiler.rbi +0 -12
  212. data/rbi/prism/dsl.rbi +0 -524
  213. data/rbi/prism/inspect_visitor.rbi +0 -12
  214. data/rbi/prism/node.rbi +0 -8750
  215. data/rbi/prism/node_ext.rbi +0 -107
  216. data/rbi/prism/parse_result.rbi +0 -404
  217. data/rbi/prism/reflection.rbi +0 -58
  218. data/rbi/prism/string_query.rbi +0 -12
  219. data/rbi/prism/visitor.rbi +0 -473
  220. data/rbi/prism.rbi +0 -63
  221. data/sig/prism/compiler.rbs +0 -9
  222. data/sig/prism/dispatcher.rbs +0 -19
  223. data/sig/prism/dot_visitor.rbs +0 -6
  224. data/sig/prism/dsl.rbs +0 -351
  225. data/sig/prism/inspect_visitor.rbs +0 -22
  226. data/sig/prism/lex_compat.rbs +0 -10
  227. data/sig/prism/node.rbs +0 -4042
  228. data/sig/prism/node_ext.rbs +0 -149
  229. data/sig/prism/pack.rbs +0 -43
  230. data/sig/prism/parse_result/comments.rbs +0 -38
  231. data/sig/prism/parse_result.rbs +0 -197
  232. data/sig/prism/pattern.rbs +0 -13
  233. data/sig/prism/reflection.rbs +0 -50
  234. data/sig/prism/relocation.rbs +0 -185
  235. data/sig/prism/serialize.rbs +0 -8
  236. data/sig/prism/string_query.rbs +0 -11
  237. data/sig/prism/visitor.rbs +0 -169
  238. data/sig/prism.rbs +0 -272
  239. data/src/pack.c +0 -509
  240. data/src/util/pm_constant_pool.c +0 -342
  241. data/src/util/pm_list.c +0 -49
  242. data/src/util/pm_newline_list.c +0 -125
  243. data/src/util/pm_string.c +0 -381
  244. data/src/util/pm_strpbrk.c +0 -206
@@ -0,0 +1,52 @@
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
+ /**
16
+ * A wrapper around a contiguous block of allocated memory.
17
+ */
18
+ typedef struct pm_buffer_t pm_buffer_t;
19
+
20
+ /**
21
+ * Allocate and initialize a new buffer. If the buffer cannot be allocated, this
22
+ * function will abort the process.
23
+ *
24
+ * @returns A pointer to the initialized buffer. The caller is responsible for
25
+ * freeing the buffer with pm_buffer_free.
26
+ */
27
+ PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_buffer_t * pm_buffer_new(void);
28
+
29
+ /**
30
+ * Free both the memory held by the buffer and the buffer itself.
31
+ *
32
+ * @param buffer The buffer to free.
33
+ */
34
+ PRISM_EXPORTED_FUNCTION void pm_buffer_free(pm_buffer_t *buffer) PRISM_NONNULL(1);
35
+
36
+ /**
37
+ * Return the value of the buffer.
38
+ *
39
+ * @param buffer The buffer to get the value of.
40
+ * @returns The value of the buffer.
41
+ */
42
+ PRISM_EXPORTED_FUNCTION char * pm_buffer_value(const pm_buffer_t *buffer) PRISM_NONNULL(1);
43
+
44
+ /**
45
+ * Return the length of the buffer.
46
+ *
47
+ * @param buffer The buffer to get the length of.
48
+ * @returns The length of the buffer.
49
+ */
50
+ PRISM_EXPORTED_FUNCTION size_t pm_buffer_length(const pm_buffer_t *buffer) PRISM_NONNULL(1);
51
+
52
+ #endif
@@ -0,0 +1,55 @@
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
+ /** This is the type of a comment that we've found while parsing. */
18
+ typedef enum {
19
+ PM_COMMENT_INLINE,
20
+ PM_COMMENT_EMBDOC
21
+ } pm_comment_type_t;
22
+
23
+ /** An opaque pointer to a comment found while parsing. */
24
+ typedef struct pm_comment_t pm_comment_t;
25
+
26
+ /**
27
+ * The comments owned by a node, as a slice of the comment attachments of the
28
+ * parser that produced it.
29
+ */
30
+ typedef struct {
31
+ /** The index of the first attachment owned by the node. */
32
+ uint32_t index;
33
+
34
+ /** The number of attachments owned by the node. */
35
+ uint32_t length;
36
+ } pm_comment_range_t;
37
+
38
+ /**
39
+ * Returns the location associated with the given comment.
40
+ *
41
+ * @param comment the comment whose location we want to get
42
+ * @returns the location associated with the given comment
43
+ */
44
+ PRISM_EXPORTED_FUNCTION pm_location_t pm_comment_location(const pm_comment_t *comment) PRISM_NONNULL(1);
45
+
46
+ /**
47
+ * Returns the type associated with the given comment.
48
+ *
49
+ * @param comment the comment whose type we want to get
50
+ * @returns the type associated with the given comment. This can either be
51
+ * PM_COMMENT_INLINE or PM_COMMENT_EMBDOC.
52
+ */
53
+ PRISM_EXPORTED_FUNCTION pm_comment_type_t pm_comment_type(const pm_comment_t *comment) PRISM_NONNULL(1);
54
+
55
+ #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,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,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,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,25 @@
1
+ /**
2
+ * @file compiler/format.h
3
+ */
4
+ #ifndef PRISM_COMPILER_FORMAT_H
5
+ #define PRISM_COMPILER_FORMAT_H
6
+
7
+ /**
8
+ * Certain compilers support specifying that a function accepts variadic
9
+ * parameters that look like printf format strings to provide a better developer
10
+ * experience when someone is using the function. This macro does that in a
11
+ * compiler-agnostic way.
12
+ */
13
+ #if defined(__GNUC__)
14
+ # if defined(__MINGW_PRINTF_FORMAT)
15
+ # define PRISM_ATTRIBUTE_FORMAT(fmt_idx_, arg_idx_) __attribute__((format(__MINGW_PRINTF_FORMAT, fmt_idx_, arg_idx_)))
16
+ # else
17
+ # define PRISM_ATTRIBUTE_FORMAT(fmt_idx_, arg_idx_) __attribute__((format(printf, fmt_idx_, arg_idx_)))
18
+ # endif
19
+ #elif defined(__clang__)
20
+ # define PRISM_ATTRIBUTE_FORMAT(fmt_idx_, arg_idx_) __attribute__((__format__(__printf__, fmt_idx_, arg_idx_)))
21
+ #else
22
+ # define PRISM_ATTRIBUTE_FORMAT(fmt_idx_, arg_idx_)
23
+ #endif
24
+
25
+ #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,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
@@ -0,0 +1,81 @@
1
+ /**
2
+ * @file constant_pool.h
3
+ *
4
+ * A data structure that stores a set of strings.
5
+ *
6
+ * Each string is assigned a unique id, which can be used to compare strings for
7
+ * equality. This comparison ends up being much faster than strcmp, since it
8
+ * only requires a single integer comparison.
9
+ */
10
+ #ifndef PRISM_CONSTANT_POOL_H
11
+ #define PRISM_CONSTANT_POOL_H
12
+
13
+ #include "prism/compiler/exported.h"
14
+ #include "prism/compiler/nodiscard.h"
15
+ #include "prism/compiler/nonnull.h"
16
+
17
+ #include "prism/arena.h"
18
+
19
+ #include <stddef.h>
20
+ #include <stdint.h>
21
+
22
+ /**
23
+ * A constant id is a unique identifier for a constant in the constant pool.
24
+ */
25
+ typedef uint32_t pm_constant_id_t;
26
+
27
+ /**
28
+ * A list of constant IDs. Usually used to represent a set of locals.
29
+ */
30
+ typedef struct {
31
+ /** The number of constant ids in the list. */
32
+ size_t size;
33
+
34
+ /** The number of constant ids that have been allocated in the list. */
35
+ size_t capacity;
36
+
37
+ /** The constant ids in the list. */
38
+ pm_constant_id_t *ids;
39
+ } pm_constant_id_list_t;
40
+
41
+ /** A constant in the pool which effectively stores a string. */
42
+ typedef struct pm_constant_t pm_constant_t;
43
+
44
+ /**
45
+ * The overall constant pool, which stores constants found while parsing.
46
+ */
47
+ typedef struct pm_constant_pool_t pm_constant_pool_t;
48
+
49
+ /**
50
+ * Return a raw pointer to the start of a constant.
51
+ *
52
+ * @param constant The constant to get the start of.
53
+ * @returns A raw pointer to the start of the constant.
54
+ */
55
+ PRISM_EXPORTED_FUNCTION const uint8_t * pm_constant_start(const pm_constant_t *constant) PRISM_NONNULL(1);
56
+
57
+ /**
58
+ * Return the length of a constant.
59
+ *
60
+ * @param constant The constant to get the length of.
61
+ * @returns The length of the constant.
62
+ */
63
+ PRISM_EXPORTED_FUNCTION size_t pm_constant_length(const pm_constant_t *constant) PRISM_NONNULL(1);
64
+
65
+ /**
66
+ * Initialize a list of constant ids.
67
+ *
68
+ * @param list The list to initialize.
69
+ */
70
+ PRISM_EXPORTED_FUNCTION void pm_constant_id_list_init(pm_constant_id_list_t *list) PRISM_NONNULL(1);
71
+
72
+ /**
73
+ * Append a constant id to a list of constant ids.
74
+ *
75
+ * @param arena The arena to use for allocations.
76
+ * @param list The list to append to.
77
+ * @param id The constant id to append.
78
+ */
79
+ PRISM_EXPORTED_FUNCTION void pm_constant_id_list_append(pm_arena_t *arena, pm_constant_id_list_t *list, pm_constant_id_t id) PRISM_NONNULL(1, 2);
80
+
81
+ #endif