• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_COMPILER_CONTEXT_H
17 #define ES2PANDA_COMPILER_CORE_COMPILER_CONTEXT_H
18 
19 #include <mutex>
20 
21 #include "es2panda.h"
22 #include "compiler/base/literals.h"
23 #include "parser/parserImpl.h"
24 
25 namespace panda::es2panda::varbinder {
26 class VarBinder;
27 class FunctionScope;
28 }  // namespace panda::es2panda::varbinder
29 
30 namespace panda::es2panda::checker {
31 class Checker;
32 }  // namespace panda::es2panda::checker
33 
34 namespace panda::es2panda::compiler {
35 class Literal;
36 class DebugInfo;
37 class Emitter;
38 class CodeGen;
39 class ProgramElement;
40 class AstCompiler;
41 
42 class CompilerContext final {
43 public:
44     using CodeGenCb =
45         std::function<void(compiler::CompilerContext *context, varbinder::FunctionScope *, compiler::ProgramElement *)>;
46 
CompilerContext(varbinder::VarBinder * varbinder,checker::Checker * checker,CompilerOptions options,CodeGenCb codeGenCb)47     CompilerContext(varbinder::VarBinder *varbinder, checker::Checker *checker, CompilerOptions options,
48                     CodeGenCb codeGenCb)
49         : varbinder_(varbinder), checker_(checker), options_(std::move(options)), codeGenCb_(std::move(codeGenCb))
50     {
51     }
52 
53     CompilerContext() = delete;
54     NO_COPY_SEMANTIC(CompilerContext);
55     NO_MOVE_SEMANTIC(CompilerContext);
56     ~CompilerContext() = default;
57 
VarBinder()58     [[nodiscard]] varbinder::VarBinder *VarBinder() const noexcept
59     {
60         return varbinder_;
61     }
62 
Checker()63     [[nodiscard]] checker::Checker *Checker() const noexcept
64     {
65         return checker_;
66     }
67 
GetParser()68     [[nodiscard]] parser::ParserImpl *GetParser() const noexcept
69     {
70         return parser_;
71     }
72 
SetParser(parser::ParserImpl * const parser)73     void SetParser(parser::ParserImpl *const parser) noexcept
74     {
75         parser_ = parser;
76     }
77 
GetEmitter()78     [[nodiscard]] Emitter *GetEmitter() const noexcept
79     {
80         return emitter_;
81     }
82 
SetEmitter(Emitter * emitter)83     void SetEmitter(Emitter *emitter) noexcept
84     {
85         emitter_ = emitter;
86     }
87 
GetCodeGenCb()88     [[nodiscard]] const CodeGenCb &GetCodeGenCb() const noexcept
89     {
90         return codeGenCb_;
91     }
92 
AddContextLiteral(LiteralBuffer && literals)93     [[nodiscard]] int32_t AddContextLiteral(LiteralBuffer &&literals)
94     {
95         buffStorage_.emplace_back(std::move(literals));
96         return buffStorage_.size() - 1;
97     }
98 
ContextLiterals()99     [[nodiscard]] const std::vector<LiteralBuffer> &ContextLiterals() const noexcept
100     {
101         return buffStorage_;
102     }
103 
Options()104     [[nodiscard]] const CompilerOptions *Options() const noexcept
105     {
106         return &options_;
107     }
108 
IsDebug()109     [[nodiscard]] bool IsDebug() const noexcept
110     {
111         return options_.isDebug;
112     }
113 
DumpDebugInfo()114     [[nodiscard]] bool DumpDebugInfo() const noexcept
115     {
116         return options_.dumpDebugInfo;
117     }
118 
IsDirectEval()119     [[nodiscard]] bool IsDirectEval() const noexcept
120     {
121         return options_.isDirectEval;
122     }
123 
IsFunctionEval()124     [[nodiscard]] bool IsFunctionEval() const noexcept
125     {
126         return options_.isFunctionEval;
127     }
128 
IsEval()129     [[nodiscard]] bool IsEval() const noexcept
130     {
131         return options_.isEval;
132     }
133 
134 private:
135     varbinder::VarBinder *varbinder_;
136     checker::Checker *checker_;
137     parser::ParserImpl *parser_ = nullptr;
138     Emitter *emitter_ {};
139     std::vector<LiteralBuffer> buffStorage_;
140     CompilerOptions options_;
141     CodeGenCb codeGenCb_ {};
142 };
143 }  // namespace panda::es2panda::compiler
144 
145 #endif
146