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