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 CPP_ABCKIT_CORE_MODULE_H 17 #define CPP_ABCKIT_CORE_MODULE_H 18 19 #include "../base_classes.h" 20 #include "./class.h" 21 #include "./export_descriptor.h" 22 #include "./namespace.h" 23 #include "./annotation_interface.h" 24 25 namespace abckit::core { 26 27 /** 28 * @brief Module 29 */ 30 class Module : public ViewInResource<AbckitCoreModule *, const File *> { 31 // We restrict constructors in order to prevent C/C++ API mix-up by user. 32 /// @brief to access private constructor 33 friend class abckit::File; 34 /// @brief to access private constructor 35 friend class core::Class; 36 /// @brief to access private constructor 37 friend class core::ImportDescriptor; 38 /// @brief to access private constructor 39 friend class core::ExportDescriptor; 40 /// @brief to access private constructor 41 friend class core::AnnotationInterface; 42 /// @brief to access private constructor 43 friend class core::Class; 44 /// @brief to access private constructor 45 friend class core::Function; 46 /// @brief abckit::DefaultHash<Module> 47 friend class abckit::DefaultHash<Module>; 48 /// @brief abckit::DynamicIsa 49 friend class abckit::DynamicIsa; 50 /// @brief arkts::Module 51 friend class arkts::Module; 52 53 protected: 54 /// @brief Core API View type 55 using CoreViewT = Module; 56 57 public: 58 /** 59 * @brief Construct a new Module object 60 * @param other 61 */ 62 Module(const Module &other) = default; // CC-OFF(G.CLS.07): design decision, detail: base_concepts.h 63 64 /** 65 * @brief Constructor 66 * @param other 67 * @return Module& 68 */ 69 Module &operator=(const Module &other) = default; 70 71 /** 72 * @brief Construct a new Module object 73 * @param other 74 */ 75 Module(Module &&other) = default; // CC-OFF(G.CLS.07-CPP) 76 77 /** 78 * @brief Constructor 79 * @param other 80 * @return Module& 81 */ 82 Module &operator=(Module &&other) = default; 83 84 /** 85 * @brief Destroy the Module object 86 */ 87 ~Module() override = default; 88 89 /** 90 * @brief Returns binary file that the Module is a part of. 91 * @return `File` that contains Module. 92 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 93 */ 94 const File *GetFile() const; 95 96 /** 97 * @brief Returns the target that the Module was compiled for. 98 * @return Target of the current module. 99 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 100 */ 101 enum AbckitTarget GetTarget() const; 102 103 /** 104 * @brief Returns name. 105 * @return std::string 106 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 107 */ 108 std::string GetName() const; 109 110 /** 111 * @brief Tells if Module is defined in the same binary or externally in another binary. 112 * @return Returns `true` if Module is defined in another binary and `false` if defined locally. 113 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 114 */ 115 bool IsExternal() const; 116 117 /** 118 * @brief Return vector with module's classes. 119 * @return std::vector<core::Class> 120 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 121 */ 122 std::vector<core::Class> GetClasses() const; 123 124 /** 125 * @brief Return vector with module's functions. 126 * @return std::vector<core::Function> 127 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 128 */ 129 std::vector<core::Function> GetTopLevelFunctions() const; 130 131 /** 132 * @brief Return vector with module's annotation interfaces. 133 * @return std::vector<core::AnnotationInterface> 134 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 135 */ 136 std::vector<core::AnnotationInterface> GetAnnotationInterfaces() const; 137 138 /** 139 * @brief Return vector with module's namespaces. 140 * @return std::vector<core::Namespace> 141 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 142 */ 143 std::vector<core::Namespace> GetNamespaces() const; 144 145 /** 146 * @brief Return vector with module's imports. 147 * @return std::vector<core::ImportDescriptor> 148 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 149 */ 150 std::vector<core::ImportDescriptor> GetImports() const; 151 152 /** 153 * @brief Return vector with module's exports. 154 * @return std::vector<core::ExportDescriptor> 155 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 156 */ 157 std::vector<core::ExportDescriptor> GetExports() const; 158 159 /** 160 * @brief Enumerates namespaces of the Module, invoking callback `cb` for each namespace. 161 * @return `false` if was early exited. Otherwise - `true`. 162 * @param [ in ] cb - Callback that will be invoked. Should return `false` on early exit and `true` when iterations 163 * should continue. 164 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 165 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if 'cb' is false. 166 */ 167 bool EnumerateNamespaces(const std::function<bool(core::Namespace)> &cb) const; 168 169 /** 170 * @brief Enumerates top level functions of the Module, invoking callback `cb` for each top level function. 171 * @return `false` if was early exited. Otherwise - `true`. 172 * @param [ in ] cb - Callback that will be invoked. Should return `false` on early exit and `true` when iterations 173 * should continue. 174 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 175 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if 'cb' is false. 176 */ 177 bool EnumerateTopLevelFunctions(const std::function<bool(core::Function)> &cb) const; 178 179 /** 180 * @brief Enumerates classes of the Module, invoking callback `cb` for each class. 181 * @return `false` if was early exited. Otherwise - `true`. 182 * @param [ in ] cb - Callback that will be invoked. Should return `false` on early exit and `true` when iterations 183 * should continue. 184 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 185 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if 'cb' is false. 186 */ 187 bool EnumerateClasses(const std::function<bool(core::Class)> &cb) const; 188 189 /** 190 * @brief Enumerates imports of the Module, invoking callback `cb` for each import. 191 * @return `false` if was early exited. Otherwise - `true`. 192 * @param [ in ] cb - Callback that will be invoked. Should return `false` on early exit and `true` when iterations 193 * should continue. 194 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 195 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if 'cb' is false. 196 */ 197 bool EnumerateImports(const std::function<bool(core::ImportDescriptor)> &cb) const; 198 199 /** 200 * @brief Enumerates anonymous functions of the Module, invoking callback `cb` for each anonymous function. 201 * @return`false` if was early exited. Otherwise - `true`. 202 * @param [ in ] cb - Callback that will be invoked. Should return `false` on early exit and `true` when iterations 203 * should continue. 204 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 205 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if 'cb' is false. 206 */ 207 bool EnumerateAnonymousFunctions(const std::function<bool(core::Function)> &cb) const; 208 209 /** 210 * @brief Enumerates exports of the Module, invoking callback `cb` for each export. 211 * @return `false` if was early exited. Otherwise - `true`. 212 * @param [ in ] cb - Callback that will be invoked. Should return `false` on early exit and `true` when iterations 213 * should continue. 214 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 215 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if 'cb' is false. 216 */ 217 bool EnumerateExports(const std::function<bool(core::ExportDescriptor)> &cb) const; 218 219 /** 220 * @brief Enumerates annotation interfaces of the Module, invoking callback `cb` for each annotation interface. 221 * @return `false` if was early exited. Otherwise - `true`. 222 * @param [ in ] cb - Callback that will be invoked. Should return `false` on early exit and `true` when iterations 223 * should continue. 224 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 225 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if 'cb' is false. 226 */ 227 bool EnumerateAnnotationInterfaces(const std::function<bool(core::AnnotationInterface)> &cb) const; 228 229 private: 230 bool GetClassesInner(std::vector<core::Class> &classes) const; 231 232 bool GetTopLevelFunctionsInner(std::vector<core::Function> &functions) const; 233 234 bool GetAnnotationInterfacesInner(std::vector<core::AnnotationInterface> &ifaces) const; 235 236 bool GetNamespacesInner(std::vector<core::Namespace> &namespaces) const; 237 238 bool GetImportsInner(std::vector<core::ImportDescriptor> &imports) const; 239 240 bool GetExportsInner(std::vector<core::ExportDescriptor> &exports) const; 241 Module(AbckitCoreModule * module,const ApiConfig * conf,const File * file)242 Module(AbckitCoreModule *module, const ApiConfig *conf, const File *file) : ViewInResource(module), conf_(conf) 243 { 244 SetResource(file); 245 }; 246 const ApiConfig *conf_; 247 248 protected: GetApiConfig()249 const ApiConfig *GetApiConfig() const override 250 { 251 return conf_; 252 } 253 }; 254 255 } // namespace abckit::core 256 257 #endif // CPP_ABCKIT_CORE_MODULE_H 258