| 1 |
#ifndef RBIMPL_INTERN_CLASS_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_INTERN_CLASS_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief Public APIs related to ::rb_cClass/::rb_cModule. |
| 22 |
*/ |
| 23 |
#include "ruby/internal/dllexport.h" |
| 24 |
#include "ruby/internal/value.h" |
| 25 |
#include "ruby/backward/2/stdarg.h" |
| 26 |
|
| 27 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 28 |
|
| 29 |
/* class.c */ |
| 30 |
|
| 31 |
/** |
| 32 |
* Creates a new, anonymous class. |
| 33 |
* |
| 34 |
* @param[in] super What would become a parent class. |
| 35 |
* @exception rb_eTypeError `super` is not something inheritable. |
| 36 |
* @return An anonymous class that inherits `super`. |
| 37 |
*/ |
| 38 |
VALUE rb_class_new(VALUE super); |
| 39 |
|
| 40 |
/** |
| 41 |
* The comment that comes with this function says `:nodoc:`. Not sure what |
| 42 |
* that means though. |
| 43 |
* |
| 44 |
* @param[out] clone Destination object. |
| 45 |
* @param[in] orig Source object. |
| 46 |
* @exception rb_eTypeError Cannot copy `orig`. |
| 47 |
* @return The passed `clone`. |
| 48 |
*/ |
| 49 |
VALUE rb_mod_init_copy(VALUE clone, VALUE orig); |
| 50 |
|
| 51 |
/** |
| 52 |
* Asserts that the given class can derive a child class. A class might or |
| 53 |
* might not be able to do so; for instance a singleton class cannot. |
| 54 |
* |
| 55 |
* @param[in] super Possible super class. |
| 56 |
* @exception rb_eTypeError No it cannot. |
| 57 |
* @post Upon successful return `super` can derive. |
| 58 |
*/ |
| 59 |
void rb_check_inheritable(VALUE super); |
| 60 |
|
| 61 |
/** |
| 62 |
* This is a very badly designed API that creates an anonymous class. |
| 63 |
* |
| 64 |
* @param[in] id Discarded for no reason (why...). |
| 65 |
* @param[in] super What would become a parent class. 0 means |
| 66 |
* ::rb_cObject. |
| 67 |
* @exception rb_eTypeError `super` is not something inheritable. |
| 68 |
* @return An anonymous class that inherits `super`. |
| 69 |
* @warning You must explicitly name the return value. |
| 70 |
*/ |
| 71 |
VALUE rb_define_class_id(ID id, VALUE super); |
| 72 |
|
| 73 |
/** |
| 74 |
* Identical to rb_define_class_under(), except it takes the name in ::ID |
| 75 |
* instead of C's string. |
| 76 |
* |
| 77 |
* @param[out] outer A class which contains the new class. |
| 78 |
* @param[in] id Name of the new class |
| 79 |
* @param[in] super A class from which the new class will derive. |
| 80 |
* 0 means ::rb_cObject. |
| 81 |
* @exception rb_eTypeError The constant name `id` is already taken but the |
| 82 |
* constant is not a class. |
| 83 |
* @exception rb_eTypeError The class is already defined but the class can |
| 84 |
* not be reopened because its superclass is not |
| 85 |
* `super`. |
| 86 |
* @exception rb_eArgError `super` is NULL. |
| 87 |
* @return The created class. |
| 88 |
* @post `outer::id` refers the returned class. |
| 89 |
* @note If a class named `id` is already defined and its superclass is |
| 90 |
* `super`, the function just returns the defined class. |
| 91 |
* @note The compaction GC does not move classes returned by this |
| 92 |
* function. |
| 93 |
*/ |
| 94 |
VALUE rb_define_class_id_under(VALUE outer, ID id, VALUE super); |
| 95 |
|
| 96 |
/** |
| 97 |
* Creates a new, anonymous module. |
| 98 |
* |
| 99 |
* @return An anonymous module. |
| 100 |
*/ |
| 101 |
VALUE rb_module_new(void); |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* Creates a new, anonymous refinement. |
| 106 |
* |
| 107 |
* @return An anonymous refinement. |
| 108 |
*/ |
| 109 |
VALUE rb_refinement_new(void); |
| 110 |
|
| 111 |
/** |
| 112 |
* This is a very badly designed API that creates an anonymous module. |
| 113 |
* |
| 114 |
* @param[in] id Discarded for no reason (why...). |
| 115 |
* @return An anonymous module. |
| 116 |
* @warning You must explicitly name the return value. |
| 117 |
*/ |
| 118 |
VALUE rb_define_module_id(ID id); |
| 119 |
|
| 120 |
/** |
| 121 |
* Identical to rb_define_module_under(), except it takes the name in ::ID |
| 122 |
* instead of C's string. |
| 123 |
* |
| 124 |
* @param[out] outer A class which contains the new module. |
| 125 |
* @param[in] id Name of the new module |
| 126 |
* @exception rb_eTypeError The constant name `id` is already taken but the |
| 127 |
* constant is not a module. |
| 128 |
* @return The created module. |
| 129 |
* @post `outer::id` refers the returned module. |
| 130 |
* @note The compaction GC does not move classes returned by this |
| 131 |
* function. |
| 132 |
*/ |
| 133 |
VALUE rb_define_module_id_under(VALUE outer, ID id); |
| 134 |
|
| 135 |
/** |
| 136 |
* Queries the list of included modules. It can also be seen as a routine to |
| 137 |
* first call rb_mod_ancestors(), then rejects non-modules from the return |
| 138 |
* value. |
| 139 |
* |
| 140 |
* @param[in] mod Class or Module. |
| 141 |
* @return An array of modules that are either included or prepended in any |
| 142 |
* of `mod`'s ancestry tree (including itself). |
| 143 |
*/ |
| 144 |
VALUE rb_mod_included_modules(VALUE mod); |
| 145 |
|
| 146 |
/** |
| 147 |
* Queries if the passed module is included by the module. It can also be seen |
| 148 |
* as a routine to first call rb_mod_included_modules(), then see if the return |
| 149 |
* value contains the passed module. |
| 150 |
* |
| 151 |
* @param[in] child A Module. |
| 152 |
* @param[in] parent Another Module. |
| 153 |
* @exception rb_eTypeError `child` is not an instance of ::rb_cModule. |
| 154 |
* @retval RUBY_Qtrue `parent` is either included or prepended in any |
| 155 |
* of `child`'s ancestry tree (including itself). |
| 156 |
* @return RUBY_Qfalse Otherwise. |
| 157 |
*/ |
| 158 |
VALUE rb_mod_include_p(VALUE child, VALUE parent); |
| 159 |
|
| 160 |
/** |
| 161 |
* Queries the module's ancestors. This routine gathers classes and modules |
| 162 |
* that the passed module either inherits, includes, or prepends, then |
| 163 |
* recursively applies that routine again and again to the collected entries |
| 164 |
* until the list doesn't grow up. |
| 165 |
* |
| 166 |
* @param[in] mod A module or a class. |
| 167 |
* @return An array of classes or modules that `mod` possibly recursively |
| 168 |
* inherits, includes, or prepends. |
| 169 |
* |
| 170 |
* @internal |
| 171 |
* |
| 172 |
* Above description is written in a recursive language but in practice it |
| 173 |
* computes the return value iteratively. |
| 174 |
*/ |
| 175 |
VALUE rb_mod_ancestors(VALUE mod); |
| 176 |
|
| 177 |
/** |
| 178 |
* Queries the class's descendants. This routine gathers classes that are |
| 179 |
* subclasses of the given class (or subclasses of those subclasses, etc.), |
| 180 |
* returning an array of classes that have the given class as an ancestor. |
| 181 |
* The returned array does not include the given class or singleton classes. |
| 182 |
* |
| 183 |
* @param[in] klass A class. |
| 184 |
* @return An array of classes where `klass` is an ancestor. |
| 185 |
* |
| 186 |
* @internal |
| 187 |
*/ |
| 188 |
VALUE rb_class_descendants(VALUE klass); |
| 189 |
|
| 190 |
/** |
| 191 |
* Queries the class's direct descendants. This routine gathers classes that are |
| 192 |
* direct subclasses of the given class, |
| 193 |
* returning an array of classes that have the given class as a superclass. |
| 194 |
* The returned array does not include singleton classes. |
| 195 |
* |
| 196 |
* @param[in] klass A class. |
| 197 |
* @return An array of classes where `klass` is the `superclass`. |
| 198 |
* |
| 199 |
* @internal |
| 200 |
*/ |
| 201 |
VALUE rb_class_subclasses(VALUE klass); |
| 202 |
|
| 203 |
|
| 204 |
/** |
| 205 |
* Returns the attached object for a singleton class. |
| 206 |
* If the given class is not a singleton class, raises a TypeError. |
| 207 |
* |
| 208 |
* @param[in] klass A class. |
| 209 |
* @return The object which has the singleton class `klass`. |
| 210 |
* |
| 211 |
* @internal |
| 212 |
*/ |
| 213 |
VALUE rb_class_attached_object(VALUE klass); |
| 214 |
|
| 215 |
/** |
| 216 |
* Generates an array of symbols, which are the list of method names defined in |
| 217 |
* the passed class. |
| 218 |
* |
| 219 |
* @param[in] argc Number of objects of `argv`. |
| 220 |
* @param[in] argv Array of at most one object, which controls (if |
| 221 |
* any) whether the return array includes the names |
| 222 |
* of methods defined in ancestors or not. |
| 223 |
* @param[in] mod A module or a class. |
| 224 |
* @exception rb_eArgError `argc` out of range. |
| 225 |
* @return An array of symbols collecting names of instance methods that |
| 226 |
* are not private, defined at `mod`. |
| 227 |
*/ |
| 228 |
VALUE rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod); |
| 229 |
|
| 230 |
/** |
| 231 |
* Identical to rb_class_instance_methods(), except it returns names of methods |
| 232 |
* that are public only. |
| 233 |
* |
| 234 |
* @param[in] argc Number of objects of `argv`. |
| 235 |
* @param[in] argv Array of at most one object, which controls (if |
| 236 |
* any) whether the return array includes the names |
| 237 |
* of methods defined in ancestors or not. |
| 238 |
* @param[in] mod A module or a class. |
| 239 |
* @exception rb_eArgError `argc` out of range. |
| 240 |
* @return An array of symbols collecting names of instance methods that |
| 241 |
* are public, defined at `mod`. |
| 242 |
*/ |
| 243 |
VALUE rb_class_public_instance_methods(int argc, const VALUE *argv, VALUE mod); |
| 244 |
|
| 245 |
/** |
| 246 |
* Identical to rb_class_instance_methods(), except it returns names of methods |
| 247 |
* that are protected only. |
| 248 |
* |
| 249 |
* @param[in] argc Number of objects of `argv`. |
| 250 |
* @param[in] argv Array of at most one object, which controls (if |
| 251 |
* any) whether the return array includes the names |
| 252 |
* of methods defined in ancestors or not. |
| 253 |
* @param[in] mod A module or a class. |
| 254 |
* @exception rb_eArgError `argc` out of range. |
| 255 |
* @return An array of symbols collecting names of instance methods that |
| 256 |
* are protected, defined at `mod`. |
| 257 |
*/ |
| 258 |
VALUE rb_class_protected_instance_methods(int argc, const VALUE *argv, VALUE mod); |
| 259 |
|
| 260 |
/** |
| 261 |
* Identical to rb_class_instance_methods(), except it returns names of methods |
| 262 |
* that are private only. |
| 263 |
* |
| 264 |
* @param[in] argc Number of objects of `argv`. |
| 265 |
* @param[in] argv Array of at most one object, which controls (if |
| 266 |
* any) whether the return array includes the names |
| 267 |
* of methods defined in ancestors or not. |
| 268 |
* @param[in] mod A module or a class. |
| 269 |
* @exception rb_eArgError `argc` out of range. |
| 270 |
* @return An array of symbols collecting names of instance methods that |
| 271 |
* are protected, defined at `mod`. |
| 272 |
*/ |
| 273 |
VALUE rb_class_private_instance_methods(int argc, const VALUE *argv, VALUE mod); |
| 274 |
|
| 275 |
/** |
| 276 |
* Identical to rb_class_instance_methods(), except it returns names of |
| 277 |
* singleton methods instead of instance methods. |
| 278 |
* |
| 279 |
* @param[in] argc Number of objects of `argv`. |
| 280 |
* @param[in] argv Array of at most one object, which controls (if |
| 281 |
* any) whether the return array includes the names |
| 282 |
* of methods defined in ancestors or not. |
| 283 |
* @param[in] obj Arbitrary ruby object. |
| 284 |
* @exception rb_eArgError `argc` out of range. |
| 285 |
* @return An array of symbols collecting names of instance methods that |
| 286 |
* are not private, defined at the singleton class of `obj`. |
| 287 |
*/ |
| 288 |
VALUE rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj); |
| 289 |
|
| 290 |
/** |
| 291 |
* Identical to rb_define_method(), except it takes the name of the method in |
| 292 |
* ::ID instead of C's string. |
| 293 |
* |
| 294 |
* @param[out] klass A module or a class. |
| 295 |
* @param[in] mid Name of the function. |
| 296 |
* @param[in] func The method body. |
| 297 |
* @param[in] arity The number of parameters. See @ref defmethod. |
| 298 |
* @note There are in fact 18 different prototypes for func. |
| 299 |
* @see ::ruby::backward::cxxanyargs::define_method::rb_define_method_id |
| 300 |
*/ |
| 301 |
void rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int arity); |
| 302 |
|
| 303 |
/* vm_method.c */ |
| 304 |
|
| 305 |
/** |
| 306 |
* Inserts a method entry that hides previous method definition of the given |
| 307 |
* name. This is not a deletion of a method. Method of the same name defined |
| 308 |
* in a parent class is kept invisible in this way. |
| 309 |
* |
| 310 |
* @param[out] mod The module to insert an undef. |
| 311 |
* @param[in] mid Name of the undef. |
| 312 |
* @exception rb_eTypeError `klass` is a non-module. |
| 313 |
* @exception rb_eFrozenError `klass` is frozen. |
| 314 |
* @exception rb_eNameError No such method named `klass#name`. |
| 315 |
* @post `klass#name` is undefined. |
| 316 |
* @see rb_undef_method |
| 317 |
* |
| 318 |
* @internal |
| 319 |
* |
| 320 |
* @shyouhei doesn't understand why this is not the ::ID -taking variant of |
| 321 |
* rb_undef_method(), given rb_remove_method() has its ::ID -taking counterpart |
| 322 |
* named rb_remove_method_id(). |
| 323 |
*/ |
| 324 |
void rb_undef(VALUE mod, ID mid); |
| 325 |
|
| 326 |
/* class.c */ |
| 327 |
|
| 328 |
RBIMPL_ATTR_NONNULL(()) |
| 329 |
/** |
| 330 |
* Identical to rb_define_method(), except it defines a protected method. |
| 331 |
* |
| 332 |
* @param[out] klass A module or a class. |
| 333 |
* @param[in] mid Name of the function. |
| 334 |
* @param[in] func The method body. |
| 335 |
* @param[in] arity The number of parameters. See @ref defmethod. |
| 336 |
* @note There are in fact 18 different prototypes for func. |
| 337 |
* @see ::ruby::backward::cxxanyargs::define_method::rb_define_protected_method |
| 338 |
*/ |
| 339 |
void rb_define_protected_method(VALUE klass, const char *mid, VALUE (*func)(ANYARGS), int arity); |
| 340 |
|
| 341 |
RBIMPL_ATTR_NONNULL(()) |
| 342 |
/** |
| 343 |
* Identical to rb_define_method(), except it defines a private method. |
| 344 |
* |
| 345 |
* @param[out] klass A module or a class. |
| 346 |
* @param[in] mid Name of the function. |
| 347 |
* @param[in] func The method body. |
| 348 |
* @param[in] arity The number of parameters. See @ref defmethod. |
| 349 |
* @note There are in fact 18 different prototypes for func. |
| 350 |
* @see ::ruby::backward::cxxanyargs::define_method::rb_define_protected_method |
| 351 |
*/ |
| 352 |
void rb_define_private_method(VALUE klass, const char *mid, VALUE (*func)(ANYARGS), int arity); |
| 353 |
|
| 354 |
RBIMPL_ATTR_NONNULL(()) |
| 355 |
/** |
| 356 |
* Identical to rb_define_method(), except it defines a singleton method. |
| 357 |
* |
| 358 |
* @param[out] obj Arbitrary ruby object. |
| 359 |
* @param[in] mid Name of the function. |
| 360 |
* @param[in] func The method body. |
| 361 |
* @param[in] arity The number of parameters. See @ref defmethod. |
| 362 |
* @note There are in fact 18 different prototypes for func. |
| 363 |
* @see ::ruby::backward::cxxanyargs::define_method::rb_define_singleton_method |
| 364 |
*/ |
| 365 |
void rb_define_singleton_method(VALUE obj, const char *mid, VALUE(*func)(ANYARGS), int arity); |
| 366 |
|
| 367 |
/** |
| 368 |
* Finds or creates the singleton class of the passed object. |
| 369 |
* |
| 370 |
* @param[out] obj Arbitrary ruby object. |
| 371 |
* @exception rb_eTypeError `obj` cannot have its singleton class. |
| 372 |
* @return A (possibly newly allocated) instance of ::rb_cClass. |
| 373 |
* @post `obj` has its singleton class, which is the return value. |
| 374 |
* @post In case `obj` is a class, the returned singleton class also has |
| 375 |
* its own singleton class in order to keep consistency of the |
| 376 |
* inheritance structure of metaclasses. |
| 377 |
* @note A new singleton class will be created if `obj` did not have |
| 378 |
* one. |
| 379 |
* @note The singleton classes for ::RUBY_Qnil, ::RUBY_Qtrue, and |
| 380 |
* ::RUBY_Qfalse are ::rb_cNilClass, ::rb_cTrueClass, and |
| 381 |
* ::rb_cFalseClass respectively. |
| 382 |
* |
| 383 |
* @internal |
| 384 |
* |
| 385 |
* You can _create_ a singleton class of a frozen object. Intentional or ...? |
| 386 |
* |
| 387 |
* Nowadays there are wider range of objects who cannot have singleton classes |
| 388 |
* than before. For instance some string instances cannot for some reason. |
| 389 |
*/ |
| 390 |
VALUE rb_singleton_class(VALUE obj); |
| 391 |
|
| 392 |
RBIMPL_SYMBOL_EXPORT_END() |
| 393 |
|
| 394 |
#endif /* RBIMPL_INTERN_CLASS_H */ |