1/**
2 * Copyright (c) 2021-2022 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 ASSEMBLER_ASSEMBLY_LITERALS_H
17#define ASSEMBLER_ASSEMBLY_LITERALS_H
18
19#include <string>
20#include <vector>
21
22#include "libpandafile/literal_data_accessor-inl.h"
23
24namespace panda::pandasm {
25
26struct LiteralArray {
27    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
28    struct Literal {
29        // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
30        panda_file::LiteralTag tag_;
31        // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
32        std::variant<bool, uint8_t, uint16_t, uint32_t, uint64_t, float, double, std::string> value_;
33
34        bool IsBoolValue() const
35        {
36            switch (tag_) {
37                case panda_file::LiteralTag::ARRAY_U1:
38                case panda_file::LiteralTag::BOOL:
39                    return true;
40                default:
41                    return false;
42            }
43        }
44
45        bool IsByteValue() const
46        {
47            switch (tag_) {
48                case panda_file::LiteralTag::ARRAY_U8:
49                case panda_file::LiteralTag::ARRAY_I8:
50                case panda_file::LiteralTag::TAGVALUE:
51                case panda_file::LiteralTag::ACCESSOR:
52                case panda_file::LiteralTag::NULLVALUE:
53                    return true;
54                default:
55                    return false;
56            }
57        }
58
59        bool IsShortValue() const
60        {
61            switch (tag_) {
62                case panda_file::LiteralTag::ARRAY_U16:
63                case panda_file::LiteralTag::ARRAY_I16:
64                    return true;
65                default:
66                    return false;
67            }
68        }
69
70        bool IsIntegerValue() const
71        {
72            switch (tag_) {
73                case panda_file::LiteralTag::ARRAY_U32:
74                case panda_file::LiteralTag::ARRAY_I32:
75                case panda_file::LiteralTag::INTEGER:
76                    return true;
77                default:
78                    return false;
79            }
80        }
81
82        bool IsLongValue() const
83        {
84            switch (tag_) {
85                case panda_file::LiteralTag::ARRAY_U64:
86                case panda_file::LiteralTag::ARRAY_I64:
87                    return true;
88                default:
89                    return false;
90            }
91        }
92
93        bool IsFloatValue() const
94        {
95            switch (tag_) {
96                case panda_file::LiteralTag::ARRAY_F32:
97                case panda_file::LiteralTag::FLOAT:
98                    return true;
99                default:
100                    return false;
101            }
102        }
103
104        bool IsDoubleValue() const
105        {
106            switch (tag_) {
107                case panda_file::LiteralTag::ARRAY_F64:
108                case panda_file::LiteralTag::DOUBLE:
109                    return true;
110                default:
111                    return false;
112            }
113        }
114
115        bool IsStringValue() const
116        {
117            switch (tag_) {
118                case panda_file::LiteralTag::ARRAY_STRING:
119                case panda_file::LiteralTag::STRING:
120                case panda_file::LiteralTag::METHOD:
121                case panda_file::LiteralTag::GETTER:
122                case panda_file::LiteralTag::SETTER:
123                case panda_file::LiteralTag::GENERATORMETHOD:
124                case panda_file::LiteralTag::ASYNCGENERATORMETHOD:
125                    return true;
126                default:
127                    return false;
128            }
129        }
130    };
131
132    std::vector<panda::pandasm::LiteralArray::Literal>
133        literals_;  // NOLINT(misc-non-private-member-variables-in-classes)
134
135    explicit LiteralArray(std::vector<panda::pandasm::LiteralArray::Literal> literals) : literals_(std::move(literals))
136    {
137    }
138    explicit LiteralArray() = default;
139
140    static constexpr panda_file::LiteralTag GetArrayTagFromComponentType(panda_file::Type::TypeId type)
141    {
142        switch (type) {
143            case panda_file::Type::TypeId::U1:
144                return panda_file::LiteralTag::ARRAY_U1;
145            case panda_file::Type::TypeId::U8:
146                return panda_file::LiteralTag::ARRAY_U8;
147            case panda_file::Type::TypeId::I8:
148                return panda_file::LiteralTag::ARRAY_I8;
149            case panda_file::Type::TypeId::U16:
150                return panda_file::LiteralTag::ARRAY_U16;
151            case panda_file::Type::TypeId::I16:
152                return panda_file::LiteralTag::ARRAY_I16;
153            case panda_file::Type::TypeId::U32:
154                return panda_file::LiteralTag::ARRAY_U32;
155            case panda_file::Type::TypeId::I32:
156                return panda_file::LiteralTag::ARRAY_I32;
157            case panda_file::Type::TypeId::U64:
158                return panda_file::LiteralTag::ARRAY_U64;
159            case panda_file::Type::TypeId::I64:
160                return panda_file::LiteralTag::ARRAY_I64;
161            case panda_file::Type::TypeId::F32:
162                return panda_file::LiteralTag::ARRAY_F32;
163            case panda_file::Type::TypeId::F64:
164                return panda_file::LiteralTag::ARRAY_F64;
165            default:
166                UNREACHABLE();
167        }
168    }
169};
170
171}  // namespace panda::pandasm
172
173#endif  // ASSEMBLER_ASSEMBLY_LITERALS_H
174