Ruby: method.h Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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 VALUE owner;
157} rb_method_refined_t;
158
159 typedef struct rb_method_bmethod_struct {
160 VALUE proc; /* should be marked */
161 struct rb_hook_list_struct *hooks;
162 VALUE defined_ractor;
163} rb_method_bmethod_t;
164
165enum method_optimized_type {
166 OPTIMIZED_METHOD_TYPE_SEND,
167 OPTIMIZED_METHOD_TYPE_CALL,
168 OPTIMIZED_METHOD_TYPE_BLOCK_CALL,
169 OPTIMIZED_METHOD_TYPE_STRUCT_AREF,
170 OPTIMIZED_METHOD_TYPE_STRUCT_ASET,
171 OPTIMIZED_METHOD_TYPE__MAX
172};
173
174 typedef struct rb_method_optimized {
175 enum method_optimized_type type;
176 unsigned int index;
177} rb_method_optimized_t;
178
179 struct rb_method_definition_struct {
180 BITFIELD(rb_method_type_t, type, VM_METHOD_TYPE_MINIMUM_BITS);
181 unsigned int iseq_overload: 1;
182 unsigned int no_redef_warning: 1;
183 unsigned int aliased : 1;
184 int reference_count : 28;
185
186 union {
187 rb_method_iseq_t iseq;
188 rb_method_cfunc_t cfunc;
189 rb_method_attr_t attr;
190 rb_method_alias_t alias;
191 rb_method_refined_t refined;
192 rb_method_bmethod_t bmethod;
193 rb_method_optimized_t optimized;
194 } body;
195
196 ID original_id;
197 uintptr_t method_serial;
198};
199
200struct rb_id_table;
201
202typedef struct rb_method_definition_struct rb_method_definition_t;
203STATIC_ASSERT(sizeof_method_def, offsetof(rb_method_definition_t, body)==8);
204
205#define UNDEFINED_METHOD_ENTRY_P(me) (!(me) || !(me)->def || (me)->def->type == VM_METHOD_TYPE_UNDEF)
206#define UNDEFINED_REFINED_METHOD_P(def) \
207 ((def)->type == VM_METHOD_TYPE_REFINED && \
208 UNDEFINED_METHOD_ENTRY_P((def)->body.refined.orig_me))
209
210void rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *option, rb_method_visibility_t visi);
211void rb_add_method_cfunc(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_method_visibility_t visi);
212void rb_add_method_iseq(VALUE klass, ID mid, const rb_iseq_t *iseq, rb_cref_t *cref, rb_method_visibility_t visi);
213void rb_add_method_optimized(VALUE klass, ID mid, enum method_optimized_type, unsigned int index, rb_method_visibility_t visi);
214void rb_add_refined_method_entry(VALUE refined_class, ID mid);
215
216 rb_method_entry_t *rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *, rb_method_visibility_t noex);
217 rb_method_entry_t *rb_method_entry_create(ID called_id, VALUE klass, rb_method_visibility_t visi, rb_method_definition_t *def);
218
219const rb_method_entry_t *rb_method_entry_at(VALUE obj, ID id);
220
221const rb_method_entry_t *rb_method_entry(VALUE klass, ID id);
222const rb_method_entry_t *rb_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class);
223const rb_method_entry_t *rb_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class);
224const rb_method_entry_t *rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
225RUBY_SYMBOL_EXPORT_BEGIN
226const rb_method_entry_t *rb_resolve_me_location(const rb_method_entry_t *, VALUE[5]);
227RUBY_SYMBOL_EXPORT_END
228
229const rb_callable_method_entry_t *rb_callable_method_entry(VALUE klass, ID id);
230const rb_callable_method_entry_t *rb_callable_method_entry_or_negative(VALUE klass, ID id);
231const rb_callable_method_entry_t *rb_callable_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class);
232const rb_callable_method_entry_t *rb_callable_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class);
233
234int rb_method_entry_arity(const rb_method_entry_t *me);
235int rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2);
236st_index_t rb_hash_method_entry(st_index_t hash, const rb_method_entry_t *me);
237
238 VALUE rb_method_entry_location(const rb_method_entry_t *me);
239
240void rb_free_method_entry(const rb_method_entry_t *me);
241
242const rb_method_entry_t *rb_method_entry_clone(const rb_method_entry_t *me);
243const rb_callable_method_entry_t *rb_method_entry_complement_defined_class(const rb_method_entry_t *src_me, ID called_id, VALUE defined_class);
244void rb_method_entry_copy(rb_method_entry_t *dst, const rb_method_entry_t *src);
245
246void rb_method_table_insert(VALUE klass, struct rb_id_table *table, ID method_id, const rb_method_entry_t *me);
247
248void rb_scope_visibility_set(rb_method_visibility_t);
249
250 VALUE rb_unnamed_parameters(int arity);
251
252void rb_clear_method_cache(VALUE klass_or_module, ID mid);
253void rb_clear_method_cache_all(void);
254
255#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:603
rb_id_table
Definition id_table.c:41
rb_iseq_struct
Definition vm_core.h:526
rb_method_alias_struct
Definition method.h:150
rb_method_attr_struct
Definition method.h:145
rb_method_bmethod_struct
Definition method.h:159
rb_method_cfunc_struct
Definition method.h:139
rb_method_definition_struct
Definition method.h:179
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:174
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