Debian Codegraph @

include/ruby/internal/intern/variable.h

raw
1 #ifndef RBIMPL_INTERN_VARIABLE_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_INTERN_VARIABLE_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 names inside of a Ruby program.
22 */
23 #include "ruby/internal/attr/nonnull.h"
24 #include "ruby/internal/attr/noreturn.h"
25 #include "ruby/internal/dllexport.h"
26 #include "ruby/internal/value.h"
27 #include "ruby/st.h"
28
29 RBIMPL_SYMBOL_EXPORT_BEGIN()
30
31 /* variable.c */
32
33 /**
34 * Queries the name of a module.
35 *
36 * @param[in] mod An instance of ::rb_cModule.
37 * @retval RUBY_Qnil `mod` is anonymous.
38 * @retval otherwise `mod` is onymous.
39 */
40 VALUE rb_mod_name(VALUE mod);
41
42 /**
43 * Identical to rb_mod_name(), except it returns `#<Class: ...>` style
44 * inspection for anonymous modules.
45 *
46 * @param[in] mod An instance of ::rb_cModule.
47 * @return An instance of ::rb_cString representing `mod`'s path.
48 */
49 VALUE rb_class_path(VALUE mod);
50
51 /**
52 * @alias{rb_mod_name}
53 *
54 * @internal
55 *
56 * Am I missing something? Why we have the same thing in different names?
57 */
58 VALUE rb_class_path_cached(VALUE mod);
59
60 RBIMPL_ATTR_NONNULL(())
61 /**
62 * Names a class.
63 *
64 * @param[out] klass Target module to name.
65 * @param[out] space Namespace that `klass` shall reside.
66 * @param[in] name Name of `klass`.
67 * @post `klass` has `space::klass` name.
68 */
69 void rb_set_class_path(VALUE klass, VALUE space, const char *name);
70
71 /**
72 * Identical to rb_set_class_path(), except it accepts the name as Ruby's
73 * string instead of C's.
74 *
75 * @param[out] klass Target module to name.
76 * @param[out] space Namespace that `klass` shall reside.
77 * @param[in] name Name of `klass`.
78 * @post `klass` has `space::klass` name.
79 */
80 void rb_set_class_path_string(VALUE klass, VALUE space, VALUE name);
81
82 /**
83 * Identical to rb_path2class(), except it accepts the path as Ruby's string
84 * instead of C's.
85 *
86 * @param[in] path Path to query.
87 * @exception rb_eArgError No such constant.
88 * @exception rb_eTypeError The path resolved to a non-module.
89 * @return Resolved class.
90 */
91 VALUE rb_path_to_class(VALUE path);
92
93 RBIMPL_ATTR_NONNULL(())
94 /**
95 * Resolves a `Q::W::E::R`-style path string to the actual class it points.
96 *
97 * @param[in] path Path to query.
98 * @exception rb_eArgError No such constant.
99 * @exception rb_eTypeError The path resolved to a non-module.
100 * @return Resolved class.
101 */
102 VALUE rb_path2class(const char *path);
103
104 /**
105 * Queries the name of the given object's class.
106 *
107 * @param[in] obj Arbitrary object.
108 * @return An instance of ::rb_cString representing `obj`'s class' path.
109 */
110 VALUE rb_class_name(VALUE obj);
111
112 /**
113 * Kicks the autoload procedure as if it was "touched".
114 *
115 * @param[out] space Namespace where autoload is defined.
116 * @param[in] name Name of the autoloaded constant.
117 * @retval RUBY_Qfalse No such autoload.
118 * @retval RUBY_Qtrue Autoload successfully initiated.
119 * @note As an autoloaded library is expected to define `space::name`,
120 * it is a nature of this function to have process-global side
121 * effects.
122 * @note Multiple threads can simultaneously call this API. It blocks
123 * then. That must not last indefinitely but can take longer than
124 * you expect.
125 *
126 * @internal
127 *
128 * @shyouhei has no idea why extension libraries should use this API.
129 */
130 VALUE rb_autoload_load(VALUE space, ID name);
131
132 /**
133 * Queries if an autoload is defined at a point.
134 *
135 * @param[in] space Namespace where autoload is defined.
136 * @param[in] name Name of the autoloaded constant.
137 * @retval RUBY_Qnil No such autoload.
138 * @retval otherwise The feature (path) registered at `space::name`.
139 */
140 VALUE rb_autoload_p(VALUE space, ID name);
141
142 /**
143 * Traces a global variable.
144 *
145 * @param[in] argc Either 1 or 2.
146 * @param[in] argv Variable name, optionally a Proc.
147 * @retval RUBY_Qnil No previous tracers.
148 * @retval otherwise Previous tracers.
149 *
150 * @internal
151 *
152 * @shyouhei has no idea why extension libraries should use this API.
153 */
154 VALUE rb_f_trace_var(int argc, const VALUE *argv);
155
156 /**
157 * Deletes the passed tracer from the passed global variable, or if omitted,
158 * deletes everything.
159 *
160 * @param[in] argc Either 1 or 2.
161 * @param[in] argv Variable name, optionally a Proc.
162 * @retval RUBY_Qnil No previous tracers.
163 * @retval otherwise Deleted tracers.
164 *
165 * @internal
166 *
167 * @shyouhei has no idea why extension libraries should use this API.
168 */
169 VALUE rb_f_untrace_var(int argc, const VALUE *argv);
170
171 /**
172 * Queries the list of global variables.
173 *
174 * @return The list of the name of the global variables.
175 *
176 * @internal
177 *
178 * Above description is in fact inaccurate. This API interfaces with Ractors.
179 */
180 VALUE rb_f_global_variables(void);
181
182 /**
183 * Aliases a global variable. Did you know that you can alias a global
184 * variable? It is like aliasing methods:
185 *
186 * ```ruby
187 * alias $dst $src
188 * ```
189 *
190 * This C function does the same thing.
191 *
192 * @param[in] dst Destination name.
193 * @param[in] src Source name.
194 * @post A global variable named `dst` is defined to be an alias of a
195 * global variable named `src`.
196 *
197 * @internal
198 *
199 * Above description is in fact inaccurate. This API interfaces with Ractors.
200 */
201 void rb_alias_variable(ID dst, ID src);
202
203 /**
204 * Frees the list of instance variables. 3rd parties need not know, but there
205 * are several ways to store an object's instance variables, depending on its
206 * internal structure. This function makes sense when the passed objects is
207 * using so-called "generic" backend storage. People need not be aware of this
208 * working behind-the-scenes.
209 *
210 * @param[out] obj The object in question.
211 *
212 * @internal
213 *
214 * This just destroys the given object. @shyouhei has no idea why extension
215 * libraries should use this API.
216 */
217 void rb_free_generic_ivar(VALUE obj);
218
219 /**
220 * Identical to rb_iv_get(), except it accepts the name as an ::ID instead of a
221 * C string.
222 *
223 * @param[in] obj Target object.
224 * @param[in] name Target instance variable to query.
225 * @retval RUBY_nil No such instance variable.
226 * @retval otherwise The value assigned to the instance variable.
227 */
228 VALUE rb_ivar_get(VALUE obj, ID name);
229
230 /**
231 * Identical to rb_iv_set(), except it accepts the name as an ::ID instead of a
232 * C string.
233 *
234 * @param[out] obj Target object.
235 * @param[in] name Target instance variable.
236 * @param[in] val Value to assign.
237 * @exception rb_eFrozenError Can't modify `obj`.
238 * @exception rb_eArgError `obj` has too many instance variables.
239 * @return Passed value.
240 * @post An instance variable named `name` is defined if absent on
241 * `obj`, whose value is set to `val`.
242 */
243 VALUE rb_ivar_set(VALUE obj, ID name, VALUE val);
244
245 /**
246 * Queries if the instance variable is defined at the object. This roughly
247 * resembles `defined?(@name)` in `obj`'s context.
248 *
249 * @param[in] obj Target object.
250 * @param[in] name Target instance variable to query.
251 * @retval RUBY_Qtrue There is an instance variable.
252 * @retval RUBY_Qfalse No such instance variable.
253 */
254 VALUE rb_ivar_defined(VALUE obj, ID name);
255
256 /**
257 * Iterates over an object's instance variables.
258 *
259 * @param[in] obj Target object.
260 * @param[in] func Callback function.
261 * @param[in] arg Passed as-is to the last argument of `func`.
262 */
263 void rb_ivar_foreach(VALUE obj, int (*func)(ID name, VALUE val, st_data_t arg), st_data_t arg);
264
265 /**
266 * Number of instance variables defined on an object.
267 *
268 * @param[in] obj Target object.
269 * @return Number of instance variables defined on `obj`.
270 */
271 st_index_t rb_ivar_count(VALUE obj);
272
273 /**
274 * Identical to rb_ivar_get()
275 *
276 * @param[in] obj Target object.
277 * @param[in] name Target instance variable to query.
278 * @retval RUBY_nil No such instance variable.
279 * @retval otherwise The value assigned to the instance variable.
280 *
281 * @internal
282 *
283 * Am I missing something? Why we have the same thing in different names?
284 */
285 VALUE rb_attr_get(VALUE obj, ID name);
286
287 /**
288 * Resembles `Object#instance_variables`.
289 *
290 * @param[in] obj Target object to query.
291 * @return An array of instance variable names for the receiver.
292 * @note Simply defining an accessor does not create the corresponding
293 * instance variable.
294 */
295 VALUE rb_obj_instance_variables(VALUE obj);
296
297 /**
298 * Resembles `Object#remove_instance_variable`.
299 *
300 * @param[out] obj Target object.
301 * @param[in] name Variable name to remove, either in Symbol or String.
302 * @return What was removed.
303 * @pre Instance variable named `name` is deleted from `obj`.
304 */
305 VALUE rb_obj_remove_instance_variable(VALUE obj, VALUE name);
306
307 /**
308 * This API is mysterious. It has been there since the initial revision. No
309 * single bits of documents has ever been written. The function name doesn't
310 * describe anything. What should be passed to the argument, or what should be
311 * the return value, are not obvious. Yet it has evolved over time. The
312 * source code is written in counter-intuitive way (as of 3.0).
313 *
314 * Simply put, don't try to understand this API.
315 */
316 void *rb_mod_const_at(VALUE, void*);
317
318 /**
319 * This is a variant of rb_mod_const_at(). As a result, it is also mysterious.
320 * It _seems_ it iterates over the ancestry tree of the module. But what that
321 * means is beyond a human brain.
322 */
323 void *rb_mod_const_of(VALUE, void*);
324
325 /**
326 * This is another mysterious API that comes with no documents at all. It
327 * seems it expects some specific data structure for the passed pointer. But
328 * the details has never been made explicit. It seems nobody should use this
329 * API.
330 */
331 VALUE rb_const_list(void*);
332
333 /**
334 * Resembles `Module#constants`. List up the constants defined at the
335 * receiver. This includes the names of constants in any included modules,
336 * unless `argv[0]` is ::RUBY_Qfalse.
337 *
338 * The implementation makes no guarantees about the order in which the
339 * constants are yielded.
340 *
341 * @param[in] argc Either 0 or 1.
342 * @param[in] argv Pointer to ::RUBY_Qfalse, if `argc == 1`.
343 * @param[in] recv Target namespace.
344 * @return An array of symbols, which are constant names under `recv`.
345 */
346 VALUE rb_mod_constants(int argc, const VALUE *argv, VALUE recv);
347
348 /**
349 * Resembles `Module#remove_const`.
350 *
351 * @param[out] space Target namespace.
352 * @param[in] name Variable name to remove, either in Symbol or String.
353 * @return What was removed.
354 * @pre Constant named `space::name` is deleted.
355 * @note In case what was removed was in fact a module or a class, this
356 * operation does not affect its name. Which means when people
357 * for instance look at it using `p` etc., it still introduces
358 * itself using the deleted name. Can confuse people.
359 */
360 VALUE rb_mod_remove_const(VALUE space, VALUE name);
361
362 /**
363 * Queries if the constant is defined at the namespace.
364 *
365 * @param[in] space Target namespace.
366 * @param[in] name Target name to query.
367 * @retval RUBY_Qtrue There is a constant.
368 * @retval RUBY_Qfalse No such constant.
369 *
370 * @internal
371 *
372 * The return values are not typo! This function returns ruby values casted to
373 * `int`. Completely brain-damaged design.
374 */
375 int rb_const_defined(VALUE space, ID name);
376
377 /**
378 * Identical to rb_const_defined(), except it doesn't look for parent classes.
379 * For instance `Array` is a toplevel constant, which is visible from
380 * everywhere. But this function does not take such things into account. It
381 * concerns only what is directly defined inside of the given namespace.
382 *
383 * @param[in] space Target namespace.
384 * @param[in] name Target name to query.
385 * @retval RUBY_Qtrue There is a constant.
386 * @retval RUBY_Qfalse No such constant.
387 *
388 * @internal
389 *
390 * The return values are not typo! This function returns ruby values casted to
391 * `int`. Completely brain-damaged design.
392 */
393 int rb_const_defined_at(VALUE space, ID name);
394
395 /**
396 * Identical to rb_const_defined(), except it returns false for private
397 * constants.
398 *
399 * @param[in] space Target namespace.
400 * @param[in] name Target name to query.
401 * @retval RUBY_Qtrue There is a constant.
402 * @retval RUBY_Qfalse No such constant.
403 *
404 * @internal
405 *
406 * What does "from" mean? The name sounds quite cryptic.
407 *
408 * The return values are not typo! This function returns ruby values casted to
409 * `int`. Completely brain-damaged design.
410 */
411 int rb_const_defined_from(VALUE space, ID name);
412
413 /**
414 * Identical to rb_const_defined(), except it returns the actual defined value.
415 *
416 * @param[in] space Target namespace.
417 * @param[in] name Target name to query.
418 * @exception rb_eNameError No such constant.
419 * @return The defined constant.
420 *
421 * @internal
422 *
423 * Above description is in fact inaccurate. This API interfaces with Ractors.
424 */
425 VALUE rb_const_get(VALUE space, ID name);
426
427 /**
428 * Identical to rb_const_defined_at(), except it returns the actual defined
429 * value. It can also be seen as a routine identical to rb_const_get(), except
430 * it doesn't look for parent classes.
431 *
432 * @param[in] space Target namespace.
433 * @param[in] name Target name to query.
434 * @exception rb_eNameError No such constant.
435 * @return The defined constant.
436 *
437 * @internal
438 *
439 * Above description is in fact inaccurate. This API interfaces with Ractors.
440 */
441 VALUE rb_const_get_at(VALUE space, ID name);
442
443 /**
444 * Identical to rb_const_defined_at(), except it returns the actual defined
445 * value. It can also be seen as a routine identical to rb_const_get(), except
446 * it doesn't return a private constant.
447 *
448 * @param[in] space Target namespace.
449 * @param[in] name Target name to query.
450 * @exception rb_eNameError No such constant.
451 * @return The defined constant.
452 *
453 * @internal
454 *
455 * Above description is in fact inaccurate. This API interfaces with Ractors.
456 */
457 VALUE rb_const_get_from(VALUE space, ID name);
458
459 /**
460 * Names a constant.
461 *
462 * @param[out] space Target namespace.
463 * @param[in] name Target name to query.
464 * @param[in] val Value to define.
465 * @exception rb_eTypeError `space` is not a module.
466 * @post `name` is a constant under `space`, whose value is `val`.
467 * @note You can reassign.
468 *
469 * @internal
470 *
471 * Above description is in fact inaccurate. This API interfaces with Ractors.
472 */
473 void rb_const_set(VALUE space, ID name, VALUE val);
474
475 /**
476 * Identical to rb_mod_remove_const(), except it takes the name as ::ID instead
477 * of ::VALUE.
478 *
479 * @param[out] space Target namespace.
480 * @param[in] name Variable name to remove, either in Symbol or String.
481 * @return What was removed.
482 * @pre Constant named `space::name` is deleted.
483 * @note In case what was removed was in fact a module or a class, this
484 * operation does not affect its name. Which means when people
485 * for instance look at it using `p` etc., it still introduces
486 * itself using the deleted name. Can confuse people.
487 */
488 VALUE rb_const_remove(VALUE space, ID name);
489
490 #if 0 /* EXPERIMENTAL: remove if no problem */
491 RBIMPL_ATTR_NORETURN()
492 /**
493 * This is the default implementation of `Module#const_missing`.
494 *
495 * @param[in] space Target namespace.
496 * @param[in] name Target name that is nonexistent.
497 * @exception rb_eNameError Always.
498 */
499 VALUE rb_mod_const_missing(VALUE space, VALUE name);
500 #endif
501
502 /**
503 * Queries if the given class has the given class variable.
504 *
505 * @param[in] klass Target class.
506 * @param[in] name Name to query.
507 * @return RUBY_Qtrue Yes there is.
508 * @return RUBY_Qfalse No there isn't.
509 * @pre `klass` must be an instance of rb_cModule.
510 *
511 * @internal
512 *
513 * Above description is in fact inaccurate. This API interfaces with Ractors.
514 */
515 VALUE rb_cvar_defined(VALUE klass, ID name);
516
517 /**
518 * Assigns a value to a class variable.
519 *
520 * @param[out] klass Target class.
521 * @param[in] name Variable name.
522 * @param[in] val Value to be assigned.
523 * @post `klass` has a class variable named `name` whose value is `val`.
524 *
525 * @internal
526 *
527 * Above description is in fact inaccurate. This API interfaces with Ractors.
528 */
529 void rb_cvar_set(VALUE klass, ID name, VALUE val);
530
531 /**
532 * Obtains a value from a class variable.
533 *
534 * @param[in] klass Target class.
535 * @param[in] name Variable name.
536 * @exception rb_eNameError Uninitialised class variable.
537 * @exception rb_eRuntimeError `[Bug#14541]` situation.
538 * @return Class variable named `name` under `klass`.
539 *
540 * @internal
541 *
542 * Above description is in fact inaccurate. This API interfaces with Ractors.
543 */
544 VALUE rb_cvar_get(VALUE klass, ID name);
545
546 RBIMPL_ATTR_NONNULL(())
547 /**
548 * Identical to rb_cvar_get(), except it takes additional "front" pointer.
549 * This extra parameter is a buffer, which will have the class where the
550 * queried class variable actually resides.
551 *
552 * @param[in] klass Target class.
553 * @param[in] name Variable name.
554 * @param[out] front Return buffer.
555 * @exception rb_eNameError Uninitialised class variable.
556 * @exception rb_eRuntimeError `[Bug#14541]` situation.
557 * @return Class variable named `name` under `klass`.
558 * @post `front` has the class object, which is an ancestor of `klass`,
559 * where the queried class variable actually resides.
560 *
561 * @internal
562 *
563 * Above description is in fact inaccurate. This API interfaces with Ractors.
564 */
565 VALUE rb_cvar_find(VALUE klass, ID name, VALUE *front);
566
567 RBIMPL_ATTR_NONNULL(())
568 /**
569 * Identical to rb_cvar_set(), except it accepts C's string instead of ::ID.
570 *
571 * @param[out] klass Target class.
572 * @param[in] name Variable name.
573 * @param[in] val Value to be assigned.
574 * @post `klass` has a class variable named `name` whose value is `val`.
575 */
576 void rb_cv_set(VALUE klass, const char *name, VALUE val);
577
578 RBIMPL_ATTR_NONNULL(())
579 /**
580 * Identical to rb_cvar_get(), except it accepts C's string instead of ::ID.
581 *
582 * @param[in] klass Target class.
583 * @param[in] name Variable name.
584 * @exception rb_eNameError Uninitialised class variable.
585 * @exception rb_eRuntimeError `[Bug#14541]` situation.
586 * @return Class variable named `name` under `klass`.
587 */
588 VALUE rb_cv_get(VALUE klass, const char *name);
589
590 RBIMPL_ATTR_NONNULL(())
591 /**
592 * @alias{rb_cv_set}
593 *
594 * @internal
595 *
596 * Am I missing something? Why we have the same thing in different names?
597 */
598 void rb_define_class_variable(VALUE, const char*, VALUE);
599
600 /**
601 * Resembles `Module#class_variables`. List up the variables defined at the
602 * receiver. This includes the names of constants in any included modules,
603 * unless `argv[0]` is ::RUBY_Qfalse.
604 *
605 * The implementation makes no guarantees about the order in which the
606 * constants are yielded.
607 *
608 * @param[in] argc Either 0 or 1.
609 * @param[in] argv Pointer to ::RUBY_Qfalse, if `argc == 1`.
610 * @param[in] recv Target class.
611 * @return An array of symbols, which are class variable names under
612 * `recv`.
613 */
614 VALUE rb_mod_class_variables(int argc, const VALUE *argv, VALUE recv);
615
616 /**
617 * Resembles `Module#remove_class_variable`.
618 *
619 * @param[out] mod Target class.
620 * @param[in] name Variable name to remove, either in Symbol or String.
621 * @return What was removed.
622 * @pre Instance variable named `name` is deleted from `obj`.
623 */
624 VALUE rb_mod_remove_cvar(VALUE mod, VALUE name);
625
626 RBIMPL_SYMBOL_EXPORT_END()
627
628 #endif /* RBIMPL_INTERN_VARIABLE_H */