Ruby: method.h Source File

Ruby 4.0.6p0 (2026-07-14 revision 03b6d3f8898a28604fe6cb00eae3226b821168f4)

method.h
1#ifndef RUBY_METHOD_H
2#define RUBY_METHOD_H 1
3/**********************************************************************
4
5 method.h -
6
7 $Author$
8 created at: Wed Jul 15 20:02:33 2009
9
10 Copyright (C) 2009 Koichi Sasada
11
12**********************************************************************/
13
14#include "internal.h"
15#include "internal/imemo.h"
16#include "internal/compilers.h"
17#include "internal/static_assert.h"
18#include "ruby/atomic.h"
19
20#ifndef END_OF_ENUMERATION
21# if defined(__GNUC__) &&! defined(__STRICT_ANSI__)
22# define END_OF_ENUMERATION(key)
23# else
24# define END_OF_ENUMERATION(key) END_OF_##key##_PLACEHOLDER = 0
25# endif
26#endif
27
28/* cref */
29
30typedef enum {
31 METHOD_VISI_UNDEF = 0x00,
32 METHOD_VISI_PUBLIC = 0x01,
33 METHOD_VISI_PRIVATE = 0x02,
34 METHOD_VISI_PROTECTED = 0x03,
35
36 METHOD_VISI_MASK = 0x03
37} rb_method_visibility_t;
38
39 typedef struct rb_scope_visi_struct {
40 BITFIELD(rb_method_visibility_t, method_visi, 3);
41 unsigned int module_func : 1;
42} rb_scope_visibility_t;
43
45 typedef struct rb_cref_struct {
46 VALUE flags;
47 VALUE refinements;
48 VALUE klass_or_self;
49 struct rb_cref_struct * next;
50 const rb_scope_visibility_t scope_visi;
51} rb_cref_t;
52
53/* method data type */
54
55 typedef struct rb_method_entry_struct {
56 VALUE flags;
57 VALUE defined_class;
58 struct rb_method_definition_struct * const def;
59 ID called_id;
60 VALUE owner;
61} rb_method_entry_t;
62
63 typedef struct rb_callable_method_entry_struct { /* same fields with rb_method_entry_t */
64 VALUE flags;
65 const VALUE defined_class;
66 struct rb_method_definition_struct * const def;
67 ID called_id;
68 const VALUE owner;
69} rb_callable_method_entry_t;
70
71#define METHOD_ENTRY_VISI(me) (rb_method_visibility_t)(((me)->flags & (IMEMO_FL_USER0 | IMEMO_FL_USER1)) >> (IMEMO_FL_USHIFT+0))
72#define METHOD_ENTRY_BASIC(me) (int) (((me)->flags & (IMEMO_FL_USER2 )) >> (IMEMO_FL_USHIFT+2))
73#define METHOD_ENTRY_COMPLEMENTED(me) ((me)->flags & IMEMO_FL_USER3)
74#define METHOD_ENTRY_COMPLEMENTED_SET(me) ((me)->flags |= IMEMO_FL_USER3)
75#define METHOD_ENTRY_CACHED(me) ((me)->flags & IMEMO_FL_USER4)
76#define METHOD_ENTRY_INVALIDATED(me) ((me)->flags & IMEMO_FL_USER5)
77#define METHOD_ENTRY_INVALIDATED_SET(me) ((me)->flags |= IMEMO_FL_USER5)
78
79static inline void
80METHOD_ENTRY_CACHED_SET(rb_callable_method_entry_t *me)
81{
82 if (!METHOD_ENTRY_CACHED(me)) {
83 me->flags |= IMEMO_FL_USER4;
84 }
85}
86
87static inline void
88METHOD_ENTRY_VISI_SET(rb_method_entry_t *me, rb_method_visibility_t visi)
89{
90 VM_ASSERT((int)visi >= 0 && visi <= 3);
91 me->flags = (me->flags & ~(IMEMO_FL_USER0 | IMEMO_FL_USER1)) | (visi << (IMEMO_FL_USHIFT+0));
92}
93static inline void
94METHOD_ENTRY_BASIC_SET(rb_method_entry_t *me, unsigned int basic)
95{
96 VM_ASSERT(basic <= 1);
97 me->flags = (me->flags & ~(IMEMO_FL_USER2 )) | (basic << (IMEMO_FL_USHIFT+2));
98}
99static inline void
100METHOD_ENTRY_FLAGS_SET(rb_method_entry_t *me, rb_method_visibility_t visi, unsigned int basic)
101{
102 VM_ASSERT((int)visi >= 0 && visi <= 3);
103 VM_ASSERT(basic <= 1);
104 me->flags =
105 (me->flags & ~(IMEMO_FL_USER0|IMEMO_FL_USER1|IMEMO_FL_USER2)) |
106 ((visi << (IMEMO_FL_USHIFT+0)) | (basic << (IMEMO_FL_USHIFT+2)));
107}
108static inline void
109METHOD_ENTRY_FLAGS_COPY(rb_method_entry_t *dst, const rb_method_entry_t *src)
110{
111 dst->flags =
112 (dst->flags & ~(IMEMO_FL_USER0|IMEMO_FL_USER1|IMEMO_FL_USER2
113 |IMEMO_FL_USER3)) |
114 (src->flags & (IMEMO_FL_USER0|IMEMO_FL_USER1|IMEMO_FL_USER2|IMEMO_FL_USER3));
115}
116
117typedef enum {
118 VM_METHOD_TYPE_ISEQ,
119 VM_METHOD_TYPE_CFUNC,
120 VM_METHOD_TYPE_ATTRSET,
121 VM_METHOD_TYPE_IVAR,
122 VM_METHOD_TYPE_BMETHOD,
123 VM_METHOD_TYPE_ZSUPER,
124 VM_METHOD_TYPE_ALIAS,
125 VM_METHOD_TYPE_UNDEF,
126 VM_METHOD_TYPE_NOTIMPLEMENTED,
127 VM_METHOD_TYPE_OPTIMIZED,
128 VM_METHOD_TYPE_MISSING,
129 VM_METHOD_TYPE_REFINED,
130
131 END_OF_ENUMERATION(VM_METHOD_TYPE)
132} rb_method_type_t;
133#define VM_METHOD_TYPE_MINIMUM_BITS 4
134STATIC_ASSERT(VM_METHOD_TYPE_MINIMUM_BITS,
135 VM_METHOD_TYPE_REFINED <= (1<<VM_METHOD_TYPE_MINIMUM_BITS));
136
137#ifndef rb_iseq_t
138typedef struct rb_iseq_struct rb_iseq_t;
139#define rb_iseq_t rb_iseq_t
140#endif
141
142 typedef struct rb_method_iseq_struct {
143 const rb_iseq_t * iseqptr;
144 rb_cref_t * cref;
145} rb_method_iseq_t; /* check rb_add_method_iseq() when modify the fields */
146
147typedef VALUE (*rb_cfunc_t)(ANYARGS);
148 typedef struct rb_method_cfunc_struct {
149 rb_cfunc_t func;
150 VALUE (*invoker)(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS));
151 int argc;
152} rb_method_cfunc_t;
153
154 typedef struct rb_method_attr_struct {
155 ID id;
156 VALUE location; /* should be marked */
157} rb_method_attr_t;
158
159 typedef struct rb_method_alias_struct {
160 struct rb_method_entry_struct * original_me; /* original_me->klass is original owner */
161} rb_method_alias_t;
162
163 typedef struct rb_method_refined_struct {
164 struct rb_method_entry_struct * orig_me;
165} rb_method_refined_t;
166
167 typedef struct rb_method_bmethod_struct {
168 VALUE proc; /* should be marked */
169 rb_serial_t defined_ractor_id;
170 unsigned int local_hooks_cnt;
171} rb_method_bmethod_t;
172
173enum method_optimized_type {
174 OPTIMIZED_METHOD_TYPE_SEND,
175 OPTIMIZED_METHOD_TYPE_CALL,
176 OPTIMIZED_METHOD_TYPE_BLOCK_CALL,
177 OPTIMIZED_METHOD_TYPE_STRUCT_AREF,
178 OPTIMIZED_METHOD_TYPE_STRUCT_ASET,
179 OPTIMIZED_METHOD_TYPE__MAX
180};
181
182 typedef struct rb_method_optimized {
183 enum method_optimized_type type;
184 unsigned int index;
185} rb_method_optimized_t;
186
187 struct rb_method_definition_struct {
188 BITFIELD(rb_method_type_t, type, VM_METHOD_TYPE_MINIMUM_BITS);
189 unsigned int iseq_overload: 1;
190 unsigned int no_redef_warning: 1;
191 unsigned int aliased : 1;
192
193 rb_atomic_t reference_count;
194
195 union {
196 rb_method_iseq_t iseq;
197 rb_method_cfunc_t cfunc;
198 rb_method_attr_t attr;
199 rb_method_alias_t alias;
200 rb_method_refined_t refined;
201 rb_method_bmethod_t bmethod;
202 rb_method_optimized_t optimized;
203 } body;
204
205 ID original_id;
206 uintptr_t method_serial;
207 const rb_box_t *box;
208};
209
210struct rb_id_table;
211struct rb_ractor_struct;
212struct rb_hook_list_struct;
213
214typedef struct rb_method_definition_struct rb_method_definition_t;
215STATIC_ASSERT(sizeof_method_def, offsetof(rb_method_definition_t, body) <= 8);
216
217#define UNDEFINED_METHOD_ENTRY_P(me) (!(me) || !(me)->def || (me)->def->type == VM_METHOD_TYPE_UNDEF)
218#define UNDEFINED_REFINED_METHOD_P(def) \
219 ((def)->type == VM_METHOD_TYPE_REFINED && \
220 UNDEFINED_METHOD_ENTRY_P((def)->body.refined.orig_me))
221
222void rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *option, rb_method_visibility_t visi);
223void rb_add_method_cfunc(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_method_visibility_t visi);
224void rb_add_method_iseq(VALUE klass, ID mid, const rb_iseq_t *iseq, rb_cref_t *cref, rb_method_visibility_t visi);
225void rb_add_method_optimized(VALUE klass, ID mid, enum method_optimized_type, unsigned int index, rb_method_visibility_t visi);
226void rb_add_refined_method_entry(VALUE refined_class, ID mid);
227
228rb_method_entry_t *rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *, rb_method_visibility_t noex);
229rb_method_entry_t *rb_method_entry_create(ID called_id, VALUE klass, rb_method_visibility_t visi, rb_method_definition_t *def);
230
231const rb_method_entry_t *rb_method_entry_at(VALUE obj, ID id);
232
233const rb_method_entry_t *rb_method_entry(VALUE klass, ID id);
234const rb_method_entry_t *rb_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class);
235const rb_method_entry_t *rb_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class);
236const rb_method_entry_t *rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
237RUBY_SYMBOL_EXPORT_BEGIN
238const rb_method_entry_t *rb_resolve_me_location(const rb_method_entry_t *, VALUE[5]);
239RUBY_SYMBOL_EXPORT_END
240
241const rb_callable_method_entry_t *rb_callable_method_entry(VALUE klass, ID id);
242const rb_callable_method_entry_t *rb_callable_method_entry_or_negative(VALUE klass, ID id);
243const rb_callable_method_entry_t *rb_callable_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class);
244const rb_callable_method_entry_t *rb_callable_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class);
245
246int rb_method_entry_arity(const rb_method_entry_t *me);
247int rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2);
248st_index_t rb_hash_method_entry(st_index_t hash, const rb_method_entry_t *me);
249
250 VALUE rb_method_entry_location(const rb_method_entry_t *me);
251
252void rb_free_method_entry_vm_weak_references(const rb_method_entry_t *me);
253void rb_free_method_entry(const rb_method_entry_t *me);
254
255const rb_method_entry_t *rb_method_entry_clone(const rb_method_entry_t *me);
256const rb_callable_method_entry_t *rb_method_entry_complement_defined_class(const rb_method_entry_t *src_me, ID called_id, VALUE defined_class);
257void rb_method_entry_copy(rb_method_entry_t *dst, const rb_method_entry_t *src);
258
259void rb_method_table_insert(VALUE klass, struct rb_id_table *table, ID method_id, const rb_method_entry_t *me);
260void rb_method_table_insert0(VALUE klass, struct rb_id_table *table, ID method_id, const rb_method_entry_t *me, bool iclass_shared_mtbl);
261
262void rb_scope_visibility_set(rb_method_visibility_t);
263
264 VALUE rb_unnamed_parameters(int arity);
265
266void rb_vm_insert_cc_refinement(const struct rb_callcache *cc);
267void rb_vm_delete_cc_refinement(const struct rb_callcache *cc);
268
269void rb_clear_method_cache(VALUE klass_or_module, ID mid);
270void rb_clear_all_refinement_method_cache(void);
271void rb_invalidate_method_caches(struct rb_id_table *cm_tbl, VALUE cc_tbl);
272struct rb_hook_list_struct *rb_method_def_local_hooks(rb_method_definition_t *def, struct rb_ractor_struct *cr, bool create);
273void rb_method_definition_addref(rb_method_definition_t *def);
274void rb_method_definition_release(rb_method_definition_t *def);
275
276#endif /* RUBY_METHOD_H */
atomic.h
Atomic operations.
rb_atomic_t
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
ANYARGS
#define ANYARGS
Functions declared using this macro take arbitrary arguments, including void.
Definition stdarg.h:64
rb_callable_method_entry_struct
Definition method.h:63
rb_callcache
Definition vm_callinfo.h:292
rb_cref_struct
CREF (Class REFerence).
Definition method.h:45
rb_hook_list_struct
Definition vm_core.h:660
rb_id_table
Definition id_table.c:41
rb_iseq_struct
Definition vm_core.h:572
rb_method_alias_struct
Definition method.h:159
rb_method_attr_struct
Definition method.h:154
rb_method_bmethod_struct
Definition method.h:167
rb_method_cfunc_struct
Definition method.h:148
rb_method_definition_struct
Definition method.h:187
rb_method_entry_struct
Definition method.h:55
rb_method_iseq_struct
Definition method.h:142
rb_method_iseq_struct::cref
rb_cref_t * cref
class reference, should be marked
Definition method.h:144
rb_method_iseq_struct::iseqptr
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition method.h:143
rb_method_optimized
Definition method.h:182
rb_method_refined_struct
Definition method.h:163
rb_ractor_struct
Definition ractor_core.h:68
rb_scope_visi_struct
Definition method.h:39
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