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