• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef ES2PANDA_PUBLIC_PUBLIC_H
17 #define ES2PANDA_PUBLIC_PUBLIC_H
18 
19 #include <unordered_map>
20 #include "public/es2panda_lib.h"
21 
22 #include "assembler/assembly-program.h"
23 #include "libpandabase/mem/arena_allocator.h"
24 
25 #include "compiler/core/compileQueue.h"
26 #include "parser/ETSparser.h"
27 #include "checker/checker.h"
28 #include "checker/IsolatedDeclgenChecker.h"
29 #include "compiler/core/emitter.h"
30 
31 namespace ark::es2panda::util {
32 class Options;
33 }  // namespace ark::es2panda::util
34 
35 namespace ark::es2panda::compiler {
36 class PhaseManager;
37 }  // namespace ark::es2panda::compiler
38 
39 namespace ark::es2panda::public_lib {
40 
41 enum class CompilingState : unsigned int {
42     NONE_COMPILING = 0,
43     SINGLE_COMPILING = 1,
44     MULTI_COMPILING_INIT = 2,
45     MULTI_COMPILING_FOLLOW = 3,
46 };
47 
48 struct ConfigImpl {
49     const util::Options *options = nullptr;
50     util::DiagnosticEngine *diagnosticEngine = nullptr;
51     std::list<diagnostic::DiagnosticKind> diagnosticKindStorage;
52 };
53 
54 using ExternalSources = std::unordered_map<util::StringView, ArenaVector<parser::Program *>>;
55 using ExternalSource = ArenaUnorderedMap<util::StringView, ArenaVector<parser::Program *>>;
56 using ComputedAbstracts =
57     ArenaUnorderedMap<checker::ETSObjectType *,
58                       std::pair<ArenaVector<checker::ETSFunctionType *>, ArenaUnorderedSet<checker::ETSObjectType *>>>;
59 
60 class TransitionMemory {
61 public:
TransitionMemory(ArenaAllocator * allocator)62     explicit TransitionMemory(ArenaAllocator *allocator)
63         : permanentAllocator_(allocator), compiledPrograms_(allocator->Adapter())
64     {
65         compiledPrograms_ = {};
66     }
67 
68     NO_COPY_SEMANTIC(TransitionMemory);
69     DEFAULT_MOVE_SEMANTIC(TransitionMemory);
70 
71     ~TransitionMemory() = default;
72 
PermanentAllocator()73     ArenaAllocator *PermanentAllocator() const
74     {
75         return permanentAllocator_.get();
76     }
77 
VarBinder()78     const varbinder::VarBinder *VarBinder() const
79     {
80         return varbinder_;
81     }
82 
VarBinder()83     varbinder::VarBinder *VarBinder()
84     {
85         return varbinder_;
86     }
87 
SetVarBinder(varbinder::VarBinder * varbinder)88     void SetVarBinder(varbinder::VarBinder *varbinder)
89     {
90         varbinder_ = varbinder;
91     }
92 
GlobalTypes()93     const checker::GlobalTypesHolder *GlobalTypes() const
94     {
95         return globalTypes_;
96     }
97 
GlobalTypes()98     checker::GlobalTypesHolder *GlobalTypes()
99     {
100         return globalTypes_;
101     }
102 
SetGlobalTypes(checker::GlobalTypesHolder * globalTypes)103     void SetGlobalTypes(checker::GlobalTypesHolder *globalTypes)
104     {
105         globalTypes_ = globalTypes;
106     }
107 
AddCompiledProgram(parser::Program * program)108     void AddCompiledProgram(parser::Program *program)
109     {
110         compiledPrograms_.push_back(program);
111     }
112 
CompiledSources()113     ArenaVector<parser::Program *> &CompiledSources()
114     {
115         return compiledPrograms_;
116     }
117 
CompiledPrograms()118     const ArenaVector<parser::Program *> &CompiledPrograms() const
119     {
120         return compiledPrograms_;
121     }
122 
CachedComputedAbstracts()123     const ComputedAbstracts *CachedComputedAbstracts() const
124     {
125         return cachedComputedAbstracts_;
126     }
127 
CachedComputedAbstracts()128     ComputedAbstracts *CachedComputedAbstracts()
129     {
130         return cachedComputedAbstracts_;
131     }
132 
SetCachechedComputedAbstracts(ComputedAbstracts * cachedComputedAbstracts)133     void SetCachechedComputedAbstracts(ComputedAbstracts *cachedComputedAbstracts)
134     {
135         cachedComputedAbstracts_ = cachedComputedAbstracts;
136     }
137 
138 private:
139     std::unique_ptr<ArenaAllocator> permanentAllocator_;
140     ArenaVector<parser::Program *> compiledPrograms_;
141     varbinder::VarBinder *varbinder_ {nullptr};
142     checker::GlobalTypesHolder *globalTypes_ {nullptr};
143     ComputedAbstracts *cachedComputedAbstracts_ {nullptr};
144 };
145 
146 struct Context {
147     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
148     using CodeGenCb =
149         std::function<void(public_lib::Context *context, varbinder::FunctionScope *, compiler::ProgramElement *)>;
150 
AllocatorContext151     ArenaAllocator *Allocator() const
152     {
153         return allocator;
154     }
155 
156     template <typename T, typename... Args>
AllocNodeContext157     T *AllocNode(Args &&...args)
158     {
159         // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint)
160         return util::NodeAllocator::ForceSetParent<T>(Allocator(), std::forward<Args>(args)...);
161     }
162 
MarkGenAbcForExternalContext163     void MarkGenAbcForExternal(std::unordered_set<std::string> &genAbcList, public_lib::ExternalSource &extSources)
164     {
165         size_t genCount = 0;
166         std::unordered_set<std::string> genAbcListAbsolute;
167 
168         for (auto &path : genAbcList) {
169             genAbcListAbsolute.insert(os::GetAbsolutePath(path));
170         }
171         for (auto &[_, extPrograms] : extSources) {
172             (void)_;
173             bool setFlag = false;
174             for (auto *prog : extPrograms) {
175                 if (auto it = genAbcListAbsolute.find(prog->AbsoluteName().Mutf8()); it != genAbcListAbsolute.end()) {
176                     ++genCount;
177                     setFlag = true;
178                 }
179             }
180             if (!setFlag) {
181                 continue;
182             }
183             for (auto *prog : extPrograms) {
184                 prog->SetGenAbcForExternalSources();
185             }
186         }
187 
188         if (genCount != genAbcListAbsolute.size()) {
189             diagnosticEngine->LogFatalError(diagnostic::SIMULTANEOUSLY_MARK_FAILED.Message());
190         }
191     }
192 
193     ConfigImpl *config = nullptr;
194     std::string sourceFileName;
195     std::string input;
196     SourceFile const *sourceFile = nullptr;
197     ArenaAllocator *allocator = nullptr;
198     compiler::CompileQueue *queue = nullptr;
199     std::vector<util::Plugin> const *plugins = nullptr;
200     std::vector<compiler::LiteralBuffer> contextLiterals;
201     CodeGenCb codeGenCb;
202     compiler::PhaseManager *phaseManager = nullptr;
203 
204     parser::Program *parserProgram = nullptr;
205     parser::ParserImpl *parser = nullptr;
206     checker::Checker *checker = nullptr;
207     checker::IsolatedDeclgenChecker *isolatedDeclgenChecker = nullptr;
208 
209     checker::SemanticAnalyzer *analyzer = nullptr;
210     compiler::Emitter *emitter = nullptr;
211     pandasm::Program *program = nullptr;
212     util::DiagnosticEngine *diagnosticEngine = nullptr;
213 
214     es2panda_ContextState state = ES2PANDA_STATE_NEW;
215     std::string errorMessage;
216     lexer::SourcePosition errorPos;
217 
218     CompilingState compilingState {CompilingState::NONE_COMPILING};
219     ExternalSources externalSources;
220     TransitionMemory *transitionMemory {nullptr};
221     std::vector<std::string> sourceFileNames;
222     std::map<util::StringView, parser::Program *> dupPrograms {};
223     // NOLINTEND(misc-non-private-member-variables-in-classes)
224 };
225 }  // namespace ark::es2panda::public_lib
226 
227 #endif
228