1 /** 2 * Copyright (c) 2021-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 ES2PANDA_UTIL_PLUGINS_H 16 #define ES2PANDA_UTIL_PLUGINS_H 17 18 #include "util/es2pandaMacros.h" 19 #include "os/library_loader.h" 20 #include "public/es2panda_lib.h" 21 #include "util/ustring.h" 22 23 namespace ark::es2panda::util { 24 25 class Plugin { 26 public: 27 explicit Plugin(util::StringView const &name); 28 ~Plugin() = default; 29 30 NO_COPY_SEMANTIC(Plugin); 31 DEFAULT_MOVE_SEMANTIC(Plugin); 32 IsOk()33 bool IsOk() 34 { 35 return ok_; 36 } 37 Error()38 os::Error Error() 39 { 40 return err_; 41 } 42 Initialize()43 void Initialize() const 44 { 45 if (initialize_ != nullptr) { 46 initialize_(); 47 } 48 } 49 AfterParse(es2panda_Context * context)50 void AfterParse(es2panda_Context *context) const 51 { 52 if (afterParse_ != nullptr) { 53 afterParse_(context); 54 } 55 } 56 AfterBind(es2panda_Context * context)57 void AfterBind(es2panda_Context *context) const 58 { 59 if (afterBind_ != nullptr) { 60 afterBind_(context); 61 } 62 } 63 AfterCheck(es2panda_Context * context)64 void AfterCheck(es2panda_Context *context) const 65 { 66 if (afterCheck_ != nullptr) { 67 afterCheck_(context); 68 } 69 } 70 AfterLowerings(es2panda_Context * context)71 void AfterLowerings(es2panda_Context *context) const 72 { 73 if (afterLowerings_ != nullptr) { 74 afterLowerings_(context); 75 } 76 } 77 78 private: 79 std::string FullNameForProcedure(std::string const &shortName); 80 81 util::StringView name_; 82 bool ok_ {true}; 83 os::Error err_; 84 os::library_loader::LibraryHandle h_; 85 86 void (*initialize_)() = nullptr; 87 void (*afterParse_)(es2panda_Context *) = nullptr; 88 void (*afterBind_)(es2panda_Context *) = nullptr; 89 void (*afterCheck_)(es2panda_Context *) = nullptr; 90 void (*afterLowerings_)(es2panda_Context *) = nullptr; 91 }; 92 93 } // namespace ark::es2panda::util 94 95 #endif 96