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_ASTTYPE_H
17 #define OHOS_IDL_ASTTYPE_H
18 
19 #include <functional>
20 #include <regex>
21 #include <unordered_map>
22 
23 #include "ast/ast_namespace.h"
24 #include "ast/ast_node.h"
25 #include "util/autoptr.h"
26 #include "util/options.h"
27 #include "util/string_builder.h"
28 
29 namespace OHOS {
30 namespace Idl {
31 enum class TypeKind {
32     TYPE_UNKNOWN = 0,
33     TYPE_VOID,
34     TYPE_BOOLEAN,
35     TYPE_BYTE,
36     TYPE_SHORT,
37     TYPE_INT,
38     TYPE_LONG,
39     TYPE_FLOAT,
40     TYPE_DOUBLE,
41     TYPE_CHAR,
42     TYPE_UCHAR,
43     TYPE_USHORT,
44     TYPE_UINT,
45     TYPE_ULONG,
46     TYPE_STRING,
47     TYPE_STRING16,
48     TYPE_FILEDESCRIPTOR,
49     TYPE_SEQUENCEABLE,
50     TYPE_INTERFACE,
51     TYPE_LIST,
52     TYPE_MAP,
53     TYPE_ARRAY,
54     TYPE_ENUM,
55     TYPE_STRUCT,
56     TYPE_UNION,
57     TYPE_SMQ,
58     TYPE_ASHMEM,
59     TYPE_NATIVE_BUFFER,
60     TYPE_POINTER,
61 };
62 
63 enum class TypeMode {
64     NO_MODE,   // only type
65     PARAM_IN,  // type of the in attribute parameter
66     PARAM_OUT, // type of the out attribute parameter
67     PARAM_INOUT, // type of the inout attribute parameter
68     LOCAL_VAR, // type of the local variable
69 };
70 
71 enum class SerMode {
72     PROXY_SER,  // the flag of proxy serialized
73     STUB_SER,   // the flag of stub serialized
74     CUSTOM_SER, // the flag of custom types serialized
75 };
76 
77 class ASTType : public ASTNode {
78 public:
79     explicit ASTType(bool isPod = true, TypeKind typekind = TypeKind::TYPE_UNKNOWN)
80         :isPod_(isPod), typeKind_(typekind), name_(), namespace_()
81     {
82     }
83 
84     virtual void SetName(const std::string &name);
85 
86     virtual std::string GetName();
87 
88     virtual void SetNamespace(const AutoPtr<ASTNamespace> &nspace);
89 
90     virtual AutoPtr<ASTNamespace> GetNamespace();
91 
92     virtual std::string GetSignature() = 0;
93 
94     virtual bool IsVoidType();
95 
96     virtual bool IsBooleanType();
97 
98     virtual bool IsByteType();
99 
100     virtual bool IsShortType();
101 
102     virtual bool IsIntegerType();
103 
104     virtual bool IsLongType();
105 
106     virtual bool IsCharType();
107 
108     virtual bool IsUcharType();
109 
110     virtual bool IsUshortType();
111 
112     virtual bool IsUintType();
113 
114     virtual bool IsUlongType();
115 
116     virtual bool IsFloatType();
117 
118     virtual bool IsDoubleType();
119 
120     virtual bool IsStringType();
121 
122     virtual bool IsListType();
123 
124     virtual bool IsMapType();
125 
126     virtual bool IsEnumType();
127 
128     virtual bool IsStructType();
129 
130     virtual bool IsUnionType();
131 
132     virtual bool IsInterfaceType();
133 
134     virtual bool IsSequenceableType();
135 
136     virtual bool IsArrayType();
137 
138     virtual bool IsFdType();
139 
140     virtual bool IsSmqType();
141 
142     virtual bool IsAshmemType();
143 
144     virtual bool IsNativeBufferType();
145 
146     virtual bool IsPointerType();
147 
148     bool IsPod() const;
149 
150     bool IsTypeKind(TypeKind typekind) const;
151 
152     virtual bool HasInnerType(TypeKind innerType) const;
153 
154     virtual std::string ToShortString();
155 
156     std::string ToString() const override;
157 
158     virtual TypeKind GetTypeKind();
159 protected:
160     bool isPod_;
161     TypeKind typeKind_;
162     std::string name_;
163     AutoPtr<ASTNamespace> namespace_;
164 };
165 } // namespace Idl
166 } // namespace OHOS
167 
168 #endif // OHOS_IDL_ASTTYPE_H