1 /**
2 * Copyright (c) 2021-2024 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/statements/variableDeclarator.h"
30
31 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)32 void ArrowFunctionExpression::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
33 {
34 if (auto *transformedNode = cb(func_); func_ != transformedNode) {
35 func_->SetTransformedNode(transformationName, transformedNode);
36 func_ = transformedNode->AsScriptFunction();
37 }
38 }
39
Iterate(const NodeTraverser & cb) const40 void ArrowFunctionExpression::Iterate(const NodeTraverser &cb) const
41 {
42 cb(func_);
43 }
44
Dump(ir::AstDumper * dumper) const45 void ArrowFunctionExpression::Dump(ir::AstDumper *dumper) const
46 {
47 dumper->Add({{"type", "ArrowFunctionExpression"}, {"function", func_}});
48 }
49
Dump(ir::SrcDumper * dumper) const50 void ArrowFunctionExpression::Dump(ir::SrcDumper *dumper) const
51 {
52 func_->Dump(dumper);
53 }
54
Compile(compiler::PandaGen * pg) const55 void ArrowFunctionExpression::Compile(compiler::PandaGen *pg) const
56 {
57 pg->GetAstCompiler()->Compile(this);
58 }
59
Compile(compiler::ETSGen * etsg) const60 void ArrowFunctionExpression::Compile(compiler::ETSGen *etsg) const
61 {
62 etsg->GetAstCompiler()->Compile(this);
63 }
64
Check(checker::TSChecker * checker)65 checker::Type *ArrowFunctionExpression::Check(checker::TSChecker *checker)
66 {
67 return checker->GetAnalyzer()->Check(this);
68 }
69
Check(checker::ETSChecker * checker)70 checker::Type *ArrowFunctionExpression::Check(checker::ETSChecker *checker)
71 {
72 return checker->GetAnalyzer()->Check(this);
73 }
74
ArrowFunctionExpression(ArrowFunctionExpression const & other,ArenaAllocator * const allocator)75 ArrowFunctionExpression::ArrowFunctionExpression(ArrowFunctionExpression const &other, ArenaAllocator *const allocator)
76 : Expression(static_cast<Expression const &>(other))
77 {
78 func_ = other.func_->Clone(allocator, this)->AsScriptFunction();
79 }
80
Clone(ArenaAllocator * const allocator,AstNode * const parent)81 ArrowFunctionExpression *ArrowFunctionExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
82 {
83 if (auto *const clone = allocator->New<ArrowFunctionExpression>(*this, allocator); clone != nullptr) {
84 if (parent != nullptr) {
85 clone->SetParent(parent);
86 }
87 return clone;
88 }
89
90 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
91 }
92
CreateReturnNodeFromType(checker::ETSChecker * checker,checker::Type * returnType)93 ir::TypeNode *ArrowFunctionExpression::CreateReturnNodeFromType(checker::ETSChecker *checker, checker::Type *returnType)
94 {
95 /*
96 Construct a synthetic Node with the correct ts_type_.
97 */
98 ASSERT(returnType != nullptr);
99 auto *ident = checker->AllocNode<ir::Identifier>(util::StringView(""), checker->Allocator());
100 auto *const part = checker->AllocNode<ir::ETSTypeReferencePart>(ident);
101 auto *returnNode = checker->AllocNode<ir::ETSTypeReference>(part);
102 returnNode->SetTsType(returnType);
103 return returnNode;
104 }
105
CreateTypeAnnotation(checker::ETSChecker * checker)106 ir::TypeNode *ArrowFunctionExpression::CreateTypeAnnotation(checker::ETSChecker *checker)
107 {
108 ir::TypeNode *returnNode = nullptr;
109 /*
110 There are two scenarios for lambda type inference: defined or undefined return type.
111 example code:
112 ```
113 enum Color { Red, Blue}
114 // has Return Type Color
115 let x = () : Color => {return Color.Red}
116 // No Return Type Color
117 let y = () => {return Color.Red}
118 ```
119 */
120 if (Function()->ReturnTypeAnnotation() == nullptr) {
121 /*
122 When lambda expression does not declare a return type, we need to construct the
123 declaration node of lambda according to the Function()->Signature()->ReturnType().
124 */
125 returnNode = CreateReturnNodeFromType(checker, Function()->Signature()->ReturnType());
126 } else {
127 returnNode = Function()->ReturnTypeAnnotation()->Clone(checker->Allocator(), nullptr);
128 returnNode->SetTsType(Function()->ReturnTypeAnnotation()->TsType());
129 }
130
131 ArenaVector<ir::Expression *> params {checker->Allocator()->Adapter()};
132 checker->CopyParams(Function()->Params(), params);
133
134 auto signature = ir::FunctionSignature(nullptr, std::move(params), returnNode);
135 auto *funcType = checker->AllocNode<ir::ETSFunctionType>(std::move(signature), ir::ScriptFunctionFlags::NONE);
136 return funcType;
137 }
138
IsVarFromSubscope(const varbinder::Variable * var) const139 bool ArrowFunctionExpression::IsVarFromSubscope(const varbinder::Variable *var) const
140 {
141 // The parameter scope's and the function scope's common ancestor lives outside the function, so we have to check
142 // them separetely.
143 return Function()->Scope()->IsSuperscopeOf(var->GetScope()) ||
144 Function()->Scope()->ParamScope()->IsSuperscopeOf(var->GetScope());
145 }
146
147 } // namespace ark::es2panda::ir
148