Debian Codegraph @

include/ruby/internal/core/rbasic.h

raw
1 #ifndef RBIMPL_RBASIC_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_RBASIC_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 Defines struct ::RBasic.
22 */
23 #include "ruby/internal/attr/artificial.h"
24 #include "ruby/internal/attr/constexpr.h"
25 #include "ruby/internal/attr/forceinline.h"
26 #include "ruby/internal/attr/noalias.h"
27 #include "ruby/internal/attr/pure.h"
28 #include "ruby/internal/cast.h"
29 #include "ruby/internal/dllexport.h"
30 #include "ruby/internal/special_consts.h"
31 #include "ruby/internal/value.h"
32 #include "ruby/assert.h"
33
34 /**
35 * Convenient casting macro.
36 *
37 * @param obj Arbitrary Ruby object.
38 * @return The passed object casted to ::RBasic.
39 */
40 #define RBASIC(obj) RBIMPL_CAST((struct RBasic *)(obj))
41 /** @cond INTERNAL_MACRO */
42 #define RBASIC_CLASS RBASIC_CLASS
43 #define RBIMPL_RVALUE_EMBED_LEN_MAX 3
44 #define RVALUE_EMBED_LEN_MAX RVALUE_EMBED_LEN_MAX
45 #define RBIMPL_EMBED_LEN_MAX_OF(T) \
46 RBIMPL_CAST((int)(sizeof(VALUE[RBIMPL_RVALUE_EMBED_LEN_MAX]) / (sizeof(T))))
47 /** @endcond */
48
49 /**
50 * This is an enum because GDB wants it (rather than a macro). People need not
51 * bother.
52 */
53 enum ruby_rvalue_flags {
54 /** Max possible number of objects that can be embedded. */
55 RVALUE_EMBED_LEN_MAX = RBIMPL_RVALUE_EMBED_LEN_MAX
56 };
57
58 /**
59 * Ruby's object's, base components. Every single ruby objects have them in
60 * common.
61 */
62 struct
63 RUBY_ALIGNAS(SIZEOF_VALUE)
64 RBasic {
65
66 /**
67 * Per-object flags. Each ruby objects have their own characteristics
68 * apart from their classes. For instance whether an object is frozen or
69 * not is not controlled by its class. This is where such properties are
70 * stored.
71 *
72 * @see enum ::ruby_fl_type
73 *
74 * @note This is ::VALUE rather than an enum for alignment purpose. Back
75 * in the 1990s there were no such thing like `_Alignas` in C.
76 */
77 VALUE flags;
78
79 /**
80 * Class of an object. Every object has its class. Also, everything is an
81 * object in Ruby. This means classes are also objects. Classes have
82 * their own classes, classes of classes have their classes, too ... and
83 * it recursively continues forever.
84 *
85 * Also note the `const` qualifier. In ruby an object cannot "change" its
86 * class.
87 */
88 const VALUE klass;
89
90 #ifdef __cplusplus
91 public:
92 RBIMPL_ATTR_CONSTEXPR(CXX11)
93 RBIMPL_ATTR_ARTIFICIAL()
94 RBIMPL_ATTR_FORCEINLINE()
95 RBIMPL_ATTR_NOALIAS()
96 /**
97 * We need to define this explicit constructor because the field `klass` is
98 * const-qualified above, which effectively defines the implicit default
99 * constructor as "deleted" (as of C++11) -- No way but to define one by
100 * ourselves.
101 */
102 RBasic() :
103 flags(RBIMPL_VALUE_NULL),
104 klass(RBIMPL_VALUE_NULL)
105 {
106 }
107 #endif
108 };
109
110 RBIMPL_SYMBOL_EXPORT_BEGIN()
111 /**
112 * Make the object invisible from Ruby code.
113 *
114 * It is useful to let Ruby's GC manage your internal data structure -- The
115 * object keeps being managed by GC, but `ObjectSpace.each_object` never yields
116 * the object.
117 *
118 * Note that the object also lose a way to call a method on it.
119 *
120 * @param[out] obj A Ruby object.
121 * @return The passed object.
122 * @post The object is destructively modified to be invisible.
123 * @see rb_obj_reveal
124 */
125 VALUE rb_obj_hide(VALUE obj);
126
127 /**
128 * Make a hidden object visible again.
129 *
130 * It is the caller's responsibility to pass the right `klass` which `obj`
131 * originally used to belong to.
132 *
133 * @param[out] obj A Ruby object.
134 * @param[in] klass Class of `obj`.
135 * @return Passed `obj`.
136 * @pre `obj` was previously hidden.
137 * @post `obj`'s class is `klass`.
138 * @see rb_obj_hide
139 */
140 VALUE rb_obj_reveal(VALUE obj, VALUE klass); /* do not use this API to change klass information */
141 RBIMPL_SYMBOL_EXPORT_END()
142
143 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
144 RBIMPL_ATTR_ARTIFICIAL()
145 /**
146 * Queries the class of an object.
147 *
148 * @param[in] obj An object.
149 * @return Its class.
150 */
151 static inline VALUE
152 RBASIC_CLASS(VALUE obj)
153 {
154 RBIMPL_ASSERT_OR_ASSUME(! RB_SPECIAL_CONST_P(obj));
155 return RBASIC(obj)->klass;
156 }
157
158 #endif /* RBIMPL_RBASIC_H */