[ObjC] Wire the generation options through all the Field generators.

Nothing uses this yet, just gets things wired through.

PiperOrigin-RevId: 568230626
pull/14170/head
Thomas Van Lenten 1 year ago committed by Copybara-Service
parent cde00514e9
commit d58ed887ff
  1. 12
      src/google/protobuf/compiler/objectivec/enum_field.cc
  2. 15
      src/google/protobuf/compiler/objectivec/enum_field.h
  3. 46
      src/google/protobuf/compiler/objectivec/field.cc
  4. 19
      src/google/protobuf/compiler/objectivec/field.h
  5. 10
      src/google/protobuf/compiler/objectivec/map_field.cc
  6. 8
      src/google/protobuf/compiler/objectivec/map_field.h
  7. 2
      src/google/protobuf/compiler/objectivec/message.cc
  8. 12
      src/google/protobuf/compiler/objectivec/message_field.cc
  9. 15
      src/google/protobuf/compiler/objectivec/message_field.h
  10. 16
      src/google/protobuf/compiler/objectivec/primitive_field.cc
  11. 22
      src/google/protobuf/compiler/objectivec/primitive_field.h

@ -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_);
}

@ -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<const FileDescriptor*>* 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<const FileDescriptor*>* deps) const override;
protected:
explicit RepeatedEnumFieldGenerator(const FieldDescriptor* descriptor);
RepeatedEnumFieldGenerator(const FieldDescriptor* descriptor,
const GenerationOptions& generation_options);
~RepeatedEnumFieldGenerator() override = default;
};

@ -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<size_t>(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));
}
}

@ -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;

@ -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");

@ -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(

@ -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())) {

@ -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_);
}

@ -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;

@ -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

@ -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&) =

Loading…
Cancel
Save