• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 - 2023 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 "checker/types/ets/etsObjectType.h"
21 #include "compiler/core/ETSGen.h"
22 #include "compiler/core/pandagen.h"
23 #include "ir/astDump.h"
24 #include "ir/srcDump.h"
25 #include "ir/base/decorator.h"
26 #include "ir/typeNode.h"
27 #include "ir/expression.h"
28 #include "ir/expressions/identifier.h"
29 
30 namespace panda::es2panda::ir {
TransformChildren(const NodeTransformer & cb)31 void ClassProperty::TransformChildren(const NodeTransformer &cb)
32 {
33     key_ = cb(key_)->AsExpression();
34 
35     if (value_ != nullptr) {
36         value_ = cb(value_)->AsExpression();
37     }
38 
39     if (typeAnnotation_ != nullptr) {
40         typeAnnotation_ = static_cast<TypeNode *>(cb(typeAnnotation_));
41     }
42 
43     for (auto *&it : decorators_) {
44         it = cb(it)->AsDecorator();
45     }
46 }
47 
Iterate(const NodeTraverser & cb) const48 void ClassProperty::Iterate(const NodeTraverser &cb) const
49 {
50     cb(key_);
51 
52     if (value_ != nullptr) {
53         cb(value_);
54     }
55 
56     if (typeAnnotation_ != nullptr) {
57         cb(typeAnnotation_);
58     }
59 
60     for (auto *it : decorators_) {
61         cb(it);
62     }
63 }
64 
Dump(ir::AstDumper * dumper) const65 void ClassProperty::Dump(ir::AstDumper *dumper) const
66 {
67     dumper->Add({{"type", "ClassProperty"},
68                  {"key", key_},
69                  {"value", AstDumper::Optional(value_)},
70                  {"accessibility", AstDumper::Optional(AstDumper::ModifierToString(flags_))},
71                  {"abstract", AstDumper::Optional(IsAbstract())},
72                  {"static", IsStatic()},
73                  {"readonly", IsReadonly()},
74                  {"declare", IsDeclare()},
75                  {"optional", IsOptionalDeclaration()},
76                  {"computed", isComputed_},
77                  {"typeAnnotation", AstDumper::Optional(typeAnnotation_)},
78                  {"definite", IsDefinite()},
79                  {"decorators", decorators_}});
80 }
81 
Dump(ir::SrcDumper * dumper) const82 void ClassProperty::Dump(ir::SrcDumper *dumper) const
83 {
84     if (IsPrivate()) {
85         dumper->Add("private ");
86     } else if (IsProtected()) {
87         dumper->Add("protected ");
88     } else if (IsInternal()) {
89         dumper->Add("internal ");
90     } else {
91         dumper->Add("public ");
92     }
93 
94     if (IsStatic()) {
95         dumper->Add("static ");
96     }
97 
98     if (IsReadonly()) {
99         dumper->Add("readonly ");
100     }
101 
102     if (key_ != nullptr) {
103         key_->Dump(dumper);
104     }
105 
106     if (typeAnnotation_ != nullptr) {
107         dumper->Add(": ");
108         typeAnnotation_->Dump(dumper);
109     }
110 
111     if (value_ != nullptr) {
112         dumper->Add(" = ");
113         value_->Dump(dumper);
114     }
115 
116     dumper->Add(";");
117 }
118 
Compile(compiler::PandaGen * pg) const119 void ClassProperty::Compile(compiler::PandaGen *pg) const
120 {
121     pg->GetAstCompiler()->Compile(this);
122 }
123 
Compile(compiler::ETSGen * etsg) const124 void ClassProperty::Compile(compiler::ETSGen *etsg) const
125 {
126     etsg->GetAstCompiler()->Compile(this);
127 }
128 
Check(checker::TSChecker * checker)129 checker::Type *ClassProperty::Check(checker::TSChecker *checker)
130 {
131     return checker->GetAnalyzer()->Check(this);
132 }
133 
Check(checker::ETSChecker * checker)134 checker::Type *ClassProperty::Check(checker::ETSChecker *checker)
135 {
136     return checker->GetAnalyzer()->Check(this);
137 }
138 
139 // NOLINTNEXTLINE(google-default-arguments)
Clone(ArenaAllocator * const allocator,AstNode * const parent)140 ClassProperty *ClassProperty::Clone(ArenaAllocator *const allocator, AstNode *const parent)
141 {
142     auto *const key = key_->Clone(allocator)->AsExpression();
143     auto *const value = value_->Clone(allocator)->AsExpression();
144     auto *const typeAnnotation = typeAnnotation_->Clone(allocator, this);
145 
146     if (auto *const clone = allocator->New<ClassProperty>(key, value, typeAnnotation, flags_, allocator, isComputed_);
147         clone != nullptr) {
148         if (parent != nullptr) {
149             clone->SetParent(parent);
150         }
151 
152         key->SetParent(clone);
153         value->SetParent(clone);
154         typeAnnotation->SetParent(clone);
155 
156         for (auto *const decorator : decorators_) {
157             clone->AddDecorator(decorator->Clone(allocator, clone));
158         }
159 
160         return clone;
161     }
162 
163     throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
164 }
165 }  // namespace panda::es2panda::ir
166