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 "methodDefinition.h"
17
18#include <ir/astDump.h>
19#include <ir/base/annotation.h>
20#include <ir/base/decorator.h>
21#include <ir/base/scriptFunction.h>
22#include <ir/expression.h>
23#include <ir/expressions/callExpression.h>
24#include <ir/expressions/functionExpression.h>
25
26#include <utility>
27
28namespace panda::es2panda::ir {
29
30const ScriptFunction *MethodDefinition::Function() const
31{
32    return value_->Function();
33}
34
35ScriptFunction *MethodDefinition::Function()
36{
37    return value_->Function();
38}
39
40void MethodDefinition::Iterate(const NodeTraverser &cb) const
41{
42    cb(key_);
43    cb(value_);
44
45    for (auto *it : overloads_) {
46        cb(it);
47    }
48
49    for (auto *it : decorators_) {
50        cb(it);
51    }
52
53    for (auto *it : annotations_) {
54        cb(it);
55    }
56
57    for (auto param : paramDecorators_) {
58        for (auto *it : param.decorators) {
59            cb(it);
60        }
61    }
62}
63
64void MethodDefinition::Dump(ir::AstDumper *dumper) const
65{
66    const char *kind = nullptr;
67
68    switch (kind_) {
69        case MethodDefinitionKind::CONSTRUCTOR: {
70            kind = "constructor";
71            break;
72        }
73        case MethodDefinitionKind::METHOD: {
74            kind = "method";
75            break;
76        }
77        case MethodDefinitionKind::GET: {
78            kind = "get";
79            break;
80        }
81        case MethodDefinitionKind::SET: {
82            kind = "set";
83            break;
84        }
85        default: {
86            UNREACHABLE();
87        }
88    }
89
90    dumper->Add({{"type", "MethodDefinition"},
91                 {"key", key_},
92                 {"kind", kind},
93                 {"accessibility", AstDumper::Optional(AstDumper::ModifierToString(modifiers_))},
94                 {"abstract", AstDumper::Optional((modifiers_ & ModifierFlags::ABSTRACT) != 0)},
95                 {"static", (modifiers_ & ModifierFlags::STATIC) != 0},
96                 {"optional", (modifiers_ & ModifierFlags::OPTIONAL) != 0},
97                 {"override", AstDumper::Optional((modifiers_ & ModifierFlags::OVERRIDE) != 0)},
98                 {"computed", isComputed_},
99                 {"value", value_},
100                 {"overloads", overloads_},
101                 {"decorators", decorators_},
102                 {"annotations", annotations_}});
103}
104
105void MethodDefinition::Compile([[maybe_unused]] compiler::PandaGen *pg) const {}
106
107checker::Type *MethodDefinition::Check([[maybe_unused]] checker::Checker *checker) const
108{
109    return nullptr;
110}
111
112void MethodDefinition::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
113{
114    key_ = std::get<ir::AstNode *>(cb(key_))->AsExpression();
115    value_ = std::get<ir::AstNode *>(cb(value_))->AsFunctionExpression();
116
117    for (auto iter = overloads_.begin(); iter != overloads_.end(); iter++) {
118        *iter = std::get<ir::AstNode *>(cb(*iter))->AsMethodDefinition();
119    }
120
121    for (auto iter = decorators_.begin(); iter != decorators_.end(); iter++) {
122        *iter = std::get<ir::AstNode *>(cb(*iter))->AsDecorator();
123    }
124
125    for (auto param : paramDecorators_) {
126        for (auto iter = param.decorators.begin(); iter != param.decorators.end(); iter++) {
127            *iter = std::get<ir::AstNode *>(cb(*iter))->AsDecorator();
128        }
129    }
130}
131
132}  // namespace panda::es2panda::ir
133