python_generator.h 7.3 KB

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