• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 EmitMethodCmdID(const AutoPtr<ASTMethod> &method);
93 
94     virtual void EmitInterfaceMethodCommands(StringBuilder &sb, const std::string &prefix);
95 
96     /* add version prefix
97      * MajorVersion: 1
98      * MinorVersion: 0
99      * name: IFoo
100      * result: v1_0/ifoo.h
101      */
102     std::string EmitVersionHeaderName(const std::string &name) const;
103 
104     // log tag macro of hdf
105     void EmitLogTagMacro(StringBuilder &sb, const std::string &name) const;
106 
107     // file_name -> FILE_NAME
108     std::string ConstantName(const std::string &name) const;
109 
110     // file_name -> FileName
111     std::string PascalName(const std::string &name) const;
112 
113     // FileName -> file_name
114     std::string FileName(const std::string &name) const;
115 
116     virtual void GetUtilMethods(UtilMethodMap &methods);
117 
118     virtual void EmitUtilMethods(
119         StringBuilder &sb, const std::string &prefix, const UtilMethodMap &methods, bool isDecl);
120 
121     void EmitInterfaceBuffSizeMacro(StringBuilder &sb) const;
122 
123 protected:
124     GenMode mode_ = GenMode::IPC;
125     AutoPtr<AST> ast_ = nullptr;
126     AutoPtr<ASTInterfaceType> interface_ = nullptr;
127     std::string directory_;
128 
129     std::string interfaceName_;
130     std::string interfaceFullName_;
131     std::string baseName_;
132     std::string proxyName_;
133     std::string proxyFullName_;
134     std::string stubName_;
135     std::string stubFullName_;
136     std::string implName_;
137     std::string implFullName_;
138     std::string majorVerName_;
139     std::string minorVerName_;
140 
141     std::string dataParcelName_;
142     std::string replyParcelName_;
143     std::string optionName_;
144     std::string errorCodeName_;
145     std::string flagOfSetMemName_;
146 };
147 } // namespace HDI
148 } // namespace OHOS
149 
150 #endif // OHOS_HDI_CODE_EMITTER_H
151