1 /*
   2  * Copyright (c) 1999, 2016, 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/ciMethodType.hpp"
  27 #include "ci/ciSignature.hpp"
  28 #include "ci/ciUtilities.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/oop.inline.hpp"
  32 #include "runtime/signature.hpp"
  33 
  34 // ciSignature
  35 //
  36 // This class represents the signature of a method.
  37 
  38 // ------------------------------------------------------------------
  39 // ciSignature::ciSignature
  40 ciSignature::ciSignature(ciKlass* accessing_klass, const constantPoolHandle& cpool, ciSymbol* symbol) {
  41   ASSERT_IN_VM;
  42   EXCEPTION_CONTEXT;
  43   _accessing_klass = accessing_klass;
  44   _symbol = symbol;
  45 
  46   ciEnv* env = CURRENT_ENV;
  47   Arena* arena = env->arena();
  48   _types = new (arena) GrowableArray<ciType*>(arena, 8, 0, NULL);
  49 
  50   int size = 0;
  51   int count = 0;
  52   ResourceMark rm(THREAD);
  53   Symbol* sh = symbol->get_symbol();
  54   SignatureStream ss(sh);
  55   for (; ; ss.next()) {
  56     // Process one element of the signature
  57     ciType* type;
  58     if (!ss.is_object()) {
  59       type = ciType::make(ss.type());
  60     } else {
  61       Symbol* name = ss.as_symbol(THREAD);
  62       if (HAS_PENDING_EXCEPTION) {
  63         type = ss.is_array() ? (ciType*)ciEnv::unloaded_ciobjarrayklass()
  64           : (ciType*)ciEnv::unloaded_ciinstance_klass();
  65         env->record_out_of_memory_failure();
  66         CLEAR_PENDING_EXCEPTION;
  67       } else {
  68         ciSymbol* klass_name = env->get_symbol(name);
  69         type = env->get_klass_by_name_impl(_accessing_klass, cpool, klass_name, false);
  70       }
  71     }
  72     _types->append(type);
  73     if (ss.at_return_type()) {
  74       // Done processing the return type; do not add it into the count.
  75       break;
  76     }
  77     size += type->size();
  78     count++;
  79   }
  80   _size = size;
  81   _count = count;
  82 }
  83 
  84 // ------------------------------------------------------------------
  85 // ciSignature::ciSignature
  86 ciSignature::ciSignature(ciKlass* accessing_klass, ciSymbol* symbol, ciMethodType* method_type) :
  87   _symbol(symbol),
  88   _accessing_klass(accessing_klass),
  89   _size( method_type->ptype_slot_count()),
  90   _count(method_type->ptype_count())
  91 {
  92   ASSERT_IN_VM;
  93   EXCEPTION_CONTEXT;
  94   Arena* arena = CURRENT_ENV->arena();
  95   _types = new (arena) GrowableArray<ciType*>(arena, _count + 1, 0, NULL);
  96   for (int i = 0; i < _count; i++) {
  97     _types->append(method_type->ptype_at(i));
  98   }
  99   _types->append(method_type->rtype());
 100 }
 101 
 102 // ------------------------------------------------------------------
 103 // ciSignature::return_type
 104 //
 105 // What is the return type of this signature?
 106 ciType* ciSignature::return_type() const {
 107   return _types->at(_count);
 108 }
 109 
 110 // ------------------------------------------------------------------
 111 // ciSignature::type_at
 112 //
 113 // What is the type of the index'th element of this
 114 // signature?
 115 ciType* ciSignature::type_at(int index) const {
 116   assert(index < _count, "out of bounds");
 117   // The first _klasses element holds the return klass.
 118   return _types->at(index);
 119 }
 120 
 121 // ------------------------------------------------------------------
 122 // ciSignature::equals
 123 //
 124 // Compare this signature to another one.  Signatures with different
 125 // accessing classes but with signature-types resolved to the same
 126 // types are defined to be equal.
 127 bool ciSignature::equals(ciSignature* that) {
 128   // Compare signature
 129   if (!this->as_symbol()->equals(that->as_symbol()))  return false;
 130   // Compare all types of the arguments
 131   for (int i = 0; i < _count; i++) {
 132     if (this->type_at(i) != that->type_at(i))         return false;
 133   }
 134   // Compare the return type
 135   if (this->return_type() != that->return_type())     return false;
 136   return true;
 137 }
 138 
 139 // ------------------------------------------------------------------
 140 // ciSignature::print_signature
 141 void ciSignature::print_signature() {
 142   _symbol->print_symbol();
 143 }
 144 
 145 // ------------------------------------------------------------------
 146 // ciSignature::print
 147 void ciSignature::print() {
 148   tty->print("<ciSignature symbol=");
 149   print_signature();
 150  tty->print(" accessing_klass=");
 151   _accessing_klass->print();
 152   tty->print(" address=" INTPTR_FORMAT ">", p2i((address)this));
 153 }