1 /*
2  * Copyright 2017 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 #ifndef FLATBUFFERS_FLATC_H_
18 #define FLATBUFFERS_FLATC_H_
19 
20 #include <functional>
21 #include <limits>
22 #include <string>
23 
24 #include "flatbuffers/flatbuffers.h"
25 #include "flatbuffers/idl.h"
26 #include "flatbuffers/util.h"
27 
28 namespace flatbuffers {
29 
30 extern void LogCompilerWarn(const std::string &warn);
31 extern void LogCompilerError(const std::string &err);
32 
33 class FlatCompiler {
34  public:
35   // Output generator for the various programming languages and formats we
36   // support.
37   struct Generator {
38     typedef bool (*GenerateFn)(const flatbuffers::Parser &parser,
39                                const std::string &path,
40                                const std::string &file_name);
41     typedef std::string (*MakeRuleFn)(const flatbuffers::Parser &parser,
42                                       const std::string &path,
43                                       const std::string &file_name);
44 
45     GenerateFn generate;
46     const char *generator_opt_short;
47     const char *generator_opt_long;
48     const char *lang_name;
49     bool schema_only;
50     GenerateFn generateGRPC;
51     flatbuffers::IDLOptions::Language lang;
52     const char *generator_help;
53     MakeRuleFn make_rule;
54   };
55 
56   typedef void (*WarnFn)(const FlatCompiler *flatc, const std::string &warn,
57                          bool show_exe_name);
58 
59   typedef void (*ErrorFn)(const FlatCompiler *flatc, const std::string &err,
60                           bool usage, bool show_exe_name);
61 
62   // Parameters required to initialize the FlatCompiler.
63   struct InitParams {
InitParamsInitParams64     InitParams()
65         : generators(nullptr),
66           num_generators(0),
67           warn_fn(nullptr),
68           error_fn(nullptr) {}
69 
70     const Generator *generators;
71     size_t num_generators;
72     WarnFn warn_fn;
73     ErrorFn error_fn;
74   };
75 
FlatCompiler(const InitParams & params)76   explicit FlatCompiler(const InitParams &params) : params_(params) {}
77 
78   int Compile(int argc, const char **argv);
79 
80   std::string GetUsageString(const char *program_name) const;
81 
82  private:
83   void ParseFile(flatbuffers::Parser &parser, const std::string &filename,
84                  const std::string &contents,
85                  std::vector<const char *> &include_directories) const;
86 
87   void LoadBinarySchema(Parser &parser, const std::string &filename,
88                         const std::string &contents);
89 
90   void Warn(const std::string &warn, bool show_exe_name = true) const;
91 
92   void Error(const std::string &err, bool usage = true,
93              bool show_exe_name = true) const;
94 
95   InitParams params_;
96 };
97 
98 }  // namespace flatbuffers
99 
100 #endif  // FLATBUFFERS_FLATC_H_
101