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 #include "compiler/lowering/ets/topLevelStmts/topLevelStmts.h"
17 
18 #include "compiler/lowering/ets/topLevelStmts/globalClassHandler.h"
19 
20 namespace ark::es2panda::compiler {
21 
CheckSourceConsistency(util::StringView name, ArenaVector<parser::Program *> const &programs)22 static bool CheckSourceConsistency(util::StringView name, ArenaVector<parser::Program *> const &programs)
23 {
24     if (programs.size() == 1) {
25         return true;
26     }
27     if (std::all_of(programs.begin(), programs.end(), [](auto p) { return p->IsPackageModule(); })) {
28         return true;
29     }
30     std::stringstream ss;
31     ss << "Module name \"" << name << "\" is assigned to multiple compilation units:";
32     std::for_each(programs.begin(), programs.end(), [&ss](parser::Program *p) {
33         ss << std::endl << "  at " << p->SourceFilePath().Mutf8();
34     });
35     std::cerr << ss.str() << std::endl;
36     return false;
37 }
38 
CheckProgramSourcesConsistency(parser::Program *program)39 static bool CheckProgramSourcesConsistency(parser::Program *program)
40 {
41     bool success = true;
42     for (auto const &[name, programs] : program->ExternalSources()) {
43         success &= CheckSourceConsistency(name, programs);
44     }
45     for (auto const &[name, programs] : program->DirectExternalSources()) {
46         success &= CheckSourceConsistency(name, programs);
47     }
48     return success;
49 }
50 
Perform(public_lib::Context *ctx, parser::Program *program)51 bool TopLevelStatements::Perform(public_lib::Context *ctx, parser::Program *program)
52 {
53     auto imports = ImportExportDecls(program->VarBinder()->AsETSBinder(), ctx->parser->AsETSParser());
54     imports.ParseDefaultSources();
55     if (!CheckProgramSourcesConsistency(program)) {
56         // NOTE(vpukhov): enforce compilation failure
57     }
58 
59     GlobalClassHandler globalClass(ctx->parser->AsETSParser(), program->Allocator());
60     for (auto &[package, extPrograms] : program->ExternalSources()) {
61         auto moduleDependencies = imports.HandleGlobalStmts(extPrograms);
62         globalClass.SetupGlobalClass(extPrograms, &moduleDependencies);
63     }
64 
65     ArenaVector<parser::Program *> mainModule(program->Allocator()->Adapter());
66     mainModule.emplace_back(program);
67     auto moduleDependencies = imports.HandleGlobalStmts(mainModule);
68     globalClass.SetupGlobalClass(mainModule, &moduleDependencies);
69     return true;
70 }
71 
72 }  // namespace ark::es2panda::compiler
73