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
26namespace ark::es2panda::ir {
27TemplateLiteral::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
42TemplateLiteral *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
53void 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
70void 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
81void TemplateLiteral::Dump(ir::AstDumper *dumper) const
82{
83    dumper->Add({{"type", "TemplateLiteral"}, {"expressions", expressions_}, {"quasis", quasis_}});
84}
85
86void 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
104void TemplateLiteral::Compile([[maybe_unused]] compiler::PandaGen *pg) const
105{
106    pg->GetAstCompiler()->Compile(this);
107}
108
109checker::Type *TemplateLiteral::Check([[maybe_unused]] checker::TSChecker *checker)
110{
111    // NOTE: aszilagyi.
112    return checker->GlobalAnyType();
113}
114
115void TemplateLiteral::Compile([[maybe_unused]] compiler::ETSGen *etsg) const
116{
117    etsg->GetAstCompiler()->Compile(this);
118}
119
120checker::Type *TemplateLiteral::Check([[maybe_unused]] checker::ETSChecker *checker)
121{
122    return checker->GetAnalyzer()->Check(this);
123}
124}  // namespace ark::es2panda::ir
125