idl_gen_fbs.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 2014 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. // independent from idl_parser, since this code is not needed for most clients
  17. #include "flatbuffers/flatbuffers.h"
  18. #include "flatbuffers/idl.h"
  19. #include "flatbuffers/util.h"
  20. namespace flatbuffers {
  21. static std::string GenType(const Type &type) {
  22. switch (type.base_type) {
  23. case BASE_TYPE_STRUCT:
  24. return type.struct_def->defined_namespace->GetFullyQualifiedName(
  25. type.struct_def->name);
  26. case BASE_TYPE_UNION:
  27. return type.enum_def->defined_namespace->GetFullyQualifiedName(
  28. type.enum_def->name);
  29. case BASE_TYPE_VECTOR:
  30. return "[" + GenType(type.VectorType()) + "]";
  31. default:
  32. return kTypeNames[type.base_type];
  33. }
  34. }
  35. static void GenNameSpace(const Namespace &name_space, std::string *_schema,
  36. const Namespace **last_namespace) {
  37. if (*last_namespace == &name_space) return;
  38. *last_namespace = &name_space;
  39. auto &schema = *_schema;
  40. schema += "namespace ";
  41. for (auto it = name_space.components.begin();
  42. it != name_space.components.end(); ++it) {
  43. if (it != name_space.components.begin()) schema += ".";
  44. schema += *it;
  45. }
  46. schema += ";\n\n";
  47. }
  48. // Generate a flatbuffer schema from the Parser's internal representation.
  49. std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
  50. // Proto namespaces may clash with table names, so we have to prefix all:
  51. if (!parser.opts.escape_proto_identifiers) {
  52. for (auto it = parser.namespaces_.begin(); it != parser.namespaces_.end();
  53. ++it) {
  54. for (auto comp = (*it)->components.begin(); comp != (*it)->components.end();
  55. ++comp) {
  56. (*comp) = "_" + (*comp);
  57. }
  58. }
  59. }
  60. std::string schema;
  61. schema += "// Generated from " + file_name + ".proto\n\n";
  62. if (parser.opts.include_dependence_headers) {
  63. #ifdef FBS_GEN_INCLUDES // TODO: currently all in one file.
  64. int num_includes = 0;
  65. for (auto it = parser.included_files_.begin();
  66. it != parser.included_files_.end(); ++it) {
  67. auto basename = flatbuffers::StripPath(
  68. flatbuffers::StripExtension(it->first));
  69. if (basename != file_name) {
  70. schema += "include \"" + basename + ".fbs\";\n";
  71. num_includes++;
  72. }
  73. }
  74. if (num_includes) schema += "\n";
  75. #endif
  76. }
  77. // Generate code for all the enum declarations.
  78. const Namespace *last_namespace = nullptr;
  79. for (auto enum_def_it = parser.enums_.vec.begin();
  80. enum_def_it != parser.enums_.vec.end(); ++enum_def_it) {
  81. EnumDef &enum_def = **enum_def_it;
  82. GenNameSpace(*enum_def.defined_namespace, &schema, &last_namespace);
  83. GenComment(enum_def.doc_comment, &schema, nullptr);
  84. schema += "enum " + enum_def.name + " : ";
  85. schema += GenType(enum_def.underlying_type) + " {\n";
  86. for (auto it = enum_def.vals.vec.begin();
  87. it != enum_def.vals.vec.end(); ++it) {
  88. auto &ev = **it;
  89. GenComment(ev.doc_comment, &schema, nullptr, " ");
  90. schema += " " + ev.name + " = " + NumToString(ev.value) + ",\n";
  91. }
  92. schema += "}\n\n";
  93. }
  94. // Generate code for all structs/tables.
  95. for (auto it = parser.structs_.vec.begin();
  96. it != parser.structs_.vec.end(); ++it) {
  97. StructDef &struct_def = **it;
  98. GenNameSpace(*struct_def.defined_namespace, &schema, &last_namespace);
  99. GenComment(struct_def.doc_comment, &schema, nullptr);
  100. schema += "table " + struct_def.name + " {\n";
  101. for (auto field_it = struct_def.fields.vec.begin();
  102. field_it != struct_def.fields.vec.end(); ++field_it) {
  103. auto &field = **field_it;
  104. GenComment(field.doc_comment, &schema, nullptr, " ");
  105. schema += " " + field.name + ":" + GenType(field.value.type);
  106. if (field.value.constant != "0") schema += " = " + field.value.constant;
  107. if (field.required) schema += " (required)";
  108. schema += ";\n";
  109. }
  110. schema += "}\n\n";
  111. }
  112. return schema;
  113. }
  114. bool GenerateFBS(const Parser &parser,
  115. const std::string &path,
  116. const std::string &file_name) {
  117. return SaveFile((path + file_name + ".fbs").c_str(),
  118. GenerateFBS(parser, file_name), false);
  119. }
  120. } // namespace flatbuffers