1 /*
   2  * Copyright (c) 2018, 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 #ifndef SHARE_VM_OOPS_RECORDPARAMSTREAMS_HPP
  26 #define SHARE_VM_OOPS_RECORDPARAMSTREAMS_HPP
  27 
  28 #include "oops/instanceKlass.hpp"
  29 #include "oops/recordParamInfo.hpp"
  30 
  31 // The is the base class for iteration over the record parameters array
  32 // describing the parameters in a record.
  33 class RecordParameterStreamBase : public StackObj {
  34  protected:
  35   Array<u2>*          _record_parameters;
  36   constantPoolHandle  _constants;
  37   int                 _index;
  38   int                 _limit;
  39 
  40   RecordParamInfo* record_param() const { return RecordParamInfo::from_record_params_array(_record_parameters, _index); }
  41   InstanceKlass* record_param_holder() const { return _constants->pool_holder(); }
  42 
  43   RecordParameterStreamBase(Array<u2>* record_params, const constantPoolHandle& constants, int start, int limit) {
  44     _record_parameters = record_params;
  45     _constants = constants;
  46     _index = start;
  47     int num_record_parameters = record_params->length() / RecordParamInfo::param_slots;
  48     if (limit < start) {
  49       _limit = num_record_parameters;
  50     } else {
  51       _limit = limit;
  52     }
  53   }
  54 
  55   RecordParameterStreamBase(Array<u2>* record_params, const constantPoolHandle& constants) {
  56     _record_parameters = record_params;
  57     _constants = constants;
  58     _index = 0;
  59     _limit = record_params->length() / RecordParamInfo::param_slots;;
  60   }
  61 
  62  public:
  63   RecordParameterStreamBase(InstanceKlass* klass) {
  64     _record_parameters = klass->record_params();
  65     _constants = klass->constants();
  66     _index = 0;
  67     _limit = klass->record_params_count();
  68     assert(klass == record_param_holder(), "");
  69   }
  70 
  71   // accessors
  72   int index() const                 { return _index; }
  73 
  74   void next() {
  75     _index += 1;
  76   }
  77   bool done() const { return _index >= _limit; }
  78 
  79   // Accessors for current record parameter
  80   AccessFlags access_flags() const {
  81     AccessFlags flags;
  82     flags.set_flags(record_param()->access_flags());
  83     return flags;
  84   }
  85 
  86   void set_access_flags(u2 flags) const {
  87     record_param()->set_access_flags(flags);
  88   }
  89 
  90   void set_access_flags(AccessFlags flags) const {
  91     set_access_flags(flags.as_short());
  92   }
  93 
  94   Symbol* name() const {
  95     return record_param()->name(_constants);
  96   }
  97 
  98   Symbol* descriptor() const {
  99     return record_param()->descriptor(_constants);
 100   }
 101 
 102   Symbol* signature() const {
 103     return record_param()->signature(_constants);
 104   }
 105 };
 106 
 107 // Iterate over the record parameters
 108 class JavaRecordParameterStream : public RecordParameterStreamBase {
 109  public:
 110   JavaRecordParameterStream(const InstanceKlass* k): RecordParameterStreamBase(k->record_params(), k->constants(), 0, k->record_params_count()) {}
 111 
 112   int name_index() const {
 113     return record_param()->name_index();
 114   }
 115   void set_name_index(int index) {
 116     record_param()->set_name_index(index);
 117   }
 118   int descriptor_index() const {
 119     return record_param()->descriptor_index();
 120   }
 121   void set_descriptor_index(int index) {
 122     record_param()->set_descriptor_index(index);
 123   }
 124   int signature_index() const {
 125     return record_param()->signature_index();
 126   }
 127   void set_generic_signature_index(int index) {
 128     record_param()->set_signature_index(index);
 129   }
 130 };
 131 
 132 #endif // SHARE_VM_OOPS_RECORDPARAMSTREAMS_HPP