Ruby  3.1.4p223 (2023-03-30 revision HEAD)
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 
18 #ifdef RCLASS_SUPER
19 # undef RCLASS_SUPER
20 #endif
21 
23  VALUE klass;
24  struct rb_subclass_entry *next;
25  struct rb_subclass_entry *prev;
26 };
27 
29  uint32_t index;
30  rb_serial_t class_serial;
31  VALUE class_value;
32 };
33 
35  uint32_t index;
36  rb_serial_t global_cvar_state;
37  VALUE class_value;
38 };
39 
41  struct st_table *iv_index_tbl; // ID -> struct rb_iv_index_tbl_entry
42  struct st_table *iv_tbl;
43 #if SIZEOF_SERIAL_T == SIZEOF_VALUE /* otherwise m_tbl is in struct RClass */
44  struct rb_id_table *m_tbl;
45 #endif
46  struct rb_id_table *const_tbl;
47  struct rb_id_table *callable_m_tbl;
48  struct rb_id_table *cc_tbl; /* ID -> [[ci, cc1], cc2, ...] */
49  struct rb_id_table *cvc_tbl;
50  struct rb_subclass_entry *subclasses;
51  struct rb_subclass_entry *subclass_entry;
58 #if SIZEOF_SERIAL_T != SIZEOF_VALUE /* otherwise class_serial is in struct RClass */
59  rb_serial_t class_serial;
60 #endif
61  const VALUE origin_;
62  const VALUE refined_class;
63  rb_alloc_func_t allocator;
64  const VALUE includer;
65 };
66 
67 struct RClass {
68  struct RBasic basic;
69  VALUE super;
70 #if !USE_RVARGC
71  struct rb_classext_struct *ptr;
72 #endif
73 #if SIZEOF_SERIAL_T == SIZEOF_VALUE
74  /* Class serial is as wide as VALUE. Place it here. */
75  rb_serial_t class_serial;
76 #else
77  /* Class serial does not fit into struct RClass. Place m_tbl instead. */
78  struct rb_id_table *m_tbl;
79 #endif
80 };
81 
83 typedef struct rb_classext_struct rb_classext_t;
84 
85 #if USE_RVARGC
86 # define RCLASS_EXT(c) ((rb_classext_t *)((char *)c + sizeof(struct RClass)))
87 #else
88 # define RCLASS_EXT(c) (RCLASS(c)->ptr)
89 #endif
90 #define RCLASS_IV_TBL(c) (RCLASS_EXT(c)->iv_tbl)
91 #define RCLASS_CONST_TBL(c) (RCLASS_EXT(c)->const_tbl)
92 #if SIZEOF_SERIAL_T == SIZEOF_VALUE
93 # define RCLASS_M_TBL(c) (RCLASS_EXT(c)->m_tbl)
94 #else
95 # define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl)
96 #endif
97 #define RCLASS_CALLABLE_M_TBL(c) (RCLASS_EXT(c)->callable_m_tbl)
98 #define RCLASS_CC_TBL(c) (RCLASS_EXT(c)->cc_tbl)
99 #define RCLASS_CVC_TBL(c) (RCLASS_EXT(c)->cvc_tbl)
100 #define RCLASS_IV_INDEX_TBL(c) (RCLASS_EXT(c)->iv_index_tbl)
101 #define RCLASS_ORIGIN(c) (RCLASS_EXT(c)->origin_)
102 #define RCLASS_REFINED_CLASS(c) (RCLASS_EXT(c)->refined_class)
103 #if SIZEOF_SERIAL_T == SIZEOF_VALUE
104 # define RCLASS_SERIAL(c) (RCLASS(c)->class_serial)
105 #else
106 # define RCLASS_SERIAL(c) (RCLASS_EXT(c)->class_serial)
107 #endif
108 #define RCLASS_INCLUDER(c) (RCLASS_EXT(c)->includer)
109 #define RCLASS_SUBCLASS_ENTRY(c) (RCLASS_EXT(c)->subclass_entry)
110 #define RCLASS_MODULE_SUBCLASS_ENTRY(c) (RCLASS_EXT(c)->module_subclass_entry)
111 #define RCLASS_ALLOCATOR(c) (RCLASS_EXT(c)->allocator)
112 #define RCLASS_SUBCLASSES(c) (RCLASS_EXT(c)->subclasses)
113 
114 #define RICLASS_IS_ORIGIN FL_USER5
115 #define RCLASS_CLONED FL_USER6
116 #define RICLASS_ORIGIN_SHARED_MTBL FL_USER8
117 
118 /* class.c */
119 void rb_class_subclass_add(VALUE super, VALUE klass);
120 void rb_class_remove_from_super_subclasses(VALUE);
121 void rb_class_remove_subclass_head(VALUE);
122 int rb_singleton_class_internal_p(VALUE sklass);
124 VALUE rb_class_s_alloc(VALUE klass);
125 VALUE rb_module_s_alloc(VALUE klass);
126 void rb_module_set_initialized(VALUE module);
127 void rb_module_check_initializable(VALUE module);
128 VALUE rb_make_metaclass(VALUE, VALUE);
129 VALUE rb_include_class_new(VALUE, VALUE);
130 void rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE);
131 void rb_class_detach_subclasses(VALUE);
132 void rb_class_detach_module_subclasses(VALUE);
133 void rb_class_remove_from_module_subclasses(VALUE);
134 VALUE rb_obj_methods(int argc, const VALUE *argv, VALUE obj);
135 VALUE rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj);
136 VALUE rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj);
137 VALUE rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj);
138 VALUE rb_special_singleton_class(VALUE);
139 VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach);
141 void rb_undef_methods_from(VALUE klass, VALUE super);
142 
143 static inline void RCLASS_SET_ORIGIN(VALUE klass, VALUE origin);
144 static inline void RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass);
145 static inline VALUE RCLASS_SUPER(VALUE klass);
146 static inline VALUE RCLASS_SET_SUPER(VALUE klass, VALUE super);
147 static inline void RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass);
148 
149 MJIT_SYMBOL_EXPORT_BEGIN
151 VALUE rb_keyword_error_new(const char *, VALUE);
152 MJIT_SYMBOL_EXPORT_END
153 
154 static inline void
155 RCLASS_SET_ORIGIN(VALUE klass, VALUE origin)
156 {
157  RB_OBJ_WRITE(klass, &RCLASS_ORIGIN(klass), origin);
158  if (klass != origin) FL_SET(origin, RICLASS_IS_ORIGIN);
159 }
160 
161 static inline void
162 RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass)
163 {
164  FL_SET(iclass, RICLASS_ORIGIN_SHARED_MTBL);
165 }
166 
167 static inline bool
168 RICLASS_OWNS_M_TBL_P(VALUE iclass)
169 {
170  return FL_TEST_RAW(iclass, RICLASS_IS_ORIGIN | RICLASS_ORIGIN_SHARED_MTBL) == RICLASS_IS_ORIGIN;
171 }
172 
173 static inline void
174 RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass)
175 {
176  RB_OBJ_WRITE(iclass, &RCLASS_INCLUDER(iclass), klass);
177 }
178 
179 static inline VALUE
180 RCLASS_SUPER(VALUE klass)
181 {
182  return RCLASS(klass)->super;
183 }
184 
185 static inline VALUE
186 RCLASS_SET_SUPER(VALUE klass, VALUE super)
187 {
188  if (super) {
189  rb_class_remove_from_super_subclasses(klass);
190  rb_class_subclass_add(super, klass);
191  }
192  RB_OBJ_WRITE(klass, &RCLASS(klass)->super, super);
193  return super;
194 }
195 
196 #endif /* INTERNAL_CLASS_H */
VALUE rb_class_boot(VALUE)
A utility function that wraps class_alloc.
Definition: class.c:249
VALUE rb_class_inherited(VALUE, VALUE)
Calls Class::inherited.
Definition: class.c:828
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:2054
#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:46
#define RCLASS(obj)
Convenient casting macro.
Definition: rclass.h:40
C99 shim for <stdbool.h>
Ruby's object's, base components.
Definition: rbasic.h:64
Definition: class.h:67
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:57
Definition: class.h:34
Definition: class.h:28
Internal header for Class.
Definition: class.h:22
Definition: st.h:79
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40