[ObjC] Move some helpers into the only file that uses them.

pull/10720/head
Thomas Van Lenten 2 years ago
parent 2b3a6d2cdc
commit 0953c1be58
  1. 68
      src/google/protobuf/compiler/objectivec/field.cc
  2. 63
      src/google/protobuf/compiler/objectivec/helpers.cc
  3. 10
      src/google/protobuf/compiler/objectivec/helpers.h
  4. 6
      src/google/protobuf/compiler/objectivec/message.cc

@ -117,6 +117,74 @@ void SetCommonFieldVariables(const FieldDescriptor* descriptor,
(*variables)["storage_attribute"] = ""; (*variables)["storage_attribute"] = "";
} }
bool HasNonZeroDefaultValue(const FieldDescriptor* field) {
// Repeated fields don't have defaults.
if (field->is_repeated()) {
return false;
}
// As much as checking field->has_default_value() seems useful, it isn't
// because of enums. proto2 syntax allows the first item in an enum (the
// default) to be non zero. So checking field->has_default_value() would
// result in missing this non zero default. See MessageWithOneBasedEnum in
// objectivec/Tests/unittest_objc.proto for a test Message to confirm this.
// Some proto file set the default to the zero value, so make sure the value
// isn't the zero case.
switch (field->cpp_type()) {
case FieldDescriptor::CPPTYPE_INT32:
return field->default_value_int32() != 0;
case FieldDescriptor::CPPTYPE_UINT32:
return field->default_value_uint32() != 0U;
case FieldDescriptor::CPPTYPE_INT64:
return field->default_value_int64() != 0LL;
case FieldDescriptor::CPPTYPE_UINT64:
return field->default_value_uint64() != 0ULL;
case FieldDescriptor::CPPTYPE_DOUBLE:
return field->default_value_double() != 0.0;
case FieldDescriptor::CPPTYPE_FLOAT:
return field->default_value_float() != 0.0f;
case FieldDescriptor::CPPTYPE_BOOL:
return field->default_value_bool();
case FieldDescriptor::CPPTYPE_STRING: {
const std::string& default_string = field->default_value_string();
return default_string.length() != 0;
}
case FieldDescriptor::CPPTYPE_ENUM:
return field->default_value_enum()->number() != 0;
case FieldDescriptor::CPPTYPE_MESSAGE:
return false;
}
// Some compilers report reaching end of function even though all cases of
// the enum are handed in the switch.
GOOGLE_LOG(FATAL) << "Can't get here.";
return false;
}
bool IsPrimitiveType(const FieldDescriptor* field) {
ObjectiveCType type = GetObjectiveCType(field);
switch (type) {
case OBJECTIVECTYPE_INT32:
case OBJECTIVECTYPE_UINT32:
case OBJECTIVECTYPE_INT64:
case OBJECTIVECTYPE_UINT64:
case OBJECTIVECTYPE_FLOAT:
case OBJECTIVECTYPE_DOUBLE:
case OBJECTIVECTYPE_BOOLEAN:
case OBJECTIVECTYPE_ENUM:
return true;
break;
default:
return false;
}
}
bool IsReferenceType(const FieldDescriptor* field) {
return !IsPrimitiveType(field);
}
} // namespace } // namespace
FieldGenerator* FieldGenerator::Make(const FieldDescriptor* field) { FieldGenerator* FieldGenerator::Make(const FieldDescriptor* field) {

@ -197,24 +197,6 @@ ObjectiveCType GetObjectiveCType(FieldDescriptor::Type field_type) {
return OBJECTIVECTYPE_INT32; return OBJECTIVECTYPE_INT32;
} }
bool IsPrimitiveType(const FieldDescriptor* field) {
ObjectiveCType type = GetObjectiveCType(field);
switch (type) {
case OBJECTIVECTYPE_INT32:
case OBJECTIVECTYPE_UINT32:
case OBJECTIVECTYPE_INT64:
case OBJECTIVECTYPE_UINT64:
case OBJECTIVECTYPE_FLOAT:
case OBJECTIVECTYPE_DOUBLE:
case OBJECTIVECTYPE_BOOLEAN:
case OBJECTIVECTYPE_ENUM:
return true;
break;
default:
return false;
}
}
std::string GPBGenericValueFieldName(const FieldDescriptor* field) { std::string GPBGenericValueFieldName(const FieldDescriptor* field) {
// Returns the field within the GPBGenericValue union to use for the given // Returns the field within the GPBGenericValue union to use for the given
// field. // field.
@ -326,51 +308,6 @@ std::string DefaultValue(const FieldDescriptor* field) {
return std::string(); return std::string();
} }
bool HasNonZeroDefaultValue(const FieldDescriptor* field) {
// Repeated fields don't have defaults.
if (field->is_repeated()) {
return false;
}
// As much as checking field->has_default_value() seems useful, it isn't
// because of enums. proto2 syntax allows the first item in an enum (the
// default) to be non zero. So checking field->has_default_value() would
// result in missing this non zero default. See MessageWithOneBasedEnum in
// objectivec/Tests/unittest_objc.proto for a test Message to confirm this.
// Some proto file set the default to the zero value, so make sure the value
// isn't the zero case.
switch (field->cpp_type()) {
case FieldDescriptor::CPPTYPE_INT32:
return field->default_value_int32() != 0;
case FieldDescriptor::CPPTYPE_UINT32:
return field->default_value_uint32() != 0U;
case FieldDescriptor::CPPTYPE_INT64:
return field->default_value_int64() != 0LL;
case FieldDescriptor::CPPTYPE_UINT64:
return field->default_value_uint64() != 0ULL;
case FieldDescriptor::CPPTYPE_DOUBLE:
return field->default_value_double() != 0.0;
case FieldDescriptor::CPPTYPE_FLOAT:
return field->default_value_float() != 0.0f;
case FieldDescriptor::CPPTYPE_BOOL:
return field->default_value_bool();
case FieldDescriptor::CPPTYPE_STRING: {
const std::string& default_string = field->default_value_string();
return default_string.length() != 0;
}
case FieldDescriptor::CPPTYPE_ENUM:
return field->default_value_enum()->number() != 0;
case FieldDescriptor::CPPTYPE_MESSAGE:
return false;
}
// Some compilers report reaching end of function even though all cases of
// the enum are handed in the switch.
GOOGLE_LOG(FATAL) << "Can't get here.";
return false;
}
std::string BuildFlagsString(const FlagType flag_type, std::string BuildFlagsString(const FlagType flag_type,
const std::vector<std::string>& strings) { const std::vector<std::string>& strings) {
if (strings.empty()) { if (strings.empty()) {

@ -48,10 +48,6 @@ inline bool HasPreservingUnknownEnumSemantics(const FileDescriptor* file) {
return file->syntax() == FileDescriptor::SYNTAX_PROTO3; return file->syntax() == FileDescriptor::SYNTAX_PROTO3;
} }
inline bool IsMapEntryMessage(const Descriptor* descriptor) {
return descriptor->options().map_entry();
}
// Escape C++ trigraphs by escaping question marks to "\?". // Escape C++ trigraphs by escaping question marks to "\?".
std::string EscapeTrigraphs(absl::string_view to_escape); std::string EscapeTrigraphs(absl::string_view to_escape);
@ -83,14 +79,8 @@ inline ObjectiveCType GetObjectiveCType(const FieldDescriptor* field) {
return GetObjectiveCType(field->type()); return GetObjectiveCType(field->type());
} }
bool IsPrimitiveType(const FieldDescriptor* field);
inline bool IsReferenceType(const FieldDescriptor* field) {
return !IsPrimitiveType(field);
}
std::string GPBGenericValueFieldName(const FieldDescriptor* field); std::string GPBGenericValueFieldName(const FieldDescriptor* field);
std::string DefaultValue(const FieldDescriptor* field); std::string DefaultValue(const FieldDescriptor* field);
bool HasNonZeroDefaultValue(const FieldDescriptor* field);
std::string BuildFlagsString(const FlagType type, const std::vector<std::string>& strings); std::string BuildFlagsString(const FlagType type, const std::vector<std::string>& strings);

@ -49,6 +49,11 @@ namespace compiler {
namespace objectivec { namespace objectivec {
namespace { namespace {
bool IsMapEntryMessage(const Descriptor* descriptor) {
return descriptor->options().map_entry();
}
struct FieldOrderingByNumber { struct FieldOrderingByNumber {
inline bool operator()(const FieldDescriptor* a, inline bool operator()(const FieldDescriptor* a,
const FieldDescriptor* b) const { const FieldDescriptor* b) const {
@ -166,6 +171,7 @@ const FieldDescriptor** SortFieldsByStorageSize(const Descriptor* descriptor) {
FieldOrderingByStorageSize()); FieldOrderingByStorageSize());
return fields; return fields;
} }
} // namespace } // namespace
MessageGenerator::MessageGenerator(const std::string& root_classname, MessageGenerator::MessageGenerator(const std::string& root_classname,

Loading…
Cancel
Save