src/share/vm/classfile/genericSignatures.cpp

Tue, 02 Apr 2013 11:28:33 +0200

author
mgerdin
date
Tue, 02 Apr 2013 11:28:33 +0200
changeset 4850
ede380e13960
parent 4245
4735d2c84362
child 4960
41ed397cc0cd
permissions
-rw-r--r--

8009763: Add WB test for String.intern()
Summary: Add convenience method in StringTable, add WhiteBox method and simple sanity test
Reviewed-by: mgerdin, zgu
Contributed-by: leonid.mesnik@oracle.com

     1 /*
     2  * Copyright (c) 2012, 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  */
    25 #include "precompiled.hpp"
    27 #include "classfile/genericSignatures.hpp"
    28 #include "classfile/symbolTable.hpp"
    29 #include "classfile/systemDictionary.hpp"
    30 #include "memory/resourceArea.hpp"
    32 namespace generic {
    34 // Helper class for parsing the generic signature Symbol in klass and methods
    35 class DescriptorStream : public ResourceObj {
    36  private:
    37   Symbol* _symbol;
    38   int _offset;
    39   int _mark;
    40   const char* _parse_error;
    42   void set_parse_error(const char* error) {
    43     assert(error != NULL, "Can't set NULL error string");
    44     _parse_error = error;
    45   }
    47  public:
    48   DescriptorStream(Symbol* sym)
    49       : _symbol(sym), _offset(0), _mark(-1), _parse_error(NULL) {}
    51   const char* parse_error() const {
    52     return _parse_error;
    53   }
    55   bool at_end() { return _offset >= _symbol->utf8_length(); }
    57   char peek() {
    58     if (at_end()) {
    59       set_parse_error("Peeking past end of signature");
    60       return '\0';
    61     } else {
    62       return _symbol->byte_at(_offset);
    63     }
    64   }
    66   char read() {
    67     if (at_end()) {
    68       set_parse_error("Reading past end of signature");
    69       return '\0';
    70     } else {
    71       return _symbol->byte_at(_offset++);
    72     }
    73   }
    75   void read(char expected) {
    76     char c = read();
    77     assert_char(c, expected, 0);
    78   }
    80   void assert_char(char c, char expected, int pos = -1) {
    81     if (c != expected) {
    82       const char* fmt = "Parse error at %d: expected %c but got %c";
    83       size_t len = strlen(fmt) + 5;
    84       char* buffer = NEW_RESOURCE_ARRAY(char, len);
    85       jio_snprintf(buffer, len, fmt, _offset + pos, expected, c);
    86       set_parse_error(buffer);
    87     }
    88   }
    90   void push(char c) {
    91     assert(c == _symbol->byte_at(_offset - 1), "Pushing back wrong value");
    92     --_offset;
    93   }
    95   void expect_end() {
    96     if (!at_end()) {
    97       set_parse_error("Unexpected data trailing signature");
    98     }
    99   }
   101   bool has_mark() { return _mark != -1; }
   103   void set_mark() {
   104     _mark = _offset;
   105   }
   107   Identifier* identifier_from_mark() {
   108     assert(has_mark(), "Mark should be set");
   109     if (!has_mark()) {
   110       set_parse_error("Expected mark to be set");
   111       return NULL;
   112     } else {
   113       Identifier* id = new Identifier(_symbol, _mark, _offset - 1);
   114       _mark = -1;
   115       return id;
   116     }
   117   }
   118 };
   121 #define CHECK_FOR_PARSE_ERROR()         \
   122   if (STREAM->parse_error() != NULL) {   \
   123     if (VerifyGenericSignatures) {      \
   124       fatal(STREAM->parse_error());      \
   125     }                                   \
   126     return NULL;                        \
   127   } 0
   129 #define READ() STREAM->read(); CHECK_FOR_PARSE_ERROR()
   130 #define PEEK() STREAM->peek(); CHECK_FOR_PARSE_ERROR()
   131 #define PUSH(c) STREAM->push(c)
   132 #define EXPECT(c) STREAM->read(c); CHECK_FOR_PARSE_ERROR()
   133 #define EXPECTED(c, ch) STREAM->assert_char(c, ch); CHECK_FOR_PARSE_ERROR()
   134 #define EXPECT_END() STREAM->expect_end(); CHECK_FOR_PARSE_ERROR()
   136 #define CHECK_STREAM STREAM); CHECK_FOR_PARSE_ERROR(); (0
   138 #ifndef PRODUCT
   139 void Identifier::print_on(outputStream* str) const {
   140   for (int i = _begin; i < _end; ++i) {
   141     str->print("%c", (char)_sym->byte_at(i));
   142   }
   143 }
   144 #endif // ndef PRODUCT
   146 bool Identifier::equals(Identifier* other) {
   147   if (_sym == other->_sym && _begin == other->_begin && _end == other->_end) {
   148     return true;
   149   } else if (_end - _begin != other->_end - other->_begin) {
   150     return false;
   151   } else {
   152     size_t len = _end - _begin;
   153     char* addr = ((char*)_sym->bytes()) + _begin;
   154     char* oaddr = ((char*)other->_sym->bytes()) + other->_begin;
   155     return strncmp(addr, oaddr, len) == 0;
   156   }
   157 }
   159 bool Identifier::equals(Symbol* sym) {
   160   Identifier id(sym, 0, sym->utf8_length());
   161   return equals(&id);
   162 }
   164 /**
   165  * A formal type parameter may be found in the the enclosing class, but it could
   166  * also come from an enclosing method or outer class, in the case of inner-outer
   167  * classes or anonymous classes.  For example:
   168  *
   169  * class Outer<T,V> {
   170  *   class Inner<W> {
   171  *     void m(T t, V v, W w);
   172  *   }
   173  * }
   174  *
   175  * In this case, the type variables in m()'s signature are not all found in the
   176  * immediate enclosing class (Inner).  class Inner has only type parameter W,
   177  * but it's outer_class field will reference Outer's descriptor which contains
   178  * T & V (no outer_method in this case).
   179  *
   180  * If you have an anonymous class, it has both an enclosing method *and* an
   181  * enclosing class where type parameters can be declared:
   182  *
   183  * class MOuter<T> {
   184  *   <V> void bar(V v) {
   185  *     Runnable r = new Runnable() {
   186  *       public void run() {}
   187  *       public void foo(T t, V v) { ... }
   188  *     };
   189  *   }
   190  * }
   191  *
   192  * In this case, foo will be a member of some class, Runnable$1, which has no
   193  * formal parameters itself, but has an outer_method (bar()) which provides
   194  * type parameter V, and an outer class MOuter with type parameter T.
   195  *
   196  * It is also possible that the outer class is itself an inner class to some
   197  * other class (or an anonymous class with an enclosing method), so we need to
   198  * follow the outer_class/outer_method chain to it's end when looking for a
   199  * type parameter.
   200  */
   201 TypeParameter* Descriptor::find_type_parameter(Identifier* id, int* depth) {
   203   int current_depth = 0;
   205   MethodDescriptor* outer_method = as_method_signature();
   206   ClassDescriptor* outer_class = as_class_signature();
   208   if (outer_class == NULL) { // 'this' is a method signature; use the holder
   209     outer_class = outer_method->outer_class();
   210   }
   212   while (outer_method != NULL || outer_class != NULL) {
   213     if (outer_method != NULL) {
   214       for (int i = 0; i < outer_method->type_parameters().length(); ++i) {
   215         TypeParameter* p = outer_method->type_parameters().at(i);
   216         if (p->identifier()->equals(id)) {
   217           *depth = -1; // indicates this this is a method parameter
   218           return p;
   219         }
   220       }
   221     }
   222     if (outer_class != NULL) {
   223       for (int i = 0; i < outer_class->type_parameters().length(); ++i) {
   224         TypeParameter* p = outer_class->type_parameters().at(i);
   225         if (p->identifier()->equals(id)) {
   226           *depth = current_depth;
   227           return p;
   228         }
   229       }
   230       outer_method = outer_class->outer_method();
   231       outer_class = outer_class->outer_class();
   232       ++current_depth;
   233     }
   234   }
   236   if (VerifyGenericSignatures) {
   237     fatal("Could not resolve identifier");
   238   }
   240   return NULL;
   241 }
   243 ClassDescriptor* ClassDescriptor::parse_generic_signature(Klass* klass, TRAPS) {
   244   return parse_generic_signature(klass, NULL, CHECK_NULL);
   245 }
   247 ClassDescriptor* ClassDescriptor::parse_generic_signature(
   248       Klass* klass, Symbol* original_name, TRAPS) {
   250   InstanceKlass* ik = InstanceKlass::cast(klass);
   251   Symbol* sym = ik->generic_signature();
   253   ClassDescriptor* spec;
   255   if (sym == NULL || (spec = ClassDescriptor::parse_generic_signature(sym)) == NULL) {
   256     spec = ClassDescriptor::placeholder(ik);
   257   }
   259   u2 outer_index = get_outer_class_index(ik, CHECK_NULL);
   260   if (outer_index != 0) {
   261     if (original_name == NULL) {
   262       original_name = ik->name();
   263     }
   264     Handle class_loader = Handle(THREAD, ik->class_loader());
   265     Handle protection_domain = Handle(THREAD, ik->protection_domain());
   267     Symbol* outer_name = ik->constants()->klass_name_at(outer_index);
   268     Klass* outer = SystemDictionary::find(
   269         outer_name, class_loader, protection_domain, CHECK_NULL);
   270     if (outer == NULL && !THREAD->is_Compiler_thread()) {
   271       outer = SystemDictionary::resolve_super_or_fail(original_name,
   272           outer_name, class_loader, protection_domain, false, CHECK_NULL);
   273     }
   275     InstanceKlass* outer_ik;
   276     ClassDescriptor* outer_spec = NULL;
   277     if (outer == NULL) {
   278       outer_spec = ClassDescriptor::placeholder(ik);
   279       assert(false, "Outer class not loaded and not loadable from here");
   280     } else {
   281       outer_ik = InstanceKlass::cast(outer);
   282       outer_spec = parse_generic_signature(outer, original_name, CHECK_NULL);
   283     }
   284     spec->set_outer_class(outer_spec);
   286     u2 encl_method_idx = ik->enclosing_method_method_index();
   287     if (encl_method_idx != 0 && outer_ik != NULL) {
   288       ConstantPool* cp = ik->constants();
   289       u2 name_index = cp->name_ref_index_at(encl_method_idx);
   290       u2 sig_index = cp->signature_ref_index_at(encl_method_idx);
   291       Symbol* name = cp->symbol_at(name_index);
   292       Symbol* sig = cp->symbol_at(sig_index);
   293       Method* m = outer_ik->find_method(name, sig);
   294       if (m != NULL) {
   295         Symbol* gsig = m->generic_signature();
   296         if (gsig != NULL) {
   297           MethodDescriptor* gms = MethodDescriptor::parse_generic_signature(gsig, outer_spec);
   298           spec->set_outer_method(gms);
   299         }
   300       } else if (VerifyGenericSignatures) {
   301         ResourceMark rm;
   302         stringStream ss;
   303         ss.print("Could not find method %s %s in class %s",
   304           name->as_C_string(), sig->as_C_string(), outer_name->as_C_string());
   305         fatal(ss.as_string());
   306       }
   307     }
   308   }
   310   spec->bind_variables_to_parameters();
   311   return spec;
   312 }
   314 ClassDescriptor* ClassDescriptor::placeholder(InstanceKlass* klass) {
   315   GrowableArray<TypeParameter*> formals;
   316   GrowableArray<ClassType*> interfaces;
   317   ClassType* super_type = NULL;
   319   Klass* super_klass = klass->super();
   320   if (super_klass != NULL) {
   321     InstanceKlass* super = InstanceKlass::cast(super_klass);
   322     super_type = ClassType::from_symbol(super->name());
   323   }
   325   for (int i = 0; i < klass->local_interfaces()->length(); ++i) {
   326     InstanceKlass* iface = InstanceKlass::cast(klass->local_interfaces()->at(i));
   327     interfaces.append(ClassType::from_symbol(iface->name()));
   328   }
   329   return new ClassDescriptor(formals, super_type, interfaces);
   330 }
   332 ClassDescriptor* ClassDescriptor::parse_generic_signature(Symbol* sym) {
   334   DescriptorStream ds(sym);
   335   DescriptorStream* STREAM = &ds;
   337   GrowableArray<TypeParameter*> parameters(8);
   338   char c = READ();
   339   if (c == '<') {
   340     c = READ();
   341     while (c != '>') {
   342       PUSH(c);
   343       TypeParameter* ftp = TypeParameter::parse_generic_signature(CHECK_STREAM);
   344       parameters.append(ftp);
   345       c = READ();
   346     }
   347   } else {
   348     PUSH(c);
   349   }
   351   EXPECT('L');
   352   ClassType* super = ClassType::parse_generic_signature(CHECK_STREAM);
   354   GrowableArray<ClassType*> signatures(2);
   355   while (!STREAM->at_end()) {
   356     EXPECT('L');
   357     ClassType* iface = ClassType::parse_generic_signature(CHECK_STREAM);
   358     signatures.append(iface);
   359   }
   361   EXPECT_END();
   363   return new ClassDescriptor(parameters, super, signatures);
   364 }
   366 #ifndef PRODUCT
   367 void ClassDescriptor::print_on(outputStream* str) const {
   368   str->indent().print_cr("ClassDescriptor {");
   369   {
   370     streamIndentor si(str);
   371     if (_type_parameters.length() > 0) {
   372       str->indent().print_cr("Formals {");
   373       {
   374         streamIndentor si(str);
   375         for (int i = 0; i < _type_parameters.length(); ++i) {
   376           _type_parameters.at(i)->print_on(str);
   377         }
   378       }
   379       str->indent().print_cr("}");
   380     }
   381     if (_super != NULL) {
   382       str->indent().print_cr("Superclass: ");
   383       {
   384         streamIndentor si(str);
   385         _super->print_on(str);
   386       }
   387     }
   388     if (_interfaces.length() > 0) {
   389       str->indent().print_cr("SuperInterfaces: {");
   390       {
   391         streamIndentor si(str);
   392         for (int i = 0; i < _interfaces.length(); ++i) {
   393           _interfaces.at(i)->print_on(str);
   394         }
   395       }
   396       str->indent().print_cr("}");
   397     }
   398     if (_outer_method != NULL) {
   399       str->indent().print_cr("Outer Method: {");
   400       {
   401         streamIndentor si(str);
   402         _outer_method->print_on(str);
   403       }
   404       str->indent().print_cr("}");
   405     }
   406     if (_outer_class != NULL) {
   407       str->indent().print_cr("Outer Class: {");
   408       {
   409         streamIndentor si(str);
   410         _outer_class->print_on(str);
   411       }
   412       str->indent().print_cr("}");
   413     }
   414   }
   415   str->indent().print_cr("}");
   416 }
   417 #endif // ndef PRODUCT
   419 ClassType* ClassDescriptor::interface_desc(Symbol* sym) {
   420   for (int i = 0; i < _interfaces.length(); ++i) {
   421     if (_interfaces.at(i)->identifier()->equals(sym)) {
   422       return _interfaces.at(i);
   423     }
   424   }
   425   if (VerifyGenericSignatures) {
   426     fatal("Did not find expected interface");
   427   }
   428   return NULL;
   429 }
   431 void ClassDescriptor::bind_variables_to_parameters() {
   432   if (_outer_class != NULL) {
   433     _outer_class->bind_variables_to_parameters();
   434   }
   435   if (_outer_method != NULL) {
   436     _outer_method->bind_variables_to_parameters();
   437   }
   438   for (int i = 0; i < _type_parameters.length(); ++i) {
   439     _type_parameters.at(i)->bind_variables_to_parameters(this, i);
   440   }
   441   if (_super != NULL) {
   442     _super->bind_variables_to_parameters(this);
   443   }
   444   for (int i = 0; i < _interfaces.length(); ++i) {
   445     _interfaces.at(i)->bind_variables_to_parameters(this);
   446   }
   447 }
   449 ClassDescriptor* ClassDescriptor::canonicalize(Context* ctx) {
   451   GrowableArray<TypeParameter*> type_params(_type_parameters.length());
   452   for (int i = 0; i < _type_parameters.length(); ++i) {
   453     type_params.append(_type_parameters.at(i)->canonicalize(ctx, 0));
   454   }
   456   ClassDescriptor* outer = _outer_class == NULL ? NULL :
   457       _outer_class->canonicalize(ctx);
   459   ClassType* super = _super == NULL ? NULL : _super->canonicalize(ctx, 0);
   461   GrowableArray<ClassType*> interfaces(_interfaces.length());
   462   for (int i = 0; i < _interfaces.length(); ++i) {
   463     interfaces.append(_interfaces.at(i)->canonicalize(ctx, 0));
   464   }
   466   MethodDescriptor* md = _outer_method == NULL ? NULL :
   467       _outer_method->canonicalize(ctx);
   469   return new ClassDescriptor(type_params, super, interfaces, outer, md);
   470 }
   472 u2 ClassDescriptor::get_outer_class_index(InstanceKlass* klass, TRAPS) {
   473   int inner_index = InstanceKlass::inner_class_inner_class_info_offset;
   474   int outer_index = InstanceKlass::inner_class_outer_class_info_offset;
   475   int name_offset = InstanceKlass::inner_class_inner_name_offset;
   476   int next_offset = InstanceKlass::inner_class_next_offset;
   478   if (klass->inner_classes() == NULL || klass->inner_classes()->length() == 0) {
   479     // No inner class info => no declaring class
   480     return 0;
   481   }
   483   Array<u2>* i_icls = klass->inner_classes();
   484   ConstantPool* i_cp = klass->constants();
   485   int i_length = i_icls->length();
   487   // Find inner_klass attribute
   488   for (int i = 0; i + next_offset < i_length; i += next_offset) {
   489     u2 ioff = i_icls->at(i + inner_index);
   490     u2 ooff = i_icls->at(i + outer_index);
   491     u2 noff = i_icls->at(i + name_offset);
   492     if (ioff != 0) {
   493       // Check to see if the name matches the class we're looking for
   494       // before attempting to find the class.
   495       if (i_cp->klass_name_at_matches(klass, ioff) && ooff != 0) {
   496         return ooff;
   497       }
   498     }
   499   }
   501   // It may be anonymous; try for that.
   502   u2 encl_method_class_idx = klass->enclosing_method_class_index();
   503   if (encl_method_class_idx != 0) {
   504     return encl_method_class_idx;
   505   }
   507   return 0;
   508 }
   510 MethodDescriptor* MethodDescriptor::parse_generic_signature(Method* m, ClassDescriptor* outer) {
   511   Symbol* generic_sig = m->generic_signature();
   512   MethodDescriptor* md = NULL;
   513   if (generic_sig == NULL || (md = parse_generic_signature(generic_sig, outer)) == NULL) {
   514     md = parse_generic_signature(m->signature(), outer);
   515   }
   516   assert(md != NULL, "Could not parse method signature");
   517   md->bind_variables_to_parameters();
   518   return md;
   519 }
   521 MethodDescriptor* MethodDescriptor::parse_generic_signature(Symbol* sym, ClassDescriptor* outer) {
   523   DescriptorStream ds(sym);
   524   DescriptorStream* STREAM = &ds;
   526   GrowableArray<TypeParameter*> params(8);
   527   char c = READ();
   528   if (c == '<') {
   529     c = READ();
   530     while (c != '>') {
   531       PUSH(c);
   532       TypeParameter* ftp = TypeParameter::parse_generic_signature(CHECK_STREAM);
   533       params.append(ftp);
   534       c = READ();
   535     }
   536   } else {
   537     PUSH(c);
   538   }
   540   EXPECT('(');
   542   GrowableArray<Type*> parameters(8);
   543   c = READ();
   544   while (c != ')') {
   545     PUSH(c);
   546     Type* arg = Type::parse_generic_signature(CHECK_STREAM);
   547     parameters.append(arg);
   548     c = READ();
   549   }
   551   Type* rt = Type::parse_generic_signature(CHECK_STREAM);
   553   GrowableArray<Type*> throws;
   554   while (!STREAM->at_end()) {
   555     EXPECT('^');
   556     Type* spec = Type::parse_generic_signature(CHECK_STREAM);
   557     throws.append(spec);
   558   }
   560   return new MethodDescriptor(params, outer, parameters, rt, throws);
   561 }
   563 void MethodDescriptor::bind_variables_to_parameters() {
   564   for (int i = 0; i < _type_parameters.length(); ++i) {
   565     _type_parameters.at(i)->bind_variables_to_parameters(this, i);
   566   }
   567   for (int i = 0; i < _parameters.length(); ++i) {
   568     _parameters.at(i)->bind_variables_to_parameters(this);
   569   }
   570   _return_type->bind_variables_to_parameters(this);
   571   for (int i = 0; i < _throws.length(); ++i) {
   572     _throws.at(i)->bind_variables_to_parameters(this);
   573   }
   574 }
   576 bool MethodDescriptor::covariant_match(MethodDescriptor* other, Context* ctx) {
   578   if (_parameters.length() == other->_parameters.length()) {
   579     for (int i = 0; i < _parameters.length(); ++i) {
   580       if (!_parameters.at(i)->covariant_match(other->_parameters.at(i), ctx)) {
   581         return false;
   582       }
   583     }
   585     if (_return_type->as_primitive() != NULL) {
   586       return _return_type->covariant_match(other->_return_type, ctx);
   587     } else {
   588       // return type is a reference
   589       return other->_return_type->as_class() != NULL ||
   590              other->_return_type->as_variable() != NULL ||
   591              other->_return_type->as_array() != NULL;
   592     }
   593   } else {
   594     return false;
   595   }
   596 }
   598 MethodDescriptor* MethodDescriptor::canonicalize(Context* ctx) {
   600   GrowableArray<TypeParameter*> type_params(_type_parameters.length());
   601   for (int i = 0; i < _type_parameters.length(); ++i) {
   602     type_params.append(_type_parameters.at(i)->canonicalize(ctx, 0));
   603   }
   605   ClassDescriptor* outer = _outer_class == NULL ? NULL :
   606       _outer_class->canonicalize(ctx);
   608   GrowableArray<Type*> params(_parameters.length());
   609   for (int i = 0; i < _parameters.length(); ++i) {
   610     params.append(_parameters.at(i)->canonicalize(ctx, 0));
   611   }
   613   Type* rt = _return_type->canonicalize(ctx, 0);
   615   GrowableArray<Type*> throws(_throws.length());
   616   for (int i = 0; i < _throws.length(); ++i) {
   617     throws.append(_throws.at(i)->canonicalize(ctx, 0));
   618   }
   620   return new MethodDescriptor(type_params, outer, params, rt, throws);
   621 }
   623 #ifndef PRODUCT
   624 TempNewSymbol MethodDescriptor::reify_signature(Context* ctx, TRAPS) {
   625   stringStream ss(256);
   627   ss.print("(");
   628   for (int i = 0; i < _parameters.length(); ++i) {
   629     _parameters.at(i)->reify_signature(&ss, ctx);
   630   }
   631   ss.print(")");
   632   _return_type->reify_signature(&ss, ctx);
   633   return SymbolTable::new_symbol(ss.base(), (int)ss.size(), THREAD);
   634 }
   636 void MethodDescriptor::print_on(outputStream* str) const {
   637   str->indent().print_cr("MethodDescriptor {");
   638   {
   639     streamIndentor si(str);
   640     if (_type_parameters.length() > 0) {
   641       str->indent().print_cr("Formals: {");
   642       {
   643         streamIndentor si(str);
   644         for (int i = 0; i < _type_parameters.length(); ++i) {
   645           _type_parameters.at(i)->print_on(str);
   646         }
   647       }
   648       str->indent().print_cr("}");
   649     }
   650     str->indent().print_cr("Parameters: {");
   651     {
   652       streamIndentor si(str);
   653       for (int i = 0; i < _parameters.length(); ++i) {
   654         _parameters.at(i)->print_on(str);
   655       }
   656     }
   657     str->indent().print_cr("}");
   658     str->indent().print_cr("Return Type: ");
   659     {
   660       streamIndentor si(str);
   661       _return_type->print_on(str);
   662     }
   664     if (_throws.length() > 0) {
   665       str->indent().print_cr("Throws: {");
   666       {
   667         streamIndentor si(str);
   668         for (int i = 0; i < _throws.length(); ++i) {
   669           _throws.at(i)->print_on(str);
   670         }
   671       }
   672       str->indent().print_cr("}");
   673     }
   674   }
   675   str->indent().print_cr("}");
   676 }
   677 #endif // ndef PRODUCT
   679 TypeParameter* TypeParameter::parse_generic_signature(DescriptorStream* STREAM) {
   680   STREAM->set_mark();
   681   char c = READ();
   682   while (c != ':') {
   683     c = READ();
   684   }
   686   Identifier* id = STREAM->identifier_from_mark();
   688   ClassType* class_bound = NULL;
   689   GrowableArray<ClassType*> interface_bounds(8);
   691   c = READ();
   692   if (c != '>') {
   693     if (c != ':') {
   694       EXPECTED(c, 'L');
   695       class_bound = ClassType::parse_generic_signature(CHECK_STREAM);
   696       c = READ();
   697     }
   699     while (c == ':') {
   700       EXPECT('L');
   701       ClassType* fts = ClassType::parse_generic_signature(CHECK_STREAM);
   702       interface_bounds.append(fts);
   703       c = READ();
   704     }
   705   }
   706   PUSH(c);
   708   return new TypeParameter(id, class_bound, interface_bounds);
   709 }
   711 void TypeParameter::bind_variables_to_parameters(Descriptor* sig, int position) {
   712   if (_class_bound != NULL) {
   713     _class_bound->bind_variables_to_parameters(sig);
   714   }
   715   for (int i = 0; i < _interface_bounds.length(); ++i) {
   716     _interface_bounds.at(i)->bind_variables_to_parameters(sig);
   717   }
   718   _position = position;
   719 }
   721 Type* TypeParameter::resolve(
   722     Context* ctx, int inner_depth, int ctx_depth) {
   724   if (inner_depth == -1) {
   725     // This indicates that the parameter is a method type parameter, which
   726     // isn't resolveable using the class hierarchy context
   727     return bound();
   728   }
   730   ClassType* provider = ctx->at_depth(ctx_depth);
   731   if (provider != NULL) {
   732     for (int i = 0; i < inner_depth && provider != NULL; ++i) {
   733       provider = provider->outer_class();
   734     }
   735     if (provider != NULL) {
   736       TypeArgument* arg = provider->type_argument_at(_position);
   737       if (arg != NULL) {
   738         Type* value = arg->lower_bound();
   739         return value->canonicalize(ctx, ctx_depth + 1);
   740       }
   741     }
   742   }
   744   return bound();
   745 }
   747 TypeParameter* TypeParameter::canonicalize(Context* ctx, int ctx_depth) {
   748   ClassType* bound = _class_bound == NULL ? NULL :
   749      _class_bound->canonicalize(ctx, ctx_depth);
   751   GrowableArray<ClassType*> ifaces(_interface_bounds.length());
   752   for (int i = 0; i < _interface_bounds.length(); ++i) {
   753     ifaces.append(_interface_bounds.at(i)->canonicalize(ctx, ctx_depth));
   754   }
   756   TypeParameter* ret = new TypeParameter(_identifier, bound, ifaces);
   757   ret->_position = _position;
   758   return ret;
   759 }
   761 ClassType* TypeParameter::bound() {
   762   if (_class_bound != NULL) {
   763     return _class_bound;
   764   }
   766   if (_interface_bounds.length() == 1) {
   767     return _interface_bounds.at(0);
   768   }
   770   return ClassType::java_lang_Object(); // TODO: investigate this case
   771 }
   773 #ifndef PRODUCT
   774 void TypeParameter::print_on(outputStream* str) const {
   775   str->indent().print_cr("Formal: {");
   776   {
   777     streamIndentor si(str);
   779     str->indent().print("Identifier: ");
   780     _identifier->print_on(str);
   781     str->print_cr("");
   782     if (_class_bound != NULL) {
   783       str->indent().print_cr("Class Bound: ");
   784       streamIndentor si(str);
   785       _class_bound->print_on(str);
   786     }
   787     if (_interface_bounds.length() > 0) {
   788       str->indent().print_cr("Interface Bounds: {");
   789       {
   790         streamIndentor si(str);
   791         for (int i = 0; i < _interface_bounds.length(); ++i) {
   792           _interface_bounds.at(i)->print_on(str);
   793         }
   794       }
   795       str->indent().print_cr("}");
   796     }
   797     str->indent().print_cr("Ordinal Position: %d", _position);
   798   }
   799   str->indent().print_cr("}");
   800 }
   801 #endif // ndef PRODUCT
   803 Type* Type::parse_generic_signature(DescriptorStream* STREAM) {
   804   char c = READ();
   805   switch (c) {
   806     case 'L':
   807       return ClassType::parse_generic_signature(CHECK_STREAM);
   808     case 'T':
   809       return TypeVariable::parse_generic_signature(CHECK_STREAM);
   810     case '[':
   811       return ArrayType::parse_generic_signature(CHECK_STREAM);
   812     default:
   813       return new PrimitiveType(c);
   814   }
   815 }
   817 Identifier* ClassType::parse_generic_signature_simple(GrowableArray<TypeArgument*>* args,
   818     bool* has_inner, DescriptorStream* STREAM) {
   819   STREAM->set_mark();
   821   char c = READ();
   822   while (c != ';' && c != '.' && c != '<') { c = READ(); }
   823   Identifier* id = STREAM->identifier_from_mark();
   825   if (c == '<') {
   826     c = READ();
   827     while (c != '>') {
   828       PUSH(c);
   829       TypeArgument* arg = TypeArgument::parse_generic_signature(CHECK_STREAM);
   830       args->append(arg);
   831       c = READ();
   832     }
   833     c = READ();
   834   }
   836   *has_inner = (c == '.');
   837   if (!(*has_inner)) {
   838     EXPECTED(c, ';');
   839   }
   841   return id;
   842 }
   844 ClassType* ClassType::parse_generic_signature(DescriptorStream* STREAM) {
   845   return parse_generic_signature(NULL, CHECK_STREAM);
   846 }
   848 ClassType* ClassType::parse_generic_signature(ClassType* outer, DescriptorStream* STREAM) {
   849   GrowableArray<TypeArgument*> args;
   850   ClassType* gct = NULL;
   851   bool has_inner = false;
   853   Identifier* id = parse_generic_signature_simple(&args, &has_inner, STREAM);
   854   if (id != NULL) {
   855     gct = new ClassType(id, args, outer);
   857     if (has_inner) {
   858       gct = parse_generic_signature(gct, CHECK_STREAM);
   859     }
   860   }
   861   return gct;
   862 }
   864 ClassType* ClassType::from_symbol(Symbol* sym) {
   865   assert(sym != NULL, "Must not be null");
   866   GrowableArray<TypeArgument*> args;
   867   Identifier* id = new Identifier(sym, 0, sym->utf8_length());
   868   return new ClassType(id, args, NULL);
   869 }
   871 ClassType* ClassType::java_lang_Object() {
   872   return from_symbol(vmSymbols::java_lang_Object());
   873 }
   875 void ClassType::bind_variables_to_parameters(Descriptor* sig) {
   876   for (int i = 0; i < _type_arguments.length(); ++i) {
   877     _type_arguments.at(i)->bind_variables_to_parameters(sig);
   878   }
   879   if (_outer_class != NULL) {
   880     _outer_class->bind_variables_to_parameters(sig);
   881   }
   882 }
   884 TypeArgument* ClassType::type_argument_at(int i) {
   885   if (i >= 0 && i < _type_arguments.length()) {
   886     return _type_arguments.at(i);
   887   } else {
   888     return NULL;
   889   }
   890 }
   892 #ifndef PRODUCT
   893 void ClassType::reify_signature(stringStream* ss, Context* ctx) {
   894   ss->print("L");
   895   _identifier->print_on(ss);
   896   ss->print(";");
   897 }
   899 void ClassType::print_on(outputStream* str) const {
   900   str->indent().print_cr("Class {");
   901   {
   902     streamIndentor si(str);
   903     str->indent().print("Name: ");
   904     _identifier->print_on(str);
   905     str->print_cr("");
   906     if (_type_arguments.length() != 0) {
   907       str->indent().print_cr("Type Arguments: {");
   908       {
   909         streamIndentor si(str);
   910         for (int j = 0; j < _type_arguments.length(); ++j) {
   911           _type_arguments.at(j)->print_on(str);
   912         }
   913       }
   914       str->indent().print_cr("}");
   915     }
   916     if (_outer_class != NULL) {
   917       str->indent().print_cr("Outer Class: ");
   918       streamIndentor sir(str);
   919       _outer_class->print_on(str);
   920     }
   921   }
   922   str->indent().print_cr("}");
   923 }
   924 #endif // ndef PRODUCT
   926 bool ClassType::covariant_match(Type* other, Context* ctx) {
   928   if (other == this) {
   929     return true;
   930   }
   932   TypeVariable* variable = other->as_variable();
   933   if (variable != NULL) {
   934     other = variable->resolve(ctx, 0);
   935   }
   937   ClassType* outer = outer_class();
   938   ClassType* other_class = other->as_class();
   940   if (other_class == NULL ||
   941       (outer == NULL) != (other_class->outer_class() == NULL)) {
   942     return false;
   943   }
   945   if (!_identifier->equals(other_class->_identifier)) {
   946     return false;
   947   }
   949   if (outer != NULL && !outer->covariant_match(other_class->outer_class(), ctx)) {
   950     return false;
   951   }
   953   return true;
   954 }
   956 ClassType* ClassType::canonicalize(Context* ctx, int ctx_depth) {
   958   GrowableArray<TypeArgument*> args(_type_arguments.length());
   959   for (int i = 0; i < _type_arguments.length(); ++i) {
   960     args.append(_type_arguments.at(i)->canonicalize(ctx, ctx_depth));
   961   }
   963   ClassType* outer = _outer_class == NULL ? NULL :
   964       _outer_class->canonicalize(ctx, ctx_depth);
   966   return new ClassType(_identifier, args, outer);
   967 }
   969 TypeVariable* TypeVariable::parse_generic_signature(DescriptorStream* STREAM) {
   970   STREAM->set_mark();
   971   char c = READ();
   972   while (c != ';') {
   973     c = READ();
   974   }
   975   Identifier* id = STREAM->identifier_from_mark();
   977   return new TypeVariable(id);
   978 }
   980 void TypeVariable::bind_variables_to_parameters(Descriptor* sig) {
   981   _parameter = sig->find_type_parameter(_id, &_inner_depth);
   982   if (VerifyGenericSignatures && _parameter == NULL) {
   983     fatal("Could not find formal parameter");
   984   }
   985 }
   987 Type* TypeVariable::resolve(Context* ctx, int ctx_depth) {
   988   if (parameter() != NULL) {
   989     return parameter()->resolve(ctx, inner_depth(), ctx_depth);
   990   } else {
   991     if (VerifyGenericSignatures) {
   992       fatal("Type variable matches no parameter");
   993     }
   994     return NULL;
   995   }
   996 }
   998 bool TypeVariable::covariant_match(Type* other, Context* ctx) {
  1000   if (other == this) {
  1001     return true;
  1004   Context my_context(NULL); // empty, results in erasure
  1005   Type* my_type = resolve(&my_context, 0);
  1006   if (my_type == NULL) {
  1007     return false;
  1010   return my_type->covariant_match(other, ctx);
  1013 Type* TypeVariable::canonicalize(Context* ctx, int ctx_depth) {
  1014   return resolve(ctx, ctx_depth);
  1017 #ifndef PRODUCT
  1018 void TypeVariable::reify_signature(stringStream* ss, Context* ctx) {
  1019   Type* type = resolve(ctx, 0);
  1020   if (type != NULL) {
  1021     type->reify_signature(ss, ctx);
  1025 void TypeVariable::print_on(outputStream* str) const {
  1026   str->indent().print_cr("Type Variable {");
  1028     streamIndentor si(str);
  1029     str->indent().print("Name: ");
  1030     _id->print_on(str);
  1031     str->print_cr("");
  1032     str->indent().print_cr("Inner depth: %d", _inner_depth);
  1034   str->indent().print_cr("}");
  1036 #endif // ndef PRODUCT
  1038 ArrayType* ArrayType::parse_generic_signature(DescriptorStream* STREAM) {
  1039   Type* base = Type::parse_generic_signature(CHECK_STREAM);
  1040   return new ArrayType(base);
  1043 void ArrayType::bind_variables_to_parameters(Descriptor* sig) {
  1044   assert(_base != NULL, "Invalid base");
  1045   _base->bind_variables_to_parameters(sig);
  1048 bool ArrayType::covariant_match(Type* other, Context* ctx) {
  1049   assert(_base != NULL, "Invalid base");
  1051   if (other == this) {
  1052     return true;
  1055   ArrayType* other_array = other->as_array();
  1056   return (other_array != NULL && _base->covariant_match(other_array->_base, ctx));
  1059 ArrayType* ArrayType::canonicalize(Context* ctx, int ctx_depth) {
  1060   assert(_base != NULL, "Invalid base");
  1061   return new ArrayType(_base->canonicalize(ctx, ctx_depth));
  1064 #ifndef PRODUCT
  1065 void ArrayType::reify_signature(stringStream* ss, Context* ctx) {
  1066   assert(_base != NULL, "Invalid base");
  1067   ss->print("[");
  1068   _base->reify_signature(ss, ctx);
  1071 void ArrayType::print_on(outputStream* str) const {
  1072   str->indent().print_cr("Array {");
  1074     streamIndentor si(str);
  1075     _base->print_on(str);
  1077   str->indent().print_cr("}");
  1079 #endif // ndef PRODUCT
  1081 bool PrimitiveType::covariant_match(Type* other, Context* ctx) {
  1083   PrimitiveType* other_prim = other->as_primitive();
  1084   return (other_prim != NULL && _type == other_prim->_type);
  1087 PrimitiveType* PrimitiveType::canonicalize(Context* ctx, int ctxd) {
  1088   return this;
  1091 #ifndef PRODUCT
  1092 void PrimitiveType::reify_signature(stringStream* ss, Context* ctx) {
  1093   ss->print("%c", _type);
  1096 void PrimitiveType::print_on(outputStream* str) const {
  1097   str->indent().print_cr("Primitive: '%c'", _type);
  1099 #endif // ndef PRODUCT
  1101 void PrimitiveType::bind_variables_to_parameters(Descriptor* sig) {
  1104 TypeArgument* TypeArgument::parse_generic_signature(DescriptorStream* STREAM) {
  1105   char c = READ();
  1106   Type* type = NULL;
  1108   switch (c) {
  1109     case '*':
  1110       return new TypeArgument(ClassType::java_lang_Object(), NULL);
  1111       break;
  1112     default:
  1113       PUSH(c);
  1114       // fall-through
  1115     case '+':
  1116     case '-':
  1117       type = Type::parse_generic_signature(CHECK_STREAM);
  1118       if (c == '+') {
  1119         return new TypeArgument(type, NULL);
  1120       } else if (c == '-') {
  1121         return new TypeArgument(ClassType::java_lang_Object(), type);
  1122       } else {
  1123         return new TypeArgument(type, type);
  1128 void TypeArgument::bind_variables_to_parameters(Descriptor* sig) {
  1129   assert(_lower_bound != NULL, "Invalid lower bound");
  1130   _lower_bound->bind_variables_to_parameters(sig);
  1131   if (_upper_bound != NULL && _upper_bound != _lower_bound) {
  1132     _upper_bound->bind_variables_to_parameters(sig);
  1136 bool TypeArgument::covariant_match(TypeArgument* other, Context* ctx) {
  1137   assert(_lower_bound != NULL, "Invalid lower bound");
  1139   if (other == this) {
  1140     return true;
  1143   if (!_lower_bound->covariant_match(other->lower_bound(), ctx)) {
  1144     return false;
  1146   return true;
  1149 TypeArgument* TypeArgument::canonicalize(Context* ctx, int ctx_depth) {
  1150   assert(_lower_bound != NULL, "Invalid lower bound");
  1151   Type* lower = _lower_bound->canonicalize(ctx, ctx_depth);
  1152   Type* upper = NULL;
  1154   if (_upper_bound == _lower_bound) {
  1155     upper = lower;
  1156   } else if (_upper_bound != NULL) {
  1157     upper = _upper_bound->canonicalize(ctx, ctx_depth);
  1160   return new TypeArgument(lower, upper);
  1163 #ifndef PRODUCT
  1164 void TypeArgument::print_on(outputStream* str) const {
  1165   str->indent().print_cr("TypeArgument {");
  1167     streamIndentor si(str);
  1168     if (_lower_bound != NULL) {
  1169       str->indent().print("Lower bound: ");
  1170       _lower_bound->print_on(str);
  1172     if (_upper_bound != NULL) {
  1173       str->indent().print("Upper bound: ");
  1174       _upper_bound->print_on(str);
  1177   str->indent().print_cr("}");
  1179 #endif // ndef PRODUCT
  1181 void Context::Mark::destroy() {
  1182   if (is_active()) {
  1183     _context->reset_to_mark(_marked_size);
  1185   deactivate();
  1188 void Context::apply_type_arguments(
  1189     InstanceKlass* current, InstanceKlass* super, TRAPS) {
  1190   assert(_cache != NULL, "Cannot use an empty context");
  1191   ClassType* spec = NULL;
  1192   if (current != NULL) {
  1193     ClassDescriptor* descriptor = _cache->descriptor_for(current, CHECK);
  1194     if (super == current->super()) {
  1195       spec = descriptor->super();
  1196     } else {
  1197       spec = descriptor->interface_desc(super->name());
  1199     if (spec != NULL) {
  1200       _type_arguments.push(spec);
  1205 void Context::reset_to_mark(int size) {
  1206   _type_arguments.trunc_to(size);
  1209 ClassType* Context::at_depth(int i) const {
  1210   if (i < _type_arguments.length()) {
  1211     return _type_arguments.at(_type_arguments.length() - 1 - i);
  1213   return NULL;
  1216 #ifndef PRODUCT
  1217 void Context::print_on(outputStream* str) const {
  1218   str->indent().print_cr("Context {");
  1219   for (int i = 0; i < _type_arguments.length(); ++i) {
  1220     streamIndentor si(str);
  1221     str->indent().print("leval %d: ", i);
  1222     ClassType* ct = at_depth(i);
  1223     if (ct == NULL) {
  1224       str->print_cr("<empty>");
  1225       continue;
  1226     } else {
  1227       str->print_cr("{");
  1230     for (int j = 0; j < ct->type_arguments_length(); ++j) {
  1231       streamIndentor si(str);
  1232       TypeArgument* ta = ct->type_argument_at(j);
  1233       Type* bound = ta->lower_bound();
  1234       bound->print_on(str);
  1236     str->indent().print_cr("}");
  1238   str->indent().print_cr("}");
  1240 #endif // ndef PRODUCT
  1242 ClassDescriptor* DescriptorCache::descriptor_for(InstanceKlass* ik, TRAPS) {
  1244   ClassDescriptor** existing = _class_descriptors.get(ik);
  1245   if (existing == NULL) {
  1246     ClassDescriptor* cd = ClassDescriptor::parse_generic_signature(ik, CHECK_NULL);
  1247     _class_descriptors.put(ik, cd);
  1248     return cd;
  1249   } else {
  1250     return *existing;
  1254 MethodDescriptor* DescriptorCache::descriptor_for(
  1255     Method* mh, ClassDescriptor* cd, TRAPS) {
  1256   assert(mh != NULL && cd != NULL, "Should not be NULL");
  1257   MethodDescriptor** existing = _method_descriptors.get(mh);
  1258   if (existing == NULL) {
  1259     MethodDescriptor* md = MethodDescriptor::parse_generic_signature(mh, cd);
  1260     _method_descriptors.put(mh, md);
  1261     return md;
  1262   } else {
  1263     return *existing;
  1266 MethodDescriptor* DescriptorCache::descriptor_for(Method* mh, TRAPS) {
  1267   ClassDescriptor* cd = descriptor_for(
  1268       InstanceKlass::cast(mh->method_holder()), CHECK_NULL);
  1269   return descriptor_for(mh, cd, THREAD);
  1272 } // namespace generic

mercurial