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