Remove message_test_wrapper for python's message_test.
[google-protobuf.git] / upb_generator / common.cc
blobf5ce42b1108264d9806473ea8c2d657f55688f9a
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
8 #include "upb_generator/common.h"
10 #include <assert.h>
11 #include <stdint.h>
12 #include <stdlib.h>
14 #include <string>
16 #include "absl/strings/str_cat.h"
17 #include "absl/strings/string_view.h"
18 #include "absl/strings/substitute.h"
19 #include "upb/mini_table/field.h"
20 #include "upb/reflection/def.hpp"
22 // Must be last
23 #include "upb/port/def.inc"
25 namespace upb {
26 namespace generator {
28 std::string FieldInitializer(upb::FieldDefPtr field,
29 const upb_MiniTableField* field64,
30 const upb_MiniTableField* field32) {
31 return absl::Substitute(
32 "{$0, $1, $2, $3, $4, $5}", upb_MiniTableField_Number(field64),
33 ArchDependentSize(field32->UPB_PRIVATE(offset),
34 field64->UPB_PRIVATE(offset)),
35 ArchDependentSize(field32->presence, field64->presence),
36 field64->UPB_PRIVATE(submsg_index) == kUpb_NoSub
37 ? "kUpb_NoSub"
38 : absl::StrCat(field64->UPB_PRIVATE(submsg_index)).c_str(),
39 field64->UPB_PRIVATE(descriptortype), GetModeInit(field32, field64));
42 std::string ArchDependentSize(int64_t size32, int64_t size64) {
43 if (size32 == size64) return absl::StrCat(size32);
44 return absl::Substitute("UPB_SIZE($0, $1)", size32, size64);
47 // Returns the field mode as a string initializer.
49 // We could just emit this as a number (and we may yet go in that direction) but
50 // for now emitting symbolic constants gives this better readability and
51 // debuggability.
52 std::string GetModeInit(const upb_MiniTableField* field32,
53 const upb_MiniTableField* field64) {
54 std::string ret;
55 uint8_t mode32 = field32->UPB_PRIVATE(mode);
56 switch (mode32 & kUpb_FieldMode_Mask) {
57 case kUpb_FieldMode_Map:
58 ret = "(int)kUpb_FieldMode_Map";
59 break;
60 case kUpb_FieldMode_Array:
61 ret = "(int)kUpb_FieldMode_Array";
62 break;
63 case kUpb_FieldMode_Scalar:
64 ret = "(int)kUpb_FieldMode_Scalar";
65 break;
66 default:
67 break;
70 if (mode32 & kUpb_LabelFlags_IsPacked) {
71 absl::StrAppend(&ret, " | (int)kUpb_LabelFlags_IsPacked");
74 if (mode32 & kUpb_LabelFlags_IsExtension) {
75 absl::StrAppend(&ret, " | (int)kUpb_LabelFlags_IsExtension");
78 if (mode32 & kUpb_LabelFlags_IsAlternate) {
79 absl::StrAppend(&ret, " | (int)kUpb_LabelFlags_IsAlternate");
82 absl::StrAppend(&ret, " | ((int)", GetFieldRep(field32, field64),
83 " << kUpb_FieldRep_Shift)");
84 return ret;
87 std::string GetFieldRep(const upb_MiniTableField* field32,
88 const upb_MiniTableField* field64) {
89 const auto rep32 = UPB_PRIVATE(_upb_MiniTableField_GetRep)(field32);
90 const auto rep64 = UPB_PRIVATE(_upb_MiniTableField_GetRep)(field64);
92 switch (rep32) {
93 case kUpb_FieldRep_1Byte:
94 return "kUpb_FieldRep_1Byte";
95 break;
96 case kUpb_FieldRep_4Byte: {
97 if (rep64 == kUpb_FieldRep_4Byte) {
98 return "kUpb_FieldRep_4Byte";
99 } else {
100 assert(rep64 == kUpb_FieldRep_8Byte);
101 return "UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte)";
103 break;
105 case kUpb_FieldRep_StringView:
106 return "kUpb_FieldRep_StringView";
107 break;
108 case kUpb_FieldRep_8Byte:
109 return "kUpb_FieldRep_8Byte";
110 break;
112 UPB_UNREACHABLE();
115 } // namespace generator
116 } // namespace upb