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 typeModuleRecord_(other.typeModuleRecord_),
45 patchFixHelper_(other.patchFixHelper_),
46 isDtsFile_(other.isDtsFile_),
47 hasTLA_(other.hasTLA_),
48 isDebug_(other.isDebug_),
49 targetApiVersion_(other.targetApiVersion_),
50 useDefineSemantic_(other.useDefineSemantic_)
51 {
52 other.binder_ = nullptr;
53 other.ast_ = nullptr;
54 }
55
operator =(Program && other)56 Program &Program::operator=(Program &&other)
57 {
58 allocator_ = std::move(other.allocator_);
59 binder_ = other.binder_;
60 ast_ = other.ast_;
61 sourceCode_ = other.sourceCode_;
62 sourceFile_ = other.sourceFile_;
63 kind_ = other.kind_;
64 extension_ = other.extension_;
65 lineIndex_ = other.lineIndex_;
66 isDtsFile_ = other.isDtsFile_;
67
68 other.ast_ = nullptr;
69
70 return *this;
71 }
72
SetKind(ScriptKind kind)73 void Program::SetKind(ScriptKind kind)
74 {
75 kind_ = kind;
76 binder_->InitTopScope();
77
78 if (kind == ScriptKind::MODULE) {
79 moduleRecord_ = allocator_->New<SourceTextModuleRecord>(Allocator());
80 typeModuleRecord_ = allocator_->New<SourceTextModuleRecord>(Allocator());
81 }
82 }
83
Dump() const84 std::string Program::Dump() const
85 {
86 ir::AstDumper dumper {ast_, SourceCode()};
87 return dumper.Str();
88 }
89
90 } // namespace panda::es2panda::parser
91