• 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_ASTTYPE_H
10 #define OHOS_HDI_ASTTYPE_H
11 
12 #include <functional>
13 #include <unordered_map>
14 
15 #include "ast/ast_namespace.h"
16 #include "ast/ast_node.h"
17 #include "util/autoptr.h"
18 #include "util/options.h"
19 #include "util/string_builder.h"
20 
21 namespace OHOS {
22 namespace HDI {
23 enum class TypeKind {
24     TYPE_UNKNOWN = 0,
25     TYPE_BOOLEAN,
26     TYPE_BYTE,
27     TYPE_SHORT,
28     TYPE_INT,
29     TYPE_LONG,
30     TYPE_FLOAT,
31     TYPE_DOUBLE,
32     TYPE_UCHAR,
33     TYPE_USHORT,
34     TYPE_UINT,
35     TYPE_ULONG,
36     TYPE_STRING,
37     TYPE_FILEDESCRIPTOR,
38     TYPE_VOID,
39     TYPE_SEQUENCEABLE,
40     TYPE_INTERFACE,
41     TYPE_LIST,
42     TYPE_MAP,
43     TYPE_ARRAY,
44     TYPE_ENUM,
45     TYPE_STRUCT,
46     TYPE_UNION,
47     TYPE_SMQ,
48     TYPE_ASHMEM,
49     TYPE_BUFFER_HANDLE,
50 };
51 
52 enum class TypeMode {
53     NO_MODE,   // only type
54     PARAM_IN,  // type of the in attribute parameter
55     PARAM_OUT, // type of the out attribute parameter
56     LOCAL_VAR, // type of the local variable
57 };
58 
59 enum class SerMode {
60     PROXY_SER,  // the flag of proxy serialized
61     STUB_SER,   // the flag of stub serialized
62     CUSTOM_SER, // the flag of custom types serialized
63 };
64 
65 using UtilMethod = std::function<void(StringBuilder &, const std::string &, const std::string &, bool)>;
66 using UtilMethodMap = std::unordered_map<std::string, UtilMethod>;
67 
68 class ASTType : public ASTNode {
69 public:
70     ASTType(TypeKind kind = TypeKind::TYPE_UNKNOWN, bool isPod = true)
typeKind_(kind)71         :typeKind_(kind), isPod_(isPod), name_(), namespace_()
72     {
73     }
74 
75     virtual void SetName(const std::string &name);
76 
77     virtual std::string GetName();
78 
79     virtual void SetNamespace(const AutoPtr<ASTNamespace> &nspace);
80 
81     virtual AutoPtr<ASTNamespace> GetNamespace();
82 
83     virtual bool IsBooleanType();
84 
85     virtual bool IsByteType();
86 
87     virtual bool IsShortType();
88 
89     virtual bool IsIntegerType();
90 
91     virtual bool IsLongType();
92 
93     virtual bool IsUcharType();
94 
95     virtual bool IsUshortType();
96 
97     virtual bool IsUintType();
98 
99     virtual bool IsUlongType();
100 
101     virtual bool IsFloatType();
102 
103     virtual bool IsDoubleType();
104 
105     virtual bool IsStringType();
106 
107     virtual bool IsListType();
108 
109     virtual bool IsMapType();
110 
111     virtual bool IsEnumType();
112 
113     virtual bool IsStructType();
114 
115     virtual bool IsUnionType();
116 
117     virtual bool IsInterfaceType();
118 
119     virtual bool IsSequenceableType();
120 
121     virtual bool IsVoidType();
122 
123     virtual bool IsArrayType();
124 
125     virtual bool IsFdType();
126 
127     virtual bool IsSmqType();
128 
129     virtual bool IsAshmemType();
130 
131     virtual bool IsBufferHandleType();
132 
133     bool IsPod() const;
134 
135     virtual bool HasInnerType(TypeKind innerType) const;
136 
137     virtual std::string ToShortString();
138 
139     std::string ToString() const override;
140 
141     virtual TypeKind GetTypeKind();
142 
143     virtual std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const;
144 
145     virtual std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const;
146 
147     virtual std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const;
148 
149     virtual void EmitCWriteVar(const std::string &parcelName, const std::string &name, const std::string &ecName,
150         const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const;
151 
152     virtual void EmitCProxyWriteOutVar(const std::string &parcelName, const std::string &name,
153         const std::string &ecName, const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const;
154 
155     virtual void EmitCProxyReadVar(const std::string &parcelName, const std::string &name, bool isInnerType,
156         const std::string &ecName, const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const;
157 
158     virtual void EmitCStubReadVar(const std::string &parcelName, const std::string &name, const std::string &ecName,
159         const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const;
160 
161     virtual void EmitCStubReadOutVar(const std::string &buffSizeName, const std::string &memFlagName,
162         const std::string &parcelName, const std::string &name, const std::string &ecName, const std::string &gotoLabel,
163         StringBuilder &sb, const std::string &prefix) const;
164 
165     virtual void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
166         const std::string &prefix, unsigned int innerLevel = 0) const;
167 
168     virtual void EmitCppReadVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
169         const std::string &prefix, bool initVariable, unsigned int innerLevel = 0) const;
170 
171     virtual void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const;
172 
173     virtual void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
174         const std::string &prefix, std::vector<std::string> &freeObjStatements) const;
175 
176     void EmitFreeStatements(
177         const std::vector<std::string> &freeObjStatements, StringBuilder &sb, const std::string &prefix) const;
178 
179     virtual void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
180         const std::string &prefix, unsigned int innerLevel = 0) const;
181 
182     virtual void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
183         const std::string &prefix, bool emitType, unsigned int innerLevel = 0) const;
184 
185     virtual void EmitMemoryRecycle(
186         const std::string &name, bool isClient, bool ownership, StringBuilder &sb, const std::string &prefix) const;
187 
188     virtual void EmitJavaWriteVar(
189         const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const;
190 
191     virtual void EmitJavaReadVar(
192         const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const;
193 
194     virtual void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner,
195         StringBuilder &sb, const std::string &prefix) const;
196 
197     virtual void RegisterWriteMethod(Options::Language language, SerMode mode, UtilMethodMap &methods) const;
198 
199     virtual void RegisterReadMethod(Options::Language language, SerMode mode, UtilMethodMap &methods) const;
200 
201 protected:
202     TypeKind typeKind_;
203     bool isPod_;
204     std::string name_;
205     AutoPtr<ASTNamespace> namespace_;
206 };
207 } // namespace HDI
208 } // namespace OHOS
209 
210 #endif // OHOS_HDI_ASTTYPE_H