messagedeclarationprinter.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>, Tatyana Borisova <tanusshhka@mail.ru>
  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 "messagedeclarationprinter.h"
  26. #include "utils.h"
  27. #include "generatoroptions.h"
  28. #include "generatorcommon.h"
  29. #include <iostream>
  30. #include <google/protobuf/descriptor.h>
  31. #include <google/protobuf/descriptor.pb.h>
  32. using namespace ::QtProtobuf::generator;
  33. using namespace ::google::protobuf;
  34. using namespace ::google::protobuf::io;
  35. using namespace ::google::protobuf::compiler;
  36. MessageDeclarationPrinter::MessageDeclarationPrinter(const Descriptor *message, const std::shared_ptr<::google::protobuf::io::Printer> &printer)
  37. : DescriptorPrinterBase<google::protobuf::Descriptor>(message, printer)
  38. {
  39. mTypeMap = common::produceMessageTypeMap(message, nullptr);
  40. }
  41. void MessageDeclarationPrinter::printClassForwardDeclarationPrivate()
  42. {
  43. if (common::hasNestedMessages(mDescriptor)) {
  44. mPrinter->Print({{"namespace", mTypeMap["classname"] + Templates::QtProtobufNestedNamespace}}, Templates::NamespaceTemplate);
  45. common::iterateNestedMessages(mDescriptor, [this](const ::google::protobuf::Descriptor *nestedMessage) {
  46. MessageDeclarationPrinter nesterPrinter(nestedMessage, mPrinter);
  47. nesterPrinter.printClassForwardDeclarationPrivate();
  48. });
  49. mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  50. }
  51. mPrinter->Print(mTypeMap, Templates::ProtoClassForwardDeclarationTemplate);
  52. mPrinter->Print(mTypeMap, Templates::ComplexListTypeUsingTemplate);
  53. }
  54. void MessageDeclarationPrinter::printClassForwardDeclaration()
  55. {
  56. printNamespaces();
  57. printClassForwardDeclarationPrivate();
  58. encloseNamespaces();
  59. }
  60. void MessageDeclarationPrinter::printClassDeclaration()
  61. {
  62. printNamespaces();
  63. printClassDeclarationPrivate();
  64. encloseNamespaces();
  65. printMetaTypesDeclaration();//Meta types declaration should be outside of namespaces block
  66. }
  67. void MessageDeclarationPrinter::printClassDeclarationPrivate()
  68. {
  69. mPrinter->Print({{"namespace", mTypeMap["classname"] + Templates::QtProtobufNestedNamespace}}, Templates::NamespaceTemplate);
  70. common::iterateNestedMessages(mDescriptor, [this](const ::google::protobuf::Descriptor *nestedMessage) {
  71. MessageDeclarationPrinter nesterPrinter(nestedMessage, mPrinter);
  72. nesterPrinter.printClassDeclarationPrivate();
  73. });
  74. mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
  75. printComments(mDescriptor);
  76. printClassDeclarationBegin();
  77. printClassBody();
  78. encloseClass();
  79. printListType();
  80. }
  81. void MessageDeclarationPrinter::printCopyFunctionality()
  82. {
  83. assert(mDescriptor != nullptr);
  84. mPrinter->Print(mTypeMap,
  85. Templates::CopyConstructorDeclarationTemplate);
  86. mPrinter->Print(mTypeMap,
  87. Templates::AssignmentOperatorDeclarationTemplate);
  88. }
  89. void MessageDeclarationPrinter::printMoveSemantic()
  90. {
  91. assert(mDescriptor != nullptr);
  92. mPrinter->Print(mTypeMap,
  93. Templates::MoveConstructorDeclarationTemplate);
  94. mPrinter->Print(mTypeMap,
  95. Templates::MoveAssignmentOperatorDeclarationTemplate);
  96. }
  97. void MessageDeclarationPrinter::printComparisonOperators()
  98. {
  99. assert(mDescriptor != nullptr);
  100. mPrinter->Print(mTypeMap, Templates::EqualOperatorDeclarationTemplate);
  101. mPrinter->Print(mTypeMap, Templates::NotEqualOperatorDeclarationTemplate);
  102. }
  103. void MessageDeclarationPrinter::printConstructors()
  104. {
  105. for (int i = 0; i <= mDescriptor->field_count(); i++) {
  106. printConstructor(i);
  107. }
  108. if (mDescriptor->full_name() == std::string("google.protobuf.Timestamp")) {
  109. mPrinter->Print("Timestamp(const QDateTime &datetime, QObject *parent = nullptr);\n"
  110. "operator QDateTime() const;\n");
  111. }
  112. }
  113. void MessageDeclarationPrinter::printConstructor(int fieldCount)
  114. {
  115. std::vector<std::string> parameterList;
  116. mPrinter->Print(mTypeMap, Templates::ProtoConstructorBeginTemplate);
  117. for (int i = 0; i < fieldCount; i++) {
  118. const FieldDescriptor *field = mDescriptor->field(i);
  119. const char *parameterTemplate = Templates::ConstructorParameterTemplate;
  120. FieldDescriptor::Type fieldType = field->type();
  121. if (field->is_repeated() && !field->is_map()) {
  122. parameterTemplate = Templates::ConstructorRepeatedParameterTemplate;
  123. } else if (fieldType == FieldDescriptor::TYPE_BYTES
  124. || fieldType == FieldDescriptor::TYPE_STRING
  125. || fieldType == FieldDescriptor::TYPE_MESSAGE
  126. || field->is_map()) {
  127. parameterTemplate = Templates::ConstructorMessageParameterTemplate;
  128. }
  129. mPrinter->Print(common::producePropertyMap(field, mDescriptor), parameterTemplate);
  130. }
  131. mPrinter->Print(mTypeMap, Templates::ProtoConstructorEndTemplate);
  132. }
  133. void MessageDeclarationPrinter::printMaps()
  134. {
  135. Indent();
  136. for (int i = 0; i < mDescriptor->field_count(); i++) {
  137. const FieldDescriptor *field = mDescriptor->field(i);
  138. if (field->is_map()) {
  139. const Descriptor *type = field->message_type();
  140. const char *mapTemplate = type->field(1)->type() == FieldDescriptor::TYPE_MESSAGE ? Templates::MessageMapTypeUsingTemplate : Templates::MapTypeUsingTemplate;
  141. mPrinter->Print(common::producePropertyMap(field, mDescriptor), mapTemplate);
  142. }
  143. }
  144. Outdent();
  145. }
  146. void MessageDeclarationPrinter::printNested()
  147. {
  148. Indent();
  149. common::iterateNestedMessages(mDescriptor, [&](const ::google::protobuf::Descriptor *nestedMessage) {
  150. mPrinter->Print(common::produceMessageTypeMap(nestedMessage, mDescriptor), Templates::NestedMessageUsingTemplate);
  151. });
  152. Outdent();
  153. }
  154. void MessageDeclarationPrinter::printClassDeclarationBegin()
  155. {
  156. mPrinter->Print(mTypeMap, Templates::ProtoClassDeclarationBeginTemplate);
  157. }
  158. void MessageDeclarationPrinter::printMetaTypesDeclaration()
  159. {
  160. mPrinter->Print(mTypeMap,
  161. Templates::DeclareMetaTypeTemplate);
  162. mPrinter->Print(mTypeMap,
  163. Templates::DeclareComplexListTypeTemplate);
  164. if (GeneratorOptions::instance().hasQml()) {
  165. mPrinter->Print(mTypeMap,
  166. Templates::DeclareComplexQmlListTypeTemplate);
  167. }
  168. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, PropertyMap &propertyMap) {
  169. if (field->type() == FieldDescriptor::TYPE_ENUM
  170. && common::isLocalEnum(field->enum_type(), mDescriptor)) {
  171. mPrinter->Print(propertyMap, Templates::DeclareMetaTypeTemplate);
  172. mPrinter->Print(propertyMap, Templates::DeclareMetaTypeListTemplate);
  173. } else if (field->is_map()) {
  174. propertyMap["key_type_underscore"] = propertyMap["key_type"];
  175. utils::replace(propertyMap["key_type_underscore"], "::", "_");
  176. propertyMap["value_type_underscore"] = propertyMap["key_type"];
  177. utils::replace(propertyMap["value_type_underscore"], "::", "_");
  178. mPrinter->Print(propertyMap, Templates::DeclareMetaTypeMapTemplate);
  179. }
  180. });
  181. common::iterateNestedMessages(mDescriptor, [this](const Descriptor *nestedMessage) {
  182. MessageDeclarationPrinter nesterPrinter(nestedMessage, mPrinter);
  183. nesterPrinter.printMetaTypesDeclaration();
  184. });
  185. }
  186. void MessageDeclarationPrinter::printProperties()
  187. {
  188. assert(mDescriptor != nullptr);
  189. //private section
  190. Indent();
  191. for (int i = 0; i < mDescriptor->field_count(); i++) {
  192. const FieldDescriptor *field = mDescriptor->field(i);
  193. const char *propertyTemplate = Templates::PropertyTemplate;
  194. if (common::isPureMessage(field)) {
  195. propertyTemplate = Templates::MessagePropertyTemplate;
  196. } else if (common::hasQmlAlias(field)) {
  197. propertyTemplate = Templates::NonScriptablePropertyTemplate;
  198. } else if (field->is_repeated() && !field->is_map()) {
  199. // Non-message list properties don't require an extra QQmlListProperty to access
  200. // their data, so the property name should not contain the 'Data' suffix
  201. if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
  202. propertyTemplate = Templates::RepeatedMessagePropertyTemplate;
  203. } else {
  204. propertyTemplate = Templates::RepeatedPropertyTemplate;
  205. }
  206. }
  207. mPrinter->Print(common::producePropertyMap(field, mDescriptor), propertyTemplate);
  208. }
  209. //Generate extra QmlListProperty that is mapped to list
  210. for (int i = 0; i < mDescriptor->field_count(); i++) {
  211. const FieldDescriptor *field = mDescriptor->field(i);
  212. if (field->type() == FieldDescriptor::TYPE_MESSAGE && field->is_repeated() && !field->is_map()
  213. && GeneratorOptions::instance().hasQml()) {
  214. mPrinter->Print(common::producePropertyMap(field, mDescriptor), Templates::QmlListPropertyTemplate);
  215. } else if (common::hasQmlAlias(field)) {
  216. mPrinter->Print(common::producePropertyMap(field, mDescriptor), Templates::NonScriptableAliasPropertyTemplate);
  217. }
  218. }
  219. Outdent();
  220. }
  221. void MessageDeclarationPrinter::printGetters()
  222. {
  223. Indent();
  224. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  225. printComments(field);
  226. mPrinter->Print("\n");
  227. if (common::isPureMessage(field)) {
  228. mPrinter->Print(propertyMap, Templates::GetterMessageDeclarationTemplate);
  229. } else {
  230. mPrinter->Print(propertyMap, Templates::GetterTemplate);
  231. }
  232. if (field->is_repeated()) {
  233. mPrinter->Print(propertyMap, Templates::GetterContainerExtraTemplate);
  234. if (field->type() == FieldDescriptor::TYPE_MESSAGE && !field->is_map()
  235. && GeneratorOptions::instance().hasQml()) {
  236. mPrinter->Print(propertyMap, Templates::GetterQmlListDeclarationTemplate);
  237. }
  238. }
  239. });
  240. Outdent();
  241. }
  242. void MessageDeclarationPrinter::printSetters()
  243. {
  244. Indent();
  245. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  246. switch (field->type()) {
  247. case FieldDescriptor::TYPE_MESSAGE:
  248. if (!field->is_map() && !field->is_repeated() && !common::isQtType(field)) {
  249. mPrinter->Print(propertyMap, Templates::SetterTemplateDeclarationMessageType);
  250. } else {
  251. mPrinter->Print(propertyMap, Templates::SetterTemplateDeclarationComplexType);
  252. }
  253. break;
  254. case FieldDescriptor::FieldDescriptor::TYPE_STRING:
  255. case FieldDescriptor::FieldDescriptor::TYPE_BYTES:
  256. mPrinter->Print(propertyMap, Templates::SetterTemplateDeclarationComplexType);
  257. break;
  258. default:
  259. mPrinter->Print(propertyMap, Templates::SetterTemplate);
  260. break;
  261. }
  262. });
  263. Outdent();
  264. }
  265. void MessageDeclarationPrinter::printPrivateGetters()
  266. {
  267. Indent();
  268. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  269. if (common::isPureMessage(field)) {
  270. mPrinter->Print(propertyMap, Templates::GetterPrivateMessageDeclarationTemplate);
  271. }
  272. });
  273. Outdent();
  274. }
  275. void MessageDeclarationPrinter::printPrivateSetters()
  276. {
  277. Indent();
  278. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  279. if (common::isPureMessage(field)) {
  280. mPrinter->Print(propertyMap, Templates::SetterPrivateTemplateDeclarationMessageType);
  281. }
  282. });
  283. Outdent();
  284. }
  285. void MessageDeclarationPrinter::printSignals()
  286. {
  287. Indent();
  288. for (int i = 0; i < mDescriptor->field_count(); i++) {
  289. mPrinter->Print(common::producePropertyMap(mDescriptor->field(i), mDescriptor), Templates::SignalTemplate);
  290. }
  291. Outdent();
  292. }
  293. void MessageDeclarationPrinter::printPrivateMethods()
  294. {
  295. Indent();
  296. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  297. if (common::hasQmlAlias(field)) {
  298. mPrinter->Print(propertyMap, Templates::NonScriptableGetterTemplate);
  299. mPrinter->Print(propertyMap, Templates::NonScriptableSetterTemplate);
  300. }
  301. });
  302. Outdent();
  303. }
  304. void MessageDeclarationPrinter::printQEnums() {
  305. if (GeneratorOptions::instance().generateFieldEnum()) {
  306. printFieldEnum();
  307. Indent();
  308. mPrinter->Print({{"type", Templates::QtProtobufFieldEnum}}, Templates::QEnumTemplate);
  309. Outdent();
  310. mPrinter->Print("\n");
  311. }
  312. if (mDescriptor->enum_type_count() <= 0) {
  313. return;
  314. }
  315. Indent();
  316. for (int i = 0; i < mDescriptor->enum_type_count(); i++) {
  317. const auto enumDescr = mDescriptor->enum_type(i);
  318. auto typeMap = common::produceEnumTypeMap(enumDescr, mDescriptor);
  319. mPrinter->Print(typeMap, Templates::EnumDefinitionTemplate);
  320. Indent();
  321. for (int j = 0; j < enumDescr->value_count(); j++) {
  322. const auto valueDescr = enumDescr->value(j);
  323. mPrinter->Print({{"enumvalue", utils::upperCaseName(valueDescr->name())},
  324. {"value", std::to_string(valueDescr->number())}}, Templates::EnumFieldTemplate);
  325. }
  326. Outdent();
  327. mPrinter->Print(Templates::SemicolonBlockEnclosureTemplate);
  328. mPrinter->Print(typeMap, Templates::QEnumTemplate);
  329. }
  330. for (int i = 0; i < mDescriptor->enum_type_count(); i++) {
  331. const auto enumDescr = mDescriptor->enum_type(i);
  332. auto typeMap = common::produceEnumTypeMap(enumDescr, mDescriptor);
  333. mPrinter->Print(typeMap, Templates::EnumTypeRepeatedTemplate);
  334. }
  335. Outdent();
  336. }
  337. void MessageDeclarationPrinter::printClassBody()
  338. {
  339. printProperties();
  340. printPublicBlock();
  341. printQEnums();
  342. printNested();
  343. printMaps();
  344. Indent();
  345. printConstructors();
  346. printDestructor();
  347. printCopyFunctionality();
  348. printMoveSemantic();
  349. printComparisonOperators();
  350. Outdent();
  351. printGetters();
  352. printSetters();
  353. Indent();
  354. mPrinter->Print(mTypeMap, Templates::ManualRegistrationDeclaration);
  355. Outdent();
  356. printSignalsBlock();
  357. printSignals();
  358. printPrivateBlock();
  359. printPrivateGetters();
  360. printPrivateSetters();
  361. printPrivateMethods();
  362. printPrivateBlock();
  363. printClassMembers();
  364. }
  365. void MessageDeclarationPrinter::printListType()
  366. {
  367. mPrinter->Print(mTypeMap, Templates::ComplexListTypeUsingTemplate);
  368. }
  369. void MessageDeclarationPrinter::printClassMembers()
  370. {
  371. Indent();
  372. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
  373. if (common::isPureMessage(field)) {
  374. mPrinter->Print(propertyMap, Templates::ComplexMemberTemplate);
  375. } else if (field->is_repeated() && !field->is_map()) {
  376. mPrinter->Print(propertyMap, Templates::ListMemberTemplate);
  377. } else {
  378. mPrinter->Print(propertyMap, Templates::MemberTemplate);
  379. }
  380. });
  381. Outdent();
  382. }
  383. void MessageDeclarationPrinter::printDestructor()
  384. {
  385. mPrinter->Print(mTypeMap, "virtual ~$classname$();\n");
  386. }
  387. void MessageDeclarationPrinter::printFieldEnum()
  388. {
  389. Indent();
  390. mPrinter->Print(Templates::FieldEnumTemplate);
  391. Indent();
  392. common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *, const PropertyMap &propertyMap) {
  393. mPrinter->Print(propertyMap, Templates::FieldNumberTemplate);
  394. });
  395. Outdent();
  396. mPrinter->Print(Templates::SemicolonBlockEnclosureTemplate);
  397. Outdent();
  398. }