• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #ifndef OHOS_HDI_ASTENUMTYPE_H
10 #define OHOS_HDI_ASTENUMTYPE_H
11 
12 #include "ast/ast_attribute.h"
13 #include "ast/ast_expr.h"
14 #include "ast/ast_type.h"
15 #include "util/autoptr.h"
16 
17 #include <vector>
18 
19 namespace OHOS {
20 namespace HDI {
21 class ASTEnumValue : public ASTNode {
22 public:
ASTEnumValue(const std::string & name)23     explicit ASTEnumValue(const std::string &name) : mName_(name), value_(nullptr) {}
24 
~ASTEnumValue()25     inline virtual ~ASTEnumValue() {}
26 
GetName()27     inline std::string GetName()
28     {
29         return mName_;
30     }
31 
SetType(const AutoPtr<ASTType> & type)32     inline void SetType(const AutoPtr<ASTType> &type)
33     {
34         mType_ = type;
35     }
36 
GetType()37     inline AutoPtr<ASTType> GetType()
38     {
39         return mType_;
40     }
41 
SetExprValue(const AutoPtr<ASTExpr> & value)42     inline void SetExprValue(const AutoPtr<ASTExpr> &value)
43     {
44         value_ = value;
45     }
46 
GetExprValue()47     inline AutoPtr<ASTExpr> GetExprValue()
48     {
49         return value_;
50     }
51 
52 private:
53     std::string mName_;
54     AutoPtr<ASTType> mType_;
55     AutoPtr<ASTExpr> value_;
56 };
57 
58 class ASTEnumType : public ASTType {
59 public:
ASTEnumType()60     ASTEnumType() : ASTType(TypeKind::TYPE_ENUM, true), attr_(new ASTTypeAttr()), baseType_(), members_() {}
61 
SetName(const std::string & name)62     inline void SetName(const std::string &name)
63     {
64         name_ = name;
65     }
66 
GetName()67     inline std::string GetName()
68     {
69         return name_;
70     }
71 
SetAttribute(const AutoPtr<ASTTypeAttr> & attr)72     inline void SetAttribute(const AutoPtr<ASTTypeAttr> &attr)
73     {
74         if (attr != nullptr) {
75             attr_ = attr;
76         }
77     }
78 
IsFull()79     inline bool IsFull()
80     {
81         return attr_ != nullptr ? attr_->isFull_ : false;
82     }
83 
IsLite()84     inline bool IsLite()
85     {
86         return attr_ != nullptr ? attr_->isLite_ : false;
87     }
88 
89     void SetBaseType(const AutoPtr<ASTType> &baseType);
90 
GetBaseType()91     inline AutoPtr<ASTType> GetBaseType()
92     {
93         return baseType_;
94     }
95 
96     void AddMember(const AutoPtr<ASTEnumValue> &member);
97 
GetMemberNumber()98     inline size_t GetMemberNumber()
99     {
100         return members_.size();
101     }
102 
GetMember(size_t index)103     inline AutoPtr<ASTEnumValue> GetMember(size_t index)
104     {
105         if (index >= members_.size()) {
106             return nullptr;
107         }
108         return members_[index];
109     }
110 
111     bool IsEnumType() override;
112 
113     std::string ToString() const override;
114 
115     std::string Dump(const std::string &prefix) override;
116 
117     TypeKind GetTypeKind() override;
118 
119     std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
120 
121     std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
122 
123     std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
124 
125     std::string EmitCTypeDecl() const;
126 
127     std::string EmitCppTypeDecl() const;
128 
129     std::string EmitJavaTypeDecl() const;
130 
131     void EmitCWriteVar(const std::string &parcelName, const std::string &name, const std::string &ecName,
132         const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const override;
133 
134     void EmitCProxyReadVar(const std::string &parcelName, const std::string &name, bool isInnerType,
135         const std::string &ecName, const std::string &gotoLabel, StringBuilder &sb,
136         const std::string &prefix) const override;
137 
138     void EmitCStubReadVar(const std::string &parcelName, const std::string &name, const std::string &ecName,
139         const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const override;
140 
141     void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
142         const std::string &prefix, unsigned int innerLevel = 0) const override;
143 
144     void EmitCppReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
145         const std::string &prefix, bool initVariable, unsigned int innerLevel = 0) const override;
146 
147     void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const override;
148 
149     void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
150         const std::string &prefix, std::vector<std::string> &freeObjStatements) const override;
151 
152     void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
153         const std::string &prefix, unsigned int innerLevel = 0) const override;
154 
155     void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
156         const std::string &prefix, bool emitType, unsigned int innerLevel = 0) const override;
157 
158 private:
159     AutoPtr<ASTTypeAttr> attr_;
160     AutoPtr<ASTType> baseType_;
161     std::vector<AutoPtr<ASTEnumValue>> members_;
162 };
163 } // namespace HDI
164 } // namespace OHOS
165 
166 #endif // OHOS_HDI_ASTENUMTYPE_H