singlefilegenerator.cpp 11 KB

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