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 31 // Author: robinson@google.com (Will Robinson) 32 // 33 // Generates Python code for a given .proto file. 34 35 #ifndef GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__ 36 #define GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__ 37 38 #include <string> 39 40 #include <google/protobuf/compiler/code_generator.h> 41 #include <google/protobuf/stubs/mutex.h> 42 43 #include <google/protobuf/port_def.inc> 44 45 namespace google { 46 namespace protobuf { 47 48 class Descriptor; 49 class EnumDescriptor; 50 class EnumValueDescriptor; 51 class FieldDescriptor; 52 class OneofDescriptor; 53 class ServiceDescriptor; 54 55 namespace io { 56 class Printer; 57 } 58 59 namespace compiler { 60 namespace python { 61 62 // CodeGenerator implementation for generated Python protocol buffer classes. 63 // If you create your own protocol compiler binary and you want it to support 64 // Python output, you can do so by registering an instance of this 65 // CodeGenerator with the CommandLineInterface in your main() function. 66 class PROTOC_EXPORT Generator : public CodeGenerator { 67 public: 68 Generator(); 69 virtual ~Generator(); 70 71 // CodeGenerator methods. 72 bool Generate(const FileDescriptor* file, const std::string& parameter, 73 GeneratorContext* generator_context, 74 std::string* error) const override; 75 76 uint64_t GetSupportedFeatures() const override; 77 78 private: 79 void PrintImports() const; 80 void PrintFileDescriptor() const; 81 void PrintTopLevelEnums() const; 82 void PrintAllNestedEnumsInFile() const; 83 void PrintNestedEnums(const Descriptor& descriptor) const; 84 void PrintEnum(const EnumDescriptor& enum_descriptor) const; 85 86 void PrintTopLevelExtensions() const; 87 88 void PrintFieldDescriptor(const FieldDescriptor& field, 89 bool is_extension) const; 90 void PrintFieldDescriptorsInDescriptor( 91 const Descriptor& message_descriptor, bool is_extension, 92 const std::string& list_variable_name, int (Descriptor::*CountFn)() const, 93 const FieldDescriptor* (Descriptor::*GetterFn)(int)const) const; 94 void PrintFieldsInDescriptor(const Descriptor& message_descriptor) const; 95 void PrintExtensionsInDescriptor(const Descriptor& message_descriptor) const; 96 void PrintMessageDescriptors() const; 97 void PrintDescriptor(const Descriptor& message_descriptor) const; 98 void PrintNestedDescriptors(const Descriptor& containing_descriptor) const; 99 100 void PrintMessages() const; 101 void PrintMessage(const Descriptor& message_descriptor, 102 const std::string& prefix, 103 std::vector<std::string>* to_register, 104 bool is_nested) const; 105 void PrintNestedMessages(const Descriptor& containing_descriptor, 106 const std::string& prefix, 107 std::vector<std::string>* to_register) const; 108 109 void FixForeignFieldsInDescriptors() const; 110 void FixForeignFieldsInDescriptor( 111 const Descriptor& descriptor, 112 const Descriptor* containing_descriptor) const; 113 void FixForeignFieldsInField(const Descriptor* containing_type, 114 const FieldDescriptor& field, 115 const std::string& python_dict_name) const; 116 void AddMessageToFileDescriptor(const Descriptor& descriptor) const; 117 void AddEnumToFileDescriptor(const EnumDescriptor& descriptor) const; 118 void AddExtensionToFileDescriptor(const FieldDescriptor& descriptor) const; 119 void AddServiceToFileDescriptor(const ServiceDescriptor& descriptor) const; 120 std::string FieldReferencingExpression( 121 const Descriptor* containing_type, const FieldDescriptor& field, 122 const std::string& python_dict_name) const; 123 template <typename DescriptorT> 124 void FixContainingTypeInDescriptor( 125 const DescriptorT& descriptor, 126 const Descriptor* containing_descriptor) const; 127 128 void FixForeignFieldsInExtensions() const; 129 void FixForeignFieldsInExtension( 130 const FieldDescriptor& extension_field) const; 131 void FixForeignFieldsInNestedExtensions(const Descriptor& descriptor) const; 132 133 void PrintServices() const; 134 void PrintServiceDescriptors() const; 135 void PrintServiceDescriptor(const ServiceDescriptor& descriptor) const; 136 void PrintServiceClass(const ServiceDescriptor& descriptor) const; 137 void PrintServiceStub(const ServiceDescriptor& descriptor) const; 138 void PrintDescriptorKeyAndModuleName( 139 const ServiceDescriptor& descriptor) const; 140 141 void PrintEnumValueDescriptor(const EnumValueDescriptor& descriptor) const; 142 std::string OptionsValue(const std::string& serialized_options) const; 143 bool GeneratingDescriptorProto() const; 144 145 template <typename DescriptorT> 146 std::string ModuleLevelDescriptorName(const DescriptorT& descriptor) const; 147 std::string ModuleLevelMessageName(const Descriptor& descriptor) const; 148 std::string ModuleLevelServiceDescriptorName( 149 const ServiceDescriptor& descriptor) const; 150 151 template <typename DescriptorT, typename DescriptorProtoT> 152 void PrintSerializedPbInterval(const DescriptorT& descriptor, 153 DescriptorProtoT& proto) const; 154 155 void FixAllDescriptorOptions() const; 156 void FixOptionsForField(const FieldDescriptor& field) const; 157 void FixOptionsForOneof(const OneofDescriptor& oneof) const; 158 void FixOptionsForEnum(const EnumDescriptor& descriptor) const; 159 void FixOptionsForMessage(const Descriptor& descriptor) const; 160 161 void CopyPublicDependenciesAliases(const std::string& copy_from, 162 const FileDescriptor* file) const; 163 164 // Very coarse-grained lock to ensure that Generate() is reentrant. 165 // Guards file_, printer_ and file_descriptor_serialized_. 166 mutable Mutex mutex_; 167 mutable const FileDescriptor* file_; // Set in Generate(). Under mutex_. 168 mutable std::string file_descriptor_serialized_; 169 mutable io::Printer* printer_; // Set in Generate(). Under mutex_. 170 mutable bool pure_python_workable_; 171 172 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator); 173 }; 174 175 } // namespace python 176 } // namespace compiler 177 } // namespace protobuf 178 } // namespace google 179 180 #include <google/protobuf/port_undef.inc> 181 182 #endif // GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__ 183