• 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 "classProperty.h"
17 
18 #include "checker/ETSchecker.h"
19 #include "checker/TSchecker.h"
20 #include "compiler/core/ETSGen.h"
21 #include "compiler/core/pandagen.h"
22 #include "ir/astDump.h"
23 #include "ir/srcDump.h"
24 
25 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)26 void ClassProperty::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
27 {
28     if (auto *transformedNode = cb(key_); key_ != transformedNode) {
29         key_->SetTransformedNode(transformationName, transformedNode);
30         key_ = transformedNode->AsExpression();
31     }
32 
33     if (value_ != nullptr) {
34         if (auto *transformedNode = cb(value_); value_ != transformedNode) {
35             value_->SetTransformedNode(transformationName, transformedNode);
36             value_ = transformedNode->AsExpression();
37         }
38     }
39 
40     if (typeAnnotation_ != nullptr) {
41         if (auto *transformedNode = cb(typeAnnotation_); typeAnnotation_ != transformedNode) {
42             typeAnnotation_->SetTransformedNode(transformationName, transformedNode);
43             typeAnnotation_ = static_cast<TypeNode *>(transformedNode);
44         }
45     }
46 
47     for (auto *&it : decorators_) {
48         if (auto *transformedNode = cb(it); it != transformedNode) {
49             it->SetTransformedNode(transformationName, transformedNode);
50             it = transformedNode->AsDecorator();
51         }
52     }
53 }
54 
Iterate(const NodeTraverser & cb) const55 void ClassProperty::Iterate(const NodeTraverser &cb) const
56 {
57     cb(key_);
58 
59     if (value_ != nullptr) {
60         cb(value_);
61     }
62 
63     if (typeAnnotation_ != nullptr) {
64         cb(typeAnnotation_);
65     }
66 
67     for (auto *it : decorators_) {
68         cb(it);
69     }
70 }
71 
Dump(ir::AstDumper * dumper) const72 void ClassProperty::Dump(ir::AstDumper *dumper) const
73 {
74     dumper->Add({{"type", "ClassProperty"},
75                  {"key", key_},
76                  {"value", AstDumper::Optional(value_)},
77                  {"accessibility", AstDumper::Optional(AstDumper::ModifierToString(flags_))},
78                  {"abstract", AstDumper::Optional(IsAbstract())},
79                  {"static", IsStatic()},
80                  {"readonly", IsReadonly()},
81                  {"declare", IsDeclare()},
82                  {"optional", IsOptionalDeclaration()},
83                  {"computed", isComputed_},
84                  {"typeAnnotation", AstDumper::Optional(typeAnnotation_)},
85                  {"definite", IsDefinite()},
86                  {"decorators", decorators_}});
87 }
88 
Dump(ir::SrcDumper * dumper) const89 void ClassProperty::Dump(ir::SrcDumper *dumper) const
90 {
91     if (IsPrivate()) {
92         dumper->Add("private ");
93     } else if (IsProtected()) {
94         dumper->Add("protected ");
95     } else if (IsInternal()) {
96         dumper->Add("internal ");
97     } else {
98         dumper->Add("public ");
99     }
100 
101     if (IsStatic()) {
102         dumper->Add("static ");
103     }
104 
105     if (IsReadonly()) {
106         dumper->Add("readonly ");
107     }
108 
109     if (key_ != nullptr) {
110         key_->Dump(dumper);
111     }
112 
113     if (IsOptionalDeclaration()) {
114         dumper->Add("?");
115     }
116 
117     if (typeAnnotation_ != nullptr) {
118         dumper->Add(": ");
119         typeAnnotation_->Dump(dumper);
120     }
121 
122     if (value_ != nullptr) {
123         dumper->Add(" = ");
124         value_->Dump(dumper);
125     }
126 
127     dumper->Add(";");
128     dumper->Endl();
129 }
130 
Compile(compiler::PandaGen * pg) const131 void ClassProperty::Compile(compiler::PandaGen *pg) const
132 {
133     pg->GetAstCompiler()->Compile(this);
134 }
135 
Compile(compiler::ETSGen * etsg) const136 void ClassProperty::Compile(compiler::ETSGen *etsg) const
137 {
138     etsg->GetAstCompiler()->Compile(this);
139 }
140 
Check(checker::TSChecker * checker)141 checker::Type *ClassProperty::Check(checker::TSChecker *checker)
142 {
143     return checker->GetAnalyzer()->Check(this);
144 }
145 
Check(checker::ETSChecker * checker)146 checker::Type *ClassProperty::Check(checker::ETSChecker *checker)
147 {
148     return checker->GetAnalyzer()->Check(this);
149 }
150 
Clone(ArenaAllocator * const allocator,AstNode * const parent)151 ClassProperty *ClassProperty::Clone(ArenaAllocator *const allocator, AstNode *const parent)
152 {
153     auto *const key = key_->Clone(allocator, nullptr)->AsExpression();
154     auto *const value = value_ != nullptr ? value_->Clone(allocator, nullptr)->AsExpression() : nullptr;
155     auto *const typeAnnotation = typeAnnotation_ != nullptr ? typeAnnotation_->Clone(allocator, nullptr) : nullptr;
156 
157     if (auto *const clone = allocator->New<ClassProperty>(key, value, typeAnnotation, flags_, allocator, isComputed_);
158         clone != nullptr) {
159         if (parent != nullptr) {
160             clone->SetParent(parent);
161         }
162 
163         key->SetParent(clone);
164         if (value != nullptr) {
165             value->SetParent(clone);
166         }
167         if (typeAnnotation != nullptr) {
168             typeAnnotation->SetParent(clone);
169         }
170 
171         for (auto *const decorator : decorators_) {
172             clone->AddDecorator(decorator->Clone(allocator, clone));
173         }
174 
175         return clone;
176     }
177 
178     throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
179 }
180 }  // namespace ark::es2panda::ir
181