code_generator.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef _CODEGENERATOR_H
  17. #define _CODEGENERATOR_H
  18. #include "parse.h"
  19. #include "util/tc_file.h"
  20. #include "util/tc_encoder.h"
  21. #include <set>
  22. #ifndef TAB
  23. #define TAB g_parse->getTab()
  24. #endif
  25. #ifndef INC_TAB
  26. #define INC_TAB g_parse->incTab()
  27. #endif
  28. #ifndef DEL_TAB
  29. #define DEL_TAB g_parse->delTab()
  30. #endif
  31. #define TO_LOWER_STRING(str) TC_Common::lower(str)
  32. #define DEFINE_STRING(str) string(CSTR(str))
  33. #define CSTR(str) #str
  34. #define IDL_NAMESPACE_STR DEFINE_STRING(IDL_NAMESPACE)
  35. #define GET_CONST_GRAMMAR_PTR_V(name, ptr) \
  36. ptr->getConst##name##Ptr()
  37. #define GET_CONST_GRAMMAR_PTR_BASE(name, ptr) \
  38. GET_CONST_GRAMMAR_PTR_V(name, ptr)
  39. #define GET_CONST_GRAMMAR_PTR(ptr) \
  40. GET_CONST_GRAMMAR_PTR_BASE(GRAMMAR_NAME, ptr)
  41. #define CONST_GRAMMAR_V(name, val) \
  42. Const##name::val
  43. #define CONST_GRAMMAR_BASE(name, val) \
  44. CONST_GRAMMAR_V(name, val)
  45. #define CONST_GRAMMAR(val) \
  46. CONST_GRAMMAR_BASE(GRAMMAR_NAME, val)
  47. #define PROTOCOL_V(space, protocol, type) \
  48. space + "Stream." + protocol + "." + TC_Common::upper(protocol) + "_" + type
  49. #define PROTOCOL_SIMPLE PROTOCOL_V(IDL_NAMESPACE_STR, PROTOCOL_NAME, "SIMPLE")
  50. #define PROTOCOL_COMPLEX PROTOCOL_V(IDL_NAMESPACE_STR, PROTOCOL_NAME, "COMPLEX")
  51. #define PROTOCOL_VAR TO_LOWER_STRING(PROTOCOL_NAME)
  52. #define PROTOCOL_VJ(space, protocol, type) \
  53. space + "Stream." + protocol + "." + type + "_VERSION"
  54. #define PROTOCOL_JSON PROTOCOL_VJ(IDL_NAMESPACE_STR, PROTOCOL_NAME, "JSON")
  55. #define DISABLE_ESLINT "/* eslint-disable */"
  56. #define DISABLE_TSLINT "/* tslint:disable */"
  57. using namespace TC_NAMESPACE;
  58. class CodeGenerator
  59. {
  60. public:
  61. CodeGenerator()
  62. : uiNameIndex(0),
  63. _sRpcPath(RPC_MODULE_PATH),
  64. _sStreamPath(STREAM_MODULE_PATH),
  65. _sToPath("./"),
  66. _bClient(false),
  67. _bServer(false),
  68. _bRecursive(false),
  69. _bUseSpecialPath(false),
  70. _iLongType(Number),
  71. _bStringBinaryEncoding(false),
  72. _bEnumReverseMappings(false),
  73. _bMinimalMembers(false),
  74. _bTS(false),
  75. _bDTS(false),
  76. _iOptimizeLevel(O0) {}
  77. void createFile(const string &file, const bool bEntry = true);
  78. void setRpcPath(const string & sPath) { _sRpcPath = sPath; }
  79. void setStreamPath(const string & sPath) { _sStreamPath = sPath; }
  80. void setEnableClient(bool bEnable) { _bClient = bEnable; }
  81. void setEnableServer(bool bEnable) { _bServer = bEnable; }
  82. void setTargetPath(const string & sPath) { _sToPath = sPath + "/"; }
  83. void setRecursive(bool bEnable) { _bRecursive = bEnable; }
  84. void setUseSpecialPath(bool bEnable) { _bUseSpecialPath = bEnable; }
  85. void setLongType(int iLongType) { _iLongType = iLongType; }
  86. void setStringBinaryEncoding(bool bEnable) { _bStringBinaryEncoding = bEnable; }
  87. void setEnumReverseMappings(bool bEnable) { _bEnumReverseMappings = bEnable; }
  88. void setMinimalMembers(bool bEnable) { _bMinimalMembers = bEnable; }
  89. void setDependent(set<string>& deps) { _depMembers = deps; }
  90. void setEnableTS(bool bEnable) { _bTS = bEnable; }
  91. void setEnableDTS(bool bEnable) { _bDTS = bEnable; }
  92. void setOptimize(int iLevel) { _iOptimizeLevel = iLevel; }
  93. enum OPTIMIZE_LEVEL {O0 = 0, Os};
  94. enum LONG_TYPE {Number = 0, String, BigInt};
  95. private:
  96. struct ImportFileType
  97. {
  98. enum TYPE_T {EN_ENUM = 10000, EN_ENUM_VALUE, EN_STRUCT};
  99. int iType;
  100. string sNamespace;
  101. string sTypeName;
  102. string sName;
  103. };
  104. struct ImportFile
  105. {
  106. string sFile;
  107. string sModule;
  108. map<string, ImportFileType> mapVars;
  109. };
  110. uint32_t uiNameIndex;
  111. map<string, ImportFile> _mapFiles;
  112. set<string> _depMembers;
  113. void scan(const string & sFile, bool bNotPrefix);
  114. string makeName();
  115. string findName(const string & sNamespace, const string & sName, const bool &bBase = false);
  116. private:
  117. string toFunctionName(const TypeIdPtr & pPtr, const string &sAction);
  118. string toObjectString(const TypeIdPtr &pPtr);
  119. string getDataType(const TypePtr& pPtr, const bool &bCastEnumAsAny = false);
  120. string getClassName(const TypePtr& pPtr);
  121. string getTsType(const TypePtr &pPtr, const bool bStream = true, const bool bBase = false);
  122. string getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string & sNamespace, const bool &bGlobal = true, const bool &bCastEnumAsAny = false);
  123. private:
  124. string generateJS(const StructPtr &pPtr, const string &sNamespace, bool &bNeedAssert, bool &bQuickFunc);
  125. string generateJS(const ConstPtr &pPtr, const string &sNamespace, bool &bNeedStream);
  126. string generateJS(const EnumPtr &pPtr, const string &sNamespace);
  127. string generateJS(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedAssert, bool &bQuickFunc);
  128. bool generateJS(const ContextPtr &pPtr);
  129. private:
  130. string generateTS(const StructPtr &pPtr, const string &sNamespace, bool &bNeedAssert, bool &bQuickFunc);
  131. string generateTS(const ConstPtr &pPtr, const string &sNamespace, bool &bNeedStream);
  132. string generateTS(const EnumPtr &pPtr, const string &sNamespace);
  133. string generateTS(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedAssert, bool &bQuickFunc);
  134. string generateTS(const NamespacePtr &pPtr, const string &sContent);
  135. void generateTS(const ContextPtr &cPtr);
  136. private:
  137. string generateJSProxy(const NamespacePtr &nPtr, bool &bNeedRpc, bool &bNeedStream);
  138. string generateJSProxy(const NamespacePtr &nPtr, const InterfacePtr &pPtr);
  139. string generateJSProxy(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr);
  140. bool generateJSProxy(const ContextPtr &pPtr);
  141. private:
  142. string generateTSProxy(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedRpc);
  143. string generateTSProxy(const NamespacePtr &nPtr, const InterfacePtr &pPtr);
  144. string generateTSProxy(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr);
  145. bool generateTSProxy(const ContextPtr &pPtr);
  146. private:
  147. string generateJSServer(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedRpc, bool &bNeedAssert);
  148. string generatePing(const NamespacePtr &nPtr, const InterfacePtr &pPtr);
  149. string generateAsync(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr);
  150. string generateDispatch(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr);
  151. string generateJSServer(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr);
  152. string generateJSServer(const InterfacePtr &pPtr, const NamespacePtr &nPtr);
  153. bool generateJSServer(const ContextPtr &pPtr);
  154. private:
  155. string generateTSServerAsync(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr);
  156. string generateTSServerDispatch(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr);
  157. string generateTSServer(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedRpc, bool &bNeedAssert);
  158. string generateTSServer(const InterfacePtr &pPtr, const NamespacePtr &nPtr);
  159. bool generateTSServer(const ContextPtr &pPtr);
  160. private:
  161. string generateJSServerImp(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr);
  162. string generateJSServerImp(const NamespacePtr &nPtr, const InterfacePtr &pPtr);
  163. string generateJSServerImp(const ContextPtr &cPtr, const NamespacePtr &nPtr);
  164. void generateJSServerImp(const ContextPtr &cPtr);
  165. private:
  166. void generateTSServerImp(const ContextPtr &cPtr);
  167. private:
  168. string generateDTS(const StructPtr &pPtr, const string &sNamespace);
  169. string generateDTS(const ConstPtr &pPtr, const string &sNamespace, bool &bNeedStream);
  170. string generateDTS(const EnumPtr &pPtr, const string &sNamespace);
  171. string generateDTS(const NamespacePtr &pPtr, bool &bNeedStream);
  172. string generateDTS(const NamespacePtr &pPtr, const string &sContent);
  173. void generateDTS(const ContextPtr &cPtr);
  174. private:
  175. string generateDTSServer(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedRpc);
  176. string generateDTSServer(const NamespacePtr &nPtr, const InterfacePtr &pPtr);
  177. void generateDTSServer(const ContextPtr &cPtr);
  178. private:
  179. string generateDTSProxy(const InterfacePtr &pPtr);
  180. string generateDTSProxy(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedRpc);
  181. void generateDTSProxy(const ContextPtr &pPtr);
  182. private:
  183. void makeUTF8File(const string & sFileName, const string & sFileContent);
  184. bool isSimple(const TypePtr & pPtr) const;
  185. bool isBinBuffer(const TypePtr & pPtr) const;
  186. string representArgument(const TypePtr& pPtr) const;
  187. bool isDependent(const string & sNamespace, const string & sName) const;
  188. string getRealFileInfo(const string & sPath);
  189. string printHeaderRemark(const string & sTypeName);
  190. private:
  191. string _sRpcPath;
  192. string _sStreamPath;
  193. string _sToPath;
  194. bool _bClient;
  195. bool _bServer;
  196. bool _bRecursive;
  197. bool _bUseSpecialPath;
  198. int _iLongType;
  199. bool _bStringBinaryEncoding;
  200. bool _bEnumReverseMappings;
  201. bool _bMinimalMembers;
  202. bool _bEntry;
  203. string _sIdlFile;
  204. bool _bTS;
  205. bool _bDTS;
  206. int _iOptimizeLevel;
  207. };
  208. #endif