• 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 "macros.h"
20 #include "mem/pool_manager.h"
21 #include "os/filesystem.h"
22 #include "util/ustring.h"
23 #include "util/path.h"
24 #include "varbinder/varbinder.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::varbinder {
33 class VarBinder;
34 }  // namespace panda::es2panda::varbinder
35 
36 namespace panda::es2panda::parser {
37 enum class ScriptKind { SCRIPT, MODULE, STDLIB };
38 
39 class Program {
40 public:
41     using ExternalSource = ArenaUnorderedMap<util::StringView, ArenaVector<Program *>>;
42     template <typename T>
NewProgram(ArenaAllocator * allocator)43     static Program NewProgram(ArenaAllocator *allocator)
44     {
45         auto *varbinder = allocator->New<T>(allocator);
46         return Program(allocator, varbinder);
47     }
48 
Program(ArenaAllocator * allocator,varbinder::VarBinder * varbinder)49     Program(ArenaAllocator *allocator, varbinder::VarBinder *varbinder)
50         : allocator_(allocator),
51           varbinder_(varbinder),
52           externalSources_(allocator_->Adapter()),
53           extension_(varbinder->Extension())
54     {
55     }
56 
SetKind(ScriptKind kind)57     void SetKind(ScriptKind kind)
58     {
59         kind_ = kind;
60     }
61 
62     NO_COPY_SEMANTIC(Program);
63     DEFAULT_MOVE_SEMANTIC(Program);
64 
65     ~Program() = default;
66 
Allocator()67     ArenaAllocator *Allocator() const
68     {
69         return allocator_;
70     }
71 
VarBinder()72     const varbinder::VarBinder *VarBinder() const
73     {
74         return varbinder_;
75     }
76 
VarBinder()77     varbinder::VarBinder *VarBinder()
78     {
79         return varbinder_;
80     }
81 
Extension()82     ScriptExtension Extension() const
83     {
84         return extension_;
85     }
86 
Kind()87     ScriptKind Kind() const
88     {
89         return kind_;
90     }
91 
SourceCode()92     util::StringView SourceCode() const
93     {
94         return sourceCode_;
95     }
96 
SourceFilePath()97     util::StringView SourceFilePath() const
98     {
99         return sourceFilePath_.GetPath();
100     }
101 
SourceFileFolder()102     util::StringView SourceFileFolder() const
103     {
104         return sourceFileFolder_;
105     }
106 
FileName()107     util::StringView FileName() const
108     {
109         return fileName_;
110     }
111 
AbsoluteName()112     util::StringView AbsoluteName() const
113     {
114         return absoluteName_;
115     }
116 
ResolvedFilePath()117     util::StringView ResolvedFilePath() const
118     {
119         return resolvedFilePath_;
120     }
121 
Ast()122     ir::BlockStatement *Ast()
123     {
124         return ast_;
125     }
126 
Ast()127     const ir::BlockStatement *Ast() const
128     {
129         return ast_;
130     }
131 
SetAst(ir::BlockStatement * ast)132     void SetAst(ir::BlockStatement *ast)
133     {
134         ast_ = ast;
135     }
136 
GlobalClass()137     ir::ClassDefinition *GlobalClass()
138     {
139         return globalClass_;
140     }
141 
GlobalClass()142     const ir::ClassDefinition *GlobalClass() const
143     {
144         return globalClass_;
145     }
146 
SetGlobalClass(ir::ClassDefinition * globalClass)147     void SetGlobalClass(ir::ClassDefinition *globalClass)
148     {
149         globalClass_ = globalClass;
150     }
151 
ExternalSources()152     ExternalSource &ExternalSources()
153     {
154         return externalSources_;
155     }
156 
ExternalSources()157     const ExternalSource &ExternalSources() const
158     {
159         return externalSources_;
160     }
161 
SetSource(const util::StringView & sourceCode,const util::StringView & sourceFilePath,const util::StringView & sourceFileFolder)162     void SetSource(const util::StringView &sourceCode, const util::StringView &sourceFilePath,
163                    const util::StringView &sourceFileFolder)
164     {
165         sourceCode_ = sourceCode;
166         sourceFilePath_ = util::Path(sourceFilePath, Allocator());
167         sourceFileFolder_ = sourceFileFolder;
168         absoluteName_ = util::UString(os::GetAbsolutePath(sourceFilePath.Utf8()), Allocator()).View();
169     }
170 
SetSource(const panda::es2panda::SourceFile & sourceFile)171     void SetSource(const panda::es2panda::SourceFile &sourceFile)
172     {
173         sourceCode_ = util::UString(sourceFile.source, Allocator()).View();
174         sourceFilePath_ = util::Path(sourceFile.filePath, Allocator());
175         sourceFileFolder_ = util::UString(sourceFile.fileFolder, Allocator()).View();
176         absoluteName_ = sourceFilePath_.GetAbsolutePath();
177         resolvedFilePath_ = util::UString(sourceFile.resolvedPath, Allocator()).View();
178     }
179 
GetPackageName()180     const util::StringView &GetPackageName() const
181     {
182         return packageName_;
183     }
184 
SetPackageName(util::StringView packageName)185     void SetPackageName(util::StringView packageName)
186     {
187         packageName_ = packageName;
188     }
189 
SetFileName(util::StringView fileName)190     void SetFileName(util::StringView fileName)
191     {
192         fileName_ = util::UString(fileName, Allocator()).View();
193     }
194 
SetAbsoluteName(util::StringView absouleName)195     void SetAbsoluteName(util::StringView absouleName)
196     {
197         absoluteName_ = util::UString(absouleName, Allocator()).View();
198     }
199 
IsEntryPoint()200     const bool &IsEntryPoint() const
201     {
202         return entryPoint_;
203     }
204 
MarkEntry()205     void MarkEntry()
206     {
207         entryPoint_ = true;
208     }
209 
210     varbinder::ClassScope *GlobalClassScope();
211     const varbinder::ClassScope *GlobalClassScope() const;
212 
213     varbinder::GlobalScope *GlobalScope();
214     const varbinder::GlobalScope *GlobalScope() const;
215 
216     util::StringView PackageClassName(util::StringView className);
217 
218     std::string Dump() const;
219 
220     void DumpSilent() const;
221 
222 private:
223     ArenaAllocator *allocator_ {};
224     varbinder::VarBinder *varbinder_ {};
225     ir::BlockStatement *ast_ {};
226     ir::ClassDefinition *globalClass_ {};
227     util::StringView sourceCode_ {};
228     util::Path sourceFilePath_ {};
229     util::StringView sourceFileFolder_ {};
230     util::StringView packageName_ {};
231     util::StringView fileName_ {};
232     util::StringView absoluteName_ {};
233     util::StringView resolvedFilePath_ {};
234     ExternalSource externalSources_;
235     ScriptKind kind_ {};
236     ScriptExtension extension_ {};
237     bool entryPoint_ {};
238 };
239 }  // namespace panda::es2panda::parser
240 
241 #endif
242