1 /*
2 * Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "ci/ciSymbol.hpp"
26 #include "ci/ciSymbols.hpp"
27 #include "ci/ciUtilities.inline.hpp"
28 #include "classfile/symbolTable.hpp"
29 #include "classfile/vmSymbols.hpp"
30 #include "memory/oopFactory.hpp"
31 #include "prims/methodHandles.hpp"
32
33 // ------------------------------------------------------------------
34 // ciSymbol::ciSymbol
35 ciSymbol::ciSymbol(Symbol* s, vmSymbolID sid)
36 : _symbol(s), _sid(sid)
37 {
38 assert(_symbol != nullptr, "adding null symbol");
39 _symbol->increment_refcount(); // increment ref count
40 assert(sid_ok(), "sid must be consistent with vmSymbols");
41 }
42
43 DEBUG_ONLY(bool ciSymbol::sid_ok() { return vmSymbols::find_sid(get_symbol()) == _sid; })
44
45 // ciSymbol
46 //
47 // This class represents a Symbol* in the HotSpot virtual
48 // machine.
49
50 // ------------------------------------------------------------------
51 // ciSymbol::as_utf8
52 //
53 // The text of the symbol as a null-terminated C string.
54 const char* ciSymbol::as_utf8() {
55 GUARDED_VM_QUICK_ENTRY(return get_symbol()->as_utf8();)
56 }
57
58 // The text of the symbol as a null-terminated C string.
59 const char* ciSymbol::as_quoted_ascii() {
60 GUARDED_VM_QUICK_ENTRY(return get_symbol()->as_quoted_ascii();)
61 }
62
63 // ------------------------------------------------------------------
64 // ciSymbol::base
65 const u1* ciSymbol::base() {
66 GUARDED_VM_ENTRY(return get_symbol()->base();)
67 }
68
69 // ------------------------------------------------------------------
70 // ciSymbol::char_at
71 char ciSymbol::char_at(int i) {
72 GUARDED_VM_ENTRY(return get_symbol()->char_at(i);)
73 }
74
75 // ------------------------------------------------------------------
76 // ciSymbol::starts_with
77 //
78 // Tests if the symbol starts with the given prefix.
79 bool ciSymbol::starts_with(const char* prefix, int len) const {
80 GUARDED_VM_ENTRY(return get_symbol()->starts_with(prefix, len);)
81 }
82 bool ciSymbol::starts_with(char prefix_char) const {
83 GUARDED_VM_ENTRY(return get_symbol()->starts_with(prefix_char);)
84 }
85
86 // ------------------------------------------------------------------
87 // ciSymbol::ends_with
88 //
89 // Tests if the symbol ends with the given suffix.
90 bool ciSymbol::ends_with(const char* suffix, int len) const {
91 GUARDED_VM_ENTRY(return get_symbol()->ends_with(suffix, len);)
92 }
93 bool ciSymbol::ends_with(char suffix_char) const {
94 GUARDED_VM_ENTRY(return get_symbol()->ends_with(suffix_char);)
95 }
96
97 bool ciSymbol::is_signature_polymorphic_name() const {
98 GUARDED_VM_ENTRY(return MethodHandles::is_signature_polymorphic_name(get_symbol());)
99 }
100
101 // ------------------------------------------------------------------
102 // ciSymbol::index_of
103 //
104 // Determines where the symbol contains the given substring.
105 int ciSymbol::index_of_at(int i, const char* str, int len) const {
106 GUARDED_VM_ENTRY(return get_symbol()->index_of_at(i, str, len);)
107 }
108
109 // ------------------------------------------------------------------
110 // ciSymbol::utf8_length
111 int ciSymbol::utf8_length() {
112 GUARDED_VM_ENTRY(return get_symbol()->utf8_length();)
113 }
114
115 // ------------------------------------------------------------------
116 // ciSymbol::print_impl
117 //
118 // Implementation of the print method
119 void ciSymbol::print_impl(outputStream* st) {
120 st->print(" value=");
121 print_symbol_on(st);
122 }
123
124 // ------------------------------------------------------------------
125 // ciSymbol::print_symbol_on
126 //
127 // Print the value of this symbol on an outputStream
128 void ciSymbol::print_symbol_on(outputStream *st) {
129 GUARDED_VM_ENTRY(get_symbol()->print_symbol_on(st);)
130 }
131
132 const char* ciSymbol::as_klass_external_name() const {
133 GUARDED_VM_ENTRY(return get_symbol()->as_klass_external_name(););
134 }
135
136 // ------------------------------------------------------------------
137 // ciSymbol::make_impl
138 //
139 // Make a ciSymbol from a C string (implementation).
140 ciSymbol* ciSymbol::make_impl(const char* s) {
141 EXCEPTION_CONTEXT;
142 TempNewSymbol sym = SymbolTable::new_symbol(s);
143 return CURRENT_THREAD_ENV->get_symbol(sym);
144 }
145
146 // ------------------------------------------------------------------
147 // ciSymbol::make
148 //
149 // Make a ciSymbol from a C string.
150 ciSymbol* ciSymbol::make(const char* s) {
151 GUARDED_VM_ENTRY(return make_impl(s);)
152 }