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