1 /**
2 * Copyright (c) 2021-2025 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 enableAnnotations_(other.enableAnnotations_),
52 enableEtsImplements_(other.enableEtsImplements_),
53 targetApiSubVersion_(other.targetApiSubVersion_),
54 moduleRecordFieldName_(other.moduleRecordFieldName_),
55 sourceLang_(other.sourceLang_)
56 {
57 other.binder_ = nullptr;
58 other.ast_ = nullptr;
59 other.moduleRecord_ = nullptr;
60 other.patchFixHelper_ = nullptr;
61 other.typeModuleRecord_ = nullptr;
62 }
63
operator =(Program && other)64 Program &Program::operator=(Program &&other)
65 {
66 if (this == &other) {
67 return *this;
68 }
69 allocator_ = std::move(other.allocator_);
70 binder_ = other.binder_;
71 ast_ = other.ast_;
72 sourceCode_ = other.sourceCode_;
73 sourceFile_ = other.sourceFile_;
74 kind_ = other.kind_;
75 extension_ = other.extension_;
76 lineIndex_ = other.lineIndex_;
77 isDtsFile_ = other.isDtsFile_;
78 hasTLA_ = other.hasTLA_;
79 isDebug_ = other.isDebug_;
80 targetApiVersion_ = other.targetApiVersion_;
81 useDefineSemantic_ = other.useDefineSemantic_;
82 isShared_ = other.isShared_;
83 enableAnnotations_ = other.enableAnnotations_;
84 enableEtsImplements_ = other.enableEtsImplements_;
85 targetApiSubVersion_ = other.targetApiSubVersion_;
86 moduleRecordFieldName_ = other.moduleRecordFieldName_;
87 sourceLang_ = other.sourceLang_;
88
89 other.ast_ = nullptr;
90 other.binder_ = nullptr;
91 other.moduleRecord_ = nullptr;
92 other.patchFixHelper_ = nullptr;
93 other.typeModuleRecord_ = nullptr;
94
95 return *this;
96 }
97
SetKind(ScriptKind kind)98 void Program::SetKind(ScriptKind kind)
99 {
100 kind_ = kind;
101 binder_->InitTopScope();
102
103 if (kind == ScriptKind::MODULE) {
104 moduleRecord_ = allocator_->New<SourceTextModuleRecord>(Allocator());
105 typeModuleRecord_ = allocator_->New<SourceTextModuleRecord>(Allocator());
106 }
107 }
108
Dump() const109 std::string Program::Dump() const
110 {
111 ir::AstDumper dumper {ast_, SourceCode()};
112 return dumper.Str();
113 }
114
115 } // namespace panda::es2panda::parser
116