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 "yieldExpression.h"
17 
18 #include "compiler/core/ETSGen.h"
19 #include "compiler/core/pandagen.h"
20 #include "compiler/function/generatorFunctionBuilder.h"
21 #include "checker/TSchecker.h"
22 #include "ir/astDump.h"
23 #include "ir/srcDump.h"
24 
25 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)26 void YieldExpression::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
27 {
28     if (argument_ != nullptr) {
29         if (auto *transformedNode = cb(argument_); argument_ != transformedNode) {
30             argument_->SetTransformedNode(transformationName, transformedNode);
31             argument_ = transformedNode->AsExpression();
32         }
33     }
34 }
35 
Iterate(const NodeTraverser &cb) const36 void YieldExpression::Iterate(const NodeTraverser &cb) const
37 {
38     if (argument_ != nullptr) {
39         cb(argument_);
40     }
41 }
42 
Dump(ir::AstDumper *dumper) const43 void YieldExpression::Dump(ir::AstDumper *dumper) const
44 {
45     dumper->Add({{"type", "YieldExpression"}, {"delegate", delegate_}, {"argument", AstDumper::Nullish(argument_)}});
46 }
47 
Dump(ir::SrcDumper *dumper) const48 void YieldExpression::Dump(ir::SrcDumper *dumper) const
49 {
50     dumper->Add("YieldExpression");
51 }
52 
Compile([[maybe_unused]] compiler::PandaGen *pg) const53 void YieldExpression::Compile([[maybe_unused]] compiler::PandaGen *pg) const
54 {
55     pg->GetAstCompiler()->Compile(this);
56 }
57 
Compile(compiler::ETSGen *etsg) const58 void YieldExpression::Compile(compiler::ETSGen *etsg) const
59 {
60     etsg->GetAstCompiler()->Compile(this);
61 }
62 
Check([[maybe_unused]] checker::TSChecker *checker)63 checker::Type *YieldExpression::Check([[maybe_unused]] checker::TSChecker *checker)
64 {
65     return checker->GetAnalyzer()->Check(this);
66 }
67 
Check([[maybe_unused]] checker::ETSChecker *checker)68 checker::Type *YieldExpression::Check([[maybe_unused]] checker::ETSChecker *checker)
69 {
70     return checker->GetAnalyzer()->Check(this);
71 }
72 
Clone(ArenaAllocator *const allocator, AstNode *const parent)73 YieldExpression *YieldExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
74 {
75     auto *const argument = argument_ != nullptr ? argument_->Clone(allocator, nullptr)->AsExpression() : nullptr;
76 
77     if (auto *const clone = allocator->New<YieldExpression>(argument, delegate_); clone != nullptr) {
78         if (argument != nullptr) {
79             argument->SetParent(clone);
80         }
81         if (parent != nullptr) {
82             clone->SetParent(parent);
83         }
84         return clone;
85     }
86 
87     throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
88 }
89 }  // namespace ark::es2panda::ir
90