File indexing completed on 2025-01-31 10:12:00
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 { 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
0090
0091 mutable absl::Mutex mutex_;
0092 mutable const FileDescriptor* file_;
0093 mutable io::Printer* printer_;
0094 mutable bool strip_nonfunctional_codegen_ = false;
0095
0096
0097 mutable absl::flat_hash_map<std::string, std::string> import_map_;
0098 };
0099
0100 }
0101 }
0102 }
0103 }
0104
0105 #include "google/protobuf/port_undef.inc"
0106
0107 #endif