1 /*
  2  * Copyright (c) 1999, 2023, 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 "precompiled.hpp"
 26 #include "ci/ciSymbol.hpp"
 27 #include "ci/ciSymbols.hpp"
 28 #include "ci/ciUtilities.inline.hpp"
 29 #include "classfile/symbolTable.hpp"
 30 #include "classfile/vmSymbols.hpp"
 31 #include "memory/oopFactory.hpp"
 32 #include "prims/methodHandles.hpp"
 33 
 34 // ------------------------------------------------------------------
 35 // ciSymbol::ciSymbol
 36 ciSymbol::ciSymbol(Symbol* s, vmSymbolID sid)
 37   : _symbol(s), _sid(sid)
 38 {
 39   assert(_symbol != nullptr, "adding null symbol");
 40   _symbol->increment_refcount();  // increment ref count
 41   assert(sid_ok(), "sid must be consistent with vmSymbols");
 42 }
 43 
 44 DEBUG_ONLY(bool ciSymbol::sid_ok() { return vmSymbols::find_sid(get_symbol()) == _sid; })
 45 
 46 // ciSymbol
 47 //
 48 // This class represents a Symbol* in the HotSpot virtual
 49 // machine.
 50 
 51 // ------------------------------------------------------------------
 52 // ciSymbol::as_utf8
 53 //
 54 // The text of the symbol as a null-terminated C string.
 55 const char* ciSymbol::as_utf8() {
 56   GUARDED_VM_QUICK_ENTRY(return get_symbol()->as_utf8();)
 57 }
 58 
 59 // The text of the symbol as a null-terminated C string.
 60 const char* ciSymbol::as_quoted_ascii() {
 61   GUARDED_VM_QUICK_ENTRY(return get_symbol()->as_quoted_ascii();)
 62 }
 63 
 64 // ------------------------------------------------------------------
 65 // ciSymbol::base
 66 const u1* ciSymbol::base() {
 67   GUARDED_VM_ENTRY(return get_symbol()->base();)
 68 }
 69 
 70 // ------------------------------------------------------------------
 71 // ciSymbol::char_at
 72 char ciSymbol::char_at(int i) {
 73   GUARDED_VM_ENTRY(return get_symbol()->char_at(i);)
 74 }
 75 
 76 // ------------------------------------------------------------------
 77 // ciSymbol::starts_with
 78 //
 79 // Tests if the symbol starts with the given prefix.
 80 bool ciSymbol::starts_with(const char* prefix, int len) const {
 81   GUARDED_VM_ENTRY(return get_symbol()->starts_with(prefix, len);)
 82 }
 83 bool ciSymbol::starts_with(char prefix_char) const {
 84   GUARDED_VM_ENTRY(return get_symbol()->starts_with(prefix_char);)
 85 }
 86 
 87 // ------------------------------------------------------------------
 88 // ciSymbol::ends_with
 89 //
 90 // Tests if the symbol ends with the given suffix.
 91 bool ciSymbol::ends_with(const char* suffix, int len) const {
 92   GUARDED_VM_ENTRY(return get_symbol()->ends_with(suffix, len);)
 93 }
 94 bool ciSymbol::ends_with(char suffix_char) const {
 95   GUARDED_VM_ENTRY(return get_symbol()->ends_with(suffix_char);)
 96 }
 97 
 98 bool ciSymbol::is_signature_polymorphic_name()  const {
 99   GUARDED_VM_ENTRY(return MethodHandles::is_signature_polymorphic_name(get_symbol());)
100 }
101 
102 // ------------------------------------------------------------------
103 // ciSymbol::index_of
104 //
105 // Determines where the symbol contains the given substring.
106 int ciSymbol::index_of_at(int i, const char* str, int len) const {
107   GUARDED_VM_ENTRY(return get_symbol()->index_of_at(i, str, len);)
108 }
109 
110 // ------------------------------------------------------------------
111 // ciSymbol::utf8_length
112 int ciSymbol::utf8_length() {
113   GUARDED_VM_ENTRY(return get_symbol()->utf8_length();)
114 }
115 
116 // ------------------------------------------------------------------
117 // ciSymbol::print_impl
118 //
119 // Implementation of the print method
120 void ciSymbol::print_impl(outputStream* st) {
121   st->print(" value=");
122   print_symbol_on(st);
123 }
124 
125 // ------------------------------------------------------------------
126 // ciSymbol::print_symbol_on
127 //
128 // Print the value of this symbol on an outputStream
129 void ciSymbol::print_symbol_on(outputStream *st) {
130   GUARDED_VM_ENTRY(get_symbol()->print_symbol_on(st);)
131 }
132 
133 const char* ciSymbol::as_klass_external_name() const {
134   GUARDED_VM_ENTRY(return get_symbol()->as_klass_external_name(););
135 }
136 
137 // ------------------------------------------------------------------
138 // ciSymbol::make_impl
139 //
140 // Make a ciSymbol from a C string (implementation).
141 ciSymbol* ciSymbol::make_impl(const char* s) {
142   EXCEPTION_CONTEXT;
143   TempNewSymbol sym = SymbolTable::new_symbol(s);
144   return CURRENT_THREAD_ENV->get_symbol(sym);
145 }
146 
147 // ------------------------------------------------------------------
148 // ciSymbol::make
149 //
150 // Make a ciSymbol from a C string.
151 ciSymbol* ciSymbol::make(const char* s) {
152   GUARDED_VM_ENTRY(return make_impl(s);)
153 }