• 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 <set>
40 #include <string>
41 
42 #include <google/protobuf/stubs/mutex.h>
43 #include <google/protobuf/compiler/code_generator.h>
44 
45 // Must be included last.
46 #include <google/protobuf/port_def.inc>
47 
48 namespace google {
49 namespace protobuf {
50 class Descriptor;
51 class EnumDescriptor;
52 class FieldDescriptor;
53 class MethodDescriptor;
54 class ServiceDescriptor;
55 
56 namespace io {
57 class Printer;
58 }
59 
60 namespace compiler {
61 namespace python {
62 
63 class PROTOC_EXPORT PyiGenerator : public google::protobuf::compiler::CodeGenerator {
64  public:
65   PyiGenerator();
66   ~PyiGenerator() override;
67 
68   // CodeGenerator methods.
GetSupportedFeatures()69   uint64_t GetSupportedFeatures() const override {
70     // Code generators must explicitly support proto3 optional.
71     return CodeGenerator::FEATURE_PROTO3_OPTIONAL;
72   }
73   bool Generate(const FileDescriptor* file, const std::string& parameter,
74                 GeneratorContext* generator_context,
75                 std::string* error) const override;
76 
77  private:
78   void PrintImportForDescriptor(const FileDescriptor& desc,
79                                 std::map<std::string, std::string>* import_map,
80                                 std::set<std::string>* seen_aliases) const;
81   void PrintImports(std::map<std::string, std::string>* item_map,
82                     std::map<std::string, std::string>* import_map) const;
83   void PrintEnum(const EnumDescriptor& enum_descriptor) const;
84   void AddEnumValue(const EnumDescriptor& enum_descriptor,
85                     std::map<std::string, std::string>* item_map,
86                     const std::map<std::string, std::string>& import_map) const;
87   void PrintTopLevelEnums() const;
88   template <typename DescriptorT>
89   void AddExtensions(const DescriptorT& descriptor,
90                      std::map<std::string, std::string>* item_map) const;
91   void PrintMessages(
92       const std::map<std::string, std::string>& import_map) const;
93   void PrintMessage(const Descriptor& message_descriptor, bool is_nested,
94                     const std::map<std::string, std::string>& import_map) const;
95   void PrintServices() const;
96   void PrintItemMap(const std::map<std::string, std::string>& item_map) const;
97   std::string GetFieldType(
98       const FieldDescriptor& field_des, const Descriptor& containing_des,
99       const std::map<std::string, std::string>& import_map) const;
100   template <typename DescriptorT>
101   std::string ModuleLevelName(
102       const DescriptorT& descriptor,
103       const std::map<std::string, std::string>& import_map) const;
104 
105   // Very coarse-grained lock to ensure that Generate() is reentrant.
106   // Guards file_ and printer_.
107   mutable Mutex mutex_;
108   mutable const FileDescriptor* file_;  // Set in Generate().  Under mutex_.
109   mutable io::Printer* printer_;        // Set in Generate().  Under mutex_.
110   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PyiGenerator);
111 };
112 
113 }  // namespace python
114 }  // namespace compiler
115 }  // namespace protobuf
116 }  // namespace google
117 
118 #include <google/protobuf/port_undef.inc>
119 
120 #endif  // GOOGLE_PROTOBUF_COMPILER_PYTHON_PYI_GENERATOR_H__
121