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 ownership,StringBuilder & sb,const std::string & prefix) const272 void ASTType::EmitMemoryRecycle(
273 const std::string &name, 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)ownership;
278 (void)sb;
279 (void)prefix;
280 }
281
EmitJavaWriteVar(const std::string & parcelName,const std::string & name,StringBuilder & sb,const std::string & prefix) const282 void ASTType::EmitJavaWriteVar(
283 const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const
284 {
285 sb.Append(prefix).AppendFormat("//Writing \"%s\" type of data is not supported\n", name_.c_str());
286 }
287
EmitJavaReadVar(const std::string & parcelName,const std::string & name,StringBuilder & sb,const std::string & prefix) const288 void ASTType::EmitJavaReadVar(
289 const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const
290 {
291 sb.Append(prefix).AppendFormat("//Reading \"%s\" type of data is not supported\n", name_.c_str());
292 }
293
EmitJavaReadInnerVar(const std::string & parcelName,const std::string & name,bool isInner,StringBuilder & sb,const std::string & prefix) const294 void ASTType::EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner,
295 StringBuilder &sb, const std::string &prefix) const
296 {
297 sb.Append(prefix).AppendFormat("//Reading \"%s\" type of data is not supported\n", name_.c_str());
298 }
299
RegisterWriteMethod(Language language,SerMode mode,UtilMethodMap & methods) const300 void ASTType::RegisterWriteMethod(Language language, SerMode mode, UtilMethodMap &methods) const
301 {
302 // register methods that generate write util methods
303 (void)language;
304 (void)methods;
305 }
306
RegisterReadMethod(Language language,SerMode mode,UtilMethodMap & methods) const307 void ASTType::RegisterReadMethod(Language language, SerMode mode, UtilMethodMap &methods) const
308 {
309 // register methods that generate read util methods
310 (void)language;
311 (void)methods;
312 }
313
GetNameWithNamespace(AutoPtr<ASTNamespace> space,std::string name) const314 std::string ASTType::GetNameWithNamespace(AutoPtr<ASTNamespace> space, std::string name) const
315 {
316 std::vector<std::string> namespaceVec = StringHelper::Split(space->ToString(), ".");
317 std::regex rVer("[V|v][0-9]+_[0-9]+");
318 std::vector<std::string> result;
319 bool findVersion = false;
320
321 std::string rootPackage = Options::GetInstance().GetRootPackage(space->ToString());
322 size_t rootPackageNum = StringHelper::Split(rootPackage, ".").size();
323
324 for (size_t i = 0; i < namespaceVec.size(); i++) {
325 std::string ns;
326 if (i < rootPackageNum) {
327 ns = StringHelper::StrToUpper(namespaceVec[i]);
328 } else if (!findVersion && std::regex_match(namespaceVec[i].c_str(), rVer)) {
329 ns = StringHelper::Replace(namespaceVec[i], 'v', 'V');
330 findVersion = true;
331 } else {
332 if (findVersion) {
333 ns = namespaceVec[i];
334 } else {
335 ns = PascalName(namespaceVec[i]);
336 }
337 }
338
339 result.emplace_back(ns);
340 }
341 StringBuilder sb;
342 for (const auto &ns : result) {
343 sb.AppendFormat("%s::", ns.c_str());
344 }
345 sb.Append(name);
346 return sb.ToString();
347 }
348
PascalName(const std::string & name) const349 std::string ASTType::PascalName(const std::string &name) const
350 {
351 if (name.empty()) {
352 return name;
353 }
354
355 StringBuilder sb;
356 for (size_t i = 0; i < name.size(); i++) {
357 char c = name[i];
358 if (i == 0) {
359 if (islower(c)) {
360 c = toupper(c);
361 }
362 sb.Append(c);
363 } else {
364 if (c == '_') {
365 continue;
366 }
367
368 if (islower(c) && name[i - 1] == '_') {
369 c = toupper(c);
370 }
371 sb.Append(c);
372 }
373 }
374
375 return sb.ToString();
376 }
377
GetNamespaceWithColon(AutoPtr<ASTNamespace> space,std::string name) const378 std::string ASTType::GetNamespaceWithColon(AutoPtr<ASTNamespace> space, std::string name) const
379 {
380 std::string value = GetNameWithNamespace(space, name);
381 if (value.empty()) {
382 return "";
383 }
384 size_t index = value.rfind(':');
385 return (index == std::string::npos) ? value.substr(0) : value.substr(0, index + 1);
386 }
387 } // namespace HDI
388 } // namespace OHOS