• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     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