Ruby 4.1.0dev (2026-05-15 revision a8bcae043f931d9b79f1cb1fe2c021985d07b984)
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 return FL_TEST_RAW(klass, RCLASS_PRIME_CLASSEXT_WRITABLE);
301}
302
303static inline void
304RCLASS_SET_PRIME_CLASSEXT_WRITABLE(VALUE klass, bool writable)
305{
306 VM_ASSERT(klass != 0, "klass should be a valid object");
307 VM_ASSERT_BOXABLE_TYPE(klass);
308 if (writable) {
309 FL_SET_RAW(klass, RCLASS_PRIME_CLASSEXT_WRITABLE);
310 }
311 else {
312 FL_UNSET_RAW(klass, RCLASS_PRIME_CLASSEXT_WRITABLE);
313 }
314}
315
316static inline rb_classext_t *
317RCLASS_EXT_TABLE_LOOKUP_INTERNAL(VALUE obj, const rb_box_t *box)
318{
319 st_data_t classext_ptr;
320 st_table *classext_tbl = RCLASS_CLASSEXT_TBL(obj);
321 if (classext_tbl) {
322 if (rb_st_lookup(classext_tbl, (st_data_t)box->box_object, &classext_ptr)) {
323 return (rb_classext_t *)classext_ptr;
324 }
325 }
326 return NULL;
327}
328
329static inline rb_classext_t *
330RCLASS_EXT_READABLE_LOOKUP(VALUE obj, const rb_box_t *box)
331{
332 rb_classext_t *ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(obj, box);
333 if (ext)
334 return ext;
335 // Classext for the ns not found. Refer the prime one instead.
336 return RCLASS_EXT_PRIME(obj);
337}
338
339static inline rb_classext_t *
340RCLASS_EXT_READABLE_IN_BOX(VALUE obj, const rb_box_t *box)
341{
342 if (BOX_MASTER_P(box)
343 || RCLASS_PRIME_CLASSEXT_READABLE_P(obj)) {
344 return RCLASS_EXT_PRIME(obj);
345 }
346 return RCLASS_EXT_READABLE_LOOKUP(obj, box);
347}
348
349static inline rb_classext_t *
350RCLASS_EXT_READABLE(VALUE obj)
351{
352 const rb_box_t *box;
353 if (RCLASS_PRIME_CLASSEXT_READABLE_P(obj)) {
354 return RCLASS_EXT_PRIME(obj);
355 }
356 // delay determining the current box to optimize for unmodified classes
357 box = rb_current_box();
358 if (BOX_MASTER_P(box)) {
359 return RCLASS_EXT_PRIME(obj);
360 }
361 return RCLASS_EXT_READABLE_LOOKUP(obj, box);
362}
363
364static inline rb_classext_t *
365RCLASS_EXT_WRITABLE_LOOKUP(VALUE obj, const rb_box_t *box)
366{
367 rb_classext_t *ext;
368 int first_set = 0;
369
370 ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(obj, box);
371 if (ext)
372 return ext;
373
374 RB_VM_LOCKING() {
375 // re-check the classext is not created to avoid the multi-thread race
376 ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(obj, box);
377 if (!ext) {
378 ext = rb_class_duplicate_classext(RCLASS_EXT_PRIME(obj), obj, box);
379 first_set = RCLASS_SET_BOX_CLASSEXT(obj, box, ext);
380 if (first_set) {
381 // TODO: are there any case that a class/module become non-writable after its birthtime?
382 RCLASS_SET_PRIME_CLASSEXT_WRITABLE(obj, false);
383 }
384 }
385 }
386 return ext;
387}
388
389static inline rb_classext_t *
390RCLASS_EXT_WRITABLE_IN_BOX(VALUE obj, const rb_box_t *box)
391{
392 if (BOX_MASTER_P(box)
393 || RCLASS_PRIME_CLASSEXT_WRITABLE_P(obj)) {
394 return RCLASS_EXT_PRIME(obj);
395 }
396 return RCLASS_EXT_WRITABLE_LOOKUP(obj, box);
397}
398
399static inline rb_classext_t *
400RCLASS_EXT_WRITABLE(VALUE obj)
401{
402 const rb_box_t *box;
403 if (LIKELY(RCLASS_PRIME_CLASSEXT_WRITABLE_P(obj))) {
404 return RCLASS_EXT_PRIME(obj);
405 }
406 // delay determining the current box to optimize for unmodified classes
407 box = rb_current_box();
408 if (BOX_MASTER_P(box)) {
409 return RCLASS_EXT_PRIME(obj);
410 }
411 return RCLASS_EXT_WRITABLE_LOOKUP(obj, box);
412}
413
414static inline void
415RCLASSEXT_SET_ORIGIN(rb_classext_t *ext, VALUE klass, VALUE origin)
416{
417 RB_OBJ_WRITE(klass, &(RCLASSEXT_ORIGIN(ext)), origin);
418}
419
420static inline void
421RCLASSEXT_SET_INCLUDER(rb_classext_t *ext, VALUE klass, VALUE includer)
422{
424 RB_OBJ_WRITE(klass, &(RCLASSEXT_INCLUDER(ext)), includer);
425}
426
427/* class.c */
428typedef void rb_class_classext_foreach_callback_func(rb_classext_t *classext, bool is_prime, VALUE box_value, void *arg);
429void rb_class_classext_foreach(VALUE klass, rb_class_classext_foreach_callback_func *func, void *arg);
430void rb_class_subclass_add(VALUE super, VALUE klass);
431void rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE);
432void rb_class_update_superclasses(VALUE);
433int rb_singleton_class_internal_p(VALUE sklass);
434VALUE rb_class_set_super(VALUE klass, VALUE super);
436VALUE rb_class_s_alloc(VALUE klass);
437VALUE rb_module_s_alloc(VALUE klass);
438void rb_class_set_initialized(VALUE klass);
439void rb_module_check_initializable(VALUE module);
440VALUE rb_make_metaclass(VALUE, VALUE);
441VALUE rb_include_class_new(VALUE, VALUE);
442VALUE rb_define_class_id_under_no_pin(VALUE outer, ID id, VALUE super);
443VALUE rb_obj_methods(int argc, const VALUE *argv, VALUE obj);
444VALUE rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj);
445VALUE rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj);
446VALUE rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj);
447VALUE rb_class_undefined_instance_methods(VALUE mod);
448VALUE rb_special_singleton_class(VALUE);
449VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach);
451void rb_undef_methods_from(VALUE klass, VALUE super);
453VALUE rb_keyword_error_new(const char *, VALUE);
454
455rb_classext_t *rb_class_unlink_classext(VALUE klass, const rb_box_t *box);
456void rb_class_classext_free(VALUE klass, rb_classext_t *ext, bool is_prime);
457void rb_iclass_classext_free(VALUE klass, rb_classext_t *ext, bool is_prime);
458
459RUBY_SYMBOL_EXPORT_BEGIN
460
461/* for objspace */
462VALUE rb_class_super_of(VALUE klass);
463VALUE rb_class_singleton_p(VALUE klass);
464unsigned char rb_class_variation_count(VALUE klass);
465
466RUBY_SYMBOL_EXPORT_END
467
468static inline bool
469RCLASS_SINGLETON_P(VALUE klass)
470{
471 RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE) || RB_TYPE_P(klass, T_ICLASS));
472 return RB_BUILTIN_TYPE(klass) == T_CLASS && FL_TEST_RAW(klass, FL_SINGLETON);
473}
474
475static inline void
476RCLASS_SET_SUPER(VALUE klass, VALUE super)
477{
478 RB_OBJ_WRITE(klass, &RCLASSEXT_SUPER(RCLASS_EXT_PRIME(klass)), super);
479}
480
481static inline void
482RCLASS_WRITE_SUPER(VALUE klass, VALUE super)
483{
484 RB_OBJ_WRITE(klass, &RCLASSEXT_SUPER(RCLASS_EXT_WRITABLE(klass)), super);
485}
486
487static inline VALUE
488RCLASS_WRITABLE_ENSURE_FIELDS_OBJ(VALUE obj)
489{
491 rb_classext_t *ext = RCLASS_EXT_WRITABLE(obj);
492 if (!ext->fields_obj) {
493 RB_OBJ_WRITE(obj, &ext->fields_obj, rb_imemo_fields_new(obj, ROOT_SHAPE_ID, true));
494 }
495 return ext->fields_obj;
496}
497
498static inline VALUE
499RCLASS_WRITABLE_FIELDS_OBJ(VALUE obj)
500{
502 return RCLASSEXT_FIELDS_OBJ(RCLASS_EXT_WRITABLE(obj));
503}
504
505static inline void
506RCLASSEXT_SET_FIELDS_OBJ(VALUE obj, rb_classext_t *ext, VALUE fields_obj)
507{
509
510 RB_OBJ_ATOMIC_WRITE(obj, &ext->fields_obj, fields_obj);
511}
512
513static inline void
514RCLASS_WRITABLE_SET_FIELDS_OBJ(VALUE obj, VALUE fields_obj)
515{
517
518 RCLASSEXT_SET_FIELDS_OBJ(obj, RCLASS_EXT_WRITABLE(obj), fields_obj);
519}
520
521static inline uint32_t
522RCLASS_FIELDS_COUNT(VALUE obj)
523{
525
526 VALUE fields_obj = RCLASS_WRITABLE_FIELDS_OBJ(obj);
527 if (fields_obj) {
528 if (rb_obj_shape_complex_p(fields_obj)) {
529 return (uint32_t)rb_st_table_size(rb_imemo_fields_complex_tbl(fields_obj));
530 }
531 else {
532 return RSHAPE_LEN(RBASIC_SHAPE_ID(fields_obj));
533 }
534 }
535 return 0;
536}
537
538static inline void
539RCLASS_SET_M_TBL(VALUE klass, struct rb_id_table *table)
540{
541 RCLASSEXT_M_TBL(RCLASS_EXT_PRIME(klass)) = table;
542}
543
544static inline void
545RCLASS_WRITE_M_TBL(VALUE klass, struct rb_id_table *table)
546{
547 RCLASSEXT_M_TBL(RCLASS_EXT_WRITABLE(klass)) = table;
548}
549
550static inline void
551RCLASS_SET_CONST_TBL(VALUE klass, struct rb_id_table *table, bool shared)
552{
553 rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
554 RCLASSEXT_CONST_TBL(ext) = table;
555 if (shared)
556 RCLASSEXT_SHARED_CONST_TBL(ext) = true;
557}
558
559static inline void
560RCLASS_WRITE_CONST_TBL(VALUE klass, struct rb_id_table *table, bool shared)
561{
562 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
563 RCLASSEXT_CONST_TBL(ext) = table;
564 if (shared)
565 RCLASSEXT_SHARED_CONST_TBL(ext) = true;
566}
567
568static inline void
569RCLASS_WRITE_CALLABLE_M_TBL(VALUE klass, struct rb_id_table *table)
570{
571 RCLASSEXT_CALLABLE_M_TBL(RCLASS_EXT_WRITABLE(klass)) = table;
572}
573
574static inline void
575RCLASS_WRITE_CC_TBL(VALUE klass, VALUE table)
576{
577 RB_OBJ_ATOMIC_WRITE(klass, &RCLASSEXT_CC_TBL(RCLASS_EXT_WRITABLE(klass)), table);
578}
579
580static inline void
581RCLASS_SET_CVC_TBL(VALUE klass, VALUE table)
582{
583 RB_OBJ_ATOMIC_WRITE(klass, &RCLASSEXT_CVC_TBL(RCLASS_EXT_PRIME(klass)), table);
584}
585
586static inline void
587RCLASS_WRITE_CVC_TBL(VALUE klass, VALUE table)
588{
589 RB_OBJ_ATOMIC_WRITE(klass, &RCLASSEXT_CVC_TBL(RCLASS_EXT_WRITABLE(klass)), table);
590}
591
592static inline void
593RCLASS_SET_REFINED_CLASS(VALUE klass, VALUE refined)
594{
595 RB_OBJ_WRITE(klass, &RCLASSEXT_REFINED_CLASS(RCLASS_EXT_PRIME(klass)), refined);
596}
597
598static inline rb_alloc_func_t
599RCLASS_ALLOCATOR(VALUE klass)
600{
601 RBIMPL_ASSERT_TYPE(klass, T_CLASS);
602 RUBY_ASSERT(!RCLASS_SINGLETON_P(klass));
603 return RCLASS_EXT_PRIME(klass)->as.class.allocator;
604}
605
606static inline void
607RCLASS_SET_ALLOCATOR(VALUE klass, rb_alloc_func_t allocator)
608{
610 RUBY_ASSERT(!RCLASS_SINGLETON_P(klass));
611 RCLASS_EXT_PRIME(klass)->as.class.allocator = allocator; // Allocator is set only on the initial definition
612}
613
614static inline void
615RCLASS_SET_ORIGIN(VALUE klass, VALUE origin)
616{
617 rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
618 RB_OBJ_WRITE(klass, &RCLASSEXT_ORIGIN(ext), origin);
619 if (klass != origin) RCLASSEXT_ICLASS_IS_ORIGIN(RCLASS_EXT_WRITABLE(origin)) = true;
620}
621
622static inline void
623RCLASS_WRITE_ORIGIN(VALUE klass, VALUE origin)
624{
625 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
626 RB_OBJ_WRITE(klass, &RCLASSEXT_ORIGIN(ext), origin);
627 if (klass != origin) RCLASSEXT_ICLASS_IS_ORIGIN(RCLASS_EXT_WRITABLE(origin)) = true;
628}
629
630static inline void
631RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass)
632{
633 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(RCLASS_EXT_PRIME(iclass)) = true;
634}
635
636static inline void
637RICLASS_WRITE_ORIGIN_SHARED_MTBL(VALUE iclass)
638{
639 RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(RCLASS_EXT_WRITABLE(iclass)) = true;
640}
641
642static inline bool
643RICLASS_OWNS_M_TBL_P(VALUE iclass)
644{
645 rb_classext_t *ext = RCLASS_EXT_READABLE(iclass);
646 return RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext);
647}
648
649static inline void
650RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass)
651{
653 RB_OBJ_WRITE(iclass, &RCLASS_INCLUDER(iclass), klass);
654}
655
656static inline void
657RCLASS_WRITE_SUPERCLASSES(VALUE klass, size_t depth, VALUE *superclasses, bool with_self)
658{
659 RUBY_ASSERT(depth <= RCLASS_MAX_SUPERCLASS_DEPTH);
660
661 rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
662 RCLASSEXT_SUPERCLASS_DEPTH(ext) = depth;
663 RCLASSEXT_SUPERCLASSES(ext) = superclasses;
664 RCLASSEXT_SUPERCLASSES_WITH_SELF(ext) = with_self;
665}
666
667static inline void
668RCLASS_SET_SUBCLASSES(VALUE klass, VALUE subclasses)
669{
670 rb_classext_t *ext = RCLASS_EXT_PRIME(klass);
671 RB_OBJ_WRITE(klass, &RCLASSEXT_SUBCLASSES(ext), subclasses);
672}
673
674static inline void
675RCLASS_SET_CLASSPATH(VALUE klass, VALUE classpath, bool permanent)
676{
677 rb_classext_t *ext = RCLASS_EXT_READABLE(klass);
678 assert(BUILTIN_TYPE(klass) == T_CLASS || BUILTIN_TYPE(klass) == T_MODULE);
679 assert(classpath == 0 || BUILTIN_TYPE(classpath) == T_STRING);
680 assert(FL_TEST_RAW(classpath, RUBY_FL_SHAREABLE));
681
682 RB_OBJ_WRITE(klass, &(RCLASSEXT_CLASSPATH(ext)), classpath);
683 RCLASSEXT_PERMANENT_CLASSPATH(ext) = permanent;
684}
685
686static inline void
687RCLASS_WRITE_CLASSPATH(VALUE klass, VALUE classpath, bool permanent)
688{
689 rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
690 assert(BUILTIN_TYPE(klass) == T_CLASS || BUILTIN_TYPE(klass) == T_MODULE);
691 assert(classpath == 0 || BUILTIN_TYPE(classpath) == T_STRING);
692 assert(!RB_FL_ABLE(classpath) || FL_TEST_RAW(classpath, RUBY_FL_SHAREABLE));
693
694 RB_OBJ_WRITE(klass, &(RCLASSEXT_CLASSPATH(ext)), classpath);
695 RCLASSEXT_PERMANENT_CLASSPATH(ext) = permanent;
696}
697
698static inline VALUE
699RCLASS_SET_ATTACHED_OBJECT(VALUE klass, VALUE attached_object)
700{
701 assert(RCLASS_SINGLETON_P(klass));
702
703 RB_OBJ_WRITE(klass, &RCLASS_EXT_PRIME(klass)->as.singleton_class.attached_object, attached_object);
704 return attached_object;
705}
706
707static inline void
708RCLASS_SET_MAX_IV_COUNT(VALUE klass, attr_index_t count)
709{
710 RUBY_ASSERT(klass != rb_cObject);
712
713 RCLASS_MAX_IV_COUNT(klass) = count;
714}
715
716static inline void
717RCLASS_SET_EXPECT_NO_IVAR(VALUE klass)
718{
719 RCLASS_EXT_PRIME(klass)->expect_no_ivar = true;
720}
721
722static inline bool
723RCLASS_EXPECT_NO_IVAR(VALUE klass)
724{
725 return RCLASS_EXT_PRIME(klass)->expect_no_ivar;
726}
727
728static inline bool
729RCLASS_INITIALIZED_P(VALUE klass)
730{
731 VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE));
732 return FL_TEST_RAW(klass, RCLASS_IS_INITIALIZED);
733}
734
735#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
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