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