Ruby 3.5.0dev (2025-08-07 revision fccd96cc6c3cc9f500dc87ae9be65aaa212b02fa)
symbol.h (fccd96cc6c3cc9f500dc87ae9be65aaa212b02fa)
1#ifndef RUBY_SYMBOL_H
2#define RUBY_SYMBOL_H 1
3/**********************************************************************
4
5 symbol.h -
6
7 $Author$
8 created at: Tue Jul 8 15:49:54 JST 2014
9
10 Copyright (C) 2014 Yukihiro Matsumoto
11
12**********************************************************************/
13
14#include "id.h"
15#include "ruby/encoding.h"
16
17#define DYNAMIC_ID_P(id) (!(id&ID_STATIC_SYM)&&id>tLAST_OP_ID)
18#define STATIC_ID2SYM(id) (((VALUE)(id)<<RUBY_SPECIAL_SHIFT)|SYMBOL_FLAG)
19
20#ifdef HAVE_BUILTIN___BUILTIN_CONSTANT_P
21#define rb_id2sym(id) \
22 RB_GNUC_EXTENSION_BLOCK(__builtin_constant_p(id) && !DYNAMIC_ID_P(id) ? \
23 STATIC_ID2SYM(id) : rb_id2sym(id))
24#endif
25
26struct RSymbol {
27 struct RBasic basic;
28 st_index_t hashval;
29 VALUE fstr;
30 ID id;
31};
32
33#define RSYMBOL(obj) ((struct RSymbol *)(obj))
34
35#define is_notop_id(id) ((id)>tLAST_OP_ID)
36#define is_local_id(id) (id_type(id)==ID_LOCAL)
37#define is_global_id(id) (id_type(id)==ID_GLOBAL)
38#define is_instance_id(id) (id_type(id)==ID_INSTANCE)
39#define is_attrset_id(id) ((id)==idASET||id_type(id)==ID_ATTRSET)
40#define is_const_id(id) (id_type(id)==ID_CONST)
41#define is_class_id(id) (id_type(id)==ID_CLASS)
42#define is_internal_id(id) (id_type(id)==ID_INTERNAL)
43
44static inline int
45id_type(ID id)
46{
47 if (is_notop_id(id)) {
48 return (int)(id&ID_SCOPE_MASK);
49 }
50 else {
51 return -1;
52 }
53}
54
55typedef uint32_t rb_id_serial_t;
56static const uint32_t RB_ID_SERIAL_MAX = /* 256M on LP32 */
57 UINT32_MAX >>
58 ((sizeof(ID)-sizeof(rb_id_serial_t))*CHAR_BIT < RUBY_ID_SCOPE_SHIFT ?
59 RUBY_ID_SCOPE_SHIFT : 0);
60
61static inline rb_id_serial_t
62rb_id_to_serial(ID id)
63{
64 if (is_notop_id(id)) {
65 return (rb_id_serial_t)(id >> ID_SCOPE_SHIFT);
66 }
67 else {
68 return (rb_id_serial_t)id;
69 }
70}
71
72static inline int
73sym_type(VALUE sym)
74{
75 ID id;
76 if (STATIC_SYM_P(sym)) {
77 id = RSHIFT(sym, RUBY_SPECIAL_SHIFT);
78 if (id<=tLAST_OP_ID) {
79 return -1;
80 }
81 }
82 else {
83 id = RSYMBOL(sym)->id;
84 }
85 return (int)(id&ID_SCOPE_MASK);
86}
87
88#define is_local_sym(sym) (sym_type(sym)==ID_LOCAL)
89#define is_global_sym(sym) (sym_type(sym)==ID_GLOBAL)
90#define is_instance_sym(sym) (sym_type(sym)==ID_INSTANCE)
91#define is_attrset_sym(sym) (sym_type(sym)==ID_ATTRSET)
92#define is_const_sym(sym) (sym_type(sym)==ID_CONST)
93#define is_class_sym(sym) (sym_type(sym)==ID_CLASS)
94#define is_internal_sym(sym) (sym_type(sym)==ID_INTERNAL)
95
96#ifndef RIPPER
97RUBY_FUNC_EXPORTED
98#else
100#endif
101const uint_least32_t ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
102
103static inline int
104is_global_name_punct(const int c)
105{
106 if (c <= 0x20 || 0x7e < c) return 0;
107 return (ruby_global_name_punct_bits[(c - 0x20) / 32] >> (c % 32)) & 1;
108}
109
110RUBY_SYMBOL_EXPORT_BEGIN
111
112int rb_enc_symname_type(const char *name, long len, rb_encoding *enc, unsigned int allowed_attrset);
113size_t rb_sym_immortal_count(void);
114
115RUBY_SYMBOL_EXPORT_END
116#endif
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
#define STATIC_SYM_P
Old name of RB_STATIC_SYM_P.
Encoding relates APIs.
int len
Length of the buffer.
Definition io.h:8
@ RUBY_SPECIAL_SHIFT
Least significant 8 bits are reserved.
Ruby object's base components.
Definition rbasic.h:69
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