• 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_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 
28 namespace panda::es2panda::ir {
29 class BlockStatement;
30 }  // namespace panda::es2panda::ir
31 
32 namespace panda::es2panda::binder {
33 class Binder;
34 }  // namespace panda::es2panda::binder
35 
36 namespace panda::es2panda::parser {
37 
38 enum class ScriptKind { SCRIPT, MODULE, COMMONJS };
39 
40 class Program {
41 public:
42     explicit Program(es2panda::ScriptExtension extension);
43     NO_COPY_SEMANTIC(Program);
44     Program(Program &&other);
45     Program &operator=(Program &&other);
46     ~Program() = default;
47 
Allocator()48     ArenaAllocator *Allocator() const
49     {
50         return allocator_.get();
51     }
52 
Binder()53     const binder::Binder *Binder() const
54     {
55         return binder_;
56     }
57 
Binder()58     binder::Binder *Binder()
59     {
60         return binder_;
61     }
62 
Extension()63     ScriptExtension Extension() const
64     {
65         return extension_;
66     }
67 
Kind()68     ScriptKind Kind() const
69     {
70         return kind_;
71     }
72 
IsCommonjs()73     bool IsCommonjs() const
74     {
75         return kind_ == ScriptKind::COMMONJS;
76     }
77 
ModuleRecord()78     SourceTextModuleRecord *ModuleRecord() const
79     {
80         return moduleRecord_;
81     }
82 
TypeModuleRecord()83     SourceTextModuleRecord *TypeModuleRecord() const
84     {
85         return typeModuleRecord_;
86     }
87 
SourceCode()88     util::StringView SourceCode() const
89     {
90         return sourceCode_.View();
91     }
92 
SourceFile()93     util::StringView SourceFile() const
94     {
95         return sourceFile_.View();
96     }
97 
RecordName()98     util::StringView RecordName() const
99     {
100         return recordName_.View();
101     }
102 
FormatedRecordName()103     util::StringView FormatedRecordName() const
104     {
105         return formatedRecordName_.View();
106     }
107 
GetLineIndex()108     const lexer::LineIndex &GetLineIndex() const
109     {
110         return lineIndex_;
111     }
112 
Ast()113     ir::BlockStatement *Ast()
114     {
115         return ast_;
116     }
117 
Ast()118     const ir::BlockStatement *Ast() const
119     {
120         return ast_;
121     }
122 
SetAst(ir::BlockStatement * ast)123     void SetAst(ir::BlockStatement *ast)
124     {
125         ast_ = ast;
126     }
127 
SetSource(const std::string & sourceCode,const std::string & sourceFile,bool isDtsFile)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 
SetRecordName(const std::string & recordName)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 
AddPatchFixHelper(util::PatchFix * patchFixHelper)143     void AddPatchFixHelper(util::PatchFix *patchFixHelper)
144     {
145         patchFixHelper_ = patchFixHelper;
146     }
147 
PatchFixHelper()148     util::PatchFix *PatchFixHelper()
149     {
150         return patchFixHelper_;
151     }
152 
IsDtsFile()153     bool IsDtsFile() const
154     {
155         return isDtsFile_;
156     }
157 
HasTLA()158     bool HasTLA() const
159     {
160         return hasTLA_;
161     }
162 
SetHasTLA(bool hasTLA)163     void SetHasTLA(bool hasTLA)
164     {
165         hasTLA_ = hasTLA;
166     }
167 
IsDebug()168     bool IsDebug() const
169     {
170         return isDebug_;
171     }
172 
SetDebug(bool isDebug)173     void SetDebug(bool isDebug)
174     {
175         isDebug_ = isDebug;
176     }
177 
TargetApiVersion()178     int TargetApiVersion() const
179     {
180         return targetApiVersion_;
181     }
182 
SetTargetApiVersion(int targetApiVersion)183     void SetTargetApiVersion(int targetApiVersion)
184     {
185         targetApiVersion_ = targetApiVersion;
186     }
187 
SetTargetApiSubVersion(std::string targetApiSubVersion)188     void SetTargetApiSubVersion(std::string targetApiSubVersion)
189     {
190         targetApiSubVersion_ = targetApiSubVersion;
191     }
192 
GetTargetApiSubVersion()193     std::string GetTargetApiSubVersion() const
194     {
195         return targetApiSubVersion_;
196     }
197 
UseDefineSemantic()198     bool UseDefineSemantic() const
199     {
200         return useDefineSemantic_;
201     }
202 
SetDefineSemantic(bool useDefineSemantic)203     void SetDefineSemantic(bool useDefineSemantic)
204     {
205         useDefineSemantic_ = useDefineSemantic;
206     }
207 
SetShared(bool isShared)208     void SetShared(bool isShared)
209     {
210         isShared_ = isShared;
211     }
212 
IsShared()213     bool IsShared() const
214     {
215         return isShared_;
216     }
217 
SetModuleRecordFieldName(std::string moduleRecordFieldName)218     void SetModuleRecordFieldName(std::string moduleRecordFieldName)
219     {
220         moduleRecordFieldName_ = moduleRecordFieldName;
221     }
222 
ModuleRecordFieldName()223     std::string ModuleRecordFieldName() const
224     {
225         return moduleRecordFieldName_;
226     }
227 
SetEnableAnnotations(bool enableAnnotations)228     void SetEnableAnnotations(bool enableAnnotations)
229     {
230         enableAnnotations_ = enableAnnotations;
231     }
232 
IsEnableAnnotations()233     bool IsEnableAnnotations() const
234     {
235         return enableAnnotations_;
236     }
237 
SetEnableEtsImplements(bool enableEtsImplements)238     void SetEnableEtsImplements(bool enableEtsImplements)
239     {
240         enableEtsImplements_ = enableEtsImplements;
241     }
242 
IsEnableEtsImplements()243     bool IsEnableEtsImplements() const
244     {
245         return enableEtsImplements_;
246     }
247 
SetSourceLang(const std::string & sourceLang)248     void SetSourceLang(const std::string &sourceLang)
249     {
250         if (sourceLang == "ets") {
251             sourceLang_ = panda::pandasm::extensions::Language::ARKTS;
252         } else if (sourceLang == "ts") {
253             sourceLang_ = panda::pandasm::extensions::Language::TYPESCRIPT;
254         } else if (sourceLang == "js") {
255             sourceLang_ = panda::pandasm::extensions::Language::JAVASCRIPT;
256         } else {
257             sourceLang_ = panda::pandasm::extensions::Language::ECMASCRIPT;
258         }
259     }
260 
SourceLang()261     panda::pandasm::extensions::Language SourceLang() const
262     {
263         return sourceLang_;
264     }
265 
266     std::string Dump() const;
267     void SetKind(ScriptKind kind);
268 
269 private:
270     std::unique_ptr<ArenaAllocator> allocator_ {};
271     binder::Binder *binder_ {};
272     ir::BlockStatement *ast_ {};
273     util::UString sourceCode_ {};
274     util::UString sourceFile_ {};
275     util::UString recordName_ {};
276     util::UString formatedRecordName_ {};
277     ScriptKind kind_ {};
278     ScriptExtension extension_ {};
279     lexer::LineIndex lineIndex_ {};
280     SourceTextModuleRecord *moduleRecord_ {nullptr};
281     SourceTextModuleRecord *typeModuleRecord_ {nullptr};
282     util::PatchFix *patchFixHelper_ {nullptr};
283     bool isDtsFile_ {false};
284     bool hasTLA_ {false};
285     bool isDebug_ {false};
286     int targetApiVersion_ {0};
287     bool useDefineSemantic_ {true};
288     bool isShared_ {false};
289     bool enableAnnotations_ {false};
290     bool enableEtsImplements_ {false};
291     std::string targetApiSubVersion_ { util::Helpers::DEFAULT_SUB_API_VERSION };
292     std::string moduleRecordFieldName_;
293     panda::pandasm::extensions::Language sourceLang_ {panda::pandasm::extensions::DEFAULT_LANGUAGE};
294 };
295 
296 }  // namespace panda::es2panda::parser
297 
298 #endif
299