1 /*
2 * Copyright (c) 2024-2025 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 #ifndef CPP_ABCKIT_CONFIG_H
16 #define CPP_ABCKIT_CONFIG_H
17
18 #include "../../c/abckit.h"
19 #include "../../c/metadata_core.h"
20 #include "../../c/ir_core.h"
21 #include "../../c/isa/isa_dynamic.h"
22 #include "../../c/isa/isa_static.h"
23 #include "../../c/extensions/arkts/metadata_arkts.h"
24 #include "../../c/extensions/js/metadata_js.h"
25
26 #include "utils.h"
27
28 #include <cstdint>
29 #include <memory>
30
31 namespace abckit {
32
33 class DynamicIsa;
34 class StaticIsa;
35 class File;
36 class Graph;
37 class BasicBlock;
38 class Instruction;
39 class Value;
40 class Type;
41 class Literal;
42 class LiteralArray;
43 class Type;
44
45 namespace core {
46 class Module;
47 class Namespace;
48 class Class;
49 class Function;
50 class Annotation;
51 class AnnotationInterface;
52 class AnnotationElement;
53 class AnnotationInterfaceField;
54 class ImportDescriptor;
55 class ExportDescriptor;
56 } // namespace core
57
58 namespace arkts {
59 class Module;
60 class Namespace;
61 class Class;
62 class Function;
63 class Annotation;
64 class AnnotationInterface;
65 class AnnotationElement;
66 class AnnotationInterfaceField;
67 class ImportDescriptor;
68 class ExportDescriptor;
69 } // namespace arkts
70
71 namespace js {
72 class Module;
73 class ImportDescriptor;
74 class ExportDescriptor;
75 } // namespace js
76
77 // Class containing pointers to underlying C API's,
78 // hides C implementation from C++ API user
79 /**
80 * @brief ApiConfig
81 */
82 class ApiConfig final {
83 // Befrend with all core entities so they have an access to config
84 friend DynamicIsa;
85 friend StaticIsa;
86
87 friend File;
88 friend Graph;
89 friend BasicBlock;
90 friend Instruction;
91 friend Value;
92 friend Type;
93 friend Literal;
94 friend LiteralArray;
95 friend Type;
96
97 /// \relates abckit::core::Module
98 friend core::Module;
99 /// \relates abckit::core::Namespace
100 friend core::Namespace;
101 /// \relates abckit::core::Class
102 friend core::Class;
103 /// \relates abckit::core::Function
104 friend core::Function;
105 /// \relates abckit::core::Annotation
106 friend core::Annotation;
107 /// \relates abckit::core::AnnotationInterface
108 friend core::AnnotationInterface;
109 /// \relates abckit::core::AnnotationElement
110 friend core::AnnotationElement;
111 /// \relates abckit::core::AnnotationInterfaceField
112 friend core::AnnotationInterfaceField;
113 /// \relates abckit::core::ImportDescriptor
114 friend core::ImportDescriptor;
115 /// \relates abckit::core::ExportDescriptor
116 friend core::ExportDescriptor;
117
118 /// \relates abckit::arkts::Module
119 friend arkts::Module;
120 /// \relates abckit::arkts::Namespace
121 friend arkts::Namespace;
122 /// \relates abckit::arkts::Class
123 friend arkts::Class;
124 /// \relates abckit::arkts::Function
125 friend arkts::Function;
126 /// \relates abckit::arkts::Annotation
127 friend arkts::Annotation;
128 /// \relates abckit::arkts::AnnotationInterface
129 friend arkts::AnnotationInterface;
130 /// \relates abckit::arkts::AnnotationElement
131 friend arkts::AnnotationElement;
132 /// \relates abckit::arkts::AnnotationInterfaceField
133 friend arkts::AnnotationInterfaceField;
134 /// \relates abckit::arkts::ImportDescriptor
135 friend arkts::ImportDescriptor;
136 /// \relates abckit::arkts::ExportDescriptor
137 friend arkts::ExportDescriptor;
138
139 /// \relates abckit::js::Module
140 friend js::Module;
141 /// \relates abckit::js::ImportDescriptor
142 friend js::ImportDescriptor;
143 /// \relates abckit::js::ExportDescriptor
144 friend js::ExportDescriptor;
145
146 /**
147 * Check error
148 * @param conf
149 */
150 friend void CheckError(const ApiConfig *conf);
151
152 public:
153 /**
154 * @brief Deleted constructor
155 * @param other
156 */
157 ApiConfig(const ApiConfig &other) = delete;
158
159 /**
160 * @brief Deleted constructor
161 * @param other
162 * @return ApiConfig
163 */
164 ApiConfig &operator=(const ApiConfig &other) = delete;
165
166 /**
167 * @brief Deleted constructor
168 * @param other
169 */
170 ApiConfig(ApiConfig &&other) = delete;
171
172 /**
173 * @brief Deleted constructor
174 * @param other
175 * @return ApiConfig
176 */
177 ApiConfig &operator=(ApiConfig &&other) = delete;
178
179 /**
180 * @brief Destructor
181 */
182 ~ApiConfig() = default;
183
184 protected:
185 /**
186 * @brief Constructor
187 * @param eh - unique ptr to IErrorHandler
188 */
ApiConfig(std::unique_ptr<IErrorHandler> eh)189 explicit ApiConfig(std::unique_ptr<IErrorHandler> eh)
190 : cApi_(AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0)),
191 cIapi_(AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0)),
192 cMapi_(AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0)),
193 cArktsIapi_(AbckitGetArktsInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0)),
194 cArktsMapi_(AbckitGetArktsModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0)),
195 cJsIapi_(AbckitGetJsInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0)),
196 cJsMapi_(AbckitGetJsModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0)),
197 cGapi_(AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0)),
198 cDynapi_(AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0)),
199 cStatapi_(AbckitGetIsaApiStaticImpl(ABCKIT_VERSION_RELEASE_1_0_0)),
200 eh_(std::move(eh)) {};
201
202 private:
203 // NOTE(nsizov): make getters for these pointers
204 const AbckitApi *cApi_;
205 const AbckitInspectApi *cIapi_;
206 const AbckitModifyApi *cMapi_;
207 const AbckitArktsInspectApi *cArktsIapi_;
208 const AbckitArktsModifyApi *cArktsMapi_;
209 const AbckitJsInspectApi *cJsIapi_;
210 const AbckitJsModifyApi *cJsMapi_;
211 const AbckitGraphApi *cGapi_;
212 const AbckitIsaApiDynamic *cDynapi_;
213 const AbckitIsaApiStatic *cStatapi_;
214 const std::unique_ptr<IErrorHandler> eh_;
215 };
216
CheckError(const ApiConfig * conf)217 inline void CheckError(const ApiConfig *conf)
218 {
219 const AbckitStatus status = conf->cApi_->getLastError();
220 if (status != ABCKIT_STATUS_NO_ERROR) {
221 conf->eh_->HandleError(abckit::Exception(status));
222 }
223 }
224
225 // Class implement default hash function for all View's
226 /**
227 * @brief DefaultHash<T>
228 */
229 template <typename T>
230 class DefaultHash {
231 public:
232 /**
233 * Operator ()
234 * @param t
235 * @return uintptr_t
236 */
operator()237 virtual uintptr_t operator()(const T &t) const
238 {
239 return reinterpret_cast<uintptr_t>(t.GetView());
240 }
241 };
242
243 } // namespace abckit
244
245 #endif // CPP_ABCKIT_CONFIG_H
246