• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "sa_code_generator.h"
17 #include "cpp/sa_cpp_client_code_emitter.h"
18 #include "cpp/sa_cpp_interface_code_emitter.h"
19 #include "cpp/sa_cpp_client_proxy_code_emitter.h"
20 #include "cpp/sa_cpp_service_stub_code_emitter.h"
21 #include "cpp/sa_cpp_custom_types_code_emitter.h"
22 #include "ts/sa_ts_interface_code_emitter.h"
23 #include "ts/sa_ts_client_proxy_code_emitter.h"
24 #include "ts/sa_ts_service_stub_code_emitter.h"
25 #include "rust/sa_rust_interface_code_emitter.h"
26 #include "util/options.h"
27 #include "util/logger.h"
28 #include "parser/parser.h"
29 
30 namespace OHOS {
31 namespace Idl {
32 SACodeGenerator::GeneratePolicies SACodeGenerator::policies_ = {
33     {Language::CPP, SACodeGenerator::GenCppCode},
34     {Language::RUST, SACodeGenerator::GenRustCode},
35     {Language::TS, SACodeGenerator::GenTsCode}
36 };
37 
38 CodeEmitMap SACodeGenerator::cppCodeEmitters_ = {
39     {"client", new SaCppClientCodeEmitter()},
40     {"interface", new SaCppInterfaceCodeEmitter()},
41     {"proxy", new SaCppClientProxyCodeEmitter()},
42     {"stub", new SaCppServiceStubCodeEmitter()},
43     {"types", new SaCppCustomTypesCodeEmitter()},
44 };
45 
46 CodeEmitMap SACodeGenerator::tsCodeEmitters_ = {
47     {"interface", new SaTsInterfaceCodeEmitter()},
48     {"proxy",     new SaTsClientProxyCodeEmitter()},
49     {"stub",     new SaTsServiceStubCodeEmitter()},
50 };
51 
52 CodeEmitMap SACodeGenerator::rustCodeEmitters_ = {
53     {"interface", new SaRustInterfaceCodeEmitter()},
54 };
55 
56 // stores the path where the generated code will be saved
57 std::unordered_map<std::string, std::string> SACodeGenerator::genPath_;
58 // The path of the IDL file passed with the -c option
59 std::string SACodeGenerator::mainFilePath_;
60 
GenCodeInit()61 void SACodeGenerator::GenCodeInit()
62 {
63     genPath_ = {};
64     mainFilePath_ = "";
65 }
66 
DoGenerate(const StrAstMap & allAst)67 bool SACodeGenerator::DoGenerate(const StrAstMap &allAst)
68 {
69     GenCodeInit();
70 
71     auto genCodeFunc = GetCodeGenPoilcy();
72     if (!genCodeFunc) {
73         return false;
74     }
75 
76     std::string outDir = Options::GetInstance().GetGenerationDirectory();
77 
78     for (const auto &astPair : allAst) {
79         AutoPtr<AST> ast = astPair.second;
80         if (ast->GetASTFileType() == ASTFileType::AST_IFACE) {
81             mainFilePath_ = ast->GetIdlFile();
82             GenCppPath(ast, outDir);
83         }
84     }
85 
86     for (const auto &astPair : allAst) {
87         AutoPtr<AST> ast = astPair.second;
88         if (ast->GetASTFileType() == ASTFileType::AST_ICALLBACK) {
89             GenCppPath(ast, outDir);
90         }
91     }
92 
93     for (const auto &astPair : allAst) {
94         AutoPtr<AST> ast = astPair.second;
95         if (ast->GetASTFileType() == ASTFileType::AST_IFACE ||
96             ast->GetASTFileType() == ASTFileType::AST_TYPES ||
97             ast->GetASTFileType() == ASTFileType::AST_ICALLBACK) {
98             genCodeFunc(ast, outDir);
99         }
100     }
101 
102     return true;
103 }
104 
GetCodeGenPoilcy()105 CodeGenFunc SACodeGenerator::GetCodeGenPoilcy()
106 {
107     auto languageIter = policies_.find(Options::GetInstance().GetLanguage());
108     if (languageIter == policies_.end()) {
109         Logger::E(TAG, "the language is not supported, please check option");
110         return CodeGenFunc{};
111     }
112     return languageIter->second;
113 }
114 
GenCppPath(const AutoPtr<AST> & ast,const std::string & outDir)115 void SACodeGenerator::GenCppPath(const AutoPtr<AST> &ast, const std::string &outDir)
116 {
117     for (const auto &importPath : ast->GetImportNames()) {
118         std::string idlFilePath = ast->GetIdlFile();
119         size_t index = idlFilePath.rfind(SEPARATOR);
120         std::string importFilePath = File::CanonicalPath(idlFilePath.substr(0, index + 1) + importPath + ".idl");
121         std::string pathIdl = idlFilePath.substr(0, index);
122         index = mainFilePath_.rfind(SEPARATOR);
123         std::string pathMainFile = mainFilePath_.substr(0, index);
124         std::string relaPath = File::RelativePath(pathIdl, pathMainFile);
125         if (!relaPath.empty()) {
126             relaPath = SEPARATOR + relaPath;
127         }
128 
129         index = importPath.rfind(SEPARATOR);
130         if (index == std::string::npos) {
131             index = 0;
132         }
133         std::string relativePath = outDir + relaPath + SEPARATOR + importPath.substr(0, index);
134 
135 #ifdef __MINGW32__
136         std::replace(relativePath.begin(), relativePath.end(), '/', '\\');
137 #endif
138 
139         std::string absolutePath = File::AbsolutePath(relativePath);
140         std::string resolvePath = File::CanonicalPath(absolutePath);
141         if (!File::CreateParentDir(resolvePath)) {
142             Logger::E(TAG, "Failed to create directory: %s.", resolvePath.c_str());
143         }
144 
145         genPath_[importFilePath] = resolvePath;
146     }
147 }
148 
GenCppCode(const AutoPtr<AST> & ast,const std::string & outDir)149 void SACodeGenerator::GenCppCode(const AutoPtr<AST> &ast, const std::string &outDir)
150 {
151     GenMode mode = GenMode::IPC;
152     bool isClientOn = Options::GetInstance().DoClient();
153     switch (ast->GetASTFileType()) {
154         case ASTFileType::AST_TYPES: {
155             cppCodeEmitters_["types"]->OutPut(ast, genPath_[ast->GetIdlFile()], mode);
156             break;
157         }
158         case ASTFileType::AST_IFACE: {
159             cppCodeEmitters_["interface"]->OutPut(ast, outDir, mode);
160             cppCodeEmitters_["proxy"]->OutPut(ast, outDir, mode);
161             cppCodeEmitters_["stub"]->OutPut(ast, outDir, mode);
162             if (isClientOn) {
163                 cppCodeEmitters_["client"]->OutPut(ast, outDir, mode);
164             }
165             break;
166         }
167         case ASTFileType::AST_ICALLBACK: {
168             cppCodeEmitters_["interface"]->OutPut(ast, genPath_[ast->GetIdlFile()], mode);
169             cppCodeEmitters_["proxy"]->OutPut(ast, genPath_[ast->GetIdlFile()], mode);
170             cppCodeEmitters_["stub"]->OutPut(ast, genPath_[ast->GetIdlFile()], mode);
171             break;
172         }
173         default:
174             break;
175     }
176 }
177 
GenRustCode(const AutoPtr<AST> & ast,const std::string & outDir)178 void SACodeGenerator::GenRustCode(const AutoPtr<AST> &ast, const std::string &outDir)
179 {
180     rustCodeEmitters_["interface"]->OutPut(ast, outDir, GenMode::IPC);
181     return;
182 }
183 
GenTsCode(const AutoPtr<AST> & ast,const std::string & outDir)184 void SACodeGenerator::GenTsCode(const AutoPtr<AST> &ast, const std::string &outDir)
185 {
186     tsCodeEmitters_["interface"]->OutPut(ast, outDir, GenMode::IPC);
187     tsCodeEmitters_["proxy"]->OutPut(ast, outDir, GenMode::IPC);
188     tsCodeEmitters_["stub"]->OutPut(ast, outDir, GenMode::IPC);
189     return;
190 }
191 
GeneratorInit()192 void SACodeGenerator::GeneratorInit()
193 {
194     CodegenBuilder::GetInstance().GeneratorRegister(InterfaceType::SA, new SACodeGenerator());
195 }
196 } // namespace Idl
197 } // namespace OHOS