1 /* 2 * Copyright (c) 2022 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 EmitInterfaceMethods(StringBuilder& sb, const String& prefix); 58 59 void EmitInterfaceMethod(MetaMethod* mm, StringBuilder& sb, const String& prefix); 60 61 void EmitInterfaceMethodParameter(MetaParameter* mp, StringBuilder& sb, const String& prefix); 62 63 void EmitInterfaceMethodReturn(MetaType* mt, StringBuilder& sb, const String& prefix); 64 65 void EmitInterfaceProxyHeaderFile(); 66 67 void EmitInterfaceProxyInHeaderFile(StringBuilder& sb); 68 69 void EmitInterfaceProxyConstructor(StringBuilder& sb, const String& prefix); 70 71 void EmitInterfaceProxyMethodDecls(StringBuilder& sb, const String& prefix); 72 73 void EmitInterfaceProxyMethodDecl(MetaMethod* mm, StringBuilder& sb, const String& prefix); 74 75 void EmitInterfaceProxyConstants(StringBuilder& sb, const String& prefix); 76 77 void EmitInterfaceProxyCppFile(); 78 79 void EmitInterfaceProxyMethodImpls(StringBuilder& sb, const String& prefix); 80 81 void EmitInterfaceProxyMethodImpl(MetaMethod* mm, StringBuilder& sb, const String& prefix); 82 83 void EmitInterfaceProxyMethodBody(MetaMethod* mm, StringBuilder& sb, const String& prefix); 84 85 void EmitWriteMethodParameter(MetaParameter* mp, const String& parcelName, StringBuilder& sb, 86 const String& prefix); 87 88 void EmitReadMethodParameter(MetaParameter* mp, const String& parcelName, StringBuilder& sb, const String& prefix); 89 90 void EmitInterfaceStubHeaderFile(); 91 92 void EmitInterfaceStubInHeaderFile(StringBuilder& sb); 93 94 void EmitInterfaceStubMethodDecls(StringBuilder& sb, const String& prefix); 95 96 void EmitInterfaceStubConstants(StringBuilder& sb, const String& prefix); 97 98 void EmitInterfaceStubCppFile(); 99 100 void EmitInterfaceStubMethodImpls(StringBuilder& sb, const String& prefix); 101 102 void EmitInterfaceStubMethodImpl(MetaMethod* mm, StringBuilder& sb, const String& prefix); 103 104 void EmitInterfaceMethodCommands(StringBuilder& sb, const String& prefix); 105 106 void EmitLicense(StringBuilder& sb); 107 108 void EmitHeadMacro(StringBuilder& sb, const String& fullName); 109 110 void EmitTailMacro(StringBuilder& sb, const String& fullName); 111 112 void EmitBeginNamespace(StringBuilder& sb); 113 114 void EmitEndNamespace(StringBuilder& sb); 115 116 void EmitWriteVariable(const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb, 117 const String& prefix); 118 119 void EmitReadVariable(const String& parcelName, const std::string& name, MetaType* mt, StringBuilder& sb, 120 const String& prefix, bool emitType = true); 121 122 void EmitLocalVariable(MetaParameter* mp, StringBuilder& sb, const String& prefix); 123 124 void EmitReturnParameter(const String& name, MetaType* mt, StringBuilder& sb); 125 126 String EmitType(MetaType* mt, unsigned int attributes, bool isInnerType); 127 128 String FileName(const String& name); 129 130 String GetFilePath(const String& fpnp); 131 132 String GetNamespace(const String& fpnp); 133 134 String MacroName(const String& name); 135 136 String CppFullName(const String& name); 137 138 String ConstantName(const String& name); 139 140 const std::string UnderlineAdded(const String& name); 141 }; 142 } // namespace Idl 143 } // namespace OHOS 144 #endif // OHOS_IDL_CPPCODEEMITTER_H 145