Ruby 2.0.0p353(2013-11-22revision43784)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

class.c
Go to the documentation of this file.
1 /**********************************************************************
2
3 class.c -
4
5 $Author: nagachika $
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 extern st_table *rb_class_tbl;
35 static ID id_attached;
36
49 static VALUE
50 class_alloc(VALUE flags, VALUE klass)
51 {
52 NEWOBJ_OF(obj, struct RClass, klass, flags);
53 obj->ptr = ALLOC(rb_classext_t);
54 RCLASS_IV_TBL(obj) = 0;
55 RCLASS_CONST_TBL(obj) = 0;
56 RCLASS_M_TBL(obj) = 0;
57 RCLASS_SUPER(obj) = 0;
58 RCLASS_ORIGIN(obj) = (VALUE)obj;
59 RCLASS_IV_INDEX_TBL(obj) = 0;
60 RCLASS_REFINED_CLASS(obj) = Qnil;
61 RCLASS_EXT(obj)->allocator = 0;
62 return (VALUE)obj;
63 }
64
65
75 VALUE
76 rb_class_boot(VALUE super)
77 {
78 VALUE klass = class_alloc(T_CLASS, rb_cClass);
79
80 RCLASS_SUPER(klass) = super;
81 RCLASS_M_TBL(klass) = st_init_numtable();
82
83 OBJ_INFECT(klass, super);
84 return (VALUE)klass;
85 }
86
87
94 void
95 rb_check_inheritable(VALUE super)
96 {
97 if (!RB_TYPE_P(super, T_CLASS)) {
98 rb_raise(rb_eTypeError, "superclass must be a Class (%s given)",
99 rb_obj_classname(super));
100 }
101 if (RBASIC(super)->flags & FL_SINGLETON) {
102 rb_raise(rb_eTypeError, "can't make subclass of singleton class");
103 }
104 if (super == rb_cClass) {
105 rb_raise(rb_eTypeError, "can't make subclass of Class");
106 }
107 }
108
109
116 VALUE
117 rb_class_new(VALUE super)
118 {
119 Check_Type(super, T_CLASS);
120 rb_check_inheritable(super);
121 return rb_class_boot(super);
122 }
123
124 static NODE*
125 rewrite_cref_stack(NODE *node, VALUE old_klass, VALUE new_klass)
126 {
127 NODE *new_node;
128 if (!node) {
129 return NULL;
130 }
131 if (node->nd_clss == old_klass) {
132 new_node = NEW_CREF(new_klass);
133 new_node->nd_next = node->nd_next;
134 } else {
135 new_node = NEW_CREF(node->nd_clss);
136 new_node->nd_next = rewrite_cref_stack(node->nd_next, old_klass, new_klass);
137 }
138 return new_node;
139 }
140
141 static void
142 clone_method(VALUE klass, ID mid, const rb_method_entry_t *me)
143 {
144 VALUE newiseqval;
145 if (me->def && me->def->type == VM_METHOD_TYPE_ISEQ) {
146 rb_iseq_t *iseq;
147 newiseqval = rb_iseq_clone(me->def->body.iseq->self, klass);
148 GetISeqPtr(newiseqval, iseq);
149 iseq->cref_stack = rewrite_cref_stack(me->def->body.iseq->cref_stack, me->klass, klass);
150 rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, iseq, me->flag);
151 RB_GC_GUARD(newiseqval);
152 }
153 else {
154 rb_method_entry_set(klass, mid, me, me->flag);
155 }
156 }
157
158 static int
159 clone_method_i(st_data_t key, st_data_t value, st_data_t data)
160 {
161 clone_method((VALUE)data, (ID)key, (const rb_method_entry_t *)value);
162 return ST_CONTINUE;
163 }
164
165 static int
166 clone_const(ID key, const rb_const_entry_t *ce, st_table *tbl)
167 {
168 rb_const_entry_t *nce = ALLOC(rb_const_entry_t);
169 *nce = *ce;
170 st_insert(tbl, key, (st_data_t)nce);
171 return ST_CONTINUE;
172 }
173
174 static int
175 clone_const_i(st_data_t key, st_data_t value, st_data_t data)
176 {
177 return clone_const((ID)key, (const rb_const_entry_t *)value, (st_table *)data);
178 }
179
180 static void
181 class_init_copy_check(VALUE clone, VALUE orig)
182 {
183 if (orig == rb_cBasicObject) {
184 rb_raise(rb_eTypeError, "can't copy the root class");
185 }
186 if (RCLASS_SUPER(clone) != 0 || clone == rb_cBasicObject) {
187 rb_raise(rb_eTypeError, "already initialized class");
188 }
189 if (FL_TEST(orig, FL_SINGLETON)) {
190 rb_raise(rb_eTypeError, "can't copy singleton class");
191 }
192 }
193
194 /* :nodoc: */
195 VALUE
196 rb_mod_init_copy(VALUE clone, VALUE orig)
197 {
198 if (RB_TYPE_P(clone, T_CLASS)) {
199 class_init_copy_check(clone, orig);
200 }
201 rb_obj_init_copy(clone, orig);
202 if (!FL_TEST(CLASS_OF(clone), FL_SINGLETON)) {
203 RBASIC(clone)->klass = rb_singleton_class_clone(orig);
204 rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
205 }
206 RCLASS_SUPER(clone) = RCLASS_SUPER(orig);
207 RCLASS_EXT(clone)->allocator = RCLASS_EXT(orig)->allocator;
208 if (RCLASS_IV_TBL(orig)) {
209 st_data_t id;
210
211 if (RCLASS_IV_TBL(clone)) {
212 st_free_table(RCLASS_IV_TBL(clone));
213 }
214 RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(orig));
215 CONST_ID(id, "__tmp_classpath__");
216 st_delete(RCLASS_IV_TBL(clone), &id, 0);
217 CONST_ID(id, "__classpath__");
218 st_delete(RCLASS_IV_TBL(clone), &id, 0);
219 CONST_ID(id, "__classid__");
220 st_delete(RCLASS_IV_TBL(clone), &id, 0);
221 }
222 if (RCLASS_CONST_TBL(orig)) {
223 if (RCLASS_CONST_TBL(clone)) {
224 rb_free_const_table(RCLASS_CONST_TBL(clone));
225 }
226 RCLASS_CONST_TBL(clone) = st_init_numtable();
227 st_foreach(RCLASS_CONST_TBL(orig), clone_const_i, (st_data_t)RCLASS_CONST_TBL(clone));
228 }
229 if (RCLASS_M_TBL(orig)) {
230 if (RCLASS_M_TBL(clone)) {
231 rb_free_m_table(RCLASS_M_TBL(clone));
232 }
233 RCLASS_M_TBL(clone) = st_init_numtable();
234 st_foreach(RCLASS_M_TBL(orig), clone_method_i, (st_data_t)clone);
235 }
236
237 return clone;
238 }
239
240 VALUE
241 rb_singleton_class_clone(VALUE obj)
242 {
243 return rb_singleton_class_clone_and_attach(obj, Qundef);
244 }
245
246 VALUE
247 rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
248 {
249 VALUE klass = RBASIC(obj)->klass;
250
251 if (!FL_TEST(klass, FL_SINGLETON))
252 return klass;
253 else {
254 /* copy singleton(unnamed) class */
255 VALUE clone = class_alloc(RBASIC(klass)->flags, 0);
256
257 if (BUILTIN_TYPE(obj) == T_CLASS) {
258 RBASIC(clone)->klass = clone;
259 }
260 else {
261 RBASIC(clone)->klass = rb_singleton_class_clone(klass);
262 }
263
264 RCLASS_SUPER(clone) = RCLASS_SUPER(klass);
265 RCLASS_EXT(clone)->allocator = RCLASS_EXT(klass)->allocator;
266 if (RCLASS_IV_TBL(klass)) {
267 RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(klass));
268 }
269 if (RCLASS_CONST_TBL(klass)) {
270 RCLASS_CONST_TBL(clone) = st_init_numtable();
271 st_foreach(RCLASS_CONST_TBL(klass), clone_const_i, (st_data_t)RCLASS_CONST_TBL(clone));
272 }
273 if (attach != Qundef) {
274 rb_singleton_class_attached(clone, attach);
275 }
276 RCLASS_M_TBL(clone) = st_init_numtable();
277 st_foreach(RCLASS_M_TBL(klass), clone_method_i, (st_data_t)clone);
278 rb_singleton_class_attached(RBASIC(clone)->klass, clone);
279 FL_SET(clone, FL_SINGLETON);
280 return clone;
281 }
282 }
283
288 void
289 rb_singleton_class_attached(VALUE klass, VALUE obj)
290 {
291 if (FL_TEST(klass, FL_SINGLETON)) {
292 if (!RCLASS_IV_TBL(klass)) {
293 RCLASS_IV_TBL(klass) = st_init_numtable();
294 }
295 st_insert(RCLASS_IV_TBL(klass), id_attached, obj);
296 }
297 }
298
299
300
301 #define METACLASS_OF(k) RBASIC(k)->klass
302
308 #define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k))
309
315 #define HAVE_METACLASS_P(k) \
316 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \
317 rb_ivar_get(METACLASS_OF(k), id_attached) == (k))
318
326 #define ENSURE_EIGENCLASS(klass) \
327 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass))
328
329
339 static inline VALUE
340 make_metaclass(VALUE klass)
341 {
342 VALUE super;
343 VALUE metaclass = rb_class_boot(Qundef);
344
345 FL_SET(metaclass, FL_SINGLETON);
346 rb_singleton_class_attached(metaclass, klass);
347
348 if (META_CLASS_OF_CLASS_CLASS_P(klass)) {
349 METACLASS_OF(klass) = METACLASS_OF(metaclass) = metaclass;
350 }
351 else {
352 VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */
353 METACLASS_OF(klass) = metaclass;
354 METACLASS_OF(metaclass) = ENSURE_EIGENCLASS(tmp);
355 }
356
357 super = RCLASS_SUPER(klass);
358 while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super);
359 RCLASS_SUPER(metaclass) = super ? ENSURE_EIGENCLASS(super) : rb_cClass;
360
361 OBJ_INFECT(metaclass, RCLASS_SUPER(metaclass));
362
363 return metaclass;
364 }
365
372 static inline VALUE
373 make_singleton_class(VALUE obj)
374 {
375 VALUE orig_class = RBASIC(obj)->klass;
376 VALUE klass = rb_class_boot(orig_class);
377
378 FL_SET(klass, FL_SINGLETON);
379 RBASIC(obj)->klass = klass;
380 rb_singleton_class_attached(klass, obj);
381
382 METACLASS_OF(klass) = METACLASS_OF(rb_class_real(orig_class));
383 return klass;
384 }
385
386
387 static VALUE
388 boot_defclass(const char *name, VALUE super)
389 {
390 extern st_table *rb_class_tbl;
391 VALUE obj = rb_class_boot(super);
392 ID id = rb_intern(name);
393
394 rb_name_class(obj, id);
395 st_add_direct(rb_class_tbl, id, obj);
396 rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
397 return obj;
398 }
399
400 void
401 Init_class_hierarchy(void)
402 {
403 id_attached = rb_intern("__attached__");
404
405 rb_cBasicObject = boot_defclass("BasicObject", 0);
406 rb_cObject = boot_defclass("Object", rb_cBasicObject);
407 rb_cModule = boot_defclass("Module", rb_cObject);
408 rb_cClass = boot_defclass("Class", rb_cModule);
409
410 rb_const_set(rb_cObject, rb_intern("BasicObject"), rb_cBasicObject);
411 RBASIC(rb_cClass)->klass
412 = RBASIC(rb_cModule)->klass
413 = RBASIC(rb_cObject)->klass
414 = RBASIC(rb_cBasicObject)->klass
415 = rb_cClass;
416 }
417
418
429 VALUE
430 rb_make_metaclass(VALUE obj, VALUE unused)
431 {
432 if (BUILTIN_TYPE(obj) == T_CLASS) {
433 return make_metaclass(obj);
434 }
435 else {
436 return make_singleton_class(obj);
437 }
438 }
439
440
451 VALUE
452 rb_define_class_id(ID id, VALUE super)
453 {
454 VALUE klass;
455
456 if (!super) super = rb_cObject;
457 klass = rb_class_new(super);
458 rb_make_metaclass(klass, RBASIC(super)->klass);
459
460 return klass;
461 }
462
463
472 VALUE
473 rb_class_inherited(VALUE super, VALUE klass)
474 {
475 ID inherited;
476 if (!super) super = rb_cObject;
477 CONST_ID(inherited, "inherited");
478 return rb_funcall(super, inherited, 1, klass);
479 }
480
481
482
498 VALUE
499 rb_define_class(const char *name, VALUE super)
500 {
501 VALUE klass;
502 ID id;
503
504 id = rb_intern(name);
505 if (rb_const_defined(rb_cObject, id)) {
506 klass = rb_const_get(rb_cObject, id);
507 if (!RB_TYPE_P(klass, T_CLASS)) {
508 rb_raise(rb_eTypeError, "%s is not a class", name);
509 }
510 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
511 rb_raise(rb_eTypeError, "superclass mismatch for class %s", name);
512 }
513 return klass;
514 }
515 if (!super) {
516 rb_warn("no super class for `%s', Object assumed", name);
517 }
518 klass = rb_define_class_id(id, super);
519 st_add_direct(rb_class_tbl, id, klass);
520 rb_name_class(klass, id);
521 rb_const_set(rb_cObject, id, klass);
522 rb_class_inherited(super, klass);
523
524 return klass;
525 }
526
527
544 VALUE
545 rb_define_class_under(VALUE outer, const char *name, VALUE super)
546 {
547 return rb_define_class_id_under(outer, rb_intern(name), super);
548 }
549
550
567 VALUE
568 rb_define_class_id_under(VALUE outer, ID id, VALUE super)
569 {
570 VALUE klass;
571
572 if (rb_const_defined_at(outer, id)) {
573 klass = rb_const_get_at(outer, id);
574 if (!RB_TYPE_P(klass, T_CLASS)) {
575 rb_raise(rb_eTypeError, "%s is not a class", rb_id2name(id));
576 }
577 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
578 rb_name_error(id, "%s is already defined", rb_id2name(id));
579 }
580 return klass;
581 }
582 if (!super) {
583 rb_warn("no super class for `%s::%s', Object assumed",
584 rb_class2name(outer), rb_id2name(id));
585 }
586 klass = rb_define_class_id(id, super);
587 rb_set_class_path_string(klass, outer, rb_id2str(id));
588 rb_const_set(outer, id, klass);
589 rb_class_inherited(super, klass);
590 rb_gc_register_mark_object(klass);
591
592 return klass;
593 }
594
595 VALUE
596 rb_module_new(void)
597 {
598 VALUE mdl = class_alloc(T_MODULE, rb_cModule);
599
600 RCLASS_M_TBL(mdl) = st_init_numtable();
601
602 return (VALUE)mdl;
603 }
604
605 VALUE
606 rb_define_module_id(ID id)
607 {
608 VALUE mdl;
609
610 mdl = rb_module_new();
611 rb_name_class(mdl, id);
612
613 return mdl;
614 }
615
616 VALUE
617 rb_define_module(const char *name)
618 {
619 VALUE module;
620 ID id;
621
622 id = rb_intern(name);
623 if (rb_const_defined(rb_cObject, id)) {
624 module = rb_const_get(rb_cObject, id);
625 if (RB_TYPE_P(module, T_MODULE))
626 return module;
627 rb_raise(rb_eTypeError, "%s is not a module", rb_obj_classname(module));
628 }
629 module = rb_define_module_id(id);
630 st_add_direct(rb_class_tbl, id, module);
631 rb_const_set(rb_cObject, id, module);
632
633 return module;
634 }
635
636 VALUE
637 rb_define_module_under(VALUE outer, const char *name)
638 {
639 return rb_define_module_id_under(outer, rb_intern(name));
640 }
641
642 VALUE
643 rb_define_module_id_under(VALUE outer, ID id)
644 {
645 VALUE module;
646
647 if (rb_const_defined_at(outer, id)) {
648 module = rb_const_get_at(outer, id);
649 if (RB_TYPE_P(module, T_MODULE))
650 return module;
651 rb_raise(rb_eTypeError, "%s::%s is not a module",
652 rb_class2name(outer), rb_obj_classname(module));
653 }
654 module = rb_define_module_id(id);
655 rb_const_set(outer, id, module);
656 rb_set_class_path_string(module, outer, rb_id2str(id));
657 rb_gc_register_mark_object(module);
658
659 return module;
660 }
661
662 VALUE
663 rb_include_class_new(VALUE module, VALUE super)
664 {
665 VALUE klass = class_alloc(T_ICLASS, rb_cClass);
666
667 if (BUILTIN_TYPE(module) == T_ICLASS) {
668 module = RBASIC(module)->klass;
669 }
670 if (!RCLASS_IV_TBL(module)) {
671 RCLASS_IV_TBL(module) = st_init_numtable();
672 }
673 if (!RCLASS_CONST_TBL(module)) {
674 RCLASS_CONST_TBL(module) = st_init_numtable();
675 }
676 RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
677 RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module);
678 RCLASS_M_TBL(klass) = RCLASS_M_TBL(RCLASS_ORIGIN(module));
679 RCLASS_SUPER(klass) = super;
680 if (RB_TYPE_P(module, T_ICLASS)) {
681 RBASIC(klass)->klass = RBASIC(module)->klass;
682 }
683 else {
684 RBASIC(klass)->klass = module;
685 }
686 OBJ_INFECT(klass, module);
687 OBJ_INFECT(klass, super);
688
689 return (VALUE)klass;
690 }
691
692 static int include_modules_at(const VALUE klass, VALUE c, VALUE module);
693
694 void
695 rb_include_module(VALUE klass, VALUE module)
696 {
697 int changed = 0;
698
699 rb_frozen_class_p(klass);
700 if (!OBJ_UNTRUSTED(klass)) {
701 rb_secure(4);
702 }
703
704 if (!RB_TYPE_P(module, T_MODULE)) {
705 Check_Type(module, T_MODULE);
706 }
707
708 OBJ_INFECT(klass, module);
709
710 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module);
711 if (changed < 0)
712 rb_raise(rb_eArgError, "cyclic include detected");
713 if (changed) rb_clear_cache();
714 }
715
716 static int
717 add_refined_method_entry_i(st_data_t key, st_data_t value, st_data_t data)
718 {
719 rb_add_refined_method_entry((VALUE) data, (ID) key);
720 return ST_CONTINUE;
721 }
722
723 static int
724 include_modules_at(const VALUE klass, VALUE c, VALUE module)
725 {
726 VALUE p;
727 int changed = 0;
728 const st_table *const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass));
729
730 while (module) {
731 int superclass_seen = FALSE;
732
733 if (RCLASS_ORIGIN(module) != module)
734 goto skip;
735 if (klass_m_tbl && klass_m_tbl == RCLASS_M_TBL(module))
736 return -1;
737 /* ignore if the module included already in superclasses */
738 for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
739 switch (BUILTIN_TYPE(p)) {
740 case T_ICLASS:
741 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
742 if (!superclass_seen) {
743 c = p; /* move insertion point */
744 }
745 goto skip;
746 }
747 break;
748 case T_CLASS:
749 superclass_seen = TRUE;
750 break;
751 }
752 }
753 c = RCLASS_SUPER(c) = rb_include_class_new(module, RCLASS_SUPER(c));
754 if (FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
755 VALUE refined_class =
756 rb_refinement_module_get_refined_class(klass);
757
758 st_foreach(RMODULE_M_TBL(module), add_refined_method_entry_i,
759 (st_data_t) refined_class);
760 FL_SET(c, RMODULE_INCLUDED_INTO_REFINEMENT);
761 }
762 if (RMODULE_M_TBL(module) && RMODULE_M_TBL(module)->num_entries)
763 changed = 1;
764 if (RMODULE_CONST_TBL(module) && RMODULE_CONST_TBL(module)->num_entries)
765 changed = 1;
766 skip:
767 module = RCLASS_SUPER(module);
768 }
769
770 return changed;
771 }
772
773 static int
774 move_refined_method(st_data_t key, st_data_t value, st_data_t data)
775 {
776 rb_method_entry_t *me = (rb_method_entry_t *) value;
777 st_table *tbl = (st_table *) data;
778
779 if (me->def->type == VM_METHOD_TYPE_REFINED) {
780 if (me->def->body.orig_me) {
781 rb_method_entry_t *orig_me = me->def->body.orig_me, *new_me;
782 me->def->body.orig_me = NULL;
783 new_me = ALLOC(rb_method_entry_t);
784 *new_me = *me;
785 st_add_direct(tbl, key, (st_data_t) new_me);
786 *me = *orig_me;
787 xfree(orig_me);
788 return ST_CONTINUE;
789 }
790 else {
791 st_add_direct(tbl, key, (st_data_t) me);
792 return ST_DELETE;
793 }
794 }
795 else {
796 return ST_CONTINUE;
797 }
798 }
799
800 void
801 rb_prepend_module(VALUE klass, VALUE module)
802 {
803 void rb_vm_check_redefinition_by_prepend(VALUE klass);
804 VALUE origin;
805 int changed = 0;
806
807 rb_frozen_class_p(klass);
808 if (!OBJ_UNTRUSTED(klass)) {
809 rb_secure(4);
810 }
811
812 Check_Type(module, T_MODULE);
813
814 OBJ_INFECT(klass, module);
815
816 origin = RCLASS_ORIGIN(klass);
817 if (origin == klass) {
818 origin = class_alloc(T_ICLASS, klass);
819 RCLASS_SUPER(origin) = RCLASS_SUPER(klass);
820 RCLASS_SUPER(klass) = origin;
821 RCLASS_ORIGIN(klass) = origin;
822 RCLASS_M_TBL(origin) = RCLASS_M_TBL(klass);
823 RCLASS_M_TBL(klass) = st_init_numtable();
824 st_foreach(RCLASS_M_TBL(origin), move_refined_method,
825 (st_data_t) RCLASS_M_TBL(klass));
826 }
827 changed = include_modules_at(klass, klass, module);
828 if (changed < 0)
829 rb_raise(rb_eArgError, "cyclic prepend detected");
830 if (changed) {
831 rb_clear_cache();
832 rb_vm_check_redefinition_by_prepend(klass);
833 }
834 }
835
836 /*
837 * call-seq:
838 * mod.included_modules -> array
839 *
840 * Returns the list of modules included in <i>mod</i>.
841 *
842 * module Mixin
843 * end
844 *
845 * module Outer
846 * include Mixin
847 * end
848 *
849 * Mixin.included_modules #=> []
850 * Outer.included_modules #=> [Mixin]
851 */
852
853 VALUE
854 rb_mod_included_modules(VALUE mod)
855 {
856 VALUE ary = rb_ary_new();
857 VALUE p;
858 VALUE origin = RCLASS_ORIGIN(mod);
859
860 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
861 if (p != origin && BUILTIN_TYPE(p) == T_ICLASS) {
862 VALUE m = RBASIC(p)->klass;
863 if (RB_TYPE_P(m, T_MODULE))
864 rb_ary_push(ary, m);
865 }
866 }
867 return ary;
868 }
869
870 /*
871 * call-seq:
872 * mod.include?(module) -> true or false
873 *
874 * Returns <code>true</code> if <i>module</i> is included in
875 * <i>mod</i> or one of <i>mod</i>'s ancestors.
876 *
877 * module A
878 * end
879 * class B
880 * include A
881 * end
882 * class C < B
883 * end
884 * B.include?(A) #=> true
885 * C.include?(A) #=> true
886 * A.include?(A) #=> false
887 */
888
889 VALUE
890 rb_mod_include_p(VALUE mod, VALUE mod2)
891 {
892 VALUE p;
893
894 Check_Type(mod2, T_MODULE);
895 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
896 if (BUILTIN_TYPE(p) == T_ICLASS) {
897 if (RBASIC(p)->klass == mod2) return Qtrue;
898 }
899 }
900 return Qfalse;
901 }
902
903 /*
904 * call-seq:
905 * mod.ancestors -> array
906 *
907 * Returns a list of modules included in <i>mod</i> (including
908 * <i>mod</i> itself).
909 *
910 * module Mod
911 * include Math
912 * include Comparable
913 * end
914 *
915 * Mod.ancestors #=> [Mod, Comparable, Math]
916 * Math.ancestors #=> [Math]
917 */
918
919 VALUE
920 rb_mod_ancestors(VALUE mod)
921 {
922 VALUE p, ary = rb_ary_new();
923
924 for (p = mod; p; p = RCLASS_SUPER(p)) {
925 if (FL_TEST(p, FL_SINGLETON))
926 continue;
927 if (BUILTIN_TYPE(p) == T_ICLASS) {
928 rb_ary_push(ary, RBASIC(p)->klass);
929 }
930 else if (p == RCLASS_ORIGIN(p)) {
931 rb_ary_push(ary, p);
932 }
933 }
934 return ary;
935 }
936
937 #define VISI(x) ((x)&NOEX_MASK)
938 #define VISI_CHECK(x,f) (VISI(x) == (f))
939
940 static int
941 ins_methods_push(ID name, long type, VALUE ary, long visi)
942 {
943 if (type == -1) return ST_CONTINUE;
944
945 switch (visi) {
946 case NOEX_PRIVATE:
947 case NOEX_PROTECTED:
948 case NOEX_PUBLIC:
949 visi = (type == visi);
950 break;
951 default:
952 visi = (type != NOEX_PRIVATE);
953 break;
954 }
955 if (visi) {
956 rb_ary_push(ary, ID2SYM(name));
957 }
958 return ST_CONTINUE;
959 }
960
961 static int
962 ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
963 {
964 return ins_methods_push((ID)name, (long)type, (VALUE)ary, -1); /* everything but private */
965 }
966
967 static int
968 ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
969 {
970 return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PROTECTED);
971 }
972
973 static int
974 ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
975 {
976 return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PRIVATE);
977 }
978
979 static int
980 ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
981 {
982 return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PUBLIC);
983 }
984
985 static int
986 method_entry_i(st_data_t key, st_data_t value, st_data_t data)
987 {
988 const rb_method_entry_t *me = (const rb_method_entry_t *)value;
989 st_table *list = (st_table *)data;
990 long type;
991
992 if (me && me->def->type == VM_METHOD_TYPE_REFINED) {
993 me = rb_resolve_refined_method(Qnil, me, NULL);
994 if (!me) return ST_CONTINUE;
995 }
996 if (!st_lookup(list, key, 0)) {
997 if (UNDEFINED_METHOD_ENTRY_P(me)) {
998 type = -1; /* none */
999 }
1000 else {
1001 type = VISI(me->flag);
1002 }
1003 st_add_direct(list, key, type);
1004 }
1005 return ST_CONTINUE;
1006 }
1007
1008 static VALUE
1009 class_instance_method_list(int argc, VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t))
1010 {
1011 VALUE ary;
1012 int recur, prepended = 0;
1013 st_table *list;
1014
1015 if (argc == 0) {
1016 recur = TRUE;
1017 }
1018 else {
1019 VALUE r;
1020 rb_scan_args(argc, argv, "01", &r);
1021 recur = RTEST(r);
1022 }
1023
1024 if (!recur && RCLASS_ORIGIN(mod) != mod) {
1025 mod = RCLASS_ORIGIN(mod);
1026 prepended = 1;
1027 }
1028
1029 list = st_init_numtable();
1030 for (; mod; mod = RCLASS_SUPER(mod)) {
1031 if (RCLASS_M_TBL(mod)) st_foreach(RCLASS_M_TBL(mod), method_entry_i, (st_data_t)list);
1032 if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue;
1033 if (obj && FL_TEST(mod, FL_SINGLETON)) continue;
1034 if (!recur) break;
1035 }
1036 ary = rb_ary_new();
1037 st_foreach(list, func, ary);
1038 st_free_table(list);
1039
1040 return ary;
1041 }
1042
1043 /*
1044 * call-seq:
1045 * mod.instance_methods(include_super=true) -> array
1046 *
1047 * Returns an array containing the names of the public and protected instance
1048 * methods in the receiver. For a module, these are the public and protected methods;
1049 * for a class, they are the instance (not singleton) methods. With no
1050 * argument, or with an argument that is <code>false</code>, the
1051 * instance methods in <i>mod</i> are returned, otherwise the methods
1052 * in <i>mod</i> and <i>mod</i>'s superclasses are returned.
1053 *
1054 * module A
1055 * def method1() end
1056 * end
1057 * class B
1058 * def method2() end
1059 * end
1060 * class C < B
1061 * def method3() end
1062 * end
1063 *
1064 * A.instance_methods #=> [:method1]
1065 * B.instance_methods(false) #=> [:method2]
1066 * C.instance_methods(false) #=> [:method3]
1067 * C.instance_methods(true).length #=> 43
1068 */
1069
1070 VALUE
1071 rb_class_instance_methods(int argc, VALUE *argv, VALUE mod)
1072 {
1073 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i);
1074 }
1075
1076 /*
1077 * call-seq:
1078 * mod.protected_instance_methods(include_super=true) -> array
1079 *
1080 * Returns a list of the protected instance methods defined in
1081 * <i>mod</i>. If the optional parameter is not <code>false</code>, the
1082 * methods of any ancestors are included.
1083 */
1084
1085 VALUE
1086 rb_class_protected_instance_methods(int argc, VALUE *argv, VALUE mod)
1087 {
1088 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i);
1089 }
1090
1091 /*
1092 * call-seq:
1093 * mod.private_instance_methods(include_super=true) -> array
1094 *
1095 * Returns a list of the private instance methods defined in
1096 * <i>mod</i>. If the optional parameter is not <code>false</code>, the
1097 * methods of any ancestors are included.
1098 *
1099 * module Mod
1100 * def method1() end
1101 * private :method1
1102 * def method2() end
1103 * end
1104 * Mod.instance_methods #=> [:method2]
1105 * Mod.private_instance_methods #=> [:method1]
1106 */
1107
1108 VALUE
1109 rb_class_private_instance_methods(int argc, VALUE *argv, VALUE mod)
1110 {
1111 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i);
1112 }
1113
1114 /*
1115 * call-seq:
1116 * mod.public_instance_methods(include_super=true) -> array
1117 *
1118 * Returns a list of the public instance methods defined in <i>mod</i>.
1119 * If the optional parameter is not <code>false</code>, the methods of
1120 * any ancestors are included.
1121 */
1122
1123 VALUE
1124 rb_class_public_instance_methods(int argc, VALUE *argv, VALUE mod)
1125 {
1126 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i);
1127 }
1128
1129 /*
1130 * call-seq:
1131 * obj.methods(all=true) -> array
1132 *
1133 * Returns a list of the names of public and protected methods of
1134 * <i>obj</i>. This will include all the methods accessible in
1135 * <i>obj</i>'s ancestors.
1136 * If the <i>all</i> parameter is set to <code>false</code>, only those methods
1137 * in the receiver will be listed.
1138 *
1139 * class Klass
1140 * def klass_method()
1141 * end
1142 * end
1143 * k = Klass.new
1144 * k.methods[0..9] #=> [:klass_method, :nil?, :===,
1145 * # :==~, :!, :eql?
1146 * # :hash, :<=>, :class, :singleton_class]
1147 * k.methods.length #=> 57
1148 */
1149
1150 VALUE
1151 rb_obj_methods(int argc, VALUE *argv, VALUE obj)
1152 {
1153 retry:
1154 if (argc == 0) {
1155 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
1156 }
1157 else {
1158 VALUE recur;
1159
1160 rb_scan_args(argc, argv, "1", &recur);
1161 if (RTEST(recur)) {
1162 argc = 0;
1163 goto retry;
1164 }
1165 return rb_obj_singleton_methods(argc, argv, obj);
1166 }
1167 }
1168
1169 /*
1170 * call-seq:
1171 * obj.protected_methods(all=true) -> array
1172 *
1173 * Returns the list of protected methods accessible to <i>obj</i>. If
1174 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1175 * in the receiver will be listed.
1176 */
1177
1178 VALUE
1179 rb_obj_protected_methods(int argc, VALUE *argv, VALUE obj)
1180 {
1181 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
1182 }
1183
1184 /*
1185 * call-seq:
1186 * obj.private_methods(all=true) -> array
1187 *
1188 * Returns the list of private methods accessible to <i>obj</i>. If
1189 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1190 * in the receiver will be listed.
1191 */
1192
1193 VALUE
1194 rb_obj_private_methods(int argc, VALUE *argv, VALUE obj)
1195 {
1196 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
1197 }
1198
1199 /*
1200 * call-seq:
1201 * obj.public_methods(all=true) -> array
1202 *
1203 * Returns the list of public methods accessible to <i>obj</i>. If
1204 * the <i>all</i> parameter is set to <code>false</code>, only those methods
1205 * in the receiver will be listed.
1206 */
1207
1208 VALUE
1209 rb_obj_public_methods(int argc, VALUE *argv, VALUE obj)
1210 {
1211 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
1212 }
1213
1214 /*
1215 * call-seq:
1216 * obj.singleton_methods(all=true) -> array
1217 *
1218 * Returns an array of the names of singleton methods for <i>obj</i>.
1219 * If the optional <i>all</i> parameter is true, the list will include
1220 * methods in modules included in <i>obj</i>.
1221 * Only public and protected singleton methods are returned.
1222 *
1223 * module Other
1224 * def three() end
1225 * end
1226 *
1227 * class Single
1228 * def Single.four() end
1229 * end
1230 *
1231 * a = Single.new
1232 *
1233 * def a.one()
1234 * end
1235 *
1236 * class << a
1237 * include Other
1238 * def two()
1239 * end
1240 * end
1241 *
1242 * Single.singleton_methods #=> [:four]
1243 * a.singleton_methods(false) #=> [:two, :one]
1244 * a.singleton_methods #=> [:two, :one, :three]
1245 */
1246
1247 VALUE
1248 rb_obj_singleton_methods(int argc, VALUE *argv, VALUE obj)
1249 {
1250 VALUE recur, ary, klass;
1251 st_table *list;
1252
1253 if (argc == 0) {
1254 recur = Qtrue;
1255 }
1256 else {
1257 rb_scan_args(argc, argv, "01", &recur);
1258 }
1259 klass = CLASS_OF(obj);
1260 list = st_init_numtable();
1261 if (klass && FL_TEST(klass, FL_SINGLETON)) {
1262 if (RCLASS_M_TBL(klass))
1263 st_foreach(RCLASS_M_TBL(klass), method_entry_i, (st_data_t)list);
1264 klass = RCLASS_SUPER(klass);
1265 }
1266 if (RTEST(recur)) {
1267 while (klass && (FL_TEST(klass, FL_SINGLETON) || RB_TYPE_P(klass, T_ICLASS))) {
1268 if (RCLASS_M_TBL(klass))
1269 st_foreach(RCLASS_M_TBL(klass), method_entry_i, (st_data_t)list);
1270 klass = RCLASS_SUPER(klass);
1271 }
1272 }
1273 ary = rb_ary_new();
1274 st_foreach(list, ins_methods_i, ary);
1275 st_free_table(list);
1276
1277 return ary;
1278 }
1279
1337 void
1338 rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc)
1339 {
1340 rb_add_method_cfunc(klass, mid, func, argc, NOEX_PUBLIC);
1341 }
1342
1343 void
1344 rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1345 {
1346 rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PUBLIC);
1347 }
1348
1349 void
1350 rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1351 {
1352 rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PROTECTED);
1353 }
1354
1355 void
1356 rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
1357 {
1358 rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PRIVATE);
1359 }
1360
1361 void
1362 rb_undef_method(VALUE klass, const char *name)
1363 {
1364 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, NOEX_UNDEF);
1365 }
1366
1375 #define SPECIAL_SINGLETON(x,c) do {\
1376 if (obj == (x)) {\
1377 return (c);\
1378 }\
1379 } while (0)
1380
1381 static inline VALUE
1382 special_singleton_class_of(VALUE obj)
1383 {
1384 SPECIAL_SINGLETON(Qnil, rb_cNilClass);
1385 SPECIAL_SINGLETON(Qfalse, rb_cFalseClass);
1386 SPECIAL_SINGLETON(Qtrue, rb_cTrueClass);
1387 return Qnil;
1388 }
1389
1390 VALUE
1391 rb_special_singleton_class(VALUE obj)
1392 {
1393 return special_singleton_class_of(obj);
1394 }
1395
1405 static VALUE
1406 singleton_class_of(VALUE obj)
1407 {
1408 VALUE klass;
1409
1410 if (FIXNUM_P(obj) || FLONUM_P(obj) || SYMBOL_P(obj)) {
1411 rb_raise(rb_eTypeError, "can't define singleton");
1412 }
1413 if (SPECIAL_CONST_P(obj)) {
1414 klass = special_singleton_class_of(obj);
1415 if (NIL_P(klass))
1416 rb_bug("unknown immediate %p", (void *)obj);
1417 return klass;
1418 }
1419 else {
1420 enum ruby_value_type type = BUILTIN_TYPE(obj);
1421 if (type == T_FLOAT || type == T_BIGNUM) {
1422 rb_raise(rb_eTypeError, "can't define singleton");
1423 }
1424 }
1425
1426 if (FL_TEST(RBASIC(obj)->klass, FL_SINGLETON) &&
1427 rb_ivar_get(RBASIC(obj)->klass, id_attached) == obj) {
1428 klass = RBASIC(obj)->klass;
1429 }
1430 else {
1431 klass = rb_make_metaclass(obj, RBASIC(obj)->klass);
1432 }
1433
1434 if (OBJ_TAINTED(obj)) {
1435 OBJ_TAINT(klass);
1436 }
1437 else {
1438 FL_UNSET(klass, FL_TAINT);
1439 }
1440 if (OBJ_UNTRUSTED(obj)) {
1441 OBJ_UNTRUST(klass);
1442 }
1443 else {
1444 FL_UNSET(klass, FL_UNTRUSTED);
1445 }
1446 if (OBJ_FROZEN(obj)) OBJ_FREEZE(klass);
1447
1448 return klass;
1449 }
1450
1451
1469 VALUE
1470 rb_singleton_class(VALUE obj)
1471 {
1472 VALUE klass = singleton_class_of(obj);
1473
1474 /* ensures an exposed class belongs to its own eigenclass */
1475 if (RB_TYPE_P(obj, T_CLASS)) (void)ENSURE_EIGENCLASS(klass);
1476
1477 return klass;
1478 }
1479
1496 void
1497 rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
1498 {
1499 rb_define_method(singleton_class_of(obj), name, func, argc);
1500 }
1501
1502
1503
1511 void
1512 rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
1513 {
1514 rb_define_private_method(module, name, func, argc);
1515 rb_define_singleton_method(module, name, func, argc);
1516 }
1517
1518
1525 void
1526 rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
1527 {
1528 rb_define_module_function(rb_mKernel, name, func, argc);
1529 }
1530
1531
1538 void
1539 rb_define_alias(VALUE klass, const char *name1, const char *name2)
1540 {
1541 rb_alias(klass, rb_intern(name1), rb_intern(name2));
1542 }
1543
1551 void
1552 rb_define_attr(VALUE klass, const char *name, int read, int write)
1553 {
1554 rb_attr(klass, rb_intern(name), read, write, FALSE);
1555 }
1556
1557 int
1558 rb_obj_basic_to_s_p(VALUE obj)
1559 {
1560 const rb_method_entry_t *me = rb_method_entry(CLASS_OF(obj), rb_intern("to_s"), 0);
1561 if (me && me->def && me->def->type == VM_METHOD_TYPE_CFUNC &&
1562 me->def->body.cfunc.func == rb_any_to_s)
1563 return 1;
1564 return 0;
1565 }
1566
1567 #include <stdarg.h>
1568
1569 int
1570 rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
1571 {
1572 int i;
1573 const char *p = fmt;
1574 VALUE *var;
1575 va_list vargs;
1576 int f_var = 0, f_hash = 0, f_block = 0;
1577 int n_lead = 0, n_opt = 0, n_trail = 0, n_mand;
1578 int argi = 0;
1579 VALUE hash = Qnil;
1580
1581 if (ISDIGIT(*p)) {
1582 n_lead = *p - '0';
1583 p++;
1584 if (ISDIGIT(*p)) {
1585 n_opt = *p - '0';
1586 p++;
1587 if (ISDIGIT(*p)) {
1588 n_trail = *p - '0';
1589 p++;
1590 goto block_arg;
1591 }
1592 }
1593 }
1594 if (*p == '*') {
1595 f_var = 1;
1596 p++;
1597 if (ISDIGIT(*p)) {
1598 n_trail = *p - '0';
1599 p++;
1600 }
1601 }
1602 block_arg:
1603 if (*p == ':') {
1604 f_hash = 1;
1605 p++;
1606 }
1607 if (*p == '&') {
1608 f_block = 1;
1609 p++;
1610 }
1611 if (*p != '\0') {
1612 rb_fatal("bad scan arg format: %s", fmt);
1613 }
1614 n_mand = n_lead + n_trail;
1615
1616 if (argc < n_mand)
1617 goto argc_error;
1618
1619 va_start(vargs, fmt);
1620
1621 /* capture an option hash - phase 1: pop */
1622 if (f_hash && n_mand < argc) {
1623 VALUE last = argv[argc - 1];
1624
1625 if (NIL_P(last)) {
1626 /* nil is taken as an empty option hash only if it is not
1627 ambiguous; i.e. '*' is not specified and arguments are
1628 given more than sufficient */
1629 if (!f_var && n_mand + n_opt < argc)
1630 argc--;
1631 }
1632 else {
1633 hash = rb_check_hash_type(last);
1634 if (!NIL_P(hash))
1635 argc--;
1636 }
1637 }
1638 /* capture leading mandatory arguments */
1639 for (i = n_lead; i-- > 0; ) {
1640 var = va_arg(vargs, VALUE *);
1641 if (var) *var = argv[argi];
1642 argi++;
1643 }
1644 /* capture optional arguments */
1645 for (i = n_opt; i-- > 0; ) {
1646 var = va_arg(vargs, VALUE *);
1647 if (argi < argc - n_trail) {
1648 if (var) *var = argv[argi];
1649 argi++;
1650 }
1651 else {
1652 if (var) *var = Qnil;
1653 }
1654 }
1655 /* capture variable length arguments */
1656 if (f_var) {
1657 int n_var = argc - argi - n_trail;
1658
1659 var = va_arg(vargs, VALUE *);
1660 if (0 < n_var) {
1661 if (var) *var = rb_ary_new4(n_var, &argv[argi]);
1662 argi += n_var;
1663 }
1664 else {
1665 if (var) *var = rb_ary_new();
1666 }
1667 }
1668 /* capture trailing mandatory arguments */
1669 for (i = n_trail; i-- > 0; ) {
1670 var = va_arg(vargs, VALUE *);
1671 if (var) *var = argv[argi];
1672 argi++;
1673 }
1674 /* capture an option hash - phase 2: assignment */
1675 if (f_hash) {
1676 var = va_arg(vargs, VALUE *);
1677 if (var) *var = hash;
1678 }
1679 /* capture iterator block */
1680 if (f_block) {
1681 var = va_arg(vargs, VALUE *);
1682 if (rb_block_given_p()) {
1683 *var = rb_block_proc();
1684 }
1685 else {
1686 *var = Qnil;
1687 }
1688 }
1689 va_end(vargs);
1690
1691 if (argi < argc) {
1692 argc_error:
1693 rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt);
1694 }
1695
1696 return argc;
1697 }
1698
data
VALUE data
Definition: tcltklib.c:3368
make_metaclass
static VALUE make_metaclass(VALUE klass)
Creates a metaclass of klass.
Definition: class.c:340
RB_TYPE_P
#define RB_TYPE_P(obj, type)
rb_const_get_at
VALUE rb_const_get_at(VALUE, ID)
Definition: variable.c:1882
ALLOC
#define ALLOC(type)
tmp
volatile VALUE tmp
Definition: tcltklib.c:10209
UNDEFINED_METHOD_ENTRY_P
#define UNDEFINED_METHOD_ENTRY_P(me)
Definition: method.h:108
rb_define_module_id_under
VALUE rb_define_module_id_under(VALUE outer, ID id)
Definition: class.c:643
ary
volatile VALUE ary
Definition: tcltklib.c:9713
FLONUM_P
#define FLONUM_P(x)
rb_cBasicObject
RUBY_EXTERN VALUE rb_cBasicObject
Definition: ripper.y:1425
rb_vm_check_redefinition_by_prepend
void rb_vm_check_redefinition_by_prepend(VALUE klass)
Definition: vm.c:1016
rb_ary_new4
VALUE rb_ary_new4(long n, const VALUE *elts)
Definition: array.c:451
rb_mod_include_p
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Definition: class.c:890
rb_any_to_s
VALUE rb_any_to_s(VALUE)
Definition: object.c:384
rb_bug
void rb_bug(const char *fmt,...)
Definition: error.c:290
rb_method_definition_struct::type
rb_method_type_t type
Definition: method.h:77
FALSE
#define FALSE
Definition: nkf.h:174
rb_check_inheritable
void rb_check_inheritable(VALUE super)
Ensures a class can be derived from super.
Definition: class.c:95
rb_cNilClass
RUBY_EXTERN VALUE rb_cNilClass
Definition: ripper.y:1447
rb_cModule
RUBY_EXTERN VALUE rb_cModule
Definition: ripper.y:1445
OBJ_INFECT
#define OBJ_INFECT(x, s)
ins_methods_push
static int ins_methods_push(ID name, long type, VALUE ary, long visi)
Definition: class.c:941
id_attached
static ID id_attached
Definition: class.c:35
ST_DELETE
Definition: ripper.y:108
rb_const_entry_struct
Definition: constant.h:19
st_table
Definition: ripper.y:77
rb_obj_classname
const char * rb_obj_classname(VALUE)
Definition: variable.c:396
rb_obj_private_methods
VALUE rb_obj_private_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1194
rb_id2str
VALUE rb_id2str(ID id)
Definition: ripper.c:16944
p
Win32OLEIDispatch * p
Definition: win32ole.c:786
VISI
#define VISI(x)
Definition: class.c:937
UNLIMITED_ARGUMENTS
#define UNLIMITED_ARGUMENTS
FL_TEST
#define FL_TEST(x, f)
rewrite_cref_stack
static NODE * rewrite_cref_stack(NODE *node, VALUE old_klass, VALUE new_klass)
Definition: class.c:125
st_lookup
int st_lookup(st_table *, st_data_t, st_data_t *)
st_add_direct
void st_add_direct(st_table *, st_data_t, st_data_t)
Definition: st.c:624
rb_define_singleton_method
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1497
ruby.h
rb_method_entry_struct::flag
rb_method_flag_t flag
Definition: method.h:96
FL_SET
#define FL_SET(x, f)
st_init_numtable
st_table * st_init_numtable(void)
Definition: st.c:272
rb_class_private_instance_methods
VALUE rb_class_private_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:1109
method.h
T_ICLASS
#define T_ICLASS
rb_define_class_id_under
VALUE rb_define_class_id_under(VALUE outer, ID id, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:568
RMODULE_M_TBL
#define RMODULE_M_TBL(m)
func
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:108
rb_secure
void rb_secure(int)
Definition: safe.c:79
rb_const_get
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:1876
class_alloc
static VALUE class_alloc(VALUE flags, VALUE klass)
Allocates a struct RClass for a new class.
Definition: class.c:50
i
ssize_t i
Definition: bigdecimal.c:5655
rb_method_definition_struct::orig_me
struct rb_method_entry_struct * orig_me
Definition: method.h:90
rb_mod_ancestors
VALUE rb_mod_ancestors(VALUE mod)
Definition: class.c:920
rb_refinement_module_get_refined_class
VALUE rb_refinement_module_get_refined_class(VALUE module)
Definition: eval.c:1129
rb_define_private_method
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1356
RCLASS_ORIGIN
#define RCLASS_ORIGIN(c)
VM_METHOD_TYPE_ISEQ
Definition: method.h:46
rb_eTypeError
VALUE rb_eTypeError
Definition: error.c:511
OBJ_FREEZE
#define OBJ_FREEZE(x)
OBJ_TAINTED
#define OBJ_TAINTED(x)
rb_ary_push
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:822
rb_singleton_class_clone
VALUE rb_singleton_class_clone(VALUE obj)
Definition: class.c:241
constant.h
rb_cTrueClass
RUBY_EXTERN VALUE rb_cTrueClass
Definition: ripper.y:1461
rb_mod_init_copy
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
Definition: class.c:196
CLASS_OF
#define CLASS_OF(v)
NIL_P
NIL_P(eventloop_thread)
Definition: tcltklib.c:4068
var
VALUE var
Definition: tcltklib.c:5517
Init_class_hierarchy
void Init_class_hierarchy(void)
Definition: class.c:401
xfree
#define xfree
rb_funcall
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:774
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:545
ins_methods_priv_i
static int ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary)
Definition: class.c:974
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1780
Qtrue
return Qtrue
Definition: tcltklib.c:9610
clone_const_i
static int clone_const_i(st_data_t key, st_data_t value, st_data_t data)
Definition: class.c:175
NEW_CREF
#define NEW_CREF(a)
rb_method_definition_struct::body
union rb_method_definition_struct::@112 body
rb_include_module
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:695
r
r
Definition: bigdecimal.c:1196
FL_UNTRUSTED
#define FL_UNTRUSTED
rb_define_protected_method
void rb_define_protected_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1350
rb_define_global_function
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1526
ISDIGIT
#define ISDIGIT(c)
ruby_value_type
ruby_value_type
Definition: ripper.y:450
last
unsigned int last
Definition: nkf.c:4310
ID2SYM
#define ID2SYM(x)
T_FLOAT
#define T_FLOAT
tbl
VALUE tbl
Definition: tkutil.c:1280
rb_undef_method
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1362
rb_ivar_get
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1116
VM_METHOD_TYPE_REFINED
Definition: method.h:56
fmt
const char * fmt
Definition: tcltklib.c:841
RCLASS_EXT
#define RCLASS_EXT(c)
rb_name_error
void rb_name_error(ID id, const char *fmt,...)
Definition: error.c:899
RNode
Definition: ripper.y:240
name2
char * name2
Definition: tcltklib.c:4206
rb_prepend_module
void rb_prepend_module(VALUE klass, VALUE module)
Definition: class.c:801
st_data_t
unsigned long st_data_t
Definition: ripper.y:35
st_delete
int st_delete(st_table *, st_data_t *, st_data_t *)
rb_singleton_class
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1470
hash
VALUE hash
Definition: tkutil.c:267
rb_method_definition_struct::cfunc
rb_method_cfunc_t cfunc
Definition: method.h:81
rb_block_proc
VALUE rb_block_proc(void)
Definition: proc.c:479
rb_class_inherited
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
Definition: class.c:473
m
BDIGIT m
Definition: bigdecimal.c:5085
FIXNUM_P
#define FIXNUM_P(f)
Qfalse
return Qfalse
Definition: tcltklib.c:6779
rb_iseq_struct::cref_stack
NODE * cref_stack
Definition: vm_core.h:304
rb_block_given_p
int rb_block_given_p(void)
Definition: eval.c:672
Qnil
#define Qnil
Definition: tcltklib.c:1896
VM_METHOD_TYPE_UNDEF
Definition: method.h:52
rb_special_singleton_class
VALUE rb_special_singleton_class(VALUE obj)
Definition: class.c:1391
RCLASS_IV_TBL
#define RCLASS_IV_TBL(c)
rb_mKernel
RUBY_EXTERN VALUE rb_mKernel
Definition: ripper.y:1414
boot_defclass
static VALUE boot_defclass(const char *name, VALUE super)
Definition: class.c:388
rb_ary_new
VALUE rb_ary_new(void)
Definition: array.c:424
clone_method
static void clone_method(VALUE klass, ID mid, const rb_method_entry_t *me)
Definition: class.c:142
Check_Type
#define Check_Type(v, t)
flags
int flags
Definition: tcltklib.c:3023
ID
unsigned long ID
Definition: ripper.y:105
va_end
va_end(args)
RCLASS_SUPER
#define RCLASS_SUPER(c)
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:499
obj
static VALUE VALUE obj
Definition: tcltklib.c:3158
special_singleton_class_of
static VALUE special_singleton_class_of(VALUE obj)
Definition: class.c:1382
rb_clear_cache
void rb_clear_cache(void)
Definition: vm_method.c:46
rb_define_module_id
VALUE rb_define_module_id(ID id)
Definition: class.c:606
METACLASS_OF
#define METACLASS_OF(k)
Definition: class.c:301
ANYARGS
#define ANYARGS
META_CLASS_OF_CLASS_CLASS_P
#define META_CLASS_OF_CLASS_CLASS_P(k)
whether k is a meta^(n)-class of Class class
Definition: class.c:308
rb_method_entry_struct
Definition: method.h:95
rb_check_hash_type
VALUE rb_check_hash_type(VALUE)
Definition: hash.c:461
rb_method_definition_struct::iseq
rb_iseq_t * iseq
Definition: method.h:80
rb_obj_protected_methods
VALUE rb_obj_protected_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1179
rb_resolve_refined_method
rb_method_entry_t * rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me, VALUE *defined_class_ptr)
Definition: vm_method.c:586
key
static int VALUE key
Definition: tkutil.c:265
rb_method_entry
rb_method_entry_t * rb_method_entry(VALUE klass, ID id, VALUE *define_class_ptr)
Definition: vm_method.c:551
rb_method_cfunc_struct::func
VALUE(* func)(ANYARGS)
Definition: method.h:64
rb_method_entry_struct::klass
VALUE klass
Definition: method.h:100
rb_obj_basic_to_s_p
int rb_obj_basic_to_s_p(VALUE obj)
Definition: class.c:1558
argv
VALUE * argv
Definition: tcltklib.c:1971
rb_define_alias
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1539
RTEST
#define RTEST(v)
id
const int id
Definition: nkf.c:209
rb_define_module_function
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
Definition: class.c:1512
st_foreach
int st_foreach(st_table *, int(*)(ANYARGS), st_data_t)
Definition: st.c:1000
add_refined_method_entry_i
static int add_refined_method_entry_i(st_data_t key, st_data_t value, st_data_t data)
Definition: class.c:717
class_instance_method_list
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:1009
TRUE
#define TRUE
Definition: nkf.h:175
value
volatile VALUE value
Definition: tcltklib.c:9442
vargs
va_list vargs
Definition: regerror.c:267
rb_const_defined
int rb_const_defined(VALUE, ID)
Definition: variable.c:2103
rb_include_class_new
VALUE rb_include_class_new(VALUE module, VALUE super)
Definition: class.c:663
rb_fatal
void rb_fatal(const char *fmt,...)
Definition: error.c:1834
name1
char * name1
Definition: tcltklib.c:4205
rb_add_method
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:402
CONST_ID
#define CONST_ID(var, str)
rb_gc_register_mark_object
void rb_gc_register_mark_object(VALUE)
Definition: gc.c:2980
void
VP_EXPORT void
Definition: bigdecimal.c:5083
rb_scan_args
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1570
internal.h
rb_attr
void rb_attr(VALUE, ID, int, int, int)
Definition: vm_method.c:796
ins_methods_i
static int ins_methods_i(st_data_t name, st_data_t type, st_data_t ary)
Definition: class.c:962
rb_const_set
void rb_const_set(VALUE, ID, VALUE)
Definition: variable.c:2141
OBJ_FROZEN
#define OBJ_FROZEN(x)
RB_GC_GUARD
#define RB_GC_GUARD(v)
type
int type
Definition: tcltklib.c:111
FL_TAINT
#define FL_TAINT
include_modules_at
static int include_modules_at(const VALUE klass, VALUE c, VALUE module)
Definition: class.c:724
vm_core.h
rb_obj_public_methods
VALUE rb_obj_public_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1209
ins_methods_pub_i
static int ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary)
Definition: class.c:980
argc
int argc
Definition: tcltklib.c:1970
rb_alias
void rb_alias(VALUE, ID, ID)
Definition: vm_method.c:1182
rb_const_defined_at
int rb_const_defined_at(VALUE, ID)
Definition: variable.c:2109
NOEX_PROTECTED
Definition: method.h:26
rb_make_metaclass
VALUE rb_make_metaclass(VALUE obj, VALUE unused)
Definition: class.c:430
ST_CONTINUE
Definition: ripper.y:108
c
VpDivd * c
Definition: bigdecimal.c:1205
rb_class_instance_methods
VALUE rb_class_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:1071
rb_free_const_table
void rb_free_const_table(st_table *tbl)
Definition: gc.c:814
T_BIGNUM
#define T_BIGNUM
RCLASS_CONST_TBL
#define RCLASS_CONST_TBL(c)
rb_define_module_under
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:637
recur
#define recur(fmt)
Definition: date_strptime.c:147
rb_class2name
const char * rb_class2name(VALUE)
Definition: variable.c:389
rb_obj_init_copy
VALUE rb_obj_init_copy(VALUE, VALUE)
Definition: object.c:354
rb_class_protected_instance_methods
VALUE rb_class_protected_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:1086
NEWOBJ_OF
#define NEWOBJ_OF(obj, type, klass, flags)
rb_define_method_id
void rb_define_method_id(VALUE klass, ID mid, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1338
rb_class_public_instance_methods
VALUE rb_class_public_instance_methods(int argc, VALUE *argv, VALUE mod)
Definition: class.c:1124
rb_singleton_class_attached
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attach a object to a singleton class.
Definition: class.c:289
SYMBOL_P
#define SYMBOL_P(x)
FL_SINGLETON
#define FL_SINGLETON
rb_module_new
VALUE rb_module_new(void)
Definition: class.c:596
Qundef
#define Qundef
T_CLASS
#define T_CLASS
ENSURE_EIGENCLASS
#define ENSURE_EIGENCLASS(klass)
ensures klass belongs to its own eigenclass.
Definition: class.c:326
rb_obj_singleton_methods
VALUE rb_obj_singleton_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1248
rb_method_entry_struct::def
rb_method_definition_t * def
Definition: method.h:98
rb_define_attr
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:1552
rb_error_arity
void rb_error_arity(int argc, int min, int max)
Definition: vm_insnhelper.c:164
rb_cClass
RUBY_EXTERN VALUE rb_cClass
Definition: ripper.y:1430
rb_cObject
RUBY_EXTERN VALUE rb_cObject
Definition: ripper.y:1426
make_singleton_class
static VALUE make_singleton_class(VALUE obj)
Creates a singleton class for obj.
Definition: class.c:373
RBASIC
#define RBASIC(obj)
klass
klass
Definition: tcltklib.c:3504
method_entry_i
static int method_entry_i(st_data_t key, st_data_t value, st_data_t data)
Definition: class.c:986
list
struct rb_encoding_entry * list
Definition: encoding.c:50
rb_obj_methods
VALUE rb_obj_methods(int argc, VALUE *argv, VALUE obj)
Definition: class.c:1151
rb_add_refined_method_entry
void rb_add_refined_method_entry(VALUE refined_class, ID mid)
Definition: vm_method.c:210
st_insert
int st_insert(st_table *, st_data_t, st_data_t)
ins_methods_prot_i
static int ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary)
Definition: class.c:968
st.h
rb_set_class_path_string
void rb_set_class_path_string(VALUE, VALUE, VALUE)
Definition: variable.c:285
OBJ_UNTRUST
#define OBJ_UNTRUST(x)
move_refined_method
static int move_refined_method(st_data_t key, st_data_t value, st_data_t data)
Definition: class.c:774
T_MODULE
#define T_MODULE
rb_frozen_class_p
void rb_frozen_class_p(VALUE klass)
Definition: eval.c:403
RCLASS_IV_INDEX_TBL
#define RCLASS_IV_INDEX_TBL(c)
class_init_copy_check
static void class_init_copy_check(VALUE clone, VALUE orig)
Definition: class.c:181
rb_iseq_struct::self
VALUE self
Definition: vm_core.h:292
rb_iseq_struct
Definition: vm_core.h:196
GetISeqPtr
#define GetISeqPtr(obj, ptr)
Definition: vm_core.h:183
rb_id2name
const char * rb_id2name(ID id)
Definition: ripper.c:17005
rb_mod_included_modules
VALUE rb_mod_included_modules(VALUE mod)
Definition: class.c:854
BUILTIN_TYPE
#define BUILTIN_TYPE(x)
rb_class_real
VALUE rb_class_real(VALUE)
Definition: object.c:171
OBJ_UNTRUSTED
#define OBJ_UNTRUSTED(x)
rb_class_tbl
st_table * rb_class_tbl
Definition: variable.c:23
rb_define_class_id
VALUE rb_define_class_id(ID id, VALUE super)
Defines a new class.
Definition: class.c:452
rb_classext_struct
Definition: ripper.y:36
VALUE
unsigned long VALUE
Definition: ripper.y:104
VM_METHOD_TYPE_CFUNC
Definition: method.h:47
SPECIAL_SINGLETON
#define SPECIAL_SINGLETON(x, c)
Definition: class.c:1375
rb_method_entry_set
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:464
st_copy
st_table * st_copy(st_table *)
Definition: st.c:658
RMODULE_IS_REFINEMENT
#define RMODULE_IS_REFINEMENT
NOEX_PUBLIC
Definition: method.h:23
SPECIAL_CONST_P
#define SPECIAL_CONST_P(x)
OBJ_TAINT
#define OBJ_TAINT(x)
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:617
rb_cFalseClass
RUBY_EXTERN VALUE rb_cFalseClass
Definition: ripper.y:1434
rb_name_class
void rb_name_class(VALUE, ID)
Definition: variable.c:377
rb_intern
#define rb_intern(str)
clone_const
static int clone_const(ID key, const rb_const_entry_t *ce, st_table *tbl)
Definition: class.c:166
mod
#define mod(x, y)
Definition: date_strftime.c:28
RCLASS_M_TBL
#define RCLASS_M_TBL(c)
NULL
#define NULL
Definition: _sdbm.c:103
singleton_class_of
static VALUE singleton_class_of(VALUE obj)
Definition: class.c:1406
RClass
Definition: ripper.y:737
clone_method_i
static int clone_method_i(st_data_t key, st_data_t value, st_data_t data)
Definition: class.c:159
RCLASS_REFINED_CLASS
#define RCLASS_REFINED_CLASS(c)
name
const char * name
Definition: nkf.c:208
rb_add_method_cfunc
void rb_add_method_cfunc(VALUE klass, ID mid, VALUE(*func)(ANYARGS), int argc, rb_method_flag_t noex)
Definition: vm_method.c:84
rb_iseq_clone
VALUE rb_iseq_clone(VALUE iseqval, VALUE newcbase)
Definition: iseq.c:1896
rb_class_new
VALUE rb_class_new(VALUE super)
Creates a new class.
Definition: class.c:117
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1344
retry
int retry
Definition: tcltklib.c:10151
rb_warn
void rb_warn(const char *fmt,...)
Definition: error.c:216
NOEX_UNDEF
Definition: method.h:29
rb_class_boot
VALUE rb_class_boot(VALUE super)
A utility function that wraps class_alloc.
Definition: class.c:76
rb_eArgError
VALUE rb_eArgError
Definition: error.c:512
rb_free_m_table
void rb_free_m_table(st_table *tbl)
Definition: gc.c:800
NOEX_PRIVATE
Definition: method.h:25
st_free_table
void st_free_table(st_table *)
Definition: st.c:334
RMODULE_CONST_TBL
#define RMODULE_CONST_TBL(m)
FL_UNSET
#define FL_UNSET(x, f)
rb_singleton_class_clone_and_attach
VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach)
Definition: class.c:247
RMODULE_INCLUDED_INTO_REFINEMENT
#define RMODULE_INCLUDED_INTO_REFINEMENT