cpp_parse_function_generator.h
Go to the documentation of this file.
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 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_PARSE_FUNCTION_GENERATOR_H__
32 #define GOOGLE_PROTOBUF_COMPILER_CPP_PARSE_FUNCTION_GENERATOR_H__
33 
34 #include <map>
35 #include <string>
36 #include <vector>
37 
38 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
39 #include <google/protobuf/compiler/cpp/cpp_options.h>
40 #include <google/protobuf/io/printer.h>
41 #include <google/protobuf/descriptor.h>
43 #include <google/protobuf/wire_format_lite.h>
44 
45 namespace google {
46 namespace protobuf {
47 namespace compiler {
48 namespace cpp {
49 
50 // Helper class for generating tailcall parsing functions.
53  const std::vector<int>& has_bit_indices,
54  MessageSCCAnalyzer* scc_analyzer);
55  // Information to generate field entries.
56  struct FieldInfo {
60  };
61  // Fields parsed by the table fast-path.
62  std::vector<FieldInfo> fast_path_fields;
63  // Fields parsed by slow-path fallback.
64  std::vector<const FieldDescriptor*> fallback_fields;
65  // Table size.
67  // Mask for has-bits of required fields.
69  // True if a generated fallback function is required instead of generic.
71 };
72 
73 // ParseFunctionGenerator generates the _InternalParse function for a message
74 // (and any associated supporting members).
76  public:
77  ParseFunctionGenerator(const Descriptor* descriptor, int max_has_bit_index,
78  const std::vector<int>& has_bit_indices,
79  const std::vector<int>& inlined_string_indices,
80  const Options& options,
81  MessageSCCAnalyzer* scc_analyzer,
82  const std::map<std::string, std::string>& vars);
83 
84  // Emits class-level method declarations to `printer`:
85  void GenerateMethodDecls(io::Printer* printer);
86 
87  // Emits out-of-class method implementation definitions to `printer`:
88  void GenerateMethodImpls(io::Printer* printer);
89 
90  // Emits class-level data member declarations to `printer`:
91  void GenerateDataDecls(io::Printer* printer);
92 
93  // Emits out-of-class data member definitions to `printer`:
94  void GenerateDataDefinitions(io::Printer* printer);
95 
96  private:
97  // Returns true if tailcall table code should be generated.
98  bool should_generate_tctable() const;
99 
100  // Returns true if tailcall table code should be generated, but inside an
101  // #ifdef guard.
103  return should_generate_tctable() &&
105  }
106 
107  // Generates a tail-calling `_InternalParse` function.
109 
110  // Generates a fallback function for tailcall table-based parsing.
112 
113  // Generates functions for parsing this message as a field.
115 
116  // Generates a looping `_InternalParse` function.
118 
119  // Generates the tail-call table definition.
121  void GenerateFastFieldEntries(Formatter& format, const std::string& fallback);
122 
123  // Generates parsing code for an `ArenaString` field.
125 
126  // Generates parsing code for a string-typed field.
128  bool check_utf8);
129 
130  // Generates parsing code for a length-delimited field (strings, messages,
131  // etc.).
133 
134  // Generates the parsing code for a known field.
137  const FieldDescriptor* field);
138 
139  // Generates code to parse the next field from the input stream.
142  const std::vector<const FieldDescriptor*>& ordered_fields);
143 
144  // Generates a `switch` statement to parse each of `ordered_fields`.
145  void GenerateFieldSwitch(
146  Formatter& format,
147  const std::vector<const FieldDescriptor*>& ordered_fields);
148 
152  std::map<std::string, std::string> variables_;
153  std::unique_ptr<TailCallTableInfo> tc_table_info_;
154  std::vector<int> inlined_string_indices_;
156 };
157 
158 enum class ParseCardinality {
159  kSingular,
160  kOneof,
161  kRepeated,
162  kPacked,
163 };
164 
165 // TypeFormat defines parsing types, which encapsulates the expected wire
166 // format, conversion or validation, and the in-memory layout.
167 enum class TypeFormat {
168  // Fixed types:
169  kFixed64, // fixed64, sfixed64, double
170  kFixed32, // fixed32, sfixed32, float
171 
172  // Varint types:
173  kVar64, // int64, uint64
174  kVar32, // int32, uint32
175  kSInt64, // sint64
176  kSInt32, // sint32
177  kBool, // bool
178 
179  // Length-delimited types:
180  kBytes, // bytes
181  kString, // string (proto3/UTF-8 strict)
182  kStringValidateOnly, // string (proto2/UTF-8 validate only)
183 };
184 
185 // Returns the name of a field parser function.
186 //
187 // These are out-of-line functions generated by
188 // parse_function_inc_generator_main.
190  TypeFormat type_format,
191  int tag_length_bytes,
192  const Options& options);
193 
194 } // namespace cpp
195 } // namespace compiler
196 } // namespace protobuf
197 } // namespace google
198 
199 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_PARSE_FUNCTION_GENERATOR_H__
google::protobuf::compiler::cpp::ParseCardinality::kOneof
@ kOneof
google::protobuf::compiler::cpp::ParseCardinality
ParseCardinality
Definition: cpp_parse_function_generator.h:158
http2_test_server.format
format
Definition: http2_test_server.py:118
google::protobuf::FieldDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:515
google::protobuf::compiler::cpp::ParseCardinality::kRepeated
@ kRepeated
google::protobuf::compiler::cpp::ParseFunctionGenerator::scc_analyzer_
MessageSCCAnalyzer * scc_analyzer_
Definition: cpp_parse_function_generator.h:150
google::protobuf::compiler::cpp::TailCallTableInfo::FieldInfo::func_name
std::string func_name
Definition: cpp_parse_function_generator.h:59
options
double_dict options[]
Definition: capstone_test.c:55
google::protobuf::compiler::cpp::ParseFunctionGenerator::GenerateArenaString
void GenerateArenaString(Formatter &format, const FieldDescriptor *field)
Definition: cpp_parse_function_generator.cc:622
google::protobuf::compiler::cpp::TypeFormat::kVar64
@ kVar64
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf.internal::TcFieldData
Definition: generated_message_tctable_decl.h:52
google::protobuf::compiler::cpp::ParseFunctionGenerator::GenerateFieldBody
void GenerateFieldBody(Formatter &format, google::protobuf::internal::WireFormatLite::WireType wiretype, const FieldDescriptor *field)
Definition: cpp_parse_function_generator.cc:840
google::protobuf::compiler::cpp::ParseCardinality::kSingular
@ kSingular
google::protobuf::compiler::cpp::ParseFunctionGenerator::inlined_string_indices_
std::vector< int > inlined_string_indices_
Definition: cpp_parse_function_generator.h:154
google::protobuf::compiler::cpp::ParseFunctionGenerator::should_generate_tctable
bool should_generate_tctable() const
Definition: cpp_parse_function_generator.cc:352
google::protobuf::compiler::cpp::TypeFormat::kBytes
@ kBytes
google::protobuf::compiler::cpp::TypeFormat::kStringValidateOnly
@ kStringValidateOnly
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
google::protobuf::compiler::cpp::TypeFormat
TypeFormat
Definition: cpp_parse_function_generator.h:167
google::protobuf::compiler::cpp::ParseFunctionGenerator::GenerateMethodImpls
void GenerateMethodImpls(io::Printer *printer)
Definition: cpp_parse_function_generator.cc:313
google::protobuf::compiler::cpp::ParseFunctionGenerator::options_
const Options & options_
Definition: cpp_parse_function_generator.h:151
google::protobuf::compiler::cpp::TailCallTableInfo::fast_path_fields
std::vector< FieldInfo > fast_path_fields
Definition: cpp_parse_function_generator.h:62
google::protobuf::compiler::cpp::ParseFunctionGenerator::num_hasbits_
int num_hasbits_
Definition: cpp_parse_function_generator.h:155
google::protobuf::compiler::cpp::ParseFunctionGenerator::GenerateTailcallFallbackFunction
void GenerateTailcallFallbackFunction(Formatter &format)
Definition: cpp_parse_function_generator.cc:374
google::protobuf::compiler::cpp::ParseFunctionGenerator::descriptor_
const Descriptor * descriptor_
Definition: cpp_parse_function_generator.h:149
google::protobuf::compiler::cpp::TailCallTableInfo::use_generated_fallback
bool use_generated_fallback
Definition: cpp_parse_function_generator.h:70
google::protobuf::compiler::cpp::ParseFunctionGenerator::ParseFunctionGenerator
ParseFunctionGenerator(const Descriptor *descriptor, int max_has_bit_index, const std::vector< int > &has_bit_indices, const std::vector< int > &inlined_string_indices, const Options &options, MessageSCCAnalyzer *scc_analyzer, const std::map< std::string, std::string > &vars)
Definition: cpp_parse_function_generator.cc:244
google::protobuf::compiler::cpp::ParseFunctionGenerator::GenerateMethodDecls
void GenerateMethodDecls(io::Printer *printer)
Definition: cpp_parse_function_generator.cc:265
google::protobuf::compiler::cpp::Options::tctable_mode
enum google::protobuf::compiler::cpp::Options::@466 tctable_mode
google::protobuf::compiler::cpp::TypeFormat::kSInt32
@ kSInt32
google::protobuf::compiler::cpp::ParseFunctionGenerator::GenerateTailcallParseFunction
void GenerateTailcallParseFunction(Formatter &format)
Definition: cpp_parse_function_generator.cc:359
google::protobuf::compiler::cpp::ParseFunctionGenerator::GenerateStrings
void GenerateStrings(Formatter &format, const FieldDescriptor *field, bool check_utf8)
Definition: cpp_parse_function_generator.cc:659
google::protobuf::compiler::cpp::TypeFormat::kBool
@ kBool
google::protobuf::compiler::cpp::ParseFunctionGenerator::GenerateParseIterationBody
void GenerateParseIterationBody(Formatter &format, const Descriptor *descriptor, const std::vector< const FieldDescriptor * > &ordered_fields)
Definition: cpp_parse_function_generator.cc:984
cpp
Definition: third_party/bloaty/third_party/googletest/googlemock/scripts/generator/cpp/__init__.py:1
google::protobuf::compiler::cpp::Options
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_options.h:52
google::protobuf::compiler::cpp::TailCallTableInfo::FieldInfo::field
const FieldDescriptor * field
Definition: cpp_parse_function_generator.h:57
google::protobuf::compiler::cpp::TailCallTableInfo::fallback_fields
std::vector< const FieldDescriptor * > fallback_fields
Definition: cpp_parse_function_generator.h:64
google::protobuf::io::Printer
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/printer.h:181
google::protobuf::compiler::cpp::TailCallTableInfo
Definition: cpp_parse_function_generator.h:51
google::protobuf::compiler::cpp::TailCallTableInfo::TailCallTableInfo
TailCallTableInfo(const Descriptor *descriptor, const Options &options, const std::vector< int > &has_bit_indices, MessageSCCAnalyzer *scc_analyzer)
Definition: cpp_parse_function_generator.cc:115
generated_message_tctable_decl.h
google::protobuf::compiler::cpp::TypeFormat::kSInt64
@ kSInt64
google::protobuf::compiler::cpp::GetTailCallFieldHandlerName
std::string GetTailCallFieldHandlerName(ParseCardinality card, TypeFormat type_format, int tag_length_bytes, const Options &options)
Definition: cpp_parse_function_generator.cc:1185
google::protobuf::compiler::cpp::ParseFunctionGenerator
Definition: cpp_parse_function_generator.h:75
field
const FieldDescriptor * field
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:2692
google::protobuf::compiler::cpp::Options::kTCTableGuarded
@ kTCTableGuarded
Definition: protobuf/src/google/protobuf/compiler/cpp/cpp_options.h:82
google::protobuf::compiler::cpp::ParseCardinality::kPacked
@ kPacked
google::protobuf::compiler::cpp::TailCallTableInfo::FieldInfo::bits
google::protobuf::internal::TcFieldData bits
Definition: cpp_parse_function_generator.h:58
google::protobuf::compiler::cpp::MessageSCCAnalyzer
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.h:519
google::protobuf::compiler::cpp::TypeFormat::kVar32
@ kVar32
google::protobuf::compiler::cpp::TypeFormat::kString
@ kString
google::protobuf::compiler::cpp::ParseFunctionGenerator::GenerateTailcallFieldParseFunctions
void GenerateTailcallFieldParseFunctions(Formatter &format)
Definition: cpp_parse_function_generator.cc:404
google::protobuf::compiler::cpp::ParseFunctionGenerator::GenerateLoopingParseFunction
void GenerateLoopingParseFunction(Formatter &format)
Definition: cpp_parse_function_generator.cc:496
google::protobuf::Descriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:231
google::protobuf::compiler::cpp::TypeFormat::kFixed32
@ kFixed32
google::protobuf.internal::WireFormatLite::WireType
WireType
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:101
google::protobuf::compiler::cpp::ParseFunctionGenerator::GenerateDataDefinitions
void GenerateDataDefinitions(io::Printer *printer)
Definition: cpp_parse_function_generator.cc:482
google::protobuf::compiler::cpp::TailCallTableInfo::table_size_log2
int table_size_log2
Definition: cpp_parse_function_generator.h:66
google::protobuf::compiler::cpp::ParseFunctionGenerator::GenerateDataDecls
void GenerateDataDecls(io::Printer *printer)
Definition: cpp_parse_function_generator.cc:461
google::protobuf::compiler::cpp::TailCallTableInfo::FieldInfo
Definition: cpp_parse_function_generator.h:56
google::protobuf::compiler::cpp::ParseFunctionGenerator::should_generate_guarded_tctable
bool should_generate_guarded_tctable() const
Definition: cpp_parse_function_generator.h:102
google::protobuf::compiler::cpp::TailCallTableInfo::has_hasbits_required_mask
uint32_t has_hasbits_required_mask
Definition: cpp_parse_function_generator.h:68
google::protobuf::compiler::cpp::TypeFormat::kFixed64
@ kFixed64
google::protobuf::compiler::cpp::ParseFunctionGenerator::tc_table_info_
std::unique_ptr< TailCallTableInfo > tc_table_info_
Definition: cpp_parse_function_generator.h:153
google::protobuf::compiler::cpp::ParseFunctionGenerator::variables_
std::map< std::string, std::string > variables_
Definition: cpp_parse_function_generator.h:152
google::protobuf::compiler::cpp::ParseFunctionGenerator::GenerateLengthDelim
void GenerateLengthDelim(Formatter &format, const FieldDescriptor *field)
Definition: cpp_parse_function_generator.cc:725
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
google::protobuf::compiler::cpp::ParseFunctionGenerator::GenerateTailCallTable
void GenerateTailCallTable(Formatter &format)
Definition: cpp_parse_function_generator.cc:540
google::protobuf::compiler::cpp::ParseFunctionGenerator::GenerateFastFieldEntries
void GenerateFastFieldEntries(Formatter &format, const std::string &fallback)
Definition: cpp_parse_function_generator.cc:602
compiler
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.pb.cc:21
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
google::protobuf::compiler::cpp::Formatter
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.h:637
google::protobuf::compiler::cpp::ParseFunctionGenerator::GenerateFieldSwitch
void GenerateFieldSwitch(Formatter &format, const std::vector< const FieldDescriptor * > &ordered_fields)
Definition: cpp_parse_function_generator.cc:1047


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:59