python_generator.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. enum class StripPrintDescriptor { kCreate, kFind };
  53. // CodeGenerator implementation for generated Python protocol buffer classes.
  54. // If you create your own protocol compiler binary and you want it to support
  55. // Python output, you can do so by registering an instance of this
  56. // CodeGenerator with the CommandLineInterface in your main() function.
  57. class PROTOC_EXPORT Generator : public CodeGenerator {
  58. public:
  59. Generator();
  60. virtual ~Generator();
  61. // CodeGenerator methods.
  62. bool Generate(const FileDescriptor* file, const std::string& parameter,
  63. GeneratorContext* generator_context,
  64. std::string* error) const override;
  65. uint64_t GetSupportedFeatures() const override;
  66. private:
  67. void PrintImports() const;
  68. void PrintFileDescriptor() const;
  69. void PrintTopLevelEnums() const;
  70. void PrintAllNestedEnumsInFile(StripPrintDescriptor print_mode) const;
  71. void PrintNestedEnums(const Descriptor& descriptor,
  72. StripPrintDescriptor print_mode) const;
  73. void PrintCreateEnum(const EnumDescriptor& enum_descriptor) const;
  74. void PrintFindEnum(const EnumDescriptor& enum_descriptor) const;
  75. void PrintTopLevelExtensions() const;
  76. void PrintFieldDescriptor(const FieldDescriptor& field,
  77. bool is_extension) const;
  78. void PrintFieldDescriptorsInDescriptor(
  79. const Descriptor& message_descriptor, bool is_extension,
  80. const std::string& list_variable_name, int (Descriptor::*CountFn)() const,
  81. const FieldDescriptor* (Descriptor::*GetterFn)(int)const) const;
  82. void PrintFieldsInDescriptor(const Descriptor& message_descriptor) const;
  83. void PrintExtensionsInDescriptor(const Descriptor& message_descriptor) const;
  84. void PrintMessageDescriptors(StripPrintDescriptor print_mode) const;
  85. void PrintCreateDescriptor(const Descriptor& message_descriptor) const;
  86. void PrintFindDescriptor(const Descriptor& message_descriptor) const;
  87. void PrintNestedDescriptors(const Descriptor& containing_descriptor,
  88. StripPrintDescriptor print_mode) const;
  89. void PrintMessages() const;
  90. void PrintMessage(const Descriptor& message_descriptor,
  91. const std::string& prefix,
  92. std::vector<std::string>* to_register,
  93. bool is_nested) const;
  94. void PrintNestedMessages(const Descriptor& containing_descriptor,
  95. const std::string& prefix,
  96. std::vector<std::string>* to_register) const;
  97. void FixForeignFieldsInDescriptors() const;
  98. void FixForeignFieldsInDescriptor(
  99. const Descriptor& descriptor,
  100. const Descriptor* containing_descriptor) const;
  101. void FixForeignFieldsInField(const Descriptor* containing_type,
  102. const FieldDescriptor& field,
  103. const std::string& python_dict_name) const;
  104. void AddMessageToFileDescriptor(const Descriptor& descriptor) const;
  105. void AddEnumToFileDescriptor(const EnumDescriptor& descriptor) const;
  106. void AddExtensionToFileDescriptor(const FieldDescriptor& descriptor) const;
  107. void AddServiceToFileDescriptor(const ServiceDescriptor& descriptor) const;
  108. std::string FieldReferencingExpression(
  109. const Descriptor* containing_type, const FieldDescriptor& field,
  110. const std::string& python_dict_name) const;
  111. template <typename DescriptorT>
  112. void FixContainingTypeInDescriptor(
  113. const DescriptorT& descriptor,
  114. const Descriptor* containing_descriptor) const;
  115. void FixForeignFieldsInExtensions() const;
  116. void FixForeignFieldsInExtension(
  117. const FieldDescriptor& extension_field) const;
  118. void FixForeignFieldsInNestedExtensions(const Descriptor& descriptor) const;
  119. void PrintServices() const;
  120. void PrintServiceDescriptors() const;
  121. void PrintServiceDescriptor(const ServiceDescriptor& descriptor) const;
  122. void PrintServiceClass(const ServiceDescriptor& descriptor) const;
  123. void PrintServiceStub(const ServiceDescriptor& descriptor) const;
  124. void PrintDescriptorKeyAndModuleName(
  125. const ServiceDescriptor& descriptor) const;
  126. void PrintEnumValueDescriptor(const EnumValueDescriptor& descriptor) const;
  127. std::string OptionsValue(const std::string& serialized_options) const;
  128. bool GeneratingDescriptorProto() const;
  129. template <typename DescriptorT>
  130. std::string ModuleLevelDescriptorName(const DescriptorT& descriptor) const;
  131. std::string ModuleLevelMessageName(const Descriptor& descriptor) const;
  132. std::string ModuleLevelServiceDescriptorName(
  133. const ServiceDescriptor& descriptor) const;
  134. template <typename DescriptorT, typename DescriptorProtoT>
  135. void PrintSerializedPbInterval(const DescriptorT& descriptor,
  136. DescriptorProtoT& proto,
  137. const std::string& name) const;
  138. void FixAllDescriptorOptions() const;
  139. void FixOptionsForField(const FieldDescriptor& field) const;
  140. void FixOptionsForOneof(const OneofDescriptor& oneof) const;
  141. void FixOptionsForEnum(const EnumDescriptor& descriptor) const;
  142. void FixOptionsForService(const ServiceDescriptor& descriptor) const;
  143. void FixOptionsForMessage(const Descriptor& descriptor) const;
  144. void SetSerializedPbInterval() const;
  145. void SetMessagePbInterval(const Descriptor& descriptor) const;
  146. void CopyPublicDependenciesAliases(const std::string& copy_from,
  147. const FileDescriptor* file) const;
  148. // Very coarse-grained lock to ensure that Generate() is reentrant.
  149. // Guards file_, printer_ and file_descriptor_serialized_.
  150. mutable Mutex mutex_;
  151. mutable const FileDescriptor* file_; // Set in Generate(). Under mutex_.
  152. mutable std::string file_descriptor_serialized_;
  153. mutable io::Printer* printer_; // Set in Generate(). Under mutex_.
  154. mutable bool pure_python_workable_;
  155. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator);
  156. };
  157. } // namespace python
  158. } // namespace compiler
  159. } // namespace protobuf
  160. } // namespace google
  161. #include <google/protobuf/port_undef.inc>
  162. #endif // GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__