• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   virtual bool Generate(const FileDescriptor* file,
73                         const std::string& parameter,
74                         GeneratorContext* generator_context,
75                         std::string* error) const;
76 
77  private:
78   void PrintImports() const;
79   void PrintFileDescriptor() const;
80   void PrintTopLevelEnums() const;
81   void PrintAllNestedEnumsInFile() const;
82   void PrintNestedEnums(const Descriptor& descriptor) const;
83   void PrintEnum(const EnumDescriptor& enum_descriptor) const;
84 
85   void PrintTopLevelExtensions() const;
86 
87   void PrintFieldDescriptor(const FieldDescriptor& field,
88                             bool is_extension) const;
89   void PrintFieldDescriptorsInDescriptor(
90       const Descriptor& message_descriptor, bool is_extension,
91       const std::string& list_variable_name, int (Descriptor::*CountFn)() const,
92       const FieldDescriptor* (Descriptor::*GetterFn)(int)const) const;
93   void PrintFieldsInDescriptor(const Descriptor& message_descriptor) const;
94   void PrintExtensionsInDescriptor(const Descriptor& message_descriptor) const;
95   void PrintMessageDescriptors() const;
96   void PrintDescriptor(const Descriptor& message_descriptor) const;
97   void PrintNestedDescriptors(const Descriptor& containing_descriptor) const;
98 
99   void PrintMessages() const;
100   void PrintMessage(const Descriptor& message_descriptor,
101                     const std::string& prefix,
102                     std::vector<std::string>* to_register,
103                     bool is_nested) const;
104   void PrintNestedMessages(const Descriptor& containing_descriptor,
105                            const std::string& prefix,
106                            std::vector<std::string>* to_register) const;
107 
108   void FixForeignFieldsInDescriptors() const;
109   void FixForeignFieldsInDescriptor(
110       const Descriptor& descriptor,
111       const Descriptor* containing_descriptor) const;
112   void FixForeignFieldsInField(const Descriptor* containing_type,
113                                const FieldDescriptor& field,
114                                const std::string& python_dict_name) const;
115   void AddMessageToFileDescriptor(const Descriptor& descriptor) const;
116   void AddEnumToFileDescriptor(const EnumDescriptor& descriptor) const;
117   void AddExtensionToFileDescriptor(const FieldDescriptor& descriptor) const;
118   void AddServiceToFileDescriptor(const ServiceDescriptor& descriptor) const;
119   std::string FieldReferencingExpression(
120       const Descriptor* containing_type, const FieldDescriptor& field,
121       const std::string& python_dict_name) const;
122   template <typename DescriptorT>
123   void FixContainingTypeInDescriptor(
124       const DescriptorT& descriptor,
125       const Descriptor* containing_descriptor) const;
126 
127   void FixForeignFieldsInExtensions() const;
128   void FixForeignFieldsInExtension(
129       const FieldDescriptor& extension_field) const;
130   void FixForeignFieldsInNestedExtensions(const Descriptor& descriptor) const;
131 
132   void PrintServices() const;
133   void PrintServiceDescriptors() const;
134   void PrintServiceDescriptor(const ServiceDescriptor& descriptor) const;
135   void PrintServiceClass(const ServiceDescriptor& descriptor) const;
136   void PrintServiceStub(const ServiceDescriptor& descriptor) const;
137   void PrintDescriptorKeyAndModuleName(
138       const ServiceDescriptor& descriptor) const;
139 
140   void PrintEnumValueDescriptor(const EnumValueDescriptor& descriptor) const;
141   std::string OptionsValue(const std::string& serialized_options) const;
142   bool GeneratingDescriptorProto() const;
143 
144   template <typename DescriptorT>
145   std::string ModuleLevelDescriptorName(const DescriptorT& descriptor) const;
146   std::string ModuleLevelMessageName(const Descriptor& descriptor) const;
147   std::string ModuleLevelServiceDescriptorName(
148       const ServiceDescriptor& descriptor) const;
149 
150   template <typename DescriptorT, typename DescriptorProtoT>
151   void PrintSerializedPbInterval(const DescriptorT& descriptor,
152                                  DescriptorProtoT& proto) const;
153 
154   void FixAllDescriptorOptions() const;
155   void FixOptionsForField(const FieldDescriptor& field) const;
156   void FixOptionsForOneof(const OneofDescriptor& oneof) const;
157   void FixOptionsForEnum(const EnumDescriptor& descriptor) const;
158   void FixOptionsForMessage(const Descriptor& descriptor) const;
159 
160   void CopyPublicDependenciesAliases(const std::string& copy_from,
161                                      const FileDescriptor* file) const;
162 
163   // Very coarse-grained lock to ensure that Generate() is reentrant.
164   // Guards file_, printer_ and file_descriptor_serialized_.
165   mutable Mutex mutex_;
166   mutable const FileDescriptor* file_;  // Set in Generate().  Under mutex_.
167   mutable std::string file_descriptor_serialized_;
168   mutable io::Printer* printer_;  // Set in Generate().  Under mutex_.
169 
170   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator);
171 };
172 
173 }  // namespace python
174 }  // namespace compiler
175 }  // namespace protobuf
176 }  // namespace google
177 
178 #include <google/protobuf/port_undef.inc>
179 
180 #endif  // GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__
181