• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 
IsDebug()158     bool IsDebug() const
159     {
160         return isDebug_;
161     }
162 
SetDebug(bool isDebug)163     void SetDebug(bool isDebug)
164     {
165         isDebug_ = isDebug;
166     }
167 
168     std::string Dump() const;
169     void SetKind(ScriptKind kind);
170 
171 private:
172     std::unique_ptr<ArenaAllocator> allocator_ {};
173     binder::Binder *binder_ {};
174     ir::BlockStatement *ast_ {};
175     util::UString sourceCode_ {};
176     util::UString sourceFile_ {};
177     util::UString recordName_ {};
178     util::UString formatedRecordName_ {};
179     ScriptKind kind_ {};
180     ScriptExtension extension_ {};
181     lexer::LineIndex lineIndex_ {};
182     SourceTextModuleRecord *moduleRecord_ {nullptr};
183     SourceTextModuleRecord *typeModuleRecord_ {nullptr};
184     util::PatchFix *patchFixHelper_ {nullptr};
185     bool isDtsFile_ {false};
186     bool isDebug_ {false};
187 };
188 
189 }  // namespace panda::es2panda::parser
190 
191 #endif
192