idl_gen_fbs.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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: return type.struct_def->name;
  24. case BASE_TYPE_UNION: return type.enum_def->name;
  25. case BASE_TYPE_VECTOR: return "[" + GenType(type.VectorType()) + "]";
  26. default: return kTypeNames[type.base_type];
  27. }
  28. }
  29. // Generate a flatbuffer schema from the Parser's internal representation.
  30. std::string GenerateFBS(const Parser &parser, const std::string &file_name,
  31. const GeneratorOptions &opts) {
  32. std::string schema;
  33. schema += "// Generated from " + file_name + ".proto\n\n";
  34. if (opts.include_dependence_headers) {
  35. int num_includes = 0;
  36. for (auto it = parser.included_files_.begin();
  37. it != parser.included_files_.end(); ++it) {
  38. auto basename = flatbuffers::StripPath(
  39. flatbuffers::StripExtension(it->first));
  40. if (basename != file_name) {
  41. schema += "include \"" + basename + ".fbs\";\n";
  42. num_includes++;
  43. }
  44. }
  45. if (num_includes) schema += "\n";
  46. }
  47. schema += "namespace ";
  48. auto name_space = parser.namespaces_.back();
  49. for (auto it = name_space->components.begin();
  50. it != name_space->components.end(); ++it) {
  51. if (it != name_space->components.begin()) schema += ".";
  52. schema += *it;
  53. }
  54. schema += ";\n\n";
  55. // Generate code for all the enum declarations.
  56. for (auto it = parser.enums_.vec.begin();
  57. it != parser.enums_.vec.end(); ++it) {
  58. EnumDef &enum_def = **it;
  59. schema += "enum " + enum_def.name + " : ";
  60. schema += GenType(enum_def.underlying_type) + " {\n";
  61. for (auto it2 = enum_def.vals.vec.begin();
  62. it2 != enum_def.vals.vec.end(); ++it2) {
  63. auto &ev = **it2;
  64. schema += " " + ev.name + " = " + NumToString(ev.value) + ",\n";
  65. }
  66. schema += "}\n\n";
  67. }
  68. // Generate code for all structs/tables.
  69. for (auto it = parser.structs_.vec.begin();
  70. it != parser.structs_.vec.end(); ++it) {
  71. StructDef &struct_def = **it;
  72. schema += "table " + struct_def.name + " {\n";
  73. for (auto it2 = struct_def.fields.vec.begin();
  74. it2 != struct_def.fields.vec.end(); ++it2) {
  75. auto &field = **it2;
  76. schema += " " + field.name + ":" + GenType(field.value.type);
  77. if (field.value.constant != "0") schema += " = " + field.value.constant;
  78. if (field.required) schema += " (required)";
  79. schema += ";\n";
  80. }
  81. schema += "}\n\n";
  82. }
  83. return schema;
  84. }
  85. bool GenerateFBS(const Parser &parser,
  86. const std::string &path,
  87. const std::string &file_name,
  88. const GeneratorOptions &opts) {
  89. return SaveFile((path + file_name + ".fbs").c_str(),
  90. GenerateFBS(parser, file_name, opts), false);
  91. }
  92. } // namespace flatbuffers