• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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 "chainExpression.h"
17 
18 #include "checker/TSchecker.h"
19 #include "compiler/core/ETSGen.h"
20 #include "compiler/core/pandagen.h"
21 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23 #include "ir/expressions/memberExpression.h"
24 
25 namespace panda::es2panda::ir {
TransformChildren(const NodeTransformer & cb)26 void ChainExpression::TransformChildren(const NodeTransformer &cb)
27 {
28     expression_ = cb(expression_)->AsExpression();
29 }
30 
Iterate(const NodeTraverser & cb) const31 void ChainExpression::Iterate(const NodeTraverser &cb) const
32 {
33     cb(expression_);
34 }
35 
Dump(ir::AstDumper * dumper) const36 void ChainExpression::Dump(ir::AstDumper *dumper) const
37 {
38     dumper->Add({{"type", "ChainExpression"}, {"expression", expression_}});
39 }
40 
Dump(ir::SrcDumper * dumper) const41 void ChainExpression::Dump(ir::SrcDumper *dumper) const
42 {
43     dumper->Add("ChainExpression");
44 }
45 
Compile(compiler::PandaGen * pg) const46 void ChainExpression::Compile(compiler::PandaGen *pg) const
47 {
48     pg->GetAstCompiler()->Compile(this);
49 }
50 
CompileToReg(compiler::PandaGen * pg,compiler::VReg & objReg) const51 void ChainExpression::CompileToReg(compiler::PandaGen *pg, compiler::VReg &objReg) const
52 {
53     compiler::OptionalChain chain(pg, this);
54 
55     if (expression_->IsMemberExpression()) {
56         objReg = pg->AllocReg();
57         expression_->AsMemberExpression()->CompileToReg(pg, objReg);
58     } else {
59         objReg = compiler::VReg::Invalid();
60         expression_->Compile(pg);
61     }
62 }
63 
Compile(compiler::ETSGen * etsg) const64 void ChainExpression::Compile(compiler::ETSGen *etsg) const
65 {
66     etsg->GetAstCompiler()->Compile(this);
67 }
68 
Check(checker::TSChecker * checker)69 checker::Type *ChainExpression::Check(checker::TSChecker *checker)
70 {
71     return checker->GetAnalyzer()->Check(this);
72 }
73 
Check(checker::ETSChecker * checker)74 checker::Type *ChainExpression::Check(checker::ETSChecker *checker)
75 {
76     return checker->GetAnalyzer()->Check(this);
77 }
78 
79 // NOLINTNEXTLINE(google-default-arguments)
Clone(ArenaAllocator * const allocator,AstNode * const parent)80 ChainExpression *ChainExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
81 {
82     auto *const expression = expression_ != nullptr ? expression_->Clone(allocator)->AsExpression() : nullptr;
83 
84     if (auto *const clone = allocator->New<ChainExpression>(expression); clone != nullptr) {
85         if (expression != nullptr) {
86             expression->SetParent(clone);
87         }
88         if (parent != nullptr) {
89             clone->SetParent(parent);
90         }
91         return clone;
92     }
93 
94     throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
95 }
96 }  // namespace panda::es2panda::ir
97