multifilegenerator.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. deps.push_back(dependency);
  82. }
  83. }
  84. for (auto dependency : deps) {
  85. std::string outFileBasename = "";
  86. std::string fieldPackage = dependency->file()->package();
  87. if (fieldPackage != message->file()->package()) {
  88. std::vector<std::string> packages = utils::split(fieldPackage, '.');
  89. for (auto package : packages) {
  90. outFileBasename += package + "/";
  91. }
  92. }
  93. auto parentType = common::findHighestMessage(dependency);
  94. std::string typeName = parentType->name();
  95. utils::tolower(typeName);
  96. auto newInclude = outFileBasename + typeName;
  97. if (existingIncludes.find(newInclude) == std::end(existingIncludes)) {
  98. headerPrinter->Print({{"include", newInclude}}, Templates::InternalIncludeTemplate);
  99. existingIncludes.insert(newInclude);
  100. }
  101. }
  102. for (auto dependency : deps) {
  103. if (!common::isNestedOf(dependency, message)) {
  104. auto namespaces = common::getNamespaces(dependency);
  105. if (common::isNested(dependency)) {
  106. namespaces = common::getNestedNamespaces(dependency);
  107. }
  108. printNamespaces(headerPrinter, namespaces);
  109. headerPrinter->Print({{"classname", utils::upperCaseName(dependency->name())}}, Templates::ProtoClassForwardDeclarationTemplate);
  110. headerPrinter->Print("\n");
  111. for (size_t i = 0; i < namespaces.size(); ++i) {
  112. headerPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  113. }
  114. headerPrinter->Print("\n");
  115. }
  116. }
  117. messageDecl.printClassDeclaration();
  118. printDisclaimer(sourcePrinter);
  119. std::string includeFileName = message->name();
  120. utils::tolower(includeFileName);
  121. sourcePrinter->Print({{"include", includeFileName}}, Templates::InternalIncludeTemplate);
  122. if (GeneratorOptions::instance().hasQml()) {
  123. sourcePrinter->Print({{"include", "QQmlEngine"}}, Templates::ExternalIncludeTemplate);
  124. }
  125. MessageDefinitionPrinter messageDef(message, sourcePrinter);
  126. messageDef.printClassDefinition();
  127. });
  128. for (int i = 0; i < file->service_count(); i++) {
  129. const ServiceDescriptor *service = file->service(i);
  130. std::string baseFilename(service->name());
  131. utils::tolower(baseFilename);
  132. baseFilename = generateBaseName(file, baseFilename);
  133. std::unique_ptr<io::ZeroCopyOutputStream> headerStream(generatorContext->Open(baseFilename + "client.h"));
  134. std::unique_ptr<io::ZeroCopyOutputStream> sourceStream(generatorContext->Open(baseFilename + "client.cpp"));
  135. std::shared_ptr<::google::protobuf::io::Printer> headerPrinter(new ::google::protobuf::io::Printer(headerStream.get(), '$'));
  136. std::shared_ptr<::google::protobuf::io::Printer> sourcePrinter(new ::google::protobuf::io::Printer(sourceStream.get(), '$'));
  137. printDisclaimer(headerPrinter);
  138. printPreamble(headerPrinter);
  139. ClientDeclarationPrinter clientGen(service, headerPrinter);
  140. clientGen.printIncludes();
  141. clientGen.printClientIncludes();
  142. clientGen.run();
  143. printDisclaimer(sourcePrinter);
  144. std::string includeFileName = service->name();
  145. utils::tolower(includeFileName);
  146. sourcePrinter->Print({{"include", includeFileName}}, Templates::InternalIncludeTemplate);
  147. if (GeneratorOptions::instance().hasQml()) {
  148. sourcePrinter->Print({{"include", "QQmlEngine"}}, Templates::ExternalIncludeTemplate);
  149. sourcePrinter->Print({{"include", "QJSEngine"}}, Templates::ExternalIncludeTemplate);
  150. sourcePrinter->Print({{"include", "QJSValue"}}, Templates::ExternalIncludeTemplate);
  151. }
  152. ClientDefinitionPrinter clientSrcGen(service, sourcePrinter);
  153. clientSrcGen.run();
  154. }
  155. return true;
  156. }
  157. std::list<const ::google::protobuf::Descriptor *> MultiFileGenerator::findNestedDependency(const ::google::protobuf::Descriptor *message) const
  158. {
  159. std::list<const ::google::protobuf::Descriptor *> dependencies;
  160. common::iterateNestedMessages(message, [&dependencies, this] (const ::google::protobuf::Descriptor *message) {
  161. for (int i = 0; i < message->field_count(); i++) {
  162. auto field = message->field(i);
  163. if (common::isPureMessage(field)) {
  164. auto dependency = field->message_type();
  165. dependencies.push_back(dependency);
  166. }
  167. }
  168. auto nextLevelDependencies = findNestedDependency(message);
  169. dependencies.insert(dependencies.end(), nextLevelDependencies.begin(), nextLevelDependencies.end());
  170. });
  171. return dependencies;
  172. }
  173. bool MultiFileGenerator::GenerateAll(const std::vector<const FileDescriptor *> &files, const string &parameter, GeneratorContext *generatorContext, string *error) const
  174. {
  175. std::string globalEnumsFilename = "globalenums";
  176. std::unique_ptr<io::ZeroCopyOutputStream> headerStream(generatorContext->Open(globalEnumsFilename + ".h"));
  177. std::unique_ptr<io::ZeroCopyOutputStream> sourceStream(generatorContext->Open(globalEnumsFilename + ".cpp"));
  178. std::shared_ptr<::google::protobuf::io::Printer> headerPrinter(new ::google::protobuf::io::Printer(headerStream.get(), '$'));
  179. std::shared_ptr<::google::protobuf::io::Printer> sourcePrinter(new ::google::protobuf::io::Printer(sourceStream.get(), '$'));
  180. printDisclaimer(headerPrinter);
  181. printPreamble(headerPrinter);
  182. headerPrinter->Print(Templates::DefaultProtobufIncludesTemplate);
  183. printDisclaimer(sourcePrinter);
  184. sourcePrinter->Print({{"include", globalEnumsFilename}}, Templates::InternalIncludeTemplate);
  185. if (GeneratorOptions::instance().hasQml()) {
  186. sourcePrinter->Print({{"include", "QQmlEngine"}}, Templates::ExternalIncludeTemplate);
  187. }
  188. for (auto file : files) { //TODO: Each should be printed to separate file
  189. for (int i = 0; i < file->enum_type_count(); i++) {
  190. auto enumType = file->enum_type(i);
  191. EnumDeclarationPrinter enumDecl(enumType, headerPrinter);
  192. enumDecl.run();
  193. EnumDefinitionPrinter enumDef(enumType, sourcePrinter);
  194. enumDef.run();
  195. }
  196. }
  197. return GeneratorBase::GenerateAll(files, parameter, generatorContext, error);
  198. }