python_generator.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: robinson@google.com (Will Robinson)
  31. //
  32. // Generates Python code for a given .proto file.
  33. #ifndef GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__
  34. #define GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__
  35. #include <string>
  36. #include <google/protobuf/compiler/code_generator.h>
  37. #include <google/protobuf/stubs/mutex.h>
  38. #include <google/protobuf/port_def.inc>
  39. namespace google {
  40. namespace protobuf {
  41. class Descriptor;
  42. class EnumDescriptor;
  43. class EnumValueDescriptor;
  44. class FieldDescriptor;
  45. class OneofDescriptor;
  46. class ServiceDescriptor;
  47. namespace io {
  48. class Printer;
  49. }
  50. namespace compiler {
  51. namespace python {
  52. // CodeGenerator implementation for generated Python protocol buffer classes.
  53. // If you create your own protocol compiler binary and you want it to support
  54. // Python output, you can do so by registering an instance of this
  55. // CodeGenerator with the CommandLineInterface in your main() function.
  56. class PROTOC_EXPORT Generator : public CodeGenerator {
  57. public:
  58. Generator();
  59. virtual ~Generator();
  60. // CodeGenerator methods.
  61. bool Generate(const FileDescriptor* file, const std::string& parameter,
  62. GeneratorContext* generator_context,
  63. std::string* error) const override;
  64. uint64_t GetSupportedFeatures() const override;
  65. private:
  66. void PrintImports() const;
  67. void PrintFileDescriptor() const;
  68. void PrintTopLevelEnums() const;
  69. void PrintAllNestedEnumsInFile() const;
  70. void PrintNestedEnums(const Descriptor& descriptor) const;
  71. void PrintEnum(const EnumDescriptor& enum_descriptor) const;
  72. void PrintTopLevelExtensions() const;
  73. void PrintFieldDescriptor(const FieldDescriptor& field,
  74. bool is_extension) const;
  75. void PrintFieldDescriptorsInDescriptor(
  76. const Descriptor& message_descriptor, bool is_extension,
  77. const std::string& list_variable_name, int (Descriptor::*CountFn)() const,
  78. const FieldDescriptor* (Descriptor::*GetterFn)(int)const) const;
  79. void PrintFieldsInDescriptor(const Descriptor& message_descriptor) const;
  80. void PrintExtensionsInDescriptor(const Descriptor& message_descriptor) const;
  81. void PrintMessageDescriptors() const;
  82. void PrintDescriptor(const Descriptor& message_descriptor) const;
  83. void PrintNestedDescriptors(const Descriptor& containing_descriptor) const;
  84. void PrintMessages() const;
  85. void PrintMessage(const Descriptor& message_descriptor,
  86. const std::string& prefix,
  87. std::vector<std::string>* to_register,
  88. bool is_nested) const;
  89. void PrintNestedMessages(const Descriptor& containing_descriptor,
  90. const std::string& prefix,
  91. std::vector<std::string>* to_register) const;
  92. void FixForeignFieldsInDescriptors() const;
  93. void FixForeignFieldsInDescriptor(
  94. const Descriptor& descriptor,
  95. const Descriptor* containing_descriptor) const;
  96. void FixForeignFieldsInField(const Descriptor* containing_type,
  97. const FieldDescriptor& field,
  98. const std::string& python_dict_name) const;
  99. void AddMessageToFileDescriptor(const Descriptor& descriptor) const;
  100. void AddEnumToFileDescriptor(const EnumDescriptor& descriptor) const;
  101. void AddExtensionToFileDescriptor(const FieldDescriptor& descriptor) const;
  102. void AddServiceToFileDescriptor(const ServiceDescriptor& descriptor) const;
  103. std::string FieldReferencingExpression(
  104. const Descriptor* containing_type, const FieldDescriptor& field,
  105. const std::string& python_dict_name) const;
  106. template <typename DescriptorT>
  107. void FixContainingTypeInDescriptor(
  108. const DescriptorT& descriptor,
  109. const Descriptor* containing_descriptor) const;
  110. void FixForeignFieldsInExtensions() const;
  111. void FixForeignFieldsInExtension(
  112. const FieldDescriptor& extension_field) const;
  113. void FixForeignFieldsInNestedExtensions(const Descriptor& descriptor) const;
  114. void PrintServices() const;
  115. void PrintServiceDescriptors() const;
  116. void PrintServiceDescriptor(const ServiceDescriptor& descriptor) const;
  117. void PrintServiceClass(const ServiceDescriptor& descriptor) const;
  118. void PrintServiceStub(const ServiceDescriptor& descriptor) const;
  119. void PrintDescriptorKeyAndModuleName(
  120. const ServiceDescriptor& descriptor) const;
  121. void PrintEnumValueDescriptor(const EnumValueDescriptor& descriptor) const;
  122. std::string OptionsValue(const std::string& serialized_options) const;
  123. bool GeneratingDescriptorProto() const;
  124. template <typename DescriptorT>
  125. std::string ModuleLevelDescriptorName(const DescriptorT& descriptor) const;
  126. std::string ModuleLevelMessageName(const Descriptor& descriptor) const;
  127. std::string ModuleLevelServiceDescriptorName(
  128. const ServiceDescriptor& descriptor) const;
  129. template <typename DescriptorT, typename DescriptorProtoT>
  130. void PrintSerializedPbInterval(const DescriptorT& descriptor,
  131. DescriptorProtoT& proto) const;
  132. void FixAllDescriptorOptions() const;
  133. void FixOptionsForField(const FieldDescriptor& field) const;
  134. void FixOptionsForOneof(const OneofDescriptor& oneof) const;
  135. void FixOptionsForEnum(const EnumDescriptor& descriptor) const;
  136. void FixOptionsForMessage(const Descriptor& descriptor) const;
  137. void CopyPublicDependenciesAliases(const std::string& copy_from,
  138. const FileDescriptor* file) const;
  139. // Very coarse-grained lock to ensure that Generate() is reentrant.
  140. // Guards file_, printer_ and file_descriptor_serialized_.
  141. mutable Mutex mutex_;
  142. mutable const FileDescriptor* file_; // Set in Generate(). Under mutex_.
  143. mutable std::string file_descriptor_serialized_;
  144. mutable io::Printer* printer_; // Set in Generate(). Under mutex_.
  145. mutable bool pure_python_workable_;
  146. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator);
  147. };
  148. } // namespace python
  149. } // namespace compiler
  150. } // namespace protobuf
  151. } // namespace google
  152. #include <google/protobuf/port_undef.inc>
  153. #endif // GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__