Ruby: internal/class.h Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

class.h
Go to the documentation of this file.
1#ifndef INTERNAL_CLASS_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_CLASS_H
12#include "id_table.h" /* for struct rb_id_table */
13#include "internal/gc.h" /* for RB_OBJ_WRITE */
14#include "internal/serial.h" /* for rb_serial_t */
15#include "ruby/internal/stdbool.h" /* for bool */
16#include "ruby/intern.h" /* for rb_alloc_func_t */
17#include "ruby/ruby.h" /* for struct RBasic */
18
19#ifdef RCLASS_SUPER
20# undef RCLASS_SUPER
21#endif
22
23 struct rb_subclass_entry {
24 VALUE klass;
25 struct rb_subclass_entry *next;
26};
27
28 struct rb_iv_index_tbl_entry {
29 uint32_t index;
30 rb_serial_t class_serial;
31 VALUE class_value;
32};
33
34 struct rb_classext_struct {
35 struct st_table *iv_index_tbl; // ID -> struct rb_iv_index_tbl_entry
36 struct st_table *iv_tbl;
37#if SIZEOF_SERIAL_T == SIZEOF_VALUE /* otherwise m_tbl is in struct RClass */
38 struct rb_id_table *m_tbl;
39#endif
40 struct rb_id_table *const_tbl;
41 struct rb_id_table *callable_m_tbl;
42 struct rb_id_table *cc_tbl; /* ID -> [[ci, cc1], cc2, ...] */
43 struct rb_subclass_entry *subclasses;
44 struct rb_subclass_entry **parent_subclasses;
50 struct rb_subclass_entry **module_subclasses;
51#if SIZEOF_SERIAL_T != SIZEOF_VALUE /* otherwise class_serial is in struct RClass */
52 rb_serial_t class_serial;
53#endif
54 const VALUE origin_;
55 const VALUE refined_class;
56 rb_alloc_func_t allocator;
57 const VALUE includer;
58};
59
60 struct RClass {
61 struct RBasic basic;
62 VALUE super;
63 struct rb_classext_struct *ptr;
64#if SIZEOF_SERIAL_T == SIZEOF_VALUE
65 /* Class serial is as wide as VALUE. Place it here. */
66 rb_serial_t class_serial;
67#else
68 /* Class serial does not fit into struct RClass. Place m_tbl instead. */
69 struct rb_id_table *m_tbl;
70#endif
71};
72
73 typedef struct rb_subclass_entry rb_subclass_entry_t;
74 typedef struct rb_classext_struct rb_classext_t;
75
76#define RCLASS_EXT(c) (RCLASS(c)->ptr)
77#define RCLASS_IV_TBL(c) (RCLASS_EXT(c)->iv_tbl)
78#define RCLASS_CONST_TBL(c) (RCLASS_EXT(c)->const_tbl)
79#if SIZEOF_SERIAL_T == SIZEOF_VALUE
80# define RCLASS_M_TBL(c) (RCLASS_EXT(c)->m_tbl)
81#else
82# define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl)
83#endif
84#define RCLASS_CALLABLE_M_TBL(c) (RCLASS_EXT(c)->callable_m_tbl)
85#define RCLASS_CC_TBL(c) (RCLASS_EXT(c)->cc_tbl)
86#define RCLASS_IV_INDEX_TBL(c) (RCLASS_EXT(c)->iv_index_tbl)
87#define RCLASS_ORIGIN(c) (RCLASS_EXT(c)->origin_)
88#define RCLASS_REFINED_CLASS(c) (RCLASS_EXT(c)->refined_class)
89#if SIZEOF_SERIAL_T == SIZEOF_VALUE
90# define RCLASS_SERIAL(c) (RCLASS(c)->class_serial)
91#else
92# define RCLASS_SERIAL(c) (RCLASS_EXT(c)->class_serial)
93#endif
94#define RCLASS_INCLUDER(c) (RCLASS_EXT(c)->includer)
95
96#define RICLASS_IS_ORIGIN FL_USER5
97#define RCLASS_CLONED FL_USER6
98#define RICLASS_ORIGIN_SHARED_MTBL FL_USER8
99
100/* class.c */
101void rb_class_subclass_add(VALUE super, VALUE klass);
102void rb_class_remove_from_super_subclasses(VALUE);
103int rb_singleton_class_internal_p(VALUE sklass);
104 VALUE rb_class_boot(VALUE);
105 VALUE rb_make_metaclass(VALUE, VALUE);
106 VALUE rb_include_class_new(VALUE, VALUE);
107void rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE);
108void rb_class_detach_subclasses(VALUE);
109void rb_class_detach_module_subclasses(VALUE);
110void rb_class_remove_from_module_subclasses(VALUE);
111 VALUE rb_obj_methods(int argc, const VALUE *argv, VALUE obj);
112 VALUE rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj);
113 VALUE rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj);
114 VALUE rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj);
115 VALUE rb_special_singleton_class(VALUE);
116 VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach);
117 VALUE rb_singleton_class_get(VALUE obj);
118int rb_class_has_methods(VALUE c);
119void rb_undef_methods_from(VALUE klass, VALUE super);
120
121static inline void RCLASS_SET_ORIGIN(VALUE klass, VALUE origin);
122static inline void RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass);
123static inline VALUE RCLASS_SUPER(VALUE klass);
124static inline VALUE RCLASS_SET_SUPER(VALUE klass, VALUE super);
125static inline void RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass);
126
127 MJIT_SYMBOL_EXPORT_BEGIN
128 VALUE rb_class_inherited(VALUE, VALUE);
129 VALUE rb_keyword_error_new(const char *, VALUE);
130 MJIT_SYMBOL_EXPORT_END
131
132static inline void
133RCLASS_SET_ORIGIN(VALUE klass, VALUE origin)
134{
135 RB_OBJ_WRITE(klass, &RCLASS_ORIGIN(klass), origin);
136 if (klass != origin) FL_SET(origin, RICLASS_IS_ORIGIN);
137}
138
139static inline void
140RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass)
141{
142 FL_SET(iclass, RICLASS_ORIGIN_SHARED_MTBL);
143}
144
145static inline bool
146RICLASS_OWNS_M_TBL_P(VALUE iclass)
147{
148 return FL_TEST_RAW(iclass, RICLASS_IS_ORIGIN | RICLASS_ORIGIN_SHARED_MTBL) == RICLASS_IS_ORIGIN;
149}
150
151static inline void
152RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass)
153{
154 RB_OBJ_WRITE(iclass, &RCLASS_INCLUDER(iclass), klass);
155}
156
157static inline VALUE
158 RCLASS_SUPER(VALUE klass)
159{
160 return RCLASS(klass)->super;
161}
162
163static inline VALUE
164RCLASS_SET_SUPER(VALUE klass, VALUE super)
165{
166 if (super) {
167 rb_class_remove_from_super_subclasses(klass);
168 rb_class_subclass_add(super, klass);
169 }
170 RB_OBJ_WRITE(klass, &RCLASS(klass)->super, super);
171 return super;
172}
173
174#endif /* INTERNAL_CLASS_H */
MJIT_SYMBOL_EXPORT_END
#define MJIT_SYMBOL_EXPORT_END
Definition: dllexport.h:63
MJIT_SYMBOL_EXPORT_BEGIN
#define MJIT_SYMBOL_EXPORT_BEGIN
Definition: dllexport.h:62
rb_class_foreach_subclass
void rb_class_foreach_subclass(VALUE klass, void(*f)(VALUE, VALUE), VALUE)
Definition: class.c:125
rb_obj_private_methods
VALUE rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1597
rb_class_subclass_add
void rb_class_subclass_add(VALUE super, VALUE klass)
Definition: class.c:48
rb_obj_methods
VALUE rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1563
rb_class_detach_subclasses
void rb_class_detach_subclasses(VALUE)
Definition: class.c:145
rb_singleton_class_clone_and_attach
VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
Definition: class.c:466
rb_include_class_new
VALUE rb_include_class_new(VALUE, VALUE)
Definition: class.c:923
rb_obj_protected_methods
VALUE rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1582
rb_singleton_class_internal_p
int rb_singleton_class_internal_p(VALUE sklass)
Definition: class.c:554
rb_class_boot
VALUE rb_class_boot(VALUE)
A utility function that wraps class_alloc.
Definition: class.c:213
rb_class_detach_module_subclasses
void rb_class_detach_module_subclasses(VALUE)
Definition: class.c:157
rb_special_singleton_class
VALUE rb_special_singleton_class(VALUE)
Definition: class.c:1819
rb_class_inherited
VALUE rb_class_inherited(VALUE, VALUE)
Calls Class::inherited.
Definition: class.c:722
rb_class_remove_from_module_subclasses
void rb_class_remove_from_module_subclasses(VALUE)
Definition: class.c:106
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:1893
rb_make_metaclass
VALUE rb_make_metaclass(VALUE, VALUE)
Definition: class.c:679
rb_obj_public_methods
VALUE rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
Definition: class.c:1612
rb_class_remove_from_super_subclasses
void rb_class_remove_from_super_subclasses(VALUE)
Definition: class.c:88
rb_class_has_methods
int rb_class_has_methods(VALUE c)
Definition: class.c:2321
rb_keyword_error_new
VALUE rb_keyword_error_new(const char *, VALUE)
Definition: class.c:2018
rb_undef_methods_from
void rb_undef_methods_from(VALUE klass, VALUE super)
Definition: class.c:1791
FL_TEST_RAW
#define FL_TEST_RAW
Definition: fl_type.h:131
FL_SET
#define FL_SET
Definition: fl_type.h:128
id_table.h
rb_alloc_func_t
VALUE(* rb_alloc_func_t)(VALUE)
Definition: vm.h:50
intern.h
RCLASS_INCLUDER
#define RCLASS_INCLUDER(c)
Definition: class.h:94
RCLASS_ORIGIN
#define RCLASS_ORIGIN(c)
Definition: class.h:87
RICLASS_ORIGIN_SHARED_MTBL
#define RICLASS_ORIGIN_SHARED_MTBL
Definition: class.h:98
RICLASS_IS_ORIGIN
#define RICLASS_IS_ORIGIN
Definition: class.h:96
origin
voidpf uLong int origin
Definition: ioapi.h:144
RCLASS_SUPER
#define RCLASS_SUPER
Definition: rclass.h:33
RCLASS
#define RCLASS(obj)
Definition: rclass.h:31
RB_OBJ_WRITE
#define RB_OBJ_WRITE(a, slot, b)
WB for new reference from ‘a’ to ‘b’.
Definition: rgengc.h:107
ruby.h
argc
int argc
Definition: ruby.c:240
argv
char ** argv
Definition: ruby.c:241
serial.h
Internal header for rb_serial_t.
rb_serial_t
unsigned LONG_LONG rb_serial_t
Definition: serial.h:19
uint32_t
unsigned int uint32_t
Definition: sha2.h:101
f
#define f
stdbool.h
C99 shim for <stdbool.h>
RBasic
Definition: rbasic.h:47
RClass
Definition: class.h:60
RClass::super
VALUE super
Definition: class.h:62
RClass::ptr
struct rb_classext_struct * ptr
Definition: class.h:63
RClass::class_serial
rb_serial_t class_serial
Definition: class.h:66
RClass::basic
struct RBasic basic
Definition: class.h:61
rb_classext_struct
Definition: class.h:34
rb_classext_struct::parent_subclasses
struct rb_subclass_entry ** parent_subclasses
Definition: class.h:44
rb_classext_struct::iv_index_tbl
struct st_table * iv_index_tbl
Definition: class.h:35
rb_classext_struct::callable_m_tbl
struct rb_id_table * callable_m_tbl
Definition: class.h:41
rb_classext_struct::module_subclasses
struct rb_subclass_entry ** module_subclasses
In the case that this is an ICLASS, module_subclasses points to the link in the module's subclasses l...
Definition: class.h:50
rb_classext_struct::subclasses
struct rb_subclass_entry * subclasses
Definition: class.h:43
rb_classext_struct::const_tbl
struct rb_id_table * const_tbl
Definition: class.h:40
rb_classext_struct::cc_tbl
struct rb_id_table * cc_tbl
Definition: class.h:42
rb_classext_struct::origin_
const VALUE origin_
Definition: class.h:54
rb_classext_struct::includer
const VALUE includer
Definition: class.h:57
rb_classext_struct::iv_tbl
struct st_table * iv_tbl
Definition: class.h:36
rb_classext_struct::allocator
rb_alloc_func_t allocator
Definition: class.h:56
rb_classext_struct::m_tbl
struct rb_id_table * m_tbl
Definition: class.h:38
rb_classext_struct::refined_class
const VALUE refined_class
Definition: class.h:55
rb_id_table
Definition: id_table.c:41
rb_iv_index_tbl_entry
Definition: class.h:28
rb_iv_index_tbl_entry::index
uint32_t index
Definition: class.h:29
rb_iv_index_tbl_entry::class_value
VALUE class_value
Definition: class.h:31
rb_iv_index_tbl_entry::class_serial
rb_serial_t class_serial
Definition: class.h:30
rb_subclass_entry
Definition: class.h:23
rb_subclass_entry::klass
VALUE klass
Definition: class.h:24
rb_subclass_entry::next
struct rb_subclass_entry * next
Definition: class.h:25
st_table
Definition: st.h:79
VALUE
unsigned long VALUE
Definition: value.h:38