Ruby  3.4.0dev (2024-12-06 revision 892c46283a5ea4179500d951c9d4866c0051f27b)
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 
36 #include "ruby/internal/cast.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
50 #define rb_intern_const rb_intern_const
54 
55 
62 ID rb_sym2id(VALUE obj);
63 
71 VALUE rb_id2sym(ID id);
72 
85 ID rb_intern(const char *name);
86 
101 ID rb_intern2(const char *name, long len);
102 
118 ID rb_intern_str(VALUE str);
119 
129 const char *rb_id2name(ID id);
130 
174 ID rb_check_id(volatile VALUE *namep);
175 
186 ID rb_to_id(VALUE str);
187 
202 VALUE rb_id2str(ID id);
203 
212 VALUE rb_sym2str(VALUE symbol);
213 
229 VALUE rb_to_symbol(VALUE name);
230 
253 VALUE rb_check_symbol(volatile VALUE *namep);
255 
276 static inline ID
277 rb_intern_const(const char *str)
278 {
279  size_t len = strlen(str);
280  return rb_intern2(str, RBIMPL_CAST((long)len));
281 }
282 
290 static inline ID
291 rbimpl_intern_const(ID *ptr, const char *str)
292 {
293  while (! *ptr) {
294  *ptr = rb_intern_const(str);
295  }
296 
297  return *ptr;
298 }
299 
304 #define RUBY_CONST_ID_CACHE(result, str) \
305  { \
306  static ID rb_intern_id_cache; \
307  rbimpl_intern_const(&rb_intern_id_cache, (str)); \
308  result rb_intern_id_cache; \
309  }
310 
315 #define RUBY_CONST_ID(var, str) \
316  do { \
317  static ID rbimpl_id; \
318  (var) = rbimpl_intern_const(&rbimpl_id, (str)); \
319  } while (0)
320 
321 #if defined(HAVE_STMT_AND_DECL_IN_EXPR)
322 /* __builtin_constant_p and statement expression is available
323  * since gcc-2.7.2.3 at least. */
324 #define rb_intern(str) \
325  (RBIMPL_CONSTANT_P(str) ? \
326  __extension__ ({ \
327  static ID rbimpl_id; \
328  rbimpl_intern_const(&rbimpl_id, (str)); \
329  }) : \
330  (rb_intern)(str))
331 #endif
332 
333 #endif /* RBIMPL_SYMBOL_H */
Defines RBIMPL_CONSTANT_P.
Tweaking visibility of C variables/functions.
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition: dllexport.h:74
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition: dllexport.h:65
Defines RBIMPL_HAS_BUILTIN.
ID rb_intern2(const char *name, long len)
Identical to rb_intern(), except it additionally takes the length of the string.
Definition: symbol.c:816
VALUE rb_check_symbol(volatile VALUE *namep)
Identical to rb_check_id(), except it returns an instance of rb_cSymbol instead.
Definition: symbol.c:1174
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition: symbol.h:277
VALUE rb_id2sym(ID id)
Allocates an instance of rb_cSymbol that has the given id.
Definition: symbol.c:951
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition: symbol.c:1117
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
Definition: symbol.c:992
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
Definition: symbol.c:823
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition: symbol.c:970
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
Definition: string.c:12473
ID rb_sym2id(VALUE obj)
Converts an instance of rb_cSymbol into an ID.
Definition: symbol.c:917
ID rb_to_id(VALUE str)
Identical to rb_intern(), except it takes an instance of rb_cString.
Definition: string.c:12463
ID rb_intern_str(VALUE str)
Identical to rb_intern(), except it takes an instance of rb_cString.
Definition: symbol.c:829
VALUE rb_id2str(ID id)
Identical to rb_id2name(), except it returns a frozen Ruby String instead of a C String.
Definition: symbol.c:986
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
Definition: io.h:2
RBIMPL_ATTR_PURE() int rb_io_read_pending(rb_io_t *fptr)
Queries if the passed IO has any pending reads.
int len
Length of the buffer.
Definition: io.h:8
Defines RBIMPL_ATTR_NOALIAS.
#define RBIMPL_ATTR_NOALIAS()
Wraps (or simulates) __declspec((noalias))
Definition: noalias.h:62
Defines RBIMPL_ATTR_NONNULL.
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition: nonnull.h:27
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition: defines.h:88
Defines RBIMPL_ATTR_PURE.
Defines VALUE and ID.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition: value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40