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