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