• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 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 ark::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 
Clone(ArenaAllocator * const allocator,AstNode * const parent)42 TemplateLiteral *TemplateLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent)
43 {
44     if (auto *const clone = allocator->New<TemplateLiteral>(Tag {}, *this, allocator); clone != nullptr) {
45         if (parent != nullptr) {
46             clone->SetParent(parent);
47         }
48         return clone;
49     }
50     throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
51 }
52 
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)53 void TemplateLiteral::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
54 {
55     for (auto *&it : expressions_) {
56         if (auto *transformedNode = cb(it); it != transformedNode) {
57             it->SetTransformedNode(transformationName, transformedNode);
58             it = transformedNode->AsExpression();
59         }
60     }
61 
62     for (auto *&it : quasis_) {
63         if (auto *transformedNode = cb(it); it != transformedNode) {
64             it->SetTransformedNode(transformationName, transformedNode);
65             it = transformedNode->AsTemplateElement();
66         }
67     }
68 }
69 
Iterate(const NodeTraverser & cb) const70 void TemplateLiteral::Iterate(const NodeTraverser &cb) const
71 {
72     for (auto *it : expressions_) {
73         cb(it);
74     }
75 
76     for (auto *it : quasis_) {
77         cb(it);
78     }
79 }
80 
Dump(ir::AstDumper * dumper) const81 void TemplateLiteral::Dump(ir::AstDumper *dumper) const
82 {
83     dumper->Add({{"type", "TemplateLiteral"}, {"expressions", expressions_}, {"quasis", quasis_}});
84 }
85 
Dump(ir::SrcDumper * dumper) const86 void TemplateLiteral::Dump(ir::SrcDumper *dumper) const
87 {
88     dumper->Add("`");
89     auto const num = std::max(expressions_.size(), quasis_.size());
90 
91     for (std::size_t i = 0U; i < num; i++) {
92         if (i < quasis_.size()) {
93             quasis_[i]->Dump(dumper);
94         }
95         if (i < expressions_.size()) {
96             dumper->Add("${");
97             expressions_[i]->Dump(dumper);
98             dumper->Add("}");
99         }
100     }
101     dumper->Add("`");
102 }
103 
Compile(compiler::PandaGen * pg) const104 void TemplateLiteral::Compile([[maybe_unused]] compiler::PandaGen *pg) const
105 {
106     pg->GetAstCompiler()->Compile(this);
107 }
108 
Check(checker::TSChecker * checker)109 checker::Type *TemplateLiteral::Check([[maybe_unused]] checker::TSChecker *checker)
110 {
111     // NOTE: aszilagyi.
112     return checker->GlobalAnyType();
113 }
114 
Compile(compiler::ETSGen * etsg) const115 void TemplateLiteral::Compile([[maybe_unused]] compiler::ETSGen *etsg) const
116 {
117     etsg->GetAstCompiler()->Compile(this);
118 }
119 
Check(checker::ETSChecker * checker)120 checker::Type *TemplateLiteral::Check([[maybe_unused]] checker::ETSChecker *checker)
121 {
122     return checker->GetAnalyzer()->Check(this);
123 }
124 }  // namespace ark::es2panda::ir
125