messagedeclarationprinter.cpp 16 KB

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