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