• 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 "scriptFunction.h"
17 
18 #include <ir/astDump.h>
19 #include <ir/base/methodDefinition.h>
20 #include <ir/expressions/assignmentExpression.h>
21 #include <ir/expressions/memberExpression.h>
22 #include <ir/statements/blockStatement.h>
23 #include <ir/statements/expressionStatement.h>
24 #include <ir/ts/tsTypeParameterDeclaration.h>
25 
26 namespace panda::es2panda::ir {
27 
FormalParamsLength() const28 size_t ScriptFunction::FormalParamsLength() const
29 {
30     size_t length = 0;
31 
32     for (const auto *param : params_) {
33         if (param->IsRestElement() || param->IsAssignmentPattern()) {
34             break;
35         }
36 
37         length++;
38     }
39 
40     return length;
41 }
42 
GetName() const43 util::StringView ScriptFunction::GetName() const
44 {
45     if (id_) {
46         return id_->Name();
47     }
48 
49     if (exportDefault_) {
50         return parser::SourceTextModuleRecord::DEFAULT_LOCAL_NAME;
51     }
52 
53     return "";
54 }
55 
Iterate(const NodeTraverser & cb) const56 void ScriptFunction::Iterate(const NodeTraverser &cb) const
57 {
58     if (id_) {
59         cb(id_);
60     }
61 
62     if (typeParams_) {
63         cb(typeParams_);
64     }
65 
66     if (thisParam_) {
67         cb(thisParam_);
68     }
69 
70     for (auto *it : params_) {
71         cb(it);
72     }
73 
74     if (returnTypeAnnotation_) {
75         cb(returnTypeAnnotation_);
76     }
77 
78     if (body_) {
79         cb(body_);
80     }
81 }
82 
Dump(ir::AstDumper * dumper) const83 void ScriptFunction::Dump(ir::AstDumper *dumper) const
84 {
85     dumper->Add({{"type", "ScriptFunction"},
86                  {"id", AstDumper::Nullable(id_)},
87                  {"generator", IsGenerator()},
88                  {"async", IsAsync()},
89                  {"expression", ((flags_ & ir::ScriptFunctionFlags::EXPRESSION) != 0)},
90                  {"thisParam", AstDumper::Optional(thisParam_)},
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 (typeParams_) {
114         typeParams_ = std::get<ir::AstNode *>(cb(typeParams_))->AsTSTypeParameterDeclaration();
115     }
116 
117     if (thisParam_) {
118         thisParam_ = std::get<ir::AstNode *>(cb(thisParam_))->AsExpression();
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 
SourceCode(binder::Binder * binder) const136 util::StringView ScriptFunction::SourceCode(binder::Binder *binder) const
137 {
138     auto *funcNode = this->Parent();
139     if (funcNode->IsFunctionExpression() &&
140         funcNode->Parent()->IsMethodDefinition() &&
141         funcNode->Parent()->AsMethodDefinition()->Value() == funcNode->AsFunctionExpression()) {
142         funcNode = funcNode->Parent();
143     }
144 
145     auto startIndex = funcNode->Start().index;
146     auto endIndex = funcNode->End().index;
147     return binder->Program()->SourceCode().Substr(startIndex, endIndex);
148 }
149 
CalculateFunctionExpectedPropertyCount()150 void ScriptFunction::CalculateFunctionExpectedPropertyCount()
151 {
152     // Counting more or less than the actual number does not affect execution.
153     if (Body() == nullptr) {
154         return;
155     }
156 
157     std::unordered_set<util::StringView> propertyNames;
158     auto addPropertyName = [this, &propertyNames](const util::StringView &name) {
159         if (propertyNames.find(name) == propertyNames.end()) {
160             propertyNames.insert(name);
161             IncreasePropertyCount();
162         }
163     };
164 
165     const ir::BlockStatement *blockStat = Body()->AsBlockStatement();
166     for (const auto &stmt : blockStat->Statements()) {
167         ExtractThisPropertyFromStatement(stmt, addPropertyName);
168     }
169 }
170 
ExtractThisPropertyFromStatement(const ir::Statement * stmt,const std::function<void (const util::StringView &)> & addPropertyName)171 void ScriptFunction::ExtractThisPropertyFromStatement(
172     const ir::Statement *stmt,
173     const std::function<void(const util::StringView&)>& addPropertyName)
174 {
175     if (!stmt->IsExpressionStatement()) {
176         return;
177     }
178 
179     const auto *expr = stmt->AsExpressionStatement()->GetExpression();
180     if (!expr || !expr->IsAssignmentExpression()) {
181         return;
182     }
183 
184     const auto *left = expr->AsAssignmentExpression()->Left();
185     if (!left || !left->IsMemberExpression()) {
186         return;
187     }
188 
189     const auto *memberExpr = left->AsMemberExpression();
190     if (memberExpr->Object()->IsThisExpression() && memberExpr->Property()->IsIdentifier()) {
191         addPropertyName(memberExpr->Property()->AsIdentifier()->Name());
192     }
193 }
194 }  // namespace panda::es2panda::ir
195