1 /*
  2  * Copyright (c) 1999, 2020, 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   : _symbol(symbol), _accessing_klass(accessing_klass), _types(CURRENT_ENV->arena(), 8, 0, NULL) {
 42   ASSERT_IN_VM;
 43   EXCEPTION_CONTEXT;
 44   assert(accessing_klass != NULL, "need origin of access");
 45 
 46   ciEnv* env = CURRENT_ENV;
 47 
 48   int size = 0;
 49   int count = 0;
 50   ResourceMark rm(THREAD);
 51   Symbol* sh = symbol->get_symbol();
 52   SignatureStream ss(sh);
 53   for (; ; ss.next()) {
 54     // Process one element of the signature
 55     ciType* type;
 56     if (!ss.is_reference()) {
 57       type = ciType::make(ss.type());
 58     } else {
 59       ciSymbol* klass_name = env->get_symbol(ss.as_symbol());
 60       type = env->get_klass_by_name_impl(_accessing_klass, cpool, klass_name, false);
 61     }
 62     if (ss.at_return_type()) {
 63       // don't include return type in size calculation
 64       _return_type = type;
 65       break;
 66     }
 67     _types.append(type);
 68     size += type->size();
 69   }
 70   _size = size;
 71 }
 72 
 73 // ------------------------------------------------------------------
 74 // ciSignature::equals
 75 //
 76 // Compare this signature to another one.  Signatures with different
 77 // accessing classes but with signature-types resolved to the same
 78 // types are defined to be equal.
 79 bool ciSignature::equals(ciSignature* that) {
 80   // Compare signature
 81   if (!this->as_symbol()->equals(that->as_symbol())) {
 82     return false;
 83   }
 84   // Compare all types of the arguments
 85   if (_types.length() != that->_types.length()) {
 86     return false;
 87   }
 88   for (int i = 0; i < _types.length(); i++) {
 89     if (this->type_at(i) != that->type_at(i)) {
 90       return false;
 91     }
 92   }
 93   // Compare the return type
 94   if (this->return_type() != that->return_type()) {
 95     return false;
 96   }
 97   return true;
 98 }
 99 
100 // ------------------------------------------------------------------
101 // ciSignature::print_signature
102 void ciSignature::print_signature() {
103   _symbol->print_symbol();
104 }
105 
106 // ------------------------------------------------------------------
107 // ciSignature::print
108 void ciSignature::print() {
109   tty->print("<ciSignature symbol=");
110   print_signature();
111  tty->print(" accessing_klass=");
112   _accessing_klass->print();
113   tty->print(" address=" INTPTR_FORMAT ">", p2i((address)this));
114 }