1 /* 2 * Copyright (c) 1999, 2025, 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_CI_CICONSTANT_HPP 26 #define SHARE_CI_CICONSTANT_HPP 27 28 #include "ci/ciClassList.hpp" 29 #include "utilities/globalDefinitions.hpp" 30 31 // ciConstant 32 // 33 // This class represents a constant value. 34 class ciConstant { 35 private: 36 friend class ciEnv; 37 friend class ciField; 38 39 BasicType _type; 40 union { 41 jint _int; 42 jlong _long; 43 jfloat _float; 44 jdouble _double; 45 ciObject* _object; 46 } _value; 47 48 public: 49 50 ciConstant() { 51 _type = T_ILLEGAL; _value._long = -1; 52 } 53 ciConstant(BasicType type, jint value) { 54 assert(type != T_LONG && type != T_DOUBLE && type != T_FLOAT, 55 "using the wrong ciConstant constructor"); 56 _type = type; _value._int = value; 57 } 58 ciConstant(jlong value) { 59 _type = T_LONG; _value._long = value; 60 } 61 ciConstant(jfloat value) { 62 _type = T_FLOAT; _value._float = value; 63 } 64 ciConstant(jdouble value) { 65 _type = T_DOUBLE; _value._double = value; 66 } 67 ciConstant(BasicType type, ciObject* p) { 68 _type = type; _value._object = p; 69 } 70 71 BasicType basic_type() const { return _type; } 72 73 jboolean as_boolean() { 74 assert(basic_type() == T_BOOLEAN, "wrong type"); 75 return (jboolean)_value._int; 76 } 77 jchar as_char() { 78 assert(basic_type() == T_CHAR, "wrong type"); 79 return (jchar)_value._int; 80 } 81 jbyte as_byte() { 82 assert(basic_type() == T_BYTE, "wrong type"); 83 return (jbyte)_value._int; 84 } 85 jshort as_short() { 86 assert(basic_type() == T_SHORT, "wrong type"); 87 return (jshort)_value._int; 88 } 89 jint as_int() { 90 assert(basic_type() == T_BOOLEAN || basic_type() == T_CHAR || 91 basic_type() == T_BYTE || basic_type() == T_SHORT || 92 basic_type() == T_INT, "wrong type"); 93 return _value._int; 94 } 95 jlong as_long() { 96 assert(basic_type() == T_LONG, "wrong type"); 97 return _value._long; 98 } 99 jfloat as_float() { 100 assert(basic_type() == T_FLOAT, "wrong type"); 101 return _value._float; 102 } 103 jdouble as_double() { 104 assert(basic_type() == T_DOUBLE, "wrong type"); 105 return _value._double; 106 } 107 ciObject* as_object() const { 108 assert(is_reference_type(basic_type()), "wrong type"); 109 return _value._object; 110 } 111 112 bool is_null_or_zero() const; 113 114 bool is_valid() const { 115 return basic_type() != T_ILLEGAL; 116 } 117 118 bool is_loaded() const; 119 120 // Debugging output 121 void print(); 122 }; 123 124 #endif // SHARE_CI_CICONSTANT_HPP