Ruby  3.4.0dev (2024-11-22 revision 37a72b0150ec36b4ea27175039afc28c62207b0c)
method.h (37a72b0150ec36b4ea27175039afc28c62207b0c)
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 
29 typedef 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;
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;
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;
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 
79 static inline void
80 METHOD_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 }
85 static inline void
86 METHOD_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 }
91 static inline void
92 METHOD_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 }
100 static inline void
101 METHOD_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 
109 typedef 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
126 STATIC_ASSERT(VM_METHOD_TYPE_MINIMUM_BITS,
127  VM_METHOD_TYPE_REFINED <= (1<<VM_METHOD_TYPE_MINIMUM_BITS));
128 
129 #ifndef rb_iseq_t
130 typedef 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;
137 } rb_method_iseq_t; /* check rb_add_method_iseq() when modify the fields */
138 
139 typedef VALUE (*rb_cfunc_t)(ANYARGS);
140 typedef struct rb_method_cfunc_struct {
141  rb_cfunc_t func;
142  VALUE (*invoker)(VALUE recv, int argc, const VALUE *argv, VALUE (*func)(ANYARGS));
143  int argc;
145 
146 typedef struct rb_method_attr_struct {
147  ID id;
148  VALUE location; /* should be marked */
150 
151 typedef struct rb_method_alias_struct {
152  struct rb_method_entry_struct * original_me; /* original_me->klass is original owner */
154 
155 typedef struct rb_method_refined_struct {
156  struct rb_method_entry_struct * orig_me;
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;
164 
165 enum 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;
178 
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 
200 struct rb_id_table;
201 
203 STATIC_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 
210 void rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *option, rb_method_visibility_t visi);
211 void rb_add_method_cfunc(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_method_visibility_t visi);
212 void rb_add_method_iseq(VALUE klass, ID mid, const rb_iseq_t *iseq, rb_cref_t *cref, rb_method_visibility_t visi);
213 void rb_add_method_optimized(VALUE klass, ID mid, enum method_optimized_type, unsigned int index, rb_method_visibility_t visi);
214 void 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 
219 const rb_method_entry_t *rb_method_entry_at(VALUE obj, ID id);
220 
221 const rb_method_entry_t *rb_method_entry(VALUE klass, ID id);
222 const rb_method_entry_t *rb_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class);
223 const rb_method_entry_t *rb_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class);
224 const rb_method_entry_t *rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
225 RUBY_SYMBOL_EXPORT_BEGIN
226 const rb_method_entry_t *rb_resolve_me_location(const rb_method_entry_t *, VALUE[5]);
227 RUBY_SYMBOL_EXPORT_END
228 
229 const rb_callable_method_entry_t *rb_callable_method_entry(VALUE klass, ID id);
230 const rb_callable_method_entry_t *rb_callable_method_entry_or_negative(VALUE klass, ID id);
231 const rb_callable_method_entry_t *rb_callable_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class);
232 const rb_callable_method_entry_t *rb_callable_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class);
233 
234 int rb_method_entry_arity(const rb_method_entry_t *me);
235 int rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2);
236 st_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 
240 void rb_free_method_entry(const rb_method_entry_t *me);
241 
242 const rb_method_entry_t *rb_method_entry_clone(const rb_method_entry_t *me);
243 const rb_callable_method_entry_t *rb_method_entry_complement_defined_class(const rb_method_entry_t *src_me, ID called_id, VALUE defined_class);
244 void rb_method_entry_copy(rb_method_entry_t *dst, const rb_method_entry_t *src);
245 
246 void rb_method_table_insert(VALUE klass, struct rb_id_table *table, ID method_id, const rb_method_entry_t *me);
247 
248 void rb_scope_visibility_set(rb_method_visibility_t);
249 
250 VALUE rb_unnamed_parameters(int arity);
251 
252 void rb_clear_method_cache(VALUE klass_or_module, ID mid);
253 void rb_clear_all_refinement_method_cache(void);
254 
255 #endif /* RUBY_METHOD_H */
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
#define ANYARGS
Functions declared using this macro take arbitrary arguments, including void.
Definition: stdarg.h:64
Definition: method.h:62
CREF (Class REFerence)
Definition: method.h:44
Definition: method.h:54
rb_cref_t * cref
class reference, should be marked
Definition: method.h:136
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition: method.h:135
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