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 "tsMethodSignature.h" 17 18#include <typescript/checker.h> 19#include <binder/binder.h> 20#include <binder/scope.h> 21#include <ir/astDump.h> 22#include <ir/typeNode.h> 23#include <ir/ts/tsTypeParameter.h> 24#include <ir/ts/tsTypeParameterDeclaration.h> 25#include <ir/expressions/literals/numberLiteral.h> 26 27namespace panda::es2panda::ir { 28 29void TSMethodSignature::Iterate(const NodeTraverser &cb) const 30{ 31 cb(key_); 32 33 if (typeParams_) { 34 cb(typeParams_); 35 } 36 37 for (auto *it : params_) { 38 cb(it); 39 } 40 41 if (returnTypeAnnotation_) { 42 cb(returnTypeAnnotation_); 43 } 44} 45 46void TSMethodSignature::Dump(ir::AstDumper *dumper) const 47{ 48 dumper->Add({{"type", "TSMethodSignature"}, 49 {"computed", computed_}, 50 {"optional", optional_}, 51 {"isGetAccessor", isGetAccessor_}, 52 {"isSetAccessor", isSetAccessor_}, 53 {"key", key_}, 54 {"params", params_}, 55 {"typeParameters", AstDumper::Optional(typeParams_)}, 56 {"typeAnnotation", AstDumper::Optional(returnTypeAnnotation_)}}); 57} 58 59void TSMethodSignature::Compile([[maybe_unused]] compiler::PandaGen *pg) const {} 60 61checker::Type *TSMethodSignature::Check(checker::Checker *checker) const 62{ 63 if (computed_) { 64 checker->CheckComputedPropertyName(key_); 65 } 66 67 checker::ScopeContext scopeCtx(checker, scope_); 68 69 auto *signatureInfo = checker->Allocator()->New<checker::SignatureInfo>(checker->Allocator()); 70 checker->CheckFunctionParameterDeclarations(params_, signatureInfo); 71 72 auto *callSignature = checker->Allocator()->New<checker::Signature>(signatureInfo, checker->GlobalAnyType()); 73 Variable()->SetTsType(checker->CreateFunctionTypeWithSignature(callSignature)); 74 75 if (!returnTypeAnnotation_) { 76 checker->ThrowTypeError( 77 "Method signature, which lacks return-type annotation, implicitly has an 'any' return type.", Start()); 78 } 79 80 returnTypeAnnotation_->Check(checker); 81 CHECK_NOT_NULL(callSignature); 82 callSignature->SetReturnType(returnTypeAnnotation_->AsTypeNode()->GetType(checker)); 83 84 return nullptr; 85} 86 87void TSMethodSignature::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder) 88{ 89 auto scopeCtx = binder::LexicalScope<binder::Scope>::Enter(binder, scope_); 90 91 key_ = std::get<ir::AstNode *>(cb(key_))->AsExpression(); 92 93 if (typeParams_) { 94 typeParams_ = std::get<ir::AstNode *>(cb(typeParams_))->AsTSTypeParameterDeclaration(); 95 } 96 97 for (auto iter = params_.begin(); iter != params_.end(); iter++) { 98 *iter = std::get<ir::AstNode *>(cb(*iter))->AsExpression(); 99 } 100 101 if (returnTypeAnnotation_) { 102 returnTypeAnnotation_ = std::get<ir::AstNode *>(cb(returnTypeAnnotation_))->AsExpression(); 103 } 104} 105 106} // namespace panda::es2panda::ir 107