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

Ruby 4.0.6p0 (2026-07-14 revision 03b6d3f8898a28604fe6cb00eae3226b821168f4)

symbol.h
Go to the documentation of this file.
1#ifndef RBIMPL_SYMBOL_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RBIMPL_SYMBOL_H
23#include "ruby/internal/config.h"
24
25#ifdef STDC_HEADERS
26# include <stddef.h>
27#endif
28
29#ifdef HAVE_STRING_H
30# include <string.h>
31#endif
32
33#include "ruby/internal/attr/noalias.h"
34#include "ruby/internal/attr/nonnull.h"
35#include "ruby/internal/attr/pure.h"
36#include "ruby/internal/cast.h"
37#include "ruby/internal/constant_p.h"
38#include "ruby/internal/dllexport.h"
39#include "ruby/internal/has/builtin.h"
40#include "ruby/internal/value.h"
41
42#define RB_ID2SYM rb_id2sym
43#define RB_SYM2ID rb_sym2id
44#define ID2SYM RB_ID2SYM
45#define SYM2ID RB_SYM2ID
46#define CONST_ID_CACHE RUBY_CONST_ID_CACHE
47#define CONST_ID RUBY_CONST_ID
48
50#define rb_intern_const rb_intern_const
52
53 RBIMPL_SYMBOL_EXPORT_BEGIN()
54
55
62 ID rb_sym2id(VALUE obj);
63
71 VALUE rb_id2sym(ID id);
72
73 RBIMPL_ATTR_NONNULL(())
85 ID rb_intern(const char *name);
86
101 ID rb_intern2(const char *name, long len);
102
117 ID rb_intern_str(VALUE str);
118
135const char *rb_id2name(ID id);
136
137 RBIMPL_ATTR_NONNULL(())
177 ID rb_check_id(volatile VALUE *namep);
178
197 ID rb_to_id(VALUE str);
198
213 VALUE rb_id2str(ID id);
214
223 VALUE rb_sym2str(VALUE symbol);
224
240 VALUE rb_to_symbol(VALUE name);
241
242 RBIMPL_ATTR_NONNULL(())
261 VALUE rb_check_symbol(volatile VALUE *namep);
262 RBIMPL_SYMBOL_EXPORT_END()
263
264 RBIMPL_ATTR_PURE()
265 RBIMPL_ATTR_NONNULL(())
284static inline ID
285rb_intern_const(const char *str)
286{
287 size_t len = strlen(str);
288 return rb_intern2(str, RBIMPL_CAST((long)len));
289}
290
291 RBIMPL_ATTR_NOALIAS()
292 RBIMPL_ATTR_NONNULL(())
298static inline ID
299rbimpl_intern_const(ID *ptr, const char *str)
300{
301 while (! *ptr) {
302 *ptr = rb_intern_const(str);
303 }
304
305 return *ptr;
306}
307
312#define RUBY_CONST_ID_CACHE(result, str) \
313 { \
314 static ID rb_intern_id_cache; \
315 rbimpl_intern_const(&rb_intern_id_cache, (str)); \
316 result rb_intern_id_cache; \
317 }
318
324#define RUBY_CONST_ID(var, str) \
325 do { \
326 static ID rbimpl_id; \
327 (var) = rbimpl_intern_const(&rbimpl_id, (str)); \
328 } while (0)
329
330#if defined(HAVE_STMT_AND_DECL_IN_EXPR)
331/* GCC specific shorthand for RUBY_CONST_ID().
332 * __builtin_constant_p and statement expression is available
333 * since gcc-2.7.2.3 at least. */
334#define rb_intern(str) \
335 (RBIMPL_CONSTANT_P(str) ? \
336 __extension__ ({ \
337 static ID rbimpl_id; \
338 rbimpl_intern_const(&rbimpl_id, (str)); \
339 }) : \
340 (rb_intern)(str))
341#endif
342
343#endif /* RBIMPL_SYMBOL_H */
constant_p.h
Defines RBIMPL_CONSTANT_P.
dllexport.h
Tweaking visibility of C variables/functions.
RBIMPL_SYMBOL_EXPORT_END
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition dllexport.h:74
RBIMPL_SYMBOL_EXPORT_BEGIN
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition dllexport.h:65
builtin.h
Defines RBIMPL_HAS_BUILTIN.
rb_intern2
ID rb_intern2(const char *name, long len)
Identical to rb_intern(), except it additionally takes the length of the string.
Definition symbol.c:857
rb_check_symbol
VALUE rb_check_symbol(volatile VALUE *namep)
Identical to rb_check_id(), except it returns an instance of rb_cSymbol instead.
Definition symbol.c:1190
rb_intern_const
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:285
rb_id2sym
VALUE rb_id2sym(ID id)
Allocates an instance of rb_cSymbol that has the given id.
Definition symbol.c:974
rb_check_id
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition symbol.c:1133
rb_sym2str
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:993
rb_to_symbol
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
Definition string.c:12674
rb_sym2id
ID rb_sym2id(VALUE obj)
Converts an instance of rb_cSymbol into an ID.
Definition symbol.c:943
rb_to_id
ID rb_to_id(VALUE str)
Identical to rb_intern_str(), except it tries to convert the parameter object to an instance of rb_cS...
Definition string.c:12664
len
int len
Length of the buffer.
Definition io.h:8
noalias.h
Defines RBIMPL_ATTR_NOALIAS.
RBIMPL_ATTR_NOALIAS
#define RBIMPL_ATTR_NOALIAS()
Wraps (or simulates) __declspec((noalias)).
Definition noalias.h:66
nonnull.h
Defines RBIMPL_ATTR_NONNULL.
RBIMPL_ATTR_NONNULL
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull)).
Definition nonnull.h:30
inline
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition defines.h:91
pure.h
Defines RBIMPL_ATTR_PURE.
RBIMPL_ATTR_PURE
#define RBIMPL_ATTR_PURE()
Wraps (or simulates) __attribute__((pure)).
Definition pure.h:33
value.h
Defines VALUE and ID.
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40