1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // http://code.google.com/p/protobuf/ 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 (at) 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/common.h> 42 43 namespace google { 44 namespace protobuf { 45 46 class Descriptor; 47 class EnumDescriptor; 48 class EnumValueDescriptor; 49 class FieldDescriptor; 50 class ServiceDescriptor; 51 52 namespace io { class Printer; } 53 54 namespace compiler { 55 namespace python { 56 57 // CodeGenerator implementation for generated Python protocol buffer classes. 58 // If you create your own protocol compiler binary and you want it to support 59 // Python output, you can do so by registering an instance of this 60 // CodeGenerator with the CommandLineInterface in your main() function. 61 class LIBPROTOC_EXPORT Generator : public CodeGenerator { 62 public: 63 Generator(); 64 virtual ~Generator(); 65 66 // CodeGenerator methods. 67 virtual bool Generate(const FileDescriptor* file, 68 const string& parameter, 69 OutputDirectory* output_directory, 70 string* error) const; 71 72 private: 73 void PrintImports() const; 74 void PrintFileDescriptor() const; 75 void PrintTopLevelEnums() const; 76 void PrintAllNestedEnumsInFile() const; 77 void PrintNestedEnums(const Descriptor& descriptor) const; 78 void PrintEnum(const EnumDescriptor& enum_descriptor) const; 79 80 void PrintTopLevelExtensions() const; 81 82 void PrintFieldDescriptor( 83 const FieldDescriptor& field, bool is_extension) const; 84 void PrintFieldDescriptorsInDescriptor( 85 const Descriptor& message_descriptor, 86 bool is_extension, 87 const string& list_variable_name, 88 int (Descriptor::*CountFn)() const, 89 const FieldDescriptor* (Descriptor::*GetterFn)(int) const) const; 90 void PrintFieldsInDescriptor(const Descriptor& message_descriptor) const; 91 void PrintExtensionsInDescriptor(const Descriptor& message_descriptor) const; 92 void PrintMessageDescriptors() const; 93 void PrintDescriptor(const Descriptor& message_descriptor) const; 94 void PrintNestedDescriptors(const Descriptor& containing_descriptor) const; 95 96 void PrintMessages() const; 97 void PrintMessage(const Descriptor& message_descriptor) const; 98 void PrintNestedMessages(const Descriptor& containing_descriptor) const; 99 100 void FixForeignFieldsInDescriptors() const; 101 void FixForeignFieldsInDescriptor( 102 const Descriptor& descriptor, 103 const Descriptor* containing_descriptor) const; 104 void FixForeignFieldsInField(const Descriptor* containing_type, 105 const FieldDescriptor& field, 106 const string& python_dict_name) const; 107 string FieldReferencingExpression(const Descriptor* containing_type, 108 const FieldDescriptor& field, 109 const string& python_dict_name) const; 110 template <typename DescriptorT> 111 void FixContainingTypeInDescriptor( 112 const DescriptorT& descriptor, 113 const Descriptor* containing_descriptor) const; 114 115 void FixForeignFieldsInExtensions() const; 116 void FixForeignFieldsInExtension( 117 const FieldDescriptor& extension_field) const; 118 void FixForeignFieldsInNestedExtensions(const Descriptor& descriptor) const; 119 120 void PrintServices() const; 121 void PrintServiceDescriptor(const ServiceDescriptor& descriptor) const; 122 void PrintServiceClass(const ServiceDescriptor& descriptor) const; 123 void PrintServiceStub(const ServiceDescriptor& descriptor) const; 124 125 void PrintEnumValueDescriptor(const EnumValueDescriptor& descriptor) const; 126 string OptionsValue(const string& class_name, 127 const string& serialized_options) const; 128 bool GeneratingDescriptorProto() const; 129 130 template <typename DescriptorT> 131 string ModuleLevelDescriptorName(const DescriptorT& descriptor) const; 132 string ModuleLevelMessageName(const Descriptor& descriptor) const; 133 string ModuleLevelServiceDescriptorName( 134 const ServiceDescriptor& descriptor) const; 135 136 template <typename DescriptorT, typename DescriptorProtoT> 137 void PrintSerializedPbInterval( 138 const DescriptorT& descriptor, DescriptorProtoT& proto) const; 139 140 // Very coarse-grained lock to ensure that Generate() is reentrant. 141 // Guards file_, printer_ and file_descriptor_serialized_. 142 mutable Mutex mutex_; 143 mutable const FileDescriptor* file_; // Set in Generate(). Under mutex_. 144 mutable string file_descriptor_serialized_; 145 mutable io::Printer* printer_; // Set in Generate(). Under mutex_. 146 147 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator); 148 }; 149 150 } // namespace python 151 } // namespace compiler 152 } // namespace protobuf 153 154 } // namespace google 155 #endif // GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__ 156