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_enum_type.h"
17 #include "util/string_builder.h"
18
19 namespace OHOS {
20 namespace Idl {
SetBaseType(const AutoPtr<ASTType> & baseType)21 void ASTEnumType::SetBaseType(const AutoPtr<ASTType> &baseType)
22 {
23 if (baseType == nullptr) {
24 return;
25 }
26 if (baseType->GetTypeKind() == TypeKind::TYPE_ENUM) {
27 AutoPtr<ASTEnumType> parentEnumType = static_cast<ASTEnumType *>(baseType.Get());
28 if (parentEnumType == nullptr) {
29 return;
30 }
31 std::vector<AutoPtr<ASTEnumValue>> parentMembers = parentEnumType->GetMembers();
32 for (auto member : parentMembers) {
33 members_.push_back(member);
34 }
35 parentType_= baseType;
36 baseType_ = parentEnumType->GetBaseType();
37 } else {
38 baseType_ = baseType;
39 }
40 }
41
AddMember(const AutoPtr<ASTEnumValue> & member)42 bool ASTEnumType::AddMember(const AutoPtr<ASTEnumValue> &member)
43 {
44 for (auto members : members_) {
45 if (member->GetName() == members->GetName()) {
46 return false;
47 }
48 }
49 members_.push_back(member);
50 return true;
51 }
52
GetBaseType()53 AutoPtr<ASTType> ASTEnumType::GetBaseType()
54 {
55 return baseType_;
56 }
57
GetSignature()58 std::string ASTEnumType::GetSignature()
59 {
60 return " ";
61 }
62
IsEnumType()63 bool ASTEnumType::IsEnumType()
64 {
65 return true;
66 }
67
Dump(const std::string & prefix)68 std::string ASTEnumType::Dump(const std::string &prefix)
69 {
70 StringBuilder sb;
71 sb.Append(prefix).Append(attr_->Dump(prefix));
72 if (baseType_ != nullptr) {
73 if (parentType_ != nullptr) {
74 sb.AppendFormat("enum %s : %s ", name_.c_str(), parentType_->ToString().c_str());
75 sb.AppendFormat(" : %s {\n", baseType_->ToString().c_str());
76 } else {
77 sb.AppendFormat("enum %s : %s {\n", name_.c_str(), baseType_->ToString().c_str());
78 }
79 } else {
80 sb.AppendFormat("enum %s {\n", name_.c_str());
81 }
82
83 if (members_.size() > 0) {
84 for (auto it : members_) {
85 AutoPtr<ASTExpr> value = it->GetExprValue();
86 if (value == nullptr) {
87 sb.Append(" ").AppendFormat("%s,\n", it->GetName().c_str());
88 } else {
89 sb.Append(" ").AppendFormat("%s = %s,\n", it->GetName().c_str(), value->Dump("").c_str());
90 }
91 }
92 }
93
94 sb.Append(prefix).Append("};\n");
95
96 return sb.ToString();
97 }
98
GetTypeKind()99 TypeKind ASTEnumType::GetTypeKind()
100 {
101 return TypeKind::TYPE_ENUM;
102 }
103 } // namespace Idl
104 } // namespace OHOS
105