| 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/helpers.h> | 
| 45 | #include <google/protobuf/compiler/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: | 
| 74 |   explicit FieldGenerator(const FieldDescriptor* descriptor, | 
| 75 |                           const Options& options) | 
| 76 |       : descriptor_(descriptor), options_(options) {} | 
| 77 |   virtual ~FieldGenerator(); | 
| 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. | 
| 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. | 
| 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. | 
| 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. | 
| 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. | 
| 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; | 
| 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 initialization code for private members in the cold struct. | 
| 154 |   virtual void GenerateCreateSplitMessageCode(io::Printer* printer) const {} | 
| 155 |  | 
| 156 |   // Generate any code that needs to go in the class's SharedDtor() method, | 
| 157 |   // invoked by the destructor. | 
| 158 |   // Most field types don't need this, so the default implementation is empty. | 
| 159 |   virtual void GenerateDestructorCode(io::Printer* /*printer*/) const {} | 
| 160 |  | 
| 161 |   // Generate a manual destructor invocation for use when the message is on an | 
| 162 |   // arena. The code that this method generates will be executed inside a | 
| 163 |   // shared-for-the-whole-message-class method registered with | 
| 164 |   // OwnDestructor(). | 
| 165 |   virtual void GenerateArenaDestructorCode(io::Printer* printer) const { | 
| 166 |     GOOGLE_CHECK(NeedsArenaDestructor() == ArenaDtorNeeds::kNone) | 
| 167 |         << descriptor_->cpp_type_name(); | 
| 168 |   } | 
| 169 |  | 
| 170 |   // Generate initialization code for private members declared by | 
| 171 |   // GeneratePrivateMembers(). These go into the SharedCtor's | 
| 172 |   // aggregate initialization of the _impl_ struct and must follow the syntax | 
| 173 |   // (e.g. `decltype($field$){$default$}`). Does not include `:` or `,` | 
| 174 |   // separators.  Default values should be specified here when possible. | 
| 175 |   // | 
| 176 |   // Note: We use `decltype($field$)` for both explicit construction and the | 
| 177 |   // fact that it's self-documenting.  Pre-C++17, copy elision isn't guaranteed | 
| 178 |   // in aggregate initialization so a valid copy/move constructor must exist | 
| 179 |   // (even though it's not used).  Because of this, we need to comment out the | 
| 180 |   // decltype and fallback to implicit construction. | 
| 181 |   virtual void GenerateAggregateInitializer(io::Printer* printer) const; | 
| 182 |  | 
| 183 |   // Generate constinit initialization code for private members declared by | 
| 184 |   // GeneratePrivateMembers(). These go into the constexpr constructor's | 
| 185 |   // aggregate initialization of the _impl_ struct and must follow the syntax | 
| 186 |   // (e.g. `/*decltype($field$)*/{}`, see above). Does not | 
| 187 |   // include `:` or `,` separators. | 
| 188 |   virtual void GenerateConstexprAggregateInitializer( | 
| 189 |       io::Printer* printer) const; | 
| 190 |  | 
| 191 |   // Generate copy initialization code for private members declared by | 
| 192 |   // GeneratePrivateMembers(). These go into the copy constructor's | 
| 193 |   // aggregate initialization of the _impl_ struct and must follow the syntax | 
| 194 |   // (e.g. `decltype($field$){from.$field$}`, see above). Does not | 
| 195 |   // include `:` or `,` separators. | 
| 196 |   virtual void GenerateCopyAggregateInitializer(io::Printer* printer) const; | 
| 197 |  | 
| 198 |   // Generate lines to serialize this field directly to the array "target", | 
| 199 |   // which are placed within the message's SerializeWithCachedSizesToArray() | 
| 200 |   // method. This must also advance "target" past the written bytes. | 
| 201 |   virtual void GenerateSerializeWithCachedSizesToArray( | 
| 202 |       io::Printer* printer) const = 0; | 
| 203 |  | 
| 204 |   // Generate lines to compute the serialized size of this field, which | 
| 205 |   // are placed in the message's ByteSize() method. | 
| 206 |   virtual void GenerateByteSize(io::Printer* printer) const = 0; | 
| 207 |  | 
| 208 |   // Generates lines to call IsInitialized() for eligible message fields. Non | 
| 209 |   // message fields won't need to override this function. | 
| 210 |   virtual void GenerateIsInitialized(io::Printer* printer) const {} | 
| 211 |  | 
| 212 |   virtual bool IsInlined() const { return false; } | 
| 213 |  | 
| 214 |   virtual ArenaDtorNeeds NeedsArenaDestructor() const { | 
| 215 |     return ArenaDtorNeeds::kNone; | 
| 216 |   } | 
| 217 |  | 
| 218 |   void SetHasBitIndex(int32_t has_bit_index); | 
| 219 |   void SetInlinedStringIndex(int32_t inlined_string_index); | 
| 220 |  | 
| 221 |  protected: | 
| 222 |   const FieldDescriptor* descriptor_; | 
| 223 |   const Options& options_; | 
| 224 |   std::map<std::string, std::string> variables_; | 
| 225 |  | 
| 226 |  private: | 
| 227 |   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator); | 
| 228 | }; | 
| 229 |  | 
| 230 | // Convenience class which constructs FieldGenerators for a Descriptor. | 
| 231 | class FieldGeneratorMap { | 
| 232 |  public: | 
| 233 |   FieldGeneratorMap(const Descriptor* descriptor, const Options& options, | 
| 234 |                     MessageSCCAnalyzer* scc_analyzer); | 
| 235 |   ~FieldGeneratorMap(); | 
| 236 |  | 
| 237 |   const FieldGenerator& get(const FieldDescriptor* field) const; | 
| 238 |  | 
| 239 |   void SetHasBitIndices(const std::vector<int>& has_bit_indices_) { | 
| 240 |     for (int i = 0; i < descriptor_->field_count(); ++i) { | 
| 241 |       field_generators_[i]->SetHasBitIndex(has_bit_indices_[i]); | 
| 242 |     } | 
| 243 |   } | 
| 244 |  | 
| 245 |   void SetInlinedStringIndices(const std::vector<int>& inlined_string_indices) { | 
| 246 |     for (int i = 0; i < descriptor_->field_count(); ++i) { | 
| 247 |       field_generators_[i]->SetInlinedStringIndex(inlined_string_indices[i]); | 
| 248 |     } | 
| 249 |   } | 
| 250 |  | 
| 251 |  private: | 
| 252 |   const Descriptor* descriptor_; | 
| 253 |   std::vector<std::unique_ptr<FieldGenerator>> field_generators_; | 
| 254 |  | 
| 255 |   static FieldGenerator* MakeGoogleInternalGenerator( | 
| 256 |       const FieldDescriptor* field, const Options& options, | 
| 257 |       MessageSCCAnalyzer* scc_analyzer); | 
| 258 |   static FieldGenerator* MakeGenerator(const FieldDescriptor* field, | 
| 259 |                                        const Options& options, | 
| 260 |                                        MessageSCCAnalyzer* scc_analyzer); | 
| 261 |  | 
| 262 |   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap); | 
| 263 | }; | 
| 264 |  | 
| 265 | }  // namespace cpp | 
| 266 | }  // namespace compiler | 
| 267 | }  // namespace protobuf | 
| 268 | }  // namespace google | 
| 269 |  | 
| 270 | #endif  // GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__ | 
| 271 |  |