Home | History | Annotate | Download | only in python
      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 (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/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 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.
     62 class LIBPROTOC_EXPORT Generator : public CodeGenerator {
     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 
    164   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator);
    165 };
    166 
    167 }  // namespace python
    168 }  // namespace compiler
    169 }  // namespace protobuf
    170 
    171 }  // namespace google
    172 #endif  // GOOGLE_PROTOBUF_COMPILER_PYTHON_GENERATOR_H__
    173