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/compiler/cpp/cpp_helpers.h> 44 #include <google/protobuf/compiler/cpp/cpp_options.h> 45 #include <google/protobuf/descriptor.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(). The method should return |true| if it generated any code 162 // that requires a call; this allows the message generator to eliminate the 163 // OwnDestructor() registration if no fields require it. GenerateArenaDestructorCode(io::Printer * printer)164 virtual bool GenerateArenaDestructorCode(io::Printer* printer) const { 165 return false; 166 } 167 168 // Generate initialization code for private members declared by 169 // GeneratePrivateMembers(), specifically for the constexpr constructor. 170 // These go into the constructor's initializer list and must follow that 171 // syntax (eg `field_(args)`). Does not include `:` or `,` separators. GenerateConstinitInitializer(io::Printer * printer)172 virtual void GenerateConstinitInitializer(io::Printer* printer) const {} 173 174 // Generate lines to serialize this field directly to the array "target", 175 // which are placed within the message's SerializeWithCachedSizesToArray() 176 // method. This must also advance "target" past the written bytes. 177 virtual void GenerateSerializeWithCachedSizesToArray( 178 io::Printer* printer) const = 0; 179 180 // Generate lines to compute the serialized size of this field, which 181 // are placed in the message's ByteSize() method. 182 virtual void GenerateByteSize(io::Printer* printer) const = 0; 183 184 void SetHasBitIndex(int32_t has_bit_index); 185 186 protected: 187 const FieldDescriptor* descriptor_; 188 const Options& options_; 189 std::map<std::string, std::string> variables_; 190 191 private: 192 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGenerator); 193 }; 194 195 // Convenience class which constructs FieldGenerators for a Descriptor. 196 class FieldGeneratorMap { 197 public: 198 FieldGeneratorMap(const Descriptor* descriptor, const Options& options, 199 MessageSCCAnalyzer* scc_analyzer); 200 ~FieldGeneratorMap(); 201 202 const FieldGenerator& get(const FieldDescriptor* field) const; 203 SetHasBitIndices(const std::vector<int> & has_bit_indices_)204 void SetHasBitIndices(const std::vector<int>& has_bit_indices_) { 205 for (int i = 0; i < descriptor_->field_count(); ++i) { 206 field_generators_[i]->SetHasBitIndex(has_bit_indices_[i]); 207 } 208 } 209 210 private: 211 const Descriptor* descriptor_; 212 std::vector<std::unique_ptr<FieldGenerator>> field_generators_; 213 214 static FieldGenerator* MakeGoogleInternalGenerator( 215 const FieldDescriptor* field, const Options& options, 216 MessageSCCAnalyzer* scc_analyzer); 217 static FieldGenerator* MakeGenerator(const FieldDescriptor* field, 218 const Options& options, 219 MessageSCCAnalyzer* scc_analyzer); 220 221 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap); 222 }; 223 224 } // namespace cpp 225 } // namespace compiler 226 } // namespace protobuf 227 } // namespace google 228 229 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_FIELD_H__ 230