• 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 #include "codegen/code_emitter.h"
10 
11 #include <cctype>
12 
13 #include "util/file.h"
14 #include "util/options.h"
15 
16 namespace OHOS {
17 namespace HDI {
OutPut(const AutoPtr<AST> & ast,const std::string & targetDirectory,GenMode mode)18 bool CodeEmitter::OutPut(const AutoPtr<AST> &ast, const std::string &targetDirectory, GenMode mode)
19 {
20     if (!Reset(ast, targetDirectory, mode)) {
21         return false;
22     }
23 
24     EmitCode();
25     return true;
26 }
27 
Reset(const AutoPtr<AST> & ast,const std::string & targetDirectory,GenMode mode)28 bool CodeEmitter::Reset(const AutoPtr<AST> &ast, const std::string &targetDirectory, GenMode mode)
29 {
30     if (ast == nullptr || targetDirectory.empty()) {
31         return false;
32     }
33 
34     CleanData();
35 
36     mode_ = mode;
37     ast_ = ast;
38     if (ast_->GetASTFileType() == ASTFileType::AST_IFACE || ast_->GetASTFileType() == ASTFileType::AST_ICALLBACK) {
39         interface_ = ast_->GetInterfaceDef();
40         interfaceName_ = interface_->GetName();
41         interfaceFullName_ = interface_->GetNamespace()->ToString() + interfaceName_;
42         baseName_ = StringHelper::StartWith(interfaceName_, "I") ? interfaceName_.substr(1) : interfaceName_;
43         proxyName_ = baseName_ + "Proxy";
44         proxyFullName_ = interface_->GetNamespace()->ToString() + proxyName_;
45 
46         stubName_ = baseName_ + "Stub";
47         stubFullName_ = interface_->GetNamespace()->ToString() + stubName_;
48 
49         implName_ = baseName_ + "Service";
50         implFullName_ = interface_->GetNamespace()->ToString() + implName_;
51     } else if (ast_->GetASTFileType() == ASTFileType::AST_TYPES) {
52         baseName_ = ast_->GetName();
53     } else if (ast_->GetASTFileType() == ASTFileType::AST_SEQUENCEABLE) {
54         baseName_ = ast_->GetName();
55     }
56 
57     majorVerName_ = StringHelper::Format("%s_MAJOR_VERSION", ConstantName(interfaceName_).c_str());
58     minorVerName_ = StringHelper::Format("%s_MINOR_VERSION", ConstantName(interfaceName_).c_str());
59 
60     std::string prefix = StringHelper::Format("%c%s", tolower(baseName_[0]), baseName_.substr(1).c_str());
61     dataParcelName_ = prefix + "Data";
62     replyParcelName_ = prefix + "Reply";
63     optionName_ = prefix + "Option";
64     errorCodeName_ = prefix + "Ret";
65     flagOfSetMemName_ = prefix + "MemSet";
66 
67     if (!ResolveDirectory(targetDirectory)) {
68         return false;
69     }
70 
71     return true;
72 }
73 
CleanData()74 void CodeEmitter::CleanData()
75 {
76     ast_ = nullptr;
77     interface_ = nullptr;
78     directory_ = "";
79     interfaceName_ = "";
80     interfaceFullName_ = "";
81     baseName_ = "";
82     proxyName_ = "";
83     proxyFullName_ = "";
84     stubName_ = "";
85     stubFullName_ = "";
86     implName_ = "";
87     implFullName_ = "";
88     dataParcelName_ = "";
89     replyParcelName_ = "";
90     optionName_ = "";
91     errorCodeName_ = "";
92 }
93 
NeedFlag(const AutoPtr<ASTMethod> & method) const94 bool CodeEmitter::NeedFlag(const AutoPtr<ASTMethod> &method) const
95 {
96     for (size_t i = 0; i < method->GetParameterNumber(); i++) {
97         AutoPtr<ASTParameter> param = method->GetParameter(i);
98         AutoPtr<ASTType> type = param->GetType();
99         if (param->GetAttribute() == ParamAttr::PARAM_OUT &&
100             (type->IsStringType() || type->IsArrayType() || type->IsListType())) {
101             return true;
102         }
103     }
104     return false;
105 }
106 
107 /*
108  * -r option: -r ohos.hdi:./drivers/interface
109  * outDir: ./out
110  * package: ohos.hdi.foo.v1_0
111  * subPackage: foo.v1_0
112  * subPath: foo/v1_0
113  * GenPath: ./out/foo/v1_0/
114  */
GetFileParentPath(const std::string & outDir) const115 std::string CodeEmitter::GetFileParentPath(const std::string &outDir) const
116 {
117     std::string outPath = StringHelper::EndWith(outDir, SEPARATOR) ? outDir.substr(0, outDir.size() - 1) : outDir;
118     std::string subPackage = Options::GetInstance().GetSubPackage(ast_->GetPackageName());
119     std::string subPath = StringHelper::Replace(subPackage, '.', SEPARATOR);
120     if (subPath.empty()) {
121         return File::AdapterPath(StringHelper::Format("%s/", outPath.c_str(), subPath.c_str()));
122     } else {
123         return File::AdapterPath(StringHelper::Format("%s/%s/", outPath.c_str(), subPath.c_str()));
124     }
125 }
126 
PackageToFilePath(const std::string & packageName) const127 std::string CodeEmitter::PackageToFilePath(const std::string &packageName) const
128 {
129     std::vector<std::string> packageVec = StringHelper::Split(Options::GetInstance().GetSubPackage(packageName), ".");
130     StringBuilder filePath;
131     for (auto iter = packageVec.begin(); iter != packageVec.end(); iter++) {
132         filePath.Append(FileName(*iter));
133         if (iter != packageVec.end() - 1) {
134             filePath.Append(SEPARATOR);
135         }
136     }
137 
138     return filePath.ToString();
139 }
140 
EmitMethodCmdID(const AutoPtr<ASTMethod> & method)141 std::string CodeEmitter::EmitMethodCmdID(const AutoPtr<ASTMethod> &method)
142 {
143     return StringHelper::Format("CMD_%s_%s", ConstantName(baseName_).c_str(), ConstantName(method->GetName()).c_str());
144 }
145 
EmitInterfaceMethodCommands(StringBuilder & sb,const std::string & prefix)146 void CodeEmitter::EmitInterfaceMethodCommands(StringBuilder &sb, const std::string &prefix)
147 {
148     sb.Append(prefix).AppendFormat("enum {\n");
149     sb.Append(prefix + TAB).Append(EmitMethodCmdID(interface_->GetVersionMethod())).Append(" = 0,\n");
150     int i = 0;
151     for (const auto &method : interface_->GetMethodsBySystem(Options::GetInstance().GetSystemLevel())) {
152         sb.Append(prefix + TAB).Append(EmitMethodCmdID(method)).AppendFormat(" = %d", i + 1).Append(",\n");
153         i++;
154     }
155     sb.Append(prefix).Append("};\n");
156 }
157 
EmitVersionHeaderName(const std::string & name) const158 std::string CodeEmitter::EmitVersionHeaderName(const std::string &name) const
159 {
160     return StringHelper::Format("v%u_%u/%s", ast_->GetMajorVer(), ast_->GetMinorVer(), FileName(name).c_str());
161 }
162 
EmitLogTagMacro(StringBuilder & sb,const std::string & name) const163 void CodeEmitter::EmitLogTagMacro(StringBuilder &sb, const std::string &name) const
164 {
165     sb.AppendFormat("#define HDF_LOG_TAG    %s\n", name.c_str());
166 }
167 
ConstantName(const std::string & name) const168 std::string CodeEmitter::ConstantName(const std::string &name) const
169 {
170     if (name.empty()) {
171         return name;
172     }
173 
174     StringBuilder sb;
175 
176     for (size_t i = 0; i < name.size(); i++) {
177         char c = name[i];
178         if (isupper(c) != 0) {
179             if (i > 1) {
180                 sb.Append('_');
181             }
182             sb.Append(c);
183         } else {
184             sb.Append(toupper(c));
185         }
186     }
187 
188     return sb.ToString();
189 }
190 
PascalName(const std::string & name) const191 std::string CodeEmitter::PascalName(const std::string &name) const
192 {
193     if (name.empty()) {
194         return name;
195     }
196 
197     StringBuilder sb;
198     for (size_t i = 0; i < name.size(); i++) {
199         char c = name[i];
200         if (i == 0) {
201             if (islower(c)) {
202                 c = toupper(c);
203             }
204             sb.Append(c);
205         } else {
206             if (c == '_') {
207                 continue;
208             }
209 
210             if (islower(c) && name[i - 1] == '_') {
211                 c = toupper(c);
212             }
213             sb.Append(c);
214         }
215     }
216 
217     return sb.ToString();
218 }
219 
FileName(const std::string & name) const220 std::string CodeEmitter::FileName(const std::string &name) const
221 {
222     if (name.empty()) {
223         return name;
224     }
225 
226     StringBuilder sb;
227     for (size_t i = 0; i < name.size(); i++) {
228         char c = name[i];
229         if (isupper(c) != 0) {
230             // 2->Index of the last char array.
231             if (i > 1) {
232                 sb.Append('_');
233             }
234             sb.Append(tolower(c));
235         } else {
236             sb.Append(c);
237         }
238     }
239 
240     return sb.ToString();
241 }
242 
GetUtilMethods(UtilMethodMap & methods)243 void CodeEmitter::GetUtilMethods(UtilMethodMap &methods)
244 {
245     // get util methods
246     (void)methods;
247 }
248 
EmitUtilMethods(StringBuilder & sb,const std::string & prefix,const UtilMethodMap & methods,bool isDecl)249 void CodeEmitter::EmitUtilMethods(
250     StringBuilder &sb, const std::string &prefix, const UtilMethodMap &methods, bool isDecl)
251 {
252     // generator util methods
253     for (const auto &methodPair : methods) {
254         if (!isDecl) {
255             sb.Append("\n");
256         }
257         methodPair.second(sb, "", prefix, isDecl);
258     }
259 }
260 
EmitInterfaceBuffSizeMacro(StringBuilder & sb) const261 void CodeEmitter::EmitInterfaceBuffSizeMacro(StringBuilder &sb) const
262 {
263     sb.AppendFormat("#ifndef %s\n", MAX_BUFF_SIZE_MACRO);
264     sb.AppendFormat("#define %s (%s)\n", MAX_BUFF_SIZE_MACRO, MAX_BUFF_SIZE_VALUE);
265     sb.Append("#endif\n\n");
266 
267     sb.AppendFormat("#ifndef %s\n", CHECK_VALUE_RETURN_MACRO);
268     sb.AppendFormat("#define %s(lv, compare, rv, ret) do { \\\n", CHECK_VALUE_RETURN_MACRO);
269     sb.Append(TAB).Append("if ((lv) compare (rv)) { \\\n");
270     sb.Append(TAB).Append(TAB).Append("return ret; \\\n");
271     sb.Append(TAB).Append("} \\\n");
272     sb.Append("} while (false)\n");
273     sb.Append("#endif\n\n");
274 
275     sb.AppendFormat("#ifndef %s\n", CHECK_VALUE_RET_GOTO_MACRO);
276     sb.AppendFormat("#define %s(lv, compare, rv, ret, value, table) do { \\\n", CHECK_VALUE_RET_GOTO_MACRO);
277     sb.Append(TAB).Append("if ((lv) compare (rv)) { \\\n");
278     sb.Append(TAB).Append(TAB).Append("ret = value; \\\n");
279     sb.Append(TAB).Append(TAB).Append("goto table; \\\n");
280     sb.Append(TAB).Append("} \\\n");
281     sb.Append("} while (false)\n");
282     sb.Append("#endif\n");
283 }
284 } // namespace HDI
285 } // namespace OHOS
286