• 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 #include "program.h"
17 
18 #include <binder/binder.h>
19 #include <ir/astDump.h>
20 
21 namespace panda::es2panda::parser {
22 
Program(ScriptExtension extension)23 Program::Program(ScriptExtension extension)
24     : allocator_(std::make_unique<ArenaAllocator>(SpaceType::SPACE_TYPE_COMPILER, nullptr, true)),
25       binder_(allocator_->New<binder::Binder>(this, extension)),
26       sourceCode_(Allocator()),
27       sourceFile_(Allocator()),
28       extension_(extension)
29 {
30 }
31 
Program(Program && other)32 Program::Program(Program &&other)
33     : allocator_(std::move(other.allocator_)),
34       binder_(other.binder_),
35       ast_(other.ast_),
36       sourceCode_(other.sourceCode_),
37       sourceFile_(other.sourceFile_),
38       recordName_(other.recordName_),
39       formatedRecordName_(other.formatedRecordName_),
40       kind_(other.kind_),
41       extension_(other.extension_),
42       lineIndex_(other.lineIndex_),
43       moduleRecord_(other.moduleRecord_),
44       hotfixHelper_(other.hotfixHelper_),
45       isDtsFile_(other.isDtsFile_)
46 {
47     other.binder_ = nullptr;
48     other.ast_ = nullptr;
49 }
50 
operator =(Program && other)51 Program &Program::operator=(Program &&other)
52 {
53     allocator_ = std::move(other.allocator_);
54     binder_ = other.binder_;
55     ast_ = other.ast_;
56     sourceCode_ = other.sourceCode_;
57     sourceFile_ = other.sourceFile_;
58     kind_ = other.kind_;
59     extension_ = other.extension_;
60     lineIndex_ = other.lineIndex_;
61     isDtsFile_ = other.isDtsFile_;
62 
63     other.ast_ = nullptr;
64 
65     return *this;
66 }
67 
SetKind(ScriptKind kind)68 void Program::SetKind(ScriptKind kind)
69 {
70     kind_ = kind;
71     binder_->InitTopScope();
72 
73     if (kind == ScriptKind::MODULE) {
74         moduleRecord_ = allocator_->New<SourceTextModuleRecord>(Allocator());
75     }
76 }
77 
Dump() const78 std::string Program::Dump() const
79 {
80     ir::AstDumper dumper {ast_, SourceCode()};
81     return dumper.Str();
82 }
83 
84 }  // namespace panda::es2panda::parser
85