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