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