Ruby 4.1.0dev (2026-05-27 revision dcfba5725794815fa36e129457f9d748f24371f9)
class.h
1#ifndef INTERNAL_CLASS_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_CLASS_H
11#include "id.h"
12#include "id_table.h" /* for struct rb_id_table */
13#include "internal/box.h"
14#include "internal/serial.h" /* for rb_serial_t */
15#include "internal/static_assert.h"
16#include "internal/variable.h" /* for rb_class_ivar_set */
17#include "ruby/internal/stdbool.h" /* for bool */
18#include "ruby/intern.h" /* for rb_alloc_func_t */
19#include "ruby/ruby.h" /* for struct RBasic */
20#include "shape.h"
21#include "ruby_assert.h"
22#include "vm_core.h"
23#include "vm_sync.h"
24#include "method.h" /* for rb_cref_t */
25
26#ifdef RCLASS_SUPER
27# undef RCLASS_SUPER
28#endif
29
31 VALUE imemo_flags;
32 uint32_t index;
33 rb_serial_t global_cvar_state;
34 const rb_cref_t *cref;
35 VALUE class_value;
36};
37
39 const rb_box_t *box;
40 VALUE super;
41 VALUE fields_obj; // Fields are either ivar or other internal properties stored inline
42 VALUE classpath;
43 struct rb_id_table *m_tbl;
44 struct rb_id_table *const_tbl;
45 struct rb_id_table *callable_m_tbl;
46 VALUE cc_tbl; /* { ID => { cme, [cc1, cc2, ...] }, ... } */
47 VALUE cvc_tbl;
48 VALUE *superclasses;
54
55 const VALUE origin_;
56 const VALUE refined_class;
57 union {
58 struct {
59 rb_alloc_func_t allocator;
60 } class;
61 struct {
62 VALUE attached_object;
63 } singleton_class;
64 struct {
65 const VALUE includer;
66 } iclass;
67 } as;
68 uint16_t superclass_depth;
69 attr_index_t max_iv_count;
70 uint8_t variation_count;
71 bool permanent_classpath : 1;
72 bool shared_const_tbl : 1;
73 bool iclass_is_origin : 1;
74 bool iclass_origin_shared_mtbl : 1;
75 bool superclasses_with_self : 1;
76 bool expect_no_ivar : 1;
77};
79
80STATIC_ASSERT(shape_max_variations, SHAPE_MAX_VARIATIONS < (1 << (sizeof(((rb_classext_t *)0)->variation_count) * CHAR_BIT)));
81
82struct RClass {
83 struct RBasic basic;
84 VALUE object_id;
85 /*
86 * If box_classext_tbl is NULL, then the prime classext is readable (because no other classext exists).
87 * For the check whether writable or not, check flag RCLASS_PRIME_CLASSEXT_WRITABLE
88 */
89};
90
92 struct RClass rclass;
93 rb_classext_t classext;
94};
95
96#if SIZEOF_VALUE >= SIZEOF_LONG_LONG
97// Assert that classes can be embedded in heaps[3] (256B slot size on 64-bit).
98// On 32bit platforms there is no variable width allocation so it doesn't matter.
99STATIC_ASSERT(sizeof_rb_classext_t, sizeof(struct RClass_and_rb_classext_t) <= 256);
100#endif
101
103 struct RClass_and_rb_classext_t base;
104 st_table *box_classext_tbl; // box_object -> (rb_classext_t *)
105};
106
107static const uint16_t RCLASS_MAX_SUPERCLASS_DEPTH = ((uint16_t)-1);
108
109static inline bool RCLASS_SINGLETON_P(VALUE klass);
110
111static inline bool RCLASS_PRIME_CLASSEXT_READABLE_P(VALUE obj);
112static inline bool RCLASS_PRIME_CLASSEXT_WRITABLE_P(VALUE obj);
113static inline void RCLASS_SET_PRIME_CLASSEXT_WRITABLE(VALUE obj, bool writable);
114
115#define RCLASS_EXT_PRIME(c) (&((struct RClass_and_rb_classext_t*)(c))->classext)
116#define RCLASS_EXT_PRIME_P(ext, c) (&((struct RClass_and_rb_classext_t*)(c))->classext == ext)
117
118static inline rb_classext_t * RCLASS_EXT_READABLE_IN_BOX(VALUE obj, const rb_box_t *box);
119static inline rb_classext_t * RCLASS_EXT_READABLE(VALUE obj);
120static inline rb_classext_t * RCLASS_EXT_WRITABLE_IN_BOX(VALUE obj, const rb_box_t *box);
121static inline rb_classext_t * RCLASS_EXT_WRITABLE(VALUE obj);
122
123// Raw accessor
124#define RCLASSEXT_BOX(ext) (ext->box)
125#define RCLASSEXT_SUPER(ext) (ext->super)
126#define RCLASSEXT_FIELDS(ext) (ext->fields_obj ? ROBJECT_FIELDS(ext->fields_obj) : NULL)
127#define RCLASSEXT_FIELDS_OBJ(ext) (ext->fields_obj)
128#define RCLASSEXT_M_TBL(ext) (ext->m_tbl)
129#define RCLASSEXT_CONST_TBL(ext) (ext->const_tbl)
130#define RCLASSEXT_CALLABLE_M_TBL(ext) (ext->callable_m_tbl)
131#define RCLASSEXT_CC_TBL(ext) (ext->cc_tbl)
132#define RCLASSEXT_CVC_TBL(ext) (ext->cvc_tbl)
133#define RCLASSEXT_SUPERCLASS_DEPTH(ext) (ext->superclass_depth)
134#define RCLASSEXT_SUPERCLASSES(ext) (ext->superclasses)
135#define RCLASSEXT_SUBCLASSES(ext) (ext->subclasses)
136#define RCLASSEXT_ORIGIN(ext) (ext->origin_)
137#define RCLASSEXT_REFINED_CLASS(ext) (ext->refined_class)
138// class.allocator/singleton_class.attached_object are not accessed directly via RCLASSEXT_*
139#define RCLASSEXT_INCLUDER(ext) (ext->as.iclass.includer)
140#define RCLASSEXT_PERMANENT_CLASSPATH(ext) (ext->permanent_classpath)
141#define RCLASSEXT_SHARED_CONST_TBL(ext) (ext->shared_const_tbl)
142#define RCLASSEXT_ICLASS_IS_ORIGIN(ext) (ext->iclass_is_origin)
143#define RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) (ext->iclass_origin_shared_mtbl)
144#define RCLASSEXT_SUPERCLASSES_WITH_SELF(ext) (ext->superclasses_with_self)
145#define RCLASSEXT_CLASSPATH(ext) (ext->classpath)
146
147static inline void RCLASSEXT_SET_ORIGIN(rb_classext_t *ext, VALUE klass, VALUE origin);
148static inline void RCLASSEXT_SET_INCLUDER(rb_classext_t *ext, VALUE klass, VALUE includer);
149
150/* Prime classext entry accessor for very specific reason */
151#define RCLASS_PRIME_BOX(c) (RCLASS_EXT_PRIME(c)->box)
152// To invalidate CC by inserting&invalidating method entry into tables containing the target cme
153// See clear_method_cache_by_id_in_class()
154#define RCLASS_PRIME_FIELDS_OBJ(c) (RCLASS_EXT_PRIME(c)->fields_obj)
155#define RCLASS_PRIME_M_TBL(c) (RCLASS_EXT_PRIME(c)->m_tbl)
156#define RCLASS_PRIME_CONST_TBL(c) (RCLASS_EXT_PRIME(c)->const_tbl)
157#define RCLASS_PRIME_CALLABLE_M_TBL(c) (RCLASS_EXT_PRIME(c)->callable_m_tbl)
158#define RCLASS_PRIME_CC_TBL(c) (RCLASS_EXT_PRIME(c)->cc_tbl)
159#define RCLASS_M_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT_PRIME(c)->m_tbl != tbl)
160#define RCLASS_CALLABLE_M_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT_PRIME(c)->callable_m_tbl != tbl)
161#define RCLASS_CC_TBL_NOT_PRIME_P(c, tbl) (RCLASS_EXT_PRIME(c)->cc_tbl != tbl)
162
163// Read accessor, regarding box
164#define RCLASS_SUPER(c) (RCLASS_EXT_READABLE(c)->super)
165#define RCLASS_M_TBL(c) (RCLASS_EXT_READABLE(c)->m_tbl)
166#define RCLASS_CONST_TBL(c) (RCLASS_EXT_READABLE(c)->const_tbl)
167/*
168 * Both cc_tbl/callable_m_tbl are cache-like and always be changed when referreed,
169 * so always those should be writable.
170 */
171#define RCLASS_CVC_TBL(c) (RCLASS_EXT_READABLE(c)->cvc_tbl)
172#define RCLASS_SUBCLASSES(c) (RCLASS_EXT_PRIME(c)->subclasses)
173#define RCLASS_ORIGIN(c) (RCLASS_EXT_READABLE(c)->origin_)
174#define RICLASS_IS_ORIGIN_P(c) (RCLASS_EXT_READABLE(c)->iclass_is_origin)
175#define RCLASS_PERMANENT_CLASSPATH_P(c) (RCLASS_EXT_READABLE(c)->permanent_classpath)
176#define RCLASS_CLASSPATH(c) (RCLASS_EXT_READABLE(c)->classpath)
177
178// Superclasses can't be changed after initialization
179#define RCLASS_SUPERCLASS_DEPTH(c) (RCLASS_EXT_PRIME(c)->superclass_depth)
180#define RCLASS_SUPERCLASSES(c) (RCLASS_EXT_PRIME(c)->superclasses)
181#define RCLASS_SUPERCLASSES_WITH_SELF_P(c) (RCLASS_EXT_PRIME(c)->superclasses_with_self)
182
183// Ruby Box doesn't make changes on these refined_class/attached_object/includer
184#define RCLASS_REFINED_CLASS(c) (RCLASS_EXT_PRIME(c)->refined_class)
185#define RCLASS_ATTACHED_OBJECT(c) (RCLASS_EXT_PRIME(c)->as.singleton_class.attached_object)
186#define RCLASS_INCLUDER(c) (RCLASS_EXT_PRIME(c)->as.iclass.includer)
187
188// max IV count and variation count are just hints, so they don't need to be per-box
189#define RCLASS_MAX_IV_COUNT(ext) (RCLASS_EXT_PRIME(ext)->max_iv_count)
190#define RCLASS_VARIATION_COUNT(ext) (RCLASS_EXT_PRIME(ext)->variation_count)
191
192// Writable classext entries (instead of RCLASS_SET_*) because member data will be operated directly
193#define RCLASS_WRITABLE_M_TBL(c) (RCLASS_EXT_WRITABLE(c)->m_tbl)
194#define RCLASS_WRITABLE_CONST_TBL(c) (RCLASS_EXT_WRITABLE(c)->const_tbl)
195#define RCLASS_WRITABLE_CALLABLE_M_TBL(c) (RCLASS_EXT_WRITABLE(c)->callable_m_tbl)
196#define RCLASS_WRITABLE_CC_TBL(c) (RCLASS_EXT_WRITABLE(c)->cc_tbl)
197#define RCLASS_WRITABLE_CVC_TBL(c) (RCLASS_EXT_WRITABLE(c)->cvc_tbl)
198// Subclasses are only in the prime classext (box-invariant)
199#define RCLASS_WRITABLE_SUBCLASSES(c) (RCLASS_EXT_PRIME(c)->subclasses)
200
201static inline void RCLASS_SET_SUPER(VALUE klass, VALUE super);
202static inline void RCLASS_WRITE_SUPER(VALUE klass, VALUE super);
203static inline void RCLASS_SET_CONST_TBL(VALUE klass, struct rb_id_table *table, bool shared);
204static inline void RCLASS_WRITE_CONST_TBL(VALUE klass, struct rb_id_table *table, bool shared);
205static inline void RCLASS_WRITE_CALLABLE_M_TBL(VALUE klass, struct rb_id_table *table);
206static inline void RCLASS_WRITE_CC_TBL(VALUE klass, VALUE table);
207static inline void RCLASS_SET_CVC_TBL(VALUE klass, VALUE table);
208static inline void RCLASS_WRITE_CVC_TBL(VALUE klass, VALUE table);
209
210static inline void RCLASS_WRITE_SUPERCLASSES(VALUE klass, size_t depth, VALUE *superclasses, bool with_self);
211static inline void RCLASS_SET_SUBCLASSES(VALUE klass, VALUE subclasses);
212
213static inline void RCLASS_SET_ORIGIN(VALUE klass, VALUE origin);
214static inline void RCLASS_WRITE_ORIGIN(VALUE klass, VALUE origin);
215static inline void RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass);
216static inline void RICLASS_WRITE_ORIGIN_SHARED_MTBL(VALUE iclass);
217static inline bool RICLASS_OWNS_M_TBL_P(VALUE iclass);
218
219static inline void RCLASS_SET_REFINED_CLASS(VALUE klass, VALUE refined);
220static inline rb_alloc_func_t RCLASS_ALLOCATOR(VALUE klass);
221static inline void RCLASS_SET_ALLOCATOR(VALUE klass, rb_alloc_func_t allocator);
222static inline VALUE RCLASS_SET_ATTACHED_OBJECT(VALUE klass, VALUE attached_object);
223
224static inline void RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass);
225static inline void RCLASS_SET_MAX_IV_COUNT(VALUE klass, attr_index_t count);
226static inline void RCLASS_SET_CLASSPATH(VALUE klass, VALUE classpath, bool permanent);
227static inline void RCLASS_WRITE_CLASSPATH(VALUE klass, VALUE classpath, bool permanent);
228
229#define RCLASS_IS_ROOT FL_USER0
230// 1 is for RUBY_FL_SINGLETON or RMODULE_IS_REFINEMENT
231#define RCLASS_PRIME_CLASSEXT_WRITABLE FL_USER2
232#define RCLASS_IS_INITIALIZED FL_USER3
233// 3 is RMODULE_IS_REFINEMENT for RMODULE
234#define RCLASS_BOXABLE FL_USER4
235#define RCLASS_ALLOCATOR_DEFINED FL_USER5
236#define RCLASS_HAS_SUBCLASSES FL_USER6
237
238static inline st_table *
239RCLASS_CLASSEXT_TBL(VALUE klass)
240{
241 if (FL_TEST_RAW(klass, RCLASS_BOXABLE)) {
242 struct RClass_boxable *box_klass = (struct RClass_boxable *)klass;
243 return box_klass->box_classext_tbl;
244 }
245 return NULL;
246}
247
248static inline void
249RCLASS_SET_CLASSEXT_TBL(VALUE klass, st_table *tbl)
250{
251 RUBY_ASSERT(FL_TEST_RAW(klass, RCLASS_BOXABLE));
252 struct RClass_boxable *box_klass = (struct RClass_boxable *)klass;
253 box_klass->box_classext_tbl = tbl;
254}
255
256/* class.c */
257rb_classext_t * rb_class_duplicate_classext(rb_classext_t *orig, VALUE obj, const rb_box_t *box);
258void rb_class_ensure_writable(VALUE obj);
259
260void rb_class_set_box_classext(VALUE obj, const rb_box_t *box, rb_classext_t *ext);
261
262static inline int
263RCLASS_SET_BOX_CLASSEXT(VALUE obj, const rb_box_t *box, rb_classext_t *ext)
264{
265 int first_set = 0;
266 st_table *tbl = RCLASS_CLASSEXT_TBL(obj);
267 VM_ASSERT(BOX_MUTABLE_P(box)); // Setting non-prime classext never happens on the master box
268 VM_ASSERT(box->box_object);
269 VM_ASSERT(RCLASSEXT_BOX(ext) == box);
270 if (!tbl) {
271 tbl = st_init_numtable_with_size(1);
272 RCLASS_SET_CLASSEXT_TBL(obj, tbl);
273 }
274 if (rb_st_table_size(tbl) == 0) {
275 first_set = 1;
276 }
277
278 rb_class_set_box_classext(obj, box, ext);
279
280 return first_set;
281}
282
283#define VM_ASSERT_BOXABLE_TYPE(klass) \
284 VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE) || RB_TYPE_P(klass, T_ICLASS), "%s is not boxable type", rb_type_str(BUILTIN_TYPE(klass)))
285
286static inline bool
287RCLASS_PRIME_CLASSEXT_READABLE_P(VALUE klass)
288{
289 VM_ASSERT(klass != 0, "klass should be a valid object");
290 VM_ASSERT_BOXABLE_TYPE(klass);
291 // if the lookup table exists, then it means the prime classext is NOT directly readable.
292 return !FL_TEST_RAW(klass, RCLASS_BOXABLE) || RCLASS_CLASSEXT_TBL(klass) == NULL;
293}
294
295static inline bool
296RCLASS_PRIME_CLASSEXT_WRITABLE_P(VALUE klass)
297{
298 VM_ASSERT(klass != 0, "klass should be a valid object");
299 VM_ASSERT_BOXABLE_TYPE(klass);
300 RBIMPL_ASSUME(klass != 0);
301 return FL_TEST_RAW(klass, RCLASS_PRIME_CLASSEXT_WRITABLE);
302}
303
304static inline void
305RCLASS_SET_PRIME_CLASSEXT_WRITABLE(VALUE klass, bool writable)
306{
307 VM_ASSERT(klass != 0, "klass should be a valid object");
308 VM_ASSERT_BOXABLE_TYPE(klass);
309 if (writable) {
310 FL_SET_RAW(klass, RCLASS_PRIME_CLASSEXT_WRITABLE);
311 }
312 else {
313 FL_UNSET_RAW(klass, RCLASS_PRIME_CLASSEXT_WRITABLE);
314 }
315}
316
317static inline rb_classext_t *
318RCLASS_EXT_TABLE_LOOKUP_INTERNAL(VALUE obj, const rb_box_t *box)
319{
320 st_data_t classext_ptr;
321 st_table *classext_tbl = RCLASS_CLASSEXT_TBL(obj);
322 if (classext_tbl) {
323 if (rb_st_lookup(classext_tbl, (st_data_t)box->box_object, &classext_ptr)) {
324 return (rb_classext_t *)classext_ptr;
325 }
326 }
327 return NULL;
328}
329
330static inline rb_classext_t *
331RCLASS_EXT_READABLE_LOOKUP(VALUE obj, const rb_box_t *box)
332{
333 rb_classext_t *ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(obj, box);
334 if (ext)
335 return ext;
336 // Classext for the ns not found. Refer the prime one instead.
337 return RCLASS_EXT_PRIME(obj);
338}
339
340static inline rb_classext_t *
341RCLASS_EXT_READABLE_IN_BOX(VALUE obj, const rb_box_t *box)
342{
343 if (BOX_MASTER_P(box)
344 || RCLASS_PRIME_CLASSEXT_READABLE_P(obj)) {
345 return RCLASS_EXT_PRIME(obj);
346 }
347 return RCLASS_EXT_READABLE_LOOKUP(obj, box);
348}
349
350static inline rb_classext_t *
351RCLASS_EXT_READABLE(VALUE obj)
352{
353 const rb_box_t *box;
354 if (RCLASS_PRIME_CLASSEXT_READABLE_P(obj)) {
355 return RCLASS_EXT_PRIME(obj);
356 }
357 // delay determining the current box to optimize for unmodified classes
358 box = rb_current_box();
359 if (BOX_MASTER_P(box)) {
360 return RCLASS_EXT_PRIME(obj);
361 }
362 return RCLASS_EXT_READABLE_LOOKUP(obj, box);
363}
364
365static inline rb_classext_t *
366RCLASS_EXT_WRITABLE_LOOKUP(VALUE obj, const rb_box_t *box)
367{
368 rb_classext_t *ext;
369 int first_set = 0;
370
371 ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(obj, box);
372 if (ext)
373 return ext;
374
375 RB_VM_LOCKING() {
376 // re-check the classext is not created to avoid the multi-thread race
377 ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(obj, box);
378 if (!ext) {
379 ext = rb_class_duplicate_classext(RCLASS_EXT_PRIME(obj), obj, box);
380 first_set = RCLASS_SET_BOX_CLASSEXT(obj, box, ext);
381 if (first_set) {
382 // TODO: are there any case that a class/module become non-writable after its birthtime?
383 RCLASS_SET_PRIME_CLASSEXT_WRITABLE(obj, false);
384 }
385 }
386 }
387 return ext;
388}
389
390static inline rb_classext_t *
391RCLASS_EXT_WRITABLE_IN_BOX(VALUE obj, const rb_box_t *box)
392{
393 if (BOX_MASTER_P(box)
394 || RCLASS_PRIME_CLASSEXT_WRITABLE_P(obj)) {
395 return RCLASS_EXT_PRIME(obj);
396 }
397 return RCLASS_EXT_WRITABLE_LOOKUP(obj, box);
398}
399
400static inline rb_classext_t *
401RCLASS_EXT_WRITABLE(VALUE obj)
402{
403 const rb_box_t *box;
404 if (LIKELY(RCLASS_PRIME_CLASSEXT_WRITABLE_P(obj))) {
405 return RCLASS_EXT_PRIME(obj);
406 }
407 // delay determining the current box to optimize for unmodified classes
408 box = rb_current_box();
409 if (BOX_MASTER_P(box)) {
410 return RCLASS_EXT_PRIME(obj);
411 }
412 return RCLASS_EXT_WRITABLE_LOOKUP(obj, box);
413}
414
415static inline void
416RCLASSEXT_SET_ORIGIN(rb_classext_t *ext, VALUE klass, VALUE origin)
417{
418 RB_OBJ_WRITE(klass, &(RCLASSEXT_ORIGIN(ext)), origin);
419}
420
421static inline void
422RCLASSEXT_SET_INCLUDER(rb_classext_t *ext, VALUE klass, VALUE includer)
423{
425 RB_OBJ_WRITE(klass, &(RCLASSEXT_INCLUDER(ext)), includer);
426}
427
428/* class.c */
429typedef void rb_class_classext_foreach_callback_func(rb_classext_t *classext, bool is_prime, VALUE box_value, void *arg);
430void rb_class_classext_foreach(VALUE klass, rb_class_classext_foreach_callback_func *func, void *arg);
431void rb_class_subclass_add(VALUE super, VALUE klass);
432void rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE);
433void rb_class_update_superclasses(VALUE);
434int rb_singleton_class_internal_p(VALUE sklass);
435VALUE rb_class_set_super(VALUE klass, VALUE super);
437VALUE rb_class_s_alloc(VALUE klass);
438VALUE rb_module_s_alloc(VALUE klass);
439void rb_class_set_initialized(VALUE klass);
440void rb_module_check_initializable(VALUE module);
441VALUE rb_make_metaclass(VALUE, VALUE);
442VALUE rb_include_class_new(VALUE, VALUE);
443VALUE rb_define_class_id_under_no_pin(VALUE outer, ID id, VALUE super);
444VALUE rb_obj_methods(int argc, const VALUE *argv, VALUE obj);
445VALUE rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj);
446VALUE rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj);
447VALUE rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj);
448VALUE rb_class_undefined_instance_methods(VALUE mod);
449VALUE rb_special_singleton_class(VALUE);
450VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach);
452void rb_undef_methods_from(VALUE klass, VALUE super);
454VALUE rb_keyword_error_new(const char *, VALUE);
455
456rb_classext_t *rb_class_unlink_classext(VALUE klass, const rb_box_t *box);
457void rb_class_classext_free(VALUE klass, rb_classext_t *ext, bool is_prime);
458void rb_iclass_classext_free(VALUE klass, rb_classext_t *ext, bool is_prime);
459
460RUBY_SYMBOL_EXPORT_BEGIN
461
462/* for objspace */
463VALUE rb_class_super_of(VALUE klass);
464VALUE rb_class_singleton_p(VALUE klass);
465unsigned char rb_class_variation_count(VALUE klass);
466
467RUBY_SYMBOL_EXPORT_END
468
469static inline bool
470RCLASS_SINGLETON_P(VALUE klass)
471{
472 RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE) || RB_TYPE_P(klass, T_ICLASS));
473 return RB_BUILTIN_TYPE(klass) == T_CLASS && FL_TEST_RAW(klass, FL_SINGLETON);
474}
475
476static inline void
477RCLASS_SET_SUPER(VALUE klass, VALUE super)
478{
479 RB_OBJ_WRITE(klass, &RCLASSEXT_SUPER(RCLASS_EXT_PRIME(klass)), super);
480}
481
482static inline void
483RCLASS_WRITE_SUPER(VALUE klass, VALUE super)
484{
485 RB_OBJ_WRITE(klass, &RCLASSEXT_SUPER(RCLASS_EXT_WRITABLE(klass)), super);
486}
487
488static inline VALUE
489RCLASS_WRITABLE_ENSURE_FIELDS_OBJ(VALUE obj)
490{
492 rb_classext_t *ext = RCLASS_EXT_WRITABLE(obj);
493 if (!ext->fields_obj) {
494 RB_OBJ_WRITE(obj, &ext->fields_obj, rb_imemo_fields_new(obj, ROOT_SHAPE_ID, true));
495 }
496 return ext->fields_obj;
497}
498
499static inline VALUE
500RCLASS_WRITABLE_FIELDS_OBJ(VALUE obj)
501{
503 return RCLASSEXT_FIELDS_OBJ(RCLASS_EXT_WRITABLE(obj));
504}
505
506static inline void
507RCLASSEXT_SET_FIELDS_OBJ(VALUE obj, rb_classext_t *ext, VALUE fields_obj)
508{
510
511 RB_OBJ_ATOMIC_WRITE(obj, &ext->fields_obj, fields_obj);
512}
513
514static inline void
515RCLASS_WRITABLE_SET_FIELDS_OBJ(VALUE obj, VALUE fields_obj)
516{
518
519 RCLASSEXT_SET_FIELDS_OBJ(obj, RCLASS_EXT_WRITABLE(obj), fields_obj);
520}
521
522static inline uint32_t
523RCLASS_FIELDS_COUNT(VALUE obj)
524{
526
527 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
528 if (fields_obj) {
529 if (rb_obj_shape_complex_p(fields_obj)) {
530 return (uint32_t)rb_st_table_size(rb_imemo_fields_complex_tbl(fields_obj));
531 }
532 else {
533 return RSHAPE_LEN(RBASIC_SHAPE_ID(fields_obj));
534 }
535 }
536 return 0;
537}
538
539static inline void
540RCLASS_SET_M_TBL(VALUE klass, struct rb_id_table *table)
541{
542 RCLASSEXT_M_TBL(RCLASS_EXT_PRIME(klass)) = table;
543}
544
545static inline void
546RCLASS_WRITE_M_TBL(VALUE klass, struct rb_id_table *table)
547{
548 RCLASSEXT_M_TBL(RCLASS_EXT_WRITABLE(klass)) = table;
549}
550
551static inline void
552RCLASS_SET_CONST_TBL(VALUE klass, struct rb_id_table *table, bool shared)
553{
554 rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
555 RCLASSEXT_CONST_TBL(ext) = table;
556 if (shared)
557 RCLASSEXT_SHARED_CONST_TBL(ext) = true;
558}
559
560static inline void
561RCLASS_WRITE_CONST_TBL(VALUE klass, struct rb_id_table *table, bool shared)
562{
563 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
564 RCLASSEXT_CONST_TBL(ext) = table;
565 if (shared)
566 RCLASSEXT_SHARED_CONST_TBL(ext) = true;
567}
568
569static inline void
570RCLASS_WRITE_CALLABLE_M_TBL(VALUE klass, struct rb_id_table *table)
571{
572 RCLASSEXT_CALLABLE_M_TBL(RCLASS_EXT_WRITABLE(klass)) = table;
573}
574
575static inline void
576RCLASS_WRITE_CC_TBL(VALUE klass, VALUE table)
577{
578 RB_OBJ_ATOMIC_WRITE(klass, &RCLASSEXT_CC_TBL(RCLASS_EXT_WRITABLE(klass)), table);
579}
580
581static inline void
582RCLASS_SET_CVC_TBL(VALUE klass, VALUE table)
583{
584 RB_OBJ_ATOMIC_WRITE(klass, &RCLASSEXT_CVC_TBL(RCLASS_EXT_PRIME(klass)), table);
585}
586
587static inline void
588RCLASS_WRITE_CVC_TBL(VALUE klass, VALUE table)
589{
590 RB_OBJ_ATOMIC_WRITE(klass, &RCLASSEXT_CVC_TBL(RCLASS_EXT_WRITABLE(klass)), table);
591}
592
593static inline void
594RCLASS_SET_REFINED_CLASS(VALUE klass, VALUE refined)
595{
596 RB_OBJ_WRITE(klass, &RCLASSEXT_REFINED_CLASS(RCLASS_EXT_PRIME(klass)), refined);
597}
598
599static inline rb_alloc_func_t
600RCLASS_ALLOCATOR(VALUE klass)
601{
602 RBIMPL_ASSERT_TYPE(klass, T_CLASS);
603 RUBY_ASSERT(!RCLASS_SINGLETON_P(klass));
604 return RCLASS_EXT_PRIME(klass)->as.class.allocator;
605}
606
607static inline void
608RCLASS_SET_ALLOCATOR(VALUE klass, rb_alloc_func_t allocator)
609{
611 RUBY_ASSERT(!RCLASS_SINGLETON_P(klass));
612 RCLASS_EXT_PRIME(klass)->as.class.allocator = allocator; // Allocator is set only on the initial definition
613}
614
615static inline void
616RCLASS_SET_ORIGIN(VALUE klass, VALUE origin)
617{
618 rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
619 RB_OBJ_WRITE(klass, &RCLASSEXT_ORIGIN(ext), origin);
620 if (klass != origin) RCLASSEXT_ICLASS_IS_ORIGIN(RCLASS_EXT_WRITABLE(origin)) = true;
621}
622
623static inline void
624RCLASS_WRITE_ORIGIN(VALUE klass, VALUE origin)
625{
626 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
627 RB_OBJ_WRITE(klass, &RCLASSEXT_ORIGIN(ext), origin);
628 if (klass != origin) RCLASSEXT_ICLASS_IS_ORIGIN(RCLASS_EXT_WRITABLE(origin)) = true;
629}
630
631static inline void
632RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass)
633{
634 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(RCLASS_EXT_PRIME(iclass)) = true;
635}
636
637static inline void
638RICLASS_WRITE_ORIGIN_SHARED_MTBL(VALUE iclass)
639{
640 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(RCLASS_EXT_WRITABLE(iclass)) = true;
641}
642
643static inline bool
644RICLASS_OWNS_M_TBL_P(VALUE iclass)
645{
646 rb_classext_t *ext = RCLASS_EXT_READABLE(iclass);
647 return RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext);
648}
649
650static inline bool
651RICLASS_FOR_REFINEMENT_P(VALUE iclass)
652{
653 return BUILTIN_TYPE(iclass) == T_ICLASS &&
654 RB_TYPE_P(RBASIC(iclass)->klass, T_MODULE) &&
655 FL_TEST_RAW(RBASIC(iclass)->klass, RMODULE_IS_REFINEMENT);
656}
657
658static inline void
659RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass)
660{
662 RB_OBJ_WRITE(iclass, &RCLASS_INCLUDER(iclass), klass);
663}
664
665static inline void
666RCLASS_WRITE_SUPERCLASSES(VALUE klass, size_t depth, VALUE *superclasses, bool with_self)
667{
668 RUBY_ASSERT(depth <= RCLASS_MAX_SUPERCLASS_DEPTH);
669
670 rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
671 RCLASSEXT_SUPERCLASS_DEPTH(ext) = depth;
672 RCLASSEXT_SUPERCLASSES(ext) = superclasses;
673 RCLASSEXT_SUPERCLASSES_WITH_SELF(ext) = with_self;
674}
675
676static inline void
677RCLASS_SET_SUBCLASSES(VALUE klass, VALUE subclasses)
678{
679 rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
680 RB_OBJ_WRITE(klass, &RCLASSEXT_SUBCLASSES(ext), subclasses);
681}
682
683static inline void
684RCLASS_SET_CLASSPATH(VALUE klass, VALUE classpath, bool permanent)
685{
686 rb_classext_t *ext = RCLASS_EXT_READABLE(klass);
687 assert(BUILTIN_TYPE(klass) == T_CLASS || BUILTIN_TYPE(klass) == T_MODULE);
688 assert(classpath == 0 || BUILTIN_TYPE(classpath) == T_STRING);
689 assert(FL_TEST_RAW(classpath, RUBY_FL_SHAREABLE));
690
691 RB_OBJ_WRITE(klass, &(RCLASSEXT_CLASSPATH(ext)), classpath);
692 RCLASSEXT_PERMANENT_CLASSPATH(ext) = permanent;
693}
694
695static inline void
696RCLASS_WRITE_CLASSPATH(VALUE klass, VALUE classpath, bool permanent)
697{
698 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
699 assert(BUILTIN_TYPE(klass) == T_CLASS || BUILTIN_TYPE(klass) == T_MODULE);
700 assert(classpath == 0 || BUILTIN_TYPE(classpath) == T_STRING);
701 assert(!RB_FL_ABLE(classpath) || FL_TEST_RAW(classpath, RUBY_FL_SHAREABLE));
702
703 RB_OBJ_WRITE(klass, &(RCLASSEXT_CLASSPATH(ext)), classpath);
704 RCLASSEXT_PERMANENT_CLASSPATH(ext) = permanent;
705}
706
707static inline VALUE
708RCLASS_SET_ATTACHED_OBJECT(VALUE klass, VALUE attached_object)
709{
710 assert(RCLASS_SINGLETON_P(klass));
711
712 RB_OBJ_WRITE(klass, &RCLASS_EXT_PRIME(klass)->as.singleton_class.attached_object, attached_object);
713 return attached_object;
714}
715
716static inline void
717RCLASS_SET_MAX_IV_COUNT(VALUE klass, attr_index_t count)
718{
719 RUBY_ASSERT(klass != rb_cObject);
721
722 RCLASS_MAX_IV_COUNT(klass) = count;
723}
724
725static inline void
726RCLASS_SET_EXPECT_NO_IVAR(VALUE klass)
727{
728 RCLASS_EXT_PRIME(klass)->expect_no_ivar = true;
729}
730
731static inline bool
732RCLASS_EXPECT_NO_IVAR(VALUE klass)
733{
734 return RCLASS_EXT_PRIME(klass)->expect_no_ivar;
735}
736
737static inline bool
738RCLASS_INITIALIZED_P(VALUE klass)
739{
740 VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE));
741 return FL_TEST_RAW(klass, RCLASS_IS_INITIALIZED);
742}
743
744#endif /* INTERNAL_CLASS_H */
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
static bool RB_FL_ABLE(VALUE obj)
Checks if the object is flaggable.
Definition fl_type.h:381
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
Definition fl_type.h:253
VALUE rb_class_boot(VALUE)
A utility function that wraps class_alloc.
Definition class.c:699
VALUE rb_class_inherited(VALUE, VALUE)
Calls Class::inherited.
Definition class.c:1387
VALUE rb_singleton_class_get(VALUE obj)
Returns the singleton class of obj, or nil if obj is not a singleton object.
Definition class.c:2714
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
Definition fl_type.h:58
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
Definition fl_type.h:130
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:128
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:126
VALUE rb_cObject
Object class.
Definition object.c:61
VALUE rb_cBasicObject
BasicObject class.
Definition object.c:59
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:456
VALUE(* rb_alloc_func_t)(VALUE klass)
This is the type of functions that ruby calls when trying to allocate an object.
Definition vm.h:219
#define RBIMPL_ASSUME(_)
Wraps (or simulates) __builtin_unreachable.
Definition assume.h:76
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
C99 shim for <stdbool.h>
Ruby object's base components.
Definition rbasic.h:69
Definition class.h:82
Internal header for Ruby Box.
Definition box.h:14
VALUE subclasses
imemo_subclasses VALUE tracking this class's subclasses.
Definition class.h:53
CREF (Class REFerence)
Definition method.h:45
Internal header for Class.
Definition class.h:30
Definition st.h:79
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 enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.
Definition value_type.h:182
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
@ RUBY_T_MODULE
Definition value_type.h:118
@ RUBY_T_CLASS
Definition value_type.h:117