• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 - 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 
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->IsPackage(); })) {
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 
CheckFileHeaderFlag(parser::Program * program)51 static void CheckFileHeaderFlag(parser::Program *program)
52 {
53     auto &statements = program->Ast()->Statements();
54     if (statements.empty()) {
55         return;
56     }
57 
58     if (!statements.front()->IsExpressionStatement()) {
59         return;
60     }
61 
62     // If further processing based on "use static" is required later, such as throwing a warning or modifying the node,
63     // perform the operation here.
64     auto *expansion = statements.front()->AsExpressionStatement()->GetExpression();
65     if (expansion->IsStringLiteral() && expansion->AsStringLiteral()->Str() == Signatures::STATIC_PROGRAM_FLAG) {
66         statements.erase(statements.begin());
67         return;
68     }
69 }
70 
Perform(public_lib::Context * ctx,parser::Program * program)71 bool TopLevelStatements::Perform(public_lib::Context *ctx, parser::Program *program)
72 {
73     CheckFileHeaderFlag(program);
74     auto imports = ImportExportDecls(program->VarBinder()->AsETSBinder(), ctx->parser->AsETSParser());
75     imports.ParseDefaultSources();
76     if (!CheckProgramSourcesConsistency(program)) {
77         // NOTE(vpukhov): enforce compilation failure
78     }
79 
80     GlobalClassHandler globalClass(ctx->parser->AsETSParser(), ctx->Allocator());
81     for (auto &[package, extPrograms] : program->ExternalSources()) {
82         auto moduleDependencies = imports.HandleGlobalStmts(extPrograms);
83         globalClass.SetupGlobalClass(extPrograms, &moduleDependencies);
84     }
85 
86     ArenaVector<parser::Program *> mainModule(ctx->Allocator()->Adapter());
87     mainModule.emplace_back(program);
88     auto moduleDependencies = imports.HandleGlobalStmts(mainModule);
89     globalClass.SetupGlobalClass(mainModule, &moduleDependencies);
90     return true;
91 }
92 
93 }  // namespace ark::es2panda::compiler
94