singlefilegenerator.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. });
  96. if (hasQtTypes) {
  97. externalIncludes.insert("QtProtobufQtTypes");
  98. }
  99. for (int i = 0; i < file->dependency_count(); i++) {
  100. if (file->dependency(i)->name() == "QtProtobuf/QtCore.proto"
  101. || file->dependency(i)->name() == "QtProtobuf/QtGui.proto") {
  102. continue;
  103. }
  104. internalIncludes.insert(utils::removeFileSuffix(file->dependency(i)->name()) + Templates::ProtoFileSuffix);
  105. }
  106. for (auto include : externalIncludes) {
  107. headerPrinter->Print({{"include", include}}, Templates::ExternalIncludeTemplate);
  108. }
  109. for (auto include : internalIncludes) {
  110. headerPrinter->Print({{"include", include}}, Templates::InternalIncludeTemplate);
  111. }
  112. if (GeneratorOptions::instance().hasQml()) {
  113. sourcePrinter->Print({{"include", "QQmlEngine"}}, Templates::ExternalIncludeTemplate);
  114. }
  115. printQtProtobufUsingNamespace(sourcePrinter);
  116. PackagesList packageList;
  117. packageList[file->package()].push_back(file);
  118. for (int i = 0; i < file->enum_type_count(); i++) {
  119. EnumDeclarationPrinter enumDecl(file->enum_type(i), headerPrinter);
  120. enumDecl.run();
  121. EnumDefinitionPrinter enumSourceDef(file->enum_type(i), sourcePrinter);
  122. enumSourceDef.run();
  123. }
  124. common::iterateMessages(file, [&headerPrinter](const ::google::protobuf::Descriptor *message) {
  125. MessageDeclarationPrinter messageDecl(message, headerPrinter);
  126. messageDecl.printClassForwardDeclaration();
  127. });
  128. common::iterateMessages(file, [&headerPrinter, &sourcePrinter](const ::google::protobuf::Descriptor *message) {
  129. MessageDeclarationPrinter messageDecl(message, headerPrinter);
  130. messageDecl.printClassDeclaration();
  131. MessageDefinitionPrinter messageDef(message, sourcePrinter);
  132. messageDef.printClassDefinition();
  133. });
  134. return true;
  135. }
  136. bool SingleFileGenerator::GenerateServices(const ::google::protobuf::FileDescriptor *file,
  137. const std::string &,
  138. ::google::protobuf::compiler::GeneratorContext *generatorContext,
  139. std::string *) const
  140. {
  141. if (file->service_count() <= 0) {
  142. return true;
  143. }
  144. std::set<std::string> internalIncludes;
  145. std::set<std::string> externalIncludes;
  146. std::string basename = generateBaseName(file, utils::extractFileBasename(file->name()));
  147. std::unique_ptr<io::ZeroCopyOutputStream> headerStream(generatorContext->Open(basename + Templates::GrpcFileSuffix + Templates::ProtoFileSuffix + ".h"));
  148. std::unique_ptr<io::ZeroCopyOutputStream> sourceStream(generatorContext->Open(basename + Templates::GrpcFileSuffix + Templates::ProtoFileSuffix + ".cpp"));
  149. std::shared_ptr<::google::protobuf::io::Printer> headerPrinter(new ::google::protobuf::io::Printer(headerStream.get(), '$'));
  150. std::shared_ptr<::google::protobuf::io::Printer> sourcePrinter(new ::google::protobuf::io::Printer(sourceStream.get(), '$'));
  151. printDisclaimer(headerPrinter);
  152. printPreamble(headerPrinter);
  153. headerPrinter->Print(Templates::DefaultProtobufIncludesTemplate);
  154. if (GeneratorOptions::instance().hasQml()) {
  155. headerPrinter->Print(Templates::QmlProtobufIncludesTemplate);
  156. }
  157. printDisclaimer(sourcePrinter);
  158. sourcePrinter->Print({{"include", basename + Templates::GrpcFileSuffix + Templates::ProtoFileSuffix}}, Templates::InternalIncludeTemplate);
  159. for (int i = 0; i < file->service_count(); i++) {
  160. const ServiceDescriptor *service = file->service(i);
  161. for (int i = 0; i < service->method_count(); i++) {
  162. const MethodDescriptor *method = service->method(i);
  163. if (method->input_type()->file() != service->file()) {
  164. internalIncludes.insert(utils::removeFileSuffix(method->input_type()->file()->name()) + Templates::ProtoFileSuffix);
  165. }
  166. if (method->output_type()->file() != service->file()) {
  167. internalIncludes.insert(utils::removeFileSuffix(method->output_type()->file()->name()) + Templates::ProtoFileSuffix);
  168. }
  169. }
  170. }
  171. externalIncludes.insert("QAbstractGrpcClient");
  172. externalIncludes.insert("QGrpcAsyncReply");
  173. externalIncludes.insert("QGrpcSubscription");
  174. if (file->message_type_count() > 0) {
  175. internalIncludes.insert(basename + Templates::ProtoFileSuffix);
  176. }
  177. for (auto include : externalIncludes) {
  178. headerPrinter->Print({{"include", include}}, Templates::ExternalIncludeTemplate);
  179. }
  180. for (auto include : internalIncludes) {
  181. headerPrinter->Print({{"include", include}}, Templates::InternalIncludeTemplate);
  182. }
  183. if (GeneratorOptions::instance().hasQml()) {
  184. sourcePrinter->Print({{"include", "QQmlEngine"}}, Templates::ExternalIncludeTemplate);
  185. sourcePrinter->Print({{"include", "QJSEngine"}}, Templates::ExternalIncludeTemplate);
  186. sourcePrinter->Print({{"include", "QJSValue"}}, Templates::ExternalIncludeTemplate);
  187. }
  188. printQtProtobufUsingNamespace(sourcePrinter);
  189. for (int i = 0; i < file->service_count(); i++) {
  190. const ServiceDescriptor *service = file->service(i);
  191. // Serverside descriptor is not in use
  192. // std::string baseFilename(service->name());
  193. // utils::tolower(baseFilename);
  194. // std::string fullFilename = baseFilename + "server.h";
  195. // ServerDeclarationprinter serverGen(service,
  196. // std::shared_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(fullFilename)));
  197. // serverGen.run();
  198. ClientDeclarationPrinter clientDecl(service, headerPrinter);
  199. clientDecl.run();
  200. ClientDefinitionPrinter clientDef(service, sourcePrinter);
  201. clientDef.run();
  202. }
  203. return true;
  204. }