generated_message_tctable_decl.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // Must come last:
  40. #include <google/protobuf/port_def.inc>
  41. namespace google {
  42. namespace protobuf {
  43. namespace internal {
  44. // Additional information about this field:
  45. struct TcFieldData {
  46. constexpr TcFieldData() : data(0) {}
  47. constexpr TcFieldData(uint16_t coded_tag, uint8_t hasbit_idx, uint16_t offset)
  48. : data(static_cast<uint64_t>(offset) << 48 |
  49. static_cast<uint64_t>(hasbit_idx) << 16 | coded_tag) {}
  50. template <typename TagType = uint16_t>
  51. TagType coded_tag() const {
  52. return static_cast<TagType>(data);
  53. }
  54. uint8_t hasbit_idx() const { return static_cast<uint8_t>(data >> 16); }
  55. uint16_t offset() const { return static_cast<uint16_t>(data >> 48); }
  56. uint64_t data;
  57. };
  58. struct TcParseTableBase;
  59. // TailCallParseFunc is the function pointer type used in the tailcall table.
  60. typedef const char* (*TailCallParseFunc)(PROTOBUF_TC_PARAM_DECL);
  61. #if defined(_MSC_VER) && !defined(_WIN64)
  62. #pragma warning(push)
  63. // TcParseTableBase is intentionally overaligned on 32 bit targets.
  64. #pragma warning(disable : 4324)
  65. #endif
  66. // Base class for message-level table with info for the tail-call parser.
  67. struct alignas(uint64_t) TcParseTableBase {
  68. // Common attributes for message layout:
  69. uint16_t has_bits_offset;
  70. uint16_t extension_offset;
  71. uint32_t extension_range_low;
  72. uint32_t extension_range_high;
  73. uint8_t fast_idx_mask;
  74. uint8_t reserved;
  75. uint16_t num_fields;
  76. const MessageLite* default_instance;
  77. // Handler for fields which are not handled by table dispatch.
  78. TailCallParseFunc fallback;
  79. // Table entry for fast-path tailcall dispatch handling.
  80. struct FastFieldEntry {
  81. // Target function for dispatch:
  82. TailCallParseFunc target;
  83. // Field data used during parse:
  84. TcFieldData bits;
  85. };
  86. // There is always at least one table entry.
  87. const FastFieldEntry* fast_entry(size_t idx) const {
  88. return reinterpret_cast<const FastFieldEntry*>(this + 1) + idx;
  89. }
  90. };
  91. #if defined(_MSC_VER) && !defined(_WIN64)
  92. #pragma warning(pop)
  93. #endif
  94. static_assert(sizeof(TcParseTableBase::FastFieldEntry) <= 16,
  95. "Field entry is too big.");
  96. template <size_t kFastTableSizeLog2>
  97. struct TcParseTable {
  98. TcParseTableBase header;
  99. // Entries for each field.
  100. //
  101. // Fields are indexed by the lowest bits of their field number. The field
  102. // number is masked to fit inside the table. Note that the parsing logic
  103. // generally calls `TailCallParseTableBase::table()` instead of accessing
  104. // this field directly.
  105. TcParseTableBase::FastFieldEntry entries[(1 << kFastTableSizeLog2)];
  106. };
  107. static_assert(std::is_standard_layout<TcParseTable<1>>::value,
  108. "TcParseTable must be standard layout.");
  109. static_assert(offsetof(TcParseTable<1>, entries) == sizeof(TcParseTableBase),
  110. "Table entries must be laid out after TcParseTableBase.");
  111. } // namespace internal
  112. } // namespace protobuf
  113. } // namespace google
  114. #include <google/protobuf/port_undef.inc>
  115. #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_TCTABLE_DECL_H__