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