1/*
2 * Copyright (c) 2024 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#ifndef ES2PANDA_EVALUATE_DEBUG_INFO_DESERIALIZER_H
17#define ES2PANDA_EVALUATE_DEBUG_INFO_DESERIALIZER_H
18
19#include "libpandabase/utils/arena_containers.h"
20#include "libpandafile/debug_info_extractor.h"
21#include "libpandafile/file.h"
22
23namespace ark::es2panda::varbinder {
24class Variable;
25}  // namespace ark::es2panda::varbinder
26
27namespace ark::es2panda::parser {
28class Program;
29}  // namespace ark::es2panda::parser
30
31namespace ark::es2panda::ir {
32class ETSTypeReference;
33}  // namespace ark::es2panda::ir
34
35namespace ark::panda_file {
36class ClassDataAccessor;
37}  // namespace ark::panda_file
38
39namespace ark::es2panda::evaluate {
40
41struct ChainEntryInfo;
42struct FileDebugInfo;
43class ScopedDebugInfoPlugin;
44
45class DebugInfoDeserializer final {
46public:
47    using RegisterNumber = int32_t;
48
49public:
50    NO_COPY_SEMANTIC(DebugInfoDeserializer);
51    NO_MOVE_SEMANTIC(DebugInfoDeserializer);
52
53    explicit DebugInfoDeserializer(ScopedDebugInfoPlugin &debugInfoPlugin);
54    ~DebugInfoDeserializer() = default;
55
56    varbinder::Variable *CreateIrClass(panda_file::File::EntityId classId, parser::Program *classProgram,
57                                       util::StringView pathToSource, util::StringView classDeclName);
58
59    varbinder::Variable *CreateIrLocalVariable(ir::Identifier *ident,
60                                               const panda_file::LocalVariableTable &localVariableTable,
61                                               size_t bytecodeOffset);
62
63    varbinder::Variable *CreateIrGlobalVariable(parser::Program *program, util::StringView pathToSource,
64                                                util::StringView varDeclName);
65
66    varbinder::Variable *CreateIrGlobalMethods(ArenaVector<ir::AstNode *> &createdMethods, parser::Program *program,
67                                               util::StringView pathToSource, util::StringView methodDeclName);
68
69private:
70    // Methods for resolving inheritance.
71    ir::ETSTypeReference *GetSuperClass(panda_file::ClassDataAccessor &cda);
72
73    /**
74     * @brief Creates and checks all entries in inheritance chain.
75     * @returns TypeReference to the last super class in resolved chain.
76     */
77    ir::ETSTypeReference *ResolveInheritanceChain(util::StringView abcSuperName, FileDebugInfo *debugInfo);
78
79    ir::ETSTypeReference *ResolveInheritanceChainImpl(util::StringView abcSuperName, FileDebugInfo *debugInfo);
80
81    /**
82     * @brief Fills information required for inheritance chain recreation.
83     * @returns declaration name of the first already created class in chain,
84     * empty string if algorithm has reached std.core.Object.
85     */
86    util::StringView CollectChainInfo(ArenaVector<ChainEntryInfo> &chainEntryList, util::StringView abcSuperName,
87                                      FileDebugInfo *debugInfo);
88
89    varbinder::Variable *CreateLocalVarDecl(ir::Identifier *ident, RegisterNumber regNumber,
90                                            const std::string &typeSignature);
91
92    ir::ClassDeclaration *CreateClassDeclaration(util::StringView identName, panda_file::ClassDataAccessor &cda,
93                                                 ir::ETSTypeReference *superClass, parser::Program *program);
94
95private:
96    ScopedDebugInfoPlugin &debugInfoPlugin_;
97};
98
99}  // namespace ark::es2panda::evaluate
100
101#endif  // ES2PANDA_EVALUATE_DEBUG_INFO_DESERIALIZER_H
102