1 /**
2 * Copyright (c) 2021 - 2023 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 "templateLiteral.h"
17
18 #include "compiler/core/pandagen.h"
19 #include "compiler/core/ETSGen.h"
20 #include "checker/TSchecker.h"
21 #include "checker/ETSchecker.h"
22 #include "ir/astDump.h"
23 #include "ir/srcDump.h"
24 #include "ir/base/templateElement.h"
25
26 namespace panda::es2panda::ir {
TemplateLiteral(Tag const tag,TemplateLiteral const & other,ArenaAllocator * const allocator)27 TemplateLiteral::TemplateLiteral([[maybe_unused]] Tag const tag, TemplateLiteral const &other,
28 ArenaAllocator *const allocator)
29 : Expression(static_cast<Expression const &>(other)),
30 quasis_(allocator->Adapter()),
31 expressions_(allocator->Adapter())
32 {
33 for (auto *quasy : other.quasis_) {
34 quasis_.emplace_back(quasy->Clone(allocator, this));
35 }
36
37 for (auto *expression : other.expressions_) {
38 expressions_.emplace_back(expression->Clone(allocator, this)->AsExpression());
39 }
40 }
41
42 // NOLINTNEXTLINE(google-default-arguments)
Clone(ArenaAllocator * const allocator,AstNode * const parent)43 TemplateLiteral *TemplateLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent)
44 {
45 if (auto *const clone = allocator->New<TemplateLiteral>(Tag {}, *this, allocator); clone != nullptr) {
46 if (parent != nullptr) {
47 clone->SetParent(parent);
48 }
49 return clone;
50 }
51 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
52 }
53
TransformChildren(const NodeTransformer & cb)54 void TemplateLiteral::TransformChildren(const NodeTransformer &cb)
55 {
56 for (auto *&it : expressions_) {
57 it = cb(it)->AsExpression();
58 }
59
60 for (auto *&it : quasis_) {
61 it = cb(it)->AsTemplateElement();
62 }
63 }
64
Iterate(const NodeTraverser & cb) const65 void TemplateLiteral::Iterate(const NodeTraverser &cb) const
66 {
67 for (auto *it : expressions_) {
68 cb(it);
69 }
70
71 for (auto *it : quasis_) {
72 cb(it);
73 }
74 }
75
Dump(ir::AstDumper * dumper) const76 void TemplateLiteral::Dump(ir::AstDumper *dumper) const
77 {
78 dumper->Add({{"type", "TemplateLiteral"}, {"expressions", expressions_}, {"quasis", quasis_}});
79 }
80
Dump(ir::SrcDumper * dumper) const81 void TemplateLiteral::Dump(ir::SrcDumper *dumper) const
82 {
83 dumper->Add("TemplateLiteral");
84 }
85
Compile(compiler::PandaGen * pg) const86 void TemplateLiteral::Compile([[maybe_unused]] compiler::PandaGen *pg) const
87 {
88 pg->GetAstCompiler()->Compile(this);
89 }
90
Check(checker::TSChecker * checker)91 checker::Type *TemplateLiteral::Check([[maybe_unused]] checker::TSChecker *checker)
92 {
93 // NOTE: aszilagyi.
94 return checker->GlobalAnyType();
95 }
96
Compile(compiler::ETSGen * etsg) const97 void TemplateLiteral::Compile([[maybe_unused]] compiler::ETSGen *etsg) const
98 {
99 etsg->GetAstCompiler()->Compile(this);
100 }
101
Check(checker::ETSChecker * checker)102 checker::Type *TemplateLiteral::Check([[maybe_unused]] checker::ETSChecker *checker)
103 {
104 return checker->GetAnalyzer()->Check(this);
105 }
106 } // namespace panda::es2panda::ir
107