• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 
23 namespace ark::es2panda::ir {
TemplateLiteral(Tag const tag,TemplateLiteral const & other,ArenaAllocator * const allocator)24 TemplateLiteral::TemplateLiteral([[maybe_unused]] Tag const tag, TemplateLiteral const &other,
25                                  ArenaAllocator *const allocator)
26     : Expression(static_cast<Expression const &>(other)),
27       quasis_(allocator->Adapter()),
28       expressions_(allocator->Adapter())
29 {
30     for (auto *quasy : other.quasis_) {
31         quasis_.emplace_back(quasy->Clone(allocator, this));
32     }
33 
34     for (auto *expression : other.expressions_) {
35         expressions_.emplace_back(expression->Clone(allocator, this)->AsExpression());
36     }
37 
38     multilineString_ = util::StringView(other.multilineString_);
39 }
40 
Clone(ArenaAllocator * const allocator,AstNode * const parent)41 TemplateLiteral *TemplateLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent)
42 {
43     auto *const clone = allocator->New<TemplateLiteral>(Tag {}, *this, allocator);
44     ES2PANDA_ASSERT(clone != nullptr);
45     if (parent != nullptr) {
46         clone->SetParent(parent);
47     }
48     clone->SetRange(Range());
49     return clone;
50 }
51 
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)52 void TemplateLiteral::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
53 {
54     for (auto *&it : VectorIterationGuard(expressions_)) {
55         if (auto *transformedNode = cb(it); it != transformedNode) {
56             it->SetTransformedNode(transformationName, transformedNode);
57             it = transformedNode->AsExpression();
58         }
59     }
60 
61     for (auto *&it : VectorIterationGuard(quasis_)) {
62         if (auto *transformedNode = cb(it); it != transformedNode) {
63             it->SetTransformedNode(transformationName, transformedNode);
64             it = transformedNode->AsTemplateElement();
65         }
66     }
67 }
68 
Iterate(const NodeTraverser & cb) const69 void TemplateLiteral::Iterate(const NodeTraverser &cb) const
70 {
71     for (auto *it : VectorIterationGuard(expressions_)) {
72         cb(it);
73     }
74 
75     for (auto *it : VectorIterationGuard(quasis_)) {
76         cb(it);
77     }
78 }
79 
Dump(ir::AstDumper * dumper) const80 void TemplateLiteral::Dump(ir::AstDumper *dumper) const
81 {
82     dumper->Add({{"type", "TemplateLiteral"}, {"expressions", expressions_}, {"quasis", quasis_}});
83 }
84 
Dump(ir::SrcDumper * dumper) const85 void TemplateLiteral::Dump(ir::SrcDumper *dumper) const
86 {
87     dumper->Add("`");
88     auto const num = std::max(expressions_.size(), quasis_.size());
89 
90     for (std::size_t i = 0U; i < num; i++) {
91         if (i < quasis_.size()) {
92             quasis_[i]->Dump(dumper);
93         }
94         if (i < expressions_.size()) {
95             dumper->Add("${");
96             expressions_[i]->Dump(dumper);
97             dumper->Add("}");
98         }
99     }
100     dumper->Add("`");
101 }
102 
Compile(compiler::PandaGen * pg) const103 void TemplateLiteral::Compile([[maybe_unused]] compiler::PandaGen *pg) const
104 {
105     pg->GetAstCompiler()->Compile(this);
106 }
107 
Check(checker::TSChecker * checker)108 checker::Type *TemplateLiteral::Check([[maybe_unused]] checker::TSChecker *checker)
109 {
110     // NOTE: aszilagyi.
111     return checker->GlobalAnyType();
112 }
113 
Compile(compiler::ETSGen * etsg) const114 void TemplateLiteral::Compile([[maybe_unused]] compiler::ETSGen *etsg) const
115 {
116     etsg->GetAstCompiler()->Compile(this);
117 }
118 
Check(checker::ETSChecker * checker)119 checker::VerifiedType TemplateLiteral::Check([[maybe_unused]] checker::ETSChecker *checker)
120 {
121     return {this, checker->GetAnalyzer()->Check(this)};
122 }
123 
GetMultilineString() const124 util::StringView TemplateLiteral::GetMultilineString() const
125 {
126     return multilineString_;
127 }
128 }  // namespace ark::es2panda::ir
129