• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 - 2023 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 "arrowFunctionExpression.h"
17 
18 #include "compiler/core/pandagen.h"
19 #include "compiler/core/ETSGen.h"
20 #include "checker/ETSchecker.h"
21 #include "checker/ets/typeRelationContext.h"
22 #include "checker/TSchecker.h"
23 #include "ir/astDump.h"
24 #include "ir/srcDump.h"
25 #include "ir/base/scriptFunction.h"
26 #include "ir/ets/etsTypeReference.h"
27 #include "ir/ets/etsTypeReferencePart.h"
28 #include "ir/expressions/identifier.h"
29 #include "ir/expressions/thisExpression.h"
30 #include "ir/statements/variableDeclarator.h"
31 
32 namespace panda::es2panda::ir {
TransformChildren(const NodeTransformer & cb)33 void ArrowFunctionExpression::TransformChildren(const NodeTransformer &cb)
34 {
35     func_ = cb(func_)->AsScriptFunction();
36 }
37 
Iterate(const NodeTraverser & cb) const38 void ArrowFunctionExpression::Iterate(const NodeTraverser &cb) const
39 {
40     cb(func_);
41 }
42 
Dump(ir::AstDumper * dumper) const43 void ArrowFunctionExpression::Dump(ir::AstDumper *dumper) const
44 {
45     dumper->Add({{"type", "ArrowFunctionExpression"}, {"function", func_}});
46 }
47 
Dump(ir::SrcDumper * dumper) const48 void ArrowFunctionExpression::Dump(ir::SrcDumper *dumper) const
49 {
50     func_->Dump(dumper);
51 }
52 
Compile(compiler::PandaGen * pg) const53 void ArrowFunctionExpression::Compile(compiler::PandaGen *pg) const
54 {
55     pg->GetAstCompiler()->Compile(this);
56 }
57 
Compile(compiler::ETSGen * etsg) const58 void ArrowFunctionExpression::Compile(compiler::ETSGen *etsg) const
59 {
60     etsg->GetAstCompiler()->Compile(this);
61 }
62 
Check(checker::TSChecker * checker)63 checker::Type *ArrowFunctionExpression::Check(checker::TSChecker *checker)
64 {
65     return checker->GetAnalyzer()->Check(this);
66 }
67 
Check(checker::ETSChecker * checker)68 checker::Type *ArrowFunctionExpression::Check(checker::ETSChecker *checker)
69 {
70     return checker->GetAnalyzer()->Check(this);
71 }
72 
ArrowFunctionExpression(ArrowFunctionExpression const & other,ArenaAllocator * const allocator)73 ArrowFunctionExpression::ArrowFunctionExpression(ArrowFunctionExpression const &other, ArenaAllocator *const allocator)
74     : Expression(static_cast<Expression const &>(other)),
75       capturedVars_(allocator->Adapter()),
76       propagateThis_(other.propagateThis_)
77 {
78     func_ = other.func_->Clone(allocator, this)->AsScriptFunction();
79 
80     for (auto *const variable : other.capturedVars_) {
81         capturedVars_.emplace_back(variable);
82     }
83 
84     if (other.resolvedLambda_ != nullptr) {
85         resolvedLambda_ = other.resolvedLambda_->Clone(allocator, this)->AsClassDefinition();
86     }
87 }
88 
89 // NOLINTNEXTLINE(google-default-arguments)
Clone(ArenaAllocator * const allocator,AstNode * const parent)90 ArrowFunctionExpression *ArrowFunctionExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
91 {
92     if (auto *const clone = allocator->New<ArrowFunctionExpression>(*this, allocator); clone != nullptr) {
93         if (parent != nullptr) {
94             clone->SetParent(parent);
95         }
96         return clone;
97     }
98 
99     throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
100 }
101 
CreateReturnNodeFromType(checker::ETSChecker * checker,checker::Type * returnType)102 ir::TypeNode *ArrowFunctionExpression::CreateReturnNodeFromType(checker::ETSChecker *checker, checker::Type *returnType)
103 {
104     /*
105     Construct a synthetic Node with the correct ts_type_.
106     */
107     ASSERT(returnType != nullptr);
108     ir::TypeNode *returnNode = nullptr;
109     auto *ident = checker->Allocator()->New<ir::Identifier>(util::StringView(""), checker->Allocator());
110     ir::ETSTypeReferencePart *part = checker->Allocator()->New<ir::ETSTypeReferencePart>(ident);
111     returnNode = checker->Allocator()->New<ir::ETSTypeReference>(part);
112     part->SetParent(returnNode);
113     returnNode->SetTsType(returnType);
114     return returnNode;
115 }
116 
CreateTypeAnnotation(checker::ETSChecker * checker)117 ir::TypeNode *ArrowFunctionExpression::CreateTypeAnnotation(checker::ETSChecker *checker)
118 {
119     ir::TypeNode *returnNode = nullptr;
120     /*
121     There are two scenarios for lambda type inference: defined or undefined return type.
122     example code:
123     ```
124     enum Color { Red, Blue}
125     // has Return Type Color
126     let x  = () : Color => {return Color.Red}
127     // No Return Type Color
128     let y  = () => {return Color.Red}
129     ```
130     */
131     if (Function()->ReturnTypeAnnotation() == nullptr) {
132         /*
133         When lambda expression does not declare a return type, we need to construct the
134         declaration node of lambda according to the Function()->Signature()->ReturnType().
135         */
136         returnNode = CreateReturnNodeFromType(checker, Function()->Signature()->ReturnType());
137     } else {
138         returnNode = Function()->ReturnTypeAnnotation();
139     }
140 
141     auto origParams = Function()->Params();
142     auto signature = ir::FunctionSignature(nullptr, std::move(origParams), returnNode);
143     auto *funcType =
144         checker->Allocator()->New<ir::ETSFunctionType>(std::move(signature), ir::ScriptFunctionFlags::NONE);
145     returnNode->SetParent(funcType);
146     return funcType;
147 }
148 }  // namespace panda::es2panda::ir
149