Ruby: include/ruby/internal/special_consts.h Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

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
27#include "ruby/internal/attr/artificial.h"
28#include "ruby/internal/attr/const.h"
29#include "ruby/internal/attr/constexpr.h"
30#include "ruby/internal/attr/enum_extensibility.h"
31#include "ruby/internal/stdbool.h"
32#include "ruby/internal/value.h"
33
34#if defined(USE_FLONUM)
35# /* Take that. */
36#elif SIZEOF_VALUE >= SIZEOF_DOUBLE
37# define USE_FLONUM 1
38#else
39# define USE_FLONUM 0
40#endif
41
42#define RTEST RB_TEST
43
44#define FIXNUM_P RB_FIXNUM_P
45#define IMMEDIATE_P RB_IMMEDIATE_P
46#define NIL_P RB_NIL_P
47#define SPECIAL_CONST_P RB_SPECIAL_CONST_P
48#define STATIC_SYM_P RB_STATIC_SYM_P
49
50#define Qfalse RUBY_Qfalse
51#define Qnil RUBY_Qnil
52#define Qtrue RUBY_Qtrue
53#define Qundef RUBY_Qundef
54
56#define FIXNUM_FLAG RUBY_FIXNUM_FLAG
57#define FLONUM_FLAG RUBY_FLONUM_FLAG
58#define FLONUM_MASK RUBY_FLONUM_MASK
59#define FLONUM_P RB_FLONUM_P
60#define IMMEDIATE_MASK RUBY_IMMEDIATE_MASK
61#define SYMBOL_FLAG RUBY_SYMBOL_FLAG
62
63#define RB_FIXNUM_P RB_FIXNUM_P
64#define RB_FLONUM_P RB_FLONUM_P
65#define RB_IMMEDIATE_P RB_IMMEDIATE_P
66#define RB_NIL_P RB_NIL_P
67#define RB_SPECIAL_CONST_P RB_SPECIAL_CONST_P
68#define RB_STATIC_SYM_P RB_STATIC_SYM_P
69#define RB_TEST RB_TEST
73 enum
74 RBIMPL_ATTR_ENUM_EXTENSIBILITY(closed)
75 ruby_special_consts {
76#if USE_FLONUM
77 RUBY_Qfalse = 0x00, /* ...0000 0000 */
78 RUBY_Qtrue = 0x14, /* ...0001 0100 */
79 RUBY_Qnil = 0x08, /* ...0000 1000 */
80 RUBY_Qundef = 0x34, /* ...0011 0100 */
81 RUBY_IMMEDIATE_MASK = 0x07, /* ...0000 0111 */
82 RUBY_FIXNUM_FLAG = 0x01, /* ...xxxx xxx1 */
83 RUBY_FLONUM_MASK = 0x03, /* ...0000 0011 */
84 RUBY_FLONUM_FLAG = 0x02, /* ...xxxx xx10 */
85 RUBY_SYMBOL_FLAG = 0x0c, /* ...xxxx 1100 */
86#else
87 RUBY_Qfalse = 0x00, /* ...0000 0000 */
88 RUBY_Qtrue = 0x02, /* ...0000 0010 */
89 RUBY_Qnil = 0x04, /* ...0000 0100 */
90 RUBY_Qundef = 0x06, /* ...0000 0110 */
91 RUBY_IMMEDIATE_MASK = 0x03, /* ...0000 0011 */
92 RUBY_FIXNUM_FLAG = 0x01, /* ...xxxx xxx1 */
93 RUBY_FLONUM_MASK = 0x00, /* any values ANDed with FLONUM_MASK cannot be FLONUM_FLAG */
94 RUBY_FLONUM_FLAG = 0x02, /* ...0000 0010 */
95 RUBY_SYMBOL_FLAG = 0x0e, /* ...0000 1110 */
96#endif
97
98 RUBY_SPECIAL_SHIFT = 8
99};
100
101 RBIMPL_ATTR_CONST()
102 RBIMPL_ATTR_CONSTEXPR(CXX11)
103 RBIMPL_ATTR_ARTIFICIAL()
104/*
105 * :NOTE: rbimpl_test HAS to be `__attribute__((const))` in order for clang to
106 * properly deduce `__builtin_assume()`.
107 */
108static inline bool
109RB_TEST(VALUE obj)
110{
111 /*
112 * Qfalse: ....0000 0000
113 * Qnil: ....0000 1000
114 * ~Qnil: ....1111 0111
115 * v ....xxxx xxxx
116 * ----------------------------
117 * RTEST(v) ....xxxx 0xxx
118 *
119 * RTEST(v) can be 0 if and only if (v == Qfalse || v == Qnil).
120 */
121 return obj & ~RUBY_Qnil;
122}
123
124 RBIMPL_ATTR_CONST()
125 RBIMPL_ATTR_CONSTEXPR(CXX11)
126 RBIMPL_ATTR_ARTIFICIAL()
127static inline bool
128RB_NIL_P(VALUE obj)
129{
130 return obj == RUBY_Qnil;
131}
132
133 RBIMPL_ATTR_CONST()
134 RBIMPL_ATTR_CONSTEXPR(CXX11)
135 RBIMPL_ATTR_ARTIFICIAL()
136static inline bool
137RB_FIXNUM_P(VALUE obj)
138{
139 return obj & RUBY_FIXNUM_FLAG;
140}
141
142 RBIMPL_ATTR_CONST()
143 RBIMPL_ATTR_CONSTEXPR(CXX14)
144 RBIMPL_ATTR_ARTIFICIAL()
145static inline bool
146RB_STATIC_SYM_P(VALUE obj)
147{
148 RBIMPL_ATTR_CONSTEXPR(CXX14)
149 const VALUE mask = ~(RBIMPL_VALUE_FULL << RUBY_SPECIAL_SHIFT);
150 return (obj & mask) == RUBY_SYMBOL_FLAG;
151}
152
153 RBIMPL_ATTR_CONST()
154 RBIMPL_ATTR_CONSTEXPR(CXX11)
155 RBIMPL_ATTR_ARTIFICIAL()
156static inline bool
157RB_FLONUM_P(VALUE obj)
158{
159#if USE_FLONUM
160 return (obj & RUBY_FLONUM_MASK) == RUBY_FLONUM_FLAG;
161#else
162 return false;
163#endif
164}
165
166 RBIMPL_ATTR_CONST()
167 RBIMPL_ATTR_CONSTEXPR(CXX11)
168 RBIMPL_ATTR_ARTIFICIAL()
169static inline bool
170RB_IMMEDIATE_P(VALUE obj)
171{
172 return obj & RUBY_IMMEDIATE_MASK;
173}
174
175 RBIMPL_ATTR_CONST()
176 RBIMPL_ATTR_CONSTEXPR(CXX11)
177 RBIMPL_ATTR_ARTIFICIAL()
178static inline bool
179RB_SPECIAL_CONST_P(VALUE obj)
180{
181 return RB_IMMEDIATE_P(obj) || ! RB_TEST(obj);
182}
183
184 RBIMPL_ATTR_CONST()
185 RBIMPL_ATTR_CONSTEXPR(CXX11)
186/* This function is to mimic old rb_special_const_p macro but have anyone
187 * actually used its return value? Wasn't it just something no one needed? */
188static inline VALUE
189rb_special_const_p(VALUE obj)
190{
191 return RB_SPECIAL_CONST_P(obj) * RUBY_Qtrue;
192}
193
198#define RUBY_Qfalse RBIMPL_CAST((VALUE)RUBY_Qfalse)
199#define RUBY_Qtrue RBIMPL_CAST((VALUE)RUBY_Qtrue)
200#define RUBY_Qnil RBIMPL_CAST((VALUE)RUBY_Qnil)
201#define RUBY_Qundef RBIMPL_CAST((VALUE)RUBY_Qundef)
204#endif /* RBIMPL_SPECIAL_CONSTS_H */
artificial.h
Defines RBIMPL_ATTR_ARTIFICIAL.
RBIMPL_ATTR_ARTIFICIAL
#define RBIMPL_ATTR_ARTIFICIAL()
Wraps (or simulates) __attribute__((artificial))
Definition: artificial.h:43
const.h
Defines RBIMPL_ATTR_CONST.
RBIMPL_ATTR_CONST
#define RBIMPL_ATTR_CONST()
Wraps (or simulates) __attribute__((const))
Definition: const.h:36
constexpr.h
RBIMPL_ATTR_CONSTEXPR.
RBIMPL_ATTR_CONSTEXPR
#define RBIMPL_ATTR_CONSTEXPR(_)
Wraps (or simulates) C++11 constexpr.
Definition: constexpr.h:75
mask
enum @11::@13::@14 mask
enum_extensibility.h
RBIMPL_ATTR_ENUM_EXTENSIBILITY.
RBIMPL_ATTR_ENUM_EXTENSIBILITY
#define RBIMPL_ATTR_ENUM_EXTENSIBILITY(_)
Wraps (or simulates) __attribute__((enum_extensibility))
Definition: enum_extensibility.h:29
ruby_special_consts
ruby_special_consts
special constants - i.e.
Definition: special_consts.h:75
RUBY_Qtrue
@ RUBY_Qtrue
Definition: special_consts.h:88
RUBY_SPECIAL_SHIFT
@ RUBY_SPECIAL_SHIFT
Definition: special_consts.h:98
RUBY_FIXNUM_FLAG
@ RUBY_FIXNUM_FLAG
Definition: special_consts.h:92
RUBY_Qnil
@ RUBY_Qnil
Definition: special_consts.h:89
RUBY_FLONUM_MASK
@ RUBY_FLONUM_MASK
Definition: special_consts.h:93
RUBY_FLONUM_FLAG
@ RUBY_FLONUM_FLAG
Definition: special_consts.h:94
RUBY_Qundef
@ RUBY_Qundef
Definition: special_consts.h:90
RUBY_SYMBOL_FLAG
@ RUBY_SYMBOL_FLAG
Definition: special_consts.h:95
RUBY_IMMEDIATE_MASK
@ RUBY_IMMEDIATE_MASK
Definition: special_consts.h:91
RUBY_Qfalse
@ RUBY_Qfalse
Definition: special_consts.h:87
stdbool.h
C99 shim for <stdbool.h>
value.h
Defines VALUE and ID.
VALUE
unsigned long VALUE
Definition: value.h:38
RBIMPL_VALUE_FULL
#define RBIMPL_VALUE_FULL
Definition: value.h:45