2 #define RUBY_METHOD_H 1
3 /**********************************************************************
8 created at: Wed Jul 15 20:02:33 2009
10 Copyright (C) 2009 Koichi Sasada
12 **********************************************************************/
15 #include "internal/imemo.h"
16 #include "internal/compilers.h"
17 #include "internal/static_assert.h"
19 #ifndef END_OF_ENUMERATION
20 # if defined(__GNUC__) &&! defined(__STRICT_ANSI__)
21 # define END_OF_ENUMERATION(key)
23 # define END_OF_ENUMERATION(key) END_OF_##key##_PLACEHOLDER = 0
30 METHOD_VISI_UNDEF
= 0x00,
31 METHOD_VISI_PUBLIC
= 0x01,
32 METHOD_VISI_PRIVATE
= 0x02,
33 METHOD_VISI_PROTECTED
= 0x03,
35 METHOD_VISI_MASK
= 0x03
36 } rb_method_visibility_t
;
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
;
43 /*! CREF (Class REFerence) */
44 typedef struct rb_cref_struct
{
48 struct rb_cref_struct
* next
;
49 const rb_scope_visibility_t scope_visi
;
52 /* method data type */
54 typedef struct rb_method_entry_struct
{
57 struct rb_method_definition_struct
* const def
;
62 typedef struct rb_callable_method_entry_struct
{ /* same fields with rb_method_entry_t */
64 const VALUE defined_class
;
65 struct rb_method_definition_struct
* const def
;
68 } rb_callable_method_entry_t
;
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 #define METHOD_ENTRY_CACHEABLE(me) !(METHOD_ENTRY_VISI(me) == METHOD_VISI_PROTECTED)
81 METHOD_ENTRY_VISI_SET(rb_method_entry_t
*me
, rb_method_visibility_t visi
)
83 VM_ASSERT((int)visi
>= 0 && visi
<= 3);
84 me
->flags
= (me
->flags
& ~(IMEMO_FL_USER0
| IMEMO_FL_USER1
)) | (visi
<< (IMEMO_FL_USHIFT
+0));
87 METHOD_ENTRY_BASIC_SET(rb_method_entry_t
*me
, unsigned int basic
)
89 VM_ASSERT(basic
<= 1);
90 me
->flags
= (me
->flags
& ~(IMEMO_FL_USER2
)) | (basic
<< (IMEMO_FL_USHIFT
+2));
93 METHOD_ENTRY_FLAGS_SET(rb_method_entry_t
*me
, rb_method_visibility_t visi
, unsigned int basic
)
95 VM_ASSERT((int)visi
>= 0 && visi
<= 3);
96 VM_ASSERT(basic
<= 1);
98 (me
->flags
& ~(IMEMO_FL_USER0
|IMEMO_FL_USER1
|IMEMO_FL_USER2
)) |
99 ((visi
<< (IMEMO_FL_USHIFT
+0)) | (basic
<< (IMEMO_FL_USHIFT
+2)));
102 METHOD_ENTRY_FLAGS_COPY(rb_method_entry_t
*dst
, const rb_method_entry_t
*src
)
105 (dst
->flags
& ~(IMEMO_FL_USER0
|IMEMO_FL_USER1
|IMEMO_FL_USER2
)) |
106 (src
->flags
& (IMEMO_FL_USER0
|IMEMO_FL_USER1
|IMEMO_FL_USER2
));
110 VM_METHOD_TYPE_ISEQ
, /*!< Ruby method */
111 VM_METHOD_TYPE_CFUNC
, /*!< C method */
112 VM_METHOD_TYPE_ATTRSET
, /*!< attr_writer or attr_accessor */
113 VM_METHOD_TYPE_IVAR
, /*!< attr_reader or attr_accessor */
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
, /*!< Kernel#send, Proc#call, etc */
120 VM_METHOD_TYPE_MISSING
, /*!< wrapper for method_missing(id) */
121 VM_METHOD_TYPE_REFINED
, /*!< refinement */
123 END_OF_ENUMERATION(VM_METHOD_TYPE
)
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
));
130 typedef struct rb_iseq_struct rb_iseq_t
;
131 #define rb_iseq_t rb_iseq_t
134 typedef struct rb_method_iseq_struct
{
135 const rb_iseq_t
* iseqptr
; /*!< iseq pointer, should be separated from iseqval */
136 rb_cref_t
* cref
; /*!< class reference, should be marked */
137 } rb_method_iseq_t
; /* check rb_add_method_iseq() when modify the fields */
139 typedef struct rb_method_cfunc_struct
{
140 VALUE (*func
)(ANYARGS
);
141 VALUE (*invoker
)(VALUE recv
, int argc
, const VALUE
*argv
, VALUE (*func
)(ANYARGS
));
145 typedef struct rb_method_attr_struct
{
147 VALUE location
; /* should be marked */
150 typedef struct rb_method_alias_struct
{
151 struct rb_method_entry_struct
* original_me
; /* original_me->klass is original owner */
154 typedef struct rb_method_refined_struct
{
155 struct rb_method_entry_struct
* orig_me
;
157 } rb_method_refined_t
;
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
;
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
174 typedef struct rb_method_optimized
{
175 enum method_optimized_type type
;
177 } rb_method_optimized_t
;
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 int alias_count
: 27;
183 int complemented_count
: 28;
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
;
196 uintptr_t method_serial
;
201 typedef struct rb_method_definition_struct rb_method_definition_t
;
202 STATIC_ASSERT(sizeof_method_def
, offsetof(rb_method_definition_t
, body
)==8);
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))
209 void rb_add_method(VALUE klass
, ID mid
, rb_method_type_t type
, void *option
, rb_method_visibility_t visi
);
210 void rb_add_method_cfunc(VALUE klass
, ID mid
, VALUE (*func
)(ANYARGS
), int argc
, rb_method_visibility_t visi
);
211 void rb_add_method_iseq(VALUE klass
, ID mid
, const rb_iseq_t
*iseq
, rb_cref_t
*cref
, rb_method_visibility_t visi
);
212 void rb_add_method_optimized(VALUE klass
, ID mid
, enum method_optimized_type
, unsigned int index
, rb_method_visibility_t visi
);
213 void rb_add_refined_method_entry(VALUE refined_class
, ID mid
);
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
, const rb_method_definition_t
*def
);
218 const rb_method_entry_t
*rb_method_entry_at(VALUE obj
, ID id
);
220 const rb_method_entry_t
*rb_method_entry(VALUE klass
, ID id
);
221 const rb_method_entry_t
*rb_method_entry_with_refinements(VALUE klass
, ID id
, VALUE
*defined_class
);
222 const rb_method_entry_t
*rb_method_entry_without_refinements(VALUE klass
, ID id
, VALUE
*defined_class
);
223 const rb_method_entry_t
*rb_resolve_refined_method(VALUE refinements
, const rb_method_entry_t
*me
);
224 RUBY_SYMBOL_EXPORT_BEGIN
225 const rb_method_entry_t
*rb_resolve_me_location(const rb_method_entry_t
*, VALUE
[5]);
226 RUBY_SYMBOL_EXPORT_END
228 const rb_callable_method_entry_t
*rb_callable_method_entry(VALUE klass
, ID id
);
229 const rb_callable_method_entry_t
*rb_callable_method_entry_with_refinements(VALUE klass
, ID id
, VALUE
*defined_class
);
230 const rb_callable_method_entry_t
*rb_callable_method_entry_without_refinements(VALUE klass
, ID id
, VALUE
*defined_class
);
232 int rb_method_entry_arity(const rb_method_entry_t
*me
);
233 int rb_method_entry_eq(const rb_method_entry_t
*m1
, const rb_method_entry_t
*m2
);
234 st_index_t
rb_hash_method_entry(st_index_t hash
, const rb_method_entry_t
*me
);
236 VALUE
rb_method_entry_location(const rb_method_entry_t
*me
);
238 void rb_free_method_entry(const rb_method_entry_t
*me
);
240 const rb_method_entry_t
*rb_method_entry_clone(const rb_method_entry_t
*me
);
241 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
);
242 void rb_method_entry_copy(rb_method_entry_t
*dst
, const rb_method_entry_t
*src
);
244 void rb_method_table_insert(VALUE klass
, struct rb_id_table
*table
, ID method_id
, const rb_method_entry_t
*me
);
246 void rb_scope_visibility_set(rb_method_visibility_t
);
248 VALUE
rb_unnamed_parameters(int arity
);
250 void rb_clear_method_cache(VALUE klass_or_module
, ID mid
);
251 void rb_clear_method_cache_all(void);
253 #endif /* RUBY_METHOD_H */