浏览代码

[ObjC] Move over some custom logic to Descriptor provided info.

FieldDescriptor::has_presence works as a replacement for the custom logic we
had for FieldGenerator::WantsHasProperty(), and is already proto3 optional
aware.
Thomas Van Lenten 5 年之前
父节点
当前提交
59f8dd5faf

+ 4 - 17
src/google/protobuf/compiler/objectivec/objectivec_field.cc

@@ -245,6 +245,10 @@ void FieldGenerator::SetOneofIndexBase(int index_base) {
   }
 }
 
+bool FieldGenerator::WantsHasProperty(void) const {
+  return descriptor_->has_presence() && !descriptor_->real_containing_oneof();
+}
+
 void FieldGenerator::FinishInitialization(void) {
   // If "property_type" wasn't set, make it "storage_type".
   if ((variables_.find("property_type") == variables_.end()) &&
@@ -289,18 +293,6 @@ void SingleFieldGenerator::GeneratePropertyImplementation(
   }
 }
 
-bool SingleFieldGenerator::WantsHasProperty(void) const {
-  if (descriptor_->containing_oneof() != NULL) {
-    // If in a oneof, it uses the oneofcase instead of a has bit.
-    return false;
-  }
-  if (HasFieldPresence(descriptor_->file())) {
-    // In proto1/proto2, every field has a has_$name$() method.
-    return true;
-  }
-  return false;
-}
-
 bool SingleFieldGenerator::RuntimeUsesHasBit(void) const {
   if (descriptor_->containing_oneof() != NULL) {
     // The oneof tracks what is set instead.
@@ -402,11 +394,6 @@ void RepeatedFieldGenerator::GeneratePropertyDeclaration(
   printer->Print("\n");
 }
 
-bool RepeatedFieldGenerator::WantsHasProperty(void) const {
-  // Consumer check the array size/existence rather than a has bit.
-  return false;
-}
-
 bool RepeatedFieldGenerator::RuntimeUsesHasBit(void) const {
   return false;  // The array having anything is what is used.
 }

+ 1 - 3
src/google/protobuf/compiler/objectivec/objectivec_field.h

@@ -96,7 +96,7 @@ class FieldGenerator {
   FieldGenerator(const FieldDescriptor* descriptor, const Options& options);
 
   virtual void FinishInitialization(void);
-  virtual bool WantsHasProperty(void) const = 0;
+  bool WantsHasProperty(void) const;
 
   const FieldDescriptor* descriptor_;
   std::map<string, string> variables_;
@@ -119,7 +119,6 @@ class SingleFieldGenerator : public FieldGenerator {
  protected:
   SingleFieldGenerator(const FieldDescriptor* descriptor,
                        const Options& options);
-  virtual bool WantsHasProperty(void) const;
 };
 
 // Subclass with common support for when the field ends up as an ObjC Object.
@@ -156,7 +155,6 @@ class RepeatedFieldGenerator : public ObjCObjFieldGenerator {
   RepeatedFieldGenerator(const FieldDescriptor* descriptor,
                          const Options& options);
   virtual void FinishInitialization(void);
-  virtual bool WantsHasProperty(void) const;
 };
 
 // Convenience class which constructs FieldGenerators for a Descriptor.

+ 0 - 4
src/google/protobuf/compiler/objectivec/objectivec_helpers.h

@@ -126,10 +126,6 @@ string PROTOC_EXPORT ObjCClass(const string& class_name);
 // be refrerred to by ObjCClass.
 string PROTOC_EXPORT ObjCClassDeclaration(const string& class_name);
 
-inline bool HasFieldPresence(const FileDescriptor* file) {
-  return file->syntax() != FileDescriptor::SYNTAX_PROTO3;
-}
-
 inline bool HasPreservingUnknownEnumSemantics(const FileDescriptor* file) {
   return file->syntax() == FileDescriptor::SYNTAX_PROTO3;
 }

+ 0 - 10
src/google/protobuf/compiler/objectivec/objectivec_message_field.cc

@@ -77,16 +77,6 @@ void MessageFieldGenerator::DetermineObjectiveCClassDefinitions(
   fwd_decls->insert(ObjCClassDeclaration(variable("storage_type")));
 }
 
-bool MessageFieldGenerator::WantsHasProperty(void) const {
-  if (descriptor_->containing_oneof() != NULL) {
-    // If in a oneof, it uses the oneofcase instead of a has bit.
-    return false;
-  }
-  // In both proto2 & proto3, message fields have a has* property to tell
-  // when it is a non default value.
-  return true;
-}
-
 RepeatedMessageFieldGenerator::RepeatedMessageFieldGenerator(
     const FieldDescriptor* descriptor, const Options& options)
     : RepeatedFieldGenerator(descriptor, options) {

+ 0 - 1
src/google/protobuf/compiler/objectivec/objectivec_message_field.h

@@ -52,7 +52,6 @@ class MessageFieldGenerator : public ObjCObjFieldGenerator {
   MessageFieldGenerator& operator=(const MessageFieldGenerator&) = delete;
 
   virtual ~MessageFieldGenerator();
-  virtual bool WantsHasProperty(void) const;
 
  public:
   virtual void DetermineForwardDeclarations(std::set<string>* fwd_decls) const;