1 /*
2  * Copyright (c) 2023 - 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 PANDA_IMPORTEXPORTDECLS_H
17 #define PANDA_IMPORTEXPORTDECLS_H
18 
19 #include "util/errorHandler.h"
20 #include <parser/ETSparser.h>
21 #include "varbinder/ETSBinder.h"
22 #include "compiler/lowering/phase.h"
23 #include "ir/visitor/IterateAstVisitor.h"
24 #include "globalClassHandler.h"
25 
26 namespace ark::es2panda::compiler {
27 
28 class ImportExportDecls : ir::visitor::EmptyAstVisitor {
29     static constexpr std::string_view DEFAULT_IMPORT_SOURCE_FILE = "<default_import>.sts";
30 
CreateDefaultImportSource(const std::vector<std::string> &paths)31     static std::string CreateDefaultImportSource(const std::vector<std::string> &paths)
32     {
33         std::string importStdlibFile;
34         for (const auto &path : paths) {
35             importStdlibFile += "import * from \"" + path + "\";";
36         }
37         return importStdlibFile;
38     }
39 
40     const std::string defaultImportSource_ = CreateDefaultImportSource(util::Helpers::StdLib());
41 
42 public:
43     ImportExportDecls() = default;
ImportExportDecls(varbinder::ETSBinder *varbinder, parser::ETSParser *parser)44     ImportExportDecls(varbinder::ETSBinder *varbinder, parser::ETSParser *parser)
45         : varbinder_(varbinder), parser_(parser)
46     {
47     }
48 
49     /**
50      * Add stdlib names to default imports
51      */
52     void ParseDefaultSources();
53 
54     /**
55      * Verifies import errors, and add Exported flag to top level variables and methods
56      * @param global_stmts program global statements
57      */
58     GlobalClassHandler::ModuleDependencies HandleGlobalStmts(ArenaVector<parser::Program *> &programs);
59     void VerifyTypeExports(const ArenaVector<parser::Program *> &programs);
60     void VerifyType(ir::Statement *stmt, parser::Program *program, std::set<util::StringView> &exportedTypes,
61                     std::set<util::StringView> &exportedStatements,
62                     std::map<util::StringView, ir::AstNode *> &typesMap);
63     void HandleSimpleType(std::set<util::StringView> &exportedTypes, std::set<util::StringView> &exportedStatements,
64                           ir::Statement *stmt, util::StringView name, parser::Program *program,
65                           lexer::SourcePosition pos);
66 
67     void VerifySingleExportDefault(const ArenaVector<parser::Program *> &programs);
68     void HandleSelectiveExportWithAlias(util::StringView originalFieldName, util::StringView exportName,
69                                         lexer::SourcePosition startLoc);
70     void PopulateAliasMap(const ir::ExportNamedDeclaration *decl, const util::StringView &path);
71 
72 private:
73     bool MatchResolvedPathWithProgram(std::string_view resolvedPath, parser::Program *prog);
74     void CollectImportedProgramsFromStmts(ark::es2panda::ir::ETSImportDeclaration *stmt, parser::Program *program,
75                                           GlobalClassHandler::ModuleDependencies *moduleDependencies);
76     void VisitFunctionDeclaration(ir::FunctionDeclaration *funcDecl) override;
77     void VisitVariableDeclaration(ir::VariableDeclaration *varDecl) override;
78     void VisitExportNamedDeclaration(ir::ExportNamedDeclaration *exportDecl) override;
79     void VisitClassDeclaration(ir::ClassDeclaration *classDecl) override;
80     void VisitTSTypeAliasDeclaration(ir::TSTypeAliasDeclaration *typeAliasDecl) override;
81     void VisitTSInterfaceDeclaration(ir::TSInterfaceDeclaration *interfaceDecl) override;
82     void VisitETSImportDeclaration(ir::ETSImportDeclaration *importDecl) override;
83 
84 private:
85     varbinder::ETSBinder *varbinder_ {nullptr};
86     std::map<util::StringView, ir::AstNode *> fieldMap_;
87     std::map<util::StringView, lexer::SourcePosition> exportNameMap_;
88     std::set<util::StringView> exportedTypes_;
89     parser::ETSParser *parser_ {nullptr};
90     std::set<util::StringView> importedSpecifiersForExportCheck_;
91 };
92 }  // namespace ark::es2panda::compiler
93 
94 #endif  // PANDA_IMPORTEXPORTDECLS_H
95