Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:30:25

0001 /*
0002  * Copyright 2017 Google Inc. All rights reserved.
0003  *
0004  * Licensed under the Apache License, Version 2.0 (the "License");
0005  * you may not use this file except in compliance with the License.
0006  * You may obtain a copy of the License at
0007  *
0008  *     http://www.apache.org/licenses/LICENSE-2.0
0009  *
0010  * Unless required by applicable law or agreed to in writing, software
0011  * distributed under the License is distributed on an "AS IS" BASIS,
0012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013  * See the License for the specific language governing permissions and
0014  * limitations under the License.
0015  */
0016 
0017 #ifndef FLATBUFFERS_FLATC_H_
0018 #define FLATBUFFERS_FLATC_H_
0019 
0020 #include <functional>
0021 #include <limits>
0022 #include <list>
0023 #include <memory>
0024 #include <string>
0025 
0026 #include "flatbuffers/code_generator.h"
0027 #include "flatbuffers/flatbuffers.h"
0028 #include "flatbuffers/idl.h"
0029 #include "flatbuffers/util.h"
0030 
0031 namespace flatbuffers {
0032 
0033 extern void LogCompilerWarn(const std::string &warn);
0034 extern void LogCompilerError(const std::string &err);
0035 
0036 struct FlatCOptions {
0037   IDLOptions opts;
0038 
0039   std::string program_name;
0040 
0041   std::string output_path;
0042 
0043   std::vector<std::string> filenames;
0044 
0045   std::list<std::string> include_directories_storage;
0046   std::vector<const char *> include_directories;
0047   std::vector<const char *> conform_include_directories;
0048   std::vector<bool> generator_enabled;
0049   size_t binary_files_from = std::numeric_limits<size_t>::max();
0050   std::string conform_to_schema;
0051   std::string annotate_schema;
0052   bool annotate_include_vector_contents = true;
0053   bool any_generator = false;
0054   bool print_make_rules = false;
0055   bool raw_binary = false;
0056   bool schema_binary = false;
0057   bool grpc_enabled = false;
0058   bool requires_bfbs = false;
0059   bool file_names_only = false;
0060 
0061   std::vector<std::shared_ptr<CodeGenerator>> generators;
0062 };
0063 
0064 struct FlatCOption {
0065   std::string short_opt;
0066   std::string long_opt;
0067   std::string parameter;
0068   std::string description;
0069 };
0070 
0071 class FlatCompiler {
0072  public:
0073   typedef void (*WarnFn)(const FlatCompiler *flatc, const std::string &warn,
0074                          bool show_exe_name);
0075 
0076   typedef void (*ErrorFn)(const FlatCompiler *flatc, const std::string &err,
0077                           bool usage, bool show_exe_name);
0078 
0079   // Parameters required to initialize the FlatCompiler.
0080   struct InitParams {
0081     InitParams() : warn_fn(nullptr), error_fn(nullptr) {}
0082 
0083     WarnFn warn_fn;
0084     ErrorFn error_fn;
0085   };
0086 
0087   explicit FlatCompiler(const InitParams &params) : params_(params) {}
0088 
0089   bool RegisterCodeGenerator(const FlatCOption &option,
0090                              std::shared_ptr<CodeGenerator> code_generator);
0091 
0092   int Compile(const FlatCOptions &options);
0093 
0094   std::string GetShortUsageString(const std::string &program_name) const;
0095   std::string GetUsageString(const std::string &program_name) const;
0096 
0097   // Parse the FlatC options from command line arguments.
0098   FlatCOptions ParseFromCommandLineArguments(int argc, const char **argv);
0099 
0100  private:
0101   void ParseFile(flatbuffers::Parser &parser, const std::string &filename,
0102                  const std::string &contents,
0103                  const std::vector<const char *> &include_directories) const;
0104 
0105   void LoadBinarySchema(Parser &parser, const std::string &filename,
0106                         const std::string &contents);
0107 
0108   void Warn(const std::string &warn, bool show_exe_name = true) const;
0109 
0110   void Error(const std::string &err, bool usage = true,
0111              bool show_exe_name = true) const;
0112 
0113   void AnnotateBinaries(const uint8_t *binary_schema,
0114                         uint64_t binary_schema_size,
0115                         const FlatCOptions &options);
0116 
0117   void ValidateOptions(const FlatCOptions &options);
0118 
0119   Parser GetConformParser(const FlatCOptions &options);
0120 
0121   std::unique_ptr<Parser> GenerateCode(const FlatCOptions &options,
0122                                        Parser &conform_parser);
0123 
0124   std::map<std::string, std::shared_ptr<CodeGenerator>> code_generators_;
0125 
0126   InitParams params_;
0127 };
0128 
0129 }  // namespace flatbuffers
0130 
0131 #endif  // FLATBUFFERS_FLATC_H_