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_FUNCTION_H 17 #define CPP_ABCKIT_CORE_FUNCTION_H 18 19 #include "../base_classes.h" 20 #include "./annotation.h" 21 22 #include <functional> 23 #include <string> 24 #include <vector> 25 26 namespace abckit::core { 27 28 /** 29 * @brief Function 30 */ 31 class Function : public ViewInResource<AbckitCoreFunction *, const File *> { 32 // We restrict constructors in order to prevent C/C++ API mix-up by user. 33 /// @brief to access private constructor 34 friend class core::Class; 35 /// @brief to access private constructor 36 friend class core::Namespace; 37 /// @brief to access private constructor 38 friend class core::Module; 39 /// @brief to access private constructor 40 friend class abckit::Instruction; 41 /// @brief abckit::DefaultHash<Function> 42 friend class abckit::DefaultHash<Function>; 43 /// @brief abckit::DynamicIsa 44 friend class abckit::DynamicIsa; 45 /// @brief to access private constructor 46 friend class abckit::File; 47 /// @brief to access private constructor 48 friend class arkts::Namespace; 49 50 protected: 51 /// @brief Core API View type 52 using CoreViewT = Function; 53 54 public: 55 /** 56 * @brief Construct a new empty Function object 57 */ Function()58 Function() : ViewInResource(nullptr), conf_(nullptr) 59 { 60 SetResource(nullptr); 61 }; 62 63 /** 64 * @brief Construct a new Function object 65 * @param other 66 */ 67 Function(const Function &other) = default; // CC-OFF(G.CLS.07): design decision, detail: base_concepts.h 68 69 /** 70 * @brief Constructor 71 * @param other 72 * @return Function& 73 */ 74 Function &operator=(const Function &other) = default; 75 76 /** 77 * @brief Construct a new Function object 78 * @param other 79 */ 80 Function(Function &&other) = default; // CC-OFF(G.CLS.07): design decision, detail: base_concepts.h 81 82 /** 83 * @brief Constructor 84 * @param other 85 * @return Function& 86 */ 87 Function &operator=(Function &&other) = default; 88 89 /** 90 * @brief Destroy the Function object 91 */ 92 ~Function() override = default; 93 94 /** 95 * @brief Create the `Graph` object 96 * @return Created `Graph` 97 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 98 */ 99 Graph CreateGraph() const; 100 101 /** 102 * @brief Sets graph for Function. 103 * @param [ in ] graph - Graph to be set. 104 * @return New state of Function 105 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if Function is false. 106 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if Graph itself is false. 107 * @note Set `ABCKIT_STATUS_WRONG_CTX` error if corresponding `File`s owning `function` and `graph` are 108 * differ. 109 * @note Allocates 110 */ 111 Function SetGraph(const Graph &graph) const; 112 113 /** 114 * @brief Get the name 115 * @return std::string 116 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 117 */ 118 std::string GetName() const; 119 120 /** 121 * @brief Returns binary file that the current `Function` is a part of. 122 * @return Pointer to the `File`. It should be nullptr if current `Function` is false. 123 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 124 */ 125 const File *GetFile() const; 126 127 /** 128 * @brief Get the annotation 129 * @return std::vector<core::Annotation> 130 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 131 */ 132 std::vector<core::Annotation> GetAnnotations() const; 133 134 /** 135 * @brief Tells if method is static. 136 * @return bool 137 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 138 */ 139 bool IsStatic() const; 140 141 /** 142 * @brief Get the Module object 143 * @return `core::Module` 144 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 145 */ 146 core::Module GetModule() const; 147 148 /** 149 * @brief Get the Parent Class object 150 * @return `core::Class` 151 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 152 */ 153 core::Class GetParentClass() const; 154 155 /** 156 * @brief Returns parent function for function. 157 * @return `core::Function`. 158 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 159 */ 160 Function GetParentFunction() const; 161 162 /** 163 * @brief Returns parent namespace for function. 164 * @return `core::Namespace`. 165 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 166 */ 167 core::Namespace GetParentNamespace() const; 168 169 /** 170 * @brief Enumeraterated nested functions 171 * @return `false` if was early exited. Otherwise - `true`. 172 * @param cb - Callback that will be invoked. 173 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 174 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `cb` is false. 175 */ 176 bool EnumerateNestedFunctions(const std::function<bool(core::Function)> &cb) const; 177 178 /** 179 * @brief Enumerates nested classes 180 * @return `false` if was early exited. Otherwise - `true`. 181 * @param cb - Callback that will be invoked. 182 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 183 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `cb` is false. 184 */ 185 bool EnumerateNestedClasses(const std::function<bool(core::Class)> &cb) const; 186 187 /** 188 * @brief Enumerates annotations of Function, invoking callback `cb` for each annotation. 189 * The return value of `cb` used as a signal to continue (true) or early-exit (false) enumeration. 190 * @param cb that will be invoked. 191 * @return `false` if was early exited. Otherwise - `true`. 192 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 193 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `cb` is false. 194 */ 195 bool EnumerateAnnotations(const std::function<bool(core::Annotation)> &cb) const; 196 197 /** 198 * @brief Tells if function is constructor. 199 * @return Returns `true` if function is constructor and `false` otherwise. 200 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 201 */ 202 bool IsCtor() const; 203 204 /** 205 * @brief Tells if function is anonymous. 206 * @return Returns `true` if function is anonymous and `false` otherwise. 207 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 208 */ 209 bool IsAnonymous() const; 210 211 /** 212 * @brief Sets graph for function. 213 * @param [ in ] graph - Graph to be set. 214 * @return New state of Function. 215 * @note Allocates 216 */ 217 Function SetGraph(Graph &graph) const; 218 219 private: Function(AbckitCoreFunction * func,const ApiConfig * conf,const File * file)220 Function(AbckitCoreFunction *func, const ApiConfig *conf, const File *file) : ViewInResource(func), conf_(conf) 221 { 222 SetResource(file); 223 }; 224 const ApiConfig *conf_; 225 226 protected: GetApiConfig()227 const ApiConfig *GetApiConfig() const override 228 { 229 return conf_; 230 } 231 }; 232 233 } // namespace abckit::core 234 235 #endif // CPP_ABCKIT_CORE_FUNCTION_H 236