• 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 #ifndef OHOS_IDL_ASTSTRUCTTYPE_H
17 #define OHOS_IDL_ASTSTRUCTTYPE_H
18 
19 #include <tuple>
20 #include <vector>
21 
22 #include "ast/ast_attribute.h"
23 #include "ast/ast_type.h"
24 #include "util/autoptr.h"
25 
26 namespace OHOS {
27 namespace Idl {
28 class ASTStructType : public ASTType {
29 public:
ASTStructType()30     ASTStructType() : ASTType(true), attr_(new ASTAttr()), members_() {}
31 
SetName(const std::string & name)32     inline void SetName(const std::string &name) override
33     {
34         name_ = name;
35     }
36 
GetName()37     inline std::string GetName() override
38     {
39         return name_;
40     }
41 
SetAttribute(const AutoPtr<ASTAttr> & attr)42     inline void SetAttribute(const AutoPtr<ASTAttr> &attr)
43     {
44         if (attr != nullptr) {
45             attr_ = attr;
46         }
47     }
48 
IsFull()49     inline bool IsFull()
50     {
51         return attr_ != nullptr ? attr_->HasValue(ASTAttr::FULL) : false;
52     }
53 
IsLite()54     inline bool IsLite()
55     {
56         return attr_ != nullptr ? attr_->HasValue(ASTAttr::LITE) : false;
57     }
58 
59     void SetParentType(const AutoPtr<ASTStructType> &parentType);
60 
61     void AddMember(const AutoPtr<ASTType> &typeName, std::string name);
62 
GetMembers()63     inline std::vector<std::tuple<std::string, AutoPtr<ASTType>>> GetMembers()
64     {
65         return members_;
66     }
67 
GetMemberNumber()68     inline size_t GetMemberNumber()
69     {
70         return members_.size();
71     }
72 
GetMemberName(size_t index)73     inline std::string GetMemberName(size_t index)
74     {
75         if (index >= members_.size()) {
76             return std::string("");
77         }
78         return std::get<0>(members_[index]);
79     }
80 
GetMemberType(size_t index)81     inline AutoPtr<ASTType> GetMemberType(size_t index)
82     {
83         if (index >= members_.size()) {
84             return nullptr;
85         }
86         return std::get<1>(members_[index]);
87     }
88 
89     std::string GetSignature() override;
90 
91     bool IsStructType() override;
92 
93     std::string Dump(const std::string &prefix) override;
94 
95     TypeKind GetTypeKind() override;
96 
97 private:
98     AutoPtr<ASTAttr> attr_;
99     std::vector<std::tuple<std::string, AutoPtr<ASTType>>> members_;
100     AutoPtr<ASTStructType> parentType_; // used to dump parent type when using struct extension identify in idl
101 };
102 } // namespace Idl
103 } // namespace OHOS
104 
105 #endif // OHOS_IDL_ASTSTRUCTTYPE_H