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