1 /**
2 * Copyright (c) 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 "evaluate/debugInfoDeserialization/classBuilder.h"
17 #include "parser/program/program.h"
18 #include "checker/ETSchecker.h"
19 #include "ir/statements/classDeclaration.h"
20 #include "ir/expressions/identifier.h"
21 #include "libpandafile/class_data_accessor-inl.h"
22 #include "libpandafile/file-inl.h"
23 #include "libpandafile/method_data_accessor.h"
24 #include "evaluate/helpers.h"
25 #include "evaluate/debugInfoDeserialization/methodBuilder.h"
26
27 namespace ark::es2panda::evaluate {
28
ClassBuilder(checker::ETSChecker * checker,util::StringView name,panda_file::ClassDataAccessor & cda,ir::Expression * superClass)29 ClassBuilder::ClassBuilder(checker::ETSChecker *checker, util::StringView name, panda_file::ClassDataAccessor &cda,
30 ir::Expression *superClass)
31 : checker_(checker), className_(name), cda_(cda), superClass_(superClass)
32 {
33 classModifiers_ = ir::ClassDefinitionModifiers::ID_REQUIRED | ir::ClassDefinitionModifiers::CLASS_DECL |
34 ir::ClassDefinitionModifiers::DECLARATION;
35 if (superClass_ != nullptr) {
36 classModifiers_ |= ir::ClassDefinitionModifiers::HAS_SUPER;
37 }
38
39 modifierFlags_ = helpers::GetModifierFlags(cda);
40 }
41
Build(parser::Program * program)42 ir::ClassDeclaration *ClassBuilder::Build(parser::Program *program) &&
43 {
44 auto *allocator = checker_->Allocator();
45 ArenaVector<ir::AstNode *> classBody(allocator->Adapter());
46
47 BuildFields(classBody);
48 BuildMethods(classBody);
49
50 // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint)
51 auto *classId = checker_->AllocNode<ir::Identifier>(className_, allocator);
52
53 // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint)
54 auto *classDef = checker_->AllocNode<ir::ClassDefinition>(
55 classId, nullptr, nullptr, ArenaVector<ir::TSClassImplements *>(allocator->Adapter()), nullptr, superClass_,
56 ArenaVector<ir::AstNode *>(allocator->Adapter()), classModifiers_, modifierFlags_,
57 ToLanguage(program->Extension()));
58
59 // Set parents for all class'es fields and methods.
60 classDef->AddProperties(std::move(classBody));
61
62 // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint)
63 auto *classDecl = checker_->AllocNode<ir::ClassDeclaration>(classDef, allocator);
64 // Declare everything as exported for correct import resolution.
65 classDecl->AddModifier(ir::ModifierFlags::EXPORT);
66
67 auto *programAst = program->Ast();
68 classDecl->SetParent(programAst);
69 // Here we assume that global statements of the passed `program` are not currently checked, so that
70 // insertion is safe.
71 programAst->Statements().push_back(classDecl);
72
73 return classDecl;
74 }
75
BuildFields(ArenaVector<ir::AstNode * > & classBody)76 void ClassBuilder::BuildFields(ArenaVector<ir::AstNode *> &classBody)
77 {
78 const auto &pf = cda_.GetPandaFile();
79
80 cda_.EnumerateFields([&](panda_file::FieldDataAccessor &fda) -> void {
81 const char *name = utf::Mutf8AsCString(pf.GetStringData(fda.GetNameId()).data);
82
83 auto *typeNode = helpers::PandaTypeToTypeNode(pf, fda, checker_);
84 ES2PANDA_ASSERT(typeNode);
85
86 auto *field = helpers::CreateClassProperty(checker_, name, typeNode, helpers::GetModifierFlags(fda, true));
87 classBody.push_back(field);
88 });
89 }
90
BuildMethods(ArenaVector<ir::AstNode * > & classBody)91 void ClassBuilder::BuildMethods(ArenaVector<ir::AstNode *> &classBody)
92 {
93 cda_.EnumerateMethods([&](panda_file::MethodDataAccessor &mda) -> void {
94 auto *method = MethodBuilder(checker_, mda, modifierFlags_).Build();
95 classBody.push_back(method);
96 });
97 }
98
99 } // namespace ark::es2panda::evaluate
100