Ruby  3.1.4p223 (2023-03-30 revision HEAD)
special_consts.h
Go to the documentation of this file.
1 #ifndef RBIMPL_SPECIAL_CONSTS_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_SPECIAL_CONSTS_H
31 #include "ruby/internal/stdbool.h"
32 #include "ruby/internal/value.h"
33 
42 #if defined(USE_FLONUM)
43 # /* Take that. */
44 #elif SIZEOF_VALUE >= SIZEOF_DOUBLE
45 # define USE_FLONUM 1
46 #else
47 # define USE_FLONUM 0
48 #endif
49 
51 #define RTEST RB_TEST
52 
53 #define FIXNUM_P RB_FIXNUM_P
54 #define IMMEDIATE_P RB_IMMEDIATE_P
55 #define NIL_P RB_NIL_P
56 #define SPECIAL_CONST_P RB_SPECIAL_CONST_P
57 #define STATIC_SYM_P RB_STATIC_SYM_P
59 #define Qfalse RUBY_Qfalse
60 #define Qnil RUBY_Qnil
61 #define Qtrue RUBY_Qtrue
62 #define Qundef RUBY_Qundef
64 #define FIXNUM_FLAG RUBY_FIXNUM_FLAG
65 #define FLONUM_FLAG RUBY_FLONUM_FLAG
66 #define FLONUM_MASK RUBY_FLONUM_MASK
67 #define FLONUM_P RB_FLONUM_P
68 #define IMMEDIATE_MASK RUBY_IMMEDIATE_MASK
69 #define SYMBOL_FLAG RUBY_SYMBOL_FLAG
72 #define RB_FIXNUM_P RB_FIXNUM_P
73 #define RB_FLONUM_P RB_FLONUM_P
74 #define RB_IMMEDIATE_P RB_IMMEDIATE_P
75 #define RB_NIL_P RB_NIL_P
76 #define RB_SPECIAL_CONST_P RB_SPECIAL_CONST_P
77 #define RB_STATIC_SYM_P RB_STATIC_SYM_P
78 #define RB_TEST RB_TEST
82 enum
85 #if defined(__DOXYGEN__)
95 #elif USE_FLONUM
96  RUBY_Qfalse = 0x00, /* ...0000 0000 */
97  RUBY_Qtrue = 0x14, /* ...0001 0100 */
98  RUBY_Qnil = 0x08, /* ...0000 1000 */
99  RUBY_Qundef = 0x34, /* ...0011 0100 */
100  RUBY_IMMEDIATE_MASK = 0x07, /* ...0000 0111 */
101  RUBY_FIXNUM_FLAG = 0x01, /* ...xxxx xxx1 */
102  RUBY_FLONUM_MASK = 0x03, /* ...0000 0011 */
103  RUBY_FLONUM_FLAG = 0x02, /* ...xxxx xx10 */
104  RUBY_SYMBOL_FLAG = 0x0c, /* ...xxxx 1100 */
105 #else
106  RUBY_Qfalse = 0x00, /* ...0000 0000 */
107  RUBY_Qtrue = 0x02, /* ...0000 0010 */
108  RUBY_Qnil = 0x04, /* ...0000 0100 */
109  RUBY_Qundef = 0x06, /* ...0000 0110 */
110  RUBY_IMMEDIATE_MASK = 0x03, /* ...0000 0011 */
111  RUBY_FIXNUM_FLAG = 0x01, /* ...xxxx xxx1 */
112  RUBY_FLONUM_MASK = 0x00, /* any values ANDed with FLONUM_MASK cannot be FLONUM_FLAG */
113  RUBY_FLONUM_FLAG = 0x02, /* ...0000 0010 */
114  RUBY_SYMBOL_FLAG = 0x0e, /* ...0000 1110 */
115 #endif
116 
117  RUBY_SPECIAL_SHIFT = 8
118 };
119 
135 static inline bool
137 {
138  /*
139  * Qfalse: ....0000 0000
140  * Qnil: ....0000 1000
141  * ~Qnil: ....1111 0111
142  * v ....xxxx xxxx
143  * ----------------------------
144  * RTEST(v) ....xxxx 0xxx
145  *
146  * RTEST(v) can be 0 if and only if (v == Qfalse || v == Qnil).
147  */
148  return obj & ~RUBY_Qnil;
149 }
150 
161 static inline bool
163 {
164  return obj == RUBY_Qnil;
165 }
166 
179 static inline bool
181 {
182  return obj & RUBY_FIXNUM_FLAG;
183 }
184 
199 static inline bool
201 {
202  RBIMPL_ATTR_CONSTEXPR(CXX14)
203  const VALUE mask = ~(RBIMPL_VALUE_FULL << RUBY_SPECIAL_SHIFT);
204  return (obj & mask) == RUBY_SYMBOL_FLAG;
205 }
206 
220 static inline bool
222 {
223 #if USE_FLONUM
224  return (obj & RUBY_FLONUM_MASK) == RUBY_FLONUM_FLAG;
225 #else
226  return false;
227 #endif
228 }
229 
243 static inline bool
245 {
246  return obj & RUBY_IMMEDIATE_MASK;
247 }
248 
259 static inline bool
261 {
262  return RB_IMMEDIATE_P(obj) || ! RB_TEST(obj);
263 }
264 
279 static inline VALUE
281 {
282  return RB_SPECIAL_CONST_P(obj) * RUBY_Qtrue;
283 }
284 
289 #define RUBY_Qfalse RBIMPL_CAST((VALUE)RUBY_Qfalse)
290 #define RUBY_Qtrue RBIMPL_CAST((VALUE)RUBY_Qtrue)
291 #define RUBY_Qnil RBIMPL_CAST((VALUE)RUBY_Qnil)
292 #define RUBY_Qundef RBIMPL_CAST((VALUE)RUBY_Qundef)
295 #endif /* RBIMPL_SPECIAL_CONSTS_H */
Defines RBIMPL_ATTR_ARTIFICIAL.
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition: artificial.h:41
Defines RBIMPL_ATTR_CONST.
RBIMPL_ATTR_CONSTEXPR.
#define RBIMPL_ATTR_CONSTEXPR(_)
Wraps (or simulates) C++11 constexpr.
Definition: constexpr.h:74
RBIMPL_ATTR_ENUM_EXTENSIBILITY.
#define RBIMPL_ATTR_ENUM_EXTENSIBILITY(_)
Wraps (or simulates) __attribute__((enum_extensibility))
RBIMPL_ATTR_CONST() int rb_io_oflags_fmode(int oflags)
Converts an oflags (that rb_io_modestr_oflags() returns) to a fmode (that rb_io_mode_flags() returns)...
static bool RB_STATIC_SYM_P(VALUE obj)
Checks if the given object is a static symbol.
static bool RB_TEST(VALUE obj)
Emulates Ruby's "if" statement.
static bool RB_FIXNUM_P(VALUE obj)
Checks if the given object is a so-called Fixnum.
static bool RB_IMMEDIATE_P(VALUE obj)
Checks if the given object is an immediate i.e.
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
static VALUE rb_special_const_p(VALUE obj)
Identical to RB_SPECIAL_CONST_P, except it returns a VALUE.
ruby_special_consts
special constants - i.e.
@ RUBY_Qtrue
@ RUBY_SPECIAL_SHIFT
Least significant 8 bits are reserved.
@ RUBY_FIXNUM_FLAG
Flag to denote a fixnum.
@ RUBY_Qnil
@ RUBY_FLONUM_MASK
Bit mask detecting a flonum.
@ RUBY_FLONUM_FLAG
Flag to denote a flonum.
@ RUBY_Qundef
Represents so-called undef.
@ RUBY_SYMBOL_FLAG
Flag to denote a static symbol.
@ RUBY_IMMEDIATE_MASK
Bit mask detecting special consts.
@ RUBY_Qfalse
static bool RB_NIL_P(VALUE obj)
Checks if the given object is nil.
static bool RB_FLONUM_P(VALUE obj)
Checks if the given object is a so-called Flonum.
C99 shim for <stdbool.h>
Defines VALUE and ID.
#define RBIMPL_VALUE_FULL
Maximum possible value that a VALUE can take.
Definition: value.h:90
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40