1 /**
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef BYTECODE_OPTIMIZER_CONSTANT_PROPAGATION_CONSTANT_VALUE_H
17 #define BYTECODE_OPTIMIZER_CONSTANT_PROPAGATION_CONSTANT_VALUE_H
18 
19 namespace panda::bytecodeopt {
20 
21 class ConstantValue {
22 public:
23     enum ConstantType {
24         CONSTANT_BOOL,
25         CONSTANT_INT32,
26         CONSTANT_INT64,
27         CONSTANT_DOUBLE,
28         CONSTANT_STRING,
29         CONSTANT_INVALID
30     };
31 
ConstantValue()32     explicit ConstantValue()
33         : type_(CONSTANT_INVALID), value_() {}
34 
35     ConstantValue(const ConstantValue &other) = default;
36     ConstantValue& operator=(const ConstantValue &other) = default;
37     ~ConstantValue() = default;
38 
ConstantValue(bool val)39     explicit ConstantValue(bool val)
40         : type_(CONSTANT_BOOL), value_(val) {}
41 
ConstantValue(int32_t val)42     explicit ConstantValue(int32_t val)
43         : type_(CONSTANT_INT32), value_(val) {}
44 
ConstantValue(int64_t val)45     explicit ConstantValue(int64_t val)
46         : type_(CONSTANT_INT64), value_(val) {}
47 
ConstantValue(double val)48     explicit ConstantValue(double val)
49         : type_(CONSTANT_DOUBLE), value_(val) {}
50 
ConstantValue(std::string val)51     explicit ConstantValue(std::string val)
52         : type_(CONSTANT_STRING), value_(val) {}
53 
GetType() const54     ConstantType GetType() const
55     {
56         return type_;
57     }
58 
59     template<class T>
GetValue() const60     T GetValue() const
61     {
62         ASSERT(std::holds_alternative<T>(value_));
63         return std::get<T>(value_);
64     }
65 
GetValue() const66     auto& GetValue() const
67     {
68         return value_;
69     }
70 
ToString()71     std::string ToString()
72     {
73         std::stringstream ss;
74         switch (type_) {
75             case CONSTANT_BOOL:
76                 ss << "[Bool: " << (std::get<bool>(value_) ? "True" : "False") << "]";
77                 break;
78             case CONSTANT_INT32:
79                 ss << "[Int32: " << std::get<int32_t>(value_) << "]";
80                 break;
81             case CONSTANT_INT64:
82                 ss << "[Int64: " << std::get<int64_t>(value_) << "]";
83                 break;
84             case CONSTANT_DOUBLE:
85                 ss << "[Double: " << std::get<double>(value_) << "]";
86                 break;
87             case CONSTANT_STRING:
88                 ss << "[String: " << std::get<std::string>(value_) << "]";
89                 break;
90             default:
91                 ss << "[INVALID]";
92         }
93         return ss.str();
94     }
95 
96     bool operator==(const ConstantValue &other) const noexcept
97     {
98         return type_ == other.type_ && value_ == other.value_;
99     }
100 
101 private:
102     ConstantType type_;
103     std::variant<bool, int32_t, int64_t, double, std::string> value_;
104 };
105 
106 }  // namespace panda::bytecodeopt
107 
108 #endif  // BYTECODE_OPTIMIZER_CONSTANT_PROPAGATION_CONSTANT_VALUE_H
109