• 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 "tsIndexedAccessType.h"
17 
18 #include <typescript/checker.h>
19 #include <ir/astDump.h>
20 #include <ir/typeNode.h>
21 
22 namespace panda::es2panda::ir {
23 
Iterate(const NodeTraverser & cb) const24 void TSIndexedAccessType::Iterate(const NodeTraverser &cb) const
25 {
26     cb(objectType_);
27     cb(indexType_);
28 }
29 
Dump(ir::AstDumper * dumper) const30 void TSIndexedAccessType::Dump(ir::AstDumper *dumper) const
31 {
32     dumper->Add({{"type", "TSIndexedAccessType"}, {"objectType", objectType_}, {"indexType", indexType_}});
33 }
34 
Compile(compiler::PandaGen * pg) const35 void TSIndexedAccessType::Compile([[maybe_unused]] compiler::PandaGen *pg) const {}
36 
Check(checker::Checker * checker) const37 checker::Type *TSIndexedAccessType::Check(checker::Checker *checker) const
38 {
39     objectType_->Check(checker);
40     indexType_->Check(checker);
41     checker::Type *resolved = GetType(checker);
42 
43     if (resolved) {
44         return nullptr;
45     }
46 
47     checker::Type *indexType = checker->CheckTypeCached(indexType_);
48 
49     if (!indexType->HasTypeFlag(checker::TypeFlag::STRING_LIKE | checker::TypeFlag::NUMBER_LIKE)) {
50         checker->ThrowTypeError({"Type ", indexType, " cannot be used as index type"}, indexType_->Start());
51     }
52 
53     if (indexType->IsNumberType()) {
54         checker->ThrowTypeError("Type has no matching singature for type 'number'", Start());
55     }
56 
57     checker->ThrowTypeError("Type has no matching singature for type 'string'", Start());
58     return nullptr;
59 }
60 
GetType(checker::Checker * checker) const61 checker::Type *TSIndexedAccessType::GetType(checker::Checker *checker) const
62 {
63     auto found = checker->NodeCache().find(this);
64 
65     if (found != checker->NodeCache().end()) {
66         return found->second;
67     }
68 
69     checker::Type *baseType = objectType_->AsTypeNode()->GetType(checker);
70     checker::Type *indexType = indexType_->AsTypeNode()->GetType(checker);
71     checker::Type *resolved = checker->GetPropertyTypeForIndexType(baseType, indexType);
72 
73     checker->NodeCache().insert({this, resolved});
74 
75     return resolved;
76 }
77 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)78 void TSIndexedAccessType::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
79 {
80     objectType_ = std::get<ir::AstNode *>(cb(objectType_))->AsExpression();
81     indexType_ = std::get<ir::AstNode *>(cb(indexType_))->AsExpression();
82 }
83 
84 }  // namespace panda::es2panda::ir
85