• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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 "returnStatement.h"
17 
18 #include "checker/TSchecker.h"
19 #include "compiler/core/pandagen.h"
20 #include "compiler/core/ETSGen.h"
21 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23 
24 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)25 void ReturnStatement::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
26 {
27     if (argument_ != nullptr) {
28         if (auto *transformedNode = cb(argument_); argument_ != transformedNode) {
29             argument_->SetTransformedNode(transformationName, transformedNode);
30             argument_ = transformedNode->AsExpression();
31         }
32     }
33 }
34 
Iterate(const NodeTraverser & cb) const35 void ReturnStatement::Iterate(const NodeTraverser &cb) const
36 {
37     if (argument_ != nullptr) {
38         cb(argument_);
39     }
40 }
41 
Dump(ir::AstDumper * dumper) const42 void ReturnStatement::Dump(ir::AstDumper *dumper) const
43 {
44     dumper->Add({{"type", "ReturnStatement"}, {"argument", AstDumper::Nullish(argument_)}});
45 }
46 
Dump(ir::SrcDumper * dumper) const47 void ReturnStatement::Dump(ir::SrcDumper *dumper) const
48 {
49     dumper->Add("return");
50     if (argument_ != nullptr) {
51         dumper->Add(" ");
52         argument_->Dump(dumper);
53     }
54     dumper->Add(";");
55 }
56 
Compile(compiler::PandaGen * pg) const57 void ReturnStatement::Compile([[maybe_unused]] compiler::PandaGen *pg) const
58 {
59     pg->GetAstCompiler()->Compile(this);
60 }
61 
Compile(compiler::ETSGen * etsg) const62 void ReturnStatement::Compile([[maybe_unused]] compiler::ETSGen *etsg) const
63 {
64     etsg->GetAstCompiler()->Compile(this);
65 }
66 
Check(checker::TSChecker * checker)67 checker::Type *ReturnStatement::Check([[maybe_unused]] checker::TSChecker *checker)
68 {
69     return checker->GetAnalyzer()->Check(this);
70 }
71 
Check(checker::ETSChecker * checker)72 checker::VerifiedType ReturnStatement::Check(checker::ETSChecker *checker)
73 {
74     return {this, checker->GetAnalyzer()->Check(this)};
75 }
76 
SetReturnType(checker::ETSChecker * checker,checker::Type * type)77 void ReturnStatement::SetReturnType(checker::ETSChecker *checker, checker::Type *type)
78 {
79     returnType_ = type;
80     if (argument_ != nullptr) {
81         checker::Type *argumentType = argument_->Check(checker);
82         if (type->IsETSReferenceType() && !argumentType->IsETSReferenceType()) {
83             auto *const relation = checker->Relation();
84             relation->SetNode(argument_);
85             relation->SetFlags(checker::TypeRelationFlag::NONE);
86 
87             argumentType = checker->MaybeBoxInRelation(argumentType);
88             if (argumentType == nullptr) {
89                 checker->LogError(diagnostic::INVALID_EXPR_IN_RETURN, {}, argument_->Start());
90                 return;
91             }
92             argument_->AddBoxingUnboxingFlags(checker->GetBoxingFlag(argumentType));
93 
94             relation->SetNode(nullptr);
95         }
96     }
97 }
98 
SetArgument(Expression * arg)99 void ReturnStatement::SetArgument(Expression *arg)
100 {
101     argument_ = arg;
102     if (argument_ != nullptr) {
103         argument_->SetParent(this);
104     }
105 }
106 
IsAsyncImplReturn() const107 bool ReturnStatement::IsAsyncImplReturn() const
108 {
109     const auto *parent = Parent();
110 
111     while (parent != nullptr) {
112         if (parent->IsScriptFunction()) {
113             return parent->AsScriptFunction()->IsAsyncImplFunc();
114         }
115         parent = parent->Parent();
116     }
117     return false;
118 }
119 
Clone(ArenaAllocator * const allocator,AstNode * const parent)120 ReturnStatement *ReturnStatement::Clone(ArenaAllocator *const allocator, AstNode *const parent)
121 {
122     ir::ReturnStatement *clone = allocator->New<ir::ReturnStatement>();
123     if (clone != nullptr) {
124         clone->SetParent(parent);
125         if (argument_ != nullptr) {
126             clone->SetArgument(argument_->Clone(allocator, clone)->AsExpression());
127         }
128         clone->returnType_ = returnType_;
129         clone->SetRange(Range());
130     }
131     return clone;
132 }
133 }  // namespace ark::es2panda::ir
134