• 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,bool isKernelCode)18 bool CodeEmitter::OutPut(const AutoPtr<AST> &ast, const std::string &targetDirectory, bool isKernelCode)
19 {
20     if (!Reset(ast, targetDirectory, isKernelCode)) {
21         return false;
22     }
23 
24     EmitCode();
25     return true;
26 }
27 
Reset(const AutoPtr<AST> & ast,const std::string & targetDirectory,bool isKernelCode)28 bool CodeEmitter::Reset(const AutoPtr<AST> &ast, const std::string &targetDirectory, bool isKernelCode)
29 {
30     if (ast == nullptr || targetDirectory.empty()) {
31         return false;
32     }
33 
34     CleanData();
35 
36     isKernelCode_ = isKernelCode;
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     isKernelCode_ = false;
77     ast_ = nullptr;
78     interface_ = nullptr;
79     directory_ = "";
80     interfaceName_ = "";
81     interfaceFullName_ = "";
82     baseName_ = "";
83     proxyName_ = "";
84     proxyFullName_ = "";
85     stubName_ = "";
86     stubFullName_ = "";
87     implName_ = "";
88     implFullName_ = "";
89     dataParcelName_ = "";
90     replyParcelName_ = "";
91     optionName_ = "";
92     errorCodeName_ = "";
93 }
94 
NeedFlag(const AutoPtr<ASTMethod> & method)95 bool CodeEmitter::NeedFlag(const AutoPtr<ASTMethod> &method)
96 {
97     for (size_t i = 0; i < method->GetParameterNumber(); i++) {
98         AutoPtr<ASTParameter> param = method->GetParameter(i);
99         AutoPtr<ASTType> type = param->GetType();
100         if (param->GetAttribute() == ParamAttr::PARAM_OUT &&
101             (type->IsStringType() || type->IsArrayType() || type->IsListType())) {
102             return true;
103         }
104     }
105     return false;
106 }
107 
108 /*
109  * -r option: -r ohos.hdi:./drivers/interface
110  * outDir: ./out
111  * package: ohos.hdi.foo.v1_0
112  * subPackage: foo.v1_0
113  * subPath: foo/v1_0
114  * GenPath: ./out/foo/v1_0/
115  */
GetFileParentPath(const std::string & outDir)116 std::string CodeEmitter::GetFileParentPath(const std::string &outDir)
117 {
118     std::string outPath = StringHelper::EndWith(outDir, SEPARATOR) ? outDir.substr(0, outDir.size() - 1) : outDir;
119     std::string subPackage = Options::GetInstance().GetSubPackage(ast_->GetPackageName());
120     std::string subPath = StringHelper::Replace(subPackage, '.', SEPARATOR);
121     if (subPath.empty()) {
122         return File::AdapterPath(StringHelper::Format("%s/", outPath.c_str(), subPath.c_str()));
123     } else {
124         return File::AdapterPath(StringHelper::Format("%s/%s/", outPath.c_str(), subPath.c_str()));
125     }
126 }
127 
PackageToFilePath(const std::string & packageName)128 std::string CodeEmitter::PackageToFilePath(const std::string &packageName)
129 {
130     std::vector<std::string> packageVec = StringHelper::Split(Options::GetInstance().GetSubPackage(packageName), ".");
131     StringBuilder filePath;
132     for (auto iter = packageVec.begin(); iter != packageVec.end(); iter++) {
133         filePath.Append(FileName(*iter));
134         if (iter != packageVec.end() - 1) {
135             filePath.Append(SEPARATOR);
136         }
137     }
138 
139     return filePath.ToString();
140 }
141 
EmitMethodCmdID(const AutoPtr<ASTMethod> & method)142 std::string CodeEmitter::EmitMethodCmdID(const AutoPtr<ASTMethod> &method)
143 {
144     return StringHelper::Format("CMD_%s_%s", ConstantName(baseName_).c_str(), ConstantName(method->GetName()).c_str());
145 }
146 
EmitInterfaceMethodCommands(StringBuilder & sb,const std::string & prefix)147 void CodeEmitter::EmitInterfaceMethodCommands(StringBuilder &sb, const std::string &prefix)
148 {
149     sb.Append(prefix).AppendFormat("enum {\n");
150     for (size_t i = 0; i < interface_->GetMethodNumber(); i++) {
151         AutoPtr<ASTMethod> method = interface_->GetMethod(i);
152         sb.Append(prefix + TAB).Append(EmitMethodCmdID(method)).Append(",\n");
153     }
154 
155     sb.Append(prefix + TAB).Append(EmitMethodCmdID(interface_->GetVersionMethod())).Append(",\n");
156     sb.Append(prefix).Append("};\n");
157 }
158 
EmitVersionHeaderName(const std::string & name)159 std::string CodeEmitter::EmitVersionHeaderName(const std::string &name)
160 {
161     return StringHelper::Format("v%u_%u/%s", ast_->GetMajorVer(), ast_->GetMinorVer(), FileName(name).c_str());
162 }
163 
EmitLogTagMacro(StringBuilder & sb,const std::string & name)164 void CodeEmitter::EmitLogTagMacro(StringBuilder &sb, const std::string &name)
165 {
166     sb.AppendFormat("#define HDF_LOG_TAG    %s\n", name.c_str());
167 }
168 
ConstantName(const std::string & name)169 std::string CodeEmitter::ConstantName(const std::string &name)
170 {
171     if (name.empty()) {
172         return name;
173     }
174 
175     StringBuilder sb;
176 
177     for (size_t i = 0; i < name.size(); i++) {
178         char c = name[i];
179         if (isupper(c) != 0) {
180             if (i > 1) {
181                 sb.Append('_');
182             }
183             sb.Append(c);
184         } else {
185             sb.Append(toupper(c));
186         }
187     }
188 
189     return sb.ToString();
190 }
191 
PascalName(const std::string & name)192 std::string CodeEmitter::PascalName(const std::string &name)
193 {
194     if (name.empty()) {
195         return name;
196     }
197 
198     StringBuilder sb;
199     for (size_t i = 0; i < name.size(); i++) {
200         char c = name[i];
201         if (i == 0) {
202             if (islower(c)) {
203                 c = toupper(c);
204             }
205             sb.Append(c);
206         } else {
207             if (c == '_') {
208                 continue;
209             }
210 
211             if (islower(c) && name[i - 1] == '_') {
212                 c = toupper(c);
213             }
214             sb.Append(c);
215         }
216     }
217 
218     return sb.ToString();
219 }
220 
FileName(const std::string & name)221 std::string CodeEmitter::FileName(const std::string &name)
222 {
223     if (name.empty()) {
224         return name;
225     }
226 
227     StringBuilder sb;
228     for (size_t i = 0; i < name.size(); i++) {
229         char c = name[i];
230         if (isupper(c) != 0) {
231             // 2->Index of the last char array.
232             if (i > 1) {
233                 sb.Append('_');
234             }
235             sb.Append(tolower(c));
236         } else {
237             sb.Append(c);
238         }
239     }
240 
241     return sb.ToString();
242 }
243 
GetUtilMethods(UtilMethodMap & methods)244 void CodeEmitter::GetUtilMethods(UtilMethodMap &methods)
245 {
246     // get util methods
247     (void)methods;
248 }
249 
EmitUtilMethods(StringBuilder & sb,const std::string & prefix,const UtilMethodMap & methods,bool isDecl)250 void CodeEmitter::EmitUtilMethods(
251     StringBuilder &sb, const std::string &prefix, const UtilMethodMap &methods, bool isDecl)
252 {
253     // generator util methods
254     for (const auto &methodPair : methods) {
255         if (!isDecl) {
256             sb.Append("\n");
257         }
258         methodPair.second(sb, "", prefix, isDecl);
259     }
260 }
261 
EmitInterfaceBuffSizeMacro(StringBuilder & sb)262 void CodeEmitter::EmitInterfaceBuffSizeMacro(StringBuilder &sb)
263 {
264     sb.AppendFormat("#ifndef %s\n", MAX_BUFF_SIZE_MACRO);
265     sb.AppendFormat("#define %s (%s)\n", MAX_BUFF_SIZE_MACRO, MAX_BUFF_SIZE_VALUE);
266     sb.Append("#endif\n\n");
267 
268     sb.AppendFormat("#ifndef %s\n", CHECK_VALUE_RETURN_MACRO);
269     sb.AppendFormat("#define %s(lv, compare, rv, ret) do { \\\n", CHECK_VALUE_RETURN_MACRO);
270     sb.Append(TAB).Append("if ((lv) compare (rv)) { \\\n");
271     sb.Append(TAB).Append(TAB).Append("return ret; \\\n");
272     sb.Append(TAB).Append("} \\\n");
273     sb.Append("} while (false)\n");
274     sb.Append("#endif\n\n");
275 
276     sb.AppendFormat("#ifndef %s\n", CHECK_VALUE_RET_GOTO_MACRO);
277     sb.AppendFormat("#define %s(lv, compare, rv, ret, value, table) do { \\\n", CHECK_VALUE_RET_GOTO_MACRO);
278     sb.Append(TAB).Append("if ((lv) compare (rv)) { \\\n");
279     sb.Append(TAB).Append(TAB).Append("ret = value; \\\n");
280     sb.Append(TAB).Append(TAB).Append("goto table; \\\n");
281     sb.Append(TAB).Append("} \\\n");
282     sb.Append("} while (false)\n");
283     sb.Append("#endif\n");
284 }
285 } // namespace HDI
286 } // namespace OHOS
287