Ruby 4.1.0dev (2026-11-27 revision ba5c016a98797fd300f62ac58fffdcf0505bf63d)
const_path.h (ba5c016a98797fd300f62ac58fffdcf0505bf63d)
1#ifndef CONST_PATH_H
2#define CONST_PATH_H
3
4#include "internal/compilers.h"
5#include "symbol.h"
6
8 uint32_t size;
9 rb_id_serial_t segments[FLEX_ARY_LEN];
10};
11
15typedef union {
16 const struct rb_const_path_heap *heap;
17
18 // On 32bit:
19 // | 31-2 | 1 | 0 |
20 // | first | absolute | packed |
21 //
22 // On 64bit:
23 // | 63-33 | 32-2 | 1 | 0 |
24 // | second | first | absolute | packed |
25 uintptr_t packed;
27
28#define PACKED_SHIFT 2
29#define PACKED_FLAG 0x1
30#define ABSOLUTE_FLAG 0x2
31
32#if SIZEOF_VOIDP == 8
33#define PACKED_ID_BITS 31
34#else
35#define PACKED_ID_BITS 30
36#endif
37
38#define PACKED_ID_BITS_MASK (((uintptr_t)1 << PACKED_ID_BITS) - 1)
39
40#define PACKED_ID_STATIC_FLAG 0x1
41
42static inline bool
43const_path_packed_p(const rb_const_path_t const_path)
44{
45 return const_path.packed & PACKED_FLAG;
46}
47
48static inline const struct rb_const_path_heap *
49const_path_heap(const rb_const_path_t const_path)
50{
51 rb_const_path_t unpacked_path = const_path;
52 unpacked_path.packed &= ~ABSOLUTE_FLAG;
53 return unpacked_path.heap;
54}
55
56static inline size_t
57const_path_memsize_for(size_t size)
58{
59 return offsetof(struct rb_const_path_heap, segments) + size * sizeof(rb_id_serial_t);
60}
61
62static inline size_t
63const_path_memsize(const rb_const_path_t const_path)
64{
65 if (const_path_packed_p(const_path)) {
66 return 0;
67 }
68
69 const struct rb_const_path_heap *heap = const_path_heap(const_path);
70 if (!heap) {
71 return 0;
72 }
73
74 return const_path_memsize_for(heap->size);
75}
76
77static inline void
78const_path_free(const rb_const_path_t const_path)
79{
80 if (!const_path_packed_p(const_path)) {
81 ruby_xfree_sized((void *)const_path_heap(const_path), const_path_memsize(const_path));
82 }
83}
84
85static inline bool
86const_path_absolute_p(const rb_const_path_t const_path)
87{
88 return const_path.packed & ABSOLUTE_FLAG;
89}
90
91static inline ID
92const_path_unpack_id(uint32_t packed_id)
93{
94 bool static_p = packed_id & PACKED_ID_STATIC_FLAG;
95 packed_id >>= 1;
96
97 ID id = rb_id_from_serial_and_type(packed_id, RUBY_ID_CONST);
98 if (static_p) {
99 id |= RUBY_ID_STATIC_SYM;
100 }
101 return id;
102}
103
104static inline ID
105const_path_aref(const rb_const_path_t const_path, size_t index)
106{
107 rb_id_serial_t serial;
108 if (const_path_packed_p(const_path)) {
109 switch (index) {
110 case 0:
111 serial = ((const_path.packed >> PACKED_SHIFT) & PACKED_ID_BITS_MASK);
112 break;
113#if SIZEOF_VOIDP == 8
114 case 1:
115 serial = (const_path.packed >> PACKED_SHIFT >> PACKED_ID_BITS) & PACKED_ID_BITS_MASK;
116 break;
117#endif
118 default:
119 return 0;
120 }
121 if (!serial) {
122 return 0;
123 }
124 }
125 else {
126 const struct rb_const_path_heap *heap = const_path_heap(const_path);
127 if (!heap || index >= heap->size) {
128 return 0;
129 }
130 serial = heap->segments[index];
131 }
132 return const_path_unpack_id(serial);
133}
134
135VALUE rb_const_path_to_array(const rb_const_path_t const_path);
136rb_const_path_t rb_const_path_from_array(VALUE arr);
137
138#endif /* CONST_PATH_H */
Represent a constant's path, e.g.
Definition const_path.h:15
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40