Ruby: symbol.c Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

symbol.c
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#include "gc.h"
13#include "internal.h"
14#include "internal/error.h"
15#include "internal/gc.h"
16#include "internal/hash.h"
17#include "internal/object.h"
18#include "internal/symbol.h"
19#include "internal/vm.h"
20#include "probes.h"
21#include "ruby/encoding.h"
22#include "ruby/st.h"
23#include "symbol.h"
24#include "vm_sync.h"
25#include "builtin.h"
26
27#if defined(USE_SYMBOL_GC) && !(USE_SYMBOL_GC+0)
28# undef USE_SYMBOL_GC
29# define USE_SYMBOL_GC 0
30#else
31# undef USE_SYMBOL_GC
32# define USE_SYMBOL_GC 1
33#endif
34#ifndef SYMBOL_DEBUG
35# define SYMBOL_DEBUG 0
36#endif
37#ifndef CHECK_ID_SERIAL
38# define CHECK_ID_SERIAL SYMBOL_DEBUG
39#endif
40
41#define SYMBOL_PINNED_P(sym) (RSYMBOL(sym)->id&~ID_SCOPE_MASK)
42
43#define STATIC_SYM2ID(sym) RSHIFT((VALUE)(sym), RUBY_SPECIAL_SHIFT)
44
45static ID register_static_symid(ID, const char *, long, rb_encoding *);
46static ID register_static_symid_str(ID, VALUE);
47#define REGISTER_SYMID(id, name) register_static_symid((id), (name), strlen(name), enc)
48#include "id.c"
49
50#define is_identchar(p,e,enc) (ISALNUM((unsigned char)*(p)) || (*(p)) == '_' || !ISASCII(*(p)))
51
52#define op_tbl_count numberof(op_tbl)
53STATIC_ASSERT(op_tbl_name_size, sizeof(op_tbl[0].name) == 3);
54#define op_tbl_len(i) (!op_tbl[i].name[1] ? 1 : !op_tbl[i].name[2] ? 2 : 3)
55
56static void
57Init_op_tbl(void)
58{
59 int i;
60 rb_encoding *const enc = rb_usascii_encoding();
61
62 for (i = '!'; i <= '~'; ++i) {
63 if (!ISALNUM(i) && i != '_') {
64 char c = (char)i;
65 register_static_symid(i, &c, 1, enc);
66 }
67 }
68 for (i = 0; i < op_tbl_count; ++i) {
69 register_static_symid(op_tbl[i].token, op_tbl[i].name, op_tbl_len(i), enc);
70 }
71}
72
73static const int ID_ENTRY_UNIT = 512;
74
75enum id_entry_type {
76 ID_ENTRY_STR,
77 ID_ENTRY_SYM,
78 ID_ENTRY_SIZE
79};
80
81 rb_symbols_t ruby_global_symbols = {tNEXT_ID-1};
82
83static const struct st_hash_type symhash = {
84 rb_str_hash_cmp,
85 rb_str_hash,
86};
87
88void
89Init_sym(void)
90{
91 rb_symbols_t *symbols = &ruby_global_symbols;
92
93 VALUE dsym_fstrs = rb_ident_hash_new();
94 symbols->dsymbol_fstr_hash = dsym_fstrs;
95 rb_gc_register_mark_object(dsym_fstrs);
96 rb_obj_hide(dsym_fstrs);
97
98 symbols->str_sym = st_init_table_with_size(&symhash, 1000);
99 symbols->ids = rb_ary_hidden_new(0);
100 rb_gc_register_mark_object(symbols->ids);
101
102 Init_op_tbl();
103 Init_id();
104}
105
106WARN_UNUSED_RESULT(static VALUE dsymbol_alloc(rb_symbols_t *symbols, const VALUE klass, const VALUE str, rb_encoding *const enc, const ID type));
107WARN_UNUSED_RESULT(static VALUE dsymbol_check(rb_symbols_t *symbols, const VALUE sym));
108WARN_UNUSED_RESULT(static ID lookup_str_id(VALUE str));
109WARN_UNUSED_RESULT(static VALUE lookup_str_sym_with_lock(rb_symbols_t *symbols, const VALUE str));
110WARN_UNUSED_RESULT(static VALUE lookup_str_sym(const VALUE str));
111WARN_UNUSED_RESULT(static VALUE lookup_id_str(ID id));
112WARN_UNUSED_RESULT(static ID intern_str(VALUE str, int mutable));
113
114#define GLOBAL_SYMBOLS_ENTER(symbols) rb_symbols_t *symbols = &ruby_global_symbols; RB_VM_LOCK_ENTER()
115#define GLOBAL_SYMBOLS_LEAVE() RB_VM_LOCK_LEAVE()
116
117 ID
118rb_id_attrset(ID id)
119{
120 VALUE str, sym;
121 int scope;
122
123 if (!is_notop_id(id)) {
124 switch (id) {
125 case tAREF: case tASET:
126 return tASET; /* only exception */
127 }
128 rb_name_error(id, "cannot make operator ID :%"PRIsVALUE" attrset",
129 rb_id2str(id));
130 }
131 else {
132 scope = id_type(id);
133 switch (scope) {
134 case ID_LOCAL: case ID_INSTANCE: case ID_GLOBAL:
135 case ID_CONST: case ID_CLASS: case ID_JUNK:
136 break;
137 case ID_ATTRSET:
138 return id;
139 default:
140 {
141 if ((str = lookup_id_str(id)) != 0) {
142 rb_name_error(id, "cannot make unknown type ID %d:%"PRIsVALUE" attrset",
143 scope, str);
144 }
145 else {
146 rb_name_error_str(Qnil, "cannot make unknown type anonymous ID %d:%"PRIxVALUE" attrset",
147 scope, (VALUE)id);
148 }
149 }
150 }
151 }
152
153 /* make new symbol and ID */
154 if (!(str = lookup_id_str(id))) {
155 static const char id_types[][8] = {
156 "local",
157 "instance",
158 "invalid",
159 "global",
160 "attrset",
161 "const",
162 "class",
163 "junk",
164 };
165 rb_name_error(id, "cannot make anonymous %.*s ID %"PRIxVALUE" attrset",
166 (int)sizeof(id_types[0]), id_types[scope], (VALUE)id);
167 }
168 str = rb_str_dup(str);
169 rb_str_cat(str, "=", 1);
170 sym = lookup_str_sym(str);
171 id = sym ? rb_sym2id(sym) : intern_str(str, 1);
172 return id;
173}
174
175static int
176is_special_global_name(const char *m, const char *e, rb_encoding *enc)
177{
178 int mb = 0;
179
180 if (m >= e) return 0;
181 if (is_global_name_punct(*m)) {
182 ++m;
183 }
184 else if (*m == '-') {
185 if (++m >= e) return 0;
186 if (is_identchar(m, e, enc)) {
187 if (!ISASCII(*m)) mb = 1;
188 m += rb_enc_mbclen(m, e, enc);
189 }
190 }
191 else {
192 if (!ISDIGIT(*m)) return 0;
193 do {
194 if (!ISASCII(*m)) mb = 1;
195 ++m;
196 } while (m < e && ISDIGIT(*m));
197 }
198 return m == e ? mb + 1 : 0;
199}
200
201int
202rb_symname_p(const char *name)
203{
204 return rb_enc_symname_p(name, rb_ascii8bit_encoding());
205}
206
207int
208rb_enc_symname_p(const char *name, rb_encoding *enc)
209{
210 return rb_enc_symname2_p(name, strlen(name), enc);
211}
212
213static int
214rb_sym_constant_char_p(const char *name, long nlen, rb_encoding *enc)
215{
216 int c, len;
217 const char *end = name + nlen;
218
219 if (nlen < 1) return FALSE;
220 if (ISASCII(*name)) return ISUPPER(*name);
221 c = rb_enc_precise_mbclen(name, end, enc);
222 if (!MBCLEN_CHARFOUND_P(c)) return FALSE;
223 len = MBCLEN_CHARFOUND_LEN(c);
224 c = rb_enc_mbc_to_codepoint(name, end, enc);
225 if (ONIGENC_IS_UNICODE(enc)) {
226 static int ctype_titlecase = 0;
227 if (rb_enc_isupper(c, enc)) return TRUE;
228 if (rb_enc_islower(c, enc)) return FALSE;
229 if (!ctype_titlecase) {
230 static const UChar cname[] = "titlecaseletter";
231 static const UChar *const end = cname + sizeof(cname) - 1;
232 ctype_titlecase = ONIGENC_PROPERTY_NAME_TO_CTYPE(enc, cname, end);
233 }
234 if (rb_enc_isctype(c, ctype_titlecase, enc)) return TRUE;
235 }
236 else {
237 /* fallback to case-folding */
238 OnigUChar fold[ONIGENC_GET_CASE_FOLD_CODES_MAX_NUM];
239 const OnigUChar *beg = (const OnigUChar *)name;
240 int r = enc->mbc_case_fold(ONIGENC_CASE_FOLD,
241 &beg, (const OnigUChar *)end,
242 fold, enc);
243 if (r > 0 && (r != len || memcmp(fold, name, r)))
244 return TRUE;
245 }
246 return FALSE;
247}
248
249#define IDSET_ATTRSET_FOR_SYNTAX ((1U<<ID_LOCAL)|(1U<<ID_CONST))
250#define IDSET_ATTRSET_FOR_INTERN (~(~0U<<(1<<ID_SCOPE_SHIFT)) & ~(1U<<ID_ATTRSET))
251
252 struct enc_synmane_type_leading_chars_tag {
253 const enum { invalid, stophere, needmore, } kind;
254 const enum ruby_id_types type;
255 const long nread;
256};
257
258#define t struct enc_synmane_type_leading_chars_tag
259
260static struct enc_synmane_type_leading_chars_tag
261enc_synmane_type_leading_chars(const char *name, long len, rb_encoding *enc, int allowed_attrset)
262{
263 const char *m = name;
264 const char *e = m + len;
265
266 if (! rb_enc_asciicompat(enc)) {
267 return (t) { invalid, 0, 0, };
268 }
269 else if (! m) {
270 return (t) { invalid, 0, 0, };
271 }
272 else if ( len <= 0 ) {
273 return (t) { invalid, 0, 0, };
274 }
275 switch (*m) {
276 case '\0':
277 return (t) { invalid, 0, 0, };
278
279 case '$':
280 if (is_special_global_name(++m, e, enc)) {
281 return (t) { stophere, ID_GLOBAL, len, };
282 }
283 else {
284 return (t) { needmore, ID_GLOBAL, 1, };
285 }
286
287 case '@':
288 switch (*++m) {
289 default: return (t) { needmore, ID_INSTANCE, 1, };
290 case '@': return (t) { needmore, ID_CLASS, 2, };
291 }
292
293 case '<':
294 switch (*++m) {
295 default: return (t) { stophere, ID_JUNK, 1, };
296 case '<': return (t) { stophere, ID_JUNK, 2, };
297 case '=':
298 switch (*++m) {
299 default: return (t) { stophere, ID_JUNK, 2, };
300 case '>': return (t) { stophere, ID_JUNK, 3, };
301 }
302 }
303
304 case '>':
305 switch (*++m) {
306 default: return (t) { stophere, ID_JUNK, 1, };
307 case '>': case '=': return (t) { stophere, ID_JUNK, 2, };
308 }
309
310 case '=':
311 switch (*++m) {
312 default: return (t) { invalid, 0, 1, };
313 case '~': return (t) { stophere, ID_JUNK, 2, };
314 case '=':
315 switch (*++m) {
316 default: return (t) { stophere, ID_JUNK, 2, };
317 case '=': return (t) { stophere, ID_JUNK, 3, };
318 }
319 }
320
321 case '*':
322 switch (*++m) {
323 default: return (t) { stophere, ID_JUNK, 1, };
324 case '*': return (t) { stophere, ID_JUNK, 2, };
325 }
326
327 case '+': case '-':
328 switch (*++m) {
329 default: return (t) { stophere, ID_JUNK, 1, };
330 case '@': return (t) { stophere, ID_JUNK, 2, };
331 }
332
333 case '|': case '^': case '&': case '/': case '%': case '~': case '`':
334 return (t) { stophere, ID_JUNK, 1, };
335
336 case '[':
337 switch (*++m) {
338 default: return (t) { needmore, ID_JUNK, 0, };
339 case ']':
340 switch (*++m) {
341 default: return (t) { stophere, ID_JUNK, 2, };
342 case '=': return (t) { stophere, ID_JUNK, 3, };
343 }
344 }
345
346 case '!':
347 switch (*++m) {
348 case '=': case '~': return (t) { stophere, ID_JUNK, 2, };
349 default:
350 if (allowed_attrset & (1U << ID_JUNK)) {
351 return (t) { needmore, ID_JUNK, 1, };
352 }
353 else {
354 return (t) { stophere, ID_JUNK, 1, };
355 }
356 }
357
358 default:
359 if (rb_sym_constant_char_p(name, len, enc)) {
360 return (t) { needmore, ID_CONST, 0, };
361 }
362 else {
363 return (t) { needmore, ID_LOCAL, 0, };
364 }
365 }
366}
367#undef t
368
369int
370rb_enc_symname_type(const char *name, long len, rb_encoding *enc, unsigned int allowed_attrset)
371{
372 const struct enc_synmane_type_leading_chars_tag f =
373 enc_synmane_type_leading_chars(name, len, enc, allowed_attrset);
374 const char *m = name + f.nread;
375 const char *e = name + len;
376 int type = (int)f.type;
377
378 switch (f.kind) {
379 case invalid: return -1;
380 case stophere: break;
381 case needmore:
382
383 if (m >= e || (*m != '_' && !ISALPHA(*m) && ISASCII(*m))) {
384 if (len > 1 && *(e-1) == '=') {
385 type = rb_enc_symname_type(name, len-1, enc, allowed_attrset);
386 if (allowed_attrset & (1U << type)) return ID_ATTRSET;
387 }
388 return -1;
389 }
390 while (m < e && is_identchar(m, e, enc)) m += rb_enc_mbclen(m, e, enc);
391 if (m >= e) break;
392 switch (*m) {
393 case '!': case '?':
394 if (type == ID_GLOBAL || type == ID_CLASS || type == ID_INSTANCE) return -1;
395 type = ID_JUNK;
396 ++m;
397 if (m + 1 < e || *m != '=') break;
398 /* fall through */
399 case '=':
400 if (!(allowed_attrset & (1U << type))) return -1;
401 type = ID_ATTRSET;
402 ++m;
403 break;
404 }
405 }
406
407 return m == e ? type : -1;
408}
409
410int
411rb_enc_symname2_p(const char *name, long len, rb_encoding *enc)
412{
413 return rb_enc_symname_type(name, len, enc, IDSET_ATTRSET_FOR_SYNTAX) != -1;
414}
415
416static int
417rb_str_symname_type(VALUE name, unsigned int allowed_attrset)
418{
419 const char *ptr = StringValuePtr(name);
420 long len = RSTRING_LEN(name);
421 int type = rb_enc_symname_type(ptr, len, rb_enc_get(name), allowed_attrset);
422 RB_GC_GUARD(name);
423 return type;
424}
425
426static void
427set_id_entry(rb_symbols_t *symbols, rb_id_serial_t num, VALUE str, VALUE sym)
428{
429 ASSERT_vm_locking();
430 size_t idx = num / ID_ENTRY_UNIT;
431
432 VALUE ary, ids = symbols->ids;
433 if (idx >= (size_t)RARRAY_LEN(ids) || NIL_P(ary = rb_ary_entry(ids, (long)idx))) {
434 ary = rb_ary_hidden_new(ID_ENTRY_UNIT * ID_ENTRY_SIZE);
435 rb_ary_store(ids, (long)idx, ary);
436 }
437 idx = (num % ID_ENTRY_UNIT) * ID_ENTRY_SIZE;
438 rb_ary_store(ary, (long)idx + ID_ENTRY_STR, str);
439 rb_ary_store(ary, (long)idx + ID_ENTRY_SYM, sym);
440}
441
442static VALUE
443get_id_serial_entry(rb_id_serial_t num, ID id, const enum id_entry_type t)
444{
445 VALUE result = 0;
446
447 GLOBAL_SYMBOLS_ENTER(symbols);
448 {
449 if (num && num <= symbols->last_id) {
450 size_t idx = num / ID_ENTRY_UNIT;
451 VALUE ids = symbols->ids;
452 VALUE ary;
453 if (idx < (size_t)RARRAY_LEN(ids) && !NIL_P(ary = rb_ary_entry(ids, (long)idx))) {
454 long pos = (long)(num % ID_ENTRY_UNIT) * ID_ENTRY_SIZE;
455 result = rb_ary_entry(ary, pos + t);
456
457 if (NIL_P(result)) {
458 result = 0;
459 }
460 else {
461#if CHECK_ID_SERIAL
462 if (id) {
463 VALUE sym = result;
464 if (t != ID_ENTRY_SYM)
465 sym = rb_ary_entry(ary, pos + ID_ENTRY_SYM);
466 if (STATIC_SYM_P(sym)) {
467 if (STATIC_SYM2ID(sym) != id) result = 0;
468 }
469 else {
470 if (RSYMBOL(sym)->id != id) result = 0;
471 }
472 }
473#endif
474 }
475 }
476 }
477 }
478 GLOBAL_SYMBOLS_LEAVE();
479
480 return result;
481}
482
483static VALUE
484get_id_entry(ID id, const enum id_entry_type t)
485{
486 return get_id_serial_entry(rb_id_to_serial(id), id, t);
487}
488
489int
490rb_static_id_valid_p(ID id)
491{
492 return STATIC_ID2SYM(id) == get_id_entry(id, ID_ENTRY_SYM);
493}
494
495static inline ID
496rb_id_serial_to_id(rb_id_serial_t num)
497{
498 if (is_notop_id((ID)num)) {
499 VALUE sym = get_id_serial_entry(num, 0, ID_ENTRY_SYM);
500 if (sym) return SYM2ID(sym);
501 return ((ID)num << ID_SCOPE_SHIFT) | ID_INTERNAL | ID_STATIC_SYM;
502 }
503 else {
504 return (ID)num;
505 }
506}
507
508#if SYMBOL_DEBUG
509static int
510register_sym_update_callback(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
511{
512 if (existing) {
513 rb_fatal("symbol :% "PRIsVALUE" is already registered with %"PRIxVALUE,
514 (VALUE)*key, (VALUE)*value);
515 }
516 *value = arg;
517 return ST_CONTINUE;
518}
519#endif
520
521static void
522register_sym(rb_symbols_t *symbols, VALUE str, VALUE sym)
523{
524 ASSERT_vm_locking();
525
526#if SYMBOL_DEBUG
527 st_update(symbols->str_sym, (st_data_t)str,
528 register_sym_update_callback, (st_data_t)sym);
529#else
530 st_add_direct(symbols->str_sym, (st_data_t)str, (st_data_t)sym);
531#endif
532}
533
534static void
535unregister_sym(rb_symbols_t *symbols, VALUE str, VALUE sym)
536{
537 ASSERT_vm_locking();
538
539 st_data_t str_data = (st_data_t)str;
540 if (!st_delete(symbols->str_sym, &str_data, NULL)) {
541 rb_bug("%p can't remove str from str_id (%s)", (void *)sym, RSTRING_PTR(str));
542 }
543}
544
545static ID
546register_static_symid(ID id, const char *name, long len, rb_encoding *enc)
547{
548 VALUE str = rb_enc_str_new(name, len, enc);
549 return register_static_symid_str(id, str);
550}
551
552static ID
553register_static_symid_str(ID id, VALUE str)
554{
555 rb_id_serial_t num = rb_id_to_serial(id);
556 VALUE sym = STATIC_ID2SYM(id);
557
558 OBJ_FREEZE(str);
559 str = rb_fstring(str);
560
561 RUBY_DTRACE_CREATE_HOOK(SYMBOL, RSTRING_PTR(str));
562
563 GLOBAL_SYMBOLS_ENTER(symbols)
564 {
565 register_sym(symbols, str, sym);
566 set_id_entry(symbols, num, str, sym);
567 }
568 GLOBAL_SYMBOLS_LEAVE();
569
570 return id;
571}
572
573static int
574sym_check_asciionly(VALUE str, bool fake_str)
575{
576 if (!rb_enc_asciicompat(rb_enc_get(str))) return FALSE;
577 switch (rb_enc_str_coderange(str)) {
578 case ENC_CODERANGE_BROKEN:
579 if (fake_str) {
580 str = rb_enc_str_new(RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
581 }
582 rb_raise(rb_eEncodingError, "invalid symbol in encoding %s :%+"PRIsVALUE,
583 rb_enc_name(rb_enc_get(str)), str);
584 case ENC_CODERANGE_7BIT:
585 return TRUE;
586 }
587 return FALSE;
588}
589
590#if 0
591/*
592 * _str_ itself will be registered at the global symbol table. _str_
593 * can be modified before the registration, since the encoding will be
594 * set to ASCII-8BIT if it is a special global name.
595 */
596
597static inline void
598must_be_dynamic_symbol(VALUE x)
599{
600 if (UNLIKELY(!DYNAMIC_SYM_P(x))) {
601 if (STATIC_SYM_P(x)) {
602 VALUE str = lookup_id_str(RSHIFT((unsigned long)(x),RUBY_SPECIAL_SHIFT));
603
604 if (str) {
605 rb_bug("wrong argument: %s (inappropriate Symbol)", RSTRING_PTR(str));
606 }
607 else {
608 rb_bug("wrong argument: inappropriate Symbol (%p)", (void *)x);
609 }
610 }
611 else {
612 rb_bug("wrong argument type %s (expected Symbol)", rb_builtin_class_name(x));
613 }
614 }
615}
616#endif
617
618static VALUE
619dsymbol_alloc(rb_symbols_t *symbols, const VALUE klass, const VALUE str, rb_encoding * const enc, const ID type)
620{
621 ASSERT_vm_locking();
622
623 const VALUE dsym = rb_newobj_of(klass, T_SYMBOL | FL_WB_PROTECTED);
624 long hashval;
625
626 rb_enc_set_index(dsym, rb_enc_to_index(enc));
627 OBJ_FREEZE(dsym);
628 RB_OBJ_WRITE(dsym, &RSYMBOL(dsym)->fstr, str);
629 RSYMBOL(dsym)->id = type;
630
631 /* we want hashval to be in Fixnum range [ruby-core:15713] r15672 */
632 hashval = (long)rb_str_hash(str);
633 RSYMBOL(dsym)->hashval = RSHIFT((long)hashval, 1);
634 register_sym(symbols, str, dsym);
635 rb_hash_aset(symbols->dsymbol_fstr_hash, str, Qtrue);
636 RUBY_DTRACE_CREATE_HOOK(SYMBOL, RSTRING_PTR(RSYMBOL(dsym)->fstr));
637
638 return dsym;
639}
640
641static inline VALUE
642dsymbol_check(rb_symbols_t *symbols, const VALUE sym)
643{
644 ASSERT_vm_locking();
645
646 if (UNLIKELY(rb_objspace_garbage_object_p(sym))) {
647 const VALUE fstr = RSYMBOL(sym)->fstr;
648 const ID type = RSYMBOL(sym)->id & ID_SCOPE_MASK;
649 RSYMBOL(sym)->fstr = 0;
650 unregister_sym(symbols, fstr, sym);
651 return dsymbol_alloc(symbols, rb_cSymbol, fstr, rb_enc_get(fstr), type);
652 }
653 else {
654 return sym;
655 }
656}
657
658static ID
659lookup_str_id(VALUE str)
660{
661 st_data_t sym_data;
662 int found;
663
664 GLOBAL_SYMBOLS_ENTER(symbols);
665 {
666 found = st_lookup(symbols->str_sym, (st_data_t)str, &sym_data);
667 }
668 GLOBAL_SYMBOLS_LEAVE();
669
670 if (found) {
671 const VALUE sym = (VALUE)sym_data;
672
673 if (STATIC_SYM_P(sym)) {
674 return STATIC_SYM2ID(sym);
675 }
676 else if (DYNAMIC_SYM_P(sym)) {
677 ID id = RSYMBOL(sym)->id;
678 if (id & ~ID_SCOPE_MASK) return id;
679 }
680 else {
681 rb_bug("non-symbol object %s:%"PRIxVALUE" for %"PRIsVALUE" in symbol table",
682 rb_builtin_class_name(sym), sym, str);
683 }
684 }
685 return (ID)0;
686}
687
688static VALUE
689lookup_str_sym_with_lock(rb_symbols_t *symbols, const VALUE str)
690{
691 st_data_t sym_data;
692 if (st_lookup(symbols->str_sym, (st_data_t)str, &sym_data)) {
693 VALUE sym = (VALUE)sym_data;
694 if (DYNAMIC_SYM_P(sym)) {
695 sym = dsymbol_check(symbols, sym);
696 }
697 return sym;
698 }
699 else {
700 return Qfalse;
701 }
702}
703
704static VALUE
705lookup_str_sym(const VALUE str)
706{
707 VALUE sym;
708
709 GLOBAL_SYMBOLS_ENTER(symbols);
710 {
711 sym = lookup_str_sym_with_lock(symbols, str);
712 }
713 GLOBAL_SYMBOLS_LEAVE();
714
715 return sym;
716}
717
718static VALUE
719lookup_id_str(ID id)
720{
721 return get_id_entry(id, ID_ENTRY_STR);
722}
723
724 ID
725rb_intern3(const char *name, long len, rb_encoding *enc)
726{
727 VALUE sym;
728 struct RString fake_str;
729 VALUE str = rb_setup_fake_str(&fake_str, name, len, enc);
730 OBJ_FREEZE(str);
731 sym = lookup_str_sym(str);
732 if (sym) return rb_sym2id(sym);
733 str = rb_enc_str_new(name, len, enc); /* make true string */
734 return intern_str(str, 1);
735}
736
737static ID
738next_id_base_with_lock(rb_symbols_t *symbols)
739{
740 ID id;
741 rb_id_serial_t next_serial = symbols->last_id + 1;
742
743 if (next_serial == 0) {
744 id = (ID)-1;
745 }
746 else {
747 const size_t num = ++symbols->last_id;
748 id = num << ID_SCOPE_SHIFT;
749 }
750
751 return id;
752}
753
754static ID
755next_id_base(void)
756{
757 ID id;
758 GLOBAL_SYMBOLS_ENTER(symbols);
759 {
760 id = next_id_base_with_lock(symbols);
761 }
762 GLOBAL_SYMBOLS_LEAVE();
763 return id;
764}
765
766static ID
767intern_str(VALUE str, int mutable)
768{
769 ID id;
770 ID nid;
771
772 id = rb_str_symname_type(str, IDSET_ATTRSET_FOR_INTERN);
773 if (id == (ID)-1) id = ID_JUNK;
774 if (sym_check_asciionly(str, false)) {
775 if (!mutable) str = rb_str_dup(str);
776 rb_enc_associate(str, rb_usascii_encoding());
777 }
778 if ((nid = next_id_base()) == (ID)-1) {
779 str = rb_str_ellipsize(str, 20);
780 rb_raise(rb_eRuntimeError, "symbol table overflow (symbol %"PRIsVALUE")",
781 str);
782 }
783 id |= nid;
784 id |= ID_STATIC_SYM;
785 return register_static_symid_str(id, str);
786}
787
788 ID
789rb_intern2(const char *name, long len)
790{
791 return rb_intern3(name, len, rb_usascii_encoding());
792}
793
794#undef rb_intern
795 ID
796rb_intern(const char *name)
797{
798 return rb_intern2(name, strlen(name));
799}
800
801 ID
802rb_intern_str(VALUE str)
803{
804 VALUE sym = lookup_str_sym(str);
805
806 if (sym) {
807 return SYM2ID(sym);
808 }
809
810 return intern_str(str, 0);
811}
812
813void
814rb_gc_free_dsymbol(VALUE sym)
815{
816 VALUE str = RSYMBOL(sym)->fstr;
817
818 if (str) {
819 RSYMBOL(sym)->fstr = 0;
820
821 GLOBAL_SYMBOLS_ENTER(symbols);
822 {
823 unregister_sym(symbols, str, sym);
824 rb_hash_delete_entry(symbols->dsymbol_fstr_hash, str);
825 }
826 GLOBAL_SYMBOLS_LEAVE();
827 }
828}
829
830/*
831 * call-seq:
832 * str.intern -> symbol
833 * str.to_sym -> symbol
834 *
835 * Returns the Symbol corresponding to <i>str</i>, creating the
836 * symbol if it did not previously exist. See Symbol#id2name.
837 *
838 * "Koala".intern #=> :Koala
839 * s = 'cat'.to_sym #=> :cat
840 * s == :cat #=> true
841 * s = '@cat'.to_sym #=> :@cat
842 * s == :@cat #=> true
843 *
844 * This can also be used to create symbols that cannot be represented using the
845 * <code>:xxx</code> notation.
846 *
847 * 'cat and dog'.to_sym #=> :"cat and dog"
848 */
849
850 VALUE
851rb_str_intern(VALUE str)
852{
853 VALUE sym;
854
855 GLOBAL_SYMBOLS_ENTER(symbols);
856 {
857 sym = lookup_str_sym_with_lock(symbols, str);
858
859 if (sym) {
860 // ok
861 }
862 else if (USE_SYMBOL_GC) {
863 rb_encoding *enc = rb_enc_get(str);
864 rb_encoding *ascii = rb_usascii_encoding();
865 if (enc != ascii && sym_check_asciionly(str, false)) {
866 str = rb_str_dup(str);
867 rb_enc_associate(str, ascii);
868 OBJ_FREEZE(str);
869 enc = ascii;
870 }
871 else {
872 str = rb_str_dup(str);
873 OBJ_FREEZE(str);
874 }
875 str = rb_fstring(str);
876 int type = rb_str_symname_type(str, IDSET_ATTRSET_FOR_INTERN);
877 if (type < 0) type = ID_JUNK;
878 sym = dsymbol_alloc(symbols, rb_cSymbol, str, enc, type);
879 }
880 else {
881 ID id = intern_str(str, 0);
882 sym = ID2SYM(id);
883 }
884 }
885 GLOBAL_SYMBOLS_LEAVE();
886 return sym;
887}
888
889 ID
890rb_sym2id(VALUE sym)
891{
892 ID id;
893 if (STATIC_SYM_P(sym)) {
894 id = STATIC_SYM2ID(sym);
895 }
896 else if (DYNAMIC_SYM_P(sym)) {
897 GLOBAL_SYMBOLS_ENTER(symbols);
898 {
899 sym = dsymbol_check(symbols, sym);
900 id = RSYMBOL(sym)->id;
901
902 if (UNLIKELY(!(id & ~ID_SCOPE_MASK))) {
903 VALUE fstr = RSYMBOL(sym)->fstr;
904 ID num = next_id_base_with_lock(symbols);
905
906 RSYMBOL(sym)->id = id |= num;
907 /* make it permanent object */
908
909 set_id_entry(symbols, rb_id_to_serial(num), fstr, sym);
910 rb_hash_delete_entry(symbols->dsymbol_fstr_hash, fstr);
911 }
912 }
913 GLOBAL_SYMBOLS_LEAVE();
914 }
915 else {
916 rb_raise(rb_eTypeError, "wrong argument type %s (expected Symbol)",
917 rb_builtin_class_name(sym));
918 }
919 return id;
920}
921
922#undef rb_id2sym
923 VALUE
924rb_id2sym(ID x)
925{
926 if (!DYNAMIC_ID_P(x)) return STATIC_ID2SYM(x);
927 return get_id_entry(x, ID_ENTRY_SYM);
928}
929
930/*
931 * call-seq:
932 * name -> string
933 *
934 * Returns a frozen string representation of +self+ (not including the leading colon):
935 *
936 * :foo.name # => "foo"
937 * :foo.name.frozen? # => true
938 *
939 * Related: Symbol#to_s, Symbol#inspect.
940 */
941
942 VALUE
943rb_sym2str(VALUE sym)
944{
945 if (DYNAMIC_SYM_P(sym)) {
946 return RSYMBOL(sym)->fstr;
947 }
948 else {
949 return rb_id2str(STATIC_SYM2ID(sym));
950 }
951}
952
953 VALUE
954rb_id2str(ID id)
955{
956 return lookup_id_str(id);
957}
958
959const char *
960rb_id2name(ID id)
961{
962 VALUE str = rb_id2str(id);
963
964 if (!str) return 0;
965 return RSTRING_PTR(str);
966}
967
968 ID
969rb_make_internal_id(void)
970{
971 return next_id_base() | ID_INTERNAL | ID_STATIC_SYM;
972}
973
974 ID
975rb_make_temporary_id(size_t n)
976{
977 const ID max_id = RB_ID_SERIAL_MAX & ~0xffff;
978 const ID id = max_id - (ID)n;
979 if (id <= ruby_global_symbols.last_id) {
980 rb_raise(rb_eRuntimeError, "too big to make temporary ID: %" PRIdSIZE, n);
981 }
982 return (id << ID_SCOPE_SHIFT) | ID_STATIC_SYM | ID_INTERNAL;
983}
984
985static int
986symbols_i(st_data_t key, st_data_t value, st_data_t arg)
987{
988 VALUE ary = (VALUE)arg;
989 VALUE sym = (VALUE)value;
990
991 if (STATIC_SYM_P(sym)) {
992 rb_ary_push(ary, sym);
993 return ST_CONTINUE;
994 }
995 else if (!DYNAMIC_SYM_P(sym)) {
996 rb_bug("invalid symbol: %s", RSTRING_PTR((VALUE)key));
997 }
998 else if (!SYMBOL_PINNED_P(sym) && rb_objspace_garbage_object_p(sym)) {
999 RSYMBOL(sym)->fstr = 0;
1000 return ST_DELETE;
1001 }
1002 else {
1003 rb_ary_push(ary, sym);
1004 return ST_CONTINUE;
1005 }
1006
1007}
1008
1009 VALUE
1010rb_sym_all_symbols(void)
1011{
1012 VALUE ary;
1013
1014 GLOBAL_SYMBOLS_ENTER(symbols);
1015 {
1016 ary = rb_ary_new2(symbols->str_sym->num_entries);
1017 st_foreach(symbols->str_sym, symbols_i, ary);
1018 }
1019 GLOBAL_SYMBOLS_LEAVE();
1020
1021 return ary;
1022}
1023
1024size_t
1025rb_sym_immortal_count(void)
1026{
1027 return (size_t)ruby_global_symbols.last_id;
1028}
1029
1030int
1031rb_is_const_id(ID id)
1032{
1033 return is_const_id(id);
1034}
1035
1036int
1037rb_is_class_id(ID id)
1038{
1039 return is_class_id(id);
1040}
1041
1042int
1043rb_is_global_id(ID id)
1044{
1045 return is_global_id(id);
1046}
1047
1048int
1049rb_is_instance_id(ID id)
1050{
1051 return is_instance_id(id);
1052}
1053
1054int
1055rb_is_attrset_id(ID id)
1056{
1057 return is_attrset_id(id);
1058}
1059
1060int
1061rb_is_local_id(ID id)
1062{
1063 return is_local_id(id);
1064}
1065
1066int
1067rb_is_junk_id(ID id)
1068{
1069 return is_junk_id(id);
1070}
1071
1072int
1073rb_is_const_sym(VALUE sym)
1074{
1075 return is_const_sym(sym);
1076}
1077
1078int
1079rb_is_attrset_sym(VALUE sym)
1080{
1081 return is_attrset_sym(sym);
1082}
1083
1084 ID
1085rb_check_id(volatile VALUE *namep)
1086{
1087 VALUE tmp;
1088 VALUE name = *namep;
1089
1090 if (STATIC_SYM_P(name)) {
1091 return STATIC_SYM2ID(name);
1092 }
1093 else if (DYNAMIC_SYM_P(name)) {
1094 if (SYMBOL_PINNED_P(name)) {
1095 return RSYMBOL(name)->id;
1096 }
1097 else {
1098 *namep = RSYMBOL(name)->fstr;
1099 return 0;
1100 }
1101 }
1102 else if (!RB_TYPE_P(name, T_STRING)) {
1103 tmp = rb_check_string_type(name);
1104 if (NIL_P(tmp)) {
1105 rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol nor a string",
1106 name);
1107 }
1108 name = tmp;
1109 *namep = name;
1110 }
1111
1112 sym_check_asciionly(name, false);
1113
1114 return lookup_str_id(name);
1115}
1116
1117// Used by yjit for handling .send without throwing exceptions
1118 ID
1119rb_get_symbol_id(VALUE name)
1120{
1121 if (STATIC_SYM_P(name)) {
1122 return STATIC_SYM2ID(name);
1123 }
1124 else if (DYNAMIC_SYM_P(name)) {
1125 if (SYMBOL_PINNED_P(name)) {
1126 return RSYMBOL(name)->id;
1127 }
1128 else {
1129 return 0;
1130 }
1131 }
1132 else {
1133 RUBY_ASSERT_ALWAYS(RB_TYPE_P(name, T_STRING));
1134 return lookup_str_id(name);
1135 }
1136}
1137
1138
1139 VALUE
1140rb_check_symbol(volatile VALUE *namep)
1141{
1142 VALUE sym;
1143 VALUE tmp;
1144 VALUE name = *namep;
1145
1146 if (STATIC_SYM_P(name)) {
1147 return name;
1148 }
1149 else if (DYNAMIC_SYM_P(name)) {
1150 if (!SYMBOL_PINNED_P(name)) {
1151 GLOBAL_SYMBOLS_ENTER(symbols);
1152 {
1153 name = dsymbol_check(symbols, name);
1154 }
1155 GLOBAL_SYMBOLS_LEAVE();
1156
1157 *namep = name;
1158 }
1159 return name;
1160 }
1161 else if (!RB_TYPE_P(name, T_STRING)) {
1162 tmp = rb_check_string_type(name);
1163 if (NIL_P(tmp)) {
1164 rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol nor a string",
1165 name);
1166 }
1167 name = tmp;
1168 *namep = name;
1169 }
1170
1171 sym_check_asciionly(name, false);
1172
1173 if ((sym = lookup_str_sym(name)) != 0) {
1174 return sym;
1175 }
1176
1177 return Qnil;
1178}
1179
1180 ID
1181rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc)
1182{
1183 struct RString fake_str;
1184 const VALUE name = rb_setup_fake_str(&fake_str, ptr, len, enc);
1185
1186 sym_check_asciionly(name, true);
1187
1188 return lookup_str_id(name);
1189}
1190
1191 VALUE
1192rb_check_symbol_cstr(const char *ptr, long len, rb_encoding *enc)
1193{
1194 VALUE sym;
1195 struct RString fake_str;
1196 const VALUE name = rb_setup_fake_str(&fake_str, ptr, len, enc);
1197
1198 sym_check_asciionly(name, true);
1199
1200 if ((sym = lookup_str_sym(name)) != 0) {
1201 return sym;
1202 }
1203
1204 return Qnil;
1205}
1206
1207#undef rb_sym_intern_ascii_cstr
1208#ifdef __clang__
1209NOINLINE(VALUE rb_sym_intern(const char *ptr, long len, rb_encoding *enc));
1210#else
1211FUNC_MINIMIZED(VALUE rb_sym_intern(const char *ptr, long len, rb_encoding *enc));
1212FUNC_MINIMIZED(VALUE rb_sym_intern_ascii(const char *ptr, long len));
1213FUNC_MINIMIZED(VALUE rb_sym_intern_ascii_cstr(const char *ptr));
1214#endif
1215
1216 VALUE
1217rb_sym_intern(const char *ptr, long len, rb_encoding *enc)
1218{
1219 struct RString fake_str;
1220 const VALUE name = rb_setup_fake_str(&fake_str, ptr, len, enc);
1221 return rb_str_intern(name);
1222}
1223
1224 VALUE
1225rb_sym_intern_ascii(const char *ptr, long len)
1226{
1227 return rb_sym_intern(ptr, len, rb_usascii_encoding());
1228}
1229
1230 VALUE
1231rb_sym_intern_ascii_cstr(const char *ptr)
1232{
1233 return rb_sym_intern_ascii(ptr, strlen(ptr));
1234}
1235
1236 VALUE
1237rb_to_symbol_type(VALUE obj)
1238{
1239 return rb_convert_type_with_id(obj, T_SYMBOL, "Symbol", idTo_sym);
1240}
1241
1242int
1243rb_is_const_name(VALUE name)
1244{
1245 return rb_str_symname_type(name, 0) == ID_CONST;
1246}
1247
1248int
1249rb_is_class_name(VALUE name)
1250{
1251 return rb_str_symname_type(name, 0) == ID_CLASS;
1252}
1253
1254int
1255rb_is_instance_name(VALUE name)
1256{
1257 return rb_str_symname_type(name, 0) == ID_INSTANCE;
1258}
1259
1260int
1261rb_is_local_name(VALUE name)
1262{
1263 return rb_str_symname_type(name, 0) == ID_LOCAL;
1264}
1265
1266#include "id_table.c"
1267#include "symbol.rbinc"
RUBY_ASSERT_ALWAYS
#define RUBY_ASSERT_ALWAYS(expr)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
Definition assert.h:167
rb_enc_isupper
static bool rb_enc_isupper(OnigCodePoint c, rb_encoding *enc)
Identical to rb_isupper(), except it additionally takes an encoding.
Definition ctype.h:124
rb_enc_isctype
static bool rb_enc_isctype(OnigCodePoint c, OnigCtype t, rb_encoding *enc)
Queries if the passed code point is of passed character type in the passed encoding.
Definition ctype.h:63
rb_enc_islower
static bool rb_enc_islower(OnigCodePoint c, rb_encoding *enc)
Identical to rb_islower(), except it additionally takes an encoding.
Definition ctype.h:110
ENC_CODERANGE_7BIT
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
Definition coderange.h:180
T_STRING
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
ISUPPER
#define ISUPPER
Old name of rb_isupper.
Definition ctype.h:89
ID2SYM
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
OBJ_FREEZE
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:143
SYM2ID
#define SYM2ID
Old name of RB_SYM2ID.
Definition symbol.h:45
ISDIGIT
#define ISDIGIT
Old name of rb_isdigit.
Definition ctype.h:93
STATIC_SYM_P
#define STATIC_SYM_P
Old name of RB_STATIC_SYM_P.
Definition special_consts.h:57
MBCLEN_CHARFOUND_LEN
#define MBCLEN_CHARFOUND_LEN(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_LEN.
Definition encoding.h:533
ISALPHA
#define ISALPHA
Old name of rb_isalpha.
Definition ctype.h:92
ISASCII
#define ISASCII
Old name of rb_isascii.
Definition ctype.h:85
Qtrue
#define Qtrue
Old name of RUBY_Qtrue.
Definition special_consts.h:61
DYNAMIC_SYM_P
#define DYNAMIC_SYM_P
Old name of RB_DYNAMIC_SYM_P.
Definition value_type.h:86
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
Qfalse
#define Qfalse
Old name of RUBY_Qfalse.
Definition special_consts.h:59
ENC_CODERANGE_BROKEN
#define ENC_CODERANGE_BROKEN
Old name of RUBY_ENC_CODERANGE_BROKEN.
Definition coderange.h:182
NIL_P
#define NIL_P
Old name of RB_NIL_P.
Definition special_consts.h:55
MBCLEN_CHARFOUND_P
#define MBCLEN_CHARFOUND_P(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_P.
Definition encoding.h:532
FL_WB_PROTECTED
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
Definition fl_type.h:59
T_SYMBOL
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
rb_ary_new2
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:651
ISALNUM
#define ISALNUM
Old name of rb_isalnum.
Definition ctype.h:91
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
Definition error.c:3150
rb_bug
void rb_bug(const char *fmt,...)
Interpreter panic switch.
Definition error.c:794
rb_name_error
void rb_name_error(ID id, const char *fmt,...)
Raises an instance of rb_eNameError.
Definition error.c:1784
rb_eTypeError
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1091
rb_name_error_str
void rb_name_error_str(VALUE str, const char *fmt,...)
Identical to rb_name_error(), except it takes a VALUE instead of ID.
Definition error.c:1799
rb_fatal
void rb_fatal(const char *fmt,...)
Raises the unsung "fatal" exception.
Definition error.c:3201
rb_eRuntimeError
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1089
rb_eEncodingError
VALUE rb_eEncodingError
EncodingError exception.
Definition error.c:1097
rb_obj_hide
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition object.c:84
rb_cSymbol
VALUE rb_cSymbol
Sumbol class.
Definition string.c:80
RB_OBJ_WRITE
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition rgengc.h:220
encoding.h
Encoding relates APIs.
rb_enc_name
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
Definition encoding.h:433
rb_enc_asciicompat
static bool rb_enc_asciicompat(rb_encoding *enc)
Queries if the passed encoding is in some sense compatible with ASCII.
Definition encoding.h:784
rb_enc_mbc_to_codepoint
static OnigCodePoint rb_enc_mbc_to_codepoint(const char *p, const char *e, rb_encoding *enc)
Identical to rb_enc_codepoint(), except it assumes the passed character is not broken.
Definition encoding.h:607
rb_enc_str_coderange
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
Definition string.c:821
rb_enc_str_new
VALUE rb_enc_str_new(const char *ptr, long len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it additionally takes an encoding.
Definition string.c:981
rb_enc_symname_p
int rb_enc_symname_p(const char *str, rb_encoding *enc)
Identical to rb_symname_p(), except it additionally takes an encoding.
Definition symbol.c:208
rb_intern3
ID rb_intern3(const char *name, long len, rb_encoding *enc)
Identical to rb_intern2(), except it additionally takes an encoding.
Definition symbol.c:725
rb_check_symbol_cstr
VALUE rb_check_symbol_cstr(const char *ptr, long len, rb_encoding *enc)
Identical to rb_check_id_cstr(), except for the return type.
Definition symbol.c:1192
rb_enc_symname2_p
int rb_enc_symname2_p(const char *name, long len, rb_encoding *enc)
Identical to rb_enc_symname_p(), except it additionally takes the passed string's length.
Definition symbol.c:411
rb_check_id_cstr
ID rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc)
Identical to rb_check_id(), except it takes a pointer to a memory region instead of Ruby's string.
Definition symbol.c:1181
rb_sym_all_symbols
VALUE rb_sym_all_symbols(void)
Collects every single bits of symbols that have ever interned in the entire history of the current pr...
Definition symbol.c:1010
rb_is_global_id
int rb_is_global_id(ID id)
Classifies the given ID, then sees if it is a global variable.
Definition symbol.c:1043
rb_is_instance_id
int rb_is_instance_id(ID id)
Classifies the given ID, then sees if it is an instance variable.
Definition symbol.c:1049
rb_is_const_id
int rb_is_const_id(ID id)
Classifies the given ID, then sees if it is a constant.
Definition symbol.c:1031
rb_is_junk_id
int rb_is_junk_id(ID)
Classifies the given ID, then sees if it is a junk ID.
Definition symbol.c:1067
rb_symname_p
int rb_symname_p(const char *str)
Sees if the passed C string constructs a valid syntactic symbol.
Definition symbol.c:202
rb_id_attrset
ID rb_id_attrset(ID id)
Calculates an ID of attribute writer.
Definition symbol.c:118
rb_is_class_id
int rb_is_class_id(ID id)
Classifies the given ID, then sees if it is a class variable.
Definition symbol.c:1037
rb_is_attrset_id
int rb_is_attrset_id(ID id)
Classifies the given ID, then sees if it is an attribute writer.
Definition symbol.c:1055
rb_is_local_id
int rb_is_local_id(ID id)
Classifies the given ID, then sees if it is a local variable.
Definition symbol.c:1061
rb_str_hash_cmp
int rb_str_hash_cmp(VALUE str1, VALUE str2)
Compares two strings.
Definition string.c:3581
rb_str_ellipsize
VALUE rb_str_ellipsize(VALUE str, long len)
Shortens str and adds three dots, an ellipsis, if it is longer than len characters.
Definition string.c:10890
rb_str_dup
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:1834
rb_str_hash
st_index_t rb_str_hash(VALUE str)
Calculates a hash value of a string.
Definition string.c:3571
rb_str_cat
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
Definition string.c:3177
rb_check_string_type
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:2640
rb_str_intern
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
Definition symbol.c:851
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:789
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:1140
rb_id2sym
VALUE rb_id2sym(ID id)
Allocates an instance of rb_cSymbol that has the given id.
Definition symbol.c:924
rb_check_id
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition symbol.c:1085
rb_intern
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
Definition symbol.c:796
rb_sym2str
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
Definition symbol.c:943
rb_sym2id
ID rb_sym2id(VALUE obj)
Converts an instance of rb_cSymbol into an ID.
Definition symbol.c:890
rb_id2name
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
Definition symbol.c:960
rb_intern_str
ID rb_intern_str(VALUE str)
Identical to rb_intern(), except it takes an instance of rb_cString.
Definition symbol.c:802
RB_GC_GUARD
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:161
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
RARRAY_LEN
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:68
StringValuePtr
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
Definition rstring.h:82
RSTRING_LEN
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
Definition rstring.h:484
RSTRING_PTR
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
Definition rstring.h:498
RUBY_SPECIAL_SHIFT
@ RUBY_SPECIAL_SHIFT
Least significant 8 bits are reserved.
Definition special_consts.h:119
OnigEncodingTypeST
Definition onigmo.h:160
RString
Ruby's String.
Definition rstring.h:231
RString::len
long len
Length of the string, not including terminating NUL character.
Definition rstring.h:250
RString::ary
char ary[RSTRING_EMBED_LEN_MAX+1]
When a string is short enough, it uses this area to store the contents themselves.
Definition rstring.h:298
RString::ptr
char * ptr
Pointer to the contents of the string.
Definition rstring.h:258
enc_synmane_type_leading_chars_tag
Definition symbol.c:252
rb_symbols_t
Definition symbol.h:61
st_hash_type
Definition st.h:61
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
RB_TYPE_P
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:375