• 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: jieluo@google.com (Jie Luo)
32 //
33 // Generates Python stub (.pyi) for a given .proto file.
34 
35 #ifndef GOOGLE_PROTOBUF_COMPILER_PYTHON_PYI_GENERATOR_H__
36 #define GOOGLE_PROTOBUF_COMPILER_PYTHON_PYI_GENERATOR_H__
37 
38 #include <map>
39 #include <string>
40 
41 #include <google/protobuf/compiler/code_generator.h>
42 #include <google/protobuf/stubs/mutex.h>
43 
44 // Must be included last.
45 #include <google/protobuf/port_def.inc>
46 
47 namespace google {
48 namespace protobuf {
49 class Descriptor;
50 class EnumDescriptor;
51 class FieldDescriptor;
52 class MethodDescriptor;
53 class ServiceDescriptor;
54 
55 namespace io {
56 class Printer;
57 }
58 
59 namespace compiler {
60 namespace python {
61 
62 class PROTOC_EXPORT PyiGenerator : public google::protobuf::compiler::CodeGenerator {
63  public:
64   PyiGenerator();
65   ~PyiGenerator() override;
66 
67   // CodeGenerator methods.
68   bool Generate(const FileDescriptor* file, const std::string& parameter,
69                 GeneratorContext* generator_context,
70                 std::string* error) const override;
71 
72  private:
73   void PrintImports(std::map<std::string, std::string>* item_map) const;
74   void PrintEnum(const EnumDescriptor& enum_descriptor) const;
75   void AddEnumValue(const EnumDescriptor& enum_descriptor,
76                     std::map<std::string, std::string>* item_map) const;
77   void PrintTopLevelEnums() const;
78   template <typename DescriptorT>
79   void AddExtensions(const DescriptorT& descriptor,
80                      std::map<std::string, std::string>* item_map) const;
81   void PrintMessages() const;
82   void PrintMessage(const Descriptor& message_descriptor, bool is_nested) const;
83   void PrintServices() const;
84   void PrintItemMap(const std::map<std::string, std::string>& item_map) const;
85   std::string GetFieldType(const FieldDescriptor& field_des) const;
86   template <typename DescriptorT>
87   std::string ModuleLevelName(const DescriptorT& descriptor) const;
88 
89   // Very coarse-grained lock to ensure that Generate() is reentrant.
90   // Guards file_ and printer_.
91   mutable Mutex mutex_;
92   mutable const FileDescriptor* file_;  // Set in Generate().  Under mutex_.
93   mutable io::Printer* printer_;        // Set in Generate().  Under mutex_.
94   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PyiGenerator);
95 };
96 
97 }  // namespace python
98 }  // namespace compiler
99 }  // namespace protobuf
100 }  // namespace google
101 
102 #include <google/protobuf/port_undef.inc>
103 
104 #endif  // GOOGLE_PROTOBUF_COMPILER_PYTHON_PYI_GENERATOR_H__
105