Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

symbol.h
Go to the documentation of this file.
1 /**********************************************************************
2
3 symbol.h -
4
5 $Author$
6 created at: Tue Jul 8 15:49:54 JST 2014
7
8 Copyright (C) 2014 Yukihiro Matsumoto
9
10 **********************************************************************/
11
12 #ifndef RUBY_SYMBOL_H
13 #define RUBY_SYMBOL_H 1
14
15 #include "id.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
26 struct RSymbol {
27 struct RBasic basic;
28 st_index_t hashval;
29 VALUE fstr;
30 ID id;
31 };
32
33 #define RSYMBOL(obj) (R_CAST(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_junk_id(id) (id_type(id)==ID_JUNK)
43
44 static inline int
45 id_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
55 typedef uint32_t rb_id_serial_t;
56 static 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
61 typedef struct {
62 rb_id_serial_t last_id;
63 st_table *str_sym;
64 VALUE ids;
65 VALUE dsymbol_fstr_hash;
66 } rb_symbols_t;
67
68 static inline rb_id_serial_t
69 rb_id_to_serial(ID id)
70 {
71 if (is_notop_id(id)) {
72 return (rb_id_serial_t)(id >> ID_SCOPE_SHIFT);
73 }
74 else {
75 return (rb_id_serial_t)id;
76 }
77 }
78
79 static inline int
80 sym_type(VALUE sym)
81 {
82 ID id;
83 if (STATIC_SYM_P(sym)) {
84 id = RSHIFT(sym, RUBY_SPECIAL_SHIFT);
85 if (id<=tLAST_OP_ID) {
86 return -1;
87 }
88 }
89 else {
90 id = RSYMBOL(sym)->id;
91 }
92 return (int)(id&ID_SCOPE_MASK);
93 }
94
95 #define is_local_sym(sym) (sym_type(sym)==ID_LOCAL)
96 #define is_global_sym(sym) (sym_type(sym)==ID_GLOBAL)
97 #define is_instance_sym(sym) (sym_type(sym)==ID_INSTANCE)
98 #define is_attrset_sym(sym) (sym_type(sym)==ID_ATTRSET)
99 #define is_const_sym(sym) (sym_type(sym)==ID_CONST)
100 #define is_class_sym(sym) (sym_type(sym)==ID_CLASS)
101 #define is_junk_sym(sym) (sym_type(sym)==ID_JUNK)
102
103 RUBY_FUNC_EXPORTED const unsigned int ruby_global_name_punct_bits[(0x7e - 0x20 + 31) / 32];
104
105 static inline int
106 is_global_name_punct(const int c)
107 {
108 if (c <= 0x20 || 0x7e < c) return 0;
109 return (ruby_global_name_punct_bits[(c - 0x20) / 32] >> (c % 32)) & 1;
110 }
111
112 int rb_enc_symname_type(const char *name, long len, rb_encoding *enc, unsigned int allowed_attrset);
113
114 RUBY_SYMBOL_EXPORT_BEGIN
115
116 size_t rb_sym_immortal_count(void);
117
118 RUBY_SYMBOL_EXPORT_END
119 #endif
rb_sym_immortal_count
RUBY_SYMBOL_EXPORT_BEGIN size_t rb_sym_immortal_count(void)
Definition: symbol.c:848
ID
unsigned long ID
Definition: ruby.h:103
RUBY_SYMBOL_EXPORT_END
#define RUBY_SYMBOL_EXPORT_END
Definition: missing.h:49
STATIC_SYM_P
#define STATIC_SYM_P(x)
Definition: ruby.h:411
id
const int id
Definition: nkf.c:209
RSymbol::hashval
st_index_t hashval
Definition: symbol.h:28
rb_symbols_t::str_sym
st_table * str_sym
Definition: symbol.h:63
RUBY_SPECIAL_SHIFT
@ RUBY_SPECIAL_SHIFT
Definition: ruby.h:460
VALUE
unsigned long VALUE
Definition: ruby.h:102
rb_id_serial_t
uint32_t rb_id_serial_t
Definition: symbol.h:55
rb_symbols_t::ids
VALUE ids
Definition: symbol.h:64
id.h
ID_SCOPE_MASK
#define ID_SCOPE_MASK
Definition: id.h:32
RSYMBOL
#define RSYMBOL(obj)
Definition: symbol.h:33
tLAST_OP_ID
@ tLAST_OP_ID
Definition: id.h:286
CHAR_BIT
#define CHAR_BIT
Definition: ruby.h:227
ruby_global_name_punct_bits
const RUBY_FUNC_EXPORTED unsigned int ruby_global_name_punct_bits[(0x7e - 0x20+31)/32]
Definition: symbol.h:103
rb_symbols_t::dsymbol_fstr_hash
VALUE dsymbol_fstr_hash
Definition: symbol.h:65
uint32_t
unsigned int uint32_t
Definition: sha2.h:101
RSymbol::basic
struct RBasic basic
Definition: symbol.h:27
RUBY_FUNC_EXPORTED
#define RUBY_FUNC_EXPORTED
Definition: defines.h:391
RSymbol::id
ID id
Definition: symbol.h:30
rb_encoding
const typedef OnigEncodingType rb_encoding
Definition: encoding.h:115
UINT32_MAX
#define UINT32_MAX
Definition: rb_mjit_min_header-2.7.0.h:2950
rb_enc_symname_type
int rb_enc_symname_type(const char *name, long len, rb_encoding *enc, unsigned int allowed_attrset)
Definition: symbol.c:233
sym
#define sym(x)
Definition: date_core.c:3716
st_index_t
st_data_t st_index_t
Definition: st.h:50
RSHIFT
#define RSHIFT(x, y)
Definition: rb_mjit_min_header-2.7.0.h:414
ID_SCOPE_SHIFT
#define ID_SCOPE_SHIFT
Definition: id.h:31
RUBY_SYMBOL_EXPORT_BEGIN
#define RUBY_SYMBOL_EXPORT_BEGIN
Definition: missing.h:48
RSymbol
Definition: symbol.h:26
RUBY_ID_SCOPE_SHIFT
@ RUBY_ID_SCOPE_SHIFT
Definition: id.h:35
is_notop_id
#define is_notop_id(id)
Definition: symbol.h:35
len
uint8_t len
Definition: escape.c:17
rb_symbols_t
Definition: symbol.h:61
rb_symbols_t::last_id
rb_id_serial_t last_id
Definition: symbol.h:62
st_table
Definition: st.h:79
RSymbol::fstr
VALUE fstr
Definition: symbol.h:29
name
const char * name
Definition: nkf.c:208