generated_message_tctable_decl.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 TailCallParseTableBase;
  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. // TailCallParseTableBase 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) TailCallParseTableBase {
  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. const MessageLite* default_instance;
  74. // Handler for fields which are not handled by table dispatch.
  75. TailCallParseFunc fallback;
  76. // Table entry for fast-path tailcall dispatch handling.
  77. struct FieldEntry {
  78. // Target function for dispatch:
  79. TailCallParseFunc target;
  80. // Field data used during parse:
  81. TcFieldData bits;
  82. };
  83. // There is always at least one table entry.
  84. const FieldEntry* table() const {
  85. return reinterpret_cast<const FieldEntry*>(this + 1);
  86. }
  87. };
  88. #if defined(_MSC_VER) && !defined(_WIN64)
  89. #pragma warning(pop)
  90. #endif
  91. static_assert(sizeof(TailCallParseTableBase::FieldEntry) <= 16,
  92. "Field entry is too big.");
  93. template <size_t kTableSizeLog2>
  94. struct TailCallParseTable {
  95. TailCallParseTableBase header;
  96. // Entries for each field.
  97. //
  98. // Fields are indexed by the lowest bits of their field number. The field
  99. // number is masked to fit inside the table. Note that the parsing logic
  100. // generally calls `TailCallParseTableBase::table()` instead of accessing
  101. // this field directly.
  102. TailCallParseTableBase::FieldEntry entries[(1 << kTableSizeLog2)];
  103. };
  104. static_assert(std::is_standard_layout<TailCallParseTable<1>>::value,
  105. "TailCallParseTable must be standard layout.");
  106. static_assert(offsetof(TailCallParseTable<1>, entries) ==
  107. sizeof(TailCallParseTableBase),
  108. "Table entries must be laid out after TailCallParseTableBase.");
  109. } // namespace internal
  110. } // namespace protobuf
  111. } // namespace google
  112. #include <google/protobuf/port_undef.inc>
  113. #endif // GOOGLE_PROTOBUF_GENERATED_MESSAGE_TCTABLE_DECL_H__