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