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