Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:00

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 // Author: jieluo@google.com (Jie Luo)
0009 //
0010 // Generates Python stub (.pyi) for a given .proto file.
0011 
0012 #ifndef GOOGLE_PROTOBUF_COMPILER_PYTHON_PYI_GENERATOR_H__
0013 #define GOOGLE_PROTOBUF_COMPILER_PYTHON_PYI_GENERATOR_H__
0014 
0015 #include <string>
0016 #include <vector>
0017 
0018 #include "absl/container/flat_hash_map.h"
0019 #include "absl/container/flat_hash_set.h"
0020 #include "absl/synchronization/mutex.h"
0021 #include "google/protobuf/compiler/code_generator.h"
0022 
0023 // Must be included last.
0024 #include "google/protobuf/port_def.inc"
0025 
0026 namespace google {
0027 namespace protobuf {
0028 class Descriptor;
0029 class EnumDescriptor;
0030 class FieldDescriptor;
0031 class MethodDescriptor;
0032 class ServiceDescriptor;
0033 
0034 namespace io {
0035 class Printer;
0036 }
0037 
0038 namespace compiler {
0039 namespace python {
0040 
0041 class PROTOC_EXPORT PyiGenerator : public google::protobuf::compiler::CodeGenerator {
0042  public:
0043   PyiGenerator();
0044   PyiGenerator(const PyiGenerator&) = delete;
0045   PyiGenerator& operator=(const PyiGenerator&) = delete;
0046   ~PyiGenerator() override;
0047 
0048   // CodeGenerator methods.
0049   uint64_t GetSupportedFeatures() const override {
0050     // Code generators must explicitly support proto3 optional.
0051     return Feature::FEATURE_PROTO3_OPTIONAL |
0052            Feature::FEATURE_SUPPORTS_EDITIONS;
0053   }
0054   bool Generate(const FileDescriptor* file, const std::string& parameter,
0055                 GeneratorContext* generator_context,
0056                 std::string* error) const override;
0057 
0058   Edition GetMinimumEdition() const override { return Edition::EDITION_PROTO2; }
0059   Edition GetMaximumEdition() const override { return Edition::EDITION_2023; }
0060   std::vector<const FieldDescriptor*> GetFeatureExtensions() const override {
0061     return {};
0062   }
0063 
0064  private:
0065   void PrintImportForDescriptor(const FileDescriptor& desc,
0066                                 absl::flat_hash_set<std::string>* seen_aliases,
0067                                 bool* has_importlib) const;
0068   template <typename DescriptorT>
0069   void Annotate(const std::string& label, const DescriptorT* descriptor) const;
0070   void PrintImports() const;
0071   void PrintTopLevelEnums() const;
0072   void PrintEnum(const EnumDescriptor& enum_descriptor) const;
0073   void PrintEnumValues(const EnumDescriptor& enum_descriptor,
0074                        bool is_classvar = false) const;
0075   template <typename DescriptorT>
0076   void PrintExtensions(const DescriptorT& descriptor) const;
0077   void PrintMessages() const;
0078   void PrintMessage(const Descriptor& message_descriptor, bool is_nested) const;
0079   void PrintServices() const;
0080   std::string GetFieldType(
0081       const FieldDescriptor& field_des, const Descriptor& containing_des) const;
0082   template <typename DescriptorT>
0083   std::string ModuleLevelName(const DescriptorT& descriptor) const;
0084   std::string PublicPackage() const;
0085   std::string InternalPackage() const;
0086 
0087   bool opensource_runtime_ = true;
0088 
0089   // Very coarse-grained lock to ensure that Generate() is reentrant.
0090   // Guards file_, printer_, and import_map_.
0091   mutable absl::Mutex mutex_;
0092   mutable const FileDescriptor* file_;  // Set in Generate().  Under mutex_.
0093   mutable io::Printer* printer_;        // Set in Generate().  Under mutex_.
0094   mutable bool strip_nonfunctional_codegen_ = false;  // Set in Generate().
0095   // import_map will be a mapping from filename to module alias, e.g.
0096   // "google3/foo/bar.py" -> "_bar"
0097   mutable absl::flat_hash_map<std::string, std::string> import_map_;
0098 };
0099 
0100 }  // namespace python
0101 }  // namespace compiler
0102 }  // namespace protobuf
0103 }  // namespace google
0104 
0105 #include "google/protobuf/port_undef.inc"
0106 
0107 #endif  // GOOGLE_PROTOBUF_COMPILER_PYTHON_PYI_GENERATOR_H__