generatorcommon.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 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 "generatorcommon.h"
  26. #include "generatoroptions.h"
  27. #include <assert.h>
  28. using namespace ::QtProtobuf::generator;
  29. using namespace ::google::protobuf;
  30. std::string common::getNamespacesString(const std::vector<std::string> &namespacesList, const std::string &separator)
  31. {
  32. std::string namespaces;
  33. for (auto namespacePart : namespacesList) {
  34. namespaces += namespacePart + separator;
  35. }
  36. if (!namespaces.empty()) {
  37. namespaces.resize(namespaces.size() - separator.size());
  38. }
  39. return namespaces;
  40. }
  41. std::string common::getScopeNamespacesString(std::string original, const std::string &scope)
  42. {
  43. if (scope.empty()) {
  44. return original;
  45. }
  46. if (original == scope) {
  47. return "";
  48. }
  49. if (original.find(scope + "::") == 0) {
  50. original = original.replace(0, scope.size() + 2, ""); //Remove trailing :: as well
  51. }
  52. return original;
  53. }
  54. TypeMap common::produceQtTypeMap(const ::Descriptor *type, const Descriptor *scope)
  55. {
  56. std::vector<std::string> namespaceList = getNamespaces(type);
  57. std::string namespaces = getNamespacesString(namespaceList, "::");
  58. std::string scopeNamespaces = getScopeNamespacesString(namespaces, getNamespacesString(getNamespaces(scope), "::"));
  59. std::string qmlPackage = getNamespacesString(namespaceList, ".");
  60. std::string name = type->name();
  61. std::string fullName = name;
  62. std::string scopeName = name;
  63. std::string listName = std::string("QList<") + Templates::ListSuffix + ">";
  64. std::string fullListName = listName;
  65. std::string scopeListName = listName;
  66. return {
  67. {"type", name},
  68. {"full_type", fullName},
  69. {"scope_type", scopeName},
  70. {"list_type", listName},
  71. {"full_list_type", fullListName},
  72. {"scope_list_type", scopeListName},
  73. {"namespaces", namespaces},
  74. {"scope_namespaces", scopeNamespaces},
  75. {"qml_package", qmlPackage},
  76. {"property_type", fullName},
  77. {"property_list_type", fullListName},
  78. {"getter_type", scopeName},
  79. {"setter_type", scopeName}
  80. };
  81. }
  82. TypeMap common::produceMessageTypeMap(const ::Descriptor *type, const Descriptor *scope)
  83. {
  84. std::vector<std::string> namespaceList = getNamespaces(type);
  85. if (!GeneratorOptions::instance().extraNamespace().empty()) {
  86. namespaceList.insert(namespaceList.begin(), GeneratorOptions::instance().extraNamespace());
  87. }
  88. std::vector<std::string> nestedNamespaceList = namespaceList;
  89. if(isNested(type)) {
  90. nestedNamespaceList = getNestedNamespaces(type);
  91. }
  92. std::string namespaces = getNamespacesString(namespaceList, "::");
  93. std::string scopeNamespaces = getScopeNamespacesString(getNamespacesString(nestedNamespaceList, "::"), getNamespacesString(getNamespaces(scope), "::"));
  94. std::string qmlPackage = getNamespacesString(namespaceList, ".");
  95. std::string name = utils::upperCaseName(type->name());
  96. std::string fullName = namespaces.empty() ? name : (namespaces + "::" + name);
  97. std::string scopeName = scopeNamespaces.empty() ? name : (scopeNamespaces + "::" + name);
  98. std::string listName = name + Templates::ListSuffix;
  99. std::string fullListName = namespaces.empty() ? listName : (namespaces + "::" + listName);
  100. std::string scopeListName = scopeNamespaces.empty() ? listName : (scopeNamespaces + "::" + listName);
  101. return {
  102. {"classname", name},
  103. {"type", name},
  104. {"full_type", fullName},
  105. {"scope_type", scopeName},
  106. {"list_type", listName},
  107. {"full_list_type", fullListName},
  108. {"scope_list_type", scopeListName},
  109. {"namespaces", namespaces},
  110. {"scope_namespaces", scopeNamespaces},
  111. {"qml_package", qmlPackage},
  112. {"property_type", fullName},
  113. {"property_list_type", fullListName},
  114. {"getter_type", scopeName},
  115. {"setter_type", scopeName}
  116. };
  117. }
  118. TypeMap common::produceEnumTypeMap(const EnumDescriptor *type, const Descriptor *scope)
  119. {
  120. EnumVisibility visibility = enumVisibility(type, scope);
  121. std::vector<std::string> namespaceList = getNamespaces(type);
  122. if (!GeneratorOptions::instance().extraNamespace().empty()) {
  123. namespaceList.insert(namespaceList.begin(), GeneratorOptions::instance().extraNamespace());
  124. }
  125. std::string name = utils::upperCaseName(type->name());
  126. std::string qmlPackage = getNamespacesString(namespaceList, ".");//qml package should consist only from proto spackage
  127. std::string enumGadget = scope != nullptr ? utils::upperCaseName(scope->name()) : "";//Not used
  128. if (visibility == GLOBAL_ENUM) {
  129. enumGadget = name + Templates::EnumClassSuffix;
  130. namespaceList.push_back(enumGadget);//Global enums are stored in helper Gadget
  131. }
  132. std::string namespaces = getNamespacesString(namespaceList, "::");
  133. std::string scopeNamespaces = getScopeNamespacesString(namespaces, getNamespacesString(getNamespaces(scope), "::"));
  134. std::string fullName = namespaces.empty() ? name : (namespaces + "::" + name);
  135. std::string scopeName = scopeNamespaces.empty() ? name : (scopeNamespaces + "::" + name);
  136. std::string listName = name + Templates::ListSuffix;
  137. std::string fullListName = namespaces.empty() ? listName : (namespaces + "::" + listName);
  138. std::string scopeListName = scopeNamespaces.empty() ? listName: (scopeNamespaces + "::" + listName);
  139. std::string propertyType = fullName;
  140. if (visibility == LOCAL_ENUM) {
  141. //Note: For local enum classes it's impossible to use class name space in Q_PROPERTY
  142. //declaration. So please avoid addition of namespaces in line bellow
  143. propertyType = name;
  144. }
  145. return {
  146. {"classname", enumGadget},
  147. {"type", name},
  148. {"full_type", fullName},
  149. {"scope_type", scopeName},
  150. {"list_type", listName},
  151. {"full_list_type", fullListName},
  152. {"scope_list_type", scopeListName},
  153. {"namespaces", namespaces},
  154. {"scope_namespaces", scopeNamespaces},
  155. {"qml_package", qmlPackage},
  156. {"property_type", propertyType},
  157. {"property_list_type", fullListName},
  158. {"getter_type", scopeName},
  159. {"setter_type", scopeName},
  160. {"enum_gadget", enumGadget}
  161. };
  162. }
  163. TypeMap common::produceSimpleTypeMap(FieldDescriptor::Type type)
  164. {
  165. std::string namespaces;
  166. if (type != FieldDescriptor::TYPE_STRING
  167. && type != FieldDescriptor::TYPE_BYTES
  168. && type != FieldDescriptor::TYPE_BOOL
  169. && type != FieldDescriptor::TYPE_FLOAT
  170. && type != FieldDescriptor::TYPE_DOUBLE) {
  171. namespaces = Templates::QtProtobufNamespace;
  172. }
  173. std::string name;
  174. std::string qmlPackage = Templates::QtProtobufNamespace;
  175. auto it = Templates::TypeReflection.find(type);
  176. if (it != std::end(Templates::TypeReflection)) {
  177. name = it->second;
  178. } else {
  179. assert(name.empty());
  180. }
  181. std::string fullName = namespaces.empty() ? name : (namespaces + "::" + name);
  182. std::string scopeName = fullName;
  183. std::string listName = name + "List";
  184. if (type == FieldDescriptor::TYPE_FLOAT
  185. || type == FieldDescriptor::TYPE_DOUBLE
  186. || type == FieldDescriptor::TYPE_BOOL) {
  187. listName = utils::upperCaseName(name) + "List";
  188. }
  189. std::string fullListName = listName;
  190. if (type != FieldDescriptor::TYPE_STRING
  191. && type != FieldDescriptor::TYPE_BYTES) {
  192. fullListName = std::string(Templates::QtProtobufNamespace) + "::" + listName;
  193. }
  194. std::string scopeListName = fullListName;
  195. std::string qmlAliasType = fullName;
  196. switch (type) {
  197. case FieldDescriptor::TYPE_INT32:
  198. case FieldDescriptor::TYPE_SFIXED32:
  199. qmlAliasType = "int";
  200. break;
  201. case FieldDescriptor::TYPE_FIXED32:
  202. qmlAliasType = "unsigned int";
  203. break;
  204. default:
  205. //Do nothing
  206. break;
  207. }
  208. std::string getterType = fullName;
  209. if (type == FieldDescriptor::TYPE_INT32
  210. || type == FieldDescriptor::TYPE_FIXED32
  211. || type == FieldDescriptor::TYPE_SFIXED32
  212. || type == FieldDescriptor::TYPE_INT64
  213. || type == FieldDescriptor::TYPE_FIXED64
  214. || type == FieldDescriptor::TYPE_SFIXED64) {
  215. getterType = "const " + getterType;
  216. }
  217. return {{"type", name},
  218. {"full_type", fullName},
  219. {"scope_type", scopeName},
  220. {"list_type", listName},
  221. {"full_list_type", fullListName},
  222. {"scope_list_type", scopeListName},
  223. {"namespaces", namespaces},
  224. {"scope_namespaces", namespaces},
  225. {"qml_package", qmlPackage},
  226. {"property_type", fullName},
  227. {"qml_alias_type", qmlAliasType},
  228. {"property_list_type", fullListName},
  229. {"getter_type", getterType},
  230. {"setter_type", fullName}
  231. };
  232. }
  233. bool common::isQtType(const FieldDescriptor *field)
  234. {
  235. auto namespaces = getNamespaces(field->message_type());
  236. return namespaces.size() == 1 && namespaces[0] == "QtProtobuf"
  237. && field->file()->package() != "QtProtobuf"; //Used for qttypes library to avoid types conversion inside library
  238. }
  239. bool common::isPureMessage(const ::google::protobuf::FieldDescriptor *field)
  240. {
  241. return field->type() == FieldDescriptor::TYPE_MESSAGE && !field->is_map() && !field->is_repeated() && !common::isQtType(field);
  242. }
  243. TypeMap common::produceTypeMap(const FieldDescriptor *field, const Descriptor *scope)
  244. {
  245. TypeMap typeMap;
  246. assert(field != nullptr);
  247. std::string namespaceTypeName;
  248. std::vector<std::string> typeNamespace;
  249. switch (field->type()) {
  250. case FieldDescriptor::TYPE_MESSAGE: {
  251. if (isQtType(field)) {
  252. typeMap = produceQtTypeMap(field->message_type(), nullptr);
  253. } else {
  254. typeMap = produceMessageTypeMap(field->message_type(), scope);
  255. }
  256. }
  257. break;
  258. case FieldDescriptor::TYPE_ENUM:
  259. typeMap = produceEnumTypeMap(field->enum_type(), scope);
  260. break;
  261. default:
  262. typeMap = produceSimpleTypeMap(field->type());
  263. break;
  264. }
  265. return typeMap;
  266. }
  267. PropertyMap common::producePropertyMap(const FieldDescriptor *field, const Descriptor *scope)
  268. {
  269. assert(field != nullptr);
  270. PropertyMap propertyMap = produceTypeMap(field, scope);
  271. std::string scriptable = "true";
  272. if (!field->is_map() && !field->is_repeated() && (field->type() == FieldDescriptor::TYPE_INT64
  273. || field->type() == FieldDescriptor::TYPE_SINT64
  274. || field->type() == FieldDescriptor::TYPE_FIXED64
  275. || field->type() == FieldDescriptor::TYPE_SFIXED64)) {
  276. scriptable = "false";
  277. }
  278. std::string propertyName = qualifiedName(utils::lowerCaseName(field->camelcase_name()));
  279. std::string propertyNameCap = utils::upperCaseName(propertyName);
  280. propertyMap["property_name"] = propertyName;
  281. propertyMap["property_name_cap"] = propertyNameCap;
  282. propertyMap["scriptable"] = scriptable;
  283. auto scopeTypeMap = produceMessageTypeMap(scope, nullptr);
  284. propertyMap["key_type"] = "";
  285. propertyMap["value_type"] = "";
  286. propertyMap["classname"] = scope != nullptr ? scopeTypeMap["classname"] : "";
  287. propertyMap["number"] = std::to_string(field->number());
  288. if (field->is_map()) {
  289. const Descriptor *type = field->message_type();
  290. auto keyMap = common::producePropertyMap(type->field(0), scope);
  291. auto valueMap = common::producePropertyMap(type->field(1), scope);
  292. propertyMap["key_type"] = keyMap["scope_type"];
  293. propertyMap["value_type"] = valueMap["scope_type"];
  294. propertyMap["value_list_type"] = valueMap["scope_list_type"];
  295. } else if (field->is_repeated()) {
  296. propertyMap["getter_type"] = propertyMap["scope_list_type"];
  297. propertyMap["setter_type"] = propertyMap["scope_list_type"];
  298. }
  299. return propertyMap;
  300. }
  301. std::string common::qualifiedName(const std::string &name)
  302. {
  303. std::string fieldName(name);
  304. const std::vector<std::string> &searchExeptions = Templates::ListOfQmlExeptions;
  305. auto searchResult = std::find(searchExeptions.begin(), searchExeptions.end(), fieldName);
  306. if (searchResult != searchExeptions.end()) {
  307. return fieldName.append(Templates::ProtoSufix);
  308. }
  309. return fieldName;
  310. }
  311. bool common::isLocalEnum(const EnumDescriptor *type, const google::protobuf::Descriptor *scope)
  312. {
  313. if (scope == nullptr) {
  314. return false;
  315. }
  316. assert(type != nullptr);
  317. for (int i = 0; i < scope->enum_type_count(); i++) {
  318. const auto scopeEnum = scope->enum_type(i);
  319. if (scopeEnum && scopeEnum->full_name() == type->full_name()) {
  320. return true;
  321. }
  322. }
  323. return false;
  324. }
  325. common::EnumVisibility common::enumVisibility(const EnumDescriptor *type, const Descriptor *scope)
  326. {
  327. assert(type != nullptr);
  328. if (isLocalEnum(type, scope)) {
  329. return LOCAL_ENUM;
  330. }
  331. const FileDescriptor *typeFile = type->file();
  332. for (int i = 0; i < typeFile->message_type_count(); i++) {
  333. const Descriptor *msg = typeFile->message_type(i);
  334. for (int j = 0; j < msg->enum_type_count(); j++) {
  335. if (type->full_name() == msg->enum_type(j)->full_name()) {
  336. return NEIGHBOR_ENUM;
  337. }
  338. }
  339. }
  340. return GLOBAL_ENUM;
  341. }
  342. bool common::hasQmlAlias(const ::google::protobuf::FieldDescriptor *field)
  343. {
  344. return !field->is_map() && !field->is_repeated()
  345. && (field->type() == FieldDescriptor::TYPE_INT32
  346. || field->type() == FieldDescriptor::TYPE_SFIXED32
  347. || field->type() == FieldDescriptor::TYPE_FIXED32)
  348. && GeneratorOptions::instance().hasQml();
  349. }
  350. MethodMap common::produceMethodMap(const MethodDescriptor *method, const std::string& scope)
  351. {
  352. std::string inputTypeName = method->input_type()->full_name();
  353. std::string outputTypeName = method->output_type()->full_name();
  354. std::string methodName = method->name();
  355. std::string methodNameUpper = method->name();
  356. methodNameUpper[0] = static_cast<char>(::toupper(methodNameUpper[0]));
  357. utils::replace(inputTypeName, ".", "::");
  358. utils::replace(outputTypeName, ".", "::");
  359. return {{"classname", scope},
  360. {"return_type", outputTypeName},
  361. {"method_name", methodName},
  362. {"method_name_upper", methodNameUpper},
  363. {"param_type", inputTypeName},
  364. {"param_name", "arg"},
  365. {"return_name", "ret"}
  366. };
  367. }
  368. void common::iterateMessages(const ::google::protobuf::FileDescriptor *file, std::function<void(const ::google::protobuf::Descriptor *)> callback)
  369. {
  370. for (int i = 0; i < file->message_type_count(); i++) {
  371. callback(file->message_type(i));
  372. }
  373. }
  374. void common::iterateNestedMessages(const ::google::protobuf::Descriptor *message, std::function<void(const ::google::protobuf::Descriptor *)> callback)
  375. {
  376. for (int i = 0; i < message->nested_type_count(); i++) {
  377. auto nestedMessage = message->nested_type(i);
  378. if (message->field_count() <= 0) {
  379. callback(nestedMessage);
  380. continue;
  381. }
  382. for (int j = 0; j < message->field_count(); j++) {
  383. auto field = message->field(j);
  384. if (!field->is_map() && field->message_type() == nestedMessage) { //Probably there is more correct way to detect map in nested messages. //TODO: Have idea to make maps nested classes instead of typedefs.
  385. callback(nestedMessage);
  386. break;
  387. }
  388. }
  389. }
  390. }
  391. bool common::hasNestedMessages(const ::google::protobuf::Descriptor *message)
  392. {
  393. if (message->nested_type_count() > 0 && message->field_count() <= 0) {
  394. return true;
  395. }
  396. for (int i = 0; i < message->nested_type_count(); i++) {
  397. auto nestedMessage = message->nested_type(i);
  398. for (int j = 0; j < message->field_count(); j++) {
  399. auto field = message->field(j);
  400. if (!field->is_map() && field->message_type() == nestedMessage) { //Probably there is more correct way to detect map in nested messages. //TODO: Have idea to make maps nested classes instead of typedefs.
  401. return true;
  402. }
  403. }
  404. }
  405. return false;
  406. }
  407. bool common::isNested(const ::google::protobuf::Descriptor *message)
  408. {
  409. if (message->containing_type() == nullptr) {
  410. return false;
  411. }
  412. auto containingType = message->containing_type();
  413. bool isNested = true;
  414. for (int i = 0; i < containingType->field_count(); i++) {
  415. auto field = containingType->field(i);
  416. if (field->message_type() == message) {
  417. isNested = !field->is_map();
  418. break;
  419. }
  420. }
  421. return isNested;
  422. }
  423. const ::google::protobuf::Descriptor *common::findHighestMessage(const ::google::protobuf::Descriptor *message)
  424. {
  425. const ::google::protobuf::Descriptor *highestMessage = message;
  426. while (highestMessage->containing_type() != nullptr) {
  427. highestMessage = highestMessage->containing_type();
  428. }
  429. return highestMessage;
  430. }