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 "classProperty.h"
17
18 #include "ir/astDump.h"
19 #include "ir/base/decorator.h"
20 #include "ir/base/scriptFunction.h"
21
22 namespace panda::es2panda::ir {
23
Iterate(const NodeTraverser & cb) const24 void ClassProperty::Iterate(const NodeTraverser &cb) const
25 {
26 cb(key_);
27
28 if (value_) {
29 cb(value_);
30 }
31
32 if (typeAnnotation_) {
33 cb(typeAnnotation_);
34 }
35
36 for (auto *it : decorators_) {
37 cb(it);
38 }
39 }
40
Dump(ir::AstDumper * dumper) const41 void ClassProperty::Dump(ir::AstDumper *dumper) const
42 {
43 dumper->Add({{"type", "ClassProperty"},
44 {"key", key_},
45 {"value", AstDumper::Optional(value_)},
46 {"accessibility", AstDumper::Optional(AstDumper::ModifierToString(modifiers_))},
47 {"abstract", AstDumper::Optional((modifiers_ & ModifierFlags::ABSTRACT) != 0)},
48 {"static", (modifiers_ & ModifierFlags::STATIC) != 0},
49 {"readonly", (modifiers_ & ModifierFlags::READONLY) != 0},
50 {"override", AstDumper::Optional((modifiers_ & ModifierFlags::OVERRIDE) != 0)},
51 {"declare", (modifiers_ & ModifierFlags::DECLARE) != 0},
52 {"optional", (modifiers_ & ModifierFlags::OPTIONAL) != 0},
53 {"computed", isComputed_},
54 {"typeAnnotation", AstDumper::Optional(typeAnnotation_)},
55 {"definite", AstDumper::Optional(definite_)},
56 {"decorators", decorators_}});
57 }
58
Compile(compiler::PandaGen * pg) const59 void ClassProperty::Compile([[maybe_unused]] compiler::PandaGen *pg) const {}
60
Check(checker::Checker * checker) const61 checker::Type *ClassProperty::Check([[maybe_unused]] checker::Checker *checker) const
62 {
63 return nullptr;
64 }
65
UpdateChildNodes(const NodeUpdater & cb)66 void ClassProperty::UpdateChildNodes(const NodeUpdater &cb)
67 {
68 key_ = std::get<ir::AstNode *>(cb(key_))->AsExpression();
69
70 if (value_) {
71 value_ = std::get<ir::AstNode *>(cb(value_))->AsExpression();
72 }
73
74 if (typeAnnotation_) {
75 typeAnnotation_ = std::get<ir::AstNode *>(cb(typeAnnotation_))->AsExpression();
76 }
77
78 for (auto iter = decorators_.begin(); iter != decorators_.end(); iter++) {
79 *iter = std::get<ir::AstNode *>(cb(*iter))->AsDecorator();
80 }
81 }
82
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)83 void ClassProperty::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)
84 {
85 if (!IsStatic()) {
86 const ir::ScriptFunction *ctor = util::Helpers::GetContainingConstructor(this);
87 CHECK_NOT_NULL(ctor);
88 auto scopeCtx = binder::LexicalScope<binder::FunctionScope>::Enter(binder, ctor->Scope());
89
90 UpdateChildNodes(cb);
91 } else {
92 UpdateChildNodes(cb);
93 }
94 }
95
96 } // namespace panda::es2panda::ir
97