Ruby  2.5.0dev(2017-10-22revision60238)
method.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  method.h -
4 
5  $Author$
6  created at: Wed Jul 15 20:02:33 2009
7 
8  Copyright (C) 2009 Koichi Sasada
9 
10 **********************************************************************/
11 #ifndef RUBY_METHOD_H
12 #define RUBY_METHOD_H 1
13 
14 #include "internal.h"
15 
16 #ifndef END_OF_ENUMERATION
17 # if defined(__GNUC__) &&! defined(__STRICT_ANSI__)
18 # define END_OF_ENUMERATION(key)
19 # else
20 # define END_OF_ENUMERATION(key) END_OF_##key##_PLACEHOLDER = 0
21 # endif
22 #endif
23 
24 /* cref */
25 
26 typedef enum {
31 
34 
35 typedef struct rb_scope_visi_struct {
37  unsigned int module_func : 1;
39 
41 typedef struct rb_cref_struct {
44  const VALUE klass;
45  struct rb_cref_struct * const next;
47 } rb_cref_t;
48 
49 /* method data type */
50 
51 typedef struct rb_method_entry_struct {
54  struct rb_method_definition_struct * const def;
56  const VALUE owner;
58 
59 typedef struct rb_callable_method_entry_struct { /* same fields with rb_method_entry_t */
62  struct rb_method_definition_struct * const def;
64  const VALUE owner;
66 
67 #define METHOD_ENTRY_VISI(me) (rb_method_visibility_t)(((me)->flags & (IMEMO_FL_USER0 | IMEMO_FL_USER1)) >> (IMEMO_FL_USHIFT+0))
68 #define METHOD_ENTRY_BASIC(me) (int) (((me)->flags & (IMEMO_FL_USER2 )) >> (IMEMO_FL_USHIFT+2))
69 #define METHOD_ENTRY_COMPLEMENTED(me) ((me)->flags & IMEMO_FL_USER3)
70 #define METHOD_ENTRY_COMPLEMENTED_SET(me) ((me)->flags = (me)->flags | IMEMO_FL_USER3)
71 
72 static inline void
73 METHOD_ENTRY_VISI_SET(rb_method_entry_t *me, rb_method_visibility_t visi)
74 {
75  VM_ASSERT((int)visi >= 0 && visi <= 3);
76  me->flags = (me->flags & ~(IMEMO_FL_USER0 | IMEMO_FL_USER1)) | (visi << (IMEMO_FL_USHIFT+0));
77 }
78 static inline void
79 METHOD_ENTRY_BASIC_SET(rb_method_entry_t *me, unsigned int basic)
80 {
81  VM_ASSERT(basic <= 1);
82  me->flags = (me->flags & ~(IMEMO_FL_USER2 )) | (basic << (IMEMO_FL_USHIFT+2));
83 }
84 static inline void
85 METHOD_ENTRY_FLAGS_SET(rb_method_entry_t *me, rb_method_visibility_t visi, unsigned int basic)
86 {
87  VM_ASSERT((int)visi >= 0 && visi <= 3);
88  VM_ASSERT(basic <= 1);
89  me->flags =
91  ((visi << (IMEMO_FL_USHIFT+0)) | (basic << (IMEMO_FL_USHIFT+2)));
92 }
93 static inline void
94 METHOD_ENTRY_FLAGS_COPY(rb_method_entry_t *dst, const rb_method_entry_t *src)
95 {
96  dst->flags =
99 }
100 
101 typedef enum {
115  END_OF_ENUMERATION(VM_METHOD_TYPE)
117 
118 #ifndef rb_iseq_t
119 typedef struct rb_iseq_struct rb_iseq_t;
120 #define rb_iseq_t rb_iseq_t
121 #endif
122 
123 typedef struct rb_method_iseq_struct {
124  const rb_iseq_t * const iseqptr;
125  rb_cref_t * const cref;
126 } rb_method_iseq_t; /* check rb_add_method_iseq() when modify the fields */
127 
128 typedef struct rb_method_cfunc_struct {
129  VALUE (*func)(ANYARGS);
130  VALUE (*invoker)(VALUE (*func)(ANYARGS), VALUE recv, int argc, const VALUE *argv);
131  int argc;
133 
134 typedef struct rb_method_attr_struct {
136  const VALUE location; /* should be marked */
138 
139 typedef struct rb_method_alias_struct {
140  const struct rb_method_entry_struct * const original_me; /* original_me->klass is original owner */
142 
143 typedef struct rb_method_refined_struct {
144  const struct rb_method_entry_struct * const orig_me;
145  const VALUE owner;
147 
152 };
153 
154 PACKED_STRUCT_UNALIGNED(struct rb_method_definition_struct {
155  unsigned int type : 4; /* method type */
156  int alias_count : 28;
157  int complemented_count : 28;
158 
159  union {
160  rb_method_iseq_t iseq;
161  rb_method_cfunc_t cfunc;
162  rb_method_attr_t attr;
164  rb_method_refined_t refined;
165 
166  const VALUE proc; /* should be marked */
167  enum method_optimized_type optimize_type;
168  } body;
169 
170  ID original_id;
171 });
172 
173 typedef struct rb_method_definition_struct rb_method_definition_t;
174 
175 #define UNDEFINED_METHOD_ENTRY_P(me) (!(me) || !(me)->def || (me)->def->type == VM_METHOD_TYPE_UNDEF)
176 #define UNDEFINED_REFINED_METHOD_P(def) \
177  ((def)->type == VM_METHOD_TYPE_REFINED && \
178  UNDEFINED_METHOD_ENTRY_P((def)->body.refined.orig_me))
179 
180 void rb_add_method_cfunc(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_method_visibility_t visi);
181 void rb_add_method_iseq(VALUE klass, ID mid, const rb_iseq_t *iseq, rb_cref_t *cref, rb_method_visibility_t visi);
182 void rb_add_refined_method_entry(VALUE refined_class, ID mid);
183 
184 rb_method_entry_t *rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *option, rb_method_visibility_t visi);
187 
189 
190 const rb_method_entry_t *rb_method_entry(VALUE klass, ID id);
191 const rb_method_entry_t *rb_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class);
192 const rb_method_entry_t *rb_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class);
194 
199 
201 int rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2);
203 
207 
209 void rb_sweep_method_entry(void *vm);
210 
214 
216 
217 #endif /* RUBY_METHOD_H */
wrapper for method_missing(id)
Definition: method.h:112
method_optimized_type
Definition: method.h:148
const VALUE owner
Definition: method.h:145
rb_method_entry_t * rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *, rb_method_visibility_t noex)
Definition: vm_method.c:660
const rb_method_entry_t * rb_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class)
Definition: vm_method.c:866
struct rb_method_iseq_struct rb_method_iseq_t
const rb_method_entry_t * rb_method_entry_clone(const rb_method_entry_t *me)
Definition: vm_method.c:396
void rb_add_method_iseq(VALUE klass, ID mid, const rb_iseq_t *iseq, rb_cref_t *cref, rb_method_visibility_t visi)
Definition: vm_method.c:637
const VALUE owner
Definition: method.h:64
const VALUE location
Definition: method.h:136
rb_cref_t *const cref
class reference, should be marked
Definition: method.h:125
struct rb_method_definition_struct rb_method_definition_t
Definition: method.h:173
struct rb_method_definition_struct *const def
Definition: method.h:62
VALUE rb_mod_method_location(VALUE mod, ID id)
Definition: proc.c:2501
struct rb_method_attr_struct rb_method_attr_t
Ruby method.
Definition: method.h:102
rb_method_entry_t * rb_method_entry_create(ID called_id, VALUE klass, rb_method_visibility_t visi, const rb_method_definition_t *def)
Definition: vm_method.c:387
const VALUE owner
Definition: method.h:56
struct rb_method_alias_struct rb_method_alias_t
ID called_id
Definition: method.h:55
void rb_sweep_method_entry(void *vm)
struct rb_method_entry_struct rb_method_entry_t
const char * alias
Definition: nkf.c:1151
st_data_t st_index_t
Definition: st.h:50
const rb_callable_method_entry_t * rb_callable_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class)
Definition: vm_method.c:886
#define IMEMO_FL_USER1
Definition: internal.h:875
struct rb_method_refined_struct rb_method_refined_t
int rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2)
Definition: vm_method.c:1420
refinement
Definition: method.h:113
VALUE flags
Definition: method.h:60
const rb_scope_visibility_t scope_visi
Definition: method.h:46
struct rb_cref_struct rb_cref_t
CREF (Class REFerence)
struct rb_cref_struct *const next
Definition: method.h:45
void rb_method_entry_copy(rb_method_entry_t *dst, const rb_method_entry_t *src)
Definition: vm_method.c:418
const rb_callable_method_entry_t * rb_callable_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class)
Definition: vm_method.c:872
#define IMEMO_FL_USER2
Definition: internal.h:876
rb_method_visibility_t
Definition: method.h:26
struct rb_method_definition_struct *const def
Definition: method.h:54
void rb_free_method_entry(const rb_method_entry_t *me)
Definition: vm_method.c:168
int rb_method_entry_arity(const rb_method_entry_t *me)
Definition: proc.c:2334
int argc
Definition: ruby.c:187
const rb_method_entry_t * rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me)
Definition: vm_method.c:940
const VALUE refinements
Definition: method.h:43
const VALUE defined_class
Definition: method.h:61
Definition: method.h:51
Definition: method.h:59
VALUE rb_method_entry_location(const rb_method_entry_t *me)
Definition: proc.c:2494
const rb_callable_method_entry_t * rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me)
Definition: vm_method.c:946
rb_method_type_t
Definition: method.h:101
attr_writer or attr_accessor
Definition: method.h:104
void rb_add_method_cfunc(VALUE klass, ID mid, VALUE(*func)(ANYARGS), int argc, rb_method_visibility_t visi)
Definition: vm_method.c:130
const VALUE klass
Definition: method.h:44
const rb_callable_method_entry_t * rb_callable_method_entry(VALUE klass, ID id)
Definition: vm_method.c:833
#define VM_ASSERT(expr)
Definition: vm_core.h:53
const rb_method_entry_t * rb_method_entry(VALUE klass, ID id)
Definition: vm_method.c:793
const rb_method_entry_t * rb_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class)
Definition: vm_method.c:880
unsigned long ID
Definition: ruby.h:86
#define IMEMO_FL_USER0
Definition: internal.h:874
st_index_t rb_hash_method_entry(st_index_t hash, const rb_method_entry_t *me)
Definition: vm_method.c:1519
unsigned long VALUE
Definition: ruby.h:85
const VALUE defined_class
Definition: method.h:53
rb_method_visibility_t method_visi
Definition: method.h:36
ID called_id
Definition: method.h:63
const struct rb_method_entry_struct *const original_me
Definition: method.h:140
struct rb_callable_method_entry_struct rb_callable_method_entry_t
CREF (Class REFerence)
Definition: method.h:41
const rb_method_entry_t * rb_method_entry_at(VALUE obj, ID id)
Definition: vm_method.c:709
Kernel::send, Proc::call, etc.
Definition: method.h:111
struct rb_method_cfunc_struct rb_method_cfunc_t
#define IMEMO_FL_USHIFT
Definition: internal.h:873
#define END_OF_ENUMERATION(key)
Definition: method.h:20
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)
Definition: vm_method.c:405
#define ANYARGS
Definition: defines.h:173
PACKED_STRUCT_UNALIGNED(struct rb_method_definition_struct { unsigned int type :4;int alias_count :28;int complemented_count :28;union { rb_method_iseq_t iseq;rb_method_cfunc_t cfunc;rb_method_attr_t attr;rb_method_alias_t alias;rb_method_refined_t refined;const VALUE proc;enum method_optimized_type optimize_type;} body;ID original_id;})
struct rb_scope_visi_struct rb_scope_visibility_t
rb_method_entry_t * rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *option, rb_method_visibility_t visi)
Definition: vm_method.c:625
void rb_scope_visibility_set(rb_method_visibility_t)
Definition: vm_method.c:1125
void rb_add_refined_method_entry(VALUE refined_class, ID mid)
Definition: vm_method.c:456
VALUE flags
Definition: method.h:42
VALUE flags
Definition: method.h:52
C method.
Definition: method.h:103
const rb_iseq_t *const iseqptr
iseq pointer, should be separated from iseqval
Definition: method.h:124
#define mod(x, y)
Definition: date_strftime.c:28
const struct rb_method_entry_struct *const orig_me
Definition: method.h:144
attr_reader or attr_accessor
Definition: method.h:105
VALUE rb_obj_method_location(VALUE obj, ID id)
Definition: proc.c:2508
unsigned int module_func
Definition: method.h:37
char ** argv
Definition: ruby.c:188