• 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 "etsUnionType.h"
17 
18 #include "checker/ETSchecker.h"
19 #include "ir/astDump.h"
20 #include "ir/srcDump.h"
21 
22 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)23 void ETSUnionType::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
24 {
25     for (auto *&it : types_) {
26         if (auto *transformedNode = cb(it); it != transformedNode) {
27             it->SetTransformedNode(transformationName, transformedNode);
28             it = static_cast<TypeNode *>(transformedNode);
29         }
30     }
31 }
32 
Iterate(const NodeTraverser & cb) const33 void ETSUnionType::Iterate(const NodeTraverser &cb) const
34 {
35     for (auto *it : types_) {
36         cb(it);
37     }
38 }
39 
Dump(ir::AstDumper * dumper) const40 void ETSUnionType::Dump(ir::AstDumper *dumper) const
41 {
42     dumper->Add({{"type", "ETSUnionType"}, {"types", types_}});
43 }
44 
Dump(ir::SrcDumper * dumper) const45 void ETSUnionType::Dump(ir::SrcDumper *dumper) const
46 {
47     for (auto type : types_) {
48         type->Dump(dumper);
49         if (type != types_.back()) {
50             dumper->Add(" | ");
51         }
52     }
53 }
54 
Compile(compiler::PandaGen * pg) const55 void ETSUnionType::Compile([[maybe_unused]] compiler::PandaGen *pg) const {}
56 
Check(checker::TSChecker * checker)57 checker::Type *ETSUnionType::Check([[maybe_unused]] checker::TSChecker *checker)
58 {
59     return nullptr;
60 }
61 
Check(checker::ETSChecker * checker)62 checker::Type *ETSUnionType::Check(checker::ETSChecker *checker)
63 {
64     for (auto *it : types_) {
65         it->Check(checker);
66     }
67 
68     return GetType(checker);
69 }
70 
GetType(checker::ETSChecker * checker)71 checker::Type *ETSUnionType::GetType(checker::ETSChecker *checker)
72 {
73     if (TsType() != nullptr) {
74         return TsType();
75     }
76 
77     ArenaVector<checker::Type *> types(checker->Allocator()->Adapter());
78 
79     for (auto *it : types_) {
80         types.push_back(it->GetType(checker));
81     }
82 
83     checker->Relation()->SetNode(this);
84     SetTsType(checker->CreateETSUnionType(std::move(types)));
85     checker->Relation()->SetNode(nullptr);
86     return TsType();
87 }
88 
Clone(ArenaAllocator * const allocator,AstNode * const parent)89 ETSUnionType *ETSUnionType::Clone(ArenaAllocator *const allocator, AstNode *const parent)
90 {
91     ArenaVector<ir::TypeNode *> types(allocator->Adapter());
92     for (auto *it : types_) {
93         auto *type = it->Clone(allocator, nullptr);
94         types.push_back(type);
95     }
96     ETSUnionType *const clone = allocator->New<ir::ETSUnionType>(std::move(types));
97     if (parent != nullptr) {
98         clone->SetParent(parent);
99     }
100     for (auto *it : clone->types_) {
101         it->SetParent(clone);
102     }
103     return clone;
104 }
105 }  // namespace ark::es2panda::ir
106