1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * 4 * HDF is dual licensed: you can use it either under the terms of 5 * the GPL, or the BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 9 #ifndef OHOS_HDI_CODE_EMITTER_H 10 #define OHOS_HDI_CODE_EMITTER_H 11 12 #include <set> 13 14 #include "ast/ast.h" 15 #include "util/autoptr.h" 16 #include "util/light_refcount_base.h" 17 18 namespace OHOS { 19 namespace HDI { 20 enum class HeaderFileType { 21 OWN_HEADER_FILE, 22 SYSTEM_HEADER_FILE, 23 C_STD_HEADER_FILE, 24 CPP_STD_HEADER_FILE, 25 OTHER_MODULES_HEADER_FILE, 26 OWN_MODULE_HEADER_FILE, 27 }; 28 29 struct HeaderFile { HeaderFileHeaderFile30 HeaderFile(HeaderFileType type, std::string fileName) : type_(type), fileName_(fileName) {} 31 32 struct Compare { operatorHeaderFile::Compare33 bool operator()(const HeaderFile &lhs, const HeaderFile &rhs) const 34 { 35 int compareRet = lhs.fileName_.compare(rhs.fileName_); 36 if (compareRet == 0) { 37 return false; 38 } 39 40 if (lhs.type_ != rhs.type_) { 41 return lhs.type_ < rhs.type_; 42 } 43 44 return compareRet < 0; 45 } 46 }; 47 ToStringHeaderFile48 std::string ToString() const 49 { 50 switch (type_) { 51 case HeaderFileType::OWN_HEADER_FILE: 52 case HeaderFileType::OWN_MODULE_HEADER_FILE: 53 return StringHelper::Format("#include \"%s.h\"", fileName_.c_str()); 54 case HeaderFileType::SYSTEM_HEADER_FILE: 55 case HeaderFileType::C_STD_HEADER_FILE: 56 case HeaderFileType::OTHER_MODULES_HEADER_FILE: 57 return StringHelper::Format("#include <%s.h>", fileName_.c_str()); 58 case HeaderFileType::CPP_STD_HEADER_FILE: 59 return StringHelper::Format("#include <%s>", fileName_.c_str()); 60 default: 61 return StringHelper::Format("//"); 62 } 63 } 64 65 using HeaderFileSet = std::set<HeaderFile, HeaderFile::Compare>; 66 67 HeaderFileType type_; 68 std::string fileName_; 69 }; 70 71 class CodeEmitter : public LightRefCountBase { 72 public: 73 ~CodeEmitter() override = default; 74 75 bool OutPut(const AutoPtr<AST> &ast, const std::string &targetDirectory, GenMode mode); 76 77 protected: 78 bool Reset(const AutoPtr<AST> &ast, const std::string &targetDirectory, GenMode mode); 79 80 void CleanData(); 81 82 virtual bool ResolveDirectory(const std::string &targetDirectory) = 0; 83 84 virtual void EmitCode() = 0; 85 86 bool NeedFlag(const AutoPtr<ASTMethod> &method) const; 87 88 std::string GetFileParentPath(const std::string &outDir) const; 89 90 std::string PackageToFilePath(const std::string &packageName) const; 91 92 std::string InterfaceToFilePath(const std::string &interfaceName) const; 93 94 std::string EmitMethodCmdID(const AutoPtr<ASTMethod> &method); 95 96 virtual void EmitInterfaceMethodCommands(StringBuilder &sb, const std::string &prefix); 97 98 /* add version prefix 99 * MajorVersion: 1 100 * MinorVersion: 0 101 * name: IFoo 102 * result: v1_0/ifoo.h 103 */ 104 std::string EmitVersionHeaderName(const std::string &name) const; 105 106 // log tag macro of hdf 107 void EmitLogTagMacro(StringBuilder &sb, const std::string &name) const; 108 109 // file_name -> FILE_NAME 110 std::string ConstantName(const std::string &name) const; 111 112 // file_name -> FileName 113 std::string PascalName(const std::string &name) const; 114 115 // FileName -> file_name 116 std::string FileName(const std::string &name) const; 117 118 virtual void GetUtilMethods(UtilMethodMap &methods); 119 120 virtual void EmitUtilMethods( 121 StringBuilder &sb, const std::string &prefix, const UtilMethodMap &methods, bool isDecl); 122 123 void EmitInterfaceBuffSizeMacro(StringBuilder &sb) const; 124 125 protected: 126 GenMode mode_ = GenMode::IPC; 127 AutoPtr<AST> ast_ = nullptr; 128 AutoPtr<ASTInterfaceType> interface_ = nullptr; 129 std::string directory_; 130 131 std::string interfaceName_; 132 std::string interfaceFullName_; 133 std::string baseName_; 134 std::string proxyName_; 135 std::string proxyFullName_; 136 std::string stubName_; 137 std::string stubFullName_; 138 std::string implName_; 139 std::string implFullName_; 140 std::string majorVerName_; 141 std::string minorVerName_; 142 143 std::string dataParcelName_; 144 std::string replyParcelName_; 145 std::string optionName_; 146 std::string errorCodeName_; 147 std::string flagOfSetMemName_; 148 }; 149 } // namespace HDI 150 } // namespace OHOS 151 152 #endif // OHOS_HDI_CODE_EMITTER_H 153