1/*
2 * Copyright (c) 2021-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_COMPILER_CORE_ETS_EMITTER_H
17#define ES2PANDA_COMPILER_CORE_ETS_EMITTER_H
18
19#include "emitter.h"
20
21namespace ark::es2panda::parser {
22class Program;
23}  // namespace ark::es2panda::parser
24
25namespace ark::es2panda::varbinder {
26class RecordTable;
27}  // namespace ark::es2panda::varbinder
28
29namespace ark::es2panda::ir {
30class ClassDefinition;
31}  // namespace ark::es2panda::ir
32
33namespace ark::es2panda::checker {
34class ETSObjectType;
35class ETSArrayType;
36class Signature;
37}  // namespace ark::es2panda::checker
38
39namespace ark::pandasm {
40struct Field;
41struct Record;
42class ItemMetadata;
43class AnnotationData;
44}  // namespace ark::pandasm
45
46namespace ark::es2panda::compiler {
47
48class ETSFunctionEmitter : public FunctionEmitter {
49public:
50    ETSFunctionEmitter(const CodeGen *cg, ProgramElement *programElement) : FunctionEmitter(cg, programElement) {}
51    ~ETSFunctionEmitter() override = default;
52    NO_COPY_SEMANTIC(ETSFunctionEmitter);
53    NO_MOVE_SEMANTIC(ETSFunctionEmitter);
54
55protected:
56    const ETSGen *Etsg() const
57    {
58        return reinterpret_cast<const ETSGen *>(Cg());
59    }
60
61    pandasm::Function *GenFunctionSignature() override;
62
63    void GenFunctionAnnotations(pandasm::Function *func) override;
64    void GenVariableSignature(pandasm::debuginfo::LocalVariable &variableDebug,
65                              varbinder::LocalVariable *variable) const override;
66};
67
68class ETSEmitter : public Emitter {
69public:
70    explicit ETSEmitter(const public_lib::Context *context) : Emitter(context) {}
71    ~ETSEmitter() override = default;
72    NO_COPY_SEMANTIC(ETSEmitter);
73    NO_MOVE_SEMANTIC(ETSEmitter);
74
75    void GenAnnotation() override;
76
77private:
78    using DynamicCallNamesMap = ArenaMap<const ArenaVector<util::StringView>, uint32_t>;
79
80    void GenExternalRecord(varbinder::RecordTable *recordTable);
81    void GenGlobalArrayRecord(checker::ETSArrayType *arrayType, checker::Signature *signature);
82    std::vector<pandasm::AnnotationData> GenAnnotations(const ir::ClassDefinition *classDef);
83    void GenClassRecord(const ir::ClassDefinition *classDef, bool external);
84    void GenEnumRecord(const ir::TSEnumDeclaration *enumDecl, bool external);
85    void GenAnnotationRecord(std::string_view recordNameView, bool isRuntime = false, bool isType = false);
86    void GenInterfaceRecord(const ir::TSInterfaceDeclaration *interfaceDecl, bool external);
87    void EmitDefaultFieldValue(pandasm::Field &classField, const ir::Expression *init);
88    void GenClassField(const ir::ClassProperty *field, pandasm::Record &classRecord, bool external);
89
90    // Struct to reduce number of arguments to pass code checker
91    struct GenFieldArguments {
92        const checker::Type *tsType;
93        const util::StringView &name;
94        const ir::Expression *value;
95        uint32_t accesFlags;
96        pandasm::Record &record;
97        bool external;
98    };
99
100    void GenField(const GenFieldArguments &data);
101
102    void GenInterfaceMethodDefinition(const ir::MethodDefinition *methodDef, bool external);
103    void GenClassInheritedFields(const checker::ETSObjectType *baseType, pandasm::Record &classRecord);
104    pandasm::AnnotationData GenAnnotationSignature(const ir::ClassDefinition *classDef);
105    pandasm::AnnotationData GenAnnotationEnclosingClass(std::string_view className);
106    pandasm::AnnotationData GenAnnotationEnclosingMethod(const ir::MethodDefinition *methodDef);
107    pandasm::AnnotationData GenAnnotationInnerClass(const ir::ClassDefinition *classDef, const ir::AstNode *parent);
108    pandasm::AnnotationData GenAnnotationAsync(ir::ScriptFunction *scriptFunc);
109    pandasm::AnnotationData GenAnnotationDynamicCall(DynamicCallNamesMap &callNames);
110    ir::MethodDefinition *FindAsyncImpl(ir::ScriptFunction *asyncFunc);
111};
112}  // namespace ark::es2panda::compiler
113
114#endif
115