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 "scriptFunction.h"
17
18 #include <binder/binder.h>
19 #include <binder/scope.h>
20 #include <ir/astDump.h>
21 #include <ir/expression.h>
22 #include <ir/expressions/identifier.h>
23 #include <ir/statements/blockStatement.h>
24 #include <ir/ts/tsTypeParameter.h>
25 #include <ir/ts/tsTypeParameterDeclaration.h>
26
27 namespace panda::es2panda::ir {
28
FormalParamsLength() const29 size_t ScriptFunction::FormalParamsLength() const
30 {
31 size_t length = 0;
32
33 for (const auto *param : params_) {
34 if (param->IsRestElement() || param->IsAssignmentPattern()) {
35 break;
36 }
37
38 length++;
39 }
40
41 return length;
42 }
43
GetName() const44 util::StringView ScriptFunction::GetName() const
45 {
46 if (id_) {
47 return id_->Name();
48 }
49
50 if (exportDefault_) {
51 return parser::SourceTextModuleRecord::DEFAULT_LOCAL_NAME;
52 }
53
54 return "";
55 }
56
Iterate(const NodeTraverser & cb) const57 void ScriptFunction::Iterate(const NodeTraverser &cb) const
58 {
59 if (id_) {
60 cb(id_);
61 }
62
63 if (thisParam_) {
64 cb(thisParam_);
65 }
66
67 if (typeParams_) {
68 cb(typeParams_);
69 }
70
71 for (auto *it : params_) {
72 cb(it);
73 }
74
75 if (returnTypeAnnotation_) {
76 cb(returnTypeAnnotation_);
77 }
78
79 if (body_) {
80 cb(body_);
81 }
82 }
83
Dump(ir::AstDumper * dumper) const84 void ScriptFunction::Dump(ir::AstDumper *dumper) const
85 {
86 dumper->Add({{"type", "ScriptFunction"},
87 {"id", AstDumper::Nullable(id_)},
88 {"generator", IsGenerator()},
89 {"async", IsAsync()},
90 {"expression", ((flags_ & ir::ScriptFunctionFlags::EXPRESSION) != 0)},
91 {"params", params_},
92 {"returnType", AstDumper::Optional(returnTypeAnnotation_)},
93 {"typeParameters", AstDumper::Optional(typeParams_)},
94 {"declare", AstDumper::Optional(declare_)},
95 {"body", AstDumper::Optional(body_)}});
96 }
97
Compile(compiler::PandaGen * pg) const98 void ScriptFunction::Compile([[maybe_unused]] compiler::PandaGen *pg) const {}
99
Check(checker::Checker * checker) const100 checker::Type *ScriptFunction::Check([[maybe_unused]] checker::Checker *checker) const
101 {
102 return nullptr;
103 }
104
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)105 void ScriptFunction::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)
106 {
107 if (id_) {
108 id_ = std::get<ir::AstNode *>(cb(id_))->AsIdentifier();
109 }
110
111 auto paramScopeCtx = binder::LexicalScope<binder::FunctionParamScope>::Enter(binder, scope_->ParamScope());
112
113 if (thisParam_) {
114 thisParam_ = std::get<ir::AstNode *>(cb(thisParam_))->AsExpression();
115 }
116
117 if (typeParams_) {
118 typeParams_ = std::get<ir::AstNode *>(cb(typeParams_))->AsTSTypeParameterDeclaration();
119 }
120
121 for (auto iter = params_.begin(); iter != params_.end(); iter++) {
122 *iter = std::get<ir::AstNode *>(cb(*iter))->AsExpression();
123 }
124
125 if (returnTypeAnnotation_) {
126 returnTypeAnnotation_ = std::get<ir::AstNode *>(cb(returnTypeAnnotation_))->AsExpression();
127 }
128
129 auto scopeCtx = binder::LexicalScope<binder::FunctionScope>::Enter(binder, scope_);
130
131 if (body_) {
132 body_ = std::get<ir::AstNode *>(cb(body_));
133 }
134 }
135
136 } // namespace panda::es2panda::ir
137