• 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/hotfix.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 
SourceCode()83     util::StringView SourceCode() const
84     {
85         return sourceCode_.View();
86     }
87 
SourceFile()88     util::StringView SourceFile() const
89     {
90         return sourceFile_.View();
91     }
92 
RecordName()93     util::StringView RecordName() const
94     {
95         return recordName_.View();
96     }
97 
FormatedRecordName()98     util::StringView FormatedRecordName() const
99     {
100         return formatedRecordName_.View();
101     }
102 
GetLineIndex()103     const lexer::LineIndex &GetLineIndex() const
104     {
105         return lineIndex_;
106     }
107 
Ast()108     ir::BlockStatement *Ast()
109     {
110         return ast_;
111     }
112 
Ast()113     const ir::BlockStatement *Ast() const
114     {
115         return ast_;
116     }
117 
SetAst(ir::BlockStatement * ast)118     void SetAst(ir::BlockStatement *ast)
119     {
120         ast_ = ast;
121     }
122 
SetSource(const std::string & sourceCode,const std::string & sourceFile,bool isDtsFile)123     void SetSource(const std::string &sourceCode, const std::string &sourceFile, bool isDtsFile)
124     {
125         sourceCode_ = util::UString(sourceCode, Allocator());
126         sourceFile_ = util::UString(sourceFile, Allocator());
127         lineIndex_ = lexer::LineIndex(SourceCode());
128         isDtsFile_ = isDtsFile;
129     }
130 
SetRecordName(const std::string & recordName)131     void SetRecordName(const std::string &recordName)
132     {
133         recordName_ = util::UString(recordName, Allocator());
134         std::string formatedRecordName = recordName + ".";
135         formatedRecordName_ = util::UString(formatedRecordName, Allocator());
136     }
137 
AddHotfixHelper(util::Hotfix * hotfixHelper)138     void AddHotfixHelper(util::Hotfix *hotfixHelper)
139     {
140         hotfixHelper_ = hotfixHelper;
141     }
142 
HotfixHelper()143     util::Hotfix *HotfixHelper()
144     {
145         return hotfixHelper_;
146     }
147 
IsDtsFile()148     bool IsDtsFile() const
149     {
150         return isDtsFile_;
151     }
152 
153     std::string Dump() const;
154     void SetKind(ScriptKind kind);
155 
156 private:
157     std::unique_ptr<ArenaAllocator> allocator_ {};
158     binder::Binder *binder_ {};
159     ir::BlockStatement *ast_ {};
160     util::UString sourceCode_ {};
161     util::UString sourceFile_ {};
162     util::UString recordName_ {};
163     util::UString formatedRecordName_ {};
164     ScriptKind kind_ {};
165     ScriptExtension extension_ {};
166     lexer::LineIndex lineIndex_ {};
167     SourceTextModuleRecord *moduleRecord_ {nullptr};
168     util::Hotfix *hotfixHelper_ {nullptr};
169     bool isDtsFile_ {false};
170 };
171 
172 }  // namespace panda::es2panda::parser
173 
174 #endif
175