multifilegenerator.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "multifilegenerator.h"
  26. #include "templates.h"
  27. #include "messagedeclarationprinter.h"
  28. #include "messagedefinitionprinter.h"
  29. #include "enumdeclarationprinter.h"
  30. #include "enumdefinitionprinter.h"
  31. #include "serverdeclarationprinter.h"
  32. #include "clientdeclarationprinter.h"
  33. #include "clientdefinitionprinter.h"
  34. #include "utils.h"
  35. #include "generatoroptions.h"
  36. #include <iostream>
  37. #include <set>
  38. #include <google/protobuf/stubs/logging.h>
  39. #include <google/protobuf/stubs/common.h>
  40. #include <google/protobuf/io/printer.h>
  41. #include <google/protobuf/io/zero_copy_stream.h>
  42. #include <google/protobuf/descriptor.h>
  43. using namespace ::QtProtobuf::generator;
  44. using namespace ::google::protobuf;
  45. using namespace ::google::protobuf::compiler;
  46. MultiFileGenerator::MultiFileGenerator() : GeneratorBase(GeneratorBase::MultiMode)
  47. {}
  48. bool MultiFileGenerator::Generate(const FileDescriptor *file,
  49. const std::string &,
  50. GeneratorContext *generatorContext,
  51. std::string *error) const
  52. {
  53. if (file->syntax() != FileDescriptor::SYNTAX_PROTO3) {
  54. *error = "Invalid proto used. This plugin only supports 'proto3' syntax";
  55. return false;
  56. }
  57. common::iterateMessages(file, [&] (const ::google::protobuf::Descriptor *message) {
  58. std::string baseFilename(message->name());
  59. utils::tolower(baseFilename);
  60. baseFilename = generateBaseName(file, baseFilename);
  61. std::unique_ptr<io::ZeroCopyOutputStream> headerStream(generatorContext->Open(baseFilename + ".h"));
  62. std::unique_ptr<io::ZeroCopyOutputStream> sourceStream(generatorContext->Open(baseFilename + ".cpp"));
  63. std::shared_ptr<::google::protobuf::io::Printer> headerPrinter(new ::google::protobuf::io::Printer(headerStream.get(), '$'));
  64. std::shared_ptr<::google::protobuf::io::Printer> sourcePrinter(new ::google::protobuf::io::Printer(sourceStream.get(), '$'));
  65. MessageDeclarationPrinter messageDecl(message, headerPrinter);
  66. printDisclaimer(headerPrinter);
  67. printPreamble(headerPrinter);
  68. headerPrinter->Print(Templates::DefaultProtobufIncludesTemplate);
  69. if (GeneratorOptions::instance().hasQml()) {
  70. headerPrinter->Print(Templates::QmlProtobufIncludesTemplate);
  71. }
  72. std::set<std::string> existingIncludes;
  73. for (int i = 0; i < message->field_count(); i++) {
  74. printInclude(headerPrinter, message, message->field(i), existingIncludes);
  75. }
  76. auto deps = findNestedDependency(message);
  77. for (int i = 0; i < message->field_count(); i++) {
  78. auto field = message->field(i);
  79. if (common::isPureMessage(field)) {
  80. auto dependency = field->message_type();
  81. std::cerr << "Found dependency: " << dependency->name() << std::endl;
  82. deps.push_back(dependency);
  83. }
  84. }
  85. for (auto dependency : deps) {
  86. std::string outFileBasename = "";
  87. std::string fieldPackage = dependency->file()->package();
  88. if (fieldPackage != message->file()->package()) {
  89. std::vector<std::string> packages = utils::split(fieldPackage, '.');
  90. for (auto package : packages) {
  91. outFileBasename += package + "/";
  92. }
  93. }
  94. auto parentType = common::findHighestMessage(dependency);
  95. std::string typeName = parentType->name();
  96. utils::tolower(typeName);
  97. auto newInclude = outFileBasename + typeName;
  98. if (existingIncludes.find(newInclude) == std::end(existingIncludes)) {
  99. headerPrinter->Print({{"include", newInclude}}, Templates::InternalIncludeTemplate);
  100. existingIncludes.insert(newInclude);
  101. }
  102. }
  103. for (auto dependency : deps) {
  104. if (!common::isNestedOf(dependency, message)) {
  105. auto namespaces = common::getNamespaces(dependency);
  106. if (common::isNested(dependency)) {
  107. namespaces = common::getNestedNamespaces(dependency);
  108. }
  109. printNamespaces(headerPrinter, namespaces);
  110. headerPrinter->Print({{"classname", utils::upperCaseName(dependency->name())}}, Templates::ProtoClassForwardDeclarationTemplate);
  111. headerPrinter->Print("\n");
  112. for (size_t i = 0; i < namespaces.size(); ++i) {
  113. headerPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  114. }
  115. headerPrinter->Print("\n");
  116. }
  117. }
  118. messageDecl.printClassDeclaration();
  119. printDisclaimer(sourcePrinter);
  120. std::string includeFileName = message->name();
  121. utils::tolower(includeFileName);
  122. sourcePrinter->Print({{"include", includeFileName}}, Templates::InternalIncludeTemplate);
  123. if (GeneratorOptions::instance().hasQml()) {
  124. sourcePrinter->Print({{"include", "QQmlEngine"}}, Templates::ExternalIncludeTemplate);
  125. }
  126. MessageDefinitionPrinter messageDef(message, sourcePrinter);
  127. messageDef.printClassDefinition();
  128. });
  129. for (int i = 0; i < file->service_count(); i++) {
  130. const ServiceDescriptor *service = file->service(i);
  131. std::string baseFilename(service->name());
  132. utils::tolower(baseFilename);
  133. baseFilename = generateBaseName(file, baseFilename);
  134. std::unique_ptr<io::ZeroCopyOutputStream> headerStream(generatorContext->Open(baseFilename + "client.h"));
  135. std::unique_ptr<io::ZeroCopyOutputStream> sourceStream(generatorContext->Open(baseFilename + "client.cpp"));
  136. std::shared_ptr<::google::protobuf::io::Printer> headerPrinter(new ::google::protobuf::io::Printer(headerStream.get(), '$'));
  137. std::shared_ptr<::google::protobuf::io::Printer> sourcePrinter(new ::google::protobuf::io::Printer(sourceStream.get(), '$'));
  138. printDisclaimer(headerPrinter);
  139. printPreamble(headerPrinter);
  140. ClientDeclarationPrinter clientGen(service, headerPrinter);
  141. clientGen.printIncludes();
  142. clientGen.printClientIncludes();
  143. clientGen.run();
  144. printDisclaimer(sourcePrinter);
  145. std::string includeFileName = service->name();
  146. utils::tolower(includeFileName);
  147. sourcePrinter->Print({{"include", includeFileName}}, Templates::InternalIncludeTemplate);
  148. if (GeneratorOptions::instance().hasQml()) {
  149. sourcePrinter->Print({{"include", "QQmlEngine"}}, Templates::ExternalIncludeTemplate);
  150. sourcePrinter->Print({{"include", "QJSEngine"}}, Templates::ExternalIncludeTemplate);
  151. sourcePrinter->Print({{"include", "QJSValue"}}, Templates::ExternalIncludeTemplate);
  152. }
  153. printQtProtobufUsingNamespace(sourcePrinter);
  154. ClientDefinitionPrinter clientSrcGen(service, sourcePrinter);
  155. clientSrcGen.run();
  156. }
  157. return true;
  158. }
  159. std::list<const ::google::protobuf::Descriptor *> MultiFileGenerator::findNestedDependency(const ::google::protobuf::Descriptor *message) const
  160. {
  161. std::list<const ::google::protobuf::Descriptor *> dependencies;
  162. common::iterateNestedMessages(message, [&dependencies, this] (const ::google::protobuf::Descriptor *message) {
  163. for (int i = 0; i < message->field_count(); i++) {
  164. auto field = message->field(i);
  165. if (common::isPureMessage(field)) {
  166. auto dependency = field->message_type();
  167. std::cerr << "Found dependency: " << dependency->name() << std::endl;
  168. dependencies.push_back(dependency);
  169. }
  170. }
  171. auto nextLevelDependencies = findNestedDependency(message);
  172. dependencies.insert(dependencies.end(), nextLevelDependencies.begin(), nextLevelDependencies.end());
  173. });
  174. return dependencies;
  175. }
  176. bool MultiFileGenerator::GenerateAll(const std::vector<const FileDescriptor *> &files, const string &parameter, GeneratorContext *generatorContext, string *error) const
  177. {
  178. std::string globalEnumsFilename = "globalenums";
  179. std::unique_ptr<io::ZeroCopyOutputStream> headerStream(generatorContext->Open(globalEnumsFilename + ".h"));
  180. std::unique_ptr<io::ZeroCopyOutputStream> sourceStream(generatorContext->Open(globalEnumsFilename + ".cpp"));
  181. std::shared_ptr<::google::protobuf::io::Printer> headerPrinter(new ::google::protobuf::io::Printer(headerStream.get(), '$'));
  182. std::shared_ptr<::google::protobuf::io::Printer> sourcePrinter(new ::google::protobuf::io::Printer(sourceStream.get(), '$'));
  183. printDisclaimer(headerPrinter);
  184. printPreamble(headerPrinter);
  185. headerPrinter->Print(Templates::DefaultProtobufIncludesTemplate);
  186. printDisclaimer(sourcePrinter);
  187. sourcePrinter->Print({{"include", globalEnumsFilename}}, Templates::InternalIncludeTemplate);
  188. if (GeneratorOptions::instance().hasQml()) {
  189. sourcePrinter->Print({{"include", "QQmlEngine"}}, Templates::ExternalIncludeTemplate);
  190. }
  191. for (auto file : files) { //TODO: Each should be printed to separate file
  192. for (int i = 0; i < file->enum_type_count(); i++) {
  193. auto enumType = file->enum_type(i);
  194. EnumDeclarationPrinter enumDecl(enumType, headerPrinter);
  195. enumDecl.run();
  196. EnumDefinitionPrinter enumDef(enumType, sourcePrinter);
  197. enumDef.run();
  198. }
  199. }
  200. return GeneratorBase::GenerateAll(files, parameter, generatorContext, error);
  201. }