generatorcommon.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 "templates.h"
  28. #include <assert.h>
  29. using namespace ::QtProtobuf::generator;
  30. using namespace ::google::protobuf;
  31. std::string common::getNamespacesString(const std::vector<std::string> &namespacesList, const std::string &separator)
  32. {
  33. std::string namespaces;
  34. for(auto namespacePart : namespacesList) {
  35. namespaces += namespacePart + separator;
  36. }
  37. if (!namespaces.empty()) {
  38. namespaces.resize(namespaces.size() - separator.size());
  39. }
  40. return namespaces;
  41. }
  42. std::string common::getScopeNamespacesString(std::string original, const std::string &scope)
  43. {
  44. if (scope.empty()) {
  45. return original;
  46. }
  47. if (original == scope) {
  48. return "";
  49. }
  50. if (original.find(scope + "::") == 0) {
  51. original = original.replace(0, scope.size() + 2, ""); //Remove trailing :: as well
  52. }
  53. return original;
  54. }
  55. TypeMap common::produceMessageTypeMap(const ::Descriptor *type, const Descriptor *scope)
  56. {
  57. std::vector<std::string> namespaceList = getNamespaces(type);
  58. std::string namespaces = getNamespacesString(namespaceList, "::");
  59. std::string scopeNamespaces = getScopeNamespacesString(namespaces, getNamespacesString(getNamespaces(scope), "::"));
  60. std::string qmlPackage = getNamespacesString(namespaceList, ".");
  61. std::string name = utils::upperCaseName(type->name());
  62. std::string fullName = namespaces.empty() ? name : (namespaces + "::" + name);
  63. std::string scopeName = scopeNamespaces.empty() ? name : (scopeNamespaces + "::" + name);
  64. std::string listName = name + Templates::ListSuffix;
  65. std::string fullListName = namespaces.empty() ? listName : (namespaces + "::" + listName);
  66. std::string scopeListName = scopeNamespaces.empty() ? listName : (scopeNamespaces + "::" + listName);
  67. return {
  68. {"type", name},
  69. {"full_type", fullName},
  70. {"scope_type", scopeName},
  71. {"list_type", listName},
  72. {"full_list_type", fullListName},
  73. {"scope_list_type", scopeListName},
  74. {"namespaces", namespaces},
  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::produceEnumTypeMap(const EnumDescriptor *type, const Descriptor *scope)
  83. {
  84. EnumVisibility visibility = enumVisibility(type, scope);
  85. std::vector<std::string> namespaceList = getNamespaces(type);
  86. if(visibility == GLOBAL_ENUM) {
  87. namespaceList.push_back(type->name() + Templates::EnumClassSuffix);//Global enums are stored in helper Gadget
  88. }
  89. std::string namespaces = getNamespacesString(namespaceList, "::");
  90. std::string scopeNamespaces = getScopeNamespacesString(namespaces, getNamespacesString(getNamespaces(scope), "::"));
  91. std::string qmlPackage = getNamespacesString(namespaceList, ".");
  92. std::string name = type->name();
  93. std::string fullName = namespaces.empty() ? name : (namespaces + "::" + name);
  94. std::string scopeName = scopeNamespaces.empty() ? name : (scopeNamespaces + "::" + name);
  95. std::string listName = name + Templates::ListSuffix;
  96. std::string fullListName = namespaces.empty() ? listName : (namespaces + "::" + listName);
  97. std::string scopeListName = scopeNamespaces.empty() ? listName: (scopeNamespaces + "::" + listName);
  98. std::string propertyType = fullName;
  99. if (visibility == LOCAL_ENUM) {
  100. //Note: For local enum classes it's impossible to use class name space in Q_PROPERTY
  101. //declaration. So please avoid addition of namespaces in line bellow
  102. propertyType = name;
  103. }
  104. return {
  105. {"type", name},
  106. {"full_type", fullName},
  107. {"scope_type", scopeName},
  108. {"list_type", listName},
  109. {"full_list_type", fullListName},
  110. {"scope_list_type", scopeListName},
  111. {"namespaces", namespaces},
  112. {"qml_package", qmlPackage},
  113. {"property_type", propertyType},
  114. {"property_list_type", fullListName},
  115. {"getter_type", scopeName},
  116. {"setter_type", scopeName}
  117. };
  118. }
  119. TypeMap common::produceSimpleTypeMap(FieldDescriptor::Type type)
  120. {
  121. std::string namespaces;
  122. if (type != FieldDescriptor::TYPE_STRING
  123. && type != FieldDescriptor::TYPE_BYTES
  124. && type != FieldDescriptor::TYPE_BOOL
  125. && type != FieldDescriptor::TYPE_FLOAT
  126. && type != FieldDescriptor::TYPE_DOUBLE) {
  127. namespaces = Templates::QtProtobufNamespace;
  128. }
  129. std::string name;
  130. std::string qmlPackage = Templates::QtProtobufNamespace;
  131. auto it = Templates::TypeReflection.find(type);
  132. if (it != std::end(Templates::TypeReflection)) {
  133. name = it->second;
  134. } else {
  135. assert(name.empty());
  136. }
  137. std::string fullName = namespaces.empty() ? name : (namespaces + "::" + name);
  138. std::string scopeName = fullName;
  139. std::string listName = name + "List";
  140. if (type == FieldDescriptor::TYPE_FLOAT
  141. || type == FieldDescriptor::TYPE_DOUBLE
  142. || type == FieldDescriptor::TYPE_BOOL) {
  143. listName = utils::upperCaseName(name) + "List";
  144. }
  145. std::string fullListName = listName;
  146. if (type != FieldDescriptor::TYPE_STRING
  147. && type != FieldDescriptor::TYPE_BYTES) {
  148. fullListName = std::string(Templates::QtProtobufNamespace) + "::" + listName;
  149. }
  150. std::string scopeListName = fullListName;
  151. std::string qmlAliasType = fullName;
  152. switch (type) {
  153. case FieldDescriptor::TYPE_INT32:
  154. case FieldDescriptor::TYPE_SFIXED32:
  155. qmlAliasType = "int";
  156. break;
  157. case FieldDescriptor::TYPE_FIXED32:
  158. qmlAliasType = "unsigned int";
  159. break;
  160. default:
  161. //Do nothing
  162. break;
  163. }
  164. std::string getterType = fullName;
  165. if (type == FieldDescriptor::TYPE_INT32
  166. || type == FieldDescriptor::TYPE_FIXED32
  167. || type == FieldDescriptor::TYPE_SFIXED32
  168. || type == FieldDescriptor::TYPE_INT64
  169. || type == FieldDescriptor::TYPE_FIXED64
  170. || type == FieldDescriptor::TYPE_SFIXED64) {
  171. getterType = "const " + getterType;
  172. }
  173. return {{"type", name},
  174. {"full_type", fullName},
  175. {"scope_type", scopeName},
  176. {"list_type", listName},
  177. {"full_list_type", fullListName},
  178. {"scope_list_type", scopeListName},
  179. {"namespaces", namespaces},
  180. {"qml_package", qmlPackage},
  181. {"property_type", fullName},
  182. {"qml_alias_type", qmlAliasType},
  183. {"property_list_type", fullListName},
  184. {"getter_type", getterType},
  185. {"setter_type", fullName}
  186. };
  187. }
  188. TypeMap common::produceTypeMap(const FieldDescriptor *field, const Descriptor *scope)
  189. {
  190. TypeMap typeMap;
  191. assert(field != nullptr);
  192. std::string namespaceTypeName;
  193. std::vector<std::string> typeNamespace;
  194. switch(field->type()) {
  195. case FieldDescriptor::TYPE_MESSAGE:
  196. typeMap = produceMessageTypeMap(field->message_type(), scope);
  197. break;
  198. case FieldDescriptor::TYPE_ENUM:
  199. typeMap = produceEnumTypeMap(field->enum_type(), scope);
  200. break;
  201. default:
  202. typeMap = produceSimpleTypeMap(field->type());
  203. break;
  204. }
  205. return typeMap;
  206. }
  207. PropertyMap common::producePropertyMap(const FieldDescriptor *field, const Descriptor *scope)
  208. {
  209. assert(field != nullptr);
  210. PropertyMap propertyMap = produceTypeMap(field, scope);
  211. std::string scriptable = "true";
  212. if (!field->is_map() && !field->is_repeated() && (field->type() == FieldDescriptor::TYPE_INT64
  213. || field->type() == FieldDescriptor::TYPE_SINT64
  214. || field->type() == FieldDescriptor::TYPE_FIXED64
  215. || field->type() == FieldDescriptor::TYPE_SFIXED64)) {
  216. scriptable = "false";
  217. }
  218. std::string propertyName = qualifiedName(utils::lowerCaseName(field->camelcase_name()));
  219. std::string propertyNameCap = utils::upperCaseName(propertyName);
  220. propertyMap["property_name"] = propertyName;
  221. propertyMap["property_name_cap"] = propertyNameCap;
  222. propertyMap["scriptable"] = scriptable;
  223. propertyMap["key_type"] = "";
  224. propertyMap["value_type"] = "";
  225. propertyMap["classname"] = scope != nullptr ? utils::upperCaseName(scope->name()) : "";
  226. if (field->is_map()) {
  227. const Descriptor *type = field->message_type();
  228. auto keyMap = common::producePropertyMap(type->field(0), scope);
  229. auto valueMap = common::producePropertyMap(type->field(1), scope);
  230. propertyMap["key_type"] = keyMap["scope_type"];
  231. propertyMap["value_type"] = valueMap["scope_type"];
  232. propertyMap["value_list_type"] = valueMap["scope_list_type"];
  233. } else if (field->is_repeated()) {
  234. propertyMap["getter_type"] = propertyMap["scope_list_type"];
  235. propertyMap["setter_type"] = propertyMap["scope_list_type"];
  236. }
  237. return propertyMap;
  238. }
  239. std::string common::qualifiedName(const std::string &name)
  240. {
  241. std::string fieldName(name);
  242. const std::vector<std::string> &searchExeptions = Templates::ListOfQmlExeptions;
  243. auto searchResult = std::find(searchExeptions.begin(), searchExeptions.end(), fieldName);
  244. if (searchResult != searchExeptions.end()) {
  245. return fieldName.append(Templates::ProtoSufix);
  246. }
  247. return fieldName;
  248. }
  249. bool common::isLocalEnum(const EnumDescriptor *type, const google::protobuf::Descriptor *scope)
  250. {
  251. if (scope == nullptr) {
  252. return false;
  253. }
  254. assert(type != nullptr);
  255. for (int i = 0; i < scope->enum_type_count(); i++) {
  256. const auto scopeEnum = scope->enum_type(i);
  257. if (scopeEnum && scopeEnum->full_name() == type->full_name()) {
  258. return true;
  259. }
  260. }
  261. return false;
  262. }
  263. common::EnumVisibility common::enumVisibility(const EnumDescriptor *type, const Descriptor *scope)
  264. {
  265. assert(type != nullptr);
  266. if (isLocalEnum(type, scope)) {
  267. return LOCAL_ENUM;
  268. }
  269. const FileDescriptor *typeFile = type->file();
  270. for (int i = 0; i < typeFile->message_type_count(); i++) {
  271. const Descriptor *msg = typeFile->message_type(i);
  272. for (int j = 0; j < msg->enum_type_count(); j++) {
  273. if (type->full_name() == msg->enum_type(j)->full_name()) {
  274. return NEIGHBOR_ENUM;
  275. }
  276. }
  277. }
  278. return GLOBAL_ENUM;
  279. }
  280. bool common::hasQmlAlias(const ::google::protobuf::FieldDescriptor *field)
  281. {
  282. return !field->is_map() && !field->is_repeated()
  283. && (field->type() == FieldDescriptor::TYPE_INT32
  284. || field->type() == FieldDescriptor::TYPE_SFIXED32
  285. || field->type() == FieldDescriptor::TYPE_FIXED32)
  286. && GeneratorOptions::instance().hasQml();
  287. }