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/ciMethodType.hpp"
  27 #include "ci/ciSignature.hpp"
  28 #include "ci/ciUtilities.inline.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();
  62       ciSymbol* klass_name = env->get_symbol(name);
  63       type = env->get_klass_by_name_impl(_accessing_klass, cpool, klass_name, false);
  64     }
  65     _types->append(type);
  66     if (ss.at_return_type()) {
  67       // Done processing the return type; do not add it into the count.
  68       break;
  69     }
  70     size += type->size();
  71     count++;
  72   }
  73   _size = size;
  74   _count = count;
  75 }
  76 
  77 // ------------------------------------------------------------------
  78 // ciSignature::ciSignature
  79 ciSignature::ciSignature(ciKlass* accessing_klass, ciSymbol* symbol, ciMethodType* method_type) :
  80   _symbol(symbol),
  81   _accessing_klass(accessing_klass),
  82   _size( method_type->ptype_slot_count()),
  83   _count(method_type->ptype_count())
  84 {
  85   ASSERT_IN_VM;
  86   EXCEPTION_CONTEXT;
  87   Arena* arena = CURRENT_ENV->arena();
  88   _types = new (arena) GrowableArray<ciType*>(arena, _count + 1, 0, NULL);
  89   for (int i = 0; i < _count; i++) {
  90     _types->append(method_type->ptype_at(i));
  91   }
  92   _types->append(method_type->rtype());
  93 }
  94 
  95 // ------------------------------------------------------------------
  96 // ciSignature::return_type
  97 //
  98 // What is the return type of this signature?
  99 ciType* ciSignature::return_type() const {
 100   return _types->at(_count);
 101 }
 102 
 103 // ------------------------------------------------------------------
 104 // ciSignature::type_at
 105 //
 106 // What is the type of the index'th element of this
 107 // signature?
 108 ciType* ciSignature::type_at(int index) const {
 109   assert(index < _count, "out of bounds");
 110   // The first _klasses element holds the return klass.
 111   return _types->at(index);
 112 }
 113 
 114 // ------------------------------------------------------------------
 115 // ciSignature::equals
 116 //
 117 // Compare this signature to another one.  Signatures with different
 118 // accessing classes but with signature-types resolved to the same
 119 // types are defined to be equal.
 120 bool ciSignature::equals(ciSignature* that) {
 121   // Compare signature
 122   if (!this->as_symbol()->equals(that->as_symbol()))  return false;
 123   // Compare all types of the arguments
 124   for (int i = 0; i < _count; i++) {
 125     if (this->type_at(i) != that->type_at(i))         return false;
 126   }
 127   // Compare the return type
 128   if (this->return_type() != that->return_type())     return false;
 129   return true;
 130 }
 131 
 132 // ------------------------------------------------------------------
 133 // ciSignature::print_signature
 134 void ciSignature::print_signature() {
 135   _symbol->print_symbol();
 136 }
 137 
 138 // ------------------------------------------------------------------
 139 // ciSignature::print
 140 void ciSignature::print() {
 141   tty->print("<ciSignature symbol=");
 142   print_signature();
 143  tty->print(" accessing_klass=");
 144   _accessing_klass->print();
 145   tty->print(" address=" INTPTR_FORMAT ">", p2i((address)this));
 146 }