• 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 "tsIndexSignature.h"
17 
18 #include <typescript/checker.h>
19 #include <ir/astDump.h>
20 #include <ir/typeNode.h>
21 #include <ir/expressions/identifier.h>
22 
23 namespace panda::es2panda::ir {
24 
Kind() const25 TSIndexSignature::TSIndexSignatureKind TSIndexSignature::Kind() const
26 {
27     return param_->AsIdentifier()->TypeAnnotation()->IsTSNumberKeyword() ? TSIndexSignatureKind::NUMBER
28                                                                          : TSIndexSignatureKind::STRING;
29 }
30 
Iterate(const NodeTraverser & cb) const31 void TSIndexSignature::Iterate(const NodeTraverser &cb) const
32 {
33     cb(param_);
34     cb(typeAnnotation_);
35 }
36 
Dump(ir::AstDumper * dumper) const37 void TSIndexSignature::Dump(ir::AstDumper *dumper) const
38 {
39     dumper->Add({{"type", "TSIndexSignature"},
40                  {"parameters", param_},
41                  {"typeAnnotation", typeAnnotation_},
42                  {"readonly", readonly_}});
43 }
44 
Compile(compiler::PandaGen * pg) const45 void TSIndexSignature::Compile([[maybe_unused]] compiler::PandaGen *pg) const {}
46 
Check(checker::Checker * checker) const47 checker::Type *TSIndexSignature::Check(checker::Checker *checker) const
48 {
49     auto found = checker->NodeCache().find(this);
50     if (found != checker->NodeCache().end()) {
51         return found->second;
52     }
53 
54     const util::StringView &paramName = param_->AsIdentifier()->Name();
55     typeAnnotation_->Check(checker);
56     checker::Type *indexType = typeAnnotation_->AsTypeNode()->GetType(checker);
57     checker::IndexInfo *info =
58         checker->Allocator()->New<checker::IndexInfo>(indexType, paramName, readonly_, this->Start());
59     checker::ObjectDescriptor *desc = checker->Allocator()->New<checker::ObjectDescriptor>(checker->Allocator());
60     checker::ObjectType *placeholder = checker->Allocator()->New<checker::ObjectLiteralType>(desc);
61 
62     if (Kind() == ir::TSIndexSignature::TSIndexSignatureKind::NUMBER) {
63         placeholder->Desc()->numberIndexInfo = info;
64     } else {
65         placeholder->Desc()->stringIndexInfo = info;
66     }
67 
68     checker->NodeCache().insert({this, placeholder});
69 
70     return placeholder;
71 }
72 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)73 void TSIndexSignature::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
74 {
75     param_ = std::get<ir::AstNode *>(cb(param_))->AsExpression();
76     typeAnnotation_ = std::get<ir::AstNode *>(cb(typeAnnotation_))->AsExpression();
77 }
78 
79 }  // namespace panda::es2panda::ir
80