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/expressions/identifier.h> 23#include <ir/statements/variableDeclarator.h> 24 25namespace panda::es2panda::ir { 26 27void ArrowFunctionExpression::Iterate(const NodeTraverser &cb) const 28{ 29 cb(func_); 30} 31 32void ArrowFunctionExpression::Dump(ir::AstDumper *dumper) const 33{ 34 dumper->Add({{"type", "ArrowFunctionExpression"}, {"function", func_}}); 35} 36 37void ArrowFunctionExpression::Compile(compiler::PandaGen *pg) const 38{ 39 pg->DefineFunction(func_, func_, func_->Scope()->InternalName()); 40} 41 42checker::Type *ArrowFunctionExpression::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 CHECK_NOT_NULL(signature); 65 signature->SetReturnType(checker->HandleFunctionReturn(func_)); 66 67 if (!func_->Body()->IsExpression()) { 68 func_->Body()->Check(checker); 69 } 70 71 return funcType; 72} 73 74void ArrowFunctionExpression::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) 75{ 76 func_ = std::get<ir::AstNode *>(cb(func_))->AsScriptFunction(); 77} 78 79} // namespace panda::es2panda::ir 80