code_generator.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include "code_generator.h"
  17. string CodeGenerator::printHeaderRemark(const string &sTypeName)
  18. {
  19. ostringstream s;
  20. s << "// **********************************************************************" << endl;
  21. s << "// Parsed By " << IDL_NAMESPACE_STR << "Parser(" << PARSER_VERSION << "), Generated By " << EXECUTE_FILENAME << "(" << GENERATOR_VERSION << ")" << endl;
  22. s << "// " << IDL_NAMESPACE_STR << "Parser Maintained By <" << TC_Common::upper(IDL_NAMESPACE_STR) << "> and " << EXECUTE_FILENAME << " Maintained By <superzheng>" << endl;
  23. s << "// Generated from \"" << TC_File::extractFileName(_sIdlFile) << "\" by " <<
  24. (_bEntry ? sTypeName : (_bMinimalMembers ? "Minimal" : "Relation")) << " Mode" << endl;
  25. s << "// **********************************************************************" << endl;
  26. s << endl;
  27. return s.str();
  28. }
  29. void CodeGenerator::createFile(const string &file, const bool bEntry)
  30. {
  31. _sIdlFile = getRealFileInfo(file);
  32. _bEntry = bEntry;
  33. g_parse->parse(_sIdlFile);
  34. vector<ContextPtr> contexts = g_parse->getContexts();
  35. for(size_t i = 0; i < contexts.size(); i++)
  36. {
  37. if (_sIdlFile == contexts[i]->getFileName())
  38. {
  39. scan(_sIdlFile, true); // collect idl symbols
  40. if (!_bClient && !_bServer)
  41. {
  42. if (_bTS)
  43. {
  44. generateTS(contexts[i]); // generate .ts
  45. }
  46. else
  47. {
  48. generateJS(contexts[i]); // generate .js
  49. if (_bDTS) generateDTS(contexts[i]); // generate .d.ts
  50. }
  51. }
  52. if (_bClient)
  53. {
  54. addTarsPingForProxy(contexts[i]); // add tars_ping function for client proxy
  55. if (_bTS)
  56. {
  57. if (!generateTSProxy(contexts[i])) return; // generate .ts for proxy classes
  58. }
  59. else
  60. {
  61. if (!generateJSProxy(contexts[i])) return; // generate .js for proxy classes
  62. if (_bDTS) generateDTSProxy(contexts[i]); // generate .d.ts for proxy classes
  63. }
  64. }
  65. if (_bServer)
  66. {
  67. if (_bTS)
  68. {
  69. if (!generateTSServer(contexts[i])) return; // generate .ts for server classes
  70. generateTSServerImp(contexts[i]); // generate .ts for server implementations
  71. }
  72. else
  73. {
  74. if (!generateJSServer(contexts[i])) return; // generate .js for server classes
  75. if (_bDTS) generateDTSServer(contexts[i]); // generate .d.ts for server classes
  76. generateJSServerImp(contexts[i]); // generate .js for server implementations
  77. }
  78. }
  79. vector<string> files = contexts[i]->getIncludes();
  80. for (size_t ii = 0; _bRecursive && ii < files.size(); ii++)
  81. {
  82. CodeGenerator node;
  83. node.setRpcPath(_sRpcPath);
  84. node.setStreamPath(_sStreamPath);
  85. node.setTargetPath(_sToPath);
  86. node.setRecursive(_bRecursive);
  87. node.setUseSpecialPath(_bUseSpecialPath);
  88. node.setLongType(_iLongType);
  89. node.setStringBinaryEncoding(_bStringBinaryEncoding);
  90. node.setMinimalMembers(_bMinimalMembers);
  91. node.setDependent(_depMembers);
  92. node.setEnableTS(_bTS);
  93. node.setEnableDTS(_bDTS);
  94. node.createFile(files[ii], false);
  95. }
  96. }
  97. }
  98. }
  99. void CodeGenerator::addTarsPingForProxy(const ContextPtr &cPtr){
  100. vector<NamespacePtr> namespaces = cPtr->getNamespaces();
  101. string ping = TC_Common::lower(IDL_NAMESPACE_STR) + "_ping";
  102. for(size_t i = 0; i < namespaces.size(); i++)
  103. {
  104. vector<InterfacePtr> & is = namespaces[i]->getAllInterfacePtr();
  105. for (size_t ii = 0; ii < is.size(); ii++)
  106. {
  107. is[ii]->createOperation(ping, nullptr);
  108. }
  109. }
  110. }