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
27 namespace panda::es2panda::ir {
28
Iterate(const NodeTraverser & cb) const29 void 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
Dump(ir::AstDumper * dumper) const46 void TSMethodSignature::Dump(ir::AstDumper *dumper) const
47 {
48 dumper->Add({{"type", "TSMethodSignature"},
49 {"computed", computed_},
50 {"optional", optional_},
51 {"key", key_},
52 {"params", params_},
53 {"typeParameters", AstDumper::Optional(typeParams_)},
54 {"typeAnnotation", AstDumper::Optional(returnTypeAnnotation_)}});
55 }
56
Compile(compiler::PandaGen * pg) const57 void TSMethodSignature::Compile([[maybe_unused]] compiler::PandaGen *pg) const {}
58
Check(checker::Checker * checker) const59 checker::Type *TSMethodSignature::Check(checker::Checker *checker) const
60 {
61 if (computed_) {
62 checker->CheckComputedPropertyName(key_);
63 }
64
65 checker::ScopeContext scopeCtx(checker, scope_);
66
67 auto *signatureInfo = checker->Allocator()->New<checker::SignatureInfo>(checker->Allocator());
68 checker->CheckFunctionParameterDeclarations(params_, signatureInfo);
69
70 auto *callSignature = checker->Allocator()->New<checker::Signature>(signatureInfo, checker->GlobalAnyType());
71 Variable()->SetTsType(checker->CreateFunctionTypeWithSignature(callSignature));
72
73 if (!returnTypeAnnotation_) {
74 checker->ThrowTypeError(
75 "Method signature, which lacks return-type annotation, implicitly has an 'any' return type.", Start());
76 }
77
78 returnTypeAnnotation_->Check(checker);
79 callSignature->SetReturnType(returnTypeAnnotation_->AsTypeNode()->GetType(checker));
80
81 return nullptr;
82 }
83
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)84 void TSMethodSignature::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)
85 {
86 auto scopeCtx = binder::LexicalScope<binder::Scope>::Enter(binder, scope_);
87
88 key_ = std::get<ir::AstNode *>(cb(key_))->AsExpression();
89
90 if (typeParams_) {
91 typeParams_ = std::get<ir::AstNode *>(cb(typeParams_))->AsTSTypeParameterDeclaration();
92 }
93
94 for (auto iter = params_.begin(); iter != params_.end(); iter++) {
95 *iter = std::get<ir::AstNode *>(cb(*iter))->AsExpression();
96 }
97
98 if (returnTypeAnnotation_) {
99 returnTypeAnnotation_ = std::get<ir::AstNode *>(cb(returnTypeAnnotation_))->AsExpression();
100 }
101 }
102
103 } // namespace panda::es2panda::ir
104