1 /*
2 * Copyright (c) 2021-2023 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 "ast/ast_type.h"
10
11 namespace OHOS {
12 namespace HDI {
SetName(const std::string & name)13 void ASTType::SetName(const std::string &name)
14 {
15 name_ = name;
16 }
17
GetName()18 std::string ASTType::GetName()
19 {
20 return name_;
21 }
22
SetNamespace(const AutoPtr<ASTNamespace> & nspace)23 void ASTType::SetNamespace(const AutoPtr<ASTNamespace> &nspace)
24 {
25 namespace_ = nspace;
26 }
27
GetNamespace()28 AutoPtr<ASTNamespace> ASTType::GetNamespace()
29 {
30 return namespace_;
31 }
32
IsBooleanType()33 bool ASTType::IsBooleanType()
34 {
35 return false;
36 }
37
IsByteType()38 bool ASTType::IsByteType()
39 {
40 return false;
41 }
42
IsShortType()43 bool ASTType::IsShortType()
44 {
45 return false;
46 }
47
IsIntegerType()48 bool ASTType::IsIntegerType()
49 {
50 return false;
51 }
52
IsLongType()53 bool ASTType::IsLongType()
54 {
55 return false;
56 }
57
IsUcharType()58 bool ASTType::IsUcharType()
59 {
60 return false;
61 }
62
IsUshortType()63 bool ASTType::IsUshortType()
64 {
65 return false;
66 }
67
IsUintType()68 bool ASTType::IsUintType()
69 {
70 return false;
71 }
72
IsUlongType()73 bool ASTType::IsUlongType()
74 {
75 return false;
76 }
77
IsFloatType()78 bool ASTType::IsFloatType()
79 {
80 return false;
81 }
82
IsDoubleType()83 bool ASTType::IsDoubleType()
84 {
85 return false;
86 }
87
IsStringType()88 bool ASTType::IsStringType()
89 {
90 return false;
91 }
92
IsListType()93 bool ASTType::IsListType()
94 {
95 return false;
96 }
97
IsMapType()98 bool ASTType::IsMapType()
99 {
100 return false;
101 }
102
IsEnumType()103 bool ASTType::IsEnumType()
104 {
105 return false;
106 }
107
IsStructType()108 bool ASTType::IsStructType()
109 {
110 return false;
111 }
112
IsUnionType()113 bool ASTType::IsUnionType()
114 {
115 return false;
116 }
117
IsInterfaceType()118 bool ASTType::IsInterfaceType()
119 {
120 return false;
121 }
122
IsSequenceableType()123 bool ASTType::IsSequenceableType()
124 {
125 return false;
126 }
127
IsArrayType()128 bool ASTType::IsArrayType()
129 {
130 return false;
131 }
132
IsFdType()133 bool ASTType::IsFdType()
134 {
135 return false;
136 }
137
IsSmqType()138 bool ASTType::IsSmqType()
139 {
140 return false;
141 }
142
IsAshmemType()143 bool ASTType::IsAshmemType()
144 {
145 return false;
146 }
147
IsNativeBufferType()148 bool ASTType::IsNativeBufferType()
149 {
150 return false;
151 }
152
IsPointerType()153 bool ASTType::IsPointerType()
154 {
155 return false;
156 }
157
IsPod() const158 bool ASTType::IsPod() const
159 {
160 return isPod_;
161 }
162
HasInnerType(TypeKind innerType) const163 bool ASTType::HasInnerType(TypeKind innerType) const
164 {
165 return false;
166 }
167
ToShortString()168 std::string ASTType::ToShortString()
169 {
170 return name_;
171 }
172
ToString() const173 std::string ASTType::ToString() const
174 {
175 return (namespace_ == nullptr) ? name_ : (namespace_->ToString() + name_);
176 }
177
GetTypeKind()178 TypeKind ASTType::GetTypeKind()
179 {
180 return TypeKind::TYPE_UNKNOWN;
181 }
182
EmitCType(TypeMode mode) const183 std::string ASTType::EmitCType(TypeMode mode) const
184 {
185 return "unknow";
186 }
187
EmitCppType(TypeMode mode) const188 std::string ASTType::EmitCppType(TypeMode mode) const
189 {
190 return "unknow";
191 }
192
EmitJavaType(TypeMode mode,bool isInnerType) const193 std::string ASTType::EmitJavaType(TypeMode mode, bool isInnerType) const
194 {
195 return "unknow";
196 }
197
EmitCWriteVar(const std::string & parcelName,const std::string & name,const std::string & ecName,const std::string & gotoLabel,StringBuilder & sb,const std::string & prefix) const198 void ASTType::EmitCWriteVar(const std::string &parcelName, const std::string &name, const std::string &ecName,
199 const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
200 {
201 sb.Append(prefix).AppendFormat("//Writing \"%s\" type of data is not supported\n", name_.c_str());
202 }
203
EmitCProxyWriteOutVar(const std::string & parcelName,const std::string & name,const std::string & ecName,const std::string & gotoLabel,StringBuilder & sb,const std::string & prefix) const204 void ASTType::EmitCProxyWriteOutVar(const std::string &parcelName, const std::string &name, const std::string &ecName,
205 const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
206 {
207 sb.Append(prefix).AppendFormat("//Writing \"%s\" type of data is not supported\n", name_.c_str());
208 }
209
EmitCProxyReadVar(const std::string & parcelName,const std::string & name,bool isInnerType,const std::string & ecName,const std::string & gotoLabel,StringBuilder & sb,const std::string & prefix) const210 void ASTType::EmitCProxyReadVar(const std::string &parcelName, const std::string &name, bool isInnerType,
211 const std::string &ecName, const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
212 {
213 sb.Append(prefix).AppendFormat("//Reading \"%s\" type of data is not supported\n", name_.c_str());
214 }
215
EmitCStubReadVar(const std::string & parcelName,const std::string & name,const std::string & ecName,const std::string & gotoLabel,StringBuilder & sb,const std::string & prefix) const216 void ASTType::EmitCStubReadVar(const std::string &parcelName, const std::string &name, const std::string &ecName,
217 const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const
218 {
219 sb.Append(prefix).AppendFormat("//Reading \"%s\" type of data is not supported\n", name_.c_str());
220 }
221
EmitCStubReadOutVar(const std::string & buffSizeName,const std::string & memFlagName,const std::string & parcelName,const std::string & name,const std::string & ecName,const std::string & gotoLabel,StringBuilder & sb,const std::string & prefix) const222 void ASTType::EmitCStubReadOutVar(const std::string &buffSizeName, const std::string &memFlagName,
223 const std::string &parcelName, const std::string &name, const std::string &ecName, const std::string &gotoLabel,
224 StringBuilder &sb, const std::string &prefix) const
225 {
226 sb.Append(prefix).AppendFormat("//Reading \"%s\" type of data is not supported\n", name_.c_str());
227 }
228
EmitCppWriteVar(const std::string & parcelName,const std::string & name,StringBuilder & sb,const std::string & prefix,unsigned int innerLevel) const229 void ASTType::EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
230 const std::string &prefix, unsigned int innerLevel) const
231 {
232 sb.Append(prefix).AppendFormat("//Writing \"%s\" type of data is not supported\n", name_.c_str());
233 }
234
EmitCppReadVar(const std::string & parcelName,const std::string & name,StringBuilder & sb,const std::string & prefix,bool initVariable,unsigned int innerLevel) const235 void ASTType::EmitCppReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
236 const std::string &prefix, bool initVariable, unsigned int innerLevel) const
237 {
238 sb.Append(prefix).AppendFormat("//Reading \"%s\" type of data is not supported\n", name_.c_str());
239 }
240
EmitCMarshalling(const std::string & name,StringBuilder & sb,const std::string & prefix) const241 void ASTType::EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const
242 {
243 sb.Append(prefix).AppendFormat("//Writing \"%s\" type of data is not supported\n", name_.c_str());
244 }
245
EmitCUnMarshalling(const std::string & name,const std::string & gotoLabel,StringBuilder & sb,const std::string & prefix,std::vector<std::string> & freeObjStatements) const246 void ASTType::EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
247 const std::string &prefix, std::vector<std::string> &freeObjStatements) const
248 {
249 sb.Append(prefix).AppendFormat("//Reading \"%s\" type of data is not supported\n", name_.c_str());
250 }
251
EmitFreeStatements(const std::vector<std::string> & freeObjStatements,StringBuilder & sb,const std::string & prefix) const252 void ASTType::EmitFreeStatements(
253 const std::vector<std::string> &freeObjStatements, StringBuilder &sb, const std::string &prefix) const
254 {
255 for (auto it = freeObjStatements.rbegin(); it != freeObjStatements.rend(); it++) {
256 sb.Append(prefix).Append(*it);
257 }
258 }
259
EmitCppMarshalling(const std::string & parcelName,const std::string & name,StringBuilder & sb,const std::string & prefix,unsigned int innerLevel) const260 void ASTType::EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
261 const std::string &prefix, unsigned int innerLevel) const
262 {
263 sb.Append(prefix).AppendFormat("//Writing \"%s\" type of data is not supported\n", name_.c_str());
264 }
265
EmitCppUnMarshalling(const std::string & parcelName,const std::string & name,StringBuilder & sb,const std::string & prefix,bool emitType,unsigned int innerLevel) const266 void ASTType::EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
267 const std::string &prefix, bool emitType, unsigned int innerLevel) const
268 {
269 sb.Append(prefix).AppendFormat("//Reading \"%s\" type of data is not supported\n", name_.c_str());
270 }
271
EmitMemoryRecycle(const std::string & name,bool isClient,bool ownership,StringBuilder & sb,const std::string & prefix) const272 void ASTType::EmitMemoryRecycle(
273 const std::string &name, bool isClient, bool ownership, StringBuilder &sb, const std::string &prefix) const
274 {
275 // only std::string, Array, List, struct and union type need recycle memory
276 (void)name;
277 (void)isClient;
278 (void)ownership;
279 (void)sb;
280 (void)prefix;
281 }
282
EmitJavaWriteVar(const std::string & parcelName,const std::string & name,StringBuilder & sb,const std::string & prefix) const283 void ASTType::EmitJavaWriteVar(
284 const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const
285 {
286 sb.Append(prefix).AppendFormat("//Writing \"%s\" type of data is not supported\n", name_.c_str());
287 }
288
EmitJavaReadVar(const std::string & parcelName,const std::string & name,StringBuilder & sb,const std::string & prefix) const289 void ASTType::EmitJavaReadVar(
290 const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const
291 {
292 sb.Append(prefix).AppendFormat("//Reading \"%s\" type of data is not supported\n", name_.c_str());
293 }
294
EmitJavaReadInnerVar(const std::string & parcelName,const std::string & name,bool isInner,StringBuilder & sb,const std::string & prefix) const295 void ASTType::EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner,
296 StringBuilder &sb, const std::string &prefix) const
297 {
298 sb.Append(prefix).AppendFormat("//Reading \"%s\" type of data is not supported\n", name_.c_str());
299 }
300
RegisterWriteMethod(Language language,SerMode mode,UtilMethodMap & methods) const301 void ASTType::RegisterWriteMethod(Language language, SerMode mode, UtilMethodMap &methods) const
302 {
303 // register methods that generate write util methods
304 (void)language;
305 (void)methods;
306 }
307
RegisterReadMethod(Language language,SerMode mode,UtilMethodMap & methods) const308 void ASTType::RegisterReadMethod(Language language, SerMode mode, UtilMethodMap &methods) const
309 {
310 // register methods that generate read util methods
311 (void)language;
312 (void)methods;
313 }
314
GetNameWithNamespace(AutoPtr<ASTNamespace> space,std::string name) const315 std::string ASTType::GetNameWithNamespace(AutoPtr<ASTNamespace> space, std::string name) const
316 {
317 std::vector<std::string> namespaceVec = StringHelper::Split(space->ToString(), ".");
318 std::regex rVer("[V|v][0-9]+_[0-9]+");
319 std::vector<std::string> result;
320 bool findVersion = false;
321
322 std::string rootPackage = Options::GetInstance().GetRootPackage(space->ToString());
323 size_t rootPackageNum = StringHelper::Split(rootPackage, ".").size();
324
325 for (size_t i = 0; i < namespaceVec.size(); i++) {
326 std::string ns;
327 if (i < rootPackageNum) {
328 ns = StringHelper::StrToUpper(namespaceVec[i]);
329 } else if (!findVersion && std::regex_match(namespaceVec[i].c_str(), rVer)) {
330 ns = StringHelper::Replace(namespaceVec[i], 'v', 'V');
331 findVersion = true;
332 } else {
333 if (findVersion) {
334 ns = namespaceVec[i];
335 } else {
336 ns = PascalName(namespaceVec[i]);
337 }
338 }
339
340 result.emplace_back(ns);
341 }
342 StringBuilder sb;
343 for (const auto &ns : result) {
344 sb.AppendFormat("%s::", ns.c_str());
345 }
346 sb.Append(name);
347 return sb.ToString();
348 }
349
PascalName(const std::string & name) const350 std::string ASTType::PascalName(const std::string &name) const
351 {
352 if (name.empty()) {
353 return name;
354 }
355
356 StringBuilder sb;
357 for (size_t i = 0; i < name.size(); i++) {
358 char c = name[i];
359 if (i == 0) {
360 if (islower(c)) {
361 c = toupper(c);
362 }
363 sb.Append(c);
364 } else {
365 if (c == '_') {
366 continue;
367 }
368
369 if (islower(c) && name[i - 1] == '_') {
370 c = toupper(c);
371 }
372 sb.Append(c);
373 }
374 }
375
376 return sb.ToString();
377 }
378 } // namespace HDI
379 } // namespace OHOS