Ruby 4.1.0dev (2026-11-27 revision ba5c016a98797fd300f62ac58fffdcf0505bf63d)
const_path.c (ba5c016a98797fd300f62ac58fffdcf0505bf63d)
1#include "const_path.h"
2
3#if SIZEOF_VOIDP == 8
4// On 64 bits, we only need the top two bits to be 0.
5#define PACKABLE_SERIAL_MASK (((uint32_t)0x3) << 30)
6#else
7// On 32 bits, we need the top tree bits to be 0.
8#define PACKABLE_SERIAL_MASK (((uint32_t)0x7) << 29)
9#endif
10
11static inline uint32_t
12const_path_pack_id(ID id)
13{
14 RUBY_ASSERT(is_notop_id(id));
15 RUBY_ASSERT(id_type(id) == RUBY_ID_CONST);
16 uint32_t packed_id = rb_id_to_serial(id) << 1;
17 return DYNAMIC_ID_P(id) ? packed_id : packed_id | PACKED_ID_STATIC_FLAG;
18}
19
20static inline bool
21packable_id_p(ID id)
22{
23 return (const_path_pack_id(id) & PACKABLE_SERIAL_MASK) == 0;
24}
25
26static size_t
27const_path_size(const rb_const_path_t const_path)
28{
29 if (const_path_packed_p(const_path)) {
30#if SIZEOF_VOIDP == 8
31 if ((const_path.packed >> 33)) {
32 return 2;
33 }
34#endif
35 return 1;
36 }
37 else {
38 const struct rb_const_path_heap *heap = const_path_heap(const_path);
39 if (!heap) {
40 return 0;
41 }
42 return heap->size;
43 }
44}
45
46static inline rb_const_path_t
47const_path_packed1(bool absolute, ID id)
48{
49 uintptr_t num = (uintptr_t)const_path_pack_id(id);
50
51 rb_const_path_t const_path = {
52 .packed = (num << PACKED_SHIFT) | PACKED_FLAG
53 };
54 if (absolute) {
55 const_path.packed |= ABSOLUTE_FLAG;
56 }
57 return const_path;
58}
59
60#if SIZEOF_VOIDP == 8
61static inline rb_const_path_t
62const_path_packed2(bool absolute, ID first_id, ID second_id)
63{
64 uintptr_t num = (uintptr_t)const_path_pack_id(first_id);
65 num |= ((uintptr_t)const_path_pack_id(second_id)) << PACKED_ID_BITS;
66
67 rb_const_path_t const_path = {
68 .packed = (num << PACKED_SHIFT) | PACKED_FLAG
69 };
70 if (absolute) {
71 const_path.packed |= ABSOLUTE_FLAG;
72 }
73 return const_path;
74}
75#endif
76
78rb_const_path_from_array(VALUE arr)
79{
81 rb_len_t array_size = RARRAY_LEN(arr);
82 RUBY_ASSERT(array_size >= 1);
83
84 size_t size = array_size;
85
86 rb_len_t i = 0;
87 bool absolute = RARRAY_AREF(arr, i) == ID2SYM(idNULL);
88 if (absolute) {
89 size--;
90 i++;
91 }
92
93 RUBY_ASSERT(size >= 1);
94 RUBY_ASSERT(size < UINT32_MAX);
95
96 if (size == 1) {
97 VALUE sym = RARRAY_AREF(arr, i);
98 ID id = SYM2ID(sym);
99 if (packable_id_p(id)) {
100 return const_path_packed1(absolute, id);
101 }
102 }
103
104#if SIZEOF_VOIDP == 8
105 if (size == 2) {
106 VALUE first_sym = RARRAY_AREF(arr, i);
107 VALUE second_sym = RARRAY_AREF(arr, i + 1);
108 ID first_id = SYM2ID(first_sym);
109 ID second_id = SYM2ID(second_sym);
110
111 if (packable_id_p(first_id) && packable_id_p(second_id)) {
112 return const_path_packed2(absolute, first_id, second_id);
113 }
114 }
115#endif
116
117 struct rb_const_path_heap *heap = (struct rb_const_path_heap *)ruby_xmalloc(const_path_memsize_for(size));
118 heap->size = 0;
119
120 for (; i < array_size; i++) {
121 VALUE sym = RARRAY_AREF(arr, i);
122 ID id = SYM2ID(sym);
123 heap->segments[heap->size] = const_path_pack_id(id);
124 heap->size++;
125 }
126
127 rb_const_path_t const_path = {
128 .heap = heap
129 };
130
131 RUBY_ASSERT((const_path.packed & (ABSOLUTE_FLAG | PACKED_FLAG)) == 0);
132 if (absolute) {
133 const_path.packed |= ABSOLUTE_FLAG;
134 }
135
136 return const_path;
137}
138
139VALUE
140rb_const_path_to_array(const rb_const_path_t const_path)
141{
142 VALUE arr;
143
144 if (const_path_absolute_p(const_path)) {
145 arr = rb_ary_new_capa(const_path_size(const_path) + 1);
146 rb_ary_push(arr, ID2SYM(idNULL));
147 }
148 else {
149 arr = rb_ary_new_capa(const_path_size(const_path));
150 }
151
152 for (size_t i = 0; i < const_path_size(const_path); i++) {
153 rb_ary_push(arr, ID2SYM(const_path_aref(const_path, i)));
154 }
155
156 return arr;
157}
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define SYM2ID
Old name of RB_SYM2ID.
Definition symbol.h:45
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
VALUE rb_ary_new_capa(rb_len_t capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:50
#define RARRAY_AREF(a, i)
References an object in an array.
Definition rarray.h:422
Represent a constant's path, e.g.
Definition const_path.h:15
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
Definition value.h:142
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
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376