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 "tsTypeReference.h"
17
18 #include <binder/declaration.h>
19 #include <binder/scope.h>
20 #include <binder/variable.h>
21 #include <typescript/checker.h>
22 #include <ir/astDump.h>
23 #include <ir/expressions/identifier.h>
24 #include <ir/ts/tsInterfaceDeclaration.h>
25 #include <ir/ts/tsTypeAliasDeclaration.h>
26 #include <ir/ts/tsTypeParameterInstantiation.h>
27 #include <ir/ts/tsEnumDeclaration.h>
28
29 namespace panda::es2panda::ir {
30
Iterate(const NodeTraverser & cb) const31 void TSTypeReference::Iterate(const NodeTraverser &cb) const
32 {
33 if (typeParams_) {
34 cb(typeParams_);
35 }
36
37 cb(typeName_);
38 }
39
Dump(ir::AstDumper * dumper) const40 void TSTypeReference::Dump(ir::AstDumper *dumper) const
41 {
42 dumper->Add(
43 {{"type", "TSTypeReference"}, {"typeName", typeName_}, {"typeParameters", AstDumper::Optional(typeParams_)}});
44 }
45
Compile(compiler::PandaGen * pg) const46 void TSTypeReference::Compile([[maybe_unused]] compiler::PandaGen *pg) const {}
47
Check(checker::Checker * checker) const48 checker::Type *TSTypeReference::Check(checker::Checker *checker) const
49 {
50 GetType(checker);
51 return nullptr;
52 }
53
GetType(checker::Checker * checker) const54 checker::Type *TSTypeReference::GetType(checker::Checker *checker) const
55 {
56 auto found = checker->NodeCache().find(this);
57
58 if (found != checker->NodeCache().end()) {
59 return found->second;
60 }
61
62 // TODO(aszilagyi): handle cases where type type_name_ is a QualifiedName
63 if (typeName_->IsTSQualifiedName()) {
64 return checker->GlobalAnyType();
65 }
66
67 ASSERT(typeName_->IsIdentifier());
68 binder::Variable *var = typeName_->AsIdentifier()->Variable();
69
70 if (!var) {
71 checker->ThrowTypeError({"Cannot find name ", typeName_->AsIdentifier()->Name()}, Start());
72 }
73
74 checker::Type *type = checker->GetTypeReferenceType(this, var);
75
76 checker->NodeCache().insert({this, type});
77 return type;
78 }
79
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)80 void TSTypeReference::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
81 {
82 if (typeParams_) {
83 typeParams_ = std::get<ir::AstNode *>(cb(typeParams_))->AsTSTypeParameterInstantiation();
84 }
85
86 typeName_ = std::get<ir::AstNode *>(cb(typeName_))->AsExpression();
87 }
88
89 } // namespace panda::es2panda::ir
90