From d58ed887ffd94e641894d5b122ff947a4edf0879 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Mon, 25 Sep 2023 08:44:33 -0700 Subject: [PATCH] [ObjC] Wire the generation options through all the Field generators. Nothing uses this yet, just gets things wired through. PiperOrigin-RevId: 568230626 --- .../compiler/objectivec/enum_field.cc | 12 +++-- .../protobuf/compiler/objectivec/enum_field.h | 15 ++++-- .../protobuf/compiler/objectivec/field.cc | 46 +++++++++++-------- .../protobuf/compiler/objectivec/field.h | 19 +++++--- .../protobuf/compiler/objectivec/map_field.cc | 10 ++-- .../protobuf/compiler/objectivec/map_field.h | 8 +++- .../protobuf/compiler/objectivec/message.cc | 2 +- .../compiler/objectivec/message_field.cc | 12 +++-- .../compiler/objectivec/message_field.h | 15 ++++-- .../compiler/objectivec/primitive_field.cc | 16 ++++--- .../compiler/objectivec/primitive_field.h | 22 ++++++--- 11 files changed, 119 insertions(+), 58 deletions(-) diff --git a/src/google/protobuf/compiler/objectivec/enum_field.cc b/src/google/protobuf/compiler/objectivec/enum_field.cc index b5df617493..1b8451e252 100644 --- a/src/google/protobuf/compiler/objectivec/enum_field.cc +++ b/src/google/protobuf/compiler/objectivec/enum_field.cc @@ -16,6 +16,7 @@ #include "absl/strings/string_view.h" #include "google/protobuf/compiler/objectivec/field.h" #include "google/protobuf/compiler/objectivec/names.h" +#include "google/protobuf/compiler/objectivec/options.h" #include "google/protobuf/descriptor.h" #include "google/protobuf/io/printer.h" @@ -50,8 +51,10 @@ void SetEnumVariables( } } // namespace -EnumFieldGenerator::EnumFieldGenerator(const FieldDescriptor* descriptor) - : SingleFieldGenerator(descriptor) { +EnumFieldGenerator::EnumFieldGenerator( + const FieldDescriptor* descriptor, + const GenerationOptions& generation_options) + : SingleFieldGenerator(descriptor, generation_options) { SetEnumVariables(descriptor, &variables_); } @@ -125,8 +128,9 @@ void EnumFieldGenerator::DetermineNeededFiles( } RepeatedEnumFieldGenerator::RepeatedEnumFieldGenerator( - const FieldDescriptor* descriptor) - : RepeatedFieldGenerator(descriptor) { + const FieldDescriptor* descriptor, + const GenerationOptions& generation_options) + : RepeatedFieldGenerator(descriptor, generation_options) { SetEnumVariables(descriptor, &variables_); } diff --git a/src/google/protobuf/compiler/objectivec/enum_field.h b/src/google/protobuf/compiler/objectivec/enum_field.h index eeb32a274c..971592fd06 100644 --- a/src/google/protobuf/compiler/objectivec/enum_field.h +++ b/src/google/protobuf/compiler/objectivec/enum_field.h @@ -13,6 +13,7 @@ #include "absl/container/btree_set.h" #include "absl/container/flat_hash_set.h" #include "google/protobuf/compiler/objectivec/field.h" +#include "google/protobuf/compiler/objectivec/options.h" #include "google/protobuf/descriptor.h" namespace google { @@ -21,7 +22,9 @@ namespace compiler { namespace objectivec { class EnumFieldGenerator : public SingleFieldGenerator { - friend FieldGenerator* FieldGenerator::Make(const FieldDescriptor* field); + friend FieldGenerator* FieldGenerator::Make( + const FieldDescriptor* field, + const GenerationOptions& generation_options); EnumFieldGenerator(const EnumFieldGenerator&) = delete; EnumFieldGenerator& operator=(const EnumFieldGenerator&) = delete; @@ -35,12 +38,15 @@ class EnumFieldGenerator : public SingleFieldGenerator { absl::flat_hash_set* deps) const override; protected: - explicit EnumFieldGenerator(const FieldDescriptor* descriptor); + EnumFieldGenerator(const FieldDescriptor* descriptor, + const GenerationOptions& generation_options); ~EnumFieldGenerator() override = default; }; class RepeatedEnumFieldGenerator : public RepeatedFieldGenerator { - friend FieldGenerator* FieldGenerator::Make(const FieldDescriptor* field); + friend FieldGenerator* FieldGenerator::Make( + const FieldDescriptor* field, + const GenerationOptions& generation_options); public: void EmitArrayComment(io::Printer* printer) const override; @@ -48,7 +54,8 @@ class RepeatedEnumFieldGenerator : public RepeatedFieldGenerator { absl::flat_hash_set* deps) const override; protected: - explicit RepeatedEnumFieldGenerator(const FieldDescriptor* descriptor); + RepeatedEnumFieldGenerator(const FieldDescriptor* descriptor, + const GenerationOptions& generation_options); ~RepeatedEnumFieldGenerator() override = default; }; diff --git a/src/google/protobuf/compiler/objectivec/field.cc b/src/google/protobuf/compiler/objectivec/field.cc index 4b91715a45..b22cd0d06e 100644 --- a/src/google/protobuf/compiler/objectivec/field.cc +++ b/src/google/protobuf/compiler/objectivec/field.cc @@ -23,6 +23,7 @@ #include "google/protobuf/compiler/objectivec/map_field.h" #include "google/protobuf/compiler/objectivec/message_field.h" #include "google/protobuf/compiler/objectivec/names.h" +#include "google/protobuf/compiler/objectivec/options.h" #include "google/protobuf/compiler/objectivec/primitive_field.h" #include "google/protobuf/descriptor.h" #include "google/protobuf/io/printer.h" @@ -156,39 +157,41 @@ bool HasNonZeroDefaultValue(const FieldDescriptor* field) { } // namespace -FieldGenerator* FieldGenerator::Make(const FieldDescriptor* field) { +FieldGenerator* FieldGenerator::Make( + const FieldDescriptor* field, const GenerationOptions& generation_options) { if (field->is_repeated()) { switch (GetObjectiveCType(field)) { case OBJECTIVECTYPE_MESSAGE: { if (field->is_map()) { - return new MapFieldGenerator(field); + return new MapFieldGenerator(field, generation_options); } else { - return new RepeatedMessageFieldGenerator(field); + return new RepeatedMessageFieldGenerator(field, generation_options); } } case OBJECTIVECTYPE_ENUM: - return new RepeatedEnumFieldGenerator(field); + return new RepeatedEnumFieldGenerator(field, generation_options); default: - return new RepeatedPrimitiveFieldGenerator(field); + return new RepeatedPrimitiveFieldGenerator(field, generation_options); } } switch (GetObjectiveCType(field)) { case OBJECTIVECTYPE_MESSAGE: { - return new MessageFieldGenerator(field); + return new MessageFieldGenerator(field, generation_options); } case OBJECTIVECTYPE_ENUM: - return new EnumFieldGenerator(field); + return new EnumFieldGenerator(field, generation_options); default: if (IsReferenceType(field)) { - return new PrimitiveObjFieldGenerator(field); + return new PrimitiveObjFieldGenerator(field, generation_options); } else { - return new PrimitiveFieldGenerator(field); + return new PrimitiveFieldGenerator(field, generation_options); } } } -FieldGenerator::FieldGenerator(const FieldDescriptor* descriptor) +FieldGenerator::FieldGenerator(const FieldDescriptor* descriptor, + const GenerationOptions& generation_options) : descriptor_(descriptor) { SetCommonFieldVariables(descriptor, &variables_); } @@ -275,8 +278,10 @@ bool FieldGenerator::WantsHasProperty() const { return descriptor_->has_presence() && !descriptor_->real_containing_oneof(); } -SingleFieldGenerator::SingleFieldGenerator(const FieldDescriptor* descriptor) - : FieldGenerator(descriptor) { +SingleFieldGenerator::SingleFieldGenerator( + const FieldDescriptor* descriptor, + const GenerationOptions& generation_options) + : FieldGenerator(descriptor, generation_options) { // Nothing } @@ -321,8 +326,10 @@ bool SingleFieldGenerator::RuntimeUsesHasBit() const { return true; } -ObjCObjFieldGenerator::ObjCObjFieldGenerator(const FieldDescriptor* descriptor) - : SingleFieldGenerator(descriptor) { +ObjCObjFieldGenerator::ObjCObjFieldGenerator( + const FieldDescriptor* descriptor, + const GenerationOptions& generation_options) + : SingleFieldGenerator(descriptor, generation_options) { variables_["property_storage_attribute"] = "strong"; if (IsRetainedName(variables_["name"])) { variables_["storage_attribute"] = " NS_RETURNS_NOT_RETAINED"; @@ -365,8 +372,9 @@ void ObjCObjFieldGenerator::GeneratePropertyDeclaration( } RepeatedFieldGenerator::RepeatedFieldGenerator( - const FieldDescriptor* descriptor) - : ObjCObjFieldGenerator(descriptor) {} + const FieldDescriptor* descriptor, + const GenerationOptions& generation_options) + : ObjCObjFieldGenerator(descriptor, generation_options) {} void RepeatedFieldGenerator::GenerateFieldStorageDeclaration( io::Printer* printer) const { @@ -417,11 +425,13 @@ void RepeatedFieldGenerator::EmitArrayComment(io::Printer* printer) const { // Nothing for the default } -FieldGeneratorMap::FieldGeneratorMap(const Descriptor* descriptor) +FieldGeneratorMap::FieldGeneratorMap( + const Descriptor* descriptor, const GenerationOptions& generation_options) : descriptor_(descriptor), field_generators_(static_cast(descriptor->field_count())) { for (int i = 0; i < descriptor->field_count(); i++) { - field_generators_[i].reset(FieldGenerator::Make(descriptor->field(i))); + field_generators_[i].reset( + FieldGenerator::Make(descriptor->field(i), generation_options)); } } diff --git a/src/google/protobuf/compiler/objectivec/field.h b/src/google/protobuf/compiler/objectivec/field.h index 07f5887017..db5af3ad79 100644 --- a/src/google/protobuf/compiler/objectivec/field.h +++ b/src/google/protobuf/compiler/objectivec/field.h @@ -17,6 +17,7 @@ #include "absl/container/flat_hash_set.h" #include "absl/strings/match.h" #include "absl/strings/string_view.h" +#include "google/protobuf/compiler/objectivec/options.h" #include "google/protobuf/descriptor.h" #include "google/protobuf/io/printer.h" @@ -27,7 +28,8 @@ namespace objectivec { class FieldGenerator { public: - static FieldGenerator* Make(const FieldDescriptor* field); + static FieldGenerator* Make(const FieldDescriptor* field, + const GenerationOptions& generation_options); virtual ~FieldGenerator() = default; @@ -80,7 +82,8 @@ class FieldGenerator { std::string raw_field_name() const { return variable("raw_field_name"); } protected: - explicit FieldGenerator(const FieldDescriptor* descriptor); + FieldGenerator(const FieldDescriptor* descriptor, + const GenerationOptions& generation_options); bool WantsHasProperty() const; @@ -103,7 +106,8 @@ class SingleFieldGenerator : public FieldGenerator { bool RuntimeUsesHasBit() const override; protected: - explicit SingleFieldGenerator(const FieldDescriptor* descriptor); + SingleFieldGenerator(const FieldDescriptor* descriptor, + const GenerationOptions& generation_options); }; // Subclass with common support for when the field ends up as an ObjC Object. @@ -118,7 +122,8 @@ class ObjCObjFieldGenerator : public SingleFieldGenerator { void GeneratePropertyDeclaration(io::Printer* printer) const override; protected: - explicit ObjCObjFieldGenerator(const FieldDescriptor* descriptor); + ObjCObjFieldGenerator(const FieldDescriptor* descriptor, + const GenerationOptions& generation_options); }; class RepeatedFieldGenerator : public ObjCObjFieldGenerator { @@ -138,13 +143,15 @@ class RepeatedFieldGenerator : public ObjCObjFieldGenerator { virtual void EmitArrayComment(io::Printer* printer) const; protected: - explicit RepeatedFieldGenerator(const FieldDescriptor* descriptor); + RepeatedFieldGenerator(const FieldDescriptor* descriptor, + const GenerationOptions& generation_options); }; // Convenience class which constructs FieldGenerators for a Descriptor. class FieldGeneratorMap { public: - explicit FieldGeneratorMap(const Descriptor* descriptor); + FieldGeneratorMap(const Descriptor* descriptor, + const GenerationOptions& generation_options); ~FieldGeneratorMap() = default; FieldGeneratorMap(const FieldGeneratorMap&) = delete; diff --git a/src/google/protobuf/compiler/objectivec/map_field.cc b/src/google/protobuf/compiler/objectivec/map_field.cc index 57b311fca2..27f4db69ab 100644 --- a/src/google/protobuf/compiler/objectivec/map_field.cc +++ b/src/google/protobuf/compiler/objectivec/map_field.cc @@ -17,6 +17,7 @@ #include "google/protobuf/compiler/objectivec/field.h" #include "google/protobuf/compiler/objectivec/helpers.h" #include "google/protobuf/compiler/objectivec/names.h" +#include "google/protobuf/compiler/objectivec/options.h" #include "google/protobuf/descriptor.h" namespace google { @@ -28,12 +29,15 @@ namespace objectivec { // provides a bunch of things (no has* methods, comments for contained type, // etc.). -MapFieldGenerator::MapFieldGenerator(const FieldDescriptor* descriptor) - : RepeatedFieldGenerator(descriptor) { +MapFieldGenerator::MapFieldGenerator( + const FieldDescriptor* descriptor, + const GenerationOptions& generation_options) + : RepeatedFieldGenerator(descriptor, generation_options) { const FieldDescriptor* key_descriptor = descriptor->message_type()->map_key(); const FieldDescriptor* value_descriptor = descriptor->message_type()->map_value(); - value_field_generator_.reset(FieldGenerator::Make(value_descriptor)); + value_field_generator_.reset( + FieldGenerator::Make(value_descriptor, generation_options)); // Pull over some variables_ from the value. variables_["field_type"] = value_field_generator_->variable("field_type"); diff --git a/src/google/protobuf/compiler/objectivec/map_field.h b/src/google/protobuf/compiler/objectivec/map_field.h index bed579b621..69fe2ee03a 100644 --- a/src/google/protobuf/compiler/objectivec/map_field.h +++ b/src/google/protobuf/compiler/objectivec/map_field.h @@ -14,6 +14,7 @@ #include "absl/container/btree_set.h" #include "absl/container/flat_hash_set.h" #include "google/protobuf/compiler/objectivec/field.h" +#include "google/protobuf/compiler/objectivec/options.h" #include "google/protobuf/descriptor.h" namespace google { @@ -22,7 +23,9 @@ namespace compiler { namespace objectivec { class MapFieldGenerator : public RepeatedFieldGenerator { - friend FieldGenerator* FieldGenerator::Make(const FieldDescriptor* field); + friend FieldGenerator* FieldGenerator::Make( + const FieldDescriptor* field, + const GenerationOptions& generation_options); public: void EmitArrayComment(io::Printer* printer) const override; @@ -31,7 +34,8 @@ class MapFieldGenerator : public RepeatedFieldGenerator { MapFieldGenerator& operator=(const MapFieldGenerator&) = delete; protected: - explicit MapFieldGenerator(const FieldDescriptor* descriptor); + MapFieldGenerator(const FieldDescriptor* descriptor, + const GenerationOptions& generation_options); ~MapFieldGenerator() override = default; void DetermineObjectiveCClassDefinitions( diff --git a/src/google/protobuf/compiler/objectivec/message.cc b/src/google/protobuf/compiler/objectivec/message.cc index a972ec0347..0030cda550 100644 --- a/src/google/protobuf/compiler/objectivec/message.cc +++ b/src/google/protobuf/compiler/objectivec/message.cc @@ -198,7 +198,7 @@ MessageGenerator::MessageGenerator(const std::string& file_description_name, : file_description_name_(file_description_name), descriptor_(descriptor), generation_options_(generation_options), - field_generators_(descriptor), + field_generators_(descriptor, generation_options), class_name_(ClassName(descriptor_)), deprecated_attribute_( GetOptionalDeprecatedAttribute(descriptor, descriptor->file())) { diff --git a/src/google/protobuf/compiler/objectivec/message_field.cc b/src/google/protobuf/compiler/objectivec/message_field.cc index 885200e823..cac852cd93 100644 --- a/src/google/protobuf/compiler/objectivec/message_field.cc +++ b/src/google/protobuf/compiler/objectivec/message_field.cc @@ -17,6 +17,7 @@ #include "google/protobuf/compiler/objectivec/field.h" #include "google/protobuf/compiler/objectivec/helpers.h" #include "google/protobuf/compiler/objectivec/names.h" +#include "google/protobuf/compiler/objectivec/options.h" #include "google/protobuf/descriptor.h" namespace google { @@ -39,8 +40,10 @@ void SetMessageVariables( } // namespace -MessageFieldGenerator::MessageFieldGenerator(const FieldDescriptor* descriptor) - : ObjCObjFieldGenerator(descriptor) { +MessageFieldGenerator::MessageFieldGenerator( + const FieldDescriptor* descriptor, + const GenerationOptions& generation_options) + : ObjCObjFieldGenerator(descriptor, generation_options) { SetMessageVariables(descriptor, &variables_); } @@ -72,8 +75,9 @@ void MessageFieldGenerator::DetermineNeededFiles( } RepeatedMessageFieldGenerator::RepeatedMessageFieldGenerator( - const FieldDescriptor* descriptor) - : RepeatedFieldGenerator(descriptor) { + const FieldDescriptor* descriptor, + const GenerationOptions& generation_options) + : RepeatedFieldGenerator(descriptor, generation_options) { SetMessageVariables(descriptor, &variables_); } diff --git a/src/google/protobuf/compiler/objectivec/message_field.h b/src/google/protobuf/compiler/objectivec/message_field.h index 72027b61f6..e19eee4514 100644 --- a/src/google/protobuf/compiler/objectivec/message_field.h +++ b/src/google/protobuf/compiler/objectivec/message_field.h @@ -13,6 +13,7 @@ #include "absl/container/btree_set.h" #include "absl/container/flat_hash_set.h" #include "google/protobuf/compiler/objectivec/field.h" +#include "google/protobuf/compiler/objectivec/options.h" #include "google/protobuf/descriptor.h" namespace google { @@ -21,10 +22,13 @@ namespace compiler { namespace objectivec { class MessageFieldGenerator : public ObjCObjFieldGenerator { - friend FieldGenerator* FieldGenerator::Make(const FieldDescriptor* field); + friend FieldGenerator* FieldGenerator::Make( + const FieldDescriptor* field, + const GenerationOptions& generation_options); protected: - explicit MessageFieldGenerator(const FieldDescriptor* descriptor); + MessageFieldGenerator(const FieldDescriptor* descriptor, + const GenerationOptions& generation_options); ~MessageFieldGenerator() override = default; MessageFieldGenerator(const MessageFieldGenerator&) = delete; @@ -40,10 +44,13 @@ class MessageFieldGenerator : public ObjCObjFieldGenerator { }; class RepeatedMessageFieldGenerator : public RepeatedFieldGenerator { - friend FieldGenerator* FieldGenerator::Make(const FieldDescriptor* field); + friend FieldGenerator* FieldGenerator::Make( + const FieldDescriptor* field, + const GenerationOptions& generation_options); protected: - explicit RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor); + RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor, + const GenerationOptions& generation_options); ~RepeatedMessageFieldGenerator() override = default; RepeatedMessageFieldGenerator(const RepeatedMessageFieldGenerator&) = delete; diff --git a/src/google/protobuf/compiler/objectivec/primitive_field.cc b/src/google/protobuf/compiler/objectivec/primitive_field.cc index de34a58eb6..b9428a85b1 100644 --- a/src/google/protobuf/compiler/objectivec/primitive_field.cc +++ b/src/google/protobuf/compiler/objectivec/primitive_field.cc @@ -13,6 +13,7 @@ #include "absl/strings/str_cat.h" #include "google/protobuf/compiler/objectivec/field.h" #include "google/protobuf/compiler/objectivec/helpers.h" +#include "google/protobuf/compiler/objectivec/options.h" #include "google/protobuf/descriptor.h" #include "google/protobuf/io/printer.h" @@ -22,8 +23,9 @@ namespace compiler { namespace objectivec { PrimitiveFieldGenerator::PrimitiveFieldGenerator( - const FieldDescriptor* descriptor) - : SingleFieldGenerator(descriptor) {} + const FieldDescriptor* descriptor, + const GenerationOptions& generation_options) + : SingleFieldGenerator(descriptor, generation_options) {} void PrimitiveFieldGenerator::GenerateFieldStorageDeclaration( io::Printer* printer) const { @@ -52,14 +54,16 @@ void PrimitiveFieldGenerator::SetExtraRuntimeHasBitsBase(int index_base) { } PrimitiveObjFieldGenerator::PrimitiveObjFieldGenerator( - const FieldDescriptor* descriptor) - : ObjCObjFieldGenerator(descriptor) { + const FieldDescriptor* descriptor, + const GenerationOptions& generation_options) + : ObjCObjFieldGenerator(descriptor, generation_options) { variables_["property_storage_attribute"] = "copy"; } RepeatedPrimitiveFieldGenerator::RepeatedPrimitiveFieldGenerator( - const FieldDescriptor* descriptor) - : RepeatedFieldGenerator(descriptor) {} + const FieldDescriptor* descriptor, + const GenerationOptions& generation_options) + : RepeatedFieldGenerator(descriptor, generation_options) {} } // namespace objectivec } // namespace compiler diff --git a/src/google/protobuf/compiler/objectivec/primitive_field.h b/src/google/protobuf/compiler/objectivec/primitive_field.h index fbe7758636..894ac81ad0 100644 --- a/src/google/protobuf/compiler/objectivec/primitive_field.h +++ b/src/google/protobuf/compiler/objectivec/primitive_field.h @@ -9,6 +9,7 @@ #define GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_PRIMITIVE_FIELD_H__ #include "google/protobuf/compiler/objectivec/field.h" +#include "google/protobuf/compiler/objectivec/options.h" #include "google/protobuf/descriptor.h" namespace google { @@ -17,10 +18,13 @@ namespace compiler { namespace objectivec { class PrimitiveFieldGenerator : public SingleFieldGenerator { - friend FieldGenerator* FieldGenerator::Make(const FieldDescriptor* field); + friend FieldGenerator* FieldGenerator::Make( + const FieldDescriptor* field, + const GenerationOptions& generation_options); protected: - explicit PrimitiveFieldGenerator(const FieldDescriptor* descriptor); + PrimitiveFieldGenerator(const FieldDescriptor* descriptor, + const GenerationOptions& generation_options); ~PrimitiveFieldGenerator() override = default; PrimitiveFieldGenerator(const PrimitiveFieldGenerator&) = delete; @@ -33,10 +37,13 @@ class PrimitiveFieldGenerator : public SingleFieldGenerator { }; class PrimitiveObjFieldGenerator : public ObjCObjFieldGenerator { - friend FieldGenerator* FieldGenerator::Make(const FieldDescriptor* field); + friend FieldGenerator* FieldGenerator::Make( + const FieldDescriptor* field, + const GenerationOptions& generation_options); protected: - explicit PrimitiveObjFieldGenerator(const FieldDescriptor* descriptor); + PrimitiveObjFieldGenerator(const FieldDescriptor* descriptor, + const GenerationOptions& generation_options); ~PrimitiveObjFieldGenerator() override = default; PrimitiveObjFieldGenerator(const PrimitiveObjFieldGenerator&) = delete; @@ -45,10 +52,13 @@ class PrimitiveObjFieldGenerator : public ObjCObjFieldGenerator { }; class RepeatedPrimitiveFieldGenerator : public RepeatedFieldGenerator { - friend FieldGenerator* FieldGenerator::Make(const FieldDescriptor* field); + friend FieldGenerator* FieldGenerator::Make( + const FieldDescriptor* field, + const GenerationOptions& generation_options); protected: - explicit RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor); + RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor, + const GenerationOptions& generation_options); ~RepeatedPrimitiveFieldGenerator() override = default; RepeatedPrimitiveFieldGenerator(const RepeatedPrimitiveFieldGenerator&) =