• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 ES2PANDA_DYNAMIC_LANGUAGE_H
17 #define ES2PANDA_DYNAMIC_LANGUAGE_H
18 
19 #include <array>
20 #include <optional>
21 #include <string_view>
22 
23 #include "libpandabase/macros.h"
24 
25 namespace panda::es2panda {
26 
27 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
28 #define LANGUAGES(_)   \
29     _(AS, "as", false) \
30     _(JS, "js", true)  \
31     _(TS, "ts", true)  \
32     _(ETS, "ets", false)
33 
34 class Language {
35 public:
36     enum class Id {
37 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
38 #define TO_ENUM(e, s, d) e,
39         LANGUAGES(TO_ENUM)
40 #undef TO_ENUM
41             COUNT
42     };
43 
Language(Id id)44     constexpr explicit Language(Id id) : id_(id) {}
45 
ToString()46     constexpr std::string_view ToString() const
47     {
48 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
49 #define TO_STR(e, s, d) \
50     if (id_ == Id::e) { \
51         return s;       \
52     }
53         LANGUAGES(TO_STR)
54 #undef TO_STR
55         UNREACHABLE();
56     }
57 
FromString(std::string_view str)58     static std::optional<Language> FromString(std::string_view str)
59     {
60 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
61 #define FROM_STR(e, s, d)       \
62     if (str == (s)) {           \
63         return Language(Id::e); \
64     }
65         LANGUAGES(FROM_STR)
66 #undef FROM_STR
67         return {};
68     }
69 
GetId()70     Id GetId() const
71     {
72         return id_;
73     }
74 
IsDynamic()75     bool IsDynamic() const
76     {
77 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
78 #define TO_DYN(e, s, d) \
79     if (id_ == Id::e) { \
80         return d;       \
81     }
82         LANGUAGES(TO_DYN)
83 #undef TO_DYN
84         UNREACHABLE();
85     }
86 
87     bool operator==(const Language &l) const
88     {
89         return id_ == l.id_;
90     }
91 
92     bool operator!=(const Language &l) const
93     {
94         return id_ != l.id_;
95     }
96 
97 private:
98     static constexpr auto COUNT = static_cast<size_t>(Id::COUNT);
99 
100 public:
All()101     static std::array<Language, COUNT> All()
102     {
103         return {
104 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
105 #define TO_LANG(e, s, d) Language(Id::e),
106             LANGUAGES(TO_LANG)
107 #undef TO_LANG
108         };
109     }
110 
111 private:
112     Id id_;
113 };
114 
115 }  // namespace panda::es2panda
116 
117 // NOLINTNEXTLINE(cert-dcl58-cpp)
118 namespace std {
119 
120 template <>
121 // NOLINTNEXTLINE(altera-struct-pack-align)
122 struct hash<panda::es2panda::Language> {
123     std::size_t operator()(panda::es2panda::Language lang) const
124     {
125         return std::hash<panda::es2panda::Language::Id> {}(lang.GetId());
126     }
127 };
128 
129 }  // namespace std
130 
131 #endif  // ES2PANDA_DYNAMIC_LANGUAGE_H
132