• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "classStaticBlock.h"
17 
18 #include "checker/ETSchecker.h"
19 #include "checker/TSchecker.h"
20 #include "compiler/core/ETSGen.h"
21 #include "compiler/core/pandagen.h"
22 #include "ir/astDump.h"
23 #include "ir/srcDump.h"
24 #include "ir/base/decorator.h"
25 #include "ir/base/scriptFunction.h"
26 #include "ir/expression.h"
27 #include "ir/expressions/identifier.h"
28 #include "ir/expressions/functionExpression.h"
29 
30 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)31 void ClassStaticBlock::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
32 {
33     if (auto *transformedNode = cb(value_); value_ != transformedNode) {
34         value_->SetTransformedNode(transformationName, transformedNode);
35         value_ = transformedNode->AsExpression();
36     }
37 }
38 
Iterate(const NodeTraverser & cb) const39 void ClassStaticBlock::Iterate(const NodeTraverser &cb) const
40 {
41     cb(value_);
42 }
43 
Dump(ir::AstDumper * dumper) const44 void ClassStaticBlock::Dump(ir::AstDumper *dumper) const
45 {
46     dumper->Add({{"type", "ClassStaticBlock"}, {"value", value_}});
47 }
48 
Dump(ir::SrcDumper * dumper) const49 void ClassStaticBlock::Dump([[maybe_unused]] ir::SrcDumper *dumper) const
50 {
51     ES2PANDA_ASSERT(value_);
52     ES2PANDA_ASSERT(value_->IsFunctionExpression());
53     ES2PANDA_ASSERT(value_->AsFunctionExpression()->Function()->IsScriptFunction());
54     dumper->Add("static {");
55     dumper->IncrIndent();
56     dumper->Endl();
57     const auto *scriptFunc = value_->AsFunctionExpression()->Function()->AsScriptFunction();
58     ES2PANDA_ASSERT(scriptFunc->HasBody());
59     scriptFunc->Body()->Dump(dumper);
60     dumper->DecrIndent();
61     dumper->Endl();
62     dumper->Add("}");
63 }
64 
Compile(compiler::PandaGen * pg) const65 void ClassStaticBlock::Compile(compiler::PandaGen *pg) const
66 {
67     pg->GetAstCompiler()->Compile(this);
68 }
69 
Compile(compiler::ETSGen * etsg) const70 void ClassStaticBlock::Compile(compiler::ETSGen *etsg) const
71 {
72     etsg->GetAstCompiler()->Compile(this);
73 }
74 
Check(checker::TSChecker * checker)75 checker::Type *ClassStaticBlock::Check(checker::TSChecker *checker)
76 {
77     return checker->GetAnalyzer()->Check(this);
78 }
79 
Check(checker::ETSChecker * checker)80 checker::VerifiedType ClassStaticBlock::Check(checker::ETSChecker *checker)
81 {
82     return {this, checker->GetAnalyzer()->Check(this)};
83 }
84 
Function()85 ir::ScriptFunction *ClassStaticBlock::Function()
86 {
87     return value_->AsFunctionExpression()->Function();
88 }
89 
Function() const90 const ir::ScriptFunction *ClassStaticBlock::Function() const
91 {
92     return value_->AsFunctionExpression()->Function();
93 }
94 
Name() const95 const util::StringView &ClassStaticBlock::Name() const
96 {
97     return Function()->Id()->Name();
98 }
99 
100 }  // namespace ark::es2panda::ir
101