Ruby 3.5.0dev (2025-04-03 revision 1dddc6c78b5f6dc6ae18ee04ebe44abfce3b0433)
method.h (1dddc6c78b5f6dc6ae18ee04ebe44abfce3b0433)
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
39typedef struct rb_scope_visi_struct {
40 BITFIELD(rb_method_visibility_t, method_visi, 3);
41 unsigned int module_func : 1;
43
45typedef 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
55typedef 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;
62
63typedef 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;
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_CACHED_SET(me) ((me)->flags |= IMEMO_FL_USER4)
77#define METHOD_ENTRY_INVALIDATED(me) ((me)->flags & IMEMO_FL_USER5)
78#define METHOD_ENTRY_INVALIDATED_SET(me) ((me)->flags |= IMEMO_FL_USER5)
79
80static inline void
81METHOD_ENTRY_VISI_SET(rb_method_entry_t *me, rb_method_visibility_t visi)
82{
83 VM_ASSERT((int)visi >= 0 && visi <= 3);
84 me->flags = (me->flags & ~(IMEMO_FL_USER0 | IMEMO_FL_USER1)) | (visi << (IMEMO_FL_USHIFT+0));
85}
86static inline void
87METHOD_ENTRY_BASIC_SET(rb_method_entry_t *me, unsigned int basic)
88{
89 VM_ASSERT(basic <= 1);
90 me->flags = (me->flags & ~(IMEMO_FL_USER2 )) | (basic << (IMEMO_FL_USHIFT+2));
91}
92static inline void
93METHOD_ENTRY_FLAGS_SET(rb_method_entry_t *me, rb_method_visibility_t visi, unsigned int basic)
94{
95 VM_ASSERT((int)visi >= 0 && visi <= 3);
96 VM_ASSERT(basic <= 1);
97 me->flags =
98 (me->flags & ~(IMEMO_FL_USER0|IMEMO_FL_USER1|IMEMO_FL_USER2)) |
99 ((visi << (IMEMO_FL_USHIFT+0)) | (basic << (IMEMO_FL_USHIFT+2)));
100}
101static inline void
102METHOD_ENTRY_FLAGS_COPY(rb_method_entry_t *dst, const rb_method_entry_t *src)
103{
104 dst->flags =
105 (dst->flags & ~(IMEMO_FL_USER0|IMEMO_FL_USER1|IMEMO_FL_USER2
106 |IMEMO_FL_USER3)) |
107 (src->flags & (IMEMO_FL_USER0|IMEMO_FL_USER1|IMEMO_FL_USER2|IMEMO_FL_USER3));
108}
109
110typedef enum {
111 VM_METHOD_TYPE_ISEQ,
112 VM_METHOD_TYPE_CFUNC,
113 VM_METHOD_TYPE_ATTRSET,
114 VM_METHOD_TYPE_IVAR,
115 VM_METHOD_TYPE_BMETHOD,
116 VM_METHOD_TYPE_ZSUPER,
117 VM_METHOD_TYPE_ALIAS,
118 VM_METHOD_TYPE_UNDEF,
119 VM_METHOD_TYPE_NOTIMPLEMENTED,
120 VM_METHOD_TYPE_OPTIMIZED,
121 VM_METHOD_TYPE_MISSING,
122 VM_METHOD_TYPE_REFINED,
124 END_OF_ENUMERATION(VM_METHOD_TYPE)
125} rb_method_type_t;
126#define VM_METHOD_TYPE_MINIMUM_BITS 4
127STATIC_ASSERT(VM_METHOD_TYPE_MINIMUM_BITS,
128 VM_METHOD_TYPE_REFINED <= (1<<VM_METHOD_TYPE_MINIMUM_BITS));
129
130#ifndef rb_iseq_t
131typedef struct rb_iseq_struct rb_iseq_t;
132#define rb_iseq_t rb_iseq_t
133#endif
134
135typedef struct rb_method_iseq_struct {
138} rb_method_iseq_t; /* check rb_add_method_iseq() when modify the fields */
139
140typedef VALUE (*rb_cfunc_t)(ANYARGS);
142 rb_cfunc_t func;
143 VALUE (*invoker)(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS));
144 int argc;
146
147typedef struct rb_method_attr_struct {
148 ID id;
149 VALUE location; /* should be marked */
151
153 struct rb_method_entry_struct * original_me; /* original_me->klass is original owner */
155
157 struct rb_method_entry_struct * orig_me;
159
161 VALUE proc; /* should be marked */
162 struct rb_hook_list_struct *hooks;
163 VALUE defined_ractor;
165
166enum method_optimized_type {
167 OPTIMIZED_METHOD_TYPE_SEND,
168 OPTIMIZED_METHOD_TYPE_CALL,
169 OPTIMIZED_METHOD_TYPE_BLOCK_CALL,
170 OPTIMIZED_METHOD_TYPE_STRUCT_AREF,
171 OPTIMIZED_METHOD_TYPE_STRUCT_ASET,
172 OPTIMIZED_METHOD_TYPE__MAX
173};
174
175typedef struct rb_method_optimized {
176 enum method_optimized_type type;
177 unsigned int index;
179
181 BITFIELD(rb_method_type_t, type, VM_METHOD_TYPE_MINIMUM_BITS);
182 unsigned int iseq_overload: 1;
183 unsigned int no_redef_warning: 1;
184 unsigned int aliased : 1;
185
186 rb_atomic_t reference_count;
187
188 union {
189 rb_method_iseq_t iseq;
190 rb_method_cfunc_t cfunc;
191 rb_method_attr_t attr;
192 rb_method_alias_t alias;
193 rb_method_refined_t refined;
194 rb_method_bmethod_t bmethod;
195 rb_method_optimized_t optimized;
196 } body;
197
198 ID original_id;
199 uintptr_t method_serial;
200};
201
202struct rb_id_table;
203
205STATIC_ASSERT(sizeof_method_def, offsetof(rb_method_definition_t, body) <= 8);
206
207#define UNDEFINED_METHOD_ENTRY_P(me) (!(me) || !(me)->def || (me)->def->type == VM_METHOD_TYPE_UNDEF)
208#define UNDEFINED_REFINED_METHOD_P(def) \
209 ((def)->type == VM_METHOD_TYPE_REFINED && \
210 UNDEFINED_METHOD_ENTRY_P((def)->body.refined.orig_me))
211
212void rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *option, rb_method_visibility_t visi);
213void rb_add_method_cfunc(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_method_visibility_t visi);
214void rb_add_method_iseq(VALUE klass, ID mid, const rb_iseq_t *iseq, rb_cref_t *cref, rb_method_visibility_t visi);
215void rb_add_method_optimized(VALUE klass, ID mid, enum method_optimized_type, unsigned int index, rb_method_visibility_t visi);
216void rb_add_refined_method_entry(VALUE refined_class, ID mid);
217
218rb_method_entry_t *rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *, rb_method_visibility_t noex);
219rb_method_entry_t *rb_method_entry_create(ID called_id, VALUE klass, rb_method_visibility_t visi, rb_method_definition_t *def);
220
221const rb_method_entry_t *rb_method_entry_at(VALUE obj, ID id);
222
223const rb_method_entry_t *rb_method_entry(VALUE klass, ID id);
224const rb_method_entry_t *rb_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class);
225const rb_method_entry_t *rb_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class);
226const rb_method_entry_t *rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
227RUBY_SYMBOL_EXPORT_BEGIN
228const rb_method_entry_t *rb_resolve_me_location(const rb_method_entry_t *, VALUE[5]);
229RUBY_SYMBOL_EXPORT_END
230
231const rb_callable_method_entry_t *rb_callable_method_entry(VALUE klass, ID id);
232const rb_callable_method_entry_t *rb_callable_method_entry_or_negative(VALUE klass, ID id);
233const rb_callable_method_entry_t *rb_callable_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class);
234const rb_callable_method_entry_t *rb_callable_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class);
235
236int rb_method_entry_arity(const rb_method_entry_t *me);
237int rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2);
238st_index_t rb_hash_method_entry(st_index_t hash, const rb_method_entry_t *me);
239
240VALUE rb_method_entry_location(const rb_method_entry_t *me);
241
242void rb_free_method_entry_vm_weak_references(const rb_method_entry_t *me);
243void rb_free_method_entry(const rb_method_entry_t *me);
244
245const rb_method_entry_t *rb_method_entry_clone(const rb_method_entry_t *me);
246const rb_callable_method_entry_t *rb_method_entry_complement_defined_class(const rb_method_entry_t *src_me, ID called_id, VALUE defined_class);
247void rb_method_entry_copy(rb_method_entry_t *dst, const rb_method_entry_t *src);
248
249void rb_method_table_insert(VALUE klass, struct rb_id_table *table, ID method_id, const rb_method_entry_t *me);
250
251void rb_scope_visibility_set(rb_method_visibility_t);
252
253VALUE rb_unnamed_parameters(int arity);
254
255void rb_clear_method_cache(VALUE klass_or_module, ID mid);
256void rb_clear_all_refinement_method_cache(void);
257
258#endif /* RUBY_METHOD_H */
Atomic operations.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define ANYARGS
Functions declared using this macro take arbitrary arguments, including void.
Definition stdarg.h:64
Definition method.h:63
CREF (Class REFerence)
Definition method.h:45
Definition method.h:55
rb_cref_t * cref
class reference, should be marked
Definition method.h:137
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition method.h:136
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40