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