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 "functionExpression.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/expressions/identifier.h>
23 #include <ir/statements/variableDeclarator.h>
24
25 namespace panda::es2panda::ir {
26
Iterate(const NodeTraverser & cb) const27 void FunctionExpression::Iterate(const NodeTraverser &cb) const
28 {
29 cb(func_);
30 }
31
Dump(ir::AstDumper * dumper) const32 void FunctionExpression::Dump(ir::AstDumper *dumper) const
33 {
34 dumper->Add({{"type", "FunctionExpression"}, {"function", func_}});
35 }
36
Compile(compiler::PandaGen * pg) const37 void FunctionExpression::Compile(compiler::PandaGen *pg) const
38 {
39 pg->DefineFunction(func_, func_, func_->Scope()->InternalName());
40 }
41
Check(checker::Checker * checker) const42 checker::Type *FunctionExpression::Check(checker::Checker *checker) const
43 {
44 binder::Variable *funcVar = nullptr;
45
46 if (func_->Parent()->Parent() && func_->Parent()->Parent()->IsVariableDeclarator() &&
47 func_->Parent()->Parent()->AsVariableDeclarator()->Id()->IsIdentifier()) {
48 funcVar = func_->Parent()->Parent()->AsVariableDeclarator()->Id()->AsIdentifier()->Variable();
49 }
50
51 checker::ScopeContext scopeCtx(checker, func_->Scope());
52
53 auto *signatureInfo = checker->Allocator()->New<checker::SignatureInfo>(checker->Allocator());
54 checker->CheckFunctionParameterDeclarations(func_->Params(), signatureInfo);
55
56 auto *signature =
57 checker->Allocator()->New<checker::Signature>(signatureInfo, checker->GlobalResolvingReturnType());
58 checker::Type *funcType = checker->CreateFunctionTypeWithSignature(signature);
59
60 if (funcVar && !funcVar->TsType()) {
61 funcVar->SetTsType(funcType);
62 }
63
64 signature->SetReturnType(checker->HandleFunctionReturn(func_));
65
66 func_->Body()->Check(checker);
67
68 return funcType;
69 }
70
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)71 void FunctionExpression::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
72 {
73 func_ = std::get<ir::AstNode *>(cb(func_))->AsScriptFunction();
74 }
75
76 } // namespace panda::es2panda::ir
77