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
24namespace flatbuffers {
25
26static 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
42static 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.
57std::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().begin(); it != enum_def.Vals().end(); ++it) {
100 auto &ev = **it;
101 GenComment(ev.doc_comment, &schema, nullptr, " ");
102 if (enum_def.is_union)
103 schema += " " + GenType(ev.union_type) + ",\n";
104 else
105 schema += " " + ev.name + " = " + NumToString(ev.value) + ",\n";
106 }
107 schema += "}\n\n";
108 }
109 // Generate code for all structs/tables.
110 for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end();
111 ++it) {
112 StructDef &struct_def = **it;
113 GenNameSpace(*struct_def.defined_namespace, &schema, &last_namespace);
114 GenComment(struct_def.doc_comment, &schema, nullptr);
115 schema += "table " + struct_def.name + " {\n";
116 for (auto field_it = struct_def.fields.vec.begin();
117 field_it != struct_def.fields.vec.end(); ++field_it) {
118 auto &field = **field_it;
119 if (field.value.type.base_type != BASE_TYPE_UTYPE) {
120 GenComment(field.doc_comment, &schema, nullptr, " ");
121 schema += " " + field.name + ":" + GenType(field.value.type);
122 if (field.value.constant != "0") schema += " = " + field.value.constant;
123 if (field.required) schema += " (required)";
124 schema += ";\n";
125 }
126 }
127 schema += "}\n\n";
128 }
129 return schema;
130}
131
132bool GenerateFBS(const Parser &parser, const std::string &path,
133 const std::string &file_name) {
134 return SaveFile((path + file_name + ".fbs").c_str(),
135 GenerateFBS(parser, file_name), false);
136}
137
138} // namespace flatbuffers
139