1 /*
   2  * Copyright (c) 1999, 2009, 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 "incls/_precompiled.incl"
  26 #include "incls/_ciSymbol.cpp.incl"
  27 
  28 // ------------------------------------------------------------------
  29 // ciSymbol::ciSymbol
  30 //
  31 // Preallocated handle variant.  Used with handles from vmSymboHandles.
  32 ciSymbol::ciSymbol(symbolHandle h_s, vmSymbols::SID sid)
  33   : ciObject(h_s), _sid(sid)
  34 {
  35   assert(sid_ok(), "must be in vmSymbols");
  36 }
  37 
  38 // Normal case for non-famous symbols.
  39 ciSymbol::ciSymbol(symbolOop s)
  40   : ciObject(s), _sid(vmSymbols::NO_SID)
  41 {
  42   assert(sid_ok(), "must not be in vmSymbols");
  43 }
  44 
  45 // ciSymbol
  46 //
  47 // This class represents a symbolOop 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   VM_QUICK_ENTRY_MARK;
  56   symbolOop s = get_symbolOop();
  57   return s->as_utf8();
  58 }
  59 
  60 // ------------------------------------------------------------------
  61 // ciSymbol::base
  62 jbyte* ciSymbol::base() {
  63   GUARDED_VM_ENTRY(return get_symbolOop()->base();)
  64 }
  65 
  66 // ------------------------------------------------------------------
  67 // ciSymbol::byte_at
  68 int ciSymbol::byte_at(int i) {
  69   GUARDED_VM_ENTRY(return get_symbolOop()->byte_at(i);)
  70 }
  71 
  72 // ------------------------------------------------------------------
  73 // ciSymbol::starts_with
  74 //
  75 // Tests if the symbol starts with the given prefix.
  76 bool ciSymbol::starts_with(const char* prefix, int len) const {
  77   GUARDED_VM_ENTRY(return get_symbolOop()->starts_with(prefix, len);)
  78 }
  79 
  80 // ------------------------------------------------------------------
  81 // ciSymbol::index_of
  82 //
  83 // Determines where the symbol contains the given substring.
  84 int ciSymbol::index_of_at(int i, const char* str, int len) const {
  85   GUARDED_VM_ENTRY(return get_symbolOop()->index_of_at(i, str, len);)
  86 }
  87 
  88 // ------------------------------------------------------------------
  89 // ciSymbol::utf8_length
  90 int ciSymbol::utf8_length() {
  91   GUARDED_VM_ENTRY(return get_symbolOop()->utf8_length();)
  92 }
  93 
  94 // ------------------------------------------------------------------
  95 // ciSymbol::print_impl
  96 //
  97 // Implementation of the print method
  98 void ciSymbol::print_impl(outputStream* st) {
  99   st->print(" value=");
 100   print_symbol_on(st);
 101 }
 102 
 103 // ------------------------------------------------------------------
 104 // ciSymbol::print_symbol_on
 105 //
 106 // Print the value of this symbol on an outputStream
 107 void ciSymbol::print_symbol_on(outputStream *st) {
 108   GUARDED_VM_ENTRY(get_symbolOop()->print_symbol_on(st);)
 109 }
 110 
 111 // ------------------------------------------------------------------
 112 // ciSymbol::make_impl
 113 //
 114 // Make a ciSymbol from a C string (implementation).
 115 ciSymbol* ciSymbol::make_impl(const char* s) {
 116   EXCEPTION_CONTEXT;
 117   // For some reason, oopFactory::new_symbol doesn't declare its
 118   // char* argument as const.
 119   symbolOop sym = oopFactory::new_symbol((char*)s, (int)strlen(s), THREAD);
 120   if (HAS_PENDING_EXCEPTION) {
 121     CLEAR_PENDING_EXCEPTION;
 122     CURRENT_THREAD_ENV->record_out_of_memory_failure();
 123     return ciEnv::_unloaded_cisymbol;
 124   }
 125   return CURRENT_THREAD_ENV->get_object(sym)->as_symbol();
 126 }
 127 
 128 // ------------------------------------------------------------------
 129 // ciSymbol::make
 130 //
 131 // Make a ciSymbol from a C string.
 132 ciSymbol* ciSymbol::make(const char* s) {
 133   GUARDED_VM_ENTRY(return make_impl(s);)
 134 }