generated_message_tctable_decl.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // This file contains declarations needed in generated headers for messages
  31. // that use tail-call table parsing. Everything in this file is for internal
  32. // use only.
  33. #ifndef GOOGLE_PROTOBUF_GENERATED_MESSAGE_TCTABLE_DECL_H__
  34. #define GOOGLE_PROTOBUF_GENERATED_MESSAGE_TCTABLE_DECL_H__
  35. #include <cstdint>
  36. #include <type_traits>
  37. #include <google/protobuf/parse_context.h>
  38. #include <google/protobuf/message_lite.h>
  39. namespace google {
  40. namespace protobuf {
  41. namespace internal {
  42. // Additional information about this field:
  43. struct TcFieldData {
  44. constexpr TcFieldData() : data(0) {}
  45. constexpr TcFieldData(uint16_t coded_tag, uint8_t hasbit_idx, uint16_t offset)
  46. : data(static_cast<uint64_t>(offset) << 48 |
  47. static_cast<uint64_t>(hasbit_idx) << 16 | coded_tag) {}
  48. uint16_t coded_tag() const { return static_cast<uint16_t>(data); }
  49. uint8_t hasbit_idx() const { return static_cast<uint8_t>(data >> 16); }
  50. uint16_t offset() const { return static_cast<uint16_t>(data >> 48); }
  51. uint64_t data;
  52. };
  53. struct TailCallParseTableBase;
  54. // TailCallParseFunc is the function pointer type used in the tailcall table.
  55. typedef const char* (*TailCallParseFunc)(MessageLite* msg, const char* ptr,
  56. ParseContext* ctx,
  57. const TailCallParseTableBase* table,
  58. uint64_t hasbits, TcFieldData data);
  59. // Base class for message-level table with info for the tail-call parser.
  60. struct TailCallParseTableBase {
  61. // Common attributes for message layout:
  62. uint16_t has_bits_offset;
  63. uint16_t extension_offset;
  64. uint32_t extension_range_low;
  65. uint32_t extension_range_high;
  66. uint32_t has_bits_required_mask;
  67. const MessageLite* default_instance;
  68. // Handler for fields which are not handled by table dispatch.
  69. TailCallParseFunc fallback;
  70. // Table entry for fast-path tailcall dispatch handling.
  71. struct FieldEntry {
  72. // Target function for dispatch:
  73. TailCallParseFunc target;
  74. // Field data used during parse:
  75. TcFieldData bits;
  76. };
  77. // There is always at least one table entry.
  78. const FieldEntry* table() const {
  79. return reinterpret_cast<const FieldEntry*>(this + 1);
  80. }
  81. };
  82. static_assert(sizeof(TailCallParseTableBase::FieldEntry) <= 16,
  83. "Field entry is too big.");
  84. template <size_t kTableSizeLog2>
  85. struct TailCallParseTable {
  86. TailCallParseTableBase header;
  87. // Entries for each field.
  88. //
  89. // Fields are indexed by the lowest bits of their field number. The field
  90. // number is masked to fit inside the table. Note that the parsing logic
  91. // generally calls `TailCallParseTableBase::table()` instead of accessing
  92. // this field directly.
  93. TailCallParseTableBase::FieldEntry entries[(1 << kTableSizeLog2)];
  94. };
  95. static_assert(std::is_standard_layout<TailCallParseTable<1>>::value,
  96. "TailCallParseTable must be standard layout.");
  97. static_assert(offsetof(TailCallParseTable<1>, entries) ==
  98. sizeof(TailCallParseTableBase),
  99. "Table entries must be laid out after TailCallParseTableBase.");
  100. } // namespace internal
  101. } // namespace protobuf
  102. } // namespace google
  103. #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_TCTABLE_DECL_H__