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