singlefilegenerator.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 "singlefilegenerator.h"
  26. #include "messagedeclarationprinter.h"
  27. #include "messagedefinitionprinter.h"
  28. #include "enumdeclarationprinter.h"
  29. #include "enumdefinitionprinter.h"
  30. #include "serverdeclarationprinter.h"
  31. #include "clientdeclarationprinter.h"
  32. #include "clientdefinitionprinter.h"
  33. #include "templates.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. SingleFileGenerator::SingleFileGenerator() : GeneratorBase(GeneratorBase::SingleMode)
  47. {}
  48. bool SingleFileGenerator::Generate(const FileDescriptor *file,
  49. const std::string &parameter,
  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. return GenerateMessages(file, parameter, generatorContext, error)
  58. && GenerateServices(file, parameter, generatorContext, error);
  59. }
  60. bool SingleFileGenerator::GenerateMessages(const ::google::protobuf::FileDescriptor *file,
  61. const std::string &,
  62. ::google::protobuf::compiler::GeneratorContext *generatorContext,
  63. std::string *) const {
  64. if (file->message_type_count() <= 0 && file->enum_type_count() <= 0) {
  65. return true;
  66. }
  67. std::set<std::string> internalIncludes;
  68. std::set<std::string> externalIncludes;
  69. std::string basename = generateBaseName(file, utils::extractFileBasename(file->name()));
  70. std::unique_ptr<io::ZeroCopyOutputStream> headerStream(generatorContext->Open(basename + Templates::ProtoFileSuffix + ".h"));
  71. std::unique_ptr<io::ZeroCopyOutputStream> sourceStream(generatorContext->Open(basename + Templates::ProtoFileSuffix + ".cpp"));
  72. std::shared_ptr<::google::protobuf::io::Printer> headerPrinter(new ::google::protobuf::io::Printer(headerStream.get(), '$'));
  73. std::shared_ptr<::google::protobuf::io::Printer> sourcePrinter(new ::google::protobuf::io::Printer(sourceStream.get(), '$'));
  74. printDisclaimer(headerPrinter);
  75. printPreamble(headerPrinter);
  76. headerPrinter->Print(Templates::DefaultProtobufIncludesTemplate);
  77. if (GeneratorOptions::instance().hasQml()) {
  78. headerPrinter->Print(Templates::QmlProtobufIncludesTemplate);
  79. }
  80. printDisclaimer(sourcePrinter);
  81. sourcePrinter->Print({{"include", basename + Templates::ProtoFileSuffix}}, Templates::InternalIncludeTemplate);
  82. externalIncludes.insert("QByteArray");
  83. externalIncludes.insert("QString");
  84. bool hasQtTypes = false;
  85. common::iterateMessages(file, [&externalIncludes, &hasQtTypes](const ::google::protobuf::Descriptor *message) {
  86. for (int i = 0; i < message->field_count(); i++) {
  87. auto field = message->field(i);
  88. if (field->type() == ::google::protobuf::FieldDescriptor::TYPE_MESSAGE
  89. && !field->is_map() && !field->is_repeated()
  90. && common::isQtType(field)) {
  91. externalIncludes.insert(field->message_type()->name());
  92. hasQtTypes = true;
  93. }
  94. }
  95. if (message->full_name() == "google.protobuf.Timestamp") {
  96. externalIncludes.insert("QDateTime");
  97. }
  98. });
  99. if (hasQtTypes) {
  100. externalIncludes.insert("QtProtobufQtTypes");
  101. }
  102. for (int i = 0; i < file->dependency_count(); i++) {
  103. if (file->dependency(i)->name() == "QtProtobuf/QtCore.proto"
  104. || file->dependency(i)->name() == "QtProtobuf/QtGui.proto") {
  105. continue;
  106. }
  107. internalIncludes.insert(utils::removeFileSuffix(file->dependency(i)->name()) + Templates::ProtoFileSuffix);
  108. }
  109. for (auto include : externalIncludes) {
  110. headerPrinter->Print({{"include", include}}, Templates::ExternalIncludeTemplate);
  111. }
  112. for (auto include : internalIncludes) {
  113. headerPrinter->Print({{"include", include}}, Templates::InternalIncludeTemplate);
  114. }
  115. if (GeneratorOptions::instance().hasQml()) {
  116. sourcePrinter->Print({{"include", "QQmlEngine"}}, Templates::ExternalIncludeTemplate);
  117. }
  118. printQtProtobufUsingNamespace(sourcePrinter);
  119. PackagesList packageList;
  120. packageList[file->package()].push_back(file);
  121. for (int i = 0; i < file->enum_type_count(); i++) {
  122. EnumDeclarationPrinter enumDecl(file->enum_type(i), headerPrinter);
  123. enumDecl.run();
  124. EnumDefinitionPrinter enumSourceDef(file->enum_type(i), sourcePrinter);
  125. enumSourceDef.run();
  126. }
  127. common::iterateMessages(file, [&headerPrinter](const ::google::protobuf::Descriptor *message) {
  128. MessageDeclarationPrinter messageDecl(message, headerPrinter);
  129. messageDecl.printClassForwardDeclaration();
  130. });
  131. common::iterateMessages(file, [&headerPrinter, &sourcePrinter](const ::google::protobuf::Descriptor *message) {
  132. MessageDeclarationPrinter messageDecl(message, headerPrinter);
  133. messageDecl.printClassDeclaration();
  134. MessageDefinitionPrinter messageDef(message, sourcePrinter);
  135. messageDef.printClassDefinition();
  136. });
  137. return true;
  138. }
  139. bool SingleFileGenerator::GenerateServices(const ::google::protobuf::FileDescriptor *file,
  140. const std::string &,
  141. ::google::protobuf::compiler::GeneratorContext *generatorContext,
  142. std::string *) const
  143. {
  144. if (file->service_count() <= 0) {
  145. return true;
  146. }
  147. std::set<std::string> internalIncludes;
  148. std::set<std::string> externalIncludes;
  149. std::string basename = generateBaseName(file, utils::extractFileBasename(file->name()));
  150. std::unique_ptr<io::ZeroCopyOutputStream> headerStream(generatorContext->Open(basename + Templates::GrpcFileSuffix + Templates::ProtoFileSuffix + ".h"));
  151. std::unique_ptr<io::ZeroCopyOutputStream> sourceStream(generatorContext->Open(basename + Templates::GrpcFileSuffix + Templates::ProtoFileSuffix + ".cpp"));
  152. std::shared_ptr<::google::protobuf::io::Printer> headerPrinter(new ::google::protobuf::io::Printer(headerStream.get(), '$'));
  153. std::shared_ptr<::google::protobuf::io::Printer> sourcePrinter(new ::google::protobuf::io::Printer(sourceStream.get(), '$'));
  154. printDisclaimer(headerPrinter);
  155. printPreamble(headerPrinter);
  156. headerPrinter->Print(Templates::DefaultProtobufIncludesTemplate);
  157. if (GeneratorOptions::instance().hasQml()) {
  158. headerPrinter->Print(Templates::QmlProtobufIncludesTemplate);
  159. }
  160. printDisclaimer(sourcePrinter);
  161. sourcePrinter->Print({{"include", basename + Templates::GrpcFileSuffix + Templates::ProtoFileSuffix}}, Templates::InternalIncludeTemplate);
  162. for (int i = 0; i < file->service_count(); i++) {
  163. const ServiceDescriptor *service = file->service(i);
  164. for (int i = 0; i < service->method_count(); i++) {
  165. const MethodDescriptor *method = service->method(i);
  166. if (method->input_type()->file() != service->file()) {
  167. internalIncludes.insert(utils::removeFileSuffix(method->input_type()->file()->name()) + Templates::ProtoFileSuffix);
  168. }
  169. if (method->output_type()->file() != service->file()) {
  170. internalIncludes.insert(utils::removeFileSuffix(method->output_type()->file()->name()) + Templates::ProtoFileSuffix);
  171. }
  172. }
  173. }
  174. externalIncludes.insert("QAbstractGrpcClient");
  175. externalIncludes.insert("QGrpcCallReply");
  176. externalIncludes.insert("QGrpcStream");
  177. if (file->message_type_count() > 0) {
  178. internalIncludes.insert(basename + Templates::ProtoFileSuffix);
  179. }
  180. for (auto include : externalIncludes) {
  181. headerPrinter->Print({{"include", include}}, Templates::ExternalIncludeTemplate);
  182. }
  183. for (auto include : internalIncludes) {
  184. headerPrinter->Print({{"include", include}}, Templates::InternalIncludeTemplate);
  185. }
  186. if (GeneratorOptions::instance().hasQml()) {
  187. sourcePrinter->Print({{"include", "QQmlEngine"}}, Templates::ExternalIncludeTemplate);
  188. sourcePrinter->Print({{"include", "QJSEngine"}}, Templates::ExternalIncludeTemplate);
  189. sourcePrinter->Print({{"include", "QJSValue"}}, Templates::ExternalIncludeTemplate);
  190. }
  191. printQtProtobufUsingNamespace(sourcePrinter);
  192. for (int i = 0; i < file->service_count(); i++) {
  193. const ServiceDescriptor *service = file->service(i);
  194. // Serverside descriptor is not in use
  195. // std::string baseFilename(service->name());
  196. // utils::tolower(baseFilename);
  197. // std::string fullFilename = baseFilename + "server.h";
  198. // ServerDeclarationprinter serverGen(service,
  199. // std::shared_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(fullFilename)));
  200. // serverGen.run();
  201. ClientDeclarationPrinter clientDecl(service, headerPrinter);
  202. clientDecl.run();
  203. ClientDefinitionPrinter clientDef(service, sourcePrinter);
  204. clientDef.run();
  205. }
  206. return true;
  207. }