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 "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 }; 72 73 class ETSEmitter : public Emitter { 74 public: ETSEmitter(const public_lib::Context * context)75 explicit ETSEmitter(const public_lib::Context *context) : Emitter(context) {} 76 ~ETSEmitter() override = default; 77 NO_COPY_SEMANTIC(ETSEmitter); 78 NO_MOVE_SEMANTIC(ETSEmitter); 79 80 void GenAnnotation() override; 81 std::vector<pandasm::AnnotationData> GenCustomAnnotations( 82 const ArenaVector<ir::AnnotationUsage *> &annotationUsages, std::string &baseName); 83 84 private: 85 using DynamicCallNamesMap = ArenaMap<const ArenaVector<util::StringView>, uint32_t>; 86 87 void GenExternalRecord(varbinder::RecordTable *recordTable); 88 void GenGlobalArrayRecord(checker::ETSArrayType *arrayType, checker::Signature *signature); 89 std::vector<pandasm::AnnotationData> GenAnnotations(const ir::ClassDefinition *classDef); 90 void GenClassRecord(const ir::ClassDefinition *classDef, bool external); 91 pandasm::AnnotationElement ProcessArrayType(const ir::ClassProperty *prop, std::string &baseName, 92 const ir::Expression *init); 93 pandasm::AnnotationElement GenCustomAnnotationElement(const ir::ClassProperty *prop, std::string &baseName); 94 pandasm::AnnotationData GenCustomAnnotation(ir::AnnotationUsage *anno, std::string &baseName); 95 void CreateEnumProp(const ir::ClassProperty *prop, pandasm::Field &field); 96 void ProcessArrayElement(const ir::Expression *elem, std::vector<pandasm::LiteralArray::Literal> &literals, 97 std::string &baseName, LiteralArrayVector &result); 98 LiteralArrayVector CreateLiteralArray(std::string &baseName, const ir::Expression *array); 99 void CreateLiteralArrayProp(const ir::ClassProperty *prop, std::string &baseName, pandasm::Field &field); 100 void GenCustomAnnotationProp(const ir::ClassProperty *prop, std::string &baseName, pandasm::Record &record, 101 bool external); 102 void GenCustomAnnotationRecord(const ir::AnnotationDeclaration *annoDecl, std::string &baseName, bool external); 103 void GenEnumRecord(const ir::TSEnumDeclaration *enumDecl, bool external); 104 void GenAnnotationRecord(std::string_view recordNameView, bool isRuntime = false, bool isType = false); 105 void GenInterfaceRecord(const ir::TSInterfaceDeclaration *interfaceDecl, bool external); 106 void EmitDefaultFieldValue(pandasm::Field &classField, const ir::Expression *init); 107 void GenClassField(const ir::ClassProperty *prop, pandasm::Record &classRecord, bool external); 108 109 void GenInterfaceMethodDefinition(const ir::MethodDefinition *methodDef, bool external); 110 void GenClassInheritedFields(const checker::ETSObjectType *baseType, pandasm::Record &classRecord); 111 pandasm::AnnotationData GenAnnotationSignature(const ir::ClassDefinition *classDef); 112 pandasm::AnnotationData GenAnnotationEnclosingClass(std::string_view className); 113 pandasm::AnnotationData GenAnnotationEnclosingMethod(const ir::MethodDefinition *methodDef); 114 pandasm::AnnotationData GenAnnotationInnerClass(const ir::ClassDefinition *classDef, const ir::AstNode *parent); 115 pandasm::AnnotationData GenAnnotationAsync(ir::ScriptFunction *scriptFunc); 116 pandasm::AnnotationData GenAnnotationDynamicCall(DynamicCallNamesMap &callNames); 117 ir::MethodDefinition *FindAsyncImpl(ir::ScriptFunction *asyncFunc); 118 void ProcessArrayExpression(std::string &baseName, LiteralArrayVector &result, 119 std::vector<pandasm::LiteralArray::Literal> &literals, const ir::Expression *elem); 120 }; 121 } // namespace ark::es2panda::compiler 122 123 #endif 124