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
21namespace panda::es2panda::parser {
22
23Program::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
32Program::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      isShared_(other.isShared_),
52      targetApiSubVersion_(other.targetApiSubVersion_),
53      moduleRecordFieldName_(other.moduleRecordFieldName_)
54{
55    other.binder_ = nullptr;
56    other.ast_ = nullptr;
57    other.moduleRecord_ = nullptr;
58    other.patchFixHelper_ = nullptr;
59    other.typeModuleRecord_ = nullptr;
60}
61
62Program &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
84    other.ast_ = nullptr;
85    other.binder_ = nullptr;
86    other.moduleRecord_ = nullptr;
87    other.patchFixHelper_ = nullptr;
88    other.typeModuleRecord_ = nullptr;
89
90    return *this;
91}
92
93void Program::SetKind(ScriptKind kind)
94{
95    kind_ = kind;
96    binder_->InitTopScope();
97
98    if (kind == ScriptKind::MODULE) {
99        moduleRecord_ = allocator_->New<SourceTextModuleRecord>(Allocator());
100        typeModuleRecord_ = allocator_->New<SourceTextModuleRecord>(Allocator());
101    }
102}
103
104std::string Program::Dump() const
105{
106    ir::AstDumper dumper {ast_, SourceCode()};
107    return dumper.Str();
108}
109
110}  // namespace panda::es2panda::parser
111