1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "hdi_code_generator.h"
17 #include "codegen/HDI/c/c_client_proxy_code_emitter.h"
18 #include "codegen/HDI/c/c_custom_types_code_emitter.h"
19 #include "codegen/HDI/c/c_interface_code_emitter.h"
20 #include "codegen/HDI/c/c_service_driver_code_emitter.h"
21 #include "codegen/HDI/c/c_service_impl_code_emitter.h"
22 #include "codegen/HDI/c/c_service_stub_code_emitter.h"
23 #include "codegen/HDI/cpp/cpp_client_proxy_code_emitter.h"
24 #include "codegen/HDI/cpp/cpp_custom_types_code_emitter.h"
25 #include "codegen/HDI/cpp/cpp_interface_code_emitter.h"
26 #include "codegen/HDI/cpp/cpp_service_driver_code_emitter.h"
27 #include "codegen/HDI/cpp/cpp_service_impl_code_emitter.h"
28 #include "codegen/HDI/cpp/cpp_service_stub_code_emitter.h"
29 #include "codegen/HDI/java/java_client_interface_code_emitter.h"
30 #include "codegen/HDI/java/java_client_proxy_code_emitter.h"
31 #include "util/options.h"
32 #include "util/logger.h"
33
34 namespace OHOS {
35 namespace Idl {
36 HDICodeGenerator::GeneratePolicies HDICodeGenerator::policies_ = {
37 {
38 SystemLevel::MINI,
39 {
40 {
41 GenMode::LOW,
42 {
43 {Language::C, HDICodeGenerator::GenLowCCode}
44 }
45 }
46 }
47 },
48 {
49 SystemLevel::LITE,
50 {
51 {
52 GenMode::KERNEL,
53 {
54 {Language::C, HDICodeGenerator::GenKernelCode}
55 }
56 },
57 {
58 GenMode::PASSTHROUGH,
59 {
60 {Language::C, HDICodeGenerator::GenPassthroughCCode},
61 {Language::CPP, HDICodeGenerator::GenPassthroughCppCode}
62 }
63 }
64 }
65 },
66 {
67 SystemLevel::FULL,
68 {
69 {
70 GenMode::KERNEL,
71 {
72 {Language::C, HDICodeGenerator::GenKernelCode}
73 }
74 },
75 {
76 GenMode::PASSTHROUGH,
77 {
78 {Language::C, HDICodeGenerator::GenPassthroughCCode},
79 {Language::CPP, HDICodeGenerator::GenPassthroughCppCode}
80 }
81 },
82 {
83 GenMode::IPC,
84 {
85 {Language::C, HDICodeGenerator::GenIpcCCode},
86 {Language::CPP, HDICodeGenerator::GenIpcCppCode},
87 {Language::JAVA, HDICodeGenerator::GenIpcJavaCode}
88 }
89 }
90 }
91 }
92 };
93
94 CodeEmitMap HDICodeGenerator::cCodeEmitters_ = {
95 {"types", new CCustomTypesCodeEmitter() },
96 {"interface", new CInterfaceCodeEmitter() },
97 {"proxy", new CClientProxyCodeEmitter() },
98 {"driver", new CServiceDriverCodeEmitter()},
99 {"stub", new CServiceStubCodeEmitter() },
100 {"service", new CServiceImplCodeEmitter() },
101 };
102
103 CodeEmitMap HDICodeGenerator::cppCodeEmitters_ = {
104 {"types", new CppCustomTypesCodeEmitter() },
105 {"interface", new CppInterfaceCodeEmitter() },
106 {"proxy", new CppClientProxyCodeEmitter() },
107 {"driver", new CppServiceDriverCodeEmitter()},
108 {"stub", new CppServiceStubCodeEmitter() },
109 {"service", new CppServiceImplCodeEmitter() },
110 };
111
112 CodeEmitMap HDICodeGenerator::javaCodeEmitters_ = {
113 {"interface", new JavaClientInterfaceCodeEmitter()},
114 {"proxy", new JavaClientProxyCodeEmitter() },
115 };
116
DoGenerate(const StrAstMap & allAst)117 bool HDICodeGenerator::DoGenerate(const StrAstMap &allAst)
118 {
119 auto genCodeFunc = GetCodeGenPoilcy();
120 if (!genCodeFunc) {
121 return false;
122 }
123
124 std::string outDir = Options::GetInstance().GetGenerationDirectory();
125 std::set<std::string> sourceFile = Options::GetInstance().GetSourceFiles();
126 Language language = Options::GetInstance().GetLanguage();
127 if (language == Language::CPP) {
128 for (const auto &ast : allAst) {
129 if (sourceFile.find(ast.second->GetIdlFilePath()) != sourceFile.end()) {
130 genCodeFunc(ast.second, outDir);
131 }
132 }
133 } else if (language == Language::C) {
134 for (const auto &ast : allAst) {
135 genCodeFunc(ast.second, outDir);
136 }
137 }
138
139 return true;
140 }
141
GetCodeGenPoilcy()142 CodeGenFunc HDICodeGenerator::GetCodeGenPoilcy()
143 {
144 auto systemPolicies = policies_.find(Options::GetInstance().GetSystemLevel());
145 if (systemPolicies == policies_.end()) {
146 Logger::E(TAG, "the system level is not supported, please check option");
147 return CodeGenFunc{};
148 }
149
150 auto genModePolicies = systemPolicies->second;
151 auto genModeIter = genModePolicies.find(Options::GetInstance().GetGenMode());
152 if (genModeIter == genModePolicies.end()) {
153 Logger::E(TAG, "the generate mode is not supported, please check option");
154 return CodeGenFunc{};
155 }
156
157 auto languagePolicies = genModeIter->second;
158 auto languageIter = languagePolicies.find(Options::GetInstance().GetLanguage());
159 if (languageIter == languagePolicies.end()) {
160 Logger::E(TAG, "the language is not supported, please check option");
161 return CodeGenFunc{};
162 }
163 return languageIter->second;
164 }
165
GenIpcCCode(const AutoPtr<AST> & ast,const std::string & outDir)166 void HDICodeGenerator::GenIpcCCode(const AutoPtr<AST> &ast, const std::string &outDir)
167 {
168 GenMode mode = GenMode::IPC;
169 switch (ast->GetASTFileType()) {
170 case ASTFileType::AST_TYPES: {
171 cCodeEmitters_["types"]->OutPut(ast, outDir, mode);
172 break;
173 }
174 case ASTFileType::AST_IFACE: {
175 cCodeEmitters_["interface"]->OutPut(ast, outDir, mode);
176 cCodeEmitters_["proxy"]->OutPut(ast, outDir, mode);
177 cCodeEmitters_["driver"]->OutPut(ast, outDir, mode);
178 cCodeEmitters_["stub"]->OutPut(ast, outDir, mode);
179 cCodeEmitters_["service"]->OutPut(ast, outDir, mode);
180 break;
181 }
182 case ASTFileType::AST_ICALLBACK: {
183 cCodeEmitters_["interface"]->OutPut(ast, outDir, mode);
184 cCodeEmitters_["proxy"]->OutPut(ast, outDir, mode);
185 cCodeEmitters_["stub"]->OutPut(ast, outDir, mode);
186 cCodeEmitters_["service"]->OutPut(ast, outDir, mode);
187 break;
188 }
189 default:
190 break;
191 }
192 }
193
GenIpcCppCode(const AutoPtr<AST> & ast,const std::string & outDir)194 void HDICodeGenerator::GenIpcCppCode(const AutoPtr<AST> &ast, const std::string &outDir)
195 {
196 GenMode mode = GenMode::IPC;
197 switch (ast->GetASTFileType()) {
198 case ASTFileType::AST_TYPES: {
199 cppCodeEmitters_["types"]->OutPut(ast, outDir, mode);
200 break;
201 }
202 case ASTFileType::AST_IFACE: {
203 cppCodeEmitters_["interface"]->OutPut(ast, outDir, mode);
204 cppCodeEmitters_["proxy"]->OutPut(ast, outDir, mode);
205 cppCodeEmitters_["driver"]->OutPut(ast, outDir, mode);
206 cppCodeEmitters_["stub"]->OutPut(ast, outDir, mode);
207 cppCodeEmitters_["service"]->OutPut(ast, outDir, mode);
208 break;
209 }
210 case ASTFileType::AST_ICALLBACK: {
211 cppCodeEmitters_["interface"]->OutPut(ast, outDir, mode);
212 cppCodeEmitters_["proxy"]->OutPut(ast, outDir, mode);
213 cppCodeEmitters_["stub"]->OutPut(ast, outDir, mode);
214 cppCodeEmitters_["service"]->OutPut(ast, outDir, mode);
215 break;
216 }
217 default:
218 break;
219 }
220 }
221
GenIpcJavaCode(const AutoPtr<AST> & ast,const std::string & outDir)222 void HDICodeGenerator::GenIpcJavaCode(const AutoPtr<AST> &ast, const std::string &outDir)
223 {
224 GenMode mode = GenMode::IPC;
225 switch (ast->GetASTFileType()) {
226 case ASTFileType::AST_TYPES: {
227 javaCodeEmitters_["types"]->OutPut(ast, outDir, mode);
228 break;
229 }
230 case ASTFileType::AST_IFACE:
231 case ASTFileType::AST_ICALLBACK: {
232 javaCodeEmitters_["interface"]->OutPut(ast, outDir, mode);
233 javaCodeEmitters_["proxy"]->OutPut(ast, outDir, mode);
234 break;
235 }
236 default:
237 break;
238 }
239 }
240
GenPassthroughCCode(const AutoPtr<AST> & ast,const std::string & outDir)241 void HDICodeGenerator::GenPassthroughCCode(const AutoPtr<AST> &ast, const std::string &outDir)
242 {
243 GenMode mode = GenMode::PASSTHROUGH;
244 switch (ast->GetASTFileType()) {
245 case ASTFileType::AST_TYPES: {
246 cCodeEmitters_["types"]->OutPut(ast, outDir, mode);
247 break;
248 }
249 case ASTFileType::AST_IFACE: {
250 cCodeEmitters_["interface"]->OutPut(ast, outDir, mode);
251 cCodeEmitters_["proxy"]->OutPut(ast, outDir, mode);
252 cCodeEmitters_["service"]->OutPut(ast, outDir, mode);
253 break;
254 }
255 case ASTFileType::AST_ICALLBACK: {
256 cCodeEmitters_["interface"]->OutPut(ast, outDir, mode);
257 cCodeEmitters_["service"]->OutPut(ast, outDir, mode);
258 break;
259 }
260 default:
261 break;
262 }
263 }
264
GenPassthroughCppCode(const AutoPtr<AST> & ast,const std::string & outDir)265 void HDICodeGenerator::GenPassthroughCppCode(const AutoPtr<AST> &ast, const std::string &outDir)
266 {
267 GenMode mode = GenMode::PASSTHROUGH;
268 switch (ast->GetASTFileType()) {
269 case ASTFileType::AST_TYPES: {
270 cppCodeEmitters_["types"]->OutPut(ast, outDir, mode);
271 break;
272 }
273 case ASTFileType::AST_IFACE: {
274 cppCodeEmitters_["interface"]->OutPut(ast, outDir, mode);
275 cppCodeEmitters_["proxy"]->OutPut(ast, outDir, mode);
276 cppCodeEmitters_["service"]->OutPut(ast, outDir, mode);
277 break;
278 }
279 case ASTFileType::AST_ICALLBACK: {
280 cppCodeEmitters_["interface"]->OutPut(ast, outDir, mode);
281 cppCodeEmitters_["service"]->OutPut(ast, outDir, mode);
282 break;
283 }
284 default:
285 break;
286 }
287 }
288
GenKernelCode(const AutoPtr<AST> & ast,const std::string & outDir)289 void HDICodeGenerator::GenKernelCode(const AutoPtr<AST> &ast, const std::string &outDir)
290 {
291 GenMode mode = GenMode::KERNEL;
292 switch (ast->GetASTFileType()) {
293 case ASTFileType::AST_TYPES: {
294 cCodeEmitters_["types"]->OutPut(ast, outDir, mode);
295 break;
296 }
297 case ASTFileType::AST_IFACE: {
298 cCodeEmitters_["interface"]->OutPut(ast, outDir, mode);
299 cCodeEmitters_["proxy"]->OutPut(ast, outDir, mode);
300 cCodeEmitters_["driver"]->OutPut(ast, outDir, mode);
301 cCodeEmitters_["stub"]->OutPut(ast, outDir, mode);
302 cCodeEmitters_["service"]->OutPut(ast, outDir, mode);
303 break;
304 }
305 default:
306 break;
307 }
308 }
309
GenLowCCode(const AutoPtr<AST> & ast,const std::string & outDir)310 void HDICodeGenerator::GenLowCCode(const AutoPtr<AST> &ast, const std::string &outDir)
311 {
312 GenMode mode = GenMode::LOW;
313 switch (ast->GetASTFileType()) {
314 case ASTFileType::AST_TYPES: {
315 cCodeEmitters_["types"]->OutPut(ast, outDir, mode);
316 break;
317 }
318 case ASTFileType::AST_IFACE: {
319 cCodeEmitters_["interface"]->OutPut(ast, outDir, mode);
320 cCodeEmitters_["driver"]->OutPut(ast, outDir, mode);
321 cCodeEmitters_["service"]->OutPut(ast, outDir, mode);
322 break;
323 }
324 case ASTFileType::AST_ICALLBACK: {
325 cCodeEmitters_["interface"]->OutPut(ast, outDir, mode);
326 cCodeEmitters_["service"]->OutPut(ast, outDir, mode);
327 break;
328 }
329 default:
330 break;
331 }
332 }
333
GeneratorInit()334 void HDICodeGenerator::GeneratorInit()
335 {
336 CodegenBuilder::GetInstance().GeneratorRegister(InterfaceType::HDI, new HDICodeGenerator());
337 }
338 } // namespace Idl
339 } // namespace OHOS