Ruby  2.1.10p492(2016-04-01revision54464)
class.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  class.c -
4 
5  $Author: usa $
6  created at: Tue Aug 10 15:05:44 JST 1993
7 
8  Copyright (C) 1993-2007 Yukihiro Matsumoto
9 
10 **********************************************************************/
11 
26 #include "ruby/ruby.h"
27 #include "ruby/st.h"
28 #include "method.h"
29 #include "constant.h"
30 #include "vm_core.h"
31 #include "internal.h"
32 #include <ctype.h>
33 
34 int rb_vm_add_root_module(ID id, VALUE module);
35 
36 
37 #define id_attached id__attached__
38 
39 void
41 {
42  rb_subclass_entry_t *entry, *head;
43 
44  if (super && super != Qundef) {
45  entry = xmalloc(sizeof(*entry));
46  entry->klass = klass;
47  entry->next = NULL;
48 
49  head = RCLASS_EXT(super)->subclasses;
50  if (head) {
51  entry->next = head;
52  RCLASS_EXT(head->klass)->parent_subclasses = &entry->next;
53  }
54 
55  RCLASS_EXT(super)->subclasses = entry;
56  RCLASS_EXT(klass)->parent_subclasses = &RCLASS_EXT(super)->subclasses;
57  }
58 }
59 
60 static void
62 {
63  rb_subclass_entry_t *entry, *head;
64 
65  entry = xmalloc(sizeof(*entry));
66  entry->klass = iclass;
67  entry->next = NULL;
68 
69  head = RCLASS_EXT(module)->subclasses;
70  if (head) {
71  entry->next = head;
72  RCLASS_EXT(head->klass)->module_subclasses = &entry->next;
73  }
74 
75  RCLASS_EXT(module)->subclasses = entry;
76  RCLASS_EXT(iclass)->module_subclasses = &RCLASS_EXT(module)->subclasses;
77 }
78 
79 void
81 {
82  rb_subclass_entry_t *entry;
83 
84  if (RCLASS_EXT(klass)->parent_subclasses) {
85  entry = *RCLASS_EXT(klass)->parent_subclasses;
86 
87  *RCLASS_EXT(klass)->parent_subclasses = entry->next;
88  if (entry->next) {
89  RCLASS_EXT(entry->next->klass)->parent_subclasses = RCLASS_EXT(klass)->parent_subclasses;
90  }
91  xfree(entry);
92  }
93 
94  RCLASS_EXT(klass)->parent_subclasses = NULL;
95 }
96 
97 void
99 {
100  rb_subclass_entry_t *entry;
101 
102  if (RCLASS_EXT(klass)->module_subclasses) {
103  entry = *RCLASS_EXT(klass)->module_subclasses;
104  *RCLASS_EXT(klass)->module_subclasses = entry->next;
105 
106  if (entry->next) {
107  RCLASS_EXT(entry->next->klass)->module_subclasses = RCLASS_EXT(klass)->module_subclasses;
108  }
109 
110  xfree(entry);
111  }
112 
113  RCLASS_EXT(klass)->module_subclasses = NULL;
114 }
115 
116 void
118 {
119  rb_subclass_entry_t *cur = RCLASS_EXT(klass)->subclasses;
120 
121  /* do not be tempted to simplify this loop into a for loop, the order of
122  operations is important here if `f` modifies the linked list */
123  while (cur) {
124  VALUE curklass = cur->klass;
125  cur = cur->next;
126  f(curklass);
127  }
128 }
129 
130 void
132 {
134 }
135 
136 void
138 {
140 }
141 
154 static VALUE
155 class_alloc(VALUE flags, VALUE klass)
156 {
157  NEWOBJ_OF(obj, struct RClass, klass, (flags & T_MASK) | (RGENGC_WB_PROTECTED_CLASS ? FL_WB_PROTECTED : 0));
158  obj->ptr = ALLOC(rb_classext_t);
159  RCLASS_IV_TBL(obj) = 0;
160  RCLASS_CONST_TBL(obj) = 0;
161  RCLASS_M_TBL_WRAPPER(obj) = 0;
162  RCLASS_SET_SUPER((VALUE)obj, 0);
163  RCLASS_ORIGIN(obj) = (VALUE)obj;
164  RCLASS_IV_INDEX_TBL(obj) = 0;
165 
166  RCLASS_EXT(obj)->subclasses = NULL;
167  RCLASS_EXT(obj)->parent_subclasses = NULL;
168  RCLASS_EXT(obj)->module_subclasses = NULL;
170 
171  RCLASS_REFINED_CLASS(obj) = Qnil;
172  RCLASS_EXT(obj)->allocator = 0;
173  return (VALUE)obj;
174 }
175 
176 
186 VALUE
188 {
190 
191  RCLASS_SET_SUPER(klass, super);
192  RCLASS_M_TBL_INIT(klass);
193 
194  OBJ_INFECT(klass, super);
195  return (VALUE)klass;
196 }
197 
198 
205 void
207 {
208  if (!RB_TYPE_P(super, T_CLASS)) {
209  rb_raise(rb_eTypeError, "superclass must be a Class (%s given)",
210  rb_obj_classname(super));
211  }
212  if (RBASIC(super)->flags & FL_SINGLETON) {
213  rb_raise(rb_eTypeError, "can't make subclass of singleton class");
214  }
215  if (super == rb_cClass) {
216  rb_raise(rb_eTypeError, "can't make subclass of Class");
217  }
218 }
219 
220 
227 VALUE
229 {
230  Check_Type(super, T_CLASS);
231  rb_check_inheritable(super);
232  return rb_class_boot(super);
233 }
234 
235 static void
236 clone_method(VALUE klass, ID mid, const rb_method_entry_t *me)
237 {
238  VALUE newiseqval;
239  if (me->def && me->def->type == VM_METHOD_TYPE_ISEQ) {
240  rb_iseq_t *iseq;
241  NODE *new_cref;
242  newiseqval = rb_iseq_clone(me->def->body.iseq->self, klass);
243  GetISeqPtr(newiseqval, iseq);
244  rb_vm_rewrite_cref_stack(me->def->body.iseq->cref_stack, me->klass, klass, &new_cref);
245  RB_OBJ_WRITE(iseq->self, &iseq->cref_stack, new_cref);
246  rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, iseq, me->flag);
247  RB_GC_GUARD(newiseqval);
248  }
249  else {
250  rb_method_entry_set(klass, mid, me, me->flag);
251  }
252 }
253 
254 static int
256 {
257  clone_method((VALUE)data, (ID)key, (const rb_method_entry_t *)value);
258  return ST_CONTINUE;
259 }
260 
264 };
265 
266 static int
268 {
270  MEMCPY(nce, ce, rb_const_entry_t, 1);
271  RB_OBJ_WRITTEN(arg->klass, Qundef, ce->value);
272  RB_OBJ_WRITTEN(arg->klass, Qundef, ce->file);
273 
274  st_insert(arg->tbl, key, (st_data_t)nce);
275  return ST_CONTINUE;
276 }
277 
278 static int
280 {
281  return clone_const((ID)key, (const rb_const_entry_t *)value, (struct clone_const_arg *)data);
282 }
283 
284 static void
286 {
287  if (orig == rb_cBasicObject) {
288  rb_raise(rb_eTypeError, "can't copy the root class");
289  }
290  if (RCLASS_SUPER(clone) != 0 || clone == rb_cBasicObject) {
291  rb_raise(rb_eTypeError, "already initialized class");
292  }
293  if (FL_TEST(orig, FL_SINGLETON)) {
294  rb_raise(rb_eTypeError, "can't copy singleton class");
295  }
296 }
297 
298 /* :nodoc: */
299 VALUE
301 {
302  if (RB_TYPE_P(clone, T_CLASS)) {
303  class_init_copy_check(clone, orig);
304  }
305  if (!OBJ_INIT_COPY(clone, orig)) return clone;
306  if (!FL_TEST(CLASS_OF(clone), FL_SINGLETON)) {
308  rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
309  }
310  RCLASS_SET_SUPER(clone, RCLASS_SUPER(orig));
311  RCLASS_EXT(clone)->allocator = RCLASS_EXT(orig)->allocator;
312  if (RCLASS_IV_TBL(clone)) {
314  RCLASS_IV_TBL(clone) = 0;
315  }
316  if (RCLASS_CONST_TBL(clone)) {
318  RCLASS_CONST_TBL(clone) = 0;
319  }
320  if (RCLASS_M_TBL_WRAPPER(clone)) {
322  RCLASS_M_TBL_WRAPPER(clone) = 0;
323  }
324  if (RCLASS_IV_TBL(orig)) {
325  st_data_t id;
326 
327  RCLASS_IV_TBL(clone) = rb_st_copy(clone, RCLASS_IV_TBL(orig));
328  CONST_ID(id, "__tmp_classpath__");
329  st_delete(RCLASS_IV_TBL(clone), &id, 0);
330  CONST_ID(id, "__classpath__");
331  st_delete(RCLASS_IV_TBL(clone), &id, 0);
332  CONST_ID(id, "__classid__");
333  st_delete(RCLASS_IV_TBL(clone), &id, 0);
334  }
335  if (RCLASS_CONST_TBL(orig)) {
336  struct clone_const_arg arg;
337 
339  arg.klass = clone;
340  arg.tbl = RCLASS_CONST_TBL(clone);
342  }
343  if (RCLASS_M_TBL(orig)) {
344  RCLASS_M_TBL_INIT(clone);
346  }
347 
348  return clone;
349 }
350 
351 VALUE
353 {
355 }
356 
357 VALUE
359 {
360  VALUE klass = RBASIC(obj)->klass;
361 
362  if (!FL_TEST(klass, FL_SINGLETON))
363  return klass;
364  else {
365  /* copy singleton(unnamed) class */
366  VALUE clone = class_alloc(RBASIC(klass)->flags, 0);
367 
368  if (BUILTIN_TYPE(obj) == T_CLASS) {
369  RBASIC_SET_CLASS(clone, clone);
370  }
371  else {
373  }
374 
376  RCLASS_EXT(clone)->allocator = RCLASS_EXT(klass)->allocator;
377  if (RCLASS_IV_TBL(klass)) {
378  RCLASS_IV_TBL(clone) = rb_st_copy(clone, RCLASS_IV_TBL(klass));
379  }
380  if (RCLASS_CONST_TBL(klass)) {
381  struct clone_const_arg arg;
383  arg.klass = clone;
384  arg.tbl = RCLASS_CONST_TBL(clone);
386  }
387  if (attach != Qundef) {
388  rb_singleton_class_attached(clone, attach);
389  }
390  RCLASS_M_TBL_INIT(clone);
392  rb_singleton_class_attached(RBASIC(clone)->klass, clone);
393  FL_SET(clone, FL_SINGLETON);
394 
395  return clone;
396  }
397 }
398 
403 void
405 {
406  if (FL_TEST(klass, FL_SINGLETON)) {
407  if (!RCLASS_IV_TBL(klass)) {
409  }
411  }
412 }
413 
414 
415 
416 #define METACLASS_OF(k) RBASIC(k)->klass
417 #define SET_METACLASS_OF(k, cls) RBASIC_SET_CLASS(k, cls)
418 
424 #define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
425 
431 #define HAVE_METACLASS_P(k) \
432  (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
433  rb_ivar_get(METACLASS_OF(k), id_attached) == (k))
434 
442 #define ENSURE_EIGENCLASS(klass) \
443  (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
444 
445 
455 static inline VALUE
457 {
458  VALUE super;
459  VALUE metaclass = rb_class_boot(Qundef);
460 
461  FL_SET(metaclass, FL_SINGLETON);
463 
465  SET_METACLASS_OF(klass, metaclass);
466  SET_METACLASS_OF(metaclass, metaclass);
467  }
468  else {
469  VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
470  SET_METACLASS_OF(klass, metaclass);
471  SET_METACLASS_OF(metaclass, ENSURE_EIGENCLASS(tmp));
472  }
473 
474  super = RCLASS_SUPER(klass);
475  while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
476  RCLASS_SET_SUPER(metaclass, super ? ENSURE_EIGENCLASS(super) : rb_cClass);
477 
478  OBJ_INFECT(metaclass, RCLASS_SUPER(metaclass));
479 
480  return metaclass;
481 }
482 
489 static inline VALUE
491 {
492  VALUE orig_class = RBASIC(obj)->klass;
493  VALUE klass = rb_class_boot(orig_class);
494 
496  RBASIC_SET_CLASS(obj, klass);
498 
500  return klass;
501 }
502 
503 
504 static VALUE
505 boot_defclass(const char *name, VALUE super)
506 {
507  VALUE obj = rb_class_boot(super);
508  ID id = rb_intern(name);
509 
510  rb_name_class(obj, id);
511  rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
512  return obj;
513 }
514 
515 void
517 {
518  rb_cBasicObject = boot_defclass("BasicObject", 0);
520  rb_cModule = boot_defclass("Module", rb_cObject);
521  rb_cClass = boot_defclass("Class", rb_cModule);
522 
528 }
529 
530 
541 VALUE
543 {
544  if (BUILTIN_TYPE(obj) == T_CLASS) {
545  return make_metaclass(obj);
546  }
547  else {
548  return make_singleton_class(obj);
549  }
550 }
551 
552 
563 VALUE
565 {
566  VALUE klass;
567 
568  if (!super) super = rb_cObject;
569  klass = rb_class_new(super);
571 
572  return klass;
573 }
574 
575 
584 VALUE
586 {
587  ID inherited;
588  if (!super) super = rb_cObject;
589  CONST_ID(inherited, "inherited");
590  return rb_funcall(super, inherited, 1, klass);
591 }
592 
593 
594 
610 VALUE
611 rb_define_class(const char *name, VALUE super)
612 {
613  VALUE klass;
614  ID id;
615 
616  id = rb_intern(name);
617  if (rb_const_defined(rb_cObject, id)) {
619  if (!RB_TYPE_P(klass, T_CLASS)) {
620  rb_raise(rb_eTypeError, "%s is not a class", name);
621  }
622  if (rb_class_real(RCLASS_SUPER(klass)) != super) {
623  rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
624  }
625  return klass;
626  }
627  if (!super) {
628  rb_warn("no super class for `%s', Object assumed", name);
629  }
630  klass = rb_define_class_id(id, super);
632  rb_name_class(klass, id);
634  rb_class_inherited(super, klass);
635 
636  return klass;
637 }
638 
639 
656 VALUE
657 rb_define_class_under(VALUE outer, const char *name, VALUE super)
658 {
659  return rb_define_class_id_under(outer, rb_intern(name), super);
660 }
661 
662 
679 VALUE
681 {
682  VALUE klass;
683 
684  if (rb_const_defined_at(outer, id)) {
685  klass = rb_const_get_at(outer, id);
686  if (!RB_TYPE_P(klass, T_CLASS)) {
687  rb_raise(rb_eTypeError, "%s is not a class", rb_id2name(id));
688  }
689  if (rb_class_real(RCLASS_SUPER(klass)) != super) {
690  rb_name_error(id, "%s is already defined", rb_id2name(id));
691  }
692  return klass;
693  }
694  if (!super) {
695  rb_warn("no super class for `%s::%s', Object assumed",
696  rb_class2name(outer), rb_id2name(id));
697  }
698  klass = rb_define_class_id(id, super);
700  rb_const_set(outer, id, klass);
701  rb_class_inherited(super, klass);
703 
704  return klass;
705 }
706 
707 VALUE
709 {
711  RCLASS_M_TBL_INIT(mdl);
712  return (VALUE)mdl;
713 }
714 
715 VALUE
717 {
718  VALUE mdl;
719 
720  mdl = rb_module_new();
721  rb_name_class(mdl, id);
722 
723  return mdl;
724 }
725 
726 VALUE
727 rb_define_module(const char *name)
728 {
729  VALUE module;
730  ID id;
731 
732  id = rb_intern(name);
733  if (rb_const_defined(rb_cObject, id)) {
734  module = rb_const_get(rb_cObject, id);
735  if (RB_TYPE_P(module, T_MODULE))
736  return module;
737  rb_raise(rb_eTypeError, "%s is not a module", rb_obj_classname(module));
738  }
739  module = rb_define_module_id(id);
740  rb_vm_add_root_module(id, module);
741  rb_const_set(rb_cObject, id, module);
742 
743  return module;
744 }
745 
746 VALUE
747 rb_define_module_under(VALUE outer, const char *name)
748 {
749  return rb_define_module_id_under(outer, rb_intern(name));
750 }
751 
752 VALUE
754 {
755  VALUE module;
756 
757  if (rb_const_defined_at(outer, id)) {
758  module = rb_const_get_at(outer, id);
759  if (RB_TYPE_P(module, T_MODULE))
760  return module;
761  rb_raise(rb_eTypeError, "%s::%s is not a module",
762  rb_class2name(outer), rb_obj_classname(module));
763  }
764  module = rb_define_module_id(id);
765  rb_const_set(outer, id, module);
766  rb_set_class_path_string(module, outer, rb_id2str(id));
768 
769  return module;
770 }
771 
772 VALUE
774 {
776 
777  if (BUILTIN_TYPE(module) == T_ICLASS) {
778  module = RBASIC(module)->klass;
779  }
780  if (!RCLASS_IV_TBL(module)) {
781  RCLASS_IV_TBL(module) = st_init_numtable();
782  }
783  if (!RCLASS_CONST_TBL(module)) {
785  }
786  RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
788 
791 
792  RCLASS_SET_SUPER(klass, super);
793  if (RB_TYPE_P(module, T_ICLASS)) {
794  RBASIC_SET_CLASS(klass, RBASIC(module)->klass);
795  }
796  else {
797  RBASIC_SET_CLASS(klass, module);
798  }
799  OBJ_INFECT(klass, module);
800  OBJ_INFECT(klass, super);
801 
802  return (VALUE)klass;
803 }
804 
805 static int include_modules_at(const VALUE klass, VALUE c, VALUE module);
806 
807 void
809 {
810  int changed = 0;
811 
813 
814  if (!RB_TYPE_P(module, T_MODULE)) {
815  Check_Type(module, T_MODULE);
816  }
817 
818  OBJ_INFECT(klass, module);
819 
820  changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module);
821  if (changed < 0)
822  rb_raise(rb_eArgError, "cyclic include detected");
823 }
824 
825 static int
827 {
829  return ST_CONTINUE;
830 }
831 
832 static int
834 {
835  VALUE p, iclass;
836  int method_changed = 0, constant_changed = 0;
837  const st_table *const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
838 
839  while (module) {
840  int superclass_seen = FALSE;
841 
842  if (RCLASS_ORIGIN(module) != module)
843  goto skip;
844  if (klass_m_tbl && klass_m_tbl == RCLASS_M_TBL(module))
845  return -1;
846  /* ignore if the module included already in superclasses */
847  for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
848  switch (BUILTIN_TYPE(p)) {
849  case T_ICLASS:
850  if (RCLASS_M_TBL_WRAPPER(p) == RCLASS_M_TBL_WRAPPER(module)) {
851  if (!superclass_seen) {
852  c = p; /* move insertion point */
853  }
854  goto skip;
855  }
856  break;
857  case T_CLASS:
858  superclass_seen = TRUE;
859  break;
860  }
861  }
862  iclass = rb_include_class_new(module, RCLASS_SUPER(c));
863  c = RCLASS_SET_SUPER(c, iclass);
864 
865  if (BUILTIN_TYPE(module) == T_ICLASS) {
867  } else {
868  rb_module_add_to_subclasses_list(module, iclass);
869  }
870 
872  VALUE refined_class =
874 
876  (st_data_t) refined_class);
878  }
879  if (RMODULE_M_TBL(module) && RMODULE_M_TBL(module)->num_entries)
880  method_changed = 1;
881  if (RMODULE_CONST_TBL(module) && RMODULE_CONST_TBL(module)->num_entries)
882  constant_changed = 1;
883  skip:
884  module = RCLASS_SUPER(module);
885  }
886 
887  if (method_changed) rb_clear_method_cache_by_class(klass);
888  if (constant_changed) rb_clear_constant_cache();
889 
890  return method_changed;
891 }
892 
893 static int
895 {
896  rb_method_entry_t *me = (rb_method_entry_t *) value;
897  st_table *tbl = (st_table *) data;
898 
899  if (me->def->type == VM_METHOD_TYPE_REFINED) {
900  if (me->def->body.orig_me) {
901  rb_method_entry_t *orig_me = me->def->body.orig_me, *new_me;
902  me->def->body.orig_me = NULL;
903  new_me = ALLOC(rb_method_entry_t);
904  *new_me = *me;
905  st_add_direct(tbl, key, (st_data_t) new_me);
906  *me = *orig_me;
907  xfree(orig_me);
908  return ST_CONTINUE;
909  }
910  else {
911  st_add_direct(tbl, key, (st_data_t) me);
912  return ST_DELETE;
913  }
914  }
915  else {
916  return ST_CONTINUE;
917  }
918 }
919 
920 void
922 {
924  VALUE origin;
925  int changed = 0;
926 
928 
929  Check_Type(module, T_MODULE);
930 
931  OBJ_INFECT(klass, module);
932 
933  origin = RCLASS_ORIGIN(klass);
934  if (origin == klass) {
935  origin = class_alloc(T_ICLASS, klass);
936  OBJ_WB_UNPROTECT(origin); /* TODO: conservertive shading. Need more survery. */
938  RCLASS_SET_SUPER(klass, origin);
944  }
945  changed = include_modules_at(klass, klass, module);
946  if (changed < 0)
947  rb_raise(rb_eArgError, "cyclic prepend detected");
948  if (changed) {
950  }
951 }
952 
953 /*
954  * call-seq:
955  * mod.included_modules -> array
956  *
957  * Returns the list of modules included in <i>mod</i>.
958  *
959  * module Mixin
960  * end
961  *
962  * module Outer
963  * include Mixin
964  * end
965  *
966  * Mixin.included_modules #=> []
967  * Outer.included_modules #=> [Mixin]
968  */
969 
970 VALUE
972 {
973  VALUE ary = rb_ary_new();
974  VALUE p;
975  VALUE origin = RCLASS_ORIGIN(mod);
976 
977  for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
978  if (p != origin && BUILTIN_TYPE(p) == T_ICLASS) {
979  VALUE m = RBASIC(p)->klass;
980  if (RB_TYPE_P(m, T_MODULE))
981  rb_ary_push(ary, m);
982  }
983  }
984  return ary;
985 }
986 
987 /*
988  * call-seq:
989  * mod.include?(module) -> true or false
990  *
991  * Returns <code>true</code> if <i>module</i> is included in
992  * <i>mod</i> or one of <i>mod</i>'s ancestors.
993  *
994  * module A
995  * end
996  * class B
997  * include A
998  * end
999  * class C < B
1000  * end
1001  * B.include?(A) #=> true
1002  * C.include?(A) #=> true
1003  * A.include?(A) #=> false
1004  */
1005 
1006 VALUE
1008 {
1009  VALUE p;
1010 
1011  Check_Type(mod2, T_MODULE);
1012  for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
1013  if (BUILTIN_TYPE(p) == T_ICLASS) {
1014  if (RBASIC(p)->klass == mod2) return Qtrue;
1015  }
1016  }
1017  return Qfalse;
1018 }
1019 
1020 /*
1021  * call-seq:
1022  * mod.ancestors -> array
1023  *
1024  * Returns a list of modules included in <i>mod</i> (including
1025  * <i>mod</i> itself).
1026  *
1027  * module Mod
1028  * include Math
1029  * include Comparable
1030  * end
1031  *
1032  * Mod.ancestors #=> [Mod, Comparable, Math]
1033  * Math.ancestors #=> [Math]
1034  */
1035 
1036 VALUE
1038 {
1039  VALUE p, ary = rb_ary_new();
1040 
1041  for (p = mod; p; p = RCLASS_SUPER(p)) {
1042  if (BUILTIN_TYPE(p) == T_ICLASS) {
1043  rb_ary_push(ary, RBASIC(p)->klass);
1044  }
1045  else if (p == RCLASS_ORIGIN(p)) {
1046  rb_ary_push(ary, p);
1047  }
1048  }
1049  return ary;
1050 }
1051 
1052 #define VISI(x) ((x)&NOEX_MASK)
1053 #define VISI_CHECK(x,f) (VISI(x) == (f))
1054 
1055 static int
1056 ins_methods_push(ID name, long type, VALUE ary, long visi)
1057 {
1058  if (type == -1) return ST_CONTINUE;
1059 
1060  switch (visi) {
1061  case NOEX_PRIVATE:
1062  case NOEX_PROTECTED:
1063  case NOEX_PUBLIC:
1064  visi = (type == visi);
1065  break;
1066  default:
1067  visi = (type != NOEX_PRIVATE);
1068  break;
1069  }
1070  if (visi) {
1071  rb_ary_push(ary, ID2SYM(name));
1072  }
1073  return ST_CONTINUE;
1074 }
1075 
1076 static int
1078 {
1079  return ins_methods_push((ID)name, (long)type, (VALUE)ary, -1); /* everything but private */
1080 }
1081 
1082 static int
1084 {
1085  return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PROTECTED);
1086 }
1087 
1088 static int
1090 {
1091  return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PRIVATE);
1092 }
1093 
1094 static int
1096 {
1097  return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PUBLIC);
1098 }
1099 
1102  int recur;
1103 };
1104 
1105 static int
1107 {
1108  const rb_method_entry_t *me = (const rb_method_entry_t *)value;
1109  struct method_entry_arg *arg = (struct method_entry_arg *)data;
1110  long type;
1111 
1112  if (me && me->def->type == VM_METHOD_TYPE_REFINED) {
1113  VALUE klass = me->klass;
1114  me = rb_resolve_refined_method(Qnil, me, NULL);
1115  if (!me) return ST_CONTINUE;
1116  if (!arg->recur && me->klass != klass) return ST_CONTINUE;
1117  }
1118  if (!st_lookup(arg->list, key, 0)) {
1119  if (UNDEFINED_METHOD_ENTRY_P(me)) {
1120  type = -1; /* none */
1121  }
1122  else {
1123  type = VISI(me->flag);
1124  }
1125  st_add_direct(arg->list, key, type);
1126  }
1127  return ST_CONTINUE;
1128 }
1129 
1130 static VALUE
1132 {
1133  VALUE ary;
1134  int recur, prepended = 0;
1135  struct method_entry_arg me_arg;
1136 
1137  if (argc == 0) {
1138  recur = TRUE;
1139  }
1140  else {
1141  VALUE r;
1142  rb_scan_args(argc, argv, "01", &r);
1143  recur = RTEST(r);
1144  }
1145 
1146  if (!recur && RCLASS_ORIGIN(mod) != mod) {
1147  mod = RCLASS_ORIGIN(mod);
1148  prepended = 1;
1149  }
1150 
1151  me_arg.list = st_init_numtable();
1152  me_arg.recur = recur;
1153  for (; mod; mod = RCLASS_SUPER(mod)) {
1155  if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue;
1156  if (obj && FL_TEST(mod, FL_SINGLETON)) continue;
1157  if (!recur) break;
1158  }
1159  ary = rb_ary_new();
1160  st_foreach(me_arg.list, func, ary);
1161  st_free_table(me_arg.list);
1162 
1163  return ary;
1164 }
1165 
1166 /*
1167  * call-seq:
1168  * mod.instance_methods(include_super=true) -> array
1169  *
1170  * Returns an array containing the names of the public and protected instance
1171  * methods in the receiver. For a module, these are the public and protected methods;
1172  * for a class, they are the instance (not singleton) methods. With no
1173  * argument, or with an argument that is <code>false</code>, the
1174  * instance methods in <i>mod</i> are returned, otherwise the methods
1175  * in <i>mod</i> and <i>mod</i>'s superclasses are returned.
1176  *
1177  * module A
1178  * def method1() end
1179  * end
1180  * class B
1181  * def method2() end
1182  * end
1183  * class C < B
1184  * def method3() end
1185  * end
1186  *
1187  * A.instance_methods #=> [:method1]
1188  * B.instance_methods(false) #=> [:method2]
1189  * C.instance_methods(false) #=> [:method3]
1190  * C.instance_methods(true).length #=> 43
1191  */
1192 
1193 VALUE
1195 {
1197 }
1198 
1199 /*
1200  * call-seq:
1201  * mod.protected_instance_methods(include_super=true) -> array
1202  *
1203  * Returns a list of the protected instance methods defined in
1204  * <i>mod</i>. If the optional parameter is not <code>false</code>, the
1205  * methods of any ancestors are included.
1206  */
1207 
1208 VALUE
1210 {
1212 }
1213 
1214 /*
1215  * call-seq:
1216  * mod.private_instance_methods(include_super=true) -> array
1217  *
1218  * Returns a list of the private instance methods defined in
1219  * <i>mod</i>. If the optional parameter is not <code>false</code>, the
1220  * methods of any ancestors are included.
1221  *
1222  * module Mod
1223  * def method1() end
1224  * private :method1
1225  * def method2() end
1226  * end
1227  * Mod.instance_methods #=> [:method2]
1228  * Mod.private_instance_methods #=> [:method1]
1229  */
1230 
1231 VALUE
1233 {
1235 }
1236 
1237 /*
1238  * call-seq:
1239  * mod.public_instance_methods(include_super=true) -> array
1240  *
1241  * Returns a list of the public instance methods defined in <i>mod</i>.
1242  * If the optional parameter is not <code>false</code>, the methods of
1243  * any ancestors are included.
1244  */
1245 
1246 VALUE
1248 {
1250 }
1251 
1252 /*
1253  * call-seq:
1254  * obj.methods(regular=true) -> array
1255  *
1256  * Returns a list of the names of public and protected methods of
1257  * <i>obj</i>. This will include all the methods accessible in
1258  * <i>obj</i>'s ancestors.
1259  * If the <i>regular</i> parameter is set to <code>false</code>,
1260  * Returns an array of obj's public and protected singleton methods,
1261  * the array will not include methods in modules included in <i>obj</i>.
1262  *
1263  * class Klass
1264  * def klass_method()
1265  * end
1266  * end
1267  * k = Klass.new
1268  * k.methods[0..9] #=> [:klass_method, :nil?, :===,
1269  * # :==~, :!, :eql?
1270  * # :hash, :<=>, :class, :singleton_class]
1271  * k.methods.length #=> 57
1272  *
1273  * k.methods(false) #=> []
1274  * def k.singleton_method; end
1275  * k.methods(false) #=> [:singleton_method]
1276  *
1277  * module M123; def m123; end end
1278  * k.extend M123
1279  * k.methods(false) #=> [:singleton_method]
1280  */
1281 
1282 VALUE
1284 {
1285  retry:
1286  if (argc == 0) {
1288  }
1289  else {
1290  VALUE recur;
1291 
1292  rb_scan_args(argc, argv, "1", &recur);
1293  if (RTEST(recur)) {
1294  argc = 0;
1295  goto retry;
1296  }
1297  return rb_obj_singleton_methods(argc, argv, obj);
1298  }
1299 }
1300 
1301 /*
1302  * call-seq:
1303  * obj.protected_methods(all=true) -> array
1304  *
1305  * Returns the list of protected methods accessible to <i>obj</i>. If
1306  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1307  * in the receiver will be listed.
1308  */
1309 
1310 VALUE
1312 {
1314 }
1315 
1316 /*
1317  * call-seq:
1318  * obj.private_methods(all=true) -> array
1319  *
1320  * Returns the list of private methods accessible to <i>obj</i>. If
1321  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1322  * in the receiver will be listed.
1323  */
1324 
1325 VALUE
1327 {
1329 }
1330 
1331 /*
1332  * call-seq:
1333  * obj.public_methods(all=true) -> array
1334  *
1335  * Returns the list of public methods accessible to <i>obj</i>. If
1336  * the <i>all</i> parameter is set to <code>false</code>, only those methods
1337  * in the receiver will be listed.
1338  */
1339 
1340 VALUE
1342 {
1344 }
1345 
1346 /*
1347  * call-seq:
1348  * obj.singleton_methods(all=true) -> array
1349  *
1350  * Returns an array of the names of singleton methods for <i>obj</i>.
1351  * If the optional <i>all</i> parameter is true, the list will include
1352  * methods in modules included in <i>obj</i>.
1353  * Only public and protected singleton methods are returned.
1354  *
1355  * module Other
1356  * def three() end
1357  * end
1358  *
1359  * class Single
1360  * def Single.four() end
1361  * end
1362  *
1363  * a = Single.new
1364  *
1365  * def a.one()
1366  * end
1367  *
1368  * class << a
1369  * include Other
1370  * def two()
1371  * end
1372  * end
1373  *
1374  * Single.singleton_methods #=> [:four]
1375  * a.singleton_methods(false) #=> [:two, :one]
1376  * a.singleton_methods #=> [:two, :one, :three]
1377  */
1378 
1379 VALUE
1381 {
1382  VALUE recur, ary, klass, origin;
1383  struct method_entry_arg me_arg;
1384  st_table *mtbl;
1385 
1386  if (argc == 0) {
1387  recur = Qtrue;
1388  }
1389  else {
1390  rb_scan_args(argc, argv, "01", &recur);
1391  }
1392  klass = CLASS_OF(obj);
1393  origin = RCLASS_ORIGIN(klass);
1394  me_arg.list = st_init_numtable();
1395  me_arg.recur = recur;
1396  if (klass && FL_TEST(klass, FL_SINGLETON)) {
1397  if ((mtbl = RCLASS_M_TBL(origin)) != 0)
1398  st_foreach(mtbl, method_entry_i, (st_data_t)&me_arg);
1399  klass = RCLASS_SUPER(klass);
1400  }
1401  if (RTEST(recur)) {
1402  while (klass && (FL_TEST(klass, FL_SINGLETON) || RB_TYPE_P(klass, T_ICLASS))) {
1403  if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0)
1404  st_foreach(mtbl, method_entry_i, (st_data_t)&me_arg);
1405  klass = RCLASS_SUPER(klass);
1406  }
1407  }
1408  ary = rb_ary_new();
1409  st_foreach(me_arg.list, ins_methods_i, ary);
1410  st_free_table(me_arg.list);
1411 
1412  return ary;
1413 }
1414 
1472 void
1474 {
1475  rb_add_method_cfunc(klass, mid, func, argc, NOEX_PUBLIC);
1476 }
1477 
1478 void
1479 rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1480 {
1482 }
1483 
1484 void
1486 {
1488 }
1489 
1490 void
1492 {
1494 }
1495 
1496 void
1497 rb_undef_method(VALUE klass, const char *name)
1498 {
1500 }
1501 
1510 #define SPECIAL_SINGLETON(x,c) do {\
1511  if (obj == (x)) {\
1512  return (c);\
1513  }\
1514 } while (0)
1515 
1516 static inline VALUE
1518 {
1522  return Qnil;
1523 }
1524 
1525 VALUE
1527 {
1528  return special_singleton_class_of(obj);
1529 }
1530 
1540 static VALUE
1542 {
1543  VALUE klass;
1544 
1545  if (FIXNUM_P(obj) || FLONUM_P(obj) || SYMBOL_P(obj)) {
1546  rb_raise(rb_eTypeError, "can't define singleton");
1547  }
1548  if (SPECIAL_CONST_P(obj)) {
1549  klass = special_singleton_class_of(obj);
1550  if (NIL_P(klass))
1551  rb_bug("unknown immediate %p", (void *)obj);
1552  return klass;
1553  }
1554  else {
1555  enum ruby_value_type type = BUILTIN_TYPE(obj);
1556  if (type == T_FLOAT || type == T_BIGNUM) {
1557  rb_raise(rb_eTypeError, "can't define singleton");
1558  }
1559  }
1560 
1561  if (FL_TEST(RBASIC(obj)->klass, FL_SINGLETON) &&
1562  rb_ivar_get(RBASIC(obj)->klass, id_attached) == obj) {
1563  klass = RBASIC(obj)->klass;
1564  }
1565  else {
1566  klass = rb_make_metaclass(obj, RBASIC(obj)->klass);
1567  }
1568 
1569  if (OBJ_TAINTED(obj)) {
1570  OBJ_TAINT(klass);
1571  }
1572  else {
1573  FL_UNSET(klass, FL_TAINT);
1574  }
1575  if (OBJ_FROZEN(obj)) OBJ_FREEZE(klass);
1576 
1577  return klass;
1578 }
1579 
1587 VALUE
1589 {
1590  VALUE klass;
1591 
1592  if (SPECIAL_CONST_P(obj)) {
1593  return rb_special_singleton_class(obj);
1594  }
1595  klass = RBASIC(obj)->klass;
1596  if (!FL_TEST(klass, FL_SINGLETON)) return Qnil;
1597  if (rb_ivar_get(klass, id_attached) != obj) return Qnil;
1598  return klass;
1599 }
1600 
1618 VALUE
1620 {
1621  VALUE klass = singleton_class_of(obj);
1622 
1623  /* ensures an exposed class belongs to its own eigenclass */
1624  if (RB_TYPE_P(obj, T_CLASS)) (void)ENSURE_EIGENCLASS(klass);
1625 
1626  return klass;
1627 }
1628 
1645 void
1647 {
1649 }
1650 
1651 
1652 
1660 void
1662 {
1665 }
1666 
1667 
1674 void
1676 {
1678 }
1679 
1680 
1687 void
1688 rb_define_alias(VALUE klass, const char *name1, const char *name2)
1689 {
1690  rb_alias(klass, rb_intern(name1), rb_intern(name2));
1691 }
1692 
1700 void
1701 rb_define_attr(VALUE klass, const char *name, int read, int write)
1702 {
1703  rb_attr(klass, rb_intern(name), read, write, FALSE);
1704 }
1705 
1706 int
1708 {
1709  const rb_method_entry_t *me = rb_method_entry(CLASS_OF(obj), rb_intern("to_s"), 0);
1710  if (me && me->def && me->def->type == VM_METHOD_TYPE_CFUNC &&
1711  me->def->body.cfunc.func == rb_any_to_s)
1712  return 1;
1713  return 0;
1714 }
1715 
1716 #include <stdarg.h>
1717 
1718 int
1719 rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
1720 {
1721  int i;
1722  const char *p = fmt;
1723  VALUE *var;
1724  va_list vargs;
1725  int f_var = 0, f_hash = 0, f_block = 0;
1726  int n_lead = 0, n_opt = 0, n_trail = 0, n_mand;
1727  int argi = 0;
1728  VALUE hash = Qnil;
1729 
1730  if (ISDIGIT(*p)) {
1731  n_lead = *p - '0';
1732  p++;
1733  if (ISDIGIT(*p)) {
1734  n_opt = *p - '0';
1735  p++;
1736  if (ISDIGIT(*p)) {
1737  n_trail = *p - '0';
1738  p++;
1739  goto block_arg;
1740  }
1741  }
1742  }
1743  if (*p == '*') {
1744  f_var = 1;
1745  p++;
1746  if (ISDIGIT(*p)) {
1747  n_trail = *p - '0';
1748  p++;
1749  }
1750  }
1751  block_arg:
1752  if (*p == ':') {
1753  f_hash = 1;
1754  p++;
1755  }
1756  if (*p == '&') {
1757  f_block = 1;
1758  p++;
1759  }
1760  if (*p != '\0') {
1761  rb_fatal("bad scan arg format: %s", fmt);
1762  }
1763  n_mand = n_lead + n_trail;
1764 
1765  if (argc < n_mand)
1766  goto argc_error;
1767 
1768  va_start(vargs, fmt);
1769 
1770  /* capture an option hash - phase 1: pop */
1771  if (f_hash && n_mand < argc) {
1772  VALUE last = argv[argc - 1];
1773 
1774  if (NIL_P(last)) {
1775  /* nil is taken as an empty option hash only if it is not
1776  ambiguous; i.e. '*' is not specified and arguments are
1777  given more than sufficient */
1778  if (!f_var && n_mand + n_opt < argc)
1779  argc--;
1780  }
1781  else {
1783  if (!NIL_P(hash)) {
1784  VALUE opts = rb_extract_keywords(&hash);
1785  if (!hash) argc--;
1786  hash = opts ? opts : Qnil;
1787  }
1788  }
1789  }
1790  /* capture leading mandatory arguments */
1791  for (i = n_lead; i-- > 0; ) {
1792  var = va_arg(vargs, VALUE *);
1793  if (var) *var = argv[argi];
1794  argi++;
1795  }
1796  /* capture optional arguments */
1797  for (i = n_opt; i-- > 0; ) {
1798  var = va_arg(vargs, VALUE *);
1799  if (argi < argc - n_trail) {
1800  if (var) *var = argv[argi];
1801  argi++;
1802  }
1803  else {
1804  if (var) *var = Qnil;
1805  }
1806  }
1807  /* capture variable length arguments */
1808  if (f_var) {
1809  int n_var = argc - argi - n_trail;
1810 
1811  var = va_arg(vargs, VALUE *);
1812  if (0 < n_var) {
1813  if (var) *var = rb_ary_new4(n_var, &argv[argi]);
1814  argi += n_var;
1815  }
1816  else {
1817  if (var) *var = rb_ary_new();
1818  }
1819  }
1820  /* capture trailing mandatory arguments */
1821  for (i = n_trail; i-- > 0; ) {
1822  var = va_arg(vargs, VALUE *);
1823  if (var) *var = argv[argi];
1824  argi++;
1825  }
1826  /* capture an option hash - phase 2: assignment */
1827  if (f_hash) {
1828  var = va_arg(vargs, VALUE *);
1829  if (var) *var = hash;
1830  }
1831  /* capture iterator block */
1832  if (f_block) {
1833  var = va_arg(vargs, VALUE *);
1834  if (rb_block_given_p()) {
1835  *var = rb_block_proc();
1836  }
1837  else {
1838  *var = Qnil;
1839  }
1840  }
1841  va_end(vargs);
1842 
1843  if (argi < argc) {
1844  argc_error:
1845  rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
1846  }
1847 
1848  return argc;
1849 }
1850 
1851 NORETURN(static void keyword_error(const char *error, VALUE keys));
1852 static void
1853 keyword_error(const char *error, VALUE keys)
1854 {
1855  const char *msg = "";
1856  if (RARRAY_LEN(keys) == 1) {
1857  keys = RARRAY_AREF(keys, 0);
1858  }
1859  else {
1860  keys = rb_ary_join(keys, rb_usascii_str_new2(", "));
1861  msg = "s";
1862  }
1863  rb_raise(rb_eArgError, "%s keyword%s: %"PRIsVALUE, error, msg, keys);
1864 }
1865 
1866 NORETURN(static void unknown_keyword_error(VALUE hash, const ID *table, int keywords));
1867 static void
1868 unknown_keyword_error(VALUE hash, const ID *table, int keywords)
1869 {
1870  st_table *tbl = rb_hash_tbl_raw(hash);
1871  VALUE keys;
1872  int i;
1873  for (i = 0; i < keywords; i++) {
1874  st_data_t key = ID2SYM(table[i]);
1875  st_delete(tbl, &key, NULL);
1876  }
1877  keys = rb_funcall(hash, rb_intern("keys"), 0, 0);
1878  if (!RB_TYPE_P(keys, T_ARRAY)) rb_raise(rb_eArgError, "unknown keyword");
1879  keyword_error("unknown", keys);
1880 }
1881 
1882 static int
1884 {
1885  VALUE *kwdhash = (VALUE *)arg;
1886 
1887  if (!SYMBOL_P(key)) kwdhash++;
1888  if (!*kwdhash) *kwdhash = rb_hash_new();
1889  rb_hash_aset(*kwdhash, (VALUE)key, (VALUE)value);
1890  return ST_CONTINUE;
1891 }
1892 
1893 VALUE
1895 {
1896  VALUE parthash[2] = {0, 0};
1897  VALUE hash = *orighash;
1898 
1899  if (RHASH_EMPTY_P(hash)) {
1900  *orighash = 0;
1901  return hash;
1902  }
1904  *orighash = parthash[1];
1905  return parthash[0];
1906 }
1907 
1908 int
1909 rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
1910 {
1911  int i = 0, j;
1912  int rest = 0;
1913  VALUE missing = Qnil;
1914  st_data_t key;
1915 
1916 #define extract_kwarg(keyword, val) \
1917  (key = (st_data_t)(keyword), values ? \
1918  st_delete(rb_hash_tbl_raw(keyword_hash), &key, (val)) : \
1919  st_lookup(rb_hash_tbl_raw(keyword_hash), key, (val)))
1920 
1921  if (optional < 0) {
1922  rest = 1;
1923  optional = -1-optional;
1924  }
1925  if (values) {
1926  for (j = 0; j < required + optional; j++) {
1927  values[j] = Qundef;
1928  }
1929  }
1930  if (required) {
1931  for (; i < required; i++) {
1932  VALUE keyword = ID2SYM(table[i]);
1933  if (keyword_hash) {
1934  st_data_t val;
1935  if (extract_kwarg(keyword, &val)) {
1936  if (values) values[i] = (VALUE)val;
1937  continue;
1938  }
1939  }
1940  if (NIL_P(missing)) missing = rb_ary_tmp_new(1);
1941  rb_ary_push(missing, keyword);
1942  }
1943  if (!NIL_P(missing)) {
1944  keyword_error("missing", missing);
1945  }
1946  }
1947  j = i;
1948  if (optional && keyword_hash) {
1949  for (i = 0; i < optional; i++) {
1950  st_data_t val;
1951  if (extract_kwarg(ID2SYM(table[required+i]), &val)) {
1952  if (values) values[required+i] = (VALUE)val;
1953  j++;
1954  }
1955  }
1956  }
1957  if (!rest && keyword_hash) {
1958  if (RHASH_SIZE(keyword_hash) > (unsigned int)j) {
1959  unknown_keyword_error(keyword_hash, table, required+optional);
1960  }
1961  }
1962  return j;
1963 #undef extract_kwarg
1964 }
1965 
static VALUE make_metaclass(VALUE klass)
Creates a metaclass of klass.
Definition: class.c:456
st_table * list
Definition: class.c:1101
static void rb_module_add_to_subclasses_list(VALUE module, VALUE iclass)
Definition: class.c:61
int rb_st_insert_id_and_value(VALUE obj, st_table *tbl, ID key, VALUE value)
Definition: variable.c:2621
void rb_class_remove_from_super_subclasses(VALUE klass)
Definition: class.c:80
#define ISDIGIT(c)
Definition: ruby.h:1783
#define UNDEFINED_METHOD_ENTRY_P(me)
Definition: method.h:110
Definition: st.h:100
void rb_class_detach_subclasses(VALUE klass)
Definition: class.c:131
VALUE rb_define_module_id_under(VALUE outer, ID id)
Definition: class.c:753
void rb_vm_check_redefinition_by_prepend(VALUE klass)
Definition: vm.c:1211
NODE *const cref_stack
Definition: vm_core.h:315
RUBY_EXTERN VALUE rb_cFalseClass
Definition: ruby.h:1569
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Definition: class.c:1007
#define RARRAY_LEN(a)
Definition: ruby.h:878
void rb_bug(const char *fmt,...)
Definition: error.c:327
rb_method_type_t type
Definition: method.h:79
int rb_vm_add_root_module(ID id, VALUE module)
Definition: vm.c:1810
#define FALSE
Definition: nkf.h:174
void rb_check_inheritable(VALUE super)
Ensures a class can be derived from super.
Definition: class.c:206
static int ins_methods_push(ID name, long type, VALUE ary, long visi)
Definition: class.c:1056
#define RCLASS_CONST_TBL(c)
Definition: internal.h:293
Definition: constant.h:19
Definition: st.h:69
VALUE rb_obj_private_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1326
VALUE rb_id2str(ID id)
Definition: ripper.c:17201
#define VISI(x)
Definition: class.c:1052
#define RB_OBJ_WRITTEN(a, oldv, b)
Definition: ruby.h:1222
rb_subclass_entry_t * next
Definition: internal.h:250
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1646
void rb_class_foreach_subclass(VALUE klass, void(*f)(VALUE))
Definition: class.c:117
rb_method_flag_t flag
Definition: method.h:98
#define rb_usascii_str_new2
Definition: intern.h:846
VALUE rb_class_private_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:1232
#define FL_TAINT
Definition: ruby.h:1137
#define CLASS_OF(v)
Definition: ruby.h:440
#define T_MODULE
Definition: ruby.h:480
VALUE rb_define_class_id_under(VALUE outer, ID id, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:680
const VALUE file
Definition: constant.h:22
#define RCLASS_EXT(c)
Definition: classext.h:15
void rb_class_remove_from_module_subclasses(VALUE klass)
Definition: class.c:98
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Definition: class.c:1909
#define Qtrue
Definition: ruby.h:426
int st_insert(st_table *, st_data_t, st_data_t)
static VALUE class_alloc(VALUE flags, VALUE klass)
Allocates a struct RClass for a new class.
Definition: class.c:155
#define OBJ_INIT_COPY(obj, orig)
Definition: intern.h:287
struct rb_method_entry_struct * orig_me
Definition: method.h:92
VALUE rb_mod_ancestors(VALUE mod)
Definition: class.c:1037
const int id
Definition: nkf.c:209
VALUE rb_refinement_module_get_refined_class(VALUE module)
Definition: eval.c:1190
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1491
VALUE rb_eTypeError
Definition: error.c:548
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:900
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:113
VALUE rb_singleton_class_clone(VALUE obj)
Definition: class.c:352
void st_free_table(st_table *)
Definition: st.c:334
struct st_table * rb_hash_tbl_raw(VALUE hash)
Definition: hash.c:360
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
Definition: class.c:300
VALUE rb_ary_tmp_new(long capa)
Definition: array.c:538
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:781
void Init_class_hierarchy(void)
Definition: class.c:516
#define RBASIC_SET_CLASS(obj, cls)
Definition: internal.h:611
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:657
#define Check_Type(v, t)
Definition: ruby.h:532
static int ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
Definition: class.c:1089
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1857
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1115
Definition: class.c:1100
#define SET_METACLASS_OF(k, cls)
Definition: class.c:417
static int clone_const_i(st_data_t key, st_data_t value, st_data_t data)
Definition: class.c:279
#define RB_GC_GUARD(v)
Definition: ruby.h:523
void rb_clear_constant_cache(void)
Definition: vm_method.c:60
int rb_const_defined(VALUE, ID)
Definition: variable.c:2127
#define RCLASS_M_TBL_WRAPPER(c)
Definition: internal.h:294
const VALUE value
Definition: constant.h:21
union rb_method_definition_struct::@126 body
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:808
#define T_ARRAY
Definition: ruby.h:484
void rb_define_protected_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1485
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1675
unsigned int last
Definition: nkf.c:4310
#define RMODULE_M_TBL(m)
Definition: ruby.h:799
#define FIXNUM_P(f)
Definition: ruby.h:347
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1497
#define OBJ_TAINTED(x)
Definition: ruby.h:1182
const char * rb_obj_classname(VALUE)
Definition: variable.c:406
#define head
Definition: st.c:107
RUBY_SYMBOL_EXPORT_BEGIN typedef unsigned long st_data_t
Definition: st.h:20
void rb_name_error(ID id, const char *fmt,...)
Definition: error.c:967
Definition: node.h:239
#define NEWOBJ_OF(obj, type, klass, flags)
Definition: ruby.h:694
void rb_free_m_tbl_wrapper(struct method_table_wrapper *wrapper)
Definition: gc.c:1450
#define FL_SINGLETON
Definition: ruby.h:1133
void rb_prepend_module(VALUE klass, VALUE module)
Definition: class.c:921
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1619
VALUE rb_extract_keywords(VALUE *orighash)
Definition: class.c:1894
#define RB_TYPE_P(obj, type)
Definition: ruby.h:1672
int st_lookup(st_table *, st_data_t, st_data_t *)
rb_method_cfunc_t cfunc
Definition: method.h:83
#define FL_TEST(x, f)
Definition: ruby.h:1169
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition: class.c:585
NORETURN(static void keyword_error(const char *error, VALUE keys))
Definition: internal.h:248
int rb_block_given_p(void)
Definition: eval.c:712
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1402
#define val
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1561
VALUE rb_special_singleton_class(VALUE obj)
Definition: class.c:1526
#define RMODULE_IS_REFINEMENT
Definition: ruby.h:802
#define RGENGC_WB_PROTECTED_CLASS
Definition: ruby.h:729
void rb_attr(VALUE, ID, int, int, int)
Definition: vm_method.c:872
static VALUE boot_defclass(const char *name, VALUE super)
Definition: class.c:505
VALUE rb_class_real(VALUE)
Definition: object.c:204
RUBY_EXTERN VALUE rb_cBasicObject
Definition: ruby.h:1560
VALUE rb_ary_new(void)
Definition: array.c:499
static void clone_method(VALUE klass, ID mid, const rb_method_entry_t *me)
Definition: class.c:236
#define RMODULE_CONST_TBL(m)
Definition: ruby.h:798
#define OBJ_WB_UNPROTECT(x)
Definition: ruby.h:1199
RUBY_EXTERN VALUE rb_mKernel
Definition: ruby.h:1549
#define RCLASS_ORIGIN(c)
Definition: internal.h:297
#define NIL_P(v)
Definition: ruby.h:438
static VALUE RCLASS_SET_SUPER(VALUE klass, VALUE super)
Definition: internal.h:319
void st_add_direct(st_table *, st_data_t, st_data_t)
Definition: st.c:629
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:611
static char msg[50]
Definition: strerror.c:8
RUBY_EXTERN VALUE rb_cTrueClass
Definition: ruby.h:1596
static VALUE special_singleton_class_of(VALUE obj)
Definition: class.c:1517
int st_delete(st_table *, st_data_t *, st_data_t *)
#define RCLASS_IV_TBL(c)
Definition: internal.h:292
void rb_class_subclass_add(VALUE super, VALUE klass)
Definition: class.c:40
VALUE rb_define_module_id(ID id)
Definition: class.c:716
#define METACLASS_OF(k)
Definition: class.c:416
#define OBJ_FROZEN(x)
Definition: ruby.h:1193
#define FLONUM_P(x)
Definition: ruby.h:367
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
Definition: class.c:424
#define T_FLOAT
Definition: ruby.h:481
int argc
Definition: ruby.c:131
#define Qfalse
Definition: ruby.h:425
Definition: method.h:97
static void RCLASS_M_TBL_INIT(VALUE c)
Definition: internal.h:302
RUBY_EXTERN VALUE rb_cModule
Definition: ruby.h:1580
#define T_BIGNUM
Definition: ruby.h:487
void rb_gc_register_mark_object(VALUE obj)
Definition: gc.c:4923
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1360
VALUE rb_obj_protected_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1311
#define rb_ary_new4
Definition: intern.h:92
#define OBJ_FREEZE(x)
Definition: ruby.h:1194
rb_method_entry_t * rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me, VALUE *defined_class_ptr)
Definition: vm_method.c:660
rb_method_entry_t * rb_method_entry(VALUE klass, ID id, VALUE *define_class_ptr)
Definition: vm_method.c:617
VALUE klass
Definition: method.h:102
#define ALLOC(type)
Definition: ruby.h:1342
int rb_obj_basic_to_s_p(VALUE obj)
Definition: class.c:1707
#define RCLASS_REFINED_CLASS(c)
Definition: internal.h:298
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:1880
#define id_attached
Definition: class.c:37
VALUE rb_iseq_clone(VALUE iseqval, VALUE newcbase)
Definition: iseq.c:1923
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1688
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
Definition: class.c:1661
#define RCLASS_M_TBL(c)
Definition: internal.h:295
static int add_refined_method_entry_i(st_data_t key, st_data_t value, st_data_t data)
Definition: class.c:826
rb_iseq_t *const iseq
Definition: method.h:82
static VALUE class_instance_method_list(int argc, VALUE *argv, VALUE mod, int obj, int(*func)(st_data_t, st_data_t, st_data_t))
Definition: class.c:1131
#define TRUE
Definition: nkf.h:175
VALUE rb_include_class_new(VALUE module, VALUE super)
Definition: class.c:773
void rb_fatal(const char *fmt,...)
Definition: error.c:1911
int recur
Definition: class.c:1102
VALUE klass
Definition: internal.h:249
rb_method_entry_t * rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *option, rb_method_flag_t noex)
Definition: vm_method.c:428
VALUE(* func)(ANYARGS)
Definition: method.h:66
VALUE rb_hash_new(void)
Definition: hash.c:307
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1719
VALUE rb_check_hash_type(VALUE hash)
Definition: hash.c:597
#define PRIsVALUE
Definition: ruby.h:137
unsigned long ID
Definition: ruby.h:89
static int separate_symbol(st_data_t key, st_data_t value, st_data_t arg)
Definition: class.c:1883
#define RHASH_SIZE(h)
Definition: ruby.h:930
static int ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
Definition: class.c:1077
#define Qnil
Definition: ruby.h:427
void rb_clear_method_cache_by_class(VALUE)
Definition: vm_method.c:66
void rb_const_set(VALUE, ID, VALUE)
Definition: variable.c:2163
#define extract_kwarg(keyword, val)
int type
Definition: tcltklib.c:112
static int include_modules_at(const VALUE klass, VALUE c, VALUE module)
Definition: class.c:833
VALUE rb_obj_public_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1341
static int ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
Definition: class.c:1095
#define BUILTIN_TYPE(x)
Definition: ruby.h:502
#define OBJ_TAINT(x)
Definition: ruby.h:1184
unsigned long VALUE
Definition: ruby.h:88
void rb_name_class(VALUE, ID)
Definition: variable.c:385
#define RBASIC(obj)
Definition: ruby.h:1116
const char * rb_class2name(VALUE)
Definition: variable.c:397
VALUE rb_make_metaclass(VALUE obj, VALUE unused)
Definition: class.c:542
void rb_alias(VALUE, ID, ID)
Definition: vm_method.c:1255
static int clone_const(ID key, const rb_const_entry_t *ce, struct clone_const_arg *arg)
Definition: class.c:267
st_table * tbl
Definition: class.c:263
RUBY_EXTERN VALUE rb_cClass
Definition: ruby.h:1565
st_table * st_init_numtable(void)
Definition: st.c:272
void rb_class_detach_module_subclasses(VALUE klass)
Definition: class.c:137
ruby_value_type
Definition: ruby.h:442
VALUE rb_class_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:1194
#define FL_UNSET(x, f)
Definition: ruby.h:1177
void rb_free_const_table(st_table *tbl)
Definition: gc.c:1466
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:747
#define recur(fmt)
VALUE rb_class_protected_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:1209
int rb_const_defined_at(VALUE, ID)
Definition: variable.c:2133
void rb_define_method_id(VALUE klass, ID mid, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1473
VALUE rb_class_public_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:1247
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attach a object to a singleton class.
Definition: class.c:404
#define f
static void unknown_keyword_error(VALUE hash, const ID *table, int keywords)
Definition: class.c:1868
#define UNLIMITED_ARGUMENTS
Definition: intern.h:44
#define RCLASS_SUPER(c)
Definition: classext.h:16
VALUE rb_module_new(void)
Definition: class.c:708
#define RARRAY_AREF(a, i)
Definition: ruby.h:901
#define ENSURE_EIGENCLASS(klass)
ensures klass belongs to its own eigenclass.
Definition: class.c:442
VALUE rb_block_proc(void)
Definition: proc.c:620
#define xmalloc
Definition: defines.h:108
VALUE rb_obj_singleton_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1380
rb_method_definition_t * def
Definition: method.h:100
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines (a) public accessor method(s) for an attribute.
Definition: class.c:1701
#define ANYARGS
Definition: defines.h:98
void rb_error_arity(int argc, int min, int max)
#define RCLASS_IV_INDEX_TBL(c)
Definition: internal.h:296
#define FL_WB_PROTECTED
Definition: ruby.h:1134
uint8_t key[16]
Definition: random.c:1250
static VALUE make_singleton_class(VALUE obj)
Creates a singleton class for obj.
Definition: class.c:490
VALUE rb_singleton_class_get(VALUE obj)
Returns the singleton class of obj, or nil if obj is not a singleton object.
Definition: class.c:1588
VALUE rb_any_to_s(VALUE)
Definition: object.c:452
#define RTEST(v)
Definition: ruby.h:437
static int method_entry_i(st_data_t key, st_data_t value, st_data_t data)
Definition: class.c:1106
st_table * rb_st_copy(VALUE obj, struct st_table *orig_tbl)
Definition: variable.c:2636
#define OBJ_INFECT(x, s)
Definition: ruby.h:1188
VALUE rb_obj_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1283
void rb_add_refined_method_entry(VALUE refined_class, ID mid)
Definition: vm_method.c:221
VALUE klass
Definition: class.c:262
static int ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
Definition: class.c:1083
static unsigned int hash(const char *str, unsigned int len)
Definition: lex.c:56
VALUE rb_ary_join(VALUE ary, VALUE sep)
Definition: array.c:2006
#define T_CLASS
Definition: ruby.h:478
static int move_refined_method(st_data_t key, st_data_t value, st_data_t data)
Definition: class.c:894
VALUE rb_const_get_at(VALUE, ID)
Definition: variable.c:1886
void rb_frozen_class_p(VALUE klass)
Definition: eval.c:406
const char * name
Definition: nkf.c:208
static void class_init_copy_check(VALUE clone, VALUE orig)
Definition: class.c:285
#define FL_SET(x, f)
Definition: ruby.h:1175
VALUE self
Definition: vm_core.h:303
#define ID2SYM(x)
Definition: ruby.h:355
#define GetISeqPtr(obj, ptr)
Definition: vm_core.h:193
#define RMODULE_INCLUDED_INTO_REFINEMENT
Definition: ruby.h:803
const char * rb_id2name(ID id)
Definition: ripper.c:17271
VALUE rb_mod_included_modules(VALUE mod)
Definition: class.c:971
VALUE rb_define_class_id(ID id, VALUE super)
Defines a new class.
Definition: class.c:564
rb_serial_t rb_next_class_serial(void)
Definition: vm.c:92
#define SPECIAL_SINGLETON(x, c)
Definition: class.c:1510
rb_method_entry_t * rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *, rb_method_flag_t noex)
Definition: vm_method.c:504
#define CONST_ID(var, str)
Definition: ruby.h:1436
#define SPECIAL_CONST_P(x)
Definition: ruby.h:1165
void void xfree(void *)
#define RHASH_EMPTY_P(h)
Definition: ruby.h:931
VALUE rb_define_module(const char *name)
Definition: class.c:727
#define rb_intern(str)
#define SYMBOL_P(x)
Definition: ruby.h:354
#define mod(x, y)
Definition: date_strftime.c:28
static void keyword_error(const char *error, VALUE keys)
Definition: class.c:1853
#define NULL
Definition: _sdbm.c:102
static VALUE singleton_class_of(VALUE obj)
Definition: class.c:1541
Definition: ruby.h:790
static int clone_method_i(st_data_t key, st_data_t value, st_data_t data)
Definition: class.c:255
#define Qundef
Definition: ruby.h:428
#define T_ICLASS
Definition: ruby.h:479
#define RCLASS_SERIAL(c)
Definition: internal.h:299
void rb_add_method_cfunc(VALUE klass, ID mid, VALUE(*func)(ANYARGS), int argc, rb_method_flag_t noex)
Definition: vm_method.c:99
VALUE rb_class_new(VALUE super)
Creates a new class.
Definition: class.c:228
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1479
int st_foreach(st_table *, int(*)(ANYARGS), st_data_t)
Definition: st.c:1034
void rb_warn(const char *fmt,...)
Definition: error.c:223
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
Definition: class.c:187
RUBY_EXTERN VALUE rb_cNilClass
Definition: ruby.h:1582
VALUE rb_eArgError
Definition: error.c:549
#define T_MASK
Definition: md5.c:131
void rb_set_class_path_string(VALUE, VALUE, VALUE)
Definition: variable.c:293
void rb_vm_rewrite_cref_stack(NODE *node, VALUE old_klass, VALUE new_klass, NODE **new_cref_ptr)
#define RB_OBJ_WRITE(a, slot, b)
Definition: ruby.h:1221
char ** argv
Definition: ruby.c:132
VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
Definition: class.c:358