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 "ast/ast_struct_type.h"
17 #include "util/string_builder.h"
18
19 namespace OHOS {
20 namespace Idl {
SetParentType(const AutoPtr<ASTStructType> & parentType)21 void ASTStructType::SetParentType(const AutoPtr<ASTStructType> &parentType)
22 {
23 if (parentType == nullptr) {
24 return;
25 }
26
27 std::vector<std::tuple<std::string, AutoPtr<ASTType>>> parentMembers = parentType->GetMembers();
28 for (auto member : parentMembers) {
29 AddMember(std::get<1>(member), std::get<0>(member));
30 }
31 parentType_ = parentType;
32 }
33
AddMember(const AutoPtr<ASTType> & typeName,std::string name)34 void ASTStructType::AddMember(const AutoPtr<ASTType> &typeName, std::string name)
35 {
36 for (auto it : members_) {
37 if (std::get<0>(it) == name) {
38 return;
39 }
40 }
41 members_.push_back(std::make_tuple(name, typeName));
42
43 if (!typeName->IsPod()) {
44 isPod_ = false;
45 }
46 }
47
GetSignature()48 std::string ASTStructType::GetSignature()
49 {
50 return " ";
51 }
52
IsStructType()53 bool ASTStructType::IsStructType()
54 {
55 return true;
56 }
57
Dump(const std::string & prefix)58 std::string ASTStructType::Dump(const std::string &prefix)
59 {
60 StringBuilder sb;
61 sb.Append(prefix).Append(attr_->Dump(prefix));
62 if (parentType_ == nullptr) {
63 sb.AppendFormat("struct %s {\n", name_.c_str());
64 } else {
65 sb.AppendFormat("struct %s : %s {\n", name_.c_str(), parentType_->ToString().c_str());
66 }
67 if (members_.size() > 0) {
68 for (auto it : members_) {
69 sb.Append(prefix + " ");
70 sb.AppendFormat("%s %s;\n", std::get<1>(it)->ToString().c_str(), std::get<0>(it).c_str());
71 }
72 }
73 sb.Append(prefix).Append("};\n");
74 return sb.ToString();
75 }
76
GetTypeKind()77 TypeKind ASTStructType::GetTypeKind()
78 {
79 return TypeKind::TYPE_STRUCT;
80 }
81 } // namespace Idl
82 } // namespace OHOS