Ruby: internal/class.h Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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