• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "templateLiteral.h"
17 
18 #include <compiler/core/pandagen.h>
19 #include <typescript/checker.h>
20 #include <ir/astDump.h>
21 
22 namespace panda::es2panda::ir {
23 
Iterate(const NodeTraverser & cb) const24 void TemplateLiteral::Iterate(const NodeTraverser &cb) const
25 {
26     for (auto *it : expressions_) {
27         cb(it);
28     }
29 
30     for (auto *it : quasis_) {
31         cb(it);
32     }
33 }
34 
Dump(ir::AstDumper * dumper) const35 void TemplateLiteral::Dump(ir::AstDumper *dumper) const
36 {
37     dumper->Add({{"type", "TemplateLiteral"}, {"expressions", expressions_}, {"quasis", quasis_}});
38 }
39 
Compile(compiler::PandaGen * pg) const40 void TemplateLiteral::Compile(compiler::PandaGen *pg) const
41 {
42     auto quasisIt = quasis_.begin();
43     auto expressionIt = expressions_.begin();
44 
45     pg->LoadAccumulatorString(this, (*quasisIt)->Cooked());
46 
47     quasisIt++;
48 
49     bool isQuais = false;
50     size_t total = quasis_.size() + expressions_.size();
51 
52     compiler::RegScope rs(pg);
53     compiler::VReg lhs = pg->AllocReg();
54 
55     while (total != 1) {
56         const ir::AstNode *node = nullptr;
57 
58         if (isQuais) {
59             pg->StoreAccumulator(*quasisIt, lhs);
60             pg->LoadAccumulatorString(this, (*quasisIt)->Cooked());
61 
62             node = *quasisIt;
63             quasisIt++;
64         } else {
65             const ir::Expression *element = *expressionIt;
66             pg->StoreAccumulator(element, lhs);
67 
68             element->Compile(pg);
69 
70             node = element;
71             expressionIt++;
72         }
73 
74         pg->Binary(node, lexer::TokenType::PUNCTUATOR_PLUS, lhs);
75 
76         isQuais = !isQuais;
77         total--;
78     }
79 }
80 
Check(checker::Checker * checker) const81 checker::Type *TemplateLiteral::Check(checker::Checker *checker) const
82 {
83     // TODO(aszilagyi)
84     return checker->GlobalAnyType();
85 }
86 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)87 void TemplateLiteral::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
88 {
89     for (auto iter = expressions_.begin(); iter != expressions_.end(); iter++) {
90         *iter = std::get<ir::AstNode *>(cb(*iter))->AsExpression();
91     }
92 
93     for (auto iter = quasis_.begin(); iter != quasis_.end(); iter++) {
94         *iter = std::get<ir::AstNode *>(cb(*iter))->AsTemplateElement();
95     }
96 }
97 
98 }  // namespace panda::es2panda::ir
99