1 /* 2 * Copyright (c) 2021 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 #include "ast/ast.h" 14 #include "util/autoptr.h" 15 #include "util/light_refcount_base.h" 16 #include "util/string.h" 17 #include "util/common.h" 18 19 namespace OHOS { 20 namespace HDI { 21 enum class HeaderFileType { 22 OWN_HEADER_FILE, 23 SYSTEM_HEADER_FILE, 24 C_STD_HEADER_FILE, 25 CPP_STD_HEADER_FILE, 26 OTHER_MODULES_HEADER_FILE, 27 OWN_MODULE_HEADER_FILE, 28 }; 29 30 struct HeaderFile { HeaderFileHeaderFile31 HeaderFile(HeaderFileType type, String fileName) : type_(type), fileName_(fileName) {} 32 33 struct compare { operatorHeaderFile::compare34 bool operator()(const HeaderFile& lhs, const HeaderFile& rhs) 35 { 36 if (lhs.type_ < rhs.type_) { 37 return true; 38 } else if (lhs.type_ > rhs.type_) { 39 return false; 40 } 41 42 return lhs.fileName_.Compare(rhs.fileName_) <= 0; 43 } 44 }; 45 ToStringHeaderFile46 String ToString() const 47 { 48 switch (type_) { 49 case HeaderFileType::OWN_HEADER_FILE: 50 case HeaderFileType::OWN_MODULE_HEADER_FILE: 51 return String::Format("#include \"%s.h\"", fileName_.string()); 52 case HeaderFileType::SYSTEM_HEADER_FILE: 53 case HeaderFileType::C_STD_HEADER_FILE: 54 case HeaderFileType::OTHER_MODULES_HEADER_FILE: 55 return String::Format("#include <%s.h>", fileName_.string()); 56 case HeaderFileType::CPP_STD_HEADER_FILE: 57 return String::Format("#include <%s>", fileName_.string()); 58 default: 59 return String::Format("//"); 60 } 61 } 62 63 using HeaderFileSet = std::set<HeaderFile, HeaderFile::compare>; 64 65 HeaderFileType type_; 66 String fileName_; 67 }; 68 69 class CodeEmitter : public LightRefCountBase { 70 public: 71 virtual ~CodeEmitter() = default; 72 73 bool OutPut(const AutoPtr<AST>& ast, const String& targetDirectory, bool isKernelCode = false); 74 75 protected: 76 bool Reset(const AutoPtr<AST>& ast, const String& targetDirectory, bool isKernelCode); 77 78 void CleanData(); 79 80 virtual bool ResolveDirectory(const String& targetDirectory) = 0; 81 82 virtual void EmitCode() = 0; 83 84 String GetFilePath(const String& outDir); 85 86 String PackageToFilePath(const String& packageName); 87 88 String EmitMethodCmdID(const AutoPtr<ASTMethod>& method); 89 90 void EmitInterfaceMethodCommands(StringBuilder& sb, const String& prefix); 91 92 /* ForExample: 93 * MajorVersion: 1 94 * MinorVersion: 0 95 * name: IFoo 96 * result: v1_0/ifoo.h 97 */ 98 String EmitVersionHeaderName(const String& name); 99 100 // file_name -> FILE_NAME 101 String ConstantName(const String& name); 102 103 // file_name -> FileName 104 String PascalName(const String& name); 105 106 // FileName -> file_name 107 String FileName(const String& name); 108 109 void EmitInterfaceBuffSizeMacro(StringBuilder &sb); 110 111 bool isKernelCode_ = false; 112 AutoPtr<AST> ast_ = nullptr; 113 AutoPtr<ASTInterfaceType> interface_ = nullptr; 114 String directory_; 115 116 String interfaceName_; 117 String interfaceFullName_; 118 String baseName_; 119 String proxyName_; 120 String proxyFullName_; 121 String stubName_; 122 String stubFullName_; 123 String implName_; 124 String implFullName_; 125 String majorVerName_; 126 String minorVerName_; 127 String bufferSizeMacroName_; 128 129 String dataParcelName_; 130 String replyParcelName_; 131 String optionName_; 132 String errorCodeName_; 133 }; 134 } 135 } 136 137 #endif // OHOS_HDI_CODE_EMITTER_H 138