File indexing completed on 2026-09-25 09:15:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
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
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
0049 uint64_t GetSupportedFeatures() const override {
0050
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 {
0060 return Edition::EDITION_2024;
0061 }
0062 std::vector<const FieldDescriptor*> GetFeatureExtensions() const override {
0063 return {};
0064 }
0065
0066 private:
0067 void PrintImportForDescriptor(const FileDescriptor& desc,
0068 absl::flat_hash_set<std::string>* seen_aliases,
0069 bool* has_importlib) const;
0070 template <typename DescriptorT>
0071 void Annotate(const std::string& label, const DescriptorT* descriptor) const;
0072 void PrintImports() const;
0073 void PrintTopLevelEnums() const;
0074 void PrintEnum(const EnumDescriptor& enum_descriptor) const;
0075 void PrintEnumValues(const EnumDescriptor& enum_descriptor,
0076 bool is_classvar = false) const;
0077 template <typename DescriptorT>
0078 void PrintExtensions(const DescriptorT& descriptor) const;
0079 void PrintMessages() const;
0080 void PrintMessage(const Descriptor& message_descriptor, bool is_nested) const;
0081 void PrintServices() const;
0082 std::string GetFieldType(
0083 const FieldDescriptor& field_des, const Descriptor& containing_des) const;
0084 template <typename DescriptorT>
0085 std::string ModuleLevelName(const DescriptorT& descriptor) const;
0086 std::string PublicPackage() const;
0087 std::string InternalPackage() const;
0088 std::string ExtraInitTypes(const Descriptor& msg_des) const;
0089
0090 bool opensource_runtime_ = true;
0091
0092
0093
0094 mutable absl::Mutex mutex_;
0095 mutable const FileDescriptor* file_;
0096 mutable io::Printer* printer_;
0097 mutable bool strip_nonfunctional_codegen_ = false;
0098
0099
0100 mutable absl::flat_hash_map<std::string, std::string> import_map_;
0101 };
0102
0103 }
0104 }
0105 }
0106 }
0107
0108 #include "google/protobuf/port_undef.inc"
0109
0110 #endif