1/**
2 * Copyright (c) 2021 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_PARSER_INCLUDE_PROGRAM_H
17#define ES2PANDA_PARSER_INCLUDE_PROGRAM_H
18
19#include <lexer/token/sourceLocation.h>
20#include <macros.h>
21#include <mem/arena_allocator.h>
22#include <parser/module/sourceTextModuleRecord.h>
23#include <util/patchFix.h>
24#include <util/ustring.h>
25
26#include "es2panda.h"
27
28namespace panda::es2panda::ir {
29class BlockStatement;
30}  // namespace panda::es2panda::ir
31
32namespace panda::es2panda::binder {
33class Binder;
34}  // namespace panda::es2panda::binder
35
36namespace panda::es2panda::parser {
37
38enum class ScriptKind { SCRIPT, MODULE, COMMONJS };
39
40class Program {
41public:
42    explicit Program(es2panda::ScriptExtension extension);
43    NO_COPY_SEMANTIC(Program);
44    Program(Program &&other);
45    Program &operator=(Program &&other);
46    ~Program() = default;
47
48    ArenaAllocator *Allocator() const
49    {
50        return allocator_.get();
51    }
52
53    const binder::Binder *Binder() const
54    {
55        return binder_;
56    }
57
58    binder::Binder *Binder()
59    {
60        return binder_;
61    }
62
63    ScriptExtension Extension() const
64    {
65        return extension_;
66    }
67
68    ScriptKind Kind() const
69    {
70        return kind_;
71    }
72
73    bool IsCommonjs() const
74    {
75        return kind_ == ScriptKind::COMMONJS;
76    }
77
78    SourceTextModuleRecord *ModuleRecord() const
79    {
80        return moduleRecord_;
81    }
82
83    SourceTextModuleRecord *TypeModuleRecord() const
84    {
85        return typeModuleRecord_;
86    }
87
88    util::StringView SourceCode() const
89    {
90        return sourceCode_.View();
91    }
92
93    util::StringView SourceFile() const
94    {
95        return sourceFile_.View();
96    }
97
98    util::StringView RecordName() const
99    {
100        return recordName_.View();
101    }
102
103    util::StringView FormatedRecordName() const
104    {
105        return formatedRecordName_.View();
106    }
107
108    const lexer::LineIndex &GetLineIndex() const
109    {
110        return lineIndex_;
111    }
112
113    ir::BlockStatement *Ast()
114    {
115        return ast_;
116    }
117
118    const ir::BlockStatement *Ast() const
119    {
120        return ast_;
121    }
122
123    void SetAst(ir::BlockStatement *ast)
124    {
125        ast_ = ast;
126    }
127
128    void SetSource(const std::string &sourceCode, const std::string &sourceFile, bool isDtsFile)
129    {
130        sourceCode_ = util::UString(sourceCode, Allocator());
131        sourceFile_ = util::UString(sourceFile, Allocator());
132        lineIndex_ = lexer::LineIndex(SourceCode());
133        isDtsFile_ = isDtsFile;
134    }
135
136    void SetRecordName(const std::string &recordName)
137    {
138        recordName_ = util::UString(recordName, Allocator());
139        std::string formatedRecordName = recordName + ".";
140        formatedRecordName_ = util::UString(formatedRecordName, Allocator());
141    }
142
143    void AddPatchFixHelper(util::PatchFix *patchFixHelper)
144    {
145        patchFixHelper_ = patchFixHelper;
146    }
147
148    util::PatchFix *PatchFixHelper()
149    {
150        return patchFixHelper_;
151    }
152
153    bool IsDtsFile() const
154    {
155        return isDtsFile_;
156    }
157
158    bool HasTLA() const
159    {
160        return hasTLA_;
161    }
162
163    void SetHasTLA(bool hasTLA)
164    {
165        hasTLA_ = hasTLA;
166    }
167
168    bool IsDebug() const
169    {
170        return isDebug_;
171    }
172
173    void SetDebug(bool isDebug)
174    {
175        isDebug_ = isDebug;
176    }
177
178    int TargetApiVersion() const
179    {
180        return targetApiVersion_;
181    }
182
183    void SetTargetApiVersion(int targetApiVersion)
184    {
185        targetApiVersion_ = targetApiVersion;
186    }
187
188    void SetTargetApiSubVersion(std::string targetApiSubVersion)
189    {
190        targetApiSubVersion_ = targetApiSubVersion;
191    }
192
193    std::string GetTargetApiSubVersion() const
194    {
195        return targetApiSubVersion_;
196    }
197
198    bool UseDefineSemantic() const
199    {
200        return useDefineSemantic_;
201    }
202
203    void SetDefineSemantic(bool useDefineSemantic)
204    {
205        useDefineSemantic_ = useDefineSemantic;
206    }
207
208    void SetShared(bool isShared)
209    {
210        isShared_ = isShared;
211    }
212
213    bool IsShared() const
214    {
215        return isShared_;
216    }
217
218    void SetModuleRecordFieldName(std::string moduleRecordFieldName)
219    {
220        moduleRecordFieldName_ = moduleRecordFieldName;
221    }
222
223    std::string ModuleRecordFieldName() const
224    {
225        return moduleRecordFieldName_;
226    }
227
228    void SetEnableAnnotations(bool enableAnnotations)
229    {
230        enableAnnotations_ = enableAnnotations;
231    }
232
233    bool IsEnableAnnotations()
234    {
235        return enableAnnotations_;
236    }
237
238    std::string Dump() const;
239    void SetKind(ScriptKind kind);
240
241private:
242    std::unique_ptr<ArenaAllocator> allocator_ {};
243    binder::Binder *binder_ {};
244    ir::BlockStatement *ast_ {};
245    util::UString sourceCode_ {};
246    util::UString sourceFile_ {};
247    util::UString recordName_ {};
248    util::UString formatedRecordName_ {};
249    ScriptKind kind_ {};
250    ScriptExtension extension_ {};
251    lexer::LineIndex lineIndex_ {};
252    SourceTextModuleRecord *moduleRecord_ {nullptr};
253    SourceTextModuleRecord *typeModuleRecord_ {nullptr};
254    util::PatchFix *patchFixHelper_ {nullptr};
255    bool isDtsFile_ {false};
256    bool hasTLA_ {false};
257    bool isDebug_ {false};
258    int targetApiVersion_ {0};
259    bool useDefineSemantic_ {true};
260    bool isShared_ {false};
261    bool enableAnnotations_ {false};
262    std::string targetApiSubVersion_ { util::Helpers::DEFAULT_SUB_API_VERSION };
263    std::string moduleRecordFieldName_;
264};
265
266}  // namespace panda::es2panda::parser
267
268#endif
269