[ObjC] Add utility for getting the ObjC type for a file.

PiperOrigin-RevId: 566643424
pull/14120/head
Thomas Van Lenten 1 year ago committed by Copybara-Service
parent b58bd40e1c
commit 28a5b5c2ec
  1. 1
      src/google/protobuf/compiler/objectivec/BUILD.bazel
  2. 217
      src/google/protobuf/compiler/objectivec/names.cc
  3. 21
      src/google/protobuf/compiler/objectivec/names.h
  4. 1351
      upb/cmake/google/protobuf/descriptor.upb_minitable.c
  5. 78
      upb/cmake/google/protobuf/descriptor.upb_minitable.h

@ -34,6 +34,7 @@ cc_library(
"//src/google/protobuf:protobuf_nowkt",
"//src/google/protobuf/compiler:code_generator",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/log:absl_log",
],
)

@ -17,6 +17,7 @@
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/log/absl_log.h"
#include "absl/strings/ascii.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
@ -886,6 +887,222 @@ std::string FieldNameCapitalized(const FieldDescriptor* field) {
return result;
}
namespace {
enum class FragmentNameMode : int { kCommon, kMapKey, kObjCGenerics };
std::string FragmentName(const FieldDescriptor* field,
FragmentNameMode mode = FragmentNameMode::kCommon) {
switch (field->type()) {
case FieldDescriptor::TYPE_INT32:
case FieldDescriptor::TYPE_SINT32:
case FieldDescriptor::TYPE_SFIXED32:
return "Int32";
case FieldDescriptor::TYPE_UINT32:
case FieldDescriptor::TYPE_FIXED32:
return "UInt32";
case FieldDescriptor::TYPE_INT64:
case FieldDescriptor::TYPE_SINT64:
case FieldDescriptor::TYPE_SFIXED64:
return "Int64";
case FieldDescriptor::TYPE_UINT64:
case FieldDescriptor::TYPE_FIXED64:
return "UInt64";
case FieldDescriptor::TYPE_FLOAT:
return "Float";
case FieldDescriptor::TYPE_DOUBLE:
return "Double";
case FieldDescriptor::TYPE_BOOL:
return "Bool";
case FieldDescriptor::TYPE_STRING: {
switch (mode) {
case FragmentNameMode::kCommon:
return "Object";
case FragmentNameMode::kMapKey:
return "String";
case FragmentNameMode::kObjCGenerics:
return "NSString*";
}
}
case FieldDescriptor::TYPE_BYTES:
return (mode == FragmentNameMode::kObjCGenerics ? "NSData*" : "Object");
case FieldDescriptor::TYPE_ENUM:
return "Enum";
case FieldDescriptor::TYPE_GROUP:
case FieldDescriptor::TYPE_MESSAGE:
return (mode == FragmentNameMode::kObjCGenerics
? absl::StrCat(ClassName(field->message_type()), "*")
: "Object");
}
// Some compilers report reaching end of function even though all cases of
// the enum are handed in the switch.
ABSL_LOG(FATAL) << "Can't get here.";
}
std::string FieldObjCTypeInternal(const FieldDescriptor* field,
bool* out_is_ptr, std::string* out_generics) {
if (field->is_map()) {
*out_is_ptr = true;
const FieldDescriptor* key_field = field->message_type()->map_key();
const FieldDescriptor* value_field = field->message_type()->map_value();
bool value_is_object;
switch (value_field->type()) {
case FieldDescriptor::TYPE_STRING:
case FieldDescriptor::TYPE_BYTES:
case FieldDescriptor::TYPE_GROUP:
case FieldDescriptor::TYPE_MESSAGE: {
value_is_object = true;
break;
}
default:
value_is_object = false;
break;
}
if (value_is_object && key_field->type() == FieldDescriptor::TYPE_STRING) {
if (out_generics) {
*out_generics = absl::StrCat(
"<NSString*, ",
FragmentName(value_field, FragmentNameMode::kObjCGenerics), ">");
}
return "NSMutableDictionary";
}
if (value_is_object && out_generics) {
*out_generics = absl::StrCat(
"<", FragmentName(value_field, FragmentNameMode::kObjCGenerics), ">");
}
return absl::StrCat("GPB",
FragmentName(key_field, FragmentNameMode::kMapKey),
FragmentName(value_field), "Dictionary");
}
if (field->is_repeated()) {
*out_is_ptr = true;
switch (field->type()) {
case FieldDescriptor::TYPE_STRING:
case FieldDescriptor::TYPE_BYTES:
case FieldDescriptor::TYPE_GROUP:
case FieldDescriptor::TYPE_MESSAGE: {
if (out_generics) {
*out_generics = absl::StrCat(
"<", FragmentName(field, FragmentNameMode::kObjCGenerics), ">");
}
return "NSMutableArray";
}
default:
return absl::StrCat("GPB", FragmentName(field), "Array");
}
}
// Single field
switch (field->type()) {
case FieldDescriptor::TYPE_INT32:
case FieldDescriptor::TYPE_SINT32:
case FieldDescriptor::TYPE_SFIXED32: {
*out_is_ptr = false;
return "int32_t";
}
case FieldDescriptor::TYPE_UINT32:
case FieldDescriptor::TYPE_FIXED32: {
*out_is_ptr = false;
return "uint32_t";
}
case FieldDescriptor::TYPE_INT64:
case FieldDescriptor::TYPE_SINT64:
case FieldDescriptor::TYPE_SFIXED64: {
*out_is_ptr = false;
return "int64_t";
}
case FieldDescriptor::TYPE_UINT64:
case FieldDescriptor::TYPE_FIXED64: {
*out_is_ptr = false;
return "uint64_t";
}
case FieldDescriptor::TYPE_FLOAT: {
*out_is_ptr = false;
return "float";
}
case FieldDescriptor::TYPE_DOUBLE: {
*out_is_ptr = false;
return "double";
}
case FieldDescriptor::TYPE_BOOL: {
*out_is_ptr = false;
return "BOOL";
}
case FieldDescriptor::TYPE_STRING: {
*out_is_ptr = true;
return "NSString";
}
case FieldDescriptor::TYPE_BYTES: {
*out_is_ptr = true;
return "NSData";
}
case FieldDescriptor::TYPE_ENUM: {
*out_is_ptr = false;
return EnumName(field->enum_type());
}
case FieldDescriptor::TYPE_GROUP:
case FieldDescriptor::TYPE_MESSAGE: {
*out_is_ptr = true;
return ClassName(field->message_type());
}
}
// Some compilers report reaching end of function even though all cases of
// the enum are handed in the switch.
ABSL_LOG(FATAL) << "Can't get here.";
}
} // namespace
std::string FieldObjCType(const FieldDescriptor* field,
FieldObjCTypeOptions options) {
std::string generics;
bool is_ptr;
std::string base_type = FieldObjCTypeInternal(
field, &is_ptr,
((options & kFieldObjCTypeOptions_OmitLightweightGenerics) != 0)
? nullptr
: &generics);
if (!is_ptr) {
if ((options & kFieldObjCTypeOptions_IncludeSpaceAfterBasicTypes) != 0) {
return absl::StrCat(base_type, " ");
}
return base_type;
}
if ((options & kFieldObjCTypeOptions_IncludeSpaceBeforeStar) != 0) {
return absl::StrCat(base_type, generics, " *");
}
return absl::StrCat(base_type, generics, "*");
}
std::string OneofEnumName(const OneofDescriptor* descriptor) {
const Descriptor* fieldDescriptor = descriptor->containing_type();
std::string name = absl::StrCat(

@ -99,6 +99,27 @@ PROTOC_EXPORT std::string ExtensionMethodName(
PROTOC_EXPORT std::string FieldName(const FieldDescriptor* field);
PROTOC_EXPORT std::string FieldNameCapitalized(const FieldDescriptor* field);
// The formatting options for FieldObjCType
enum FieldObjCTypeOptions : unsigned int {
// No options.
kFieldObjCTypeOptions_None = 0,
// Leave off the lightweight generics from any collection classes.
kFieldObjCTypeOptions_OmitLightweightGenerics = 1 << 0,
// For things that are pointers, include a space before the `*`.
kFieldObjCTypeOptions_IncludeSpaceBeforeStar = 1 << 1,
// For things that are basic types (int, float, etc.), include a space after
// the type, needed for some usage cases. This is mainly needed when using the
// type to declare variables. Pointers don't need the space to generate valid
// code.
kFieldObjCTypeOptions_IncludeSpaceAfterBasicTypes = 1 << 2,
};
// This returns the ObjC type for the field. `options` allows some controls on
// how the string is created for different usages.
PROTOC_EXPORT std::string FieldObjCType(
const FieldDescriptor* field,
FieldObjCTypeOptions options = kFieldObjCTypeOptions_None);
// Returns the transformed oneof name.
PROTOC_EXPORT std::string OneofEnumName(const OneofDescriptor* descriptor);
PROTOC_EXPORT std::string OneofName(const OneofDescriptor* descriptor);

File diff suppressed because it is too large Load Diff

@ -1,78 +0,0 @@
/* This file was generated by upbc (the upb compiler) from the input
* file:
*
* google/protobuf/descriptor.proto
*
* Do not edit -- your changes will be discarded when the file is
* regenerated. */
#ifndef GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_MINITABLE_H_
#define GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_MINITABLE_H_
#include "upb/upb/generated_code_support.h"
// Must be last.
#include "upb/upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
extern const upb_MiniTable google_protobuf_FileDescriptorSet_msg_init;
extern const upb_MiniTable google_protobuf_FileDescriptorProto_msg_init;
extern const upb_MiniTable google_protobuf_DescriptorProto_msg_init;
extern const upb_MiniTable google_protobuf_DescriptorProto_ExtensionRange_msg_init;
extern const upb_MiniTable google_protobuf_DescriptorProto_ReservedRange_msg_init;
extern const upb_MiniTable google_protobuf_ExtensionRangeOptions_msg_init;
extern const upb_MiniTable google_protobuf_ExtensionRangeOptions_Declaration_msg_init;
extern const upb_MiniTable google_protobuf_FieldDescriptorProto_msg_init;
extern const upb_MiniTable google_protobuf_OneofDescriptorProto_msg_init;
extern const upb_MiniTable google_protobuf_EnumDescriptorProto_msg_init;
extern const upb_MiniTable google_protobuf_EnumDescriptorProto_EnumReservedRange_msg_init;
extern const upb_MiniTable google_protobuf_EnumValueDescriptorProto_msg_init;
extern const upb_MiniTable google_protobuf_ServiceDescriptorProto_msg_init;
extern const upb_MiniTable google_protobuf_MethodDescriptorProto_msg_init;
extern const upb_MiniTable google_protobuf_FileOptions_msg_init;
extern const upb_MiniTable google_protobuf_MessageOptions_msg_init;
extern const upb_MiniTable google_protobuf_FieldOptions_msg_init;
extern const upb_MiniTable google_protobuf_FieldOptions_EditionDefault_msg_init;
extern const upb_MiniTable google_protobuf_OneofOptions_msg_init;
extern const upb_MiniTable google_protobuf_EnumOptions_msg_init;
extern const upb_MiniTable google_protobuf_EnumValueOptions_msg_init;
extern const upb_MiniTable google_protobuf_ServiceOptions_msg_init;
extern const upb_MiniTable google_protobuf_MethodOptions_msg_init;
extern const upb_MiniTable google_protobuf_UninterpretedOption_msg_init;
extern const upb_MiniTable google_protobuf_UninterpretedOption_NamePart_msg_init;
extern const upb_MiniTable google_protobuf_FeatureSet_msg_init;
extern const upb_MiniTable google_protobuf_FeatureSetDefaults_msg_init;
extern const upb_MiniTable google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault_msg_init;
extern const upb_MiniTable google_protobuf_SourceCodeInfo_msg_init;
extern const upb_MiniTable google_protobuf_SourceCodeInfo_Location_msg_init;
extern const upb_MiniTable google_protobuf_GeneratedCodeInfo_msg_init;
extern const upb_MiniTable google_protobuf_GeneratedCodeInfo_Annotation_msg_init;
extern const upb_MiniTableEnum google_protobuf_Edition_enum_init;
extern const upb_MiniTableEnum google_protobuf_ExtensionRangeOptions_VerificationState_enum_init;
extern const upb_MiniTableEnum google_protobuf_FeatureSet_EnumType_enum_init;
extern const upb_MiniTableEnum google_protobuf_FeatureSet_FieldPresence_enum_init;
extern const upb_MiniTableEnum google_protobuf_FeatureSet_JsonFormat_enum_init;
extern const upb_MiniTableEnum google_protobuf_FeatureSet_MessageEncoding_enum_init;
extern const upb_MiniTableEnum google_protobuf_FeatureSet_RepeatedFieldEncoding_enum_init;
extern const upb_MiniTableEnum google_protobuf_FieldDescriptorProto_Label_enum_init;
extern const upb_MiniTableEnum google_protobuf_FieldDescriptorProto_Type_enum_init;
extern const upb_MiniTableEnum google_protobuf_FieldOptions_CType_enum_init;
extern const upb_MiniTableEnum google_protobuf_FieldOptions_JSType_enum_init;
extern const upb_MiniTableEnum google_protobuf_FieldOptions_OptionRetention_enum_init;
extern const upb_MiniTableEnum google_protobuf_FieldOptions_OptionTargetType_enum_init;
extern const upb_MiniTableEnum google_protobuf_FileOptions_OptimizeMode_enum_init;
extern const upb_MiniTableEnum google_protobuf_GeneratedCodeInfo_Annotation_Semantic_enum_init;
extern const upb_MiniTableEnum google_protobuf_MethodOptions_IdempotencyLevel_enum_init;
extern const upb_MiniTableFile google_protobuf_descriptor_proto_upb_file_layout;
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/upb/port/undef.inc"
#endif /* GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_MINITABLE_H_ */
Loading…
Cancel
Save