• 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 "tsSignatureDeclaration.h"
17 
18 #include <ir/astDump.h>
19 #include <ir/typeNode.h>
20 #include <ir/ts/tsTypeParameterDeclaration.h>
21 #include <typescript/checker.h>
22 
23 namespace panda::es2panda::ir {
24 
Iterate(const NodeTraverser & cb) const25 void TSSignatureDeclaration::Iterate(const NodeTraverser &cb) const
26 {
27     if (typeParams_) {
28         cb(typeParams_);
29     }
30 
31     for (auto *it : params_) {
32         cb(it);
33     }
34 
35     if (returnTypeAnnotation_) {
36         cb(returnTypeAnnotation_);
37     }
38 }
39 
Dump(ir::AstDumper * dumper) const40 void TSSignatureDeclaration::Dump(ir::AstDumper *dumper) const
41 {
42     dumper->Add({{"type", (kind_ == TSSignatureDeclaration::TSSignatureDeclarationKind::CALL_SIGNATURE)
43                               ? "TSCallSignatureDeclaration"
44                               : "TSConstructSignatureDeclaration"},
45                  {"params", params_},
46                  {"typeParameters", AstDumper::Optional(typeParams_)},
47                  {"returnType", AstDumper::Optional(returnTypeAnnotation_)}});
48 }
49 
Compile(compiler::PandaGen * pg) const50 void TSSignatureDeclaration::Compile([[maybe_unused]] compiler::PandaGen *pg) const {}
51 
Check(checker::Checker * checker) const52 checker::Type *TSSignatureDeclaration::Check(checker::Checker *checker) const
53 {
54     auto found = checker->NodeCache().find(this);
55     if (found != checker->NodeCache().end()) {
56         return found->second;
57     }
58 
59     checker::ScopeContext scopeCtx(checker, scope_);
60 
61     auto *signatureInfo = checker->Allocator()->New<checker::SignatureInfo>(checker->Allocator());
62     checker->CheckFunctionParameterDeclarations(params_, signatureInfo);
63 
64     bool isCallSignature = (Kind() == ir::TSSignatureDeclaration::TSSignatureDeclarationKind::CALL_SIGNATURE);
65 
66     if (!returnTypeAnnotation_) {
67         if (isCallSignature) {
68             checker->ThrowTypeError(
69                 "Call signature, which lacks return-type annotation, implicitly has an 'any' return type.", Start());
70         }
71 
72         checker->ThrowTypeError(
73             "Construct signature, which lacks return-type annotation, implicitly has an 'any' return type.", Start());
74     }
75 
76     returnTypeAnnotation_->Check(checker);
77     checker::Type *returnType = returnTypeAnnotation_->AsTypeNode()->GetType(checker);
78 
79     auto *signature = checker->Allocator()->New<checker::Signature>(signatureInfo, returnType);
80 
81     checker::Type *placeholderObj = nullptr;
82 
83     if (isCallSignature) {
84         placeholderObj = checker->CreateObjectTypeWithCallSignature(signature);
85     } else {
86         placeholderObj = checker->CreateObjectTypeWithConstructSignature(signature);
87     }
88 
89     checker->NodeCache().insert({this, placeholderObj});
90 
91     return placeholderObj;
92 }
93 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)94 void TSSignatureDeclaration::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)
95 {
96     auto scopeCtx = binder::LexicalScope<binder::Scope>::Enter(binder, scope_);
97 
98     if (typeParams_) {
99         typeParams_ = std::get<ir::AstNode *>(cb(typeParams_))->AsTSTypeParameterDeclaration();
100     }
101 
102     for (auto iter = params_.begin(); iter != params_.end(); iter++) {
103         *iter = std::get<ir::AstNode *>(cb(*iter))->AsExpression();
104     }
105 
106     if (returnTypeAnnotation_) {
107         returnTypeAnnotation_ = std::get<ir::AstNode *>(cb(returnTypeAnnotation_))->AsExpression();
108     }
109 }
110 
111 }  // namespace panda::es2panda::ir
112