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 <compiler/core/pandagen.h>
19 #include <ir/astDump.h>
20 #include <ir/expressions/callExpression.h>
21 #include <ir/expressions/memberExpression.h>
22
23 namespace panda::es2panda::ir {
24
Iterate(const NodeTraverser & cb) const25 void ChainExpression::Iterate(const NodeTraverser &cb) const
26 {
27 cb(expression_);
28 }
29
Dump(ir::AstDumper * dumper) const30 void ChainExpression::Dump(ir::AstDumper *dumper) const
31 {
32 dumper->Add({{"type", "ChainExpression"}, {"expression", expression_}});
33 }
34
Compile(compiler::PandaGen * pg) const35 void ChainExpression::Compile(compiler::PandaGen *pg) const
36 {
37 compiler::OptionalChain chain(pg, this);
38
39 if (expression_->IsMemberExpression()) {
40 expression_->AsMemberExpression()->Compile(pg);
41 } else {
42 assert(expression_->IsCallExpression());
43 expression_->AsCallExpression()->Compile(pg);
44 }
45 }
46
Check(checker::Checker * checker) const47 checker::Type *ChainExpression::Check(checker::Checker *checker) const
48 {
49 return expression_->Check(checker);
50 }
51
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)52 void ChainExpression::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
53 {
54 expression_ = std::get<ir::AstNode *>(cb(expression_))->AsExpression();
55 }
56
57 } // namespace panda::es2panda::ir
58