1 /*
2  * Copyright (c) 2022-2023 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_CPPCODEEMITTER_H
17 #define OHOS_IDL_CPPCODEEMITTER_H
18 
19 #include <string>
20 
21 #include "codegen/code_emitter.h"
22 #include "util/string_builder.h"
23 
24 namespace OHOS {
25 namespace Idl {
26 class CppCodeEmitter : public CodeEmitter {
27 public:
CppCodeEmitter(MetaComponent * mc)28     CppCodeEmitter(MetaComponent* mc)
29         : CodeEmitter(mc)
30     {}
31 
32     void EmitInterface() override;
33 
34     void EmitInterfaceProxy() override;
35 
36     void EmitInterfaceStub() override;
37 
38 private:
39     void EmitInterfaceHeaderFile();
40 
41     void EmitInterfaceInclusions(StringBuilder& sb);
42 
43     void EmitInterfaceStdlibInclusions(StringBuilder& sb);
44 
45     void EmitInterfaceDBinderInclusions(StringBuilder& sb);
46 
47     void EmitInterfaceSelfDefinedTypeInclusions(StringBuilder& sb);
48 
49     bool EmitInterfaceUsings(StringBuilder& sb);
50 
51     void EmitInterfaceSelfDefinedTypeUsings(StringBuilder& sb);
52 
53     void EmitInterfaceDefinition(StringBuilder& sb);
54 
55     void EmitInterfaceBody(StringBuilder& sb, const String& prefix);
56 
57     void EmitInterfaceMemberVariables(StringBuilder& sb, const String& prefix);
58 
59     void EmitInterfaceMethods(StringBuilder& sb, const String& prefix);
60 
61     void EmitInterfaceMethod(MetaMethod* mm, StringBuilder& sb, const String& prefix);
62 
63     void EmitInterfaceMethodParameter(MetaParameter* mp, StringBuilder& sb, const String& prefix);
64 
65     void EmitInterfaceMethodReturn(MetaType* mt, StringBuilder& sb, const String& prefix);
66 
67     void EmitInterfaceProxyHeaderFile();
68 
69     void EmitInterfaceProxyInHeaderFile(StringBuilder& sb);
70 
71     void EmitInterfaceProxyAddCacheAbleAPI(StringBuilder& sb, const String& prefix);
72 
73     void EmitInterfaceProxyConstructor(StringBuilder& sb, const String& prefix);
74 
75     void EmitInterfaceProxyDeathRecipient(StringBuilder& sb, const String& prefix);
76 
77     void EmitInterfaceProxyRegisterDeathRecipient(StringBuilder& sb, const String& prefix);
78 
79     void EmitInterfaceProxyUnRegisterDeathRecipient(StringBuilder& sb, const String& prefix);
80 
81     void EmitInterfaceProxyMethodDecls(StringBuilder& sb, const String& prefix);
82 
83     void EmitInterfaceProxyMethodDecl(MetaMethod* mm, StringBuilder& sb, const String& prefix);
84 
85     void EmitInterfaceProxyConstants(StringBuilder& sb, const String& prefix);
86 
87     void EmitInterfaceProxyCppFile();
88 
89     void EmitInterfaceProxyMethodImpls(StringBuilder& sb, const String& prefix);
90 
91     void EmitInterfaceProxyMethodPreSendRequest(MetaMethod* mm, StringBuilder& sb, const String& prefix);
92 
93     void EmitInterfaceProxyMethodPostSendRequest(MetaMethod* mm, StringBuilder& sb, const String& prefix);
94 
95     void EmitInterfaceProxyMethodImpl(MetaMethod* mm, StringBuilder& sb, const String& prefix);
96 
97     void EmitInterfaceProxyMethodBody(MetaMethod* mm, StringBuilder& sb, const String& prefix);
98 
99     void EmitInterfaceProxyMethodErrCode(StringBuilder& sb, const String& prefix);
100 
101     void EmitInterfaceProxyMethodReply(MetaMethod* mm, StringBuilder& sb, const String& prefix);
102 
103     void EmitInterfaceProxyMethodRetValue(MetaMethod* mm, StringBuilder& sb, const String& prefix);
104 
105     void EmitWriteMethodParameter(MetaParameter* mp, const String& parcelName, StringBuilder& sb,
106         const String& prefix);
107 
108     void EmitReadMethodParameter(MetaParameter* mp, const String& parcelName, StringBuilder& sb, const String& prefix);
109 
110     void EmitInterfaceStubHeaderFile();
111 
112     void EmitInterfaceStubInHeaderFile(StringBuilder& sb);
113 
114     void EmitInterfaceStubMethodDecls(StringBuilder& sb, const String& prefix);
115 
116     void EmitInterfaceStubConstants(StringBuilder& sb, const String& prefix);
117 
118     void EmitInterfaceStubCppFile();
119 
120     void EmitInterfaceStubMethodImpls(StringBuilder& sb, const String& prefix);
121 
122     void EmitInterfaceStubMethodImplReturn(MetaMethod* mm, StringBuilder& sb, const String& prefix,
123         MetaType* returnType);
124 
125     void EmitInterfaceStubMethodImpl(MetaMethod* mm, StringBuilder& sb, const String& prefix);
126 
127     void EmitInterfaceMethodCommands(StringBuilder& sb, const String& prefix);
128 
129     void EmitLicense(StringBuilder& sb);
130 
131     void EmitHeadMacro(StringBuilder& sb, const String& fullName);
132 
133     void EmitTailMacro(StringBuilder& sb, const String& fullName);
134 
135     void EmitBeginNamespace(StringBuilder& sb);
136 
137     void EmitEndNamespace(StringBuilder& sb);
138 
139     void EmitWriteVariable(const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb,
140         const String& prefix);
141 
142     void EmitWriteVariableFloat(const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb,
143         const String& prefix);
144 
145     void EmitWriteVariableComplex(const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb,
146         const String& prefix);
147 
148     void EmitWriteVariableObject(const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb,
149         const String& prefix);
150 
151     void EmitReadVariable(const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb,
152         const String& prefix, bool emitType = true);
153 
154     void EmitReadVariableFloat(const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb,
155         const String& prefix, bool emitType = true);
156 
157     void EmitReadVariableComplex(const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb,
158         const String& prefix, bool emitType = true);
159 
160     void EmitReadVariableList(const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb,
161         const String& prefix, bool emitType = true);
162 
163     void EmitReadVariableObject(const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb,
164         const String& prefix, bool emitType = true);
165 
166     void EmitLocalVariable(MetaParameter* mp, StringBuilder& sb, const String& prefix);
167 
168     void EmitReturnParameter(const String& name, MetaType* mt, StringBuilder& sb);
169 
170     String EmitType(MetaType* mt, unsigned int attributes, bool isInnerType);
171 
172     String EmitFloatType(MetaType* mt, unsigned int attributes, bool isInnerType);
173 
174     String EmitComplexType(MetaType* mt, unsigned int attributes, bool isInnerType);
175 
176     String EmitListType(MetaType* mt, unsigned int attributes, bool isInnerType);
177 
178     String EmitObjectType(MetaType* mt, unsigned int attributes, bool isInnerType);
179 
180     String FileName(const String& name);
181 
182     String GetFilePath(const String& fpnp);
183 
184     String GetFilePathNoPoint(const String& fpnp);
185 
186     String GetNamespace(const String& fpnp);
187 
188     String MacroName(const String& name);
189 
190     String CppFullName(const String& name);
191 
192     String ConstantName(const String& name);
193 
194     const std::string UnderlineAdded(const String& name);
195 
196     int circleCount = 0;
197 };
198 } // namespace Idl
199 } // namespace OHOS
200 #endif // OHOS_IDL_CPPCODEEMITTER_H
201