1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
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"
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"
23 #include "upb/port/def.inc"
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
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
52 std::string
GetModeInit(const upb_MiniTableField
* field32
,
53 const upb_MiniTableField
* field64
) {
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";
60 case kUpb_FieldMode_Array
:
61 ret
= "(int)kUpb_FieldMode_Array";
63 case kUpb_FieldMode_Scalar
:
64 ret
= "(int)kUpb_FieldMode_Scalar";
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)");
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
);
93 case kUpb_FieldRep_1Byte
:
94 return "kUpb_FieldRep_1Byte";
96 case kUpb_FieldRep_4Byte
: {
97 if (rep64
== kUpb_FieldRep_4Byte
) {
98 return "kUpb_FieldRep_4Byte";
100 assert(rep64
== kUpb_FieldRep_8Byte
);
101 return "UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte)";
105 case kUpb_FieldRep_StringView
:
106 return "kUpb_FieldRep_StringView";
108 case kUpb_FieldRep_8Byte
:
109 return "kUpb_FieldRep_8Byte";
115 } // namespace generator