2020-04-10 01:11:40 -04:00
|
|
|
#ifndef INTERNAL_CLASS_H /*-*-C-*-vi:se ft=c:*/
|
|
|
|
#define INTERNAL_CLASS_H
|
|
|
|
/**
|
2020-04-08 00:28:13 -04:00
|
|
|
* @author Ruby developers <ruby-core@ruby-lang.org>
|
2019-11-29 01:18:34 -05:00
|
|
|
* @copyright This file is a part of the programming language Ruby.
|
|
|
|
* Permission is hereby granted, to either redistribute and/or
|
|
|
|
* modify this file, provided that the conditions mentioned in the
|
|
|
|
* file COPYING are met. Consult the file for details.
|
2020-04-08 00:28:13 -04:00
|
|
|
* @brief Internal header for Class.
|
2019-11-29 01:18:34 -05:00
|
|
|
*/
|
2019-12-02 23:49:14 -05:00
|
|
|
#include "id_table.h" /* for struct rb_id_table */
|
|
|
|
#include "internal/gc.h" /* for RB_OBJ_WRITE */
|
|
|
|
#include "internal/serial.h" /* for rb_serial_t */
|
2020-05-08 05:31:09 -04:00
|
|
|
#include "ruby/internal/stdbool.h" /* for bool */
|
2019-12-02 23:49:14 -05:00
|
|
|
#include "ruby/intern.h" /* for rb_alloc_func_t */
|
|
|
|
#include "ruby/ruby.h" /* for struct RBasic */
|
|
|
|
|
2019-12-04 03:16:30 -05:00
|
|
|
#ifdef RCLASS_SUPER
|
|
|
|
# undef RCLASS_SUPER
|
|
|
|
#endif
|
2019-11-29 01:18:34 -05:00
|
|
|
|
|
|
|
struct rb_subclass_entry {
|
|
|
|
VALUE klass;
|
2019-12-02 23:49:14 -05:00
|
|
|
struct rb_subclass_entry *next;
|
2021-11-15 16:09:10 -05:00
|
|
|
struct rb_subclass_entry *prev;
|
2019-11-29 01:18:34 -05:00
|
|
|
};
|
|
|
|
|
2020-10-16 02:20:40 -04:00
|
|
|
struct rb_iv_index_tbl_entry {
|
|
|
|
uint32_t index;
|
|
|
|
rb_serial_t class_serial;
|
|
|
|
VALUE class_value;
|
|
|
|
};
|
|
|
|
|
2021-06-01 13:34:06 -04:00
|
|
|
struct rb_cvar_class_tbl_entry {
|
|
|
|
uint32_t index;
|
|
|
|
rb_serial_t global_cvar_state;
|
|
|
|
VALUE class_value;
|
|
|
|
};
|
|
|
|
|
2019-11-29 01:18:34 -05:00
|
|
|
struct rb_classext_struct {
|
2020-10-16 02:20:40 -04:00
|
|
|
struct st_table *iv_index_tbl; // ID -> struct rb_iv_index_tbl_entry
|
2019-11-29 01:18:34 -05:00
|
|
|
struct st_table *iv_tbl;
|
|
|
|
#if SIZEOF_SERIAL_T == SIZEOF_VALUE /* otherwise m_tbl is in struct RClass */
|
|
|
|
struct rb_id_table *m_tbl;
|
|
|
|
#endif
|
|
|
|
struct rb_id_table *const_tbl;
|
|
|
|
struct rb_id_table *callable_m_tbl;
|
2020-01-08 02:14:01 -05:00
|
|
|
struct rb_id_table *cc_tbl; /* ID -> [[ci, cc1], cc2, ...] */
|
2021-06-01 13:34:06 -04:00
|
|
|
struct rb_id_table *cvc_tbl;
|
2019-12-02 23:49:14 -05:00
|
|
|
struct rb_subclass_entry *subclasses;
|
2021-11-15 16:09:10 -05:00
|
|
|
struct rb_subclass_entry *subclass_entry;
|
2019-11-29 01:18:34 -05:00
|
|
|
/**
|
|
|
|
* In the case that this is an `ICLASS`, `module_subclasses` points to the link
|
|
|
|
* in the module's `subclasses` list that indicates that the klass has been
|
|
|
|
* included. Hopefully that makes sense.
|
|
|
|
*/
|
2021-11-15 16:09:10 -05:00
|
|
|
struct rb_subclass_entry *module_subclass_entry;
|
2019-11-29 01:18:34 -05:00
|
|
|
#if SIZEOF_SERIAL_T != SIZEOF_VALUE /* otherwise class_serial is in struct RClass */
|
|
|
|
rb_serial_t class_serial;
|
|
|
|
#endif
|
|
|
|
const VALUE origin_;
|
|
|
|
const VALUE refined_class;
|
|
|
|
rb_alloc_func_t allocator;
|
|
|
|
const VALUE includer;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RClass {
|
|
|
|
struct RBasic basic;
|
|
|
|
VALUE super;
|
2021-11-10 16:33:17 -05:00
|
|
|
#if !USE_RVARGC
|
2019-12-02 23:49:14 -05:00
|
|
|
struct rb_classext_struct *ptr;
|
2021-11-10 16:33:17 -05:00
|
|
|
#endif
|
2019-11-29 01:18:34 -05:00
|
|
|
#if SIZEOF_SERIAL_T == SIZEOF_VALUE
|
|
|
|
/* Class serial is as wide as VALUE. Place it here. */
|
|
|
|
rb_serial_t class_serial;
|
|
|
|
#else
|
|
|
|
/* Class serial does not fit into struct RClass. Place m_tbl instead. */
|
|
|
|
struct rb_id_table *m_tbl;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2019-12-02 23:49:14 -05:00
|
|
|
typedef struct rb_subclass_entry rb_subclass_entry_t;
|
|
|
|
typedef struct rb_classext_struct rb_classext_t;
|
2019-11-29 01:18:34 -05:00
|
|
|
|
2021-11-10 16:33:17 -05:00
|
|
|
#if USE_RVARGC
|
|
|
|
# define RCLASS_EXT(c) ((rb_classext_t *)((char *)c + sizeof(struct RClass)))
|
|
|
|
#else
|
|
|
|
# define RCLASS_EXT(c) (RCLASS(c)->ptr)
|
|
|
|
#endif
|
2019-11-29 01:18:34 -05:00
|
|
|
#define RCLASS_IV_TBL(c) (RCLASS_EXT(c)->iv_tbl)
|
|
|
|
#define RCLASS_CONST_TBL(c) (RCLASS_EXT(c)->const_tbl)
|
|
|
|
#if SIZEOF_SERIAL_T == SIZEOF_VALUE
|
|
|
|
# define RCLASS_M_TBL(c) (RCLASS_EXT(c)->m_tbl)
|
|
|
|
#else
|
|
|
|
# define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl)
|
|
|
|
#endif
|
|
|
|
#define RCLASS_CALLABLE_M_TBL(c) (RCLASS_EXT(c)->callable_m_tbl)
|
2020-01-08 02:14:01 -05:00
|
|
|
#define RCLASS_CC_TBL(c) (RCLASS_EXT(c)->cc_tbl)
|
2021-06-01 13:34:06 -04:00
|
|
|
#define RCLASS_CVC_TBL(c) (RCLASS_EXT(c)->cvc_tbl)
|
2019-11-29 01:18:34 -05:00
|
|
|
#define RCLASS_IV_INDEX_TBL(c) (RCLASS_EXT(c)->iv_index_tbl)
|
|
|
|
#define RCLASS_ORIGIN(c) (RCLASS_EXT(c)->origin_)
|
|
|
|
#define RCLASS_REFINED_CLASS(c) (RCLASS_EXT(c)->refined_class)
|
|
|
|
#if SIZEOF_SERIAL_T == SIZEOF_VALUE
|
|
|
|
# define RCLASS_SERIAL(c) (RCLASS(c)->class_serial)
|
|
|
|
#else
|
|
|
|
# define RCLASS_SERIAL(c) (RCLASS_EXT(c)->class_serial)
|
|
|
|
#endif
|
|
|
|
#define RCLASS_INCLUDER(c) (RCLASS_EXT(c)->includer)
|
2021-11-15 16:09:10 -05:00
|
|
|
#define RCLASS_SUBCLASS_ENTRY(c) (RCLASS_EXT(c)->subclass_entry)
|
|
|
|
#define RCLASS_MODULE_SUBCLASS_ENTRY(c) (RCLASS_EXT(c)->module_subclass_entry)
|
2021-01-26 11:29:09 -05:00
|
|
|
#define RCLASS_ALLOCATOR(c) (RCLASS_EXT(c)->allocator)
|
2021-01-26 11:49:57 -05:00
|
|
|
#define RCLASS_SUBCLASSES(c) (RCLASS_EXT(c)->subclasses)
|
2019-11-29 01:18:34 -05:00
|
|
|
|
2020-05-22 10:54:34 -04:00
|
|
|
#define RICLASS_IS_ORIGIN FL_USER5
|
2020-04-05 15:10:42 -04:00
|
|
|
#define RCLASS_CLONED FL_USER6
|
|
|
|
#define RICLASS_ORIGIN_SHARED_MTBL FL_USER8
|
2019-11-29 01:18:34 -05:00
|
|
|
|
2019-12-02 23:49:14 -05:00
|
|
|
/* class.c */
|
|
|
|
void rb_class_subclass_add(VALUE super, VALUE klass);
|
|
|
|
void rb_class_remove_from_super_subclasses(VALUE);
|
2021-11-15 16:09:10 -05:00
|
|
|
void rb_class_remove_subclass_head(VALUE);
|
2019-12-02 23:49:14 -05:00
|
|
|
int rb_singleton_class_internal_p(VALUE sklass);
|
|
|
|
VALUE rb_class_boot(VALUE);
|
2020-07-25 22:52:19 -04:00
|
|
|
VALUE rb_class_s_alloc(VALUE klass);
|
|
|
|
VALUE rb_module_s_alloc(VALUE klass);
|
2021-09-23 11:55:11 -04:00
|
|
|
void rb_module_set_initialized(VALUE module);
|
2021-09-19 09:39:18 -04:00
|
|
|
void rb_module_check_initializable(VALUE module);
|
2019-12-02 23:49:14 -05:00
|
|
|
VALUE rb_make_metaclass(VALUE, VALUE);
|
|
|
|
VALUE rb_include_class_new(VALUE, VALUE);
|
|
|
|
void rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE);
|
|
|
|
void rb_class_detach_subclasses(VALUE);
|
|
|
|
void rb_class_detach_module_subclasses(VALUE);
|
|
|
|
void rb_class_remove_from_module_subclasses(VALUE);
|
|
|
|
VALUE rb_obj_methods(int argc, const VALUE *argv, VALUE obj);
|
|
|
|
VALUE rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj);
|
|
|
|
VALUE rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj);
|
|
|
|
VALUE rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj);
|
|
|
|
VALUE rb_special_singleton_class(VALUE);
|
|
|
|
VALUE rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach);
|
|
|
|
VALUE rb_singleton_class_get(VALUE obj);
|
|
|
|
void rb_undef_methods_from(VALUE klass, VALUE super);
|
Ensure origins for all included, prepended, and refined modules
This fixes various issues when a module is included in or prepended
to a module or class, and then refined, or refined and then included
or prepended to a module or class.
Implement by renaming ensure_origin to rb_ensure_origin, making it
non-static, and calling it when refining a module.
Fix Module#initialize_copy to handle origins correctly. Previously,
Module#initialize_copy did not handle origins correctly. For example,
this code:
```ruby
module B; end
class A
def b; 2 end
prepend B
end
a = A.dup.new
class A
def b; 1 end
end
p a.b
```
Printed 1 instead of 2. This is because the super chain for
a.singleton_class was:
```
a.singleton_class
A.dup
B(iclass)
B(iclass origin)
A(origin) # not A.dup(origin)
```
The B iclasses would not be modified, so the includer entry would be
still be set to A and not A.dup.
This modifies things so that if the class/module has an origin,
all iclasses between the class/module and the origin are duplicated
and have the correct includer entry set, and the correct origin
is created.
This requires other changes to make sure all tests still pass:
* rb_undef_methods_from doesn't automatically handle classes with
origins, so pass it the origin for Comparable when undefing
methods in Complex. This fixed a failure in the Complex tests.
* When adding a method, the method cache was not cleared
correctly if klass has an origin. Clear the method cache for
the klass before switching to the origin of klass. This fixed
failures in the autoload tests related to overridding require,
without breaking the optimization tests. Also clear the method
cache for both the module and origin when removing a method.
* Module#include? is fixed to skip origin iclasses.
* Refinements are fixed to use the origin class of the module that
has an origin.
* RCLASS_REFINED_BY_ANY is removed as it was only used in a single
place and is no longer needed.
* Marshal#dump is fixed to skip iclass origins.
* rb_method_entry_make is fixed to handled overridden optimized
methods for modules that have origins.
Fixes [Bug #16852]
2020-05-23 23:16:27 -04:00
|
|
|
|
2019-12-02 23:49:14 -05:00
|
|
|
static inline void RCLASS_SET_ORIGIN(VALUE klass, VALUE origin);
|
2020-04-05 15:10:42 -04:00
|
|
|
static inline void RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass);
|
2019-12-02 23:49:14 -05:00
|
|
|
static inline VALUE RCLASS_SUPER(VALUE klass);
|
|
|
|
static inline VALUE RCLASS_SET_SUPER(VALUE klass, VALUE super);
|
|
|
|
static inline void RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass);
|
|
|
|
|
|
|
|
MJIT_SYMBOL_EXPORT_BEGIN
|
|
|
|
VALUE rb_class_inherited(VALUE, VALUE);
|
|
|
|
VALUE rb_keyword_error_new(const char *, VALUE);
|
|
|
|
MJIT_SYMBOL_EXPORT_END
|
|
|
|
|
2019-11-29 01:18:34 -05:00
|
|
|
static inline void
|
|
|
|
RCLASS_SET_ORIGIN(VALUE klass, VALUE origin)
|
|
|
|
{
|
|
|
|
RB_OBJ_WRITE(klass, &RCLASS_ORIGIN(klass), origin);
|
|
|
|
if (klass != origin) FL_SET(origin, RICLASS_IS_ORIGIN);
|
|
|
|
}
|
|
|
|
|
2020-04-05 15:10:42 -04:00
|
|
|
static inline void
|
|
|
|
RICLASS_SET_ORIGIN_SHARED_MTBL(VALUE iclass)
|
|
|
|
{
|
|
|
|
FL_SET(iclass, RICLASS_ORIGIN_SHARED_MTBL);
|
|
|
|
}
|
|
|
|
|
2020-08-10 18:19:17 -04:00
|
|
|
static inline bool
|
|
|
|
RICLASS_OWNS_M_TBL_P(VALUE iclass)
|
|
|
|
{
|
|
|
|
return FL_TEST_RAW(iclass, RICLASS_IS_ORIGIN | RICLASS_ORIGIN_SHARED_MTBL) == RICLASS_IS_ORIGIN;
|
|
|
|
}
|
|
|
|
|
2019-11-29 01:18:34 -05:00
|
|
|
static inline void
|
|
|
|
RCLASS_SET_INCLUDER(VALUE iclass, VALUE klass)
|
|
|
|
{
|
|
|
|
RB_OBJ_WRITE(iclass, &RCLASS_INCLUDER(iclass), klass);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline VALUE
|
|
|
|
RCLASS_SUPER(VALUE klass)
|
|
|
|
{
|
|
|
|
return RCLASS(klass)->super;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline VALUE
|
|
|
|
RCLASS_SET_SUPER(VALUE klass, VALUE super)
|
|
|
|
{
|
|
|
|
if (super) {
|
|
|
|
rb_class_remove_from_super_subclasses(klass);
|
|
|
|
rb_class_subclass_add(super, klass);
|
|
|
|
}
|
|
|
|
RB_OBJ_WRITE(klass, &RCLASS(klass)->super, super);
|
|
|
|
return super;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* INTERNAL_CLASS_H */
|