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     if (parser.opts.proto_mode && !parser.opts.proto_namespace_suffix.empty()) {
68       // Since we know that all these namespaces come from a .proto, and all are
69       // being converted, we can simply apply this suffix to all of them.
70       ns.components.insert(ns.components.end() - ns.from_table,
71                            parser.opts.proto_namespace_suffix);
72     }
73   }
74 
75   std::string schema;
76   schema += "// Generated from " + file_name + ".proto\n\n";
77   if (parser.opts.include_dependence_headers) {
78     // clang-format off
79     int num_includes = 0;
80     for (auto it = parser.included_files_.begin();
81          it != parser.included_files_.end(); ++it) {
82       if (it->second.empty())
83         continue;
84       std::string basename;
85       if(parser.opts.keep_include_path) {
86         basename = flatbuffers::StripExtension(it->second);
87       } else {
88         basename = flatbuffers::StripPath(
89                 flatbuffers::StripExtension(it->second));
90       }
91       schema += "include \"" + basename + ".fbs\";\n";
92       num_includes++;
93     }
94     if (num_includes) schema += "\n";
95     // clang-format on
96   }
97   // Generate code for all the enum declarations.
98   const Namespace *last_namespace = nullptr;
99   for (auto enum_def_it = parser.enums_.vec.begin();
100        enum_def_it != parser.enums_.vec.end(); ++enum_def_it) {
101     EnumDef &enum_def = **enum_def_it;
102     if (parser.opts.include_dependence_headers && enum_def.generated) {
103       continue;
104     }
105     GenNameSpace(*enum_def.defined_namespace, &schema, &last_namespace);
106     GenComment(enum_def.doc_comment, &schema, nullptr);
107     if (enum_def.is_union)
108       schema += "union " + enum_def.name;
109     else
110       schema += "enum " + enum_def.name + " : ";
111     schema += GenType(enum_def.underlying_type, true) + " {\n";
112     for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
113       auto &ev = **it;
114       GenComment(ev.doc_comment, &schema, nullptr, "  ");
115       if (enum_def.is_union)
116         schema += "  " + GenType(ev.union_type) + ",\n";
117       else
118         schema += "  " + ev.name + " = " + enum_def.ToString(ev) + ",\n";
119     }
120     schema += "}\n\n";
121   }
122   // Generate code for all structs/tables.
123   for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end();
124        ++it) {
125     StructDef &struct_def = **it;
126     if (parser.opts.include_dependence_headers && struct_def.generated) {
127       continue;
128     }
129     GenNameSpace(*struct_def.defined_namespace, &schema, &last_namespace);
130     GenComment(struct_def.doc_comment, &schema, nullptr);
131     schema += "table " + struct_def.name + " {\n";
132     for (auto field_it = struct_def.fields.vec.begin();
133          field_it != struct_def.fields.vec.end(); ++field_it) {
134       auto &field = **field_it;
135       if (field.value.type.base_type != BASE_TYPE_UTYPE) {
136         GenComment(field.doc_comment, &schema, nullptr, "  ");
137         schema += "  " + field.name + ":" + GenType(field.value.type);
138         if (field.value.constant != "0") schema += " = " + field.value.constant;
139         if (field.IsRequired()) schema += " (required)";
140         schema += ";\n";
141       }
142     }
143     schema += "}\n\n";
144   }
145   return schema;
146 }
147 
GenerateFBS(const Parser & parser,const std::string & path,const std::string & file_name)148 bool GenerateFBS(const Parser &parser, const std::string &path,
149                  const std::string &file_name) {
150   return SaveFile((path + file_name + ".fbs").c_str(),
151                   GenerateFBS(parser, file_name), false);
152 }
153 
154 }  // namespace flatbuffers
155