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 HC_GEN_TEXT_GENERATOR_H 10 #define HC_GEN_TEXT_GENERATOR_H 11 12 #include <fstream> 13 #include <map> 14 15 #include "generator.h" 16 17 namespace OHOS { 18 namespace Hardware { 19 class TextGen : public Generator { 20 public: 21 explicit TextGen(std::shared_ptr<Ast> ast); 22 23 ~TextGen() override = default; 24 25 bool Output() override; 26 27 private: 28 bool Initialize(); 29 30 bool HeaderOutput(); 31 32 bool HeaderOutputTraversal(); 33 34 bool ImplOutput(); 35 36 bool InitOutput(const std::string &fileSuffix); 37 38 bool DuplicateNodeNameCheck(); 39 40 static std::string ToUpperCamelString(const std::string &str); 41 42 static std::string ToLowerCamelString(const std::string &str); 43 44 static std::string ToCamelString(const std::string &str); 45 46 static const std::string &ToUpperString(std::string &str); 47 48 std::string GetHeaderProtectMacro(const std::string &headerFileName); 49 50 uint32_t GenNodeDefinition(const std::shared_ptr<AstObject> &node); 51 52 std::string GenConfigStructName(const std::shared_ptr<AstObject> &node); 53 54 bool GenObjectDefinitionGen(const std::shared_ptr<AstObject> &object); 55 56 bool GenTermDefinition(const std::shared_ptr<AstObject> &term); 57 58 static const std::string &TypeToStr(uint32_t type); 59 60 static bool IsInTemplate(const std::shared_ptr<AstObject> &object); 61 62 bool OutputImplGlobalVariables(); 63 64 uint32_t ImplementGenTraversal(const std::shared_ptr<AstObject> &object, int32_t depth); 65 66 uint32_t ImplementCloseBraceGen(const std::shared_ptr<AstObject> &object, int32_t depth); 67 68 static const std::string &Indent(uint32_t times = 1); 69 70 static bool IsInSubClassNode(const std::shared_ptr<AstObject> &object); 71 72 uint32_t ObjectImplementGen(const std::shared_ptr<AstObject> &object, int32_t depth); 73 74 bool TemplateObjectImplGen(const std::shared_ptr<AstObject> &object, int32_t depth); 75 76 std::string GenTemplateVariableName(const std::shared_ptr<AstObject> &object); 77 78 struct Symbol { SymbolSymbol79 Symbol(const std::shared_ptr<AstObject> &obj, uint32_t c) : object(obj), duplicateCount(c) {} 80 ~Symbol() = default; 81 std::shared_ptr<AstObject> object; 82 uint32_t duplicateCount; 83 }; 84 85 std::shared_ptr<Symbol> SymbolFind(const std::string &name); 86 87 void SymbolAdd(const std::string &name, const std::shared_ptr<AstObject> &object); 88 89 std::ofstream ofs_; 90 std::string outputFileName_; 91 std::string outputNameBase_; 92 std::string prefix_; 93 std::string moduleName_; 94 std::string rootVariableName_; 95 std::map<std::string, std::shared_ptr<Symbol>> symMap; 96 97 uint32_t PrintTermImplement(const std::shared_ptr<AstObject> &object, int32_t depth); 98 99 bool PrintBaseTypeValue(const std::shared_ptr<AstObject> &object); 100 101 uint32_t PrintArrayImplement(const std::shared_ptr<AstObject> &object, int32_t depth); 102 103 bool PrintArrayImplInSubClass(const std::shared_ptr<AstObject> &object, int32_t depth); 104 105 bool HcsPrintArrayContent(const std::shared_ptr<AstObject> &object, uint32_t indent); 106 107 std::string HcsBuildObjectPath(std::shared_ptr<AstObject> object); 108 109 bool OutputTemplateImpl(); 110 111 bool OutputTemplateVariablesDeclare(); 112 113 uint32_t ArrayVariablesDeclareGen(const std::shared_ptr<AstObject> &object); 114 115 std::string GenArrayName(const std::shared_ptr<AstObject> &term); 116 117 uint32_t TemplateVariableGen(const std::shared_ptr<AstObject> &nodeObject); 118 }; 119 } // namespace Hardware 120 } // namespace OHOS 121 #endif // HC_GEN_TEXT_GENERATOR_H 122