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 "taggedTemplateExpression.h"
17
18 #include <binder/variable.h>
19 #include <compiler/base/literals.h>
20 #include <compiler/core/pandagen.h>
21 #include <compiler/core/regScope.h>
22 #include <typescript/checker.h>
23 #include <ir/astDump.h>
24 #include <ir/expressions/memberExpression.h>
25 #include <ir/expressions/templateLiteral.h>
26 #include <ir/ts/tsTypeParameterInstantiation.h>
27
28 namespace panda::es2panda::ir {
29
Iterate(const NodeTraverser & cb) const30 void TaggedTemplateExpression::Iterate(const NodeTraverser &cb) const
31 {
32 if (typeParams_) {
33 cb(typeParams_);
34 }
35
36 cb(tag_);
37 cb(quasi_);
38 }
39
Dump(ir::AstDumper * dumper) const40 void TaggedTemplateExpression::Dump(ir::AstDumper *dumper) const
41 {
42 dumper->Add({{"type", "TaggedTemplateExpression"},
43 {"tag", tag_},
44 {"quasi", quasi_},
45 {"typeParameters", AstDumper::Optional(typeParams_)}});
46 }
47
Compile(compiler::PandaGen * pg) const48 void TaggedTemplateExpression::Compile(compiler::PandaGen *pg) const
49 {
50 compiler::RegScope rs(pg);
51 compiler::VReg callee = pg->AllocReg();
52 bool hasThis = false;
53
54 if (tag_->IsMemberExpression()) {
55 hasThis = true;
56 compiler::VReg thisReg = pg->AllocReg();
57
58 compiler::RegScope mrs(pg);
59 tag_->AsMemberExpression()->Compile(pg, thisReg);
60 } else {
61 tag_->Compile(pg);
62 }
63
64 pg->StoreAccumulator(this, callee);
65
66 compiler::Literals::GetTemplateObject(pg, this);
67 compiler::VReg arg0 = pg->AllocReg();
68 pg->StoreAccumulator(this, arg0);
69
70 for (const auto *element : quasi_->Expressions()) {
71 element->Compile(pg);
72 compiler::VReg arg = pg->AllocReg();
73 pg->StoreAccumulator(element, arg);
74 }
75
76 if (hasThis) {
77 constexpr auto extraParams = 2;
78 pg->CallThis(this, callee, static_cast<int64_t>(quasi_->Expressions().size() + extraParams));
79 return;
80 }
81
82 constexpr auto extraParams = 1;
83 pg->Call(this, callee, quasi_->Expressions().size() + extraParams);
84 }
85
Check(checker::Checker * checker) const86 checker::Type *TaggedTemplateExpression::Check(checker::Checker *checker) const
87 {
88 // TODO(aszilagyi)
89 return checker->GlobalAnyType();
90 }
91
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)92 void TaggedTemplateExpression::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
93 {
94 if (typeParams_) {
95 typeParams_ = std::get<ir::AstNode *>(cb(typeParams_))->AsTSTypeParameterInstantiation();
96 }
97
98 tag_ = std::get<ir::AstNode *>(cb(tag_))->AsExpression();
99 quasi_ = std::get<ir::AstNode *>(cb(quasi_))->AsTemplateLiteral();
100 }
101
102 } // namespace panda::es2panda::ir
103