Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / externals / flatbuffers / flatc.h
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2017 Google Inc. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #ifndef FLATBUFFERS_FLATC_H_
19 #define FLATBUFFERS_FLATC_H_
20
21 #include <functional>
22 #include <limits>
23 #include <string>
24
25 #include "flatbuffers/flatbuffers.h"
26 #include "flatbuffers/idl.h"
27 #include "flatbuffers/util.h"
28
29 namespace flatbuffers
30 {
31
32 extern void LogCompilerWarn(const std::string &warn);
33 extern void LogCompilerError(const std::string &err);
34
35 class FlatCompiler
36 {
37 public:
38   // Output generator for the various programming languages and formats we
39   // support.
40   struct Generator
41   {
42     typedef bool (*GenerateFn)(const flatbuffers::Parser &parser, const std::string &path,
43                                const std::string &file_name);
44     typedef std::string (*MakeRuleFn)(const flatbuffers::Parser &parser, const std::string &path,
45                                       const std::string &file_name);
46
47     GenerateFn generate;
48     const char *generator_opt_short;
49     const char *generator_opt_long;
50     const char *lang_name;
51     bool schema_only;
52     GenerateFn generateGRPC;
53     flatbuffers::IDLOptions::Language lang;
54     const char *generator_help;
55     MakeRuleFn make_rule;
56   };
57
58   typedef void (*WarnFn)(const FlatCompiler *flatc, const std::string &warn, bool show_exe_name);
59
60   typedef void (*ErrorFn)(const FlatCompiler *flatc, const std::string &err, bool usage,
61                           bool show_exe_name);
62
63   // Parameters required to initialize the FlatCompiler.
64   struct InitParams
65   {
66     InitParams() : generators(nullptr), num_generators(0), warn_fn(nullptr), error_fn(nullptr) {}
67
68     const Generator *generators;
69     size_t num_generators;
70     WarnFn warn_fn;
71     ErrorFn error_fn;
72   };
73
74   explicit FlatCompiler(const InitParams &params) : params_(params) {}
75
76   int Compile(int argc, const char **argv);
77
78   std::string GetUsageString(const char *program_name) const;
79
80 private:
81   void ParseFile(flatbuffers::Parser &parser, const std::string &filename,
82                  const std::string &contents, std::vector<const char *> &include_directories) const;
83
84   void LoadBinarySchema(Parser &parser, const std::string &filename, const std::string &contents);
85
86   void Warn(const std::string &warn, bool show_exe_name = true) const;
87
88   void Error(const std::string &err, bool usage = true, bool show_exe_name = true) const;
89
90   InitParams params_;
91 };
92
93 } // namespace flatbuffers
94
95 #endif // FLATBUFFERS_FLATC_H_