• 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 <ir/astDump.h>
19 
20 namespace panda::es2panda::parser {
21 
Program(ScriptExtension extension)22 Program::Program(ScriptExtension extension)
23     : allocator_(std::make_unique<ArenaAllocator>(SpaceType::SPACE_TYPE_COMPILER, nullptr, true)),
24       binder_(allocator_->New<binder::Binder>(this, extension)),
25       sourceCode_(Allocator()),
26       sourceFile_(Allocator()),
27       extension_(extension)
28 {
29 }
30 
Program(Program && other)31 Program::Program(Program &&other)
32     : allocator_(std::move(other.allocator_)),
33       binder_(other.binder_),
34       ast_(other.ast_),
35       sourceCode_(other.sourceCode_),
36       sourceFile_(other.sourceFile_),
37       recordName_(other.recordName_),
38       formatedRecordName_(other.formatedRecordName_),
39       kind_(other.kind_),
40       extension_(other.extension_),
41       lineIndex_(std::move(other.lineIndex_)),
42       moduleRecord_(other.moduleRecord_),
43       typeModuleRecord_(other.typeModuleRecord_),
44       patchFixHelper_(other.patchFixHelper_),
45       isDtsFile_(other.isDtsFile_),
46       hasTLA_(other.hasTLA_),
47       isDebug_(other.isDebug_),
48       targetApiVersion_(other.targetApiVersion_),
49       useDefineSemantic_(other.useDefineSemantic_),
50       isShared_(other.isShared_),
51       targetApiSubVersion_(other.targetApiSubVersion_),
52       moduleRecordFieldName_(other.moduleRecordFieldName_),
53       sourceLang_(other.sourceLang_)
54 {
55     other.binder_ = nullptr;
56     other.ast_ = nullptr;
57     other.moduleRecord_ = nullptr;
58     other.patchFixHelper_ = nullptr;
59     other.typeModuleRecord_ = nullptr;
60 }
61 
operator =(Program && other)62 Program &Program::operator=(Program &&other)
63 {
64     if (this == &other) {
65         return *this;
66     }
67     allocator_ = std::move(other.allocator_);
68     binder_ = other.binder_;
69     ast_ = other.ast_;
70     sourceCode_ = other.sourceCode_;
71     sourceFile_ = other.sourceFile_;
72     kind_ = other.kind_;
73     extension_ = other.extension_;
74     lineIndex_ = other.lineIndex_;
75     isDtsFile_ = other.isDtsFile_;
76     hasTLA_ = other.hasTLA_;
77     isDebug_ = other.isDebug_;
78     targetApiVersion_ = other.targetApiVersion_;
79     useDefineSemantic_ = other.useDefineSemantic_;
80     isShared_ = other.isShared_;
81     targetApiSubVersion_ = other.targetApiSubVersion_;
82     moduleRecordFieldName_ = other.moduleRecordFieldName_;
83     sourceLang_ = other.sourceLang_;
84 
85     other.ast_ = nullptr;
86     other.binder_ = nullptr;
87     other.moduleRecord_ = nullptr;
88     other.patchFixHelper_ = nullptr;
89     other.typeModuleRecord_ = nullptr;
90 
91     return *this;
92 }
93 
SetKind(ScriptKind kind)94 void Program::SetKind(ScriptKind kind)
95 {
96     kind_ = kind;
97     binder_->InitTopScope();
98 
99     if (kind == ScriptKind::MODULE) {
100         moduleRecord_ = allocator_->New<SourceTextModuleRecord>(Allocator());
101         typeModuleRecord_ = allocator_->New<SourceTextModuleRecord>(Allocator());
102     }
103 }
104 
Dump() const105 std::string Program::Dump() const
106 {
107     ir::AstDumper dumper {ast_, SourceCode()};
108     return dumper.Str();
109 }
110 
111 }  // namespace panda::es2panda::parser
112