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
Iterate(const NodeTraverser & cb) const33 void MethodDefinition::Iterate(const NodeTraverser &cb) const
34 {
35 cb(key_);
36 cb(value_);
37
38 for (auto *it : overloads_) {
39 cb(it);
40 }
41
42 for (auto *it : decorators_) {
43 cb(it);
44 }
45 }
46
Dump(ir::AstDumper * dumper) const47 void MethodDefinition::Dump(ir::AstDumper *dumper) const
48 {
49 const char *kind = nullptr;
50
51 switch (kind_) {
52 case MethodDefinitionKind::CONSTRUCTOR: {
53 kind = "constructor";
54 break;
55 }
56 case MethodDefinitionKind::METHOD: {
57 kind = "method";
58 break;
59 }
60 case MethodDefinitionKind::GET: {
61 kind = "get";
62 break;
63 }
64 case MethodDefinitionKind::SET: {
65 kind = "set";
66 break;
67 }
68 default: {
69 UNREACHABLE();
70 }
71 }
72
73 dumper->Add({{"type", "MethodDefinition"},
74 {"key", key_},
75 {"kind", kind},
76 {"accessibility", AstDumper::Optional(AstDumper::ModifierToString(modifiers_))},
77 {"static", (modifiers_ & ModifierFlags::STATIC) != 0},
78 {"optional", (modifiers_ & ModifierFlags::OPTIONAL) != 0},
79 {"computed", isComputed_},
80 {"value", value_},
81 {"overloads", overloads_},
82 {"decorators", decorators_}});
83 }
84
Compile(compiler::PandaGen * pg) const85 void MethodDefinition::Compile([[maybe_unused]] compiler::PandaGen *pg) const {}
86
Check(checker::Checker * checker) const87 checker::Type *MethodDefinition::Check([[maybe_unused]] checker::Checker *checker) const
88 {
89 return nullptr;
90 }
91
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)92 void MethodDefinition::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
93 {
94 key_ = std::get<ir::AstNode *>(cb(key_))->AsExpression();
95 value_ = std::get<ir::AstNode *>(cb(value_))->AsFunctionExpression();
96
97 for (auto iter = overloads_.begin(); iter != overloads_.end(); iter++) {
98 *iter = std::get<ir::AstNode *>(cb(*iter))->AsMethodDefinition();
99 }
100
101 for (auto iter = decorators_.begin(); iter != decorators_.end(); iter++) {
102 *iter = std::get<ir::AstNode *>(cb(*iter))->AsDecorator();
103 }
104 }
105
106 } // namespace panda::es2panda::ir
107