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
17 // independent from idl_parser, since this code is not needed for most clients
18
19 #include "flatbuffers/code_generators.h"
20 #include "flatbuffers/flatbuffers.h"
21 #include "flatbuffers/idl.h"
22 #include "flatbuffers/util.h"
23
24 namespace flatbuffers {
25
GenType(const Type & type,bool underlying=false)26 static std::string GenType(const Type &type, bool underlying = false) {
27 switch (type.base_type) {
28 case BASE_TYPE_STRUCT:
29 return type.struct_def->defined_namespace->GetFullyQualifiedName(
30 type.struct_def->name);
31 case BASE_TYPE_VECTOR: return "[" + GenType(type.VectorType()) + "]";
32 default:
33 if (type.enum_def && !underlying) {
34 return type.enum_def->defined_namespace->GetFullyQualifiedName(
35 type.enum_def->name);
36 } else {
37 return kTypeNames[type.base_type];
38 }
39 }
40 }
41
GenNameSpace(const Namespace & name_space,std::string * _schema,const Namespace ** last_namespace)42 static void GenNameSpace(const Namespace &name_space, std::string *_schema,
43 const Namespace **last_namespace) {
44 if (*last_namespace == &name_space) return;
45 *last_namespace = &name_space;
46 auto &schema = *_schema;
47 schema += "namespace ";
48 for (auto it = name_space.components.begin();
49 it != name_space.components.end(); ++it) {
50 if (it != name_space.components.begin()) schema += ".";
51 schema += *it;
52 }
53 schema += ";\n\n";
54 }
55
56 // Generate a flatbuffer schema from the Parser's internal representation.
GenerateFBS(const Parser & parser,const std::string & file_name)57 std::string GenerateFBS(const Parser &parser, const std::string &file_name) {
58 // Proto namespaces may clash with table names, escape the ones that were
59 // generated from a table:
60 for (auto it = parser.namespaces_.begin(); it != parser.namespaces_.end();
61 ++it) {
62 auto &ns = **it;
63 for (size_t i = 0; i < ns.from_table; i++) {
64 ns.components[ns.components.size() - 1 - i] += "_";
65 }
66 }
67
68 std::string schema;
69 schema += "// Generated from " + file_name + ".proto\n\n";
70 if (parser.opts.include_dependence_headers) {
71 // clang-format off
72 #ifdef FBS_GEN_INCLUDES // TODO: currently all in one file.
73 int num_includes = 0;
74 for (auto it = parser.included_files_.begin();
75 it != parser.included_files_.end(); ++it) {
76 if (it->second.empty())
77 continue;
78 auto basename = flatbuffers::StripPath(
79 flatbuffers::StripExtension(it->second));
80 schema += "include \"" + basename + ".fbs\";\n";
81 num_includes++;
82 }
83 if (num_includes) schema += "\n";
84 #endif
85 // clang-format on
86 }
87 // Generate code for all the enum declarations.
88 const Namespace *last_namespace = nullptr;
89 for (auto enum_def_it = parser.enums_.vec.begin();
90 enum_def_it != parser.enums_.vec.end(); ++enum_def_it) {
91 EnumDef &enum_def = **enum_def_it;
92 GenNameSpace(*enum_def.defined_namespace, &schema, &last_namespace);
93 GenComment(enum_def.doc_comment, &schema, nullptr);
94 if (enum_def.is_union)
95 schema += "union " + enum_def.name;
96 else
97 schema += "enum " + enum_def.name + " : ";
98 schema += GenType(enum_def.underlying_type, true) + " {\n";
99 for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
100 ++it) {
101 auto &ev = **it;
102 GenComment(ev.doc_comment, &schema, nullptr, " ");
103 if (enum_def.is_union)
104 schema += " " + GenType(ev.union_type) + ",\n";
105 else
106 schema += " " + ev.name + " = " + NumToString(ev.value) + ",\n";
107 }
108 schema += "}\n\n";
109 }
110 // Generate code for all structs/tables.
111 for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end();
112 ++it) {
113 StructDef &struct_def = **it;
114 GenNameSpace(*struct_def.defined_namespace, &schema, &last_namespace);
115 GenComment(struct_def.doc_comment, &schema, nullptr);
116 schema += "table " + struct_def.name + " {\n";
117 for (auto field_it = struct_def.fields.vec.begin();
118 field_it != struct_def.fields.vec.end(); ++field_it) {
119 auto &field = **field_it;
120 if (field.value.type.base_type != BASE_TYPE_UTYPE) {
121 GenComment(field.doc_comment, &schema, nullptr, " ");
122 schema += " " + field.name + ":" + GenType(field.value.type);
123 if (field.value.constant != "0") schema += " = " + field.value.constant;
124 if (field.required) schema += " (required)";
125 schema += ";\n";
126 }
127 }
128 schema += "}\n\n";
129 }
130 return schema;
131 }
132
GenerateFBS(const Parser & parser,const std::string & path,const std::string & file_name)133 bool GenerateFBS(const Parser &parser, const std::string &path,
134 const std::string &file_name) {
135 return SaveFile((path + file_name + ".fbs").c_str(),
136 GenerateFBS(parser, file_name), false);
137 }
138
139 } // namespace flatbuffers
140