• 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: kenton@google.com (Kenton Varda)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 
35 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
36 #define GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
37 
38 #include <cstdint>
39 #include <map>
40 #include <memory>
41 #include <string>
42 
43 #include <google/protobuf/descriptor.h>
44 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
45 #include <google/protobuf/compiler/cpp/cpp_options.h>
46 
47 namespace google {
48 namespace protobuf {
49 namespace io {
50 class Printer;  // printer.h
51 }
52 }  // namespace protobuf
53 }  // namespace google
54 
55 namespace google {
56 namespace protobuf {
57 namespace compiler {
58 namespace cpp {
59 
60 // Helper function: set variables in the map that are the same for all
61 // field code generators.
62 // ['name', 'index', 'number', 'classname', 'declared_type', 'tag_size',
63 // 'deprecation'].
64 void SetCommonFieldVariables(const FieldDescriptor* descriptor,
65                              std::map<std::string, std::string>* variables,
66                              const Options& options);
67 
68 void SetCommonOneofFieldVariables(
69     const FieldDescriptor* descriptor,
70     std::map<std::string, std::string>* variables);
71 
72 class FieldGenerator {
73  public:
FieldGenerator(const FieldDescriptor * descriptor,const Options & options)74   explicit FieldGenerator(const FieldDescriptor* descriptor,
75                           const Options& options)
76       : descriptor_(descriptor), options_(options) {}
77   virtual ~FieldGenerator();
GenerateSerializeWithCachedSizes(io::Printer * printer)78   virtual void GenerateSerializeWithCachedSizes(
79       io::Printer* printer) const final{};
80   // Generate lines of code declaring members fields of the message class
81   // needed to represent this field.  These are placed inside the message
82   // class.
83   virtual void GeneratePrivateMembers(io::Printer* printer) const = 0;
84 
85   // Generate static default variable for this field. These are placed inside
86   // the message class. Most field types don't need this, so the default
87   // implementation is empty.
GenerateStaticMembers(io::Printer *)88   virtual void GenerateStaticMembers(io::Printer* /*printer*/) const {}
89 
90   // Generate prototypes for all of the accessor functions related to this
91   // field.  These are placed inside the class definition.
92   virtual void GenerateAccessorDeclarations(io::Printer* printer) const = 0;
93 
94   // Generate inline definitions of accessor functions for this field.
95   // These are placed inside the header after all class definitions.
96   virtual void GenerateInlineAccessorDefinitions(
97       io::Printer* printer) const = 0;
98 
99   // Generate definitions of accessors that aren't inlined.  These are
100   // placed somewhere in the .cc file.
101   // Most field types don't need this, so the default implementation is empty.
GenerateNonInlineAccessorDefinitions(io::Printer *)102   virtual void GenerateNonInlineAccessorDefinitions(
103       io::Printer* /*printer*/) const {}
104 
105   // Generate declarations of accessors that are for internal purposes only.
106   // Most field types don't need this, so the default implementation is empty.
GenerateInternalAccessorDefinitions(io::Printer *)107   virtual void GenerateInternalAccessorDefinitions(
108       io::Printer* /*printer*/) const {}
109 
110   // Generate definitions of accessors that are for internal purposes only.
111   // Most field types don't need this, so the default implementation is empty.
GenerateInternalAccessorDeclarations(io::Printer *)112   virtual void GenerateInternalAccessorDeclarations(
113       io::Printer* /*printer*/) const {}
114 
115   // Generate lines of code (statements, not declarations) which clear the
116   // field.  This is used to define the clear_$name$() method
117   virtual void GenerateClearingCode(io::Printer* printer) const = 0;
118 
119   // Generate lines of code (statements, not declarations) which clear the
120   // field as part of the Clear() method for the whole message.  For message
121   // types which have field presence bits, MessageGenerator::GenerateClear
122   // will have already checked the presence bits.
123   //
124   // Since most field types can re-use GenerateClearingCode, this method is
125   // not pure virtual.
GenerateMessageClearingCode(io::Printer * printer)126   virtual void GenerateMessageClearingCode(io::Printer* printer) const {
127     GenerateClearingCode(printer);
128   }
129 
130   // Generate lines of code (statements, not declarations) which merges the
131   // contents of the field from the current message to the target message,
132   // which is stored in the generated code variable "from".
133   // This is used to fill in the MergeFrom method for the whole message.
134   // Details of this usage can be found in message.cc under the
135   // GenerateMergeFrom method.
136   virtual void GenerateMergingCode(io::Printer* printer) const = 0;
137 
138   // Generates a copy constructor
139   virtual void GenerateCopyConstructorCode(io::Printer* printer) const = 0;
140 
141   // Generate lines of code (statements, not declarations) which swaps
142   // this field and the corresponding field of another message, which
143   // is stored in the generated code variable "other". This is used to
144   // define the Swap method. Details of usage can be found in
145   // message.cc under the GenerateSwap method.
146   virtual void GenerateSwappingCode(io::Printer* printer) const = 0;
147 
148   // Generate initialization code for private members declared by
149   // GeneratePrivateMembers(). These go into the message class's SharedCtor()
150   // method, invoked by each of the generated constructors.
151   virtual void GenerateConstructorCode(io::Printer* printer) const = 0;
152 
153   // Generate any code that needs to go in the class's SharedDtor() method,
154   // invoked by the destructor.
155   // Most field types don't need this, so the default implementation is empty.
GenerateDestructorCode(io::Printer *)156   virtual void GenerateDestructorCode(io::Printer* /*printer*/) const {}
157 
158   // Generate a manual destructor invocation for use when the message is on an
159   // arena. The code that this method generates will be executed inside a
160   // shared-for-the-whole-message-class method registered with
161   // OwnDestructor().
GenerateArenaDestructorCode(io::Printer * printer)162   virtual void GenerateArenaDestructorCode(io::Printer* printer) const {
163     GOOGLE_CHECK(NeedsArenaDestructor() == ArenaDtorNeeds::kNone)
164         << descriptor_->cpp_type_name();
165   }
166 
167   // Generate initialization code for private members declared by
168   // GeneratePrivateMembers(), specifically for the constexpr constructor.
169   // These go into the constructor's initializer list and must follow that
170   // syntax (eg `field_(args)`). Does not include `:` or `,` separators.
GenerateConstinitInitializer(io::Printer * printer)171   virtual void GenerateConstinitInitializer(io::Printer* printer) const {}
172 
173   // Generate lines to serialize this field directly to the array "target",
174   // which are placed within the message's SerializeWithCachedSizesToArray()
175   // method. This must also advance "target" past the written bytes.
176   virtual void GenerateSerializeWithCachedSizesToArray(
177       io::Printer* printer) const = 0;
178 
179   // Generate lines to compute the serialized size of this field, which
180   // are placed in the message's ByteSize() method.
181   virtual void GenerateByteSize(io::Printer* printer) const = 0;
182 
183   // Generates lines to call IsInitialized() for eligible message fields. Non
184   // message fields won't need to override this function.
GenerateIsInitialized(io::Printer * printer)185   virtual void GenerateIsInitialized(io::Printer* printer) const {}
186 
IsInlined()187   virtual bool IsInlined() const { return false; }
188 
NeedsArenaDestructor()189   virtual ArenaDtorNeeds NeedsArenaDestructor() const {
190     return ArenaDtorNeeds::kNone;
191   }
192 
193   void SetHasBitIndex(int32_t has_bit_index);
194   void SetInlinedStringIndex(int32_t inlined_string_index);
195 
196  protected:
197   const FieldDescriptor* descriptor_;
198   const Options& options_;
199   std::map<std::string, std::string> variables_;
200 
201  private:
202   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator);
203 };
204 
205 // Convenience class which constructs FieldGenerators for a Descriptor.
206 class FieldGeneratorMap {
207  public:
208   FieldGeneratorMap(const Descriptor* descriptor, const Options& options,
209                     MessageSCCAnalyzer* scc_analyzer);
210   ~FieldGeneratorMap();
211 
212   const FieldGenerator& get(const FieldDescriptor* field) const;
213 
SetHasBitIndices(const std::vector<int> & has_bit_indices_)214   void SetHasBitIndices(const std::vector<int>& has_bit_indices_) {
215     for (int i = 0; i < descriptor_->field_count(); ++i) {
216       field_generators_[i]->SetHasBitIndex(has_bit_indices_[i]);
217     }
218   }
219 
SetInlinedStringIndices(const std::vector<int> & inlined_string_indices)220   void SetInlinedStringIndices(const std::vector<int>& inlined_string_indices) {
221     for (int i = 0; i < descriptor_->field_count(); ++i) {
222       field_generators_[i]->SetInlinedStringIndex(inlined_string_indices[i]);
223     }
224   }
225 
226  private:
227   const Descriptor* descriptor_;
228   std::vector<std::unique_ptr<FieldGenerator>> field_generators_;
229 
230   static FieldGenerator* MakeGoogleInternalGenerator(
231       const FieldDescriptor* field, const Options& options,
232       MessageSCCAnalyzer* scc_analyzer);
233   static FieldGenerator* MakeGenerator(const FieldDescriptor* field,
234                                        const Options& options,
235                                        MessageSCCAnalyzer* scc_analyzer);
236 
237   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap);
238 };
239 
240 }  // namespace cpp
241 }  // namespace compiler
242 }  // namespace protobuf
243 }  // namespace google
244 
245 #endif  // GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__
246