Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:11:59

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
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   // Wrapper for some common state that is shared between file generations to
0034   // improve performance when more than one file is generated at a time.
0035   struct CommonState {
0036     // `include_custom_options` will cause any custom options to be included
0037     // in the calculations around files defining extensions.
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       // The minimal dependencies that cover all the dependencies with
0048       // extensions.
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,        // No special handing, require references to import then.
0103     kForceInclude,  // Always treat them as needed.
0104     kExclude,       // Never treat them as needed.
0105   };
0106   // `public_deps_handling` controls how the public imports in this file should
0107   // be handed.
0108   void DetermineNeededDeps(absl::flat_hash_set<const FileDescriptor*>* deps,
0109                            PublicDepsHandling public_deps_handling) const;
0110 
0111   bool HeadersUseForwardDeclarations() const {
0112     // The bundled protos (WKTs) don't make use of forward declarations.
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   // The first file_scoped_extension_count_ are the extensions at file level
0128   // scope. This can be less than file_->extension_count() when custom options
0129   // are being filtered away.
0130   size_t file_scoped_extension_count_;
0131   std::vector<std::unique_ptr<ExtensionGenerator>> extension_generators_;
0132 };
0133 
0134 }  // namespace objectivec
0135 }  // namespace compiler
0136 }  // namespace protobuf
0137 }  // namespace google
0138 
0139 #endif  // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_FILE_H__