1 /** 2 * Copyright (c) 2021 - 2023 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_COMPILER_CORE_SAVED_BINDINGS_CTX_H 17 #define ES2PANDA_COMPILER_CORE_SAVED_BINDINGS_CTX_H 18 19 #include "varbinder/varbinder.h" 20 #include "ir/module/importDeclaration.h" 21 22 namespace ark::es2panda::compiler { 23 class SavedBindingsContext { 24 public: SavedBindingsContext(varbinder::VarBinder * varbinder)25 explicit SavedBindingsContext(varbinder::VarBinder *varbinder) 26 : varbinder_(varbinder), savedBindings_(varbinder_->GetScope()->Bindings()) 27 { 28 } 29 NO_COPY_SEMANTIC(SavedBindingsContext); 30 NO_MOVE_SEMANTIC(SavedBindingsContext); 31 ~SavedBindingsContext() = default; 32 33 protected: Allocator()34 ArenaAllocator *Allocator() const 35 { 36 return varbinder_->Allocator(); 37 } 38 VarBinder()39 varbinder::VarBinder *VarBinder() const 40 { 41 return varbinder_; 42 } 43 SavedBindings()44 const varbinder::Scope::VariableMap &SavedBindings() const 45 { 46 return savedBindings_; 47 } 48 49 private: 50 varbinder::VarBinder *varbinder_; 51 varbinder::Scope::VariableMap savedBindings_; 52 }; 53 54 class ExportDeclarationContext : public SavedBindingsContext { 55 public: ExportDeclarationContext(varbinder::VarBinder * varbinder)56 explicit ExportDeclarationContext(varbinder::VarBinder *varbinder) : SavedBindingsContext(varbinder) {} 57 NO_COPY_SEMANTIC(ExportDeclarationContext); 58 NO_MOVE_SEMANTIC(ExportDeclarationContext); 59 ~ExportDeclarationContext() = default; 60 61 void BindExportDecl(ir::AstNode *exportDecl); 62 63 protected: 64 static constexpr std::string_view DEFAULT_EXPORT = "*default*"; 65 }; 66 67 class ImportDeclarationContext : public SavedBindingsContext { 68 public: ImportDeclarationContext(varbinder::VarBinder * varbinder)69 explicit ImportDeclarationContext(varbinder::VarBinder *varbinder) : SavedBindingsContext(varbinder) {} 70 71 NO_COPY_SEMANTIC(ImportDeclarationContext); 72 NO_MOVE_SEMANTIC(ImportDeclarationContext); 73 74 ~ImportDeclarationContext() = default; 75 76 void BindImportDecl(ir::ImportDeclaration *importDecl); 77 78 private: 79 }; 80 } // namespace ark::es2panda::compiler 81 82 #endif 83