Ruby 3.3.2p78 (2024-05-30 revision e5a195edf62fe1bf7146a191da13fa1c4fecbd71)
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 size_pools[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 RICLASS_IS_ORIGIN FL_USER0
112#define RCLASS_SUPERCLASSES_INCLUDE_SELF FL_USER2
113#define RICLASS_ORIGIN_SHARED_MTBL FL_USER3
114
115static inline st_table *
116RCLASS_IV_HASH(VALUE obj)
117{
118 RUBY_ASSERT(RB_TYPE_P(obj, RUBY_T_CLASS) || RB_TYPE_P(obj, RUBY_T_MODULE));
119 RUBY_ASSERT(rb_shape_obj_too_complex(obj));
120 return (st_table *)RCLASS_IVPTR(obj);
121}
122
123static inline void
124RCLASS_SET_IV_HASH(VALUE obj, const st_table *tbl)
125{
126 RUBY_ASSERT(RB_TYPE_P(obj, RUBY_T_CLASS) || RB_TYPE_P(obj, RUBY_T_MODULE));
127 RUBY_ASSERT(rb_shape_obj_too_complex(obj));
128 RCLASS_IVPTR(obj) = (VALUE *)tbl;
129}
130
131static inline uint32_t
132RCLASS_IV_COUNT(VALUE obj)
133{
134 RUBY_ASSERT(RB_TYPE_P(obj, RUBY_T_CLASS) || RB_TYPE_P(obj, RUBY_T_MODULE));
135 if (rb_shape_obj_too_complex(obj)) {
136 uint32_t count;
137
138 // "Too complex" classes could have their IV hash mutated in
139 // parallel, so lets lock around getting the hash size.
140 RB_VM_LOCK_ENTER();
141 {
142 count = (uint32_t)rb_st_table_size(RCLASS_IV_HASH(obj));
143 }
144 RB_VM_LOCK_LEAVE();
145
146 return count;
147 }
148 else {
149 return rb_shape_get_shape_by_id(RCLASS_SHAPE_ID(obj))->next_iv_index;
150 }
151}
152
153static inline void
154RCLASS_SET_M_TBL(VALUE klass, struct rb_id_table *table)
155{
157 RCLASS_M_TBL(klass) = table;
158}
159
160/* class.c */
161void rb_class_subclass_add(VALUE super, VALUE klass);
162void rb_class_remove_from_super_subclasses(VALUE);
163void rb_class_update_superclasses(VALUE);
164size_t rb_class_superclasses_memsize(VALUE);
165void rb_class_remove_subclass_head(VALUE);
166int rb_singleton_class_internal_p(VALUE sklass);
168VALUE rb_class_s_alloc(VALUE klass);
169VALUE rb_module_s_alloc(VALUE klass);
170void rb_module_set_initialized(VALUE module);
171void rb_module_check_initializable(VALUE module);
172VALUE rb_make_metaclass(VALUE, VALUE);
173VALUE rb_include_class_new(VALUE, VALUE);
174void rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE);
175void rb_class_detach_subclasses(VALUE);
176void rb_class_detach_module_subclasses(VALUE);
177void rb_class_remove_from_module_subclasses(VALUE);
178VALUE rb_define_class_id_under_no_pin(VALUE outer, ID id, VALUE super);
179VALUE rb_obj_methods(int argc, const VALUE *argv, VALUE obj);
180VALUE rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj);
181VALUE rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj);
182VALUE rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj);
183VALUE rb_class_undefined_instance_methods(VALUE mod);
184VALUE rb_special_singleton_class(VALUE);
185VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach);
187void rb_undef_methods_from(VALUE klass, VALUE super);
188
189static inline void RCLASS_SET_ORIGIN(VALUE klass, VALUE origin);
190static inline void RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass);
191static inline VALUE RCLASS_SUPER(VALUE klass);
192static inline VALUE RCLASS_SET_SUPER(VALUE klass, VALUE super);
193static inline void RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass);
194
196VALUE rb_keyword_error_new(const char *, VALUE);
197
198static inline rb_alloc_func_t
199RCLASS_ALLOCATOR(VALUE klass)
200{
201 if (FL_TEST_RAW(klass, FL_SINGLETON)) {
202 return 0;
203 }
204 return RCLASS_EXT(klass)->as.class.allocator;
205}
206
207static inline void
208RCLASS_SET_ALLOCATOR(VALUE klass, rb_alloc_func_t allocator)
209{
210 assert(!FL_TEST(klass, FL_SINGLETON));
211 RCLASS_EXT(klass)->as.class.allocator = allocator;
212}
213
214static inline void
215RCLASS_SET_ORIGIN(VALUE klass, VALUE origin)
216{
217 RB_OBJ_WRITE(klass, &RCLASS_ORIGIN(klass), origin);
218 if (klass != origin) FL_SET(origin, RICLASS_IS_ORIGIN);
219}
220
221static inline void
222RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass)
223{
224 FL_SET(iclass, RICLASS_ORIGIN_SHARED_MTBL);
225}
226
227static inline bool
228RICLASS_OWNS_M_TBL_P(VALUE iclass)
229{
230 return FL_TEST_RAW(iclass, RICLASS_IS_ORIGIN | RICLASS_ORIGIN_SHARED_MTBL) == RICLASS_IS_ORIGIN;
231}
232
233static inline void
234RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass)
235{
236 RB_OBJ_WRITE(iclass, &RCLASS_INCLUDER(iclass), klass);
237}
238
239static inline VALUE
240RCLASS_SUPER(VALUE klass)
241{
242 return RCLASS(klass)->super;
243}
244
245static inline VALUE
246RCLASS_SET_SUPER(VALUE klass, VALUE super)
247{
248 if (super) {
249 rb_class_remove_from_super_subclasses(klass);
250 rb_class_subclass_add(super, klass);
251 }
252 RB_OBJ_WRITE(klass, &RCLASS(klass)->super, super);
253 rb_class_update_superclasses(klass);
254 return super;
255}
256
257static inline void
258RCLASS_SET_CLASSPATH(VALUE klass, VALUE classpath, bool permanent)
259{
260 assert(BUILTIN_TYPE(klass) == T_CLASS || BUILTIN_TYPE(klass) == T_MODULE);
261 assert(classpath == 0 || BUILTIN_TYPE(classpath) == T_STRING);
262
263 RB_OBJ_WRITE(klass, &(RCLASS_EXT(klass)->classpath), classpath);
264 RCLASS_EXT(klass)->permanent_classpath = permanent;
265}
266
267static inline VALUE
268RCLASS_SET_ATTACHED_OBJECT(VALUE klass, VALUE attached_object)
269{
270 assert(BUILTIN_TYPE(klass) == T_CLASS);
271 assert(FL_TEST_RAW(klass, FL_SINGLETON));
272
273 RB_OBJ_WRITE(klass, &RCLASS_EXT(klass)->as.singleton_class.attached_object, attached_object);
274 return attached_object;
275}
276
277#endif /* INTERNAL_CLASS_H */
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:177
VALUE rb_class_boot(VALUE)
A utility function that wraps class_alloc.
Definition class.c:272
VALUE rb_class_inherited(VALUE, VALUE)
Calls Class::inherited.
Definition class.c:961
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:2274
#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 FL_TEST
Old name of RB_FL_TEST.
Definition fl_type.h:131
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:619
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:742
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's object's, base components.
Definition rbasic.h:64
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
@ RUBY_T_MODULE
Definition value_type.h:117
@ RUBY_T_CLASS
Definition value_type.h:116