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/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                         GeneratorContext* generator_context,
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 string& prefix,
98                     vector<string>* to_register) const;
99   void PrintNestedMessages(const Descriptor& containing_descriptor,
100                            const string& prefix,
101                            vector<string>* to_register) const;
102 
103   void FixForeignFieldsInDescriptors() const;
104   void FixForeignFieldsInDescriptor(
105       const Descriptor& descriptor,
106       const Descriptor* containing_descriptor) const;
107   void FixForeignFieldsInField(const Descriptor* containing_type,
108                                const FieldDescriptor& field,
109                                const string& python_dict_name) const;
110   void AddMessageToFileDescriptor(const Descriptor& descriptor) const;
111   void AddEnumToFileDescriptor(const EnumDescriptor& descriptor) const;
112   void AddExtensionToFileDescriptor(const FieldDescriptor& descriptor) const;
113   string FieldReferencingExpression(const Descriptor* containing_type,
114                                     const FieldDescriptor& field,
115                                     const string& python_dict_name) const;
116   template <typename DescriptorT>
117   void FixContainingTypeInDescriptor(
118       const DescriptorT& descriptor,
119       const Descriptor* containing_descriptor) const;
120 
121   void FixForeignFieldsInExtensions() const;
122   void FixForeignFieldsInExtension(
123       const FieldDescriptor& extension_field) const;
124   void FixForeignFieldsInNestedExtensions(const Descriptor& descriptor) const;
125 
126   void PrintServices() const;
127   void PrintServiceDescriptor(const ServiceDescriptor& descriptor) const;
128   void PrintServiceClass(const ServiceDescriptor& descriptor) const;
129   void PrintServiceStub(const ServiceDescriptor& descriptor) const;
130 
131   void PrintEnumValueDescriptor(const EnumValueDescriptor& descriptor) const;
132   string OptionsValue(const string& class_name,
133                       const string& serialized_options) const;
134   bool GeneratingDescriptorProto() const;
135 
136   template <typename DescriptorT>
137   string ModuleLevelDescriptorName(const DescriptorT& descriptor) const;
138   string ModuleLevelMessageName(const Descriptor& descriptor) const;
139   string ModuleLevelServiceDescriptorName(
140       const ServiceDescriptor& descriptor) const;
141 
142   template <typename DescriptorT, typename DescriptorProtoT>
143   void PrintSerializedPbInterval(
144       const DescriptorT& descriptor, DescriptorProtoT& proto) const;
145 
146   void FixAllDescriptorOptions() const;
147   void FixOptionsForField(const FieldDescriptor& field) const;
148   void FixOptionsForEnum(const EnumDescriptor& descriptor) const;
149   void FixOptionsForMessage(const Descriptor& descriptor) const;
150 
151   // Very coarse-grained lock to ensure that Generate() is reentrant.
152   // Guards file_, printer_ and file_descriptor_serialized_.
153   mutable Mutex mutex_;
154   mutable const FileDescriptor* file_;  // Set in Generate().  Under mutex_.
155   mutable string file_descriptor_serialized_;
156   mutable io::Printer* printer_;  // Set in Generate().  Under mutex_.
157 
158   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator);
159 };
160 
161 }  // namespace python
162 }  // namespace compiler
163 }  // namespace protobuf
164 
165 }  // namespace google
166 #endif  // GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__
167