1 /**
2 * Copyright (c) 2021 - 2023 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 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23
24 namespace panda::es2panda::ir {
TransformChildren(const NodeTransformer & cb)25 void FunctionExpression::TransformChildren(const NodeTransformer &cb)
26 {
27 func_ = cb(func_)->AsScriptFunction();
28 }
29
Iterate(const NodeTraverser & cb) const30 void FunctionExpression::Iterate(const NodeTraverser &cb) const
31 {
32 cb(func_);
33 }
34
Dump(ir::AstDumper * dumper) const35 void FunctionExpression::Dump(ir::AstDumper *dumper) const
36 {
37 dumper->Add({{"type", "FunctionExpression"}, {"function", func_}});
38 }
39
Dump(ir::SrcDumper * dumper) const40 void FunctionExpression::Dump(ir::SrcDumper *dumper) const
41 {
42 func_->Dump(dumper);
43 }
44
Compile(compiler::PandaGen * pg) const45 void FunctionExpression::Compile(compiler::PandaGen *pg) const
46 {
47 pg->GetAstCompiler()->Compile(this);
48 }
49
Compile(compiler::ETSGen * etsg) const50 void FunctionExpression::Compile(compiler::ETSGen *etsg) const
51 {
52 etsg->GetAstCompiler()->Compile(this);
53 }
54
Check(checker::TSChecker * checker)55 checker::Type *FunctionExpression::Check(checker::TSChecker *checker)
56 {
57 return checker->GetAnalyzer()->Check(this);
58 }
59
Check(checker::ETSChecker * checker)60 checker::Type *FunctionExpression::Check([[maybe_unused]] checker::ETSChecker *checker)
61 {
62 return checker->GetAnalyzer()->Check(this);
63 }
64
65 // NOLINTNEXTLINE(google-default-arguments)
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)->AsScriptFunction();
69
70 if (auto *const clone = allocator->New<FunctionExpression>(func); clone != nullptr) {
71 func->SetParent(clone);
72 if (parent != nullptr) {
73 clone->SetParent(parent);
74 }
75 return clone;
76 }
77
78 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
79 }
80 } // namespace panda::es2panda::ir
81