File indexing completed on 2025-01-31 10:11:59
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FILE_H__
0009 #define GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FILE_H__
0010
0011 #include <cstddef>
0012 #include <functional>
0013 #include <memory>
0014 #include <string>
0015 #include <vector>
0016
0017 #include "absl/container/flat_hash_map.h"
0018 #include "absl/container/flat_hash_set.h"
0019 #include "google/protobuf/compiler/objectivec/enum.h"
0020 #include "google/protobuf/compiler/objectivec/extension.h"
0021 #include "google/protobuf/compiler/objectivec/message.h"
0022 #include "google/protobuf/compiler/objectivec/options.h"
0023 #include "google/protobuf/descriptor.h"
0024 #include "google/protobuf/io/printer.h"
0025
0026 namespace google {
0027 namespace protobuf {
0028 namespace compiler {
0029 namespace objectivec {
0030
0031 class FileGenerator {
0032 public:
0033
0034
0035 struct CommonState {
0036
0037
0038 explicit CommonState(bool include_custom_options)
0039 : include_custom_options(include_custom_options) {}
0040
0041 std::vector<const FileDescriptor*>
0042 CollectMinimalFileDepsContainingExtensions(const FileDescriptor* file);
0043
0044 private:
0045 struct MinDepsEntry {
0046 bool has_extensions;
0047
0048
0049 absl::flat_hash_set<const FileDescriptor*> min_deps;
0050 absl::flat_hash_set<const FileDescriptor*> transitive_deps;
0051 };
0052 const MinDepsEntry& CollectMinimalFileDepsContainingExtensionsInternal(
0053 const FileDescriptor* file);
0054 absl::flat_hash_map<const FileDescriptor*, MinDepsEntry> deps_info_cache;
0055 const bool include_custom_options;
0056 };
0057
0058 FileGenerator(Edition edition, const FileDescriptor* file,
0059 const GenerationOptions& generation_options,
0060 CommonState& common_state);
0061 ~FileGenerator() = default;
0062
0063 FileGenerator(const FileGenerator&) = delete;
0064 FileGenerator& operator=(const FileGenerator&) = delete;
0065
0066 void GenerateHeader(io::Printer* p) const;
0067 void GenerateSource(io::Printer* p) const;
0068
0069 int NumEnums() const { return enum_generators_.size(); }
0070 int NumMessages() const { return message_generators_.size(); }
0071
0072 void GenerateGlobalSource(io::Printer* p) const;
0073 void GenerateSourceForMessage(int idx, io::Printer* p) const;
0074 void GenerateSourceForEnums(io::Printer* p) const;
0075
0076 private:
0077 enum class GeneratedFileType : int { kHeader, kSource };
0078 struct GeneratedFileOptions {
0079 std::vector<std::string> ignored_warnings;
0080 std::vector<const FileDescriptor*> forced_files_to_import;
0081 std::vector<std::string> extra_system_headers;
0082 };
0083
0084 void GenerateFile(io::Printer* p, GeneratedFileType file_type,
0085 const GeneratedFileOptions& file_options,
0086 std::function<void()> body) const;
0087 void GenerateFile(io::Printer* p, GeneratedFileType file_type,
0088 std::function<void()> body) const {
0089 GeneratedFileOptions file_options;
0090 GenerateFile(p, file_type, file_options, body);
0091 }
0092
0093 void EmitRootImplementation(
0094 io::Printer* p,
0095 const std::vector<const FileDescriptor*>& deps_with_extensions) const;
0096 void EmitRootExtensionRegistryImplementation(
0097 io::Printer* p,
0098 const std::vector<const FileDescriptor*>& deps_with_extensions) const;
0099 void EmitFileDescription(io::Printer* p) const;
0100
0101 enum class PublicDepsHandling : int {
0102 kAsUsed,
0103 kForceInclude,
0104 kExclude,
0105 };
0106
0107
0108 void DetermineNeededDeps(absl::flat_hash_set<const FileDescriptor*>* deps,
0109 PublicDepsHandling public_deps_handling) const;
0110
0111 bool HeadersUseForwardDeclarations() const {
0112
0113 return !is_bundled_proto_ &&
0114 generation_options_.headers_use_forward_declarations;
0115 }
0116
0117 const Edition edition_;
0118 const FileDescriptor* file_;
0119 const GenerationOptions& generation_options_;
0120 mutable CommonState* common_state_;
0121 const std::string root_class_name_;
0122 const std::string file_description_name_;
0123 const bool is_bundled_proto_;
0124
0125 std::vector<std::unique_ptr<EnumGenerator>> enum_generators_;
0126 std::vector<std::unique_ptr<MessageGenerator>> message_generators_;
0127
0128
0129
0130 size_t file_scoped_extension_count_;
0131 std::vector<std::unique_ptr<ExtensionGenerator>> extension_generators_;
0132 };
0133
0134 }
0135 }
0136 }
0137 }
0138
0139 #endif