File indexing completed on 2025-01-31 10:11:56
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_FILE_H__
0013 #define GOOGLE_PROTOBUF_COMPILER_CPP_FILE_H__
0014
0015 #include <functional>
0016 #include <memory>
0017 #include <string>
0018 #include <vector>
0019
0020 #include "absl/container/flat_hash_map.h"
0021 #include "absl/container/flat_hash_set.h"
0022 #include "absl/functional/any_invocable.h"
0023 #include "absl/log/absl_check.h"
0024 #include "google/protobuf/compiler/cpp/enum.h"
0025 #include "google/protobuf/compiler/cpp/extension.h"
0026 #include "google/protobuf/compiler/cpp/helpers.h"
0027 #include "google/protobuf/compiler/cpp/message.h"
0028 #include "google/protobuf/compiler/cpp/options.h"
0029 #include "google/protobuf/compiler/cpp/service.h"
0030 #include "google/protobuf/compiler/scc.h"
0031 #include "google/protobuf/descriptor.h"
0032 #include "google/protobuf/io/printer.h"
0033
0034
0035 #include "google/protobuf/port_def.inc"
0036
0037 namespace google {
0038 namespace protobuf {
0039 namespace compiler {
0040 namespace cpp {
0041 class PROTOC_EXPORT FileGenerator {
0042 public:
0043 FileGenerator(const FileDescriptor* file, const Options& options);
0044
0045 FileGenerator(const FileGenerator&) = delete;
0046 FileGenerator& operator=(const FileGenerator&) = delete;
0047
0048 ~FileGenerator() = default;
0049
0050
0051
0052 void GenerateProtoHeader(io::Printer* p, absl::string_view info_path);
0053
0054
0055 void GeneratePBHeader(io::Printer* p, absl::string_view info_path);
0056 void GenerateSource(io::Printer* p);
0057
0058
0059
0060
0061
0062
0063
0064 int NumMessages() const { return message_generators_.size(); }
0065 int NumExtensions() const { return extension_generators_.size(); }
0066
0067 void GenerateSourceForMessage(int idx, io::Printer* p);
0068
0069 void GenerateSourceForExtension(int idx, io::Printer* p);
0070
0071
0072 void GenerateGlobalSource(io::Printer* p);
0073
0074 private:
0075
0076
0077
0078
0079 void GenerateFile(io::Printer* p, GeneratedFileType file_type,
0080 std::function<void()> cb);
0081
0082
0083
0084
0085
0086 void GenerateStaticInitializer(io::Printer* p);
0087
0088
0089 void GenerateSharedHeaderCode(io::Printer* p);
0090
0091
0092 class ForwardDeclarations;
0093 struct CrossFileReferences;
0094
0095 void IncludeFile(absl::string_view google3_name, io::Printer* p) {
0096 DoIncludeFile(google3_name, false, p);
0097 }
0098 void IncludeFileAndExport(absl::string_view google3_name, io::Printer* p) {
0099 DoIncludeFile(google3_name, true, p);
0100 }
0101 void DoIncludeFile(absl::string_view google3_name, bool do_export,
0102 io::Printer* p);
0103
0104 std::string CreateHeaderInclude(absl::string_view basename,
0105 const FileDescriptor* file);
0106 void GetCrossFileReferencesForField(const FieldDescriptor* field,
0107 CrossFileReferences* refs);
0108 void GetCrossFileReferencesForFile(const FileDescriptor* file,
0109 CrossFileReferences* refs);
0110 void GenerateInternalForwardDeclarations(const CrossFileReferences& refs,
0111 io::Printer* p);
0112 void GenerateSourceIncludes(io::Printer* p);
0113 void GenerateSourcePrelude(io::Printer* p);
0114 void GenerateSourceDefaultInstance(int idx, io::Printer* p);
0115
0116 void GenerateInitForSCC(const SCC* scc, const CrossFileReferences& refs,
0117 io::Printer* p);
0118 void GenerateReflectionInitializationCode(io::Printer* p);
0119
0120
0121 void GenerateForwardDeclarations(io::Printer* p);
0122
0123
0124 void GenerateTopHeaderGuard(io::Printer* p, GeneratedFileType file_type);
0125 void GenerateBottomHeaderGuard(io::Printer* p, GeneratedFileType file_type);
0126
0127
0128 void GenerateLibraryIncludes(io::Printer* p);
0129 void GenerateDependencyIncludes(io::Printer* p);
0130
0131
0132
0133 void GenerateMetadataPragma(io::Printer* p, absl::string_view info_path);
0134
0135
0136 void GenerateGlobalStateFunctionDeclarations(io::Printer* p);
0137
0138
0139 void GenerateMessageDefinitions(io::Printer* p);
0140
0141 void GenerateEnumDefinitions(io::Printer* p);
0142
0143
0144 void GenerateServiceDefinitions(io::Printer* p);
0145
0146
0147 void GenerateExtensionIdentifiers(io::Printer* p);
0148
0149
0150 void GenerateInlineFunctionDefinitions(io::Printer* p);
0151
0152 void GenerateProto2NamespaceEnumSpecializations(io::Printer* p);
0153
0154
0155
0156
0157
0158
0159
0160
0161 void GenerateMacroUndefs(io::Printer* p);
0162
0163
0164 bool ShouldSkipDependencyImports(const FileDescriptor* dep) const;
0165
0166 bool IsDepWeak(const FileDescriptor* dep) const {
0167 if (weak_deps_.count(dep) != 0) {
0168 ABSL_CHECK(!options_.opensource_runtime);
0169 return true;
0170 }
0171 return false;
0172 }
0173
0174
0175 std::vector<const Descriptor*> MessagesInTopologicalOrder() const;
0176 friend class FileGeneratorFriendForTesting;
0177
0178 absl::flat_hash_set<const FileDescriptor*> weak_deps_;
0179
0180 std::vector<absl::AnyInvocable<void(io::Printer*)>> static_initializers_[2];
0181
0182 const FileDescriptor* file_;
0183 Options options_;
0184
0185 MessageSCCAnalyzer scc_analyzer_;
0186
0187
0188
0189
0190 absl::flat_hash_map<absl::string_view, std::string> variables_;
0191
0192
0193
0194 std::vector<std::unique_ptr<MessageGenerator>> message_generators_;
0195 std::vector<int> message_generators_topologically_ordered_;
0196 std::vector<std::unique_ptr<EnumGenerator>> enum_generators_;
0197 std::vector<std::unique_ptr<ServiceGenerator>> service_generators_;
0198 std::vector<std::unique_ptr<ExtensionGenerator>> extension_generators_;
0199 };
0200
0201 }
0202 }
0203 }
0204 }
0205
0206 #include "google/protobuf/port_undef.inc"
0207
0208 #endif