Ruby 3.5.0dev (2025-02-22 revision 412997300569c1853c09813e4924b6df3d7e8669)
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/serial.h" /* for rb_serial_t */
14#include "internal/static_assert.h"
15#include "internal/variable.h" /* for rb_class_ivar_set */
16#include "ruby/internal/stdbool.h" /* for bool */
17#include "ruby/intern.h" /* for rb_alloc_func_t */
18#include "ruby/ruby.h" /* for struct RBasic */
19#include "shape.h"
20#include "ruby_assert.h"
21#include "vm_core.h"
22#include "vm_sync.h"
23#include "method.h" /* for rb_cref_t */
24
25#ifdef RCLASS_SUPER
26# undef RCLASS_SUPER
27#endif
28
30 VALUE klass;
31 struct rb_subclass_entry *next;
32 struct rb_subclass_entry *prev;
33};
35
37 uint32_t index;
38 rb_serial_t global_cvar_state;
39 const rb_cref_t * cref;
40 VALUE class_value;
41};
42
44 VALUE *iv_ptr;
45 struct rb_id_table *const_tbl;
46 struct rb_id_table *callable_m_tbl;
47 struct rb_id_table *cc_tbl; /* ID -> [[ci1, cc1], [ci2, cc2] ...] */
48 struct rb_id_table *cvc_tbl;
49 size_t superclass_depth;
50 VALUE *superclasses;
51 struct rb_subclass_entry *subclasses;
52 struct rb_subclass_entry *subclass_entry;
59 const VALUE origin_;
60 const VALUE refined_class;
61 union {
62 struct {
63 rb_alloc_func_t allocator;
64 } class;
65 struct {
66 VALUE attached_object;
67 } singleton_class;
68 } as;
69 const VALUE includer;
70 attr_index_t max_iv_count;
71 unsigned char variation_count;
72 bool permanent_classpath : 1;
73 bool cloned : 1;
74 VALUE classpath;
75};
77
78STATIC_ASSERT(shape_max_variations, SHAPE_MAX_VARIATIONS < (1 << (sizeof(((rb_classext_t *)0)->variation_count) * CHAR_BIT)));
79
80struct RClass {
81 struct RBasic basic;
82 VALUE super;
83 struct rb_id_table *m_tbl;
84};
85
86// Assert that classes can be embedded in heaps[2] (which has 160B slot size)
87STATIC_ASSERT(sizeof_rb_classext_t, sizeof(struct RClass) + sizeof(rb_classext_t) <= 4 * RVALUE_SIZE);
88
90 struct RClass rclass;
91 rb_classext_t classext;
92};
93
94#define RCLASS_EXT(c) (&((struct RClass_and_rb_classext_t*)(c))->classext)
95#define RCLASS_CONST_TBL(c) (RCLASS_EXT(c)->const_tbl)
96#define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl)
97#define RCLASS_IVPTR(c) (RCLASS_EXT(c)->iv_ptr)
98#define RCLASS_CALLABLE_M_TBL(c) (RCLASS_EXT(c)->callable_m_tbl)
99#define RCLASS_CC_TBL(c) (RCLASS_EXT(c)->cc_tbl)
100#define RCLASS_CVC_TBL(c) (RCLASS_EXT(c)->cvc_tbl)
101#define RCLASS_ORIGIN(c) (RCLASS_EXT(c)->origin_)
102#define RCLASS_REFINED_CLASS(c) (RCLASS_EXT(c)->refined_class)
103#define RCLASS_INCLUDER(c) (RCLASS_EXT(c)->includer)
104#define RCLASS_SUBCLASS_ENTRY(c) (RCLASS_EXT(c)->subclass_entry)
105#define RCLASS_MODULE_SUBCLASS_ENTRY(c) (RCLASS_EXT(c)->module_subclass_entry)
106#define RCLASS_SUBCLASSES(c) (RCLASS_EXT(c)->subclasses)
107#define RCLASS_SUPERCLASS_DEPTH(c) (RCLASS_EXT(c)->superclass_depth)
108#define RCLASS_SUPERCLASSES(c) (RCLASS_EXT(c)->superclasses)
109#define RCLASS_ATTACHED_OBJECT(c) (RCLASS_EXT(c)->as.singleton_class.attached_object)
110
111#define RCLASS_IS_ROOT FL_USER0
112#define RICLASS_IS_ORIGIN FL_USER0
113#define RCLASS_SUPERCLASSES_INCLUDE_SELF FL_USER2
114#define RICLASS_ORIGIN_SHARED_MTBL FL_USER3
115
116static inline st_table *
117RCLASS_IV_HASH(VALUE obj)
118{
120 RUBY_ASSERT(rb_shape_obj_too_complex(obj));
121 return (st_table *)RCLASS_IVPTR(obj);
122}
123
124static inline void
125RCLASS_SET_IV_HASH(VALUE obj, const st_table *tbl)
126{
128 RUBY_ASSERT(rb_shape_obj_too_complex(obj));
129 RCLASS_IVPTR(obj) = (VALUE *)tbl;
130}
131
132static inline uint32_t
133RCLASS_IV_COUNT(VALUE obj)
134{
136 if (rb_shape_obj_too_complex(obj)) {
137 uint32_t count;
138
139 // "Too complex" classes could have their IV hash mutated in
140 // parallel, so lets lock around getting the hash size.
141 RB_VM_LOCK_ENTER();
142 {
143 count = (uint32_t)rb_st_table_size(RCLASS_IV_HASH(obj));
144 }
145 RB_VM_LOCK_LEAVE();
146
147 return count;
148 }
149 else {
150 return rb_shape_get_shape_by_id(RCLASS_SHAPE_ID(obj))->next_iv_index;
151 }
152}
153
154static inline void
155RCLASS_SET_M_TBL(VALUE klass, struct rb_id_table *table)
156{
158 RCLASS_M_TBL(klass) = table;
159}
160
161/* class.c */
162void rb_class_subclass_add(VALUE super, VALUE klass);
163void rb_class_remove_from_super_subclasses(VALUE);
164void rb_class_update_superclasses(VALUE);
165size_t rb_class_superclasses_memsize(VALUE);
166void rb_class_remove_subclass_head(VALUE);
167int rb_singleton_class_internal_p(VALUE sklass);
169VALUE rb_class_s_alloc(VALUE klass);
170VALUE rb_module_s_alloc(VALUE klass);
171void rb_module_set_initialized(VALUE module);
172void rb_module_check_initializable(VALUE module);
173VALUE rb_make_metaclass(VALUE, VALUE);
174VALUE rb_include_class_new(VALUE, VALUE);
175void rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE);
176void rb_class_detach_subclasses(VALUE);
177void rb_class_detach_module_subclasses(VALUE);
178void rb_class_remove_from_module_subclasses(VALUE);
179VALUE rb_define_class_id_under_no_pin(VALUE outer, ID id, VALUE super);
180VALUE rb_obj_methods(int argc, const VALUE *argv, VALUE obj);
181VALUE rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj);
182VALUE rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj);
183VALUE rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj);
184VALUE rb_class_undefined_instance_methods(VALUE mod);
185VALUE rb_special_singleton_class(VALUE);
186VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach);
188void rb_undef_methods_from(VALUE klass, VALUE super);
189
190static inline void RCLASS_SET_ORIGIN(VALUE klass, VALUE origin);
191static inline void RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass);
192static inline VALUE RCLASS_SUPER(VALUE klass);
193static inline VALUE RCLASS_SET_SUPER(VALUE klass, VALUE super);
194static inline void RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass);
195
197VALUE rb_keyword_error_new(const char *, VALUE);
198
199static inline bool
200RCLASS_SINGLETON_P(VALUE klass)
201{
202 return RB_TYPE_P(klass, T_CLASS) && FL_TEST_RAW(klass, FL_SINGLETON);
203}
204
205static inline rb_alloc_func_t
206RCLASS_ALLOCATOR(VALUE klass)
207{
208 if (RCLASS_SINGLETON_P(klass)) {
209 return 0;
210 }
211 return RCLASS_EXT(klass)->as.class.allocator;
212}
213
214static inline void
215RCLASS_SET_ALLOCATOR(VALUE klass, rb_alloc_func_t allocator)
216{
217 assert(!RCLASS_SINGLETON_P(klass));
218 RCLASS_EXT(klass)->as.class.allocator = allocator;
219}
220
221static inline void
222RCLASS_SET_ORIGIN(VALUE klass, VALUE origin)
223{
224 RB_OBJ_WRITE(klass, &RCLASS_ORIGIN(klass), origin);
225 if (klass != origin) FL_SET(origin, RICLASS_IS_ORIGIN);
226}
227
228static inline void
229RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass)
230{
231 FL_SET(iclass, RICLASS_ORIGIN_SHARED_MTBL);
232}
233
234static inline bool
235RICLASS_OWNS_M_TBL_P(VALUE iclass)
236{
237 return FL_TEST_RAW(iclass, RICLASS_IS_ORIGIN | RICLASS_ORIGIN_SHARED_MTBL) == RICLASS_IS_ORIGIN;
238}
239
240static inline void
241RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass)
242{
243 RB_OBJ_WRITE(iclass, &RCLASS_INCLUDER(iclass), klass);
244}
245
246static inline VALUE
247RCLASS_SUPER(VALUE klass)
248{
249 return RCLASS(klass)->super;
250}
251
252static inline VALUE
253RCLASS_SET_SUPER(VALUE klass, VALUE super)
254{
255 if (super) {
256 rb_class_remove_from_super_subclasses(klass);
257 rb_class_subclass_add(super, klass);
258 }
259 RB_OBJ_WRITE(klass, &RCLASS(klass)->super, super);
260 rb_class_update_superclasses(klass);
261 return super;
262}
263
264static inline void
265RCLASS_SET_CLASSPATH(VALUE klass, VALUE classpath, bool permanent)
266{
267 assert(BUILTIN_TYPE(klass) == T_CLASS || BUILTIN_TYPE(klass) == T_MODULE);
268 assert(classpath == 0 || BUILTIN_TYPE(classpath) == T_STRING);
269
270 RB_OBJ_WRITE(klass, &(RCLASS_EXT(klass)->classpath), classpath);
271 RCLASS_EXT(klass)->permanent_classpath = permanent;
272}
273
274static inline VALUE
275RCLASS_SET_ATTACHED_OBJECT(VALUE klass, VALUE attached_object)
276{
277 assert(RCLASS_SINGLETON_P(klass));
278
279 RB_OBJ_WRITE(klass, &RCLASS_EXT(klass)->as.singleton_class.attached_object, attached_object);
280 return attached_object;
281}
282
283#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
VALUE rb_class_boot(VALUE)
A utility function that wraps class_alloc.
Definition class.c:281
VALUE rb_class_inherited(VALUE, VALUE)
Calls Class::inherited.
Definition class.c:971
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:2283
#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 FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:132
#define FL_SET
Old name of RB_FL_SET.
Definition fl_type.h:129
#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 RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
static bool RB_OBJ_PROMOTED(VALUE obj)
Tests if the object is "promoted" – that is, whether the object experienced one or more GC marks.
Definition gc.h:726
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:216
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
Definition rclass.h:44
#define RCLASS(obj)
Convenient casting macro.
Definition rclass.h:38
C99 shim for <stdbool.h>
Ruby object's base components.
Definition rbasic.h:63
Definition class.h:80
struct rb_subclass_entry * module_subclass_entry
In the case that this is an ICLASS, module_subclasses points to the link in the module's subclasses l...
Definition class.h:58
CREF (Class REFerence)
Definition method.h:44
Definition class.h:36
Internal header for Class.
Definition class.h:29
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