[ObjC] Move storage type info over to the new helper.

PiperOrigin-RevId: 566714350
pull/14139/head
Thomas Van Lenten 1 year ago committed by Copybara-Service
parent 6864a4d0b6
commit 51cbe921bb
  1. 8
      src/google/protobuf/compiler/objectivec/enum_field.cc
  2. 11
      src/google/protobuf/compiler/objectivec/field.cc
  3. 66
      src/google/protobuf/compiler/objectivec/map_field.cc
  4. 14
      src/google/protobuf/compiler/objectivec/message_field.cc
  5. 97
      src/google/protobuf/compiler/objectivec/primitive_field.cc
  6. 1351
      upb/cmake/google/protobuf/descriptor.upb_minitable.c
  7. 78
      upb/cmake/google/protobuf/descriptor.upb_minitable.h

@ -31,7 +31,7 @@ void SetEnumVariables(
absl::flat_hash_map<absl::string_view, std::string>* variables) {
const std::string type = EnumName(descriptor->enum_type());
const std::string enum_desc_func = absl::StrCat(type, "_EnumDescriptor");
(*variables)["storage_type"] = type;
(*variables)["enum_name"] = type;
// For non repeated fields, if it was defined in a different file, the
// property decls need to use "enum NAME" rather than just "NAME" to support
// the forward declaration of the enums.
@ -112,8 +112,7 @@ void EnumFieldGenerator::DetermineForwardDeclarations(
if (include_external_types &&
descriptor_->file() != descriptor_->enum_type()->file() &&
!IsProtobufLibraryBundledProtoFile(descriptor_->enum_type()->file())) {
// Enum name is already in "storage_type".
const std::string& name = variable("storage_type");
const std::string& name = variable("enum_name");
fwd_decls->insert(absl::StrCat("GPB_ENUM_FWD_DECLARE(", name, ");"));
}
}
@ -129,13 +128,12 @@ RepeatedEnumFieldGenerator::RepeatedEnumFieldGenerator(
const FieldDescriptor* descriptor)
: RepeatedFieldGenerator(descriptor) {
SetEnumVariables(descriptor, &variables_);
variables_["array_storage_type"] = "GPBEnumArray";
}
void RepeatedEnumFieldGenerator::EmitArrayComment(io::Printer* printer) const {
auto vars = printer->WithVars(variables_);
printer->Emit(R"objc(
// |$name$| contains |$storage_type$|
// |$name$| contains |$enum_name$|
)objc");
}

@ -62,6 +62,11 @@ void SetCommonFieldVariables(
descriptor, static_cast<FieldObjCTypeOptions>(
kFieldObjCTypeOptions_IncludeSpaceAfterBasicTypes |
kFieldObjCTypeOptions_IncludeSpaceBeforeStar));
(*variables)["storage_type"] = FieldObjCType(
descriptor, static_cast<FieldObjCTypeOptions>(
kFieldObjCTypeOptions_IncludeSpaceAfterBasicTypes |
kFieldObjCTypeOptions_OmitLightweightGenerics |
kFieldObjCTypeOptions_IncludeSpaceBeforeStar));
(*variables)["field_type"] = GetCapitalizedType(descriptor);
(*variables)["deprecated_attribute"] =
GetOptionalDeprecatedAttribute(descriptor);
@ -278,7 +283,7 @@ SingleFieldGenerator::SingleFieldGenerator(const FieldDescriptor* descriptor)
void SingleFieldGenerator::GenerateFieldStorageDeclaration(
io::Printer* printer) const {
auto vars = printer->WithVars(variables_);
printer->Emit("$storage_type$ $name$;\n");
printer->Emit("$storage_type$$name$;\n");
}
void SingleFieldGenerator::GeneratePropertyDeclaration(
@ -327,7 +332,7 @@ ObjCObjFieldGenerator::ObjCObjFieldGenerator(const FieldDescriptor* descriptor)
void ObjCObjFieldGenerator::GenerateFieldStorageDeclaration(
io::Printer* printer) const {
auto vars = printer->WithVars(variables_);
printer->Emit("$storage_type$ *$name$;\n");
printer->Emit("$storage_type$$name$;\n");
}
void ObjCObjFieldGenerator::GeneratePropertyDeclaration(
@ -366,7 +371,7 @@ RepeatedFieldGenerator::RepeatedFieldGenerator(
void RepeatedFieldGenerator::GenerateFieldStorageDeclaration(
io::Printer* printer) const {
auto vars = printer->WithVars(variables_);
printer->Emit("$array_storage_type$ *$name$;\n");
printer->Emit("$storage_type$$name$;\n");
}
void RepeatedFieldGenerator::GeneratePropertyImplementation(

@ -12,7 +12,6 @@
#include "absl/container/btree_set.h"
#include "absl/container/flat_hash_set.h"
#include "absl/log/absl_log.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "google/protobuf/compiler/objectivec/field.h"
@ -29,43 +28,6 @@ namespace objectivec {
// provides a bunch of things (no has* methods, comments for contained type,
// etc.).
namespace {
const char* MapEntryTypeName(const FieldDescriptor* descriptor, bool isKey) {
ObjectiveCType type = GetObjectiveCType(descriptor);
switch (type) {
case OBJECTIVECTYPE_INT32:
return "Int32";
case OBJECTIVECTYPE_UINT32:
return "UInt32";
case OBJECTIVECTYPE_INT64:
return "Int64";
case OBJECTIVECTYPE_UINT64:
return "UInt64";
case OBJECTIVECTYPE_FLOAT:
return "Float";
case OBJECTIVECTYPE_DOUBLE:
return "Double";
case OBJECTIVECTYPE_BOOLEAN:
return "Bool";
case OBJECTIVECTYPE_STRING:
return (isKey ? "String" : "Object");
case OBJECTIVECTYPE_DATA:
return "Object";
case OBJECTIVECTYPE_ENUM:
return "Enum";
case OBJECTIVECTYPE_MESSAGE:
return "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.";
return nullptr;
}
} // namespace
MapFieldGenerator::MapFieldGenerator(const FieldDescriptor* descriptor)
: RepeatedFieldGenerator(descriptor) {
const FieldDescriptor* key_descriptor = descriptor->message_type()->map_key();
@ -102,20 +64,6 @@ MapFieldGenerator::MapFieldGenerator(const FieldDescriptor* descriptor)
variables_["fieldflags"] = BuildFlagsString(FLAGTYPE_FIELD, field_flags);
ObjectiveCType value_objc_type = GetObjectiveCType(value_descriptor);
const bool value_is_object_type =
((value_objc_type == OBJECTIVECTYPE_STRING) ||
(value_objc_type == OBJECTIVECTYPE_DATA) ||
(value_objc_type == OBJECTIVECTYPE_MESSAGE));
if ((GetObjectiveCType(key_descriptor) == OBJECTIVECTYPE_STRING) &&
value_is_object_type) {
variables_["array_storage_type"] = "NSMutableDictionary";
} else {
variables_["array_storage_type"] =
absl::StrCat("GPB", MapEntryTypeName(key_descriptor, true),
MapEntryTypeName(value_descriptor, false), "Dictionary");
}
variables_["dataTypeSpecific_name"] =
value_field_generator_->variable("dataTypeSpecific_name");
variables_["dataTypeSpecific_value"] =
@ -130,9 +78,9 @@ void MapFieldGenerator::EmitArrayComment(io::Printer* printer) const {
if (GetObjectiveCType(value_descriptor) == OBJECTIVECTYPE_ENUM) {
printer->Emit(
{{"name", variable("name")},
{"storage_type", value_field_generator_->variable("storage_type")}},
{"enum_name", value_field_generator_->variable("enum_name")}},
R"objc(
// |$name$| values are |$storage_type$|
// |$name$| values are |$enum_name$|
)objc");
}
}
@ -160,20 +108,20 @@ void MapFieldGenerator::DetermineForwardDeclarations(
if ((include_external_types &&
!IsProtobufLibraryBundledProtoFile(value_msg_descriptor->file())) ||
descriptor_->file() == value_msg_descriptor->file()) {
const std::string& value_storage_type =
value_field_generator_->variable("storage_type");
fwd_decls->insert(absl::StrCat("@class ", value_storage_type, ";"));
const std::string& value_type =
value_field_generator_->variable("msg_type");
fwd_decls->insert(absl::StrCat("@class ", value_type, ";"));
}
}
void MapFieldGenerator::DetermineObjectiveCClassDefinitions(
absl::btree_set<std::string>* fwd_decls) const {
// Class name is already in "storage_type".
// Class name is already in value's "msg_type".
const FieldDescriptor* value_descriptor =
descriptor_->message_type()->map_value();
if (GetObjectiveCType(value_descriptor) == OBJECTIVECTYPE_MESSAGE) {
fwd_decls->insert(
ObjCClassDeclaration(value_field_generator_->variable("storage_type")));
ObjCClassDeclaration(value_field_generator_->variable("msg_type")));
}
}

@ -32,9 +32,8 @@ void SetMessageVariables(
const std::string& message_type = ClassName(descriptor->message_type());
const std::string& containing_class =
ClassName(descriptor->containing_type());
(*variables)["type"] = message_type;
(*variables)["msg_type"] = message_type;
(*variables)["containing_class"] = containing_class;
(*variables)["storage_type"] = message_type;
(*variables)["dataTypeSpecific_value"] = ObjCClass(message_type);
}
@ -56,14 +55,13 @@ void MessageFieldGenerator::DetermineForwardDeclarations(
if ((include_external_types && !IsProtobufLibraryBundledProtoFile(
descriptor_->message_type()->file())) ||
descriptor_->file() == descriptor_->message_type()->file()) {
// Class name is already in "storage_type".
fwd_decls->insert(absl::StrCat("@class ", variable("storage_type"), ";"));
fwd_decls->insert(absl::StrCat("@class ", variable("msg_type"), ";"));
}
}
void MessageFieldGenerator::DetermineObjectiveCClassDefinitions(
absl::btree_set<std::string>* fwd_decls) const {
fwd_decls->insert(ObjCClassDeclaration(variable("storage_type")));
fwd_decls->insert(ObjCClassDeclaration(variable("msg_type")));
}
void MessageFieldGenerator::DetermineNeededFiles(
@ -77,7 +75,6 @@ RepeatedMessageFieldGenerator::RepeatedMessageFieldGenerator(
const FieldDescriptor* descriptor)
: RepeatedFieldGenerator(descriptor) {
SetMessageVariables(descriptor, &variables_);
variables_["array_storage_type"] = "NSMutableArray";
}
void RepeatedMessageFieldGenerator::DetermineForwardDeclarations(
@ -91,14 +88,13 @@ void RepeatedMessageFieldGenerator::DetermineForwardDeclarations(
if ((include_external_types && !IsProtobufLibraryBundledProtoFile(
descriptor_->message_type()->file())) ||
descriptor_->file() == descriptor_->message_type()->file()) {
// Class name is already in "storage_type".
fwd_decls->insert(absl::StrCat("@class ", variable("storage_type"), ";"));
fwd_decls->insert(absl::StrCat("@class ", variable("msg_type"), ";"));
}
}
void RepeatedMessageFieldGenerator::DetermineObjectiveCClassDefinitions(
absl::btree_set<std::string>* fwd_decls) const {
fwd_decls->insert(ObjCClassDeclaration(variable("storage_type")));
fwd_decls->insert(ObjCClassDeclaration(variable("msg_type")));
}
void RepeatedMessageFieldGenerator::DetermineNeededFiles(

@ -10,9 +10,7 @@
#include <string>
#include "absl/container/flat_hash_map.h"
#include "absl/log/absl_log.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/objectivec/field.h"
#include "google/protobuf/compiler/objectivec/helpers.h"
#include "google/protobuf/descriptor.h"
@ -23,90 +21,9 @@ namespace protobuf {
namespace compiler {
namespace objectivec {
namespace {
const char* PrimitiveTypeName(const FieldDescriptor* descriptor) {
ObjectiveCType type = GetObjectiveCType(descriptor);
switch (type) {
case OBJECTIVECTYPE_INT32:
return "int32_t";
case OBJECTIVECTYPE_UINT32:
return "uint32_t";
case OBJECTIVECTYPE_INT64:
return "int64_t";
case OBJECTIVECTYPE_UINT64:
return "uint64_t";
case OBJECTIVECTYPE_FLOAT:
return "float";
case OBJECTIVECTYPE_DOUBLE:
return "double";
case OBJECTIVECTYPE_BOOLEAN:
return "BOOL";
case OBJECTIVECTYPE_STRING:
return "NSString";
case OBJECTIVECTYPE_DATA:
return "NSData";
case OBJECTIVECTYPE_ENUM:
return "int32_t";
case OBJECTIVECTYPE_MESSAGE:
return nullptr; // Messages go through message_field.cc|h.
}
// 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.";
return nullptr;
}
const char* PrimitiveArrayTypeName(const FieldDescriptor* descriptor) {
ObjectiveCType type = GetObjectiveCType(descriptor);
switch (type) {
case OBJECTIVECTYPE_INT32:
return "Int32";
case OBJECTIVECTYPE_UINT32:
return "UInt32";
case OBJECTIVECTYPE_INT64:
return "Int64";
case OBJECTIVECTYPE_UINT64:
return "UInt64";
case OBJECTIVECTYPE_FLOAT:
return "Float";
case OBJECTIVECTYPE_DOUBLE:
return "Double";
case OBJECTIVECTYPE_BOOLEAN:
return "Bool";
case OBJECTIVECTYPE_STRING:
return ""; // Want NSArray
case OBJECTIVECTYPE_DATA:
return ""; // Want NSArray
case OBJECTIVECTYPE_ENUM:
return "Enum";
case OBJECTIVECTYPE_MESSAGE:
// Want NSArray (but goes through message_field.cc|h).
return "";
}
// 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.";
return nullptr;
}
void SetPrimitiveVariables(
const FieldDescriptor* descriptor,
absl::flat_hash_map<absl::string_view, std::string>* variables) {
std::string primitive_name = PrimitiveTypeName(descriptor);
(*variables)["type"] = primitive_name;
(*variables)["storage_type"] = primitive_name;
}
} // namespace
PrimitiveFieldGenerator::PrimitiveFieldGenerator(
const FieldDescriptor* descriptor)
: SingleFieldGenerator(descriptor) {
SetPrimitiveVariables(descriptor, &variables_);
}
: SingleFieldGenerator(descriptor) {}
void PrimitiveFieldGenerator::GenerateFieldStorageDeclaration(
io::Printer* printer) const {
@ -137,22 +54,12 @@ void PrimitiveFieldGenerator::SetExtraRuntimeHasBitsBase(int index_base) {
PrimitiveObjFieldGenerator::PrimitiveObjFieldGenerator(
const FieldDescriptor* descriptor)
: ObjCObjFieldGenerator(descriptor) {
SetPrimitiveVariables(descriptor, &variables_);
variables_["property_storage_attribute"] = "copy";
}
RepeatedPrimitiveFieldGenerator::RepeatedPrimitiveFieldGenerator(
const FieldDescriptor* descriptor)
: RepeatedFieldGenerator(descriptor) {
SetPrimitiveVariables(descriptor, &variables_);
std::string base_name = PrimitiveArrayTypeName(descriptor);
if (!base_name.empty()) {
variables_["array_storage_type"] = absl::StrCat("GPB", base_name, "Array");
} else {
variables_["array_storage_type"] = "NSMutableArray";
}
}
: RepeatedFieldGenerator(descriptor) {}
} // namespace objectivec
} // namespace compiler

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