1 /**
2 * Copyright (c) 2021-2025 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 "functionExpression.h"
17
18 #include "checker/TSchecker.h"
19 #include "compiler/core/ETSGen.h"
20 #include "compiler/core/pandagen.h"
21
22 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)23 void FunctionExpression::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
24 {
25 if (auto *transformedNode = cb(func_); func_ != transformedNode) {
26 func_->SetTransformedNode(transformationName, transformedNode);
27 func_ = transformedNode->AsScriptFunction();
28 }
29 }
30
Iterate(const NodeTraverser & cb) const31 void FunctionExpression::Iterate(const NodeTraverser &cb) const
32 {
33 cb(func_);
34 }
35
Dump(ir::AstDumper * dumper) const36 void FunctionExpression::Dump(ir::AstDumper *dumper) const
37 {
38 dumper->Add({{"type", "FunctionExpression"}, {"function", func_}});
39 }
40
Dump(ir::SrcDumper * dumper) const41 void FunctionExpression::Dump(ir::SrcDumper *dumper) const
42 {
43 func_->Dump(dumper);
44 }
45
Compile(compiler::PandaGen * pg) const46 void FunctionExpression::Compile(compiler::PandaGen *pg) const
47 {
48 pg->GetAstCompiler()->Compile(this);
49 }
50
Compile(compiler::ETSGen * etsg) const51 void FunctionExpression::Compile(compiler::ETSGen *etsg) const
52 {
53 etsg->GetAstCompiler()->Compile(this);
54 }
55
Check(checker::TSChecker * checker)56 checker::Type *FunctionExpression::Check(checker::TSChecker *checker)
57 {
58 return checker->GetAnalyzer()->Check(this);
59 }
60
Check(checker::ETSChecker * checker)61 checker::VerifiedType FunctionExpression::Check([[maybe_unused]] checker::ETSChecker *checker)
62 {
63 return {this, checker->GetAnalyzer()->Check(this)};
64 }
65
Clone(ArenaAllocator * const allocator,AstNode * const parent)66 FunctionExpression *FunctionExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
67 {
68 auto *const func = func_->Clone(allocator, nullptr)->AsScriptFunction();
69 auto *const clone = allocator->New<FunctionExpression>(func);
70 ES2PANDA_ASSERT(clone != nullptr);
71 func->SetParent(clone);
72 if (parent != nullptr) {
73 clone->SetParent(parent);
74 }
75 clone->SetRange(Range());
76 return clone;
77 }
78
Construct(ArenaAllocator * allocator)79 AstNode *FunctionExpression::Construct(ArenaAllocator *allocator)
80 {
81 return allocator->New<FunctionExpression>(nullptr);
82 }
83
CopyTo(AstNode * other) const84 void FunctionExpression::CopyTo(AstNode *other) const
85 {
86 auto otherImpl = other->AsFunctionExpression();
87
88 otherImpl->func_ = func_;
89 otherImpl->exprName_ = exprName_;
90
91 Expression::CopyTo(other);
92 }
93 } // namespace ark::es2panda::ir
94