• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 "namedType.h"
17 
18 #include "checker/TSchecker.h"
19 #include "compiler/core/ETSGen.h"
20 #include "compiler/core/pandagen.h"
21 #include "ir/expressions/identifier.h"
22 #include "ir/ts/tsTypeParameterInstantiation.h"
23 #include "ir/astDump.h"
24 #include "ir/srcDump.h"
25 
26 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)27 void NamedType::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
28 {
29     if (auto *transformedNode = cb(name_); name_ != transformedNode) {
30         name_->SetTransformedNode(transformationName, transformedNode);
31         name_ = transformedNode->AsIdentifier();
32     }
33 
34     if (typeParams_ != nullptr) {
35         if (auto *transformedNode = cb(typeParams_); typeParams_ != transformedNode) {
36             typeParams_->SetTransformedNode(transformationName, transformedNode);
37             typeParams_ = transformedNode->AsTSTypeParameterInstantiation();
38         }
39     }
40 
41     if (next_ != nullptr) {
42         if (auto *transformedNode = cb(next_); next_ != transformedNode) {
43             next_->SetTransformedNode(transformationName, transformedNode);
44             next_ = transformedNode->AsNamedType();
45         }
46     }
47 }
48 
Iterate(const NodeTraverser & cb) const49 void NamedType::Iterate(const NodeTraverser &cb) const
50 {
51     cb(name_);
52 
53     if (typeParams_ != nullptr) {
54         cb(typeParams_);
55     }
56 
57     if (next_ != nullptr) {
58         cb(next_);
59     }
60 }
61 
Dump(AstDumper * dumper) const62 void NamedType::Dump(AstDumper *dumper) const
63 {
64     dumper->Add({{"type", "NamedType"},
65                  {"name", name_},
66                  {"typeParameters", AstDumper::Optional(typeParams_)},
67                  {"next", AstDumper::Optional(next_)},
68                  {"isNullable", nullable_}});
69 }
70 
Dump(SrcDumper * dumper) const71 void NamedType::Dump(SrcDumper *dumper) const
72 {
73     dumper->Add("NamedType");
74 }
75 
Compile(compiler::PandaGen * pg) const76 void NamedType::Compile(compiler::PandaGen *pg) const
77 {
78     pg->GetAstCompiler()->Compile(this);
79 }
80 
Compile(compiler::ETSGen * etsg) const81 void NamedType::Compile(compiler::ETSGen *etsg) const
82 {
83     etsg->GetAstCompiler()->Compile(this);
84 }
85 
Check(checker::TSChecker * checker)86 checker::Type *NamedType::Check(checker::TSChecker *checker)
87 {
88     return checker->GetAnalyzer()->Check(this);
89 }
90 
Check(checker::ETSChecker * checker)91 checker::Type *NamedType::Check(checker::ETSChecker *checker)
92 {
93     return checker->GetAnalyzer()->Check(this);
94 }
95 }  // namespace ark::es2panda::ir
96