• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <vector>
13 #include "ast/ast_type.h"
14 #include "util/autoptr.h"
15 #include "util/string.h"
16 
17 namespace OHOS {
18 namespace HDI {
19 class ASTEnumValue : public LightRefCountBase {
20 public:
ASTEnumValue(const String & name)21     ASTEnumValue(const String& name)
22         :mName_(name), isDefault_(true), mValue_(0)
23     {}
24 
~ASTEnumValue()25     inline virtual ~ASTEnumValue() {}
26 
GetName()27     inline 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 
SetValue(unsigned long value)42     inline void SetValue(unsigned long value)
43     {
44         isDefault_ = false;
45         mValue_ = value;
46     }
47 
GetValue()48     inline unsigned long GetValue()
49     {
50         return mValue_;
51     }
52 
isDefaultValue()53     inline bool isDefaultValue()
54     {
55         return isDefault_;
56     }
57 
58 private:
59     String mName_;
60     AutoPtr<ASTType> mType_;
61     bool isDefault_;
62     unsigned long mValue_;
63 };
64 
65 class ASTEnumType : public ASTType {
66 public:
SetName(const String & name)67     inline void SetName(const String& name)
68     {
69         name_ = name;
70     }
71 
GetName()72     inline String GetName()
73     {
74         return name_;
75     }
76 
SetFull(bool full)77     inline void SetFull(bool full)
78     {
79         isFull_ = full;
80     }
81 
IsFull()82     inline bool IsFull()
83     {
84         return isFull_;
85     }
86 
SetLite(bool lite)87     inline void SetLite(bool lite)
88     {
89         isLite_ = lite;
90     }
91 
IsLite()92     inline bool IsLite()
93     {
94         return isLite_;
95     }
96 
SetDisplayBase(bool display)97     inline void SetDisplayBase(bool display)
98     {
99         isDisplayBase_ = display;
100     }
101 
IsDisplayBase()102     inline bool IsDisplayBase()
103     {
104         return isDisplayBase_;
105     }
106 
107     void SetBaseType(const AutoPtr<ASTType>& baseType);
108 
GetBaseType()109     inline AutoPtr<ASTType> GetBaseType()
110     {
111         return baseType_;
112     }
113 
114     void AddMember(const AutoPtr<ASTEnumValue>& member);
115 
GetMemberNumber()116     inline size_t GetMemberNumber()
117     {
118         return members_.size();
119     }
120 
GetMember(size_t index)121     inline AutoPtr<ASTEnumValue> GetMember(size_t index)
122     {
123         if (index >= members_.size()) {
124             return nullptr;
125         }
126         return members_[index];
127     }
128 
129     bool IsEnumType() override;
130 
131     String ToString() override;
132 
133     String Dump(const String& prefix) override;
134 
135     TypeKind GetTypeKind() override;
136 
137     String EmitCType(TypeMode mode = TypeMode::NO_MODE) const override;
138 
139     String EmitCppType(TypeMode mode = TypeMode::NO_MODE) const override;
140 
141     String EmitJavaType(TypeMode mode, bool isInnerType = false) const override;
142 
143     String EmitCTypeDecl() const;
144 
145     String EmitCppTypeDecl() const;
146 
147     String EmitJavaTypeDecl() const;
148 
149     void EmitCWriteVar(const String& parcelName, const String& name, const String& ecName,
150         const String& gotoLabel, StringBuilder& sb, const String& prefix) const override;
151 
152     void EmitCProxyReadVar(const String& parcelName, const String& name, bool isInnerType, const String& ecName,
153         const String& gotoLabel, StringBuilder& sb, const String& prefix) const override;
154 
155     void EmitCStubReadVar(const String& parcelName, const String& name, const String& ecName,
156         const String& gotoLabel, StringBuilder& sb, const String& prefix) const override;
157 
158     void EmitCppWriteVar(const String& parcelName, const String& name, StringBuilder& sb,
159         const String& prefix, unsigned int innerLevel = 0) const override;
160 
161     void EmitCppReadVar(const String& parcelName, const String& name, StringBuilder& sb,
162         const String& prefix, bool initVariable, unsigned int innerLevel = 0) const override;
163 
164     void EmitCMarshalling(const String& name, StringBuilder& sb, const String& prefix) const override;
165 
166     void EmitCUnMarshalling(const String& name, const String& gotoLabel, StringBuilder& sb, const String& prefix,
167         std::vector<String>& freeObjStatements) const override;
168 
169     void EmitCppMarshalling(const String& parcelName, const String& name, StringBuilder& sb,
170         const String& prefix, unsigned int innerLevel = 0) const override;
171 
172     void EmitCppUnMarshalling(const String& parcelName, const String& name, StringBuilder& sb,
173         const String& prefix, bool emitType, unsigned int innerLevel = 0) const override;
174 private:
175     bool isFull_ = false;
176     bool isLite_ = false;
177     bool isDisplayBase_ = false;
178     AutoPtr<ASTType> baseType_ = nullptr;
179 
180     std::vector<AutoPtr<ASTEnumValue>> members_;
181 };
182 } // namespace HDI
183 } // namespace OHOS
184 
185 #endif // OHOS_HDI_ASTENUMTYPE_H