• 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 #ifndef ES2PANDA_COMPILER_CORE_ETS_EMITTER_H
17 #define ES2PANDA_COMPILER_CORE_ETS_EMITTER_H
18 
19 #include "annotation.h"
20 #include "assembly-literals.h"
21 #include "emitter.h"
22 
23 namespace ark::es2panda::parser {
24 class Program;
25 }  // namespace ark::es2panda::parser
26 
27 namespace ark::es2panda::varbinder {
28 class RecordTable;
29 }  // namespace ark::es2panda::varbinder
30 
31 namespace ark::es2panda::ir {
32 class ClassDefinition;
33 }  // namespace ark::es2panda::ir
34 
35 namespace ark::es2panda::checker {
36 class ETSObjectType;
37 class ETSArrayType;
38 class Signature;
39 }  // namespace ark::es2panda::checker
40 
41 namespace ark::pandasm {
42 struct Field;
43 struct Record;
44 class ItemMetadata;
45 class AnnotationData;
46 }  // namespace ark::pandasm
47 
48 namespace ark::es2panda::compiler {
49 
50 using LiteralArrayPair = std::pair<std::string, std::vector<pandasm::LiteralArray::Literal>>;
51 using LiteralArrayVector = std::vector<LiteralArrayPair>;
52 
53 class ETSFunctionEmitter : public FunctionEmitter {
54 public:
ETSFunctionEmitter(const CodeGen * cg,ProgramElement * programElement)55     ETSFunctionEmitter(const CodeGen *cg, ProgramElement *programElement) : FunctionEmitter(cg, programElement) {}
56     ~ETSFunctionEmitter() override = default;
57     NO_COPY_SEMANTIC(ETSFunctionEmitter);
58     NO_MOVE_SEMANTIC(ETSFunctionEmitter);
59 
60 protected:
Etsg()61     const ETSGen *Etsg() const
62     {
63         return reinterpret_cast<const ETSGen *>(Cg());
64     }
65 
66     pandasm::Function *GenFunctionSignature() override;
67 
68     void GenFunctionAnnotations(pandasm::Function *func) override;
69     void GenVariableSignature(pandasm::debuginfo::LocalVariable &variableDebug,
70                               varbinder::LocalVariable *variable) const override;
71     void GenSourceFileDebugInfo(pandasm::Function *func) override;
72 };
73 
74 class ETSEmitter : public Emitter {
75 public:
ETSEmitter(const public_lib::Context * context)76     explicit ETSEmitter(const public_lib::Context *context) : Emitter(context) {}
77     ~ETSEmitter() override = default;
78     NO_COPY_SEMANTIC(ETSEmitter);
79     NO_MOVE_SEMANTIC(ETSEmitter);
80 
81     void GenAnnotation() override;
82     std::vector<pandasm::AnnotationData> GenCustomAnnotations(
83         const ArenaVector<ir::AnnotationUsage *> &annotationUsages, const std::string &baseName);
84 
85 private:
86     using DynamicCallNamesMap = ArenaMap<const ArenaVector<util::StringView>, uint32_t>;
87 
88     void GenExternalRecord(varbinder::RecordTable *recordTable, const parser::Program *extProg);
89     void GenGlobalArrayRecord(const checker::ETSArrayType *arrayType, checker::Signature *signature);
90     std::vector<pandasm::AnnotationData> GenAnnotations(const ir::ClassDefinition *classDef);
91     void GenClassRecord(const ir::ClassDefinition *classDef, bool external);
92     pandasm::AnnotationElement ProcessArrayType(const ir::ClassProperty *prop, std::string &baseName,
93                                                 const ir::Expression *init);
94     pandasm::AnnotationElement ProcessETSEnumType(std::string &baseName, const ir::Expression *init,
95                                                   const checker::Type *type);
96     pandasm::AnnotationElement GenCustomAnnotationElement(const ir::ClassProperty *prop, std::string &baseName);
97     pandasm::AnnotationData GenCustomAnnotation(ir::AnnotationUsage *anno, std::string &baseName);
98     void CreateEnumProp(const ir::ClassProperty *prop, pandasm::Field &field);
99     void ProcessArrayElement(const ir::Expression *elem, std::vector<pandasm::LiteralArray::Literal> &literals,
100                              std::string &baseName, LiteralArrayVector &result);
101     LiteralArrayVector CreateLiteralArray(std::string &baseName, const ir::Expression *array);
102     void CreateLiteralArrayProp(const ir::ClassProperty *prop, std::string &baseName, pandasm::Field &field);
103     void GenCustomAnnotationProp(const ir::ClassProperty *prop, std::string &baseName, pandasm::Record &record,
104                                  bool external);
105     void GenCustomAnnotationRecord(const ir::AnnotationDeclaration *annoDecl, std::string &baseName, bool external);
106     void GenEnumRecord(const ir::TSEnumDeclaration *enumDecl, bool external);
107     void GenAnnotationRecord(std::string_view recordNameView, bool isRuntime = false, bool isType = false);
108     void GenInterfaceRecord(const ir::TSInterfaceDeclaration *interfaceDecl, bool external);
109     void EmitDefaultFieldValue(pandasm::Field &classField, const ir::Expression *init);
110     void GenClassField(const ir::ClassProperty *prop, pandasm::Record &classRecord, bool external);
111 
112     void GenInterfaceMethodDefinition(const ir::MethodDefinition *methodDef, bool external);
113     void GenClassInheritedFields(const checker::ETSObjectType *baseType, pandasm::Record &classRecord);
114     pandasm::AnnotationData GenAnnotationSignature(const ir::ClassDefinition *classDef);
115     pandasm::AnnotationData GenAnnotationModule(const ir::ClassDefinition *classDef);
116     pandasm::AnnotationData GenAnnotationEnclosingClass(std::string_view className);
117     pandasm::AnnotationData GenAnnotationEnclosingMethod(const ir::MethodDefinition *methodDef);
118     pandasm::AnnotationData GenAnnotationFunctionalReference(const ir::ClassDefinition *classDef);
119     pandasm::AnnotationData GenAnnotationInnerClass(const ir::ClassDefinition *classDef, const ir::AstNode *parent);
120     pandasm::AnnotationData GenAnnotationAsync(ir::ScriptFunction *scriptFunc);
121     pandasm::AnnotationData GenAnnotationDynamicCall(DynamicCallNamesMap &callNames);
122     ir::MethodDefinition *FindAsyncImpl(ir::ScriptFunction *asyncFunc);
123     void ProcessArrayExpression(std::string &baseName, LiteralArrayVector &result,
124                                 std::vector<pandasm::LiteralArray::Literal> &literals, const ir::Expression *elem);
125 };
126 }  // namespace ark::es2panda::compiler
127 
128 #endif
129