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