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_HDI_TYPE_EMITTER_H
17 #define OHOS_IDL_HDI_TYPE_EMITTER_H
18 
19 #include <map>
20 #include "ast/ast_type.h"
21 
22 namespace OHOS {
23 namespace Idl {
24 constexpr const char *MAX_BUFF_SIZE_MACRO = "HDI_BUFF_MAX_SIZE";
25 constexpr const char *MAX_BUFF_SIZE_VALUE = "1024 * 200";    // 200KB
26 constexpr const char *CHECK_VALUE_RETURN_MACRO = "HDI_CHECK_VALUE_RETURN";
27 constexpr const char *CHECK_VALUE_RET_GOTO_MACRO = "HDI_CHECK_VALUE_RET_GOTO";
28 
29 using UtilMethodMap = std::unordered_map<std::string, std::string>;
30 class HdiTypeEmitter : public LightRefCountBase {
31 public:
32     bool IsPod() const;
33 
34     void SetPod(const bool &isPod);
35 
36     virtual TypeKind GetTypeKind();
37 
38     void SetTypeName(const std::string &name);
39 
40     std::string GetTypeName();
41 
42     void SetName(const std::string &name);
43 
44     std::string GetName();
45 
46     virtual std::string EmitCppType(TypeMode mode = TypeMode::NO_MODE) const;
47 
48     virtual std::string EmitCType(TypeMode mode = TypeMode::NO_MODE) const;
49 
50     virtual std::string EmitJavaType(TypeMode mode, bool isInnerType = false) const;
51 
52     virtual std::string EmitCTypeDecl() const;
53 
54     virtual std::string EmitCppTypeDecl() const;
55 
56     virtual std::string EmitJavaTypeDecl() const;
57 
58     virtual void EmitCWriteVar(TypeMode mode, const std::string &name, const std::string &gotoLabel,
59         StringBuilder &sb, const std::string &prefix) const;
60 
61     virtual void EmitCProxyWriteOutVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
62         const std::string &prefix) const;
63 
64     virtual void EmitCProxyReadVar(const std::string &name, bool isInnerType, const std::string &gotoLabel,
65         StringBuilder &sb, const std::string &prefix) const;
66 
67     virtual void EmitCStubReadVar(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
68         const std::string &prefix) const;
69 
70     virtual void EmitCStubReadOutVar(const std::string &memFlagName, const std::string &name,
71         const std::string &gotoLabel, StringBuilder &sb, const std::string &prefix) const;
72 
73     virtual void EmitCppWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
74         const std::string &prefix, unsigned int innerLevel = 0) const;
75 
76     virtual void EmitCppReadVar(const std::string &name, StringBuilder &sb, const std::string &prefix,
77         TypeMode mode, unsigned int innerLevel = 0) const;
78 
79     virtual void EmitCMarshalling(const std::string &name, StringBuilder &sb, const std::string &prefix) const;
80 
81     virtual void EmitCUnMarshalling(const std::string &name, const std::string &gotoLabel, StringBuilder &sb,
82         const std::string &prefix, std::vector<std::string> &freeObjStatements) const;
83 
84     virtual void EmitCWriteMethods(UtilMethodMap &methods, const std::string &prefix,
85         const std::string &methodPrefix, bool isDecl) const;
86 
87     virtual void EmitCppWriteMethods(UtilMethodMap &methods, const std::string &prefix,
88         const std::string &methodPrefix, bool isDecl) const;
89 
90     virtual void EmitCReadMethods(UtilMethodMap &methods, const std::string &prefix,
91         const std::string &methodPrefix, bool isDecl) const;
92 
93     virtual void EmitCppReadMethods(UtilMethodMap &methods, const std::string &prefix,
94         const std::string &methodPrefix, bool isDecl) const;
95 
96     virtual void EmitCStubReadMethods(UtilMethodMap &methods, const std::string &prefix,
97         const std::string &methodPrefix, bool isDecl) const;
98 
99     void EmitFreeStatements(
100         const std::vector<std::string> &freeObjStatements, StringBuilder &sb, const std::string &prefix) const;
101 
102     virtual void EmitCppMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
103         const std::string &prefix, unsigned int innerLevel = 0) const;
104 
105     virtual void EmitCppUnMarshalling(const std::string &parcelName, const std::string &name, StringBuilder &sb,
106         const std::string &prefix, unsigned int innerLevel = 0) const;
107 
108     virtual void EmitMemoryRecycle(
109         const std::string &name, bool ownership, StringBuilder &sb, const std::string &prefix) const;
110 
111     virtual void EmitJavaWriteVar(const std::string &parcelName, const std::string &name, StringBuilder &sb,
112         const std::string &prefix, TypeMode mode = TypeMode::NO_MODE) const;
113 
114     virtual void EmitJavaReadVar(
115         const std::string &parcelName, const std::string &name, StringBuilder &sb, const std::string &prefix) const;
116 
117     virtual void EmitJavaReadInnerVar(const std::string &parcelName, const std::string &name, bool isInner,
118         StringBuilder &sb, const std::string &prefix) const;
119 
120     static std::string dataParcelName_;
121     static std::string replyParcelName_;
122     static std::string errorCodeName_;
123 
124 protected:
125     bool isPod_ = false;
126     std::string typeName_;
127     std::string name_;
128 };
129 } // namespace Idl
130 } // namespace OHOS
131 
132 #endif // OHOS_IDL_HDI_TYPE_EMITTER_H