1/**
2 * Copyright (c) 2021-2022 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 <macros.h>
20#include <mem/arena_allocator.h>
21
22#include <binder/variable.h>
23#include <ir/astNode.h>
24#include <util/patchFix.h>
25#include <util/ustring.h>
26
27#include <cstdint>
28#include <mutex>
29
30namespace panda::es2panda::binder {
31class Binder;
32}  // namespace panda::es2panda::binder
33
34namespace panda::es2panda::compiler {
35
36class DebugInfo;
37class Emitter;
38
39class CompilerContext {
40public:
41    CompilerContext(binder::Binder *binder, bool isDebug, bool isDebuggerEvaluateExpressionMode,
42                    bool isMergeAbc, bool isJsonInputFile, bool isRecordDebugSource,
43                    const std::string &sourceFile, const std::string &pkgName, util::StringView recordName,
44                    util::PatchFix *patchFixHelper);
45
46    NO_COPY_SEMANTIC(CompilerContext);
47    NO_MOVE_SEMANTIC(CompilerContext);
48    ~CompilerContext() = default;
49
50    binder::Binder *Binder() const
51    {
52        return binder_;
53    }
54
55    Emitter *GetEmitter() const
56    {
57        return emitter_.get();
58    }
59
60    int32_t LiteralCount() const
61    {
62        return literalBufferIdx_;
63    }
64
65    int32_t NewLiteralIndex()
66    {
67        std::lock_guard lock(m_);
68        return literalBufferIdx_++;
69    }
70
71    std::mutex &Mutex()
72    {
73        return m_;
74    }
75
76    bool IsDebug() const
77    {
78        return isDebug_;
79    }
80
81    bool isDebuggerEvaluateExpressionMode() const
82    {
83        return isDebuggerEvaluateExpressionMode_;
84    }
85
86    bool IsMergeAbc() const
87    {
88        return isMergeAbc_;
89    }
90
91    std::string SourceFile() const
92    {
93        return sourceFile_;
94    }
95
96    std::string PkgName() const
97    {
98        return pkgName_;
99    }
100
101    util::PatchFix *PatchFixHelper() const
102    {
103        return patchFixHelper_;
104    }
105
106    const util::StringView &RecordName() const
107    {
108        return recordName_;
109    }
110
111    bool IsJsonInputFile() const
112    {
113        return isJsonInputFile_;
114    }
115
116    bool IsRecordDebugSource() const
117    {
118        return isRecordDebugSource_;
119    }
120
121private:
122    binder::Binder *binder_;
123    int32_t literalBufferIdx_ {0};
124    std::mutex m_;
125    bool isDebug_;
126    bool isDebuggerEvaluateExpressionMode_;
127    bool isMergeAbc_;
128    // true when input file is json file
129    bool isJsonInputFile_;
130    bool isRecordDebugSource_;
131    std::string sourceFile_;
132    std::string pkgName_;
133    util::StringView recordName_;
134    util::PatchFix *patchFixHelper_ {nullptr};
135    std::unique_ptr<Emitter> emitter_;
136};
137
138}  // namespace panda::es2panda::compiler
139
140#endif
141