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