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 "returnStatement.h"
17 
18 #include <ir/base/methodDefinition.h>
19 #include <ir/base/scriptFunction.h>
20 #include <compiler/core/pandagen.h>
21 #include <typescript/checker.h>
22 
23 #include <ir/astDump.h>
24 #include <ir/expression.h>
25 #include <ir/typeNode.h>
26 
27 namespace panda::es2panda::ir {
28 
Iterate(const NodeTraverser &cb) const29 void ReturnStatement::Iterate(const NodeTraverser &cb) const
30 {
31     if (argument_) {
32         cb(argument_);
33     }
34 }
35 
Dump(ir::AstDumper *dumper) const36 void ReturnStatement::Dump(ir::AstDumper *dumper) const
37 {
38     dumper->Add({{"type", "ReturnStatement"}, {"argument", AstDumper::Nullable(argument_)}});
39 }
40 
Compile(compiler::PandaGen *pg) const41 void ReturnStatement::Compile(compiler::PandaGen *pg) const
42 {
43     if (argument_) {
44         argument_->Compile(pg);
45     } else {
46         pg->LoadConst(this, compiler::Constant::JS_UNDEFINED);
47     }
48 
49     if (pg->CheckControlFlowChange()) {
50         compiler::RegScope rs(pg);
51         compiler::VReg res = pg->AllocReg();
52 
53         pg->StoreAccumulator(this, res);
54         pg->ControlFlowChangeReturn();
55         pg->LoadAccumulator(this, res);
56     }
57 
58     if (argument_) {
59         pg->ValidateClassDirectReturn(this);
60         pg->ExplicitReturn(this);
61     } else {
62         pg->ImplicitReturn(this);
63     }
64 }
65 
Check(checker::Checker *checker) const66 checker::Type *ReturnStatement::Check(checker::Checker *checker) const
67 {
68     const ir::AstNode *ancestor = checker::Checker::FindAncestorGivenByType(this, ir::AstNodeType::SCRIPT_FUNCTION);
69     CHECK_NOT_NULL(ancestor);
70     ASSERT(ancestor && ancestor->IsScriptFunction());
71     const ir::ScriptFunction *containingFunc = ancestor->AsScriptFunction();
72 
73     if (containingFunc->Parent()->Parent()->IsMethodDefinition()) {
74         const ir::MethodDefinition *containingClassMethod = containingFunc->Parent()->Parent()->AsMethodDefinition();
75         if (containingClassMethod->Kind() == ir::MethodDefinitionKind::SET) {
76             checker->ThrowTypeError("Setters cannot return a value", Start());
77         }
78     }
79 
80     if (containingFunc->ReturnTypeAnnotation()) {
81         checker::Type *returnType = checker->GlobalUndefinedType();
82         checker::Type *funcReturnType = containingFunc->ReturnTypeAnnotation()->AsTypeNode()->GetType(checker);
83 
84         if (argument_) {
85             checker->ElaborateElementwise(funcReturnType, argument_, Start());
86             returnType = checker->CheckTypeCached(argument_);
87         }
88 
89         checker->IsTypeAssignableTo(returnType, funcReturnType,
90                                     {"Type '", returnType, "' is not assignable to type '", funcReturnType, "'."},
91                                     Start());
92     }
93 
94     return nullptr;
95 }
96 
UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)97 void ReturnStatement::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
98 {
99     if (argument_) {
100         argument_ = std::get<ir::AstNode *>(cb(argument_))->AsExpression();
101     }
102 }
103 
104 }  // namespace panda::es2panda::ir
105