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