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