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