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 "arrowFunctionExpression.h"
17
18 #include <compiler/core/pandagen.h>
19 #include <typescript/checker.h>
20 #include <ir/astDump.h>
21 #include <ir/base/scriptFunction.h>
22 #include <ir/statements/variableDeclarator.h>
23
24 namespace panda::es2panda::ir {
25
Iterate(const NodeTraverser & cb) const26 void ArrowFunctionExpression::Iterate(const NodeTraverser &cb) const
27 {
28 cb(func_);
29 }
30
Dump(ir::AstDumper * dumper) const31 void ArrowFunctionExpression::Dump(ir::AstDumper *dumper) const
32 {
33 dumper->Add({{"type", "ArrowFunctionExpression"}, {"function", func_}});
34 }
35
Compile(compiler::PandaGen * pg) const36 void ArrowFunctionExpression::Compile(compiler::PandaGen *pg) const
37 {
38 pg->DefineFunction(func_, func_, func_->Scope()->InternalName());
39 }
40
Check(checker::Checker * checker) const41 checker::Type *ArrowFunctionExpression::Check(checker::Checker *checker) const
42 {
43 binder::Variable *funcVar = nullptr;
44
45 if (func_->Parent()->Parent() && func_->Parent()->Parent()->IsVariableDeclarator() &&
46 func_->Parent()->Parent()->AsVariableDeclarator()->Id()->IsIdentifier()) {
47 funcVar = func_->Parent()->Parent()->AsVariableDeclarator()->Id()->AsIdentifier()->Variable();
48 }
49
50 checker::ScopeContext scopeCtx(checker, func_->Scope());
51
52 auto *signatureInfo = checker->Allocator()->New<checker::SignatureInfo>(checker->Allocator());
53 checker->CheckFunctionParameterDeclarations(func_->Params(), signatureInfo);
54
55 auto *signature =
56 checker->Allocator()->New<checker::Signature>(signatureInfo, checker->GlobalResolvingReturnType());
57 checker::Type *funcType = checker->CreateFunctionTypeWithSignature(signature);
58
59 if (funcVar && !funcVar->TsType()) {
60 funcVar->SetTsType(funcType);
61 }
62
63 CHECK_NOT_NULL(signature);
64 signature->SetReturnType(checker->HandleFunctionReturn(func_));
65
66 if (!func_->Body()->IsExpression()) {
67 func_->Body()->Check(checker);
68 }
69
70 return funcType;
71 }
72
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)73 void ArrowFunctionExpression::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
74 {
75 func_ = std::get<ir::AstNode *>(cb(func_))->AsScriptFunction();
76 }
77
78 } // namespace panda::es2panda::ir
79