1 /* 2 * Copyright (c) 2021 - 2023 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 panda::es2panda::varbinder { 22 class RecordTable; 23 } // namespace panda::es2panda::varbinder 24 25 namespace panda::es2panda::ir { 26 class ClassDefinition; 27 } // namespace panda::es2panda::ir 28 29 namespace panda::es2panda::checker { 30 class ETSObjectType; 31 class ETSArrayType; 32 class Signature; 33 } // namespace panda::es2panda::checker 34 35 namespace panda::pandasm { 36 struct Field; 37 struct Record; 38 class ItemMetadata; 39 class AnnotationData; 40 } // namespace panda::pandasm 41 42 namespace panda::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() = 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 CompilerContext * context)66 explicit ETSEmitter(const CompilerContext *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 void GenExternalRecord(varbinder::RecordTable *recordTable); 75 void GenGlobalArrayRecord(checker::ETSArrayType *arrayType, checker::Signature *signature); 76 void GenClassRecord(const ir::ClassDefinition *classDef, bool external); 77 void GenEnumRecord(const ir::TSEnumDeclaration *enumDecl, bool external); 78 void GenAnnotationRecord(std::string_view recordNameView, bool isRuntime = false, bool isType = false); 79 void GenInterfaceRecord(const ir::TSInterfaceDeclaration *interfaceDecl, bool external); 80 void EmitDefaultFieldValue(pandasm::Field &classField, const ir::Expression *init); 81 void GenClassField(const ir::ClassProperty *field, pandasm::Record &classRecord, bool external); 82 void GenField(const checker::Type *tsType, const util::StringView &name, const ir::Expression *value, 83 uint32_t accesFlags, pandasm::Record &record, bool external); 84 void GenInterfaceMethodDefinition(const ir::MethodDefinition *methodDef, bool external); 85 void GenClassInheritedFields(const checker::ETSObjectType *baseType, pandasm::Record &classRecord); 86 pandasm::AnnotationData GenAnnotationSignature(const ir::ClassDefinition *classDef); 87 pandasm::AnnotationData GenAnnotationEnclosingClass(std::string_view className); 88 pandasm::AnnotationData GenAnnotationEnclosingMethod(const ir::MethodDefinition *methodDef); 89 pandasm::AnnotationData GenAnnotationInnerClass(const ir::ClassDefinition *classDef, const ir::AstNode *parent); 90 pandasm::AnnotationData GenAnnotationAsync(ir::ScriptFunction *scriptFunc); 91 ir::MethodDefinition *FindAsyncImpl(ir::ScriptFunction *asyncFunc); 92 }; 93 } // namespace panda::es2panda::compiler 94 95 #endif 96