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