Ruby: method.h Source File

Ruby 3.3.4p94 (2024-07-09 revision be1089c8ec5ba40e09b1553e36b3174bf4014d9d)

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