1 /*
  2  * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_RUNTIME_SIGNATURE_HPP
 27 #define SHARE_RUNTIME_SIGNATURE_HPP
 28 
 29 #include "classfile/symbolTable.hpp"
 30 #include "memory/allocation.hpp"
 31 #include "oops/method.hpp"
 32 
 33 
 34 // Static routines and parsing loops for processing field and method
 35 // descriptors.  In the HotSpot sources we call them "signatures".
 36 //
 37 // A SignatureStream iterates over a Java descriptor (or parts of it).
 38 // The syntax is documented in the Java Virtual Machine Specification,
 39 // section 4.3.
 40 //
 41 // The syntax may be summarized as follows:
 42 //
 43 //     MethodType: '(' {FieldType}* ')' (FieldType | 'V')
 44 //     FieldType: PrimitiveType | ObjectType | ArrayType
 45 //     PrimitiveType: 'B' | 'C' | 'D' | 'F' | 'I' | 'J' | 'S' | 'Z'
 46 //     ObjectType: 'L' ClassName ';' | ArrayType
 47 //     ArrayType: '[' FieldType
 48 //     ClassName: {UnqualifiedName '/'}* UnqualifiedName
 49 //     UnqualifiedName: NameChar {NameChar}*
 50 //     NameChar: ANY_CHAR_EXCEPT('/' | '.' | ';' | '[')
 51 //
 52 // All of the concrete characters in the above grammar are given
 53 // standard manifest constant names of the form JVM_SIGNATURE_x.
 54 // Executable code uses these constant names in preference to raw
 55 // character constants.  Comments and assertion code sometimes use
 56 // the raw character constants for brevity.
 57 //
 58 // The primitive field types (like 'I') correspond 1-1 with type codes
 59 // (like T_INT) which form part of the specification of the 'newarray'
 60 // instruction (JVMS 6.5, section on newarray).  These type codes are
 61 // widely used in the HotSpot code.  They are joined by ad hoc codes
 62 // like T_OBJECT and T_ARRAY (defined in HotSpot but not in the JVMS)
 63 // so that each "basic type" of field descriptor (or void return type)
 64 // has a corresponding T_x code.  Thus, while T_x codes play a very
 65 // minor role in the JVMS, they play a major role in the HotSpot
 66 // sources.  There are fewer than 16 such "basic types", so they fit
 67 // nicely into bitfields.
 68 //
 69 // The syntax of ClassName overlaps slightly with the descriptor
 70 // syntaxes.  The strings "I" and "(I)V" are both class names
 71 // *and* descriptors.  If a class name contains any character other
 72 // than "BCDFIJSZ()V" it cannot be confused with a descriptor.
 73 // Class names inside of descriptors are always contained in an
 74 // "envelope" syntax which starts with 'L' and ends with ';'.
 75 //
 76 // As a confounding factor, array types report their type name strings
 77 // in descriptor format.  These name strings are easy to recognize,
 78 // since they begin with '['.  For this reason some API points on
 79 // HotSpot look for array descriptors as well as proper class names.
 80 //
 81 // For historical reasons some API points that accept class names and
 82 // array names also look for class names wrapped inside an envelope
 83 // (like "LFoo;") and unwrap them on the fly (to a name like "Foo").
 84 
 85 class Signature : AllStatic {
 86  private:
 87   static bool is_valid_array_signature(const Symbol* sig);
 88 
 89  public:
 90 
 91   // Returns the basic type of a field signature (or T_VOID for "V").
 92   // Assumes the signature is a valid field descriptor.
 93   // Do not apply this function to class names or method signatures.
 94   static BasicType basic_type(const Symbol* signature) {
 95     return basic_type(signature->char_at(0));
 96   }
 97 
 98   // Returns T_ILLEGAL for an illegal signature char.
 99   static BasicType basic_type(int ch);
100 
101   // Assuming it is either a class name or signature,
102   // determine if it in fact cannot be a class name.
103   // This means it either starts with '[' or ends with ';'
104   static bool not_class_name(const Symbol* signature) {
105     return (signature->starts_with(JVM_SIGNATURE_ARRAY) ||
106             signature->ends_with(JVM_SIGNATURE_ENDCLASS));
107   }
108 
109   // Assuming it is either a class name or signature,
110   // determine if it in fact is an array descriptor.
111   static bool is_array(const Symbol* signature) {
112     return (signature->utf8_length() > 1 &&
113             signature->char_at(0) == JVM_SIGNATURE_ARRAY &&
114             is_valid_array_signature(signature));
115   }
116 
117   // Assuming it is either a class name or signature,
118   // determine if it contains a class name plus ';'.
119   static bool has_envelope(const Symbol* signature) {
120     return ((signature->utf8_length() > 0) &&
121             signature->ends_with(JVM_SIGNATURE_ENDCLASS) &&
122             has_envelope(signature->char_at(0)));
123   }
124 
125   // Determine if this signature char introduces an
126   // envelope, which is a class name plus ';'.
127   static bool has_envelope(char signature_char) {
128     return (signature_char == JVM_SIGNATURE_CLASS) || (signature_char == JVM_SIGNATURE_INLINE_TYPE);
129   }
130 
131   // Assuming has_envelope is true, return the symbol
132   // inside the envelope, by stripping 'L' and ';'.
133   // Caller is responsible for decrementing the newly created
134   // Symbol's refcount, use TempNewSymbol.
135   static Symbol* strip_envelope(const Symbol* signature);
136 
137   // Assuming it's either a field or method descriptor, determine
138   // whether it is in fact a method descriptor:
139   static bool is_method(const Symbol* signature) {
140     return signature->starts_with(JVM_SIGNATURE_FUNC);
141   }
142 
143   // Assuming it's a method signature, determine if it must
144   // return void.
145   static bool is_void_method(const Symbol* signature) {
146     assert(is_method(signature), "signature is not for a method");
147     return signature->ends_with(JVM_SIGNATURE_VOID);
148   }
149 };
150 
151 // A SignatureIterator uses a SignatureStream to produce BasicType
152 // results, discarding class names.  This means it can be accelerated
153 // using a fingerprint mechanism, in many cases, without loss of type
154 // information.  The FingerPrinter class computes and caches this
155 // reduced information for faster iteration.
156 
157 class SignatureIterator: public ResourceObj {
158  public:
159   typedef uint64_t fingerprint_t;
160 
161  protected:
162   Symbol*      _signature;             // the signature to iterate over
163   BasicType    _return_type;
164   fingerprint_t _fingerprint;
165 
166  public:
167   // Definitions used in generating and iterating the
168   // bit field form of the signature generated by the
169   // Fingerprinter.
170   enum {
171     fp_static_feature_size    = 1,
172     fp_is_static_bit          = 1,
173 
174     fp_result_feature_size    = 4,
175     fp_result_feature_mask    = right_n_bits(fp_result_feature_size),
176     fp_parameter_feature_size = 4,
177     fp_parameter_feature_mask = right_n_bits(fp_parameter_feature_size),
178 
179     fp_parameters_done        = 0,  // marker for end of parameters (must be zero)
180 
181     // Parameters take up full wordsize, minus the result and static bit fields.
182     // Since fp_parameters_done is zero, termination field arises from shifting
183     // in zero bits, and therefore occupies no extra space.
184     // The sentinel value is all-zero-bits, which is impossible for a true
185     // fingerprint, since at least the result field will be non-zero.
186     fp_max_size_of_parameters = ((BitsPerLong
187                                   - (fp_result_feature_size + fp_static_feature_size))
188                                  / fp_parameter_feature_size)
189   };
190 
191   static bool fp_is_valid_type(BasicType type, bool for_return_type = false);
192 
193   // Sentinel values are zero and not-zero (-1).
194   // No need to protect the sign bit, since every valid return type is non-zero
195   // (even T_VOID), and there are no valid parameter fields which are 0xF (T_VOID).
196   static fingerprint_t zero_fingerprint() { return (fingerprint_t)0; }
197   static fingerprint_t overflow_fingerprint() { return ~(fingerprint_t)0; }
198   static bool fp_is_valid(fingerprint_t fingerprint) {
199     return (fingerprint != zero_fingerprint()) && (fingerprint != overflow_fingerprint());
200   }
201 
202   // Constructors
203   SignatureIterator(Symbol* signature, fingerprint_t fingerprint = zero_fingerprint()) {
204     _signature   = signature;
205     _return_type = T_ILLEGAL;  // sentinel value for uninitialized
206     _fingerprint = zero_fingerprint();
207     if (fingerprint != _fingerprint) {
208       set_fingerprint(fingerprint);
209     }
210   }
211 
212   // If the fingerprint is present, we can use an accelerated loop.
213   void set_fingerprint(fingerprint_t fingerprint);
214 
215   // Returns the set fingerprint, or zero_fingerprint()
216   // if none has been set already.
217   fingerprint_t fingerprint() const { return _fingerprint; }
218 
219   // Iteration
220   // Hey look:  There are no virtual methods in this class.
221   // So how is it customized?  By calling do_parameters_on
222   // an object which answers to "do_type(BasicType)".
223   // By convention, this object is in the subclass
224   // itself, so the call is "do_parameters_on(this)".
225   // The effect of this is to inline the parsing loop
226   // everywhere "do_parameters_on" is called.
227   // If there is a valid fingerprint in the object,
228   // an improved loop is called which just unpacks the
229   // bitfields from the fingerprint.  Otherwise, the
230   // symbol is parsed.
231   template<typename T> inline void do_parameters_on(T* callback); // iterates over parameters only
232   BasicType return_type();  // computes the value on the fly if necessary
233 
234   static bool fp_is_static(fingerprint_t fingerprint) {
235     assert(fp_is_valid(fingerprint), "invalid fingerprint");
236     return fingerprint & fp_is_static_bit;
237   }
238   static BasicType fp_return_type(fingerprint_t fingerprint) {
239     assert(fp_is_valid(fingerprint), "invalid fingerprint");
240     return (BasicType) ((fingerprint >> fp_static_feature_size) & fp_result_feature_mask);
241   }
242   static fingerprint_t fp_start_parameters(fingerprint_t fingerprint) {
243     assert(fp_is_valid(fingerprint), "invalid fingerprint");
244     return fingerprint >> (fp_static_feature_size + fp_result_feature_size);
245   }
246   static BasicType fp_next_parameter(fingerprint_t& mask) {
247     int result = (mask & fp_parameter_feature_mask);
248     mask >>= fp_parameter_feature_size;
249     return (BasicType) result;
250   }
251 };
252 
253 
254 // Specialized SignatureIterators: Used to compute signature specific values.
255 
256 class SignatureTypeNames : public SignatureIterator {
257  protected:
258   virtual void type_name(const char* name)   = 0;
259 
260   friend class SignatureIterator;  // so do_parameters_on can call do_type
261   void do_type(BasicType type) {
262     switch (type) {
263     case T_BOOLEAN: type_name("jboolean"); break;
264     case T_CHAR:    type_name("jchar"   ); break;
265     case T_FLOAT:   type_name("jfloat"  ); break;
266     case T_DOUBLE:  type_name("jdouble" ); break;
267     case T_BYTE:    type_name("jbyte"   ); break;
268     case T_SHORT:   type_name("jshort"  ); break;
269     case T_INT:     type_name("jint"    ); break;
270     case T_LONG:    type_name("jlong"   ); break;
271     case T_VOID:    type_name("void"    ); break;
272     case T_ARRAY:
273     case T_OBJECT:
274     case T_INLINE_TYPE:  type_name("jobject" ); break;
275     default: ShouldNotReachHere();
276     }
277   }
278 
279  public:
280   SignatureTypeNames(Symbol* signature) : SignatureIterator(signature) {}
281 };
282 
283 
284 // Specialized SignatureIterator: Used to compute the argument size.
285 
286 class ArgumentSizeComputer: public SignatureIterator {
287  private:
288   int _size;
289   friend class SignatureIterator;  // so do_parameters_on can call do_type
290   void do_type(BasicType type) { _size += parameter_type_word_count(type); }
291  public:
292   ArgumentSizeComputer(Symbol* signature);
293   int size() { return _size; }
294 };
295 
296 
297 class ArgumentCount: public SignatureIterator {
298  private:
299   int _size;
300   friend class SignatureIterator;  // so do_parameters_on can call do_type
301   void do_type(BasicType type) { _size++; }
302  public:
303   ArgumentCount(Symbol* signature);
304   int size() { return _size; }
305 };
306 
307 
308 class ReferenceArgumentCount: public SignatureIterator {
309  private:
310   int _refs;
311   friend class SignatureIterator;  // so do_parameters_on can call do_type
312   void do_type(BasicType type) { if (is_reference_type(type)) _refs++; }
313  public:
314   ReferenceArgumentCount(Symbol* signature);
315   int count() { return _refs; }
316 };
317 
318 
319 // Specialized SignatureIterator: Used to compute the result type.
320 
321 class ResultTypeFinder: public SignatureIterator {
322  public:
323   BasicType type() { return return_type(); }
324   ResultTypeFinder(Symbol* signature) : SignatureIterator(signature) { }
325 };
326 
327 
328 // Fingerprinter computes a unique ID for a given method. The ID
329 // is a bitvector characterizing the methods signature (incl. the receiver).
330 class Fingerprinter: public SignatureIterator {
331  private:
332   fingerprint_t _accumulator;
333   int _param_size;
334   int _shift_count;
335   const Method* _method;
336 
337   void initialize_accumulator() {
338     _accumulator = 0;
339     _shift_count = fp_result_feature_size + fp_static_feature_size;
340     _param_size = 0;
341   }
342 
343   // Out-of-line method does it all in constructor:
344   void compute_fingerprint_and_return_type(bool static_flag = false);
345 
346   friend class SignatureIterator;  // so do_parameters_on can call do_type
347   void do_type(BasicType type) {
348     assert(fp_is_valid_type(type), "bad parameter type");
349     _accumulator |= ((fingerprint_t)type << _shift_count);
350     _shift_count += fp_parameter_feature_size;
351     _param_size += (is_double_word_type(type) ? 2 : 1);
352   }
353 
354  public:
355   int size_of_parameters() const { return _param_size; }
356   // fingerprint() and return_type() are in super class
357 
358   Fingerprinter(const methodHandle& method)
359     : SignatureIterator(method->signature()),
360       _method(method()) {
361     compute_fingerprint_and_return_type();
362   }
363   Fingerprinter(Symbol* signature, bool is_static)
364     : SignatureIterator(signature),
365       _method(NULL) {
366     compute_fingerprint_and_return_type(is_static);
367   }
368 };
369 
370 
371 // Specialized SignatureIterator: Used for native call purposes
372 
373 class NativeSignatureIterator: public SignatureIterator {
374  private:
375   methodHandle _method;
376 // We need separate JNI and Java offset values because in 64 bit mode,
377 // the argument offsets are not in sync with the Java stack.
378 // For example a long takes up 1 "C" stack entry but 2 Java stack entries.
379   int          _offset;                // The java stack offset
380   int          _prepended;             // number of prepended JNI parameters (1 JNIEnv, plus 1 mirror if static)
381   int          _jni_offset;            // the current parameter offset, starting with 0
382 
383   friend class SignatureIterator;  // so do_parameters_on can call do_type
384   void do_type(BasicType type) {
385     switch (type) {
386     case T_BYTE:
387     case T_BOOLEAN:
388       pass_byte();  _jni_offset++; _offset++;
389       break;
390     case T_CHAR:
391     case T_SHORT:
392       pass_short();  _jni_offset++; _offset++;
393       break;
394     case T_INT:
395       pass_int();    _jni_offset++; _offset++;
396       break;
397     case T_FLOAT:
398       pass_float();  _jni_offset++; _offset++;
399       break;
400     case T_DOUBLE: {
401       int jni_offset = LP64_ONLY(1) NOT_LP64(2);
402       pass_double(); _jni_offset += jni_offset; _offset += 2;
403       break;
404     }
405     case T_LONG: {
406       int jni_offset = LP64_ONLY(1) NOT_LP64(2);
407       pass_long();   _jni_offset += jni_offset; _offset += 2;
408       break;
409     }
410     case T_ARRAY:
411     case T_OBJECT:
412     case T_INLINE_TYPE:
413       pass_object(); _jni_offset++; _offset++;
414       break;
415     default:
416       ShouldNotReachHere();
417     }
418   }
419 
420  public:
421   methodHandle method() const          { return _method; }
422   int          offset() const          { return _offset; }
423   int      jni_offset() const          { return _jni_offset + _prepended; }
424   bool      is_static() const          { return method()->is_static(); }
425   virtual void pass_int()              = 0;
426   virtual void pass_long()             = 0;
427   virtual void pass_object()           = 0;  // objects, arrays, inlines
428   virtual void pass_float()            = 0;
429   virtual void pass_byte()             { pass_int(); };
430   virtual void pass_short()            { pass_int(); };
431 #ifdef _LP64
432   virtual void pass_double()           = 0;
433 #else
434   virtual void pass_double()           { pass_long(); }  // may be same as long
435 #endif
436 
437   NativeSignatureIterator(const methodHandle& method) : SignatureIterator(method->signature()) {
438     _method = method;
439     _offset = 0;
440     _jni_offset = 0;
441 
442     const int JNIEnv_words = 1;
443     const int mirror_words = 1;
444     _prepended = !is_static() ? JNIEnv_words : JNIEnv_words + mirror_words;
445   }
446 
447   void iterate() { iterate(Fingerprinter(method()).fingerprint()); }
448 
449   // iterate() calls the 3 virtual methods according to the following invocation syntax:
450   //
451   // {pass_int | pass_long | pass_object}
452   //
453   // Arguments are handled from left to right (receiver first, if any).
454   // The offset() values refer to the Java stack offsets but are 0 based and increasing.
455   // The java_offset() values count down to 0, and refer to the Java TOS.
456   // The jni_offset() values increase from 1 or 2, and refer to C arguments.
457   // The method's return type is ignored.
458 
459   void iterate(fingerprint_t fingerprint) {
460     set_fingerprint(fingerprint);
461     if (!is_static()) {
462       // handle receiver (not handled by iterate because not in signature)
463       pass_object(); _jni_offset++; _offset++;
464     }
465     do_parameters_on(this);
466   }
467 };
468 
469 
470 // This is the core parsing logic for iterating over signatures.
471 // All of the previous classes use this for doing their work.
472 
473 class SignatureStream : public StackObj {
474  private:
475   const Symbol* _signature;
476   int          _begin;
477   int          _end;
478   int          _limit;
479   int          _array_prefix;  // count of '[' before the array element descr
480   BasicType    _type;
481   int          _state;
482   Symbol*      _previous_name;    // cache the previously looked up symbol to avoid lookups
483   GrowableArray<Symbol*>* _names; // symbols created while parsing that need to be dereferenced
484 
485   Symbol* find_symbol();
486 
487   enum { _s_field = 0, _s_method = 1, _s_method_return = 3 };
488   void set_done() {
489     _state |= -2;   // preserve s_method bit
490     assert(is_done(), "Unable to set state to done");
491   }
492   int scan_type(BasicType bt);
493 
494  public:
495   bool at_return_type() const                    { return _state == (int)_s_method_return; }
496   bool is_done() const                           { return _state < 0; }
497   void next();
498 
499   SignatureStream(const Symbol* signature, bool is_method = true);
500   ~SignatureStream();
501 
502   bool is_reference() const { return is_reference_type(_type); }
503   bool is_array() const     { return _type == T_ARRAY; }
504   bool is_primitive() const { return is_java_primitive(_type); }
505   BasicType type() const    { return _type; }
506 
507   const u1* raw_bytes() const  { return _signature->bytes() + _begin; }
508   int       raw_length() const { return _end - _begin; }
509   int raw_symbol_begin() const { return _begin + (has_envelope() ? 1 : 0); }
510   int raw_symbol_end() const   { return _end  -  (has_envelope() ? 1 : 0); }
511   char raw_char_at(int i) const {
512     assert(i < _limit, "index for raw_char_at is over the limit");
513     return _signature->char_at(i);
514   }
515 
516   // True if there is an embedded class name in this type,
517   // followed by ';'.
518   bool has_envelope() const {
519     if (!Signature::has_envelope(_signature->char_at(_begin)))
520       return false;
521     // this should always be true, but let's test it:
522     assert(_signature->char_at(_end-1) == JVM_SIGNATURE_ENDCLASS, "signature envelope has no semi-colon at end");
523     return true;
524   }
525 
526   bool has_Q_descriptor() const {
527     return has_envelope() && (_signature->char_at(_begin) == JVM_SIGNATURE_INLINE_TYPE);
528   }
529 
530   // return the symbol for chars in symbol_begin()..symbol_end()
531   Symbol* as_symbol() {
532     return find_symbol();
533   }
534 
535   // in case you want only the return type:
536   void skip_to_return_type();
537 
538   // number of '[' in array prefix
539   int array_prefix_length() {
540     return _type == T_ARRAY ? _array_prefix : 0;
541   }
542 
543   // In case you want only the array base type,
544   // reset the stream after skipping some brackets '['.
545   // (The argument is clipped to array_prefix_length(),
546   // and if it ends up as zero this call is a nop.
547   // The default is value skips all brackets '['.)
548  private:
549   int skip_whole_array_prefix();
550  public:
551   int skip_array_prefix(int max_skip_length) {
552     if (_type != T_ARRAY) {
553       return 0;
554     }
555      if (_array_prefix > max_skip_length) {
556       // strip some but not all levels of T_ARRAY
557       _array_prefix -= max_skip_length;
558       _begin += max_skip_length;
559       return max_skip_length;
560     }
561     return skip_whole_array_prefix();
562   }
563   int skip_array_prefix() {
564     if (_type != T_ARRAY) {
565       return 0;
566     }
567     return skip_whole_array_prefix();
568   }
569 
570   // free-standing lookups (bring your own CL/PD pair)
571   enum FailureMode { ReturnNull, NCDFError, CachedOrNull };
572 
573   Klass* as_klass(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS);
574   InlineKlass* as_inline_klass(InstanceKlass* holder);
575   oop as_java_mirror(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS);
576 };
577 
578 class SigEntryFilter;
579 typedef GrowableArrayFilterIterator<SigEntry, SigEntryFilter> ExtendedSignature;
580 
581 // Used for adapter generation. One SigEntry is used per element of
582 // the signature of the method. Inline type arguments are treated
583 // specially. See comment for InlineKlass::collect_fields().
584 class SigEntry {
585  public:
586   BasicType _bt;
587   int _offset;
588   Symbol* _symbol;
589 
590   SigEntry()
591     : _bt(T_ILLEGAL), _offset(-1), _symbol(NULL) {}
592 
593   SigEntry(BasicType bt, int offset, Symbol* symbol)
594     : _bt(bt), _offset(offset), _symbol(symbol) {}
595 
596   static int compare(SigEntry* e1, SigEntry* e2) {
597     if (e1->_offset != e2->_offset) {
598       return e1->_offset - e2->_offset;
599     }
600     assert((e1->_bt == T_LONG && (e2->_bt == T_LONG || e2->_bt == T_VOID)) ||
601            (e1->_bt == T_DOUBLE && (e2->_bt == T_DOUBLE || e2->_bt == T_VOID)) ||
602            e1->_bt == T_INLINE_TYPE || e2->_bt == T_INLINE_TYPE || e1->_bt == T_VOID || e2->_bt == T_VOID, "bad bt");
603     if (e1->_bt == e2->_bt) {
604       assert(e1->_bt == T_INLINE_TYPE || e1->_bt == T_VOID, "only ones with duplicate offsets");
605       return 0;
606     }
607     if (e1->_bt == T_VOID ||
608         e2->_bt == T_INLINE_TYPE) {
609       return 1;
610     }
611     if (e1->_bt == T_INLINE_TYPE ||
612         e2->_bt == T_VOID) {
613       return -1;
614     }
615     ShouldNotReachHere();
616     return 0;
617   }
618   static void add_entry(GrowableArray<SigEntry>* sig, BasicType bt, Symbol* symbol, int offset = -1);
619   static bool skip_value_delimiters(const GrowableArray<SigEntry>* sig, int i);
620   static int fill_sig_bt(const GrowableArray<SigEntry>* sig, BasicType* sig_bt);
621   static TempNewSymbol create_symbol(const GrowableArray<SigEntry>* sig);
622 };
623 
624 class SigEntryFilter {
625 public:
626   bool operator()(const SigEntry& entry) { return entry._bt != T_INLINE_TYPE && entry._bt != T_VOID; }
627 };
628 
629 // Specialized SignatureStream: used for invoking SystemDictionary to either find
630 //                              or resolve the underlying type when iterating over a
631 //                              Java descriptor (or parts of it).
632 class ResolvingSignatureStream : public SignatureStream {
633   Klass*       _load_origin;
634   bool         _handles_cached;
635   Handle       _class_loader;       // cached when needed
636   Handle       _protection_domain;  // cached when needed
637 
638   void initialize_load_origin(Klass* load_origin) {
639     _load_origin = load_origin;
640     _handles_cached = (load_origin == NULL);
641   }
642   void need_handles() {
643     if (!_handles_cached) {
644       cache_handles();
645       _handles_cached = true;
646     }
647   }
648   void cache_handles();
649 
650  public:
651   ResolvingSignatureStream(Symbol* signature, Klass* load_origin, bool is_method = true);
652   ResolvingSignatureStream(Symbol* signature, Handle class_loader, Handle protection_domain, bool is_method = true);
653   ResolvingSignatureStream(const Method* method);
654   ResolvingSignatureStream(fieldDescriptor& field);
655 
656   Klass* load_origin()       { return _load_origin; }
657   Handle class_loader()      { need_handles(); return _class_loader; }
658   Handle protection_domain() { need_handles(); return _protection_domain; }
659 
660   Klass* as_klass_if_loaded(TRAPS);
661   Klass* as_klass(FailureMode failure_mode, TRAPS) {
662     need_handles();
663     return SignatureStream::as_klass(_class_loader, _protection_domain,
664                                      failure_mode, THREAD);
665   }
666   oop as_java_mirror(FailureMode failure_mode, TRAPS) {
667     if (is_reference()) {
668       need_handles();
669     }
670     return SignatureStream::as_java_mirror(_class_loader, _protection_domain,
671                                            failure_mode, THREAD);
672   }
673 };
674 
675 // Here is how all the SignatureIterator classes invoke the
676 // SignatureStream engine to do their parsing.
677 template<typename T> inline
678 void SignatureIterator::do_parameters_on(T* callback) {
679   fingerprint_t unaccumulator = _fingerprint;
680 
681   // Check for too many arguments, or missing fingerprint:
682   if (!fp_is_valid(unaccumulator)) {
683     SignatureStream ss(_signature);
684     for (; !ss.at_return_type(); ss.next()) {
685       callback->do_type(ss.type());
686     }
687     // while we are here, capture the return type
688     _return_type = ss.type();
689   } else {
690     // Optimized version of do_parameters when fingerprint is known
691     assert(_return_type != T_ILLEGAL, "return type already captured from fp");
692     unaccumulator = fp_start_parameters(unaccumulator);
693     for (BasicType type; (type = fp_next_parameter(unaccumulator)) != (BasicType)fp_parameters_done; ) {
694       assert(fp_is_valid_type(type), "garbled fingerprint");
695       callback->do_type(type);
696     }
697   }
698 }
699 
700 #ifdef ASSERT
701  class SignatureVerifier : public StackObj {
702   public:
703     static bool is_valid_method_signature(const Symbol* sig);
704     static bool is_valid_type_signature(const Symbol* sig);
705   private:
706     static ssize_t is_valid_type(const char*, ssize_t);
707 };
708 #endif
709 #endif // SHARE_RUNTIME_SIGNATURE_HPP