Ruby 3.2.1p31 (2023-02-08 revision 31819e82c88c6f8ecfaeb162519bfa26a14b21fd)
class.h
1#ifndef INTERNAL_CLASS_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_CLASS_H
11#include "id_table.h" /* for struct rb_id_table */
12#include "internal/gc.h" /* for RB_OBJ_WRITE */
13#include "internal/serial.h" /* for rb_serial_t */
14#include "ruby/internal/stdbool.h" /* for bool */
15#include "ruby/intern.h" /* for rb_alloc_func_t */
16#include "ruby/ruby.h" /* for struct RBasic */
17#include "shape.h"
18
19#ifdef RCLASS_SUPER
20# undef RCLASS_SUPER
21#endif
22
24 VALUE klass;
25 struct rb_subclass_entry *next;
26 struct rb_subclass_entry *prev;
27};
28
30 uint32_t index;
31 rb_serial_t global_cvar_state;
32 VALUE class_value;
33};
34
36 VALUE *iv_ptr;
37 struct rb_id_table *const_tbl;
38 struct rb_id_table *callable_m_tbl;
39 struct rb_id_table *cc_tbl; /* ID -> [[ci, cc1], cc2, ...] */
40 struct rb_id_table *cvc_tbl;
41 size_t superclass_depth;
42 VALUE *superclasses;
43 struct rb_subclass_entry *subclasses;
44 struct rb_subclass_entry *subclass_entry;
51 const VALUE origin_;
52 const VALUE refined_class;
53 rb_alloc_func_t allocator;
54 const VALUE includer;
55 uint32_t max_iv_count;
56 uint32_t variation_count;
57#if !SHAPE_IN_BASIC_FLAGS
58 shape_id_t shape_id;
59#endif
60};
61
62struct RClass {
63 struct RBasic basic;
64 VALUE super;
65 struct rb_id_table *m_tbl;
66#if SIZE_POOL_COUNT == 1
67 struct rb_classext_struct *ptr;
68#endif
69};
70
73
74#if RCLASS_EXT_EMBEDDED
75# define RCLASS_EXT(c) ((rb_classext_t *)((char *)(c) + sizeof(struct RClass)))
76#else
77# define RCLASS_EXT(c) (RCLASS(c)->ptr)
78#endif
79#define RCLASS_CONST_TBL(c) (RCLASS_EXT(c)->const_tbl)
80#define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl)
81#define RCLASS_IVPTR(c) (RCLASS_EXT(c)->iv_ptr)
82#define RCLASS_CALLABLE_M_TBL(c) (RCLASS_EXT(c)->callable_m_tbl)
83#define RCLASS_CC_TBL(c) (RCLASS_EXT(c)->cc_tbl)
84#define RCLASS_CVC_TBL(c) (RCLASS_EXT(c)->cvc_tbl)
85#define RCLASS_ORIGIN(c) (RCLASS_EXT(c)->origin_)
86#define RCLASS_REFINED_CLASS(c) (RCLASS_EXT(c)->refined_class)
87#define RCLASS_INCLUDER(c) (RCLASS_EXT(c)->includer)
88#define RCLASS_SUBCLASS_ENTRY(c) (RCLASS_EXT(c)->subclass_entry)
89#define RCLASS_MODULE_SUBCLASS_ENTRY(c) (RCLASS_EXT(c)->module_subclass_entry)
90#define RCLASS_ALLOCATOR(c) (RCLASS_EXT(c)->allocator)
91#define RCLASS_SUBCLASSES(c) (RCLASS_EXT(c)->subclasses)
92#define RCLASS_SUPERCLASS_DEPTH(c) (RCLASS_EXT(c)->superclass_depth)
93#define RCLASS_SUPERCLASSES(c) (RCLASS_EXT(c)->superclasses)
94
95#define RICLASS_IS_ORIGIN FL_USER0
96#define RCLASS_CLONED FL_USER1
97#define RCLASS_SUPERCLASSES_INCLUDE_SELF FL_USER2
98#define RICLASS_ORIGIN_SHARED_MTBL FL_USER3
99
100/* class.c */
101void rb_class_subclass_add(VALUE super, VALUE klass);
102void rb_class_remove_from_super_subclasses(VALUE);
103void rb_class_update_superclasses(VALUE);
104size_t rb_class_superclasses_memsize(VALUE);
105void rb_class_remove_subclass_head(VALUE);
106int rb_singleton_class_internal_p(VALUE sklass);
108VALUE rb_class_s_alloc(VALUE klass);
109VALUE rb_module_s_alloc(VALUE klass);
110void rb_module_set_initialized(VALUE module);
111void rb_module_check_initializable(VALUE module);
112VALUE rb_make_metaclass(VALUE, VALUE);
113VALUE rb_include_class_new(VALUE, VALUE);
114void rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE);
115void rb_class_detach_subclasses(VALUE);
116void rb_class_detach_module_subclasses(VALUE);
117void rb_class_remove_from_module_subclasses(VALUE);
118VALUE rb_obj_methods(int argc, const VALUE *argv, VALUE obj);
119VALUE rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj);
120VALUE rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj);
121VALUE rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj);
122VALUE rb_class_undefined_instance_methods(VALUE mod);
123VALUE rb_special_singleton_class(VALUE);
124VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach);
126void rb_undef_methods_from(VALUE klass, VALUE super);
127
128static inline void RCLASS_SET_ORIGIN(VALUE klass, VALUE origin);
129static inline void RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass);
130static inline VALUE RCLASS_SUPER(VALUE klass);
131static inline VALUE RCLASS_SET_SUPER(VALUE klass, VALUE super);
132static inline void RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass);
133
134MJIT_SYMBOL_EXPORT_BEGIN
136VALUE rb_keyword_error_new(const char *, VALUE);
137MJIT_SYMBOL_EXPORT_END
138
139static inline void
140RCLASS_SET_ORIGIN(VALUE klass, VALUE origin)
141{
142 RB_OBJ_WRITE(klass, &RCLASS_ORIGIN(klass), origin);
143 if (klass != origin) FL_SET(origin, RICLASS_IS_ORIGIN);
144}
145
146static inline void
147RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass)
148{
149 FL_SET(iclass, RICLASS_ORIGIN_SHARED_MTBL);
150}
151
152static inline bool
153RICLASS_OWNS_M_TBL_P(VALUE iclass)
154{
155 return FL_TEST_RAW(iclass, RICLASS_IS_ORIGIN | RICLASS_ORIGIN_SHARED_MTBL) == RICLASS_IS_ORIGIN;
156}
157
158static inline void
159RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass)
160{
161 RB_OBJ_WRITE(iclass, &RCLASS_INCLUDER(iclass), klass);
162}
163
164static inline VALUE
165RCLASS_SUPER(VALUE klass)
166{
167 return RCLASS(klass)->super;
168}
169
170static inline VALUE
171RCLASS_SET_SUPER(VALUE klass, VALUE super)
172{
173 if (super) {
174 rb_class_remove_from_super_subclasses(klass);
175 rb_class_subclass_add(super, klass);
176 }
177 RB_OBJ_WRITE(klass, &RCLASS(klass)->super, super);
178 rb_class_update_superclasses(klass);
179 return super;
180}
181
182#endif /* INTERNAL_CLASS_H */
VALUE rb_class_boot(VALUE)
A utility function that wraps class_alloc.
Definition class.c:247
VALUE rb_class_inherited(VALUE, VALUE)
Calls Class::inherited.
Definition class.c:879
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:2187
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:140
#define FL_SET
Old name of RB_FL_SET.
Definition fl_type.h:137
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition rgengc.h:220
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:62
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:50
Definition class.h:29
Internal header for Class.
Definition class.h:23
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40