Merge pull request #16722 from protocolbuffers/cp-java-generator
Cherry pick java_features.proto boostrap and java generator refactorpull/16729/head
commit
41149253ac
69 changed files with 1610 additions and 996 deletions
@ -1,294 +0,0 @@ |
|||||||
// Protocol Buffers - Google's data interchange format
|
|
||||||
// Copyright 2008 Google Inc. All rights reserved.
|
|
||||||
//
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file or at
|
|
||||||
// https://developers.google.com/open-source/licenses/bsd
|
|
||||||
|
|
||||||
// Author: kenton@google.com (Kenton Varda)
|
|
||||||
// Based on original Protocol Buffers design by
|
|
||||||
// Sanjay Ghemawat, Jeff Dean, and others.
|
|
||||||
|
|
||||||
#include "google/protobuf/compiler/java/field.h" |
|
||||||
|
|
||||||
#include <memory> |
|
||||||
#include <string> |
|
||||||
|
|
||||||
#include "absl/container/flat_hash_map.h" |
|
||||||
#include "absl/log/absl_log.h" |
|
||||||
#include "absl/strings/str_cat.h" |
|
||||||
#include "absl/strings/substitute.h" |
|
||||||
#include "google/protobuf/compiler/java/context.h" |
|
||||||
#include "google/protobuf/compiler/java/enum_field.h" |
|
||||||
#include "google/protobuf/compiler/java/enum_field_lite.h" |
|
||||||
#include "google/protobuf/compiler/java/helpers.h" |
|
||||||
#include "google/protobuf/compiler/java/map_field.h" |
|
||||||
#include "google/protobuf/compiler/java/map_field_lite.h" |
|
||||||
#include "google/protobuf/compiler/java/message_field.h" |
|
||||||
#include "google/protobuf/compiler/java/message_field_lite.h" |
|
||||||
#include "google/protobuf/compiler/java/primitive_field.h" |
|
||||||
#include "google/protobuf/compiler/java/primitive_field_lite.h" |
|
||||||
#include "google/protobuf/compiler/java/string_field.h" |
|
||||||
#include "google/protobuf/compiler/java/string_field_lite.h" |
|
||||||
#include "google/protobuf/io/printer.h" |
|
||||||
|
|
||||||
|
|
||||||
namespace google { |
|
||||||
namespace protobuf { |
|
||||||
namespace compiler { |
|
||||||
namespace java { |
|
||||||
|
|
||||||
namespace { |
|
||||||
|
|
||||||
ImmutableFieldGenerator* MakeImmutableGenerator(const FieldDescriptor* field, |
|
||||||
int messageBitIndex, |
|
||||||
int builderBitIndex, |
|
||||||
Context* context) { |
|
||||||
if (field->is_repeated()) { |
|
||||||
switch (GetJavaType(field)) { |
|
||||||
case JAVATYPE_MESSAGE: |
|
||||||
if (IsMapEntry(field->message_type())) { |
|
||||||
return new ImmutableMapFieldGenerator(field, messageBitIndex, |
|
||||||
builderBitIndex, context); |
|
||||||
} else { |
|
||||||
return new RepeatedImmutableMessageFieldGenerator( |
|
||||||
field, messageBitIndex, builderBitIndex, context); |
|
||||||
} |
|
||||||
case JAVATYPE_ENUM: |
|
||||||
return new RepeatedImmutableEnumFieldGenerator( |
|
||||||
field, messageBitIndex, builderBitIndex, context); |
|
||||||
case JAVATYPE_STRING: |
|
||||||
return new RepeatedImmutableStringFieldGenerator( |
|
||||||
field, messageBitIndex, builderBitIndex, context); |
|
||||||
default: |
|
||||||
return new RepeatedImmutablePrimitiveFieldGenerator( |
|
||||||
field, messageBitIndex, builderBitIndex, context); |
|
||||||
} |
|
||||||
} else { |
|
||||||
if (IsRealOneof(field)) { |
|
||||||
switch (GetJavaType(field)) { |
|
||||||
case JAVATYPE_MESSAGE: |
|
||||||
return new ImmutableMessageOneofFieldGenerator( |
|
||||||
field, messageBitIndex, builderBitIndex, context); |
|
||||||
case JAVATYPE_ENUM: |
|
||||||
return new ImmutableEnumOneofFieldGenerator(field, messageBitIndex, |
|
||||||
builderBitIndex, context); |
|
||||||
case JAVATYPE_STRING: |
|
||||||
return new ImmutableStringOneofFieldGenerator( |
|
||||||
field, messageBitIndex, builderBitIndex, context); |
|
||||||
default: |
|
||||||
return new ImmutablePrimitiveOneofFieldGenerator( |
|
||||||
field, messageBitIndex, builderBitIndex, context); |
|
||||||
} |
|
||||||
} else { |
|
||||||
switch (GetJavaType(field)) { |
|
||||||
case JAVATYPE_MESSAGE: |
|
||||||
return new ImmutableMessageFieldGenerator(field, messageBitIndex, |
|
||||||
builderBitIndex, context); |
|
||||||
case JAVATYPE_ENUM: |
|
||||||
return new ImmutableEnumFieldGenerator(field, messageBitIndex, |
|
||||||
builderBitIndex, context); |
|
||||||
case JAVATYPE_STRING: |
|
||||||
return new ImmutableStringFieldGenerator(field, messageBitIndex, |
|
||||||
builderBitIndex, context); |
|
||||||
default: |
|
||||||
return new ImmutablePrimitiveFieldGenerator(field, messageBitIndex, |
|
||||||
builderBitIndex, context); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
ImmutableFieldLiteGenerator* MakeImmutableLiteGenerator( |
|
||||||
const FieldDescriptor* field, int messageBitIndex, Context* context) { |
|
||||||
if (field->is_repeated()) { |
|
||||||
switch (GetJavaType(field)) { |
|
||||||
case JAVATYPE_MESSAGE: |
|
||||||
if (IsMapEntry(field->message_type())) { |
|
||||||
return new ImmutableMapFieldLiteGenerator(field, messageBitIndex, |
|
||||||
context); |
|
||||||
} else { |
|
||||||
return new RepeatedImmutableMessageFieldLiteGenerator( |
|
||||||
field, messageBitIndex, context); |
|
||||||
} |
|
||||||
case JAVATYPE_ENUM: |
|
||||||
return new RepeatedImmutableEnumFieldLiteGenerator( |
|
||||||
field, messageBitIndex, context); |
|
||||||
case JAVATYPE_STRING: |
|
||||||
return new RepeatedImmutableStringFieldLiteGenerator( |
|
||||||
field, messageBitIndex, context); |
|
||||||
default: |
|
||||||
return new RepeatedImmutablePrimitiveFieldLiteGenerator( |
|
||||||
field, messageBitIndex, context); |
|
||||||
} |
|
||||||
} else { |
|
||||||
if (IsRealOneof(field)) { |
|
||||||
switch (GetJavaType(field)) { |
|
||||||
case JAVATYPE_MESSAGE: |
|
||||||
return new ImmutableMessageOneofFieldLiteGenerator( |
|
||||||
field, messageBitIndex, context); |
|
||||||
case JAVATYPE_ENUM: |
|
||||||
return new ImmutableEnumOneofFieldLiteGenerator( |
|
||||||
field, messageBitIndex, context); |
|
||||||
case JAVATYPE_STRING: |
|
||||||
return new ImmutableStringOneofFieldLiteGenerator( |
|
||||||
field, messageBitIndex, context); |
|
||||||
default: |
|
||||||
return new ImmutablePrimitiveOneofFieldLiteGenerator( |
|
||||||
field, messageBitIndex, context); |
|
||||||
} |
|
||||||
} else { |
|
||||||
switch (GetJavaType(field)) { |
|
||||||
case JAVATYPE_MESSAGE: |
|
||||||
return new ImmutableMessageFieldLiteGenerator(field, messageBitIndex, |
|
||||||
context); |
|
||||||
case JAVATYPE_ENUM: |
|
||||||
return new ImmutableEnumFieldLiteGenerator(field, messageBitIndex, |
|
||||||
context); |
|
||||||
case JAVATYPE_STRING: |
|
||||||
return new ImmutableStringFieldLiteGenerator(field, messageBitIndex, |
|
||||||
context); |
|
||||||
default: |
|
||||||
return new ImmutablePrimitiveFieldLiteGenerator( |
|
||||||
field, messageBitIndex, context); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
static inline void ReportUnexpectedPackedFieldsCall(io::Printer* printer) { |
|
||||||
// Reaching here indicates a bug. Cases are:
|
|
||||||
// - This FieldGenerator should support packing,
|
|
||||||
// but this method should be overridden.
|
|
||||||
// - This FieldGenerator doesn't support packing, and this method
|
|
||||||
// should never have been called.
|
|
||||||
ABSL_LOG(FATAL) << "GenerateBuilderParsingCodeFromPacked() " |
|
||||||
<< "called on field generator that does not support packing."; |
|
||||||
} |
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
ImmutableFieldGenerator::~ImmutableFieldGenerator() {} |
|
||||||
|
|
||||||
void ImmutableFieldGenerator::GenerateBuilderParsingCodeFromPacked( |
|
||||||
io::Printer* printer) const { |
|
||||||
ReportUnexpectedPackedFieldsCall(printer); |
|
||||||
} |
|
||||||
|
|
||||||
ImmutableFieldLiteGenerator::~ImmutableFieldLiteGenerator() {} |
|
||||||
|
|
||||||
// ===================================================================
|
|
||||||
|
|
||||||
template <> |
|
||||||
FieldGeneratorMap<ImmutableFieldGenerator>::FieldGeneratorMap( |
|
||||||
const Descriptor* descriptor, Context* context) |
|
||||||
: descriptor_(descriptor), field_generators_(descriptor->field_count()) { |
|
||||||
// Construct all the FieldGenerators and assign them bit indices for their
|
|
||||||
// bit fields.
|
|
||||||
int messageBitIndex = 0; |
|
||||||
int builderBitIndex = 0; |
|
||||||
for (int i = 0; i < descriptor->field_count(); i++) { |
|
||||||
ImmutableFieldGenerator* generator = MakeImmutableGenerator( |
|
||||||
descriptor->field(i), messageBitIndex, builderBitIndex, context); |
|
||||||
field_generators_[i].reset(generator); |
|
||||||
messageBitIndex += generator->GetNumBitsForMessage(); |
|
||||||
builderBitIndex += generator->GetNumBitsForBuilder(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
template <> |
|
||||||
FieldGeneratorMap<ImmutableFieldGenerator>::~FieldGeneratorMap() {} |
|
||||||
|
|
||||||
template <> |
|
||||||
FieldGeneratorMap<ImmutableFieldLiteGenerator>::FieldGeneratorMap( |
|
||||||
const Descriptor* descriptor, Context* context) |
|
||||||
: descriptor_(descriptor), field_generators_(descriptor->field_count()) { |
|
||||||
// Construct all the FieldGenerators and assign them bit indices for their
|
|
||||||
// bit fields.
|
|
||||||
int messageBitIndex = 0; |
|
||||||
for (int i = 0; i < descriptor->field_count(); i++) { |
|
||||||
ImmutableFieldLiteGenerator* generator = MakeImmutableLiteGenerator( |
|
||||||
descriptor->field(i), messageBitIndex, context); |
|
||||||
field_generators_[i].reset(generator); |
|
||||||
messageBitIndex += generator->GetNumBitsForMessage(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
template <> |
|
||||||
FieldGeneratorMap<ImmutableFieldLiteGenerator>::~FieldGeneratorMap() {} |
|
||||||
|
|
||||||
|
|
||||||
void SetCommonFieldVariables( |
|
||||||
const FieldDescriptor* descriptor, const FieldGeneratorInfo* info, |
|
||||||
absl::flat_hash_map<absl::string_view, std::string>* variables) { |
|
||||||
(*variables)["field_name"] = descriptor->name(); |
|
||||||
(*variables)["name"] = info->name; |
|
||||||
(*variables)["classname"] = descriptor->containing_type()->name(); |
|
||||||
(*variables)["capitalized_name"] = info->capitalized_name; |
|
||||||
(*variables)["disambiguated_reason"] = info->disambiguated_reason; |
|
||||||
(*variables)["constant_name"] = FieldConstantName(descriptor); |
|
||||||
(*variables)["number"] = absl::StrCat(descriptor->number()); |
|
||||||
(*variables)["kt_dsl_builder"] = "_builder"; |
|
||||||
// These variables are placeholders to pick out the beginning and ends of
|
|
||||||
// identifiers for annotations (when doing so with existing variables would
|
|
||||||
// be ambiguous or impossible). They should never be set to anything but the
|
|
||||||
// empty string.
|
|
||||||
(*variables)["{"] = ""; |
|
||||||
(*variables)["}"] = ""; |
|
||||||
(*variables)["kt_name"] = IsForbiddenKotlin(info->name) |
|
||||||
? absl::StrCat(info->name, "_") |
|
||||||
: info->name; |
|
||||||
(*variables)["kt_capitalized_name"] = |
|
||||||
IsForbiddenKotlin(info->name) ? absl::StrCat(info->capitalized_name, "_") |
|
||||||
: info->capitalized_name; |
|
||||||
if (!descriptor->is_repeated()) { |
|
||||||
(*variables)["annotation_field_type"] = |
|
||||||
std::string(FieldTypeName(descriptor->type())); |
|
||||||
} else if (GetJavaType(descriptor) == JAVATYPE_MESSAGE && |
|
||||||
IsMapEntry(descriptor->message_type())) { |
|
||||||
(*variables)["annotation_field_type"] = |
|
||||||
absl::StrCat(FieldTypeName(descriptor->type()), "MAP"); |
|
||||||
} else { |
|
||||||
(*variables)["annotation_field_type"] = |
|
||||||
absl::StrCat(FieldTypeName(descriptor->type()), "_LIST"); |
|
||||||
if (descriptor->is_packed()) { |
|
||||||
variables->insert( |
|
||||||
{"annotation_field_type", |
|
||||||
absl::StrCat(FieldTypeName(descriptor->type()), "_LIST_PACKED")}); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void SetCommonOneofVariables( |
|
||||||
const FieldDescriptor* descriptor, const OneofGeneratorInfo* info, |
|
||||||
absl::flat_hash_map<absl::string_view, std::string>* variables) { |
|
||||||
(*variables)["oneof_name"] = info->name; |
|
||||||
(*variables)["oneof_capitalized_name"] = info->capitalized_name; |
|
||||||
(*variables)["oneof_index"] = |
|
||||||
absl::StrCat(descriptor->containing_oneof()->index()); |
|
||||||
(*variables)["oneof_stored_type"] = GetOneofStoredType(descriptor); |
|
||||||
(*variables)["set_oneof_case_message"] = |
|
||||||
absl::StrCat(info->name, "Case_ = ", descriptor->number()); |
|
||||||
(*variables)["clear_oneof_case_message"] = |
|
||||||
absl::StrCat(info->name, "Case_ = 0"); |
|
||||||
(*variables)["has_oneof_case_message"] = |
|
||||||
absl::StrCat(info->name, "Case_ == ", descriptor->number()); |
|
||||||
} |
|
||||||
|
|
||||||
void PrintExtraFieldInfo( |
|
||||||
const absl::flat_hash_map<absl::string_view, std::string>& variables, |
|
||||||
io::Printer* printer) { |
|
||||||
auto it = variables.find("disambiguated_reason"); |
|
||||||
if (it != variables.end() && !it->second.empty()) { |
|
||||||
printer->Print( |
|
||||||
variables, |
|
||||||
"// An alternative name is used for field \"$field_name$\" because:\n" |
|
||||||
"// $disambiguated_reason$\n"); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
} // namespace java
|
|
||||||
} // namespace compiler
|
|
||||||
} // namespace protobuf
|
|
||||||
} // namespace google
|
|
@ -1,169 +0,0 @@ |
|||||||
// Protocol Buffers - Google's data interchange format
|
|
||||||
// Copyright 2008 Google Inc. All rights reserved.
|
|
||||||
//
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file or at
|
|
||||||
// https://developers.google.com/open-source/licenses/bsd
|
|
||||||
|
|
||||||
// Author: kenton@google.com (Kenton Varda)
|
|
||||||
// Based on original Protocol Buffers design by
|
|
||||||
// Sanjay Ghemawat, Jeff Dean, and others.
|
|
||||||
|
|
||||||
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__ |
|
||||||
#define GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__ |
|
||||||
|
|
||||||
#include <cstdint> |
|
||||||
#include <memory> |
|
||||||
#include <string> |
|
||||||
|
|
||||||
#include "absl/container/flat_hash_map.h" |
|
||||||
#include "absl/log/absl_check.h" |
|
||||||
#include "google/protobuf/descriptor.h" |
|
||||||
#include "google/protobuf/port.h" |
|
||||||
|
|
||||||
namespace google { |
|
||||||
namespace protobuf { |
|
||||||
namespace compiler { |
|
||||||
namespace java { |
|
||||||
class Context; // context.h
|
|
||||||
class ClassNameResolver; // name_resolver.h
|
|
||||||
} // namespace java
|
|
||||||
} // namespace compiler
|
|
||||||
namespace io { |
|
||||||
class Printer; // printer.h
|
|
||||||
} |
|
||||||
} // namespace protobuf
|
|
||||||
} // namespace google
|
|
||||||
|
|
||||||
namespace google { |
|
||||||
namespace protobuf { |
|
||||||
namespace compiler { |
|
||||||
namespace java { |
|
||||||
|
|
||||||
class ImmutableFieldGenerator { |
|
||||||
public: |
|
||||||
ImmutableFieldGenerator() {} |
|
||||||
ImmutableFieldGenerator(const ImmutableFieldGenerator&) = delete; |
|
||||||
ImmutableFieldGenerator& operator=(const ImmutableFieldGenerator&) = delete; |
|
||||||
virtual ~ImmutableFieldGenerator(); |
|
||||||
|
|
||||||
virtual int GetMessageBitIndex() const = 0; |
|
||||||
virtual int GetBuilderBitIndex() const = 0; |
|
||||||
virtual int GetNumBitsForMessage() const = 0; |
|
||||||
virtual int GetNumBitsForBuilder() const = 0; |
|
||||||
virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0; |
|
||||||
virtual void GenerateMembers(io::Printer* printer) const = 0; |
|
||||||
virtual void GenerateBuilderMembers(io::Printer* printer) const = 0; |
|
||||||
virtual void GenerateInitializationCode(io::Printer* printer) const = 0; |
|
||||||
virtual void GenerateBuilderClearCode(io::Printer* printer) const = 0; |
|
||||||
virtual void GenerateMergingCode(io::Printer* printer) const = 0; |
|
||||||
virtual void GenerateBuildingCode(io::Printer* printer) const = 0; |
|
||||||
virtual void GenerateBuilderParsingCode(io::Printer* printer) const = 0; |
|
||||||
virtual void GenerateBuilderParsingCodeFromPacked(io::Printer* printer) const; |
|
||||||
virtual void GenerateSerializationCode(io::Printer* printer) const = 0; |
|
||||||
virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0; |
|
||||||
virtual void GenerateFieldBuilderInitializationCode( |
|
||||||
io::Printer* printer) const = 0; |
|
||||||
virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0; |
|
||||||
|
|
||||||
virtual void GenerateEqualsCode(io::Printer* printer) const = 0; |
|
||||||
virtual void GenerateHashCode(io::Printer* printer) const = 0; |
|
||||||
|
|
||||||
virtual std::string GetBoxedType() const = 0; |
|
||||||
}; |
|
||||||
|
|
||||||
class ImmutableFieldLiteGenerator { |
|
||||||
public: |
|
||||||
ImmutableFieldLiteGenerator() {} |
|
||||||
ImmutableFieldLiteGenerator(const ImmutableFieldLiteGenerator&) = delete; |
|
||||||
ImmutableFieldLiteGenerator& operator=(const ImmutableFieldLiteGenerator&) = |
|
||||||
delete; |
|
||||||
virtual ~ImmutableFieldLiteGenerator(); |
|
||||||
|
|
||||||
virtual int GetNumBitsForMessage() const = 0; |
|
||||||
virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0; |
|
||||||
virtual void GenerateMembers(io::Printer* printer) const = 0; |
|
||||||
virtual void GenerateBuilderMembers(io::Printer* printer) const = 0; |
|
||||||
virtual void GenerateInitializationCode(io::Printer* printer) const = 0; |
|
||||||
virtual void GenerateFieldInfo(io::Printer* printer, |
|
||||||
std::vector<uint16_t>* output) const = 0; |
|
||||||
virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0; |
|
||||||
|
|
||||||
virtual std::string GetBoxedType() const = 0; |
|
||||||
}; |
|
||||||
|
|
||||||
|
|
||||||
// Convenience class which constructs FieldGenerators for a Descriptor.
|
|
||||||
template <typename FieldGeneratorType> |
|
||||||
class FieldGeneratorMap { |
|
||||||
public: |
|
||||||
explicit FieldGeneratorMap(const Descriptor* descriptor, Context* context); |
|
||||||
FieldGeneratorMap(const FieldGeneratorMap&) = delete; |
|
||||||
FieldGeneratorMap& operator=(const FieldGeneratorMap&) = delete; |
|
||||||
~FieldGeneratorMap(); |
|
||||||
|
|
||||||
const FieldGeneratorType& get(const FieldDescriptor* field) const; |
|
||||||
|
|
||||||
private: |
|
||||||
const Descriptor* descriptor_; |
|
||||||
std::vector<std::unique_ptr<FieldGeneratorType>> field_generators_; |
|
||||||
}; |
|
||||||
|
|
||||||
template <typename FieldGeneratorType> |
|
||||||
inline const FieldGeneratorType& FieldGeneratorMap<FieldGeneratorType>::get( |
|
||||||
const FieldDescriptor* field) const { |
|
||||||
ABSL_CHECK_EQ(field->containing_type(), descriptor_); |
|
||||||
return *field_generators_[field->index()]; |
|
||||||
} |
|
||||||
|
|
||||||
// Instantiate template for mutable and immutable maps.
|
|
||||||
template <> |
|
||||||
FieldGeneratorMap<ImmutableFieldGenerator>::FieldGeneratorMap( |
|
||||||
const Descriptor* descriptor, Context* context); |
|
||||||
|
|
||||||
template <> |
|
||||||
FieldGeneratorMap<ImmutableFieldGenerator>::~FieldGeneratorMap(); |
|
||||||
|
|
||||||
|
|
||||||
template <> |
|
||||||
FieldGeneratorMap<ImmutableFieldLiteGenerator>::FieldGeneratorMap( |
|
||||||
const Descriptor* descriptor, Context* context); |
|
||||||
|
|
||||||
template <> |
|
||||||
FieldGeneratorMap<ImmutableFieldLiteGenerator>::~FieldGeneratorMap(); |
|
||||||
|
|
||||||
|
|
||||||
// Field information used in FieldGenerators.
|
|
||||||
struct FieldGeneratorInfo { |
|
||||||
std::string name; |
|
||||||
std::string capitalized_name; |
|
||||||
std::string disambiguated_reason; |
|
||||||
}; |
|
||||||
|
|
||||||
// Oneof information used in OneofFieldGenerators.
|
|
||||||
struct OneofGeneratorInfo { |
|
||||||
std::string name; |
|
||||||
std::string capitalized_name; |
|
||||||
}; |
|
||||||
|
|
||||||
// Set some common variables used in variable FieldGenerators.
|
|
||||||
void SetCommonFieldVariables( |
|
||||||
const FieldDescriptor* descriptor, const FieldGeneratorInfo* info, |
|
||||||
absl::flat_hash_map<absl::string_view, std::string>* variables); |
|
||||||
|
|
||||||
// Set some common oneof variables used in OneofFieldGenerators.
|
|
||||||
void SetCommonOneofVariables( |
|
||||||
const FieldDescriptor* descriptor, const OneofGeneratorInfo* info, |
|
||||||
absl::flat_hash_map<absl::string_view, std::string>* variables); |
|
||||||
|
|
||||||
// Print useful comments before a field's accessors.
|
|
||||||
void PrintExtraFieldInfo( |
|
||||||
const absl::flat_hash_map<absl::string_view, std::string>& variables, |
|
||||||
io::Printer* printer); |
|
||||||
|
|
||||||
} // namespace java
|
|
||||||
} // namespace compiler
|
|
||||||
} // namespace protobuf
|
|
||||||
} // namespace google
|
|
||||||
|
|
||||||
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__
|
|
@ -0,0 +1,85 @@ |
|||||||
|
#include "google/protobuf/compiler/java/field_common.h" |
||||||
|
|
||||||
|
#include <string> |
||||||
|
|
||||||
|
#include "google/protobuf/compiler/java/helpers.h" |
||||||
|
#include "google/protobuf/descriptor.h" |
||||||
|
|
||||||
|
namespace google { |
||||||
|
namespace protobuf { |
||||||
|
namespace compiler { |
||||||
|
namespace java { |
||||||
|
|
||||||
|
void SetCommonFieldVariables( |
||||||
|
const FieldDescriptor* descriptor, const FieldGeneratorInfo* info, |
||||||
|
absl::flat_hash_map<absl::string_view, std::string>* variables) { |
||||||
|
(*variables)["field_name"] = descriptor->name(); |
||||||
|
(*variables)["name"] = info->name; |
||||||
|
(*variables)["classname"] = descriptor->containing_type()->name(); |
||||||
|
(*variables)["capitalized_name"] = info->capitalized_name; |
||||||
|
(*variables)["disambiguated_reason"] = info->disambiguated_reason; |
||||||
|
(*variables)["constant_name"] = FieldConstantName(descriptor); |
||||||
|
(*variables)["number"] = absl::StrCat(descriptor->number()); |
||||||
|
(*variables)["kt_dsl_builder"] = "_builder"; |
||||||
|
// These variables are placeholders to pick out the beginning and ends of
|
||||||
|
// identifiers for annotations (when doing so with existing variables would
|
||||||
|
// be ambiguous or impossible). They should never be set to anything but the
|
||||||
|
// empty string.
|
||||||
|
(*variables)["{"] = ""; |
||||||
|
(*variables)["}"] = ""; |
||||||
|
(*variables)["kt_name"] = IsForbiddenKotlin(info->name) |
||||||
|
? absl::StrCat(info->name, "_") |
||||||
|
: info->name; |
||||||
|
(*variables)["kt_capitalized_name"] = |
||||||
|
IsForbiddenKotlin(info->name) ? absl::StrCat(info->capitalized_name, "_") |
||||||
|
: info->capitalized_name; |
||||||
|
if (!descriptor->is_repeated()) { |
||||||
|
(*variables)["annotation_field_type"] = |
||||||
|
std::string(FieldTypeName(descriptor->type())); |
||||||
|
} else if (GetJavaType(descriptor) == JAVATYPE_MESSAGE && |
||||||
|
IsMapEntry(descriptor->message_type())) { |
||||||
|
(*variables)["annotation_field_type"] = |
||||||
|
absl::StrCat(FieldTypeName(descriptor->type()), "MAP"); |
||||||
|
} else { |
||||||
|
(*variables)["annotation_field_type"] = |
||||||
|
absl::StrCat(FieldTypeName(descriptor->type()), "_LIST"); |
||||||
|
if (descriptor->is_packed()) { |
||||||
|
variables->insert( |
||||||
|
{"annotation_field_type", |
||||||
|
absl::StrCat(FieldTypeName(descriptor->type()), "_LIST_PACKED")}); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void SetCommonOneofVariables( |
||||||
|
const FieldDescriptor* descriptor, const OneofGeneratorInfo* info, |
||||||
|
absl::flat_hash_map<absl::string_view, std::string>* variables) { |
||||||
|
(*variables)["oneof_name"] = info->name; |
||||||
|
(*variables)["oneof_capitalized_name"] = info->capitalized_name; |
||||||
|
(*variables)["oneof_index"] = |
||||||
|
absl::StrCat(descriptor->containing_oneof()->index()); |
||||||
|
(*variables)["oneof_stored_type"] = GetOneofStoredType(descriptor); |
||||||
|
(*variables)["set_oneof_case_message"] = |
||||||
|
absl::StrCat(info->name, "Case_ = ", descriptor->number()); |
||||||
|
(*variables)["clear_oneof_case_message"] = |
||||||
|
absl::StrCat(info->name, "Case_ = 0"); |
||||||
|
(*variables)["has_oneof_case_message"] = |
||||||
|
absl::StrCat(info->name, "Case_ == ", descriptor->number()); |
||||||
|
} |
||||||
|
|
||||||
|
void PrintExtraFieldInfo( |
||||||
|
const absl::flat_hash_map<absl::string_view, std::string>& variables, |
||||||
|
io::Printer* printer) { |
||||||
|
auto it = variables.find("disambiguated_reason"); |
||||||
|
if (it != variables.end() && !it->second.empty()) { |
||||||
|
printer->Print( |
||||||
|
variables, |
||||||
|
"// An alternative name is used for field \"$field_name$\" because:\n" |
||||||
|
"// $disambiguated_reason$\n"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace java
|
||||||
|
} // namespace compiler
|
||||||
|
} // namespace protobuf
|
||||||
|
} // namespace google
|
@ -0,0 +1,44 @@ |
|||||||
|
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_COMMON_H__ |
||||||
|
#define GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_COMMON_H__ |
||||||
|
|
||||||
|
#include "google/protobuf/descriptor.h" |
||||||
|
|
||||||
|
namespace google { |
||||||
|
namespace protobuf { |
||||||
|
namespace compiler { |
||||||
|
namespace java { |
||||||
|
|
||||||
|
// Field information used in FieldGenerators.
|
||||||
|
struct FieldGeneratorInfo { |
||||||
|
std::string name; |
||||||
|
std::string capitalized_name; |
||||||
|
std::string disambiguated_reason; |
||||||
|
}; |
||||||
|
|
||||||
|
// Oneof information used in OneofFieldGenerators.
|
||||||
|
struct OneofGeneratorInfo { |
||||||
|
std::string name; |
||||||
|
std::string capitalized_name; |
||||||
|
}; |
||||||
|
|
||||||
|
// Set some common variables used in variable FieldGenerators.
|
||||||
|
void SetCommonFieldVariables( |
||||||
|
const FieldDescriptor* descriptor, const FieldGeneratorInfo* info, |
||||||
|
absl::flat_hash_map<absl::string_view, std::string>* variables); |
||||||
|
|
||||||
|
// Set some common oneof variables used in OneofFieldGenerators.
|
||||||
|
void SetCommonOneofVariables( |
||||||
|
const FieldDescriptor* descriptor, const OneofGeneratorInfo* info, |
||||||
|
absl::flat_hash_map<absl::string_view, std::string>* variables); |
||||||
|
|
||||||
|
// Print useful comments before a field's accessors.
|
||||||
|
void PrintExtraFieldInfo( |
||||||
|
const absl::flat_hash_map<absl::string_view, std::string>& variables, |
||||||
|
io::Printer* printer); |
||||||
|
|
||||||
|
} // namespace java
|
||||||
|
} // namespace compiler
|
||||||
|
} // namespace protobuf
|
||||||
|
} // namespace google
|
||||||
|
|
||||||
|
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_COMMON_H__
|
@ -0,0 +1,89 @@ |
|||||||
|
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_COMMON_H__ |
||||||
|
#define GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_COMMON_H__ |
||||||
|
|
||||||
|
#include <cstddef> |
||||||
|
#include <memory> |
||||||
|
#include <vector> |
||||||
|
|
||||||
|
#include "absl/log/absl_check.h" |
||||||
|
#include "absl/log/absl_log.h" |
||||||
|
#include "google/protobuf/compiler/java/context.h" |
||||||
|
#include "google/protobuf/compiler/java/name_resolver.h" |
||||||
|
#include "google/protobuf/descriptor.h" |
||||||
|
#include "google/protobuf/io/printer.h" |
||||||
|
|
||||||
|
namespace google { |
||||||
|
namespace protobuf { |
||||||
|
namespace compiler { |
||||||
|
namespace java { |
||||||
|
|
||||||
|
static const int kMaxStaticSize = 1 << 15; // aka 32k
|
||||||
|
|
||||||
|
class FieldGenerator { |
||||||
|
public: |
||||||
|
virtual ~FieldGenerator() = default; |
||||||
|
virtual void GenerateSerializationCode(io::Printer* printer) const = 0; |
||||||
|
}; |
||||||
|
|
||||||
|
// Convenience class which constructs FieldGenerators for a Descriptor.
|
||||||
|
template <typename FieldGeneratorType> |
||||||
|
class FieldGeneratorMap { |
||||||
|
public: |
||||||
|
explicit FieldGeneratorMap(const Descriptor* descriptor) |
||||||
|
: descriptor_(descriptor) { |
||||||
|
field_generators_.reserve(static_cast<size_t>(descriptor->field_count())); |
||||||
|
} |
||||||
|
|
||||||
|
~FieldGeneratorMap() { |
||||||
|
for (const auto* g : field_generators_) { |
||||||
|
delete g; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
FieldGeneratorMap(FieldGeneratorMap&&) = default; |
||||||
|
FieldGeneratorMap& operator=(FieldGeneratorMap&&) = default; |
||||||
|
|
||||||
|
FieldGeneratorMap(const FieldGeneratorMap&) = delete; |
||||||
|
FieldGeneratorMap& operator=(const FieldGeneratorMap&) = delete; |
||||||
|
|
||||||
|
void Add(const FieldDescriptor* field, |
||||||
|
std::unique_ptr<FieldGeneratorType> field_generator) { |
||||||
|
ABSL_CHECK_EQ(field->containing_type(), descriptor_); |
||||||
|
field_generators_.push_back(field_generator.release()); |
||||||
|
} |
||||||
|
|
||||||
|
const FieldGeneratorType& get(const FieldDescriptor* field) const { |
||||||
|
ABSL_CHECK_EQ(field->containing_type(), descriptor_); |
||||||
|
return *field_generators_[static_cast<size_t>(field->index())]; |
||||||
|
} |
||||||
|
|
||||||
|
std::vector<const FieldGenerator*> field_generators() const { |
||||||
|
std::vector<const FieldGenerator*> field_generators; |
||||||
|
field_generators.reserve(field_generators_.size()); |
||||||
|
for (const auto* g : field_generators_) { |
||||||
|
field_generators.push_back(g); |
||||||
|
} |
||||||
|
return field_generators; |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
const Descriptor* descriptor_; |
||||||
|
std::vector<const FieldGeneratorType*> field_generators_; |
||||||
|
}; |
||||||
|
|
||||||
|
inline void ReportUnexpectedPackedFieldsCall() { |
||||||
|
// Reaching here indicates a bug. Cases are:
|
||||||
|
// - This FieldGenerator should support packing,
|
||||||
|
// but this method should be overridden.
|
||||||
|
// - This FieldGenerator doesn't support packing, and this method
|
||||||
|
// should never have been called.
|
||||||
|
ABSL_LOG(FATAL) << "GenerateBuilderParsingCodeFromPacked() " |
||||||
|
<< "called on field generator that does not support packing."; |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace java
|
||||||
|
} // namespace compiler
|
||||||
|
} // namespace protobuf
|
||||||
|
} // namespace google
|
||||||
|
|
||||||
|
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_SERVICE_COMMON_H__
|
@ -1,63 +0,0 @@ |
|||||||
// Protocol Buffers - Google's data interchange format
|
|
||||||
// Copyright 2008 Google Inc. All rights reserved.
|
|
||||||
//
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file or at
|
|
||||||
// https://developers.google.com/open-source/licenses/bsd
|
|
||||||
|
|
||||||
// Author: liujisi@google.com (Pherl Liu)
|
|
||||||
|
|
||||||
#include "google/protobuf/compiler/java/generator_factory.h" |
|
||||||
|
|
||||||
#include "google/protobuf/compiler/java/context.h" |
|
||||||
#include "google/protobuf/compiler/java/enum_field.h" |
|
||||||
#include "google/protobuf/compiler/java/extension.h" |
|
||||||
#include "google/protobuf/compiler/java/extension_lite.h" |
|
||||||
#include "google/protobuf/compiler/java/field.h" |
|
||||||
#include "google/protobuf/compiler/java/helpers.h" |
|
||||||
#include "google/protobuf/compiler/java/message.h" |
|
||||||
#include "google/protobuf/compiler/java/message_lite.h" |
|
||||||
#include "google/protobuf/compiler/java/service.h" |
|
||||||
|
|
||||||
namespace google { |
|
||||||
namespace protobuf { |
|
||||||
namespace compiler { |
|
||||||
namespace java { |
|
||||||
|
|
||||||
GeneratorFactory::GeneratorFactory() {} |
|
||||||
GeneratorFactory::~GeneratorFactory() {} |
|
||||||
|
|
||||||
// ===================================================================
|
|
||||||
|
|
||||||
ImmutableGeneratorFactory::ImmutableGeneratorFactory(Context* context) |
|
||||||
: context_(context) {} |
|
||||||
ImmutableGeneratorFactory::~ImmutableGeneratorFactory() {} |
|
||||||
|
|
||||||
MessageGenerator* ImmutableGeneratorFactory::NewMessageGenerator( |
|
||||||
const Descriptor* descriptor) const { |
|
||||||
if (HasDescriptorMethods(descriptor, context_->EnforceLite())) { |
|
||||||
return new ImmutableMessageGenerator(descriptor, context_); |
|
||||||
} else { |
|
||||||
return new ImmutableMessageLiteGenerator(descriptor, context_); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
ExtensionGenerator* ImmutableGeneratorFactory::NewExtensionGenerator( |
|
||||||
const FieldDescriptor* descriptor) const { |
|
||||||
if (HasDescriptorMethods(descriptor->file(), context_->EnforceLite())) { |
|
||||||
return new ImmutableExtensionGenerator(descriptor, context_); |
|
||||||
} else { |
|
||||||
return new ImmutableExtensionLiteGenerator(descriptor, context_); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
ServiceGenerator* ImmutableGeneratorFactory::NewServiceGenerator( |
|
||||||
const ServiceDescriptor* descriptor) const { |
|
||||||
return new ImmutableServiceGenerator(descriptor, context_); |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
} // namespace java
|
|
||||||
} // namespace compiler
|
|
||||||
} // namespace protobuf
|
|
||||||
} // namespace google
|
|
@ -0,0 +1,132 @@ |
|||||||
|
# We use abbreviated target names in this directory to work around: |
||||||
|
# https://github.com/bazelbuild/bazel/issues/18683 |
||||||
|
cc_library( |
||||||
|
name = "fg", |
||||||
|
hdrs = ["field_generator.h"], |
||||||
|
strip_include_prefix = "/src", |
||||||
|
deps = [ |
||||||
|
"//src/google/protobuf/compiler/java:generator_common", |
||||||
|
"//src/google/protobuf/io:printer", |
||||||
|
], |
||||||
|
) |
||||||
|
|
||||||
|
# We have to expose the field generators individually (instead of just through the factory) because |
||||||
|
# the mutable generators delegate to them directly. |
||||||
|
cc_library( |
||||||
|
name = "fgs", |
||||||
|
srcs = [ |
||||||
|
"enum_field.cc", |
||||||
|
"map_field.cc", |
||||||
|
"message_field.cc", |
||||||
|
"primitive_field.cc", |
||||||
|
"string_field.cc", |
||||||
|
], |
||||||
|
hdrs = [ |
||||||
|
"enum_field.h", |
||||||
|
"map_field.h", |
||||||
|
"message_field.h", |
||||||
|
"primitive_field.h", |
||||||
|
"string_field.h", |
||||||
|
], |
||||||
|
strip_include_prefix = "/src", |
||||||
|
deps = [ |
||||||
|
":fg", |
||||||
|
"//src/google/protobuf", |
||||||
|
"//src/google/protobuf:port", |
||||||
|
"//src/google/protobuf/compiler/java:generator_common", |
||||||
|
"//src/google/protobuf/compiler/java:helpers", |
||||||
|
"//src/google/protobuf/compiler/java:internal_helpers", |
||||||
|
"//src/google/protobuf/io:printer", |
||||||
|
"@com_google_absl//absl/container:flat_hash_map", |
||||||
|
"@com_google_absl//absl/log:absl_check", |
||||||
|
"@com_google_absl//absl/log:absl_log", |
||||||
|
"@com_google_absl//absl/strings", |
||||||
|
], |
||||||
|
) |
||||||
|
|
||||||
|
cc_library( |
||||||
|
name = "mfg", |
||||||
|
srcs = ["make_field_generators.cc"], |
||||||
|
hdrs = ["make_field_generators.h"], |
||||||
|
strip_include_prefix = "/src", |
||||||
|
deps = [ |
||||||
|
":fg", |
||||||
|
":fgs", |
||||||
|
"//src/google/protobuf", |
||||||
|
"//src/google/protobuf/compiler/java:generator_common", |
||||||
|
"//src/google/protobuf/compiler/java:helpers", |
||||||
|
], |
||||||
|
) |
||||||
|
|
||||||
|
cc_library( |
||||||
|
name = "service", |
||||||
|
srcs = ["service.cc"], |
||||||
|
hdrs = ["service.h"], |
||||||
|
strip_include_prefix = "/src", |
||||||
|
visibility = ["//src/google/protobuf/compiler/java/lite:__pkg__"], |
||||||
|
deps = [ |
||||||
|
"//src/google/protobuf", |
||||||
|
"//src/google/protobuf:port", |
||||||
|
"//src/google/protobuf/compiler/java:generator_common", |
||||||
|
"//src/google/protobuf/compiler/java:helpers", |
||||||
|
"//src/google/protobuf/io:printer", |
||||||
|
"@com_google_absl//absl/log:absl_log", |
||||||
|
"@com_google_absl//absl/strings", |
||||||
|
], |
||||||
|
) |
||||||
|
|
||||||
|
cc_library( |
||||||
|
name = "eg", |
||||||
|
srcs = ["enum.cc"], |
||||||
|
hdrs = ["enum.h"], |
||||||
|
strip_include_prefix = "/src", |
||||||
|
deps = [ |
||||||
|
"//src/google/protobuf", |
||||||
|
"//src/google/protobuf:port", |
||||||
|
"//src/google/protobuf/compiler/java:generator_common", |
||||||
|
"//src/google/protobuf/compiler/java:helpers", |
||||||
|
"//src/google/protobuf/io:printer", |
||||||
|
"@com_google_absl//absl/container:flat_hash_map", |
||||||
|
"@com_google_absl//absl/strings", |
||||||
|
], |
||||||
|
) |
||||||
|
|
||||||
|
cc_library( |
||||||
|
name = "immutable", |
||||||
|
srcs = [ |
||||||
|
"extension.cc", |
||||||
|
"generator_factory.cc", |
||||||
|
"message.cc", |
||||||
|
"message.h", |
||||||
|
"message_builder.cc", |
||||||
|
], |
||||||
|
hdrs = [ |
||||||
|
"generator_factory.h", |
||||||
|
# We don't actually want to put the remaining headers in `hdrs`. |
||||||
|
# They are logically private, and should be in srcs=[], but |
||||||
|
# unfortunately `strip_include_prefix` doesn't have any effect |
||||||
|
# on headers listed in `srcs.` |
||||||
|
"message.h", |
||||||
|
"extension.h", |
||||||
|
"message_builder.h", |
||||||
|
], |
||||||
|
strip_include_prefix = "/src", |
||||||
|
visibility = ["//src/google/protobuf/compiler/java:__pkg__"], |
||||||
|
deps = [ |
||||||
|
":eg", |
||||||
|
":fg", |
||||||
|
":mfg", |
||||||
|
":service", |
||||||
|
"//src/google/protobuf", |
||||||
|
"//src/google/protobuf:port", |
||||||
|
"//src/google/protobuf/compiler/java:generator_common", |
||||||
|
"//src/google/protobuf/compiler/java:helpers", |
||||||
|
"//src/google/protobuf/compiler/java:message_serialization", |
||||||
|
"//src/google/protobuf/io", |
||||||
|
"//src/google/protobuf/io:printer", |
||||||
|
"@com_google_absl//absl/container:btree", |
||||||
|
"@com_google_absl//absl/container:flat_hash_map", |
||||||
|
"@com_google_absl//absl/log:absl_check", |
||||||
|
"@com_google_absl//absl/strings", |
||||||
|
], |
||||||
|
) |
@ -0,0 +1,54 @@ |
|||||||
|
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__ |
||||||
|
#define GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__ |
||||||
|
|
||||||
|
#include <string> |
||||||
|
|
||||||
|
#include "google/protobuf/compiler/java/generator_common.h" |
||||||
|
#include "google/protobuf/io/printer.h" |
||||||
|
|
||||||
|
namespace google { |
||||||
|
namespace protobuf { |
||||||
|
namespace compiler { |
||||||
|
namespace java { |
||||||
|
|
||||||
|
class ImmutableFieldGenerator : public FieldGenerator { |
||||||
|
public: |
||||||
|
ImmutableFieldGenerator() = default; |
||||||
|
ImmutableFieldGenerator(const ImmutableFieldGenerator&) = delete; |
||||||
|
ImmutableFieldGenerator& operator=(const ImmutableFieldGenerator&) = delete; |
||||||
|
~ImmutableFieldGenerator() override = default; |
||||||
|
|
||||||
|
virtual int GetMessageBitIndex() const = 0; |
||||||
|
virtual int GetBuilderBitIndex() const = 0; |
||||||
|
virtual int GetNumBitsForMessage() const = 0; |
||||||
|
virtual int GetNumBitsForBuilder() const = 0; |
||||||
|
virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0; |
||||||
|
virtual void GenerateMembers(io::Printer* printer) const = 0; |
||||||
|
virtual void GenerateBuilderMembers(io::Printer* printer) const = 0; |
||||||
|
virtual void GenerateInitializationCode(io::Printer* printer) const = 0; |
||||||
|
virtual void GenerateBuilderClearCode(io::Printer* printer) const = 0; |
||||||
|
virtual void GenerateMergingCode(io::Printer* printer) const = 0; |
||||||
|
virtual void GenerateBuildingCode(io::Printer* printer) const = 0; |
||||||
|
virtual void GenerateBuilderParsingCode(io::Printer* printer) const = 0; |
||||||
|
virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0; |
||||||
|
virtual void GenerateFieldBuilderInitializationCode( |
||||||
|
io::Printer* printer) const = 0; |
||||||
|
virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0; |
||||||
|
|
||||||
|
virtual void GenerateBuilderParsingCodeFromPacked( |
||||||
|
io::Printer* printer) const { |
||||||
|
ReportUnexpectedPackedFieldsCall(); |
||||||
|
} |
||||||
|
|
||||||
|
virtual void GenerateEqualsCode(io::Printer* printer) const = 0; |
||||||
|
virtual void GenerateHashCode(io::Printer* printer) const = 0; |
||||||
|
|
||||||
|
virtual std::string GetBoxedType() const = 0; |
||||||
|
}; |
||||||
|
|
||||||
|
} // namespace java
|
||||||
|
} // namespace compiler
|
||||||
|
} // namespace protobuf
|
||||||
|
} // namespace google
|
||||||
|
|
||||||
|
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__
|
@ -0,0 +1,67 @@ |
|||||||
|
// Protocol Buffers - Google's data interchange format
|
||||||
|
// Copyright 2008 Google Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file or at
|
||||||
|
// https://developers.google.com/open-source/licenses/bsd
|
||||||
|
|
||||||
|
// Author: liujisi@google.com (Pherl Liu)
|
||||||
|
|
||||||
|
#include "google/protobuf/compiler/java/generator_factory.h" |
||||||
|
|
||||||
|
#include <memory> |
||||||
|
|
||||||
|
#include "google/protobuf/compiler/java/context.h" |
||||||
|
#include "google/protobuf/compiler/java/immutable/enum.h" |
||||||
|
#include "google/protobuf/compiler/java/immutable/extension.h" |
||||||
|
#include "google/protobuf/compiler/java/immutable/message.h" |
||||||
|
#include "google/protobuf/compiler/java/immutable/service.h" |
||||||
|
#include "google/protobuf/descriptor.h" |
||||||
|
|
||||||
|
namespace google { |
||||||
|
namespace protobuf { |
||||||
|
namespace compiler { |
||||||
|
namespace java { |
||||||
|
|
||||||
|
// Factory that creates generators for immutable-default messages.
|
||||||
|
class ImmutableGeneratorFactory : public GeneratorFactory { |
||||||
|
public: |
||||||
|
explicit ImmutableGeneratorFactory(Context* context) : context_(context) {} |
||||||
|
ImmutableGeneratorFactory(const ImmutableGeneratorFactory&) = delete; |
||||||
|
ImmutableGeneratorFactory& operator=(const ImmutableGeneratorFactory&) = |
||||||
|
delete; |
||||||
|
~ImmutableGeneratorFactory() override = default; |
||||||
|
|
||||||
|
std::unique_ptr<MessageGenerator> NewMessageGenerator( |
||||||
|
const Descriptor* descriptor) const override { |
||||||
|
return std::make_unique<ImmutableMessageGenerator>(descriptor, context_); |
||||||
|
} |
||||||
|
|
||||||
|
std::unique_ptr<EnumGenerator> NewEnumGenerator( |
||||||
|
const EnumDescriptor* descriptor) const override { |
||||||
|
return std::make_unique<EnumNonLiteGenerator>(descriptor, true, context_); |
||||||
|
} |
||||||
|
|
||||||
|
std::unique_ptr<ExtensionGenerator> NewExtensionGenerator( |
||||||
|
const FieldDescriptor* descriptor) const override { |
||||||
|
return std::make_unique<ImmutableExtensionGenerator>(descriptor, context_); |
||||||
|
} |
||||||
|
|
||||||
|
std::unique_ptr<ServiceGenerator> NewServiceGenerator( |
||||||
|
const ServiceDescriptor* descriptor) const override { |
||||||
|
return std::make_unique<ImmutableServiceGenerator>(descriptor, context_); |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
Context* context_; |
||||||
|
}; |
||||||
|
|
||||||
|
std::unique_ptr<GeneratorFactory> MakeImmutableGeneratorFactory( |
||||||
|
Context* context) { |
||||||
|
return std::make_unique<ImmutableGeneratorFactory>(context); |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace java
|
||||||
|
} // namespace compiler
|
||||||
|
} // namespace protobuf
|
||||||
|
} // namespace google
|
@ -0,0 +1,19 @@ |
|||||||
|
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_GENERATOR_FACTORY_H__ |
||||||
|
#define GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_GENERATOR_FACTORY_H__ |
||||||
|
|
||||||
|
#include "google/protobuf/compiler/java/generator_factory.h" |
||||||
|
|
||||||
|
namespace google { |
||||||
|
namespace protobuf { |
||||||
|
namespace compiler { |
||||||
|
namespace java { |
||||||
|
|
||||||
|
std::unique_ptr<GeneratorFactory> MakeImmutableGeneratorFactory( |
||||||
|
Context* context); |
||||||
|
|
||||||
|
} // namespace java
|
||||||
|
} // namespace compiler
|
||||||
|
} // namespace protobuf
|
||||||
|
} // namespace google
|
||||||
|
|
||||||
|
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_GENERATOR_FACTORY_H__
|
@ -0,0 +1,114 @@ |
|||||||
|
// Protocol Buffers - Google's data interchange format
|
||||||
|
// Copyright 2008 Google Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file or at
|
||||||
|
// https://developers.google.com/open-source/licenses/bsd
|
||||||
|
|
||||||
|
// Author: kenton@google.com (Kenton Varda)
|
||||||
|
// Based on original Protocol Buffers design by
|
||||||
|
// Sanjay Ghemawat, Jeff Dean, and others.
|
||||||
|
|
||||||
|
#include <memory> |
||||||
|
#include <utility> |
||||||
|
|
||||||
|
#include "google/protobuf/compiler/java/context.h" |
||||||
|
#include "google/protobuf/compiler/java/generator_common.h" |
||||||
|
#include "google/protobuf/compiler/java/helpers.h" |
||||||
|
#include "google/protobuf/compiler/java/immutable/enum_field.h" |
||||||
|
#include "google/protobuf/compiler/java/immutable/field_generator.h" |
||||||
|
#include "google/protobuf/compiler/java/immutable/map_field.h" |
||||||
|
#include "google/protobuf/compiler/java/immutable/message_field.h" |
||||||
|
#include "google/protobuf/compiler/java/immutable/primitive_field.h" |
||||||
|
#include "google/protobuf/compiler/java/immutable/string_field.h" |
||||||
|
#include "google/protobuf/descriptor.h" |
||||||
|
|
||||||
|
namespace google { |
||||||
|
namespace protobuf { |
||||||
|
namespace compiler { |
||||||
|
namespace java { |
||||||
|
|
||||||
|
namespace { |
||||||
|
|
||||||
|
std::unique_ptr<ImmutableFieldGenerator> MakeImmutableGenerator( |
||||||
|
const FieldDescriptor* field, int messageBitIndex, int builderBitIndex, |
||||||
|
Context* context) { |
||||||
|
if (field->is_repeated()) { |
||||||
|
switch (GetJavaType(field)) { |
||||||
|
case JAVATYPE_MESSAGE: |
||||||
|
if (IsMapEntry(field->message_type())) { |
||||||
|
return std::make_unique<ImmutableMapFieldGenerator>( |
||||||
|
field, messageBitIndex, builderBitIndex, context); |
||||||
|
} else { |
||||||
|
return std::make_unique<RepeatedImmutableMessageFieldGenerator>( |
||||||
|
field, messageBitIndex, builderBitIndex, context); |
||||||
|
} |
||||||
|
case JAVATYPE_ENUM: |
||||||
|
return std::make_unique<RepeatedImmutableEnumFieldGenerator>( |
||||||
|
field, messageBitIndex, builderBitIndex, context); |
||||||
|
case JAVATYPE_STRING: |
||||||
|
return std::make_unique<RepeatedImmutableStringFieldGenerator>( |
||||||
|
field, messageBitIndex, builderBitIndex, context); |
||||||
|
default: |
||||||
|
return std::make_unique<RepeatedImmutablePrimitiveFieldGenerator>( |
||||||
|
field, messageBitIndex, builderBitIndex, context); |
||||||
|
} |
||||||
|
} else { |
||||||
|
if (IsRealOneof(field)) { |
||||||
|
switch (GetJavaType(field)) { |
||||||
|
case JAVATYPE_MESSAGE: |
||||||
|
return std::make_unique<ImmutableMessageOneofFieldGenerator>( |
||||||
|
field, messageBitIndex, builderBitIndex, context); |
||||||
|
case JAVATYPE_ENUM: |
||||||
|
return std::make_unique<ImmutableEnumOneofFieldGenerator>( |
||||||
|
field, messageBitIndex, builderBitIndex, context); |
||||||
|
case JAVATYPE_STRING: |
||||||
|
return std::make_unique<ImmutableStringOneofFieldGenerator>( |
||||||
|
field, messageBitIndex, builderBitIndex, context); |
||||||
|
default: |
||||||
|
return std::make_unique<ImmutablePrimitiveOneofFieldGenerator>( |
||||||
|
field, messageBitIndex, builderBitIndex, context); |
||||||
|
} |
||||||
|
} else { |
||||||
|
switch (GetJavaType(field)) { |
||||||
|
case JAVATYPE_MESSAGE: |
||||||
|
return std::make_unique<ImmutableMessageFieldGenerator>( |
||||||
|
field, messageBitIndex, builderBitIndex, context); |
||||||
|
case JAVATYPE_ENUM: |
||||||
|
return std::make_unique<ImmutableEnumFieldGenerator>( |
||||||
|
field, messageBitIndex, builderBitIndex, context); |
||||||
|
case JAVATYPE_STRING: |
||||||
|
return std::make_unique<ImmutableStringFieldGenerator>( |
||||||
|
field, messageBitIndex, builderBitIndex, context); |
||||||
|
default: |
||||||
|
return std::make_unique<ImmutablePrimitiveFieldGenerator>( |
||||||
|
field, messageBitIndex, builderBitIndex, context); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
FieldGeneratorMap<ImmutableFieldGenerator> MakeImmutableFieldGenerators( |
||||||
|
const Descriptor* descriptor, Context* context) { |
||||||
|
// Construct all the FieldGenerators and assign them bit indices for their
|
||||||
|
// bit fields.
|
||||||
|
int messageBitIndex = 0; |
||||||
|
int builderBitIndex = 0; |
||||||
|
FieldGeneratorMap<ImmutableFieldGenerator> ret(descriptor); |
||||||
|
for (int i = 0; i < descriptor->field_count(); i++) { |
||||||
|
const FieldDescriptor* field = descriptor->field(i); |
||||||
|
auto generator = MakeImmutableGenerator(field, messageBitIndex, |
||||||
|
builderBitIndex, context); |
||||||
|
messageBitIndex += generator->GetNumBitsForMessage(); |
||||||
|
builderBitIndex += generator->GetNumBitsForBuilder(); |
||||||
|
ret.Add(field, std::move(generator)); |
||||||
|
} |
||||||
|
return ret; |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace java
|
||||||
|
} // namespace compiler
|
||||||
|
} // namespace protobuf
|
||||||
|
} // namespace google
|
@ -0,0 +1,29 @@ |
|||||||
|
// Protocol Buffers - Google's data interchange format
|
||||||
|
// Copyright 2024 Google Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file or at
|
||||||
|
// https://developers.google.com/open-source/licenses/bsd
|
||||||
|
|
||||||
|
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_MAKE_FIELD_GENERATORS_H__ |
||||||
|
#define GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_MAKE_FIELD_GENERATORS_H__ |
||||||
|
|
||||||
|
#include "google/protobuf/compiler/java/context.h" |
||||||
|
#include "google/protobuf/compiler/java/generator_common.h" |
||||||
|
#include "google/protobuf/compiler/java/immutable/field_generator.h" |
||||||
|
#include "google/protobuf/descriptor.h" |
||||||
|
|
||||||
|
namespace google { |
||||||
|
namespace protobuf { |
||||||
|
namespace compiler { |
||||||
|
namespace java { |
||||||
|
|
||||||
|
FieldGeneratorMap<ImmutableFieldGenerator> MakeImmutableFieldGenerators( |
||||||
|
const Descriptor* descriptor, Context* context); |
||||||
|
|
||||||
|
} // namespace java
|
||||||
|
} // namespace compiler
|
||||||
|
} // namespace protobuf
|
||||||
|
} // namespace google
|
||||||
|
|
||||||
|
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_MAKE_FIELD_GENERATORS_H__
|
@ -0,0 +1,114 @@ |
|||||||
|
// Protocol Buffers - Google's data interchange format
|
||||||
|
// Copyright 2008 Google Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file or at
|
||||||
|
// https://developers.google.com/open-source/licenses/bsd
|
||||||
|
|
||||||
|
// Author: kenton@google.com (Kenton Varda)
|
||||||
|
// Based on original Protocol Buffers design by
|
||||||
|
// Sanjay Ghemawat, Jeff Dean, and others.
|
||||||
|
|
||||||
|
#include "google/protobuf/compiler/java/internal_helpers.h" |
||||||
|
|
||||||
|
#include "absl/log/absl_log.h" |
||||||
|
#include "google/protobuf/compiler/java/helpers.h" |
||||||
|
#include "google/protobuf/compiler/java/name_resolver.h" |
||||||
|
#include "google/protobuf/descriptor.pb.h" |
||||||
|
|
||||||
|
// Must be last.
|
||||||
|
#include "google/protobuf/port_def.inc" |
||||||
|
|
||||||
|
namespace google { |
||||||
|
namespace protobuf { |
||||||
|
namespace compiler { |
||||||
|
namespace java { |
||||||
|
namespace { |
||||||
|
|
||||||
|
int GetExperimentalJavaFieldTypeForSingular(const FieldDescriptor* field) { |
||||||
|
// j/c/g/protobuf/FieldType.java lists field types in a slightly different
|
||||||
|
// order from FieldDescriptor::Type so we can't do a simple cast.
|
||||||
|
//
|
||||||
|
// TODO: Make j/c/g/protobuf/FieldType.java follow the same order.
|
||||||
|
int result = field->type(); |
||||||
|
if (result == FieldDescriptor::TYPE_GROUP) { |
||||||
|
return 17; |
||||||
|
} else if (result < FieldDescriptor::TYPE_GROUP) { |
||||||
|
return result - 1; |
||||||
|
} else { |
||||||
|
return result - 2; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
int GetExperimentalJavaFieldTypeForRepeated(const FieldDescriptor* field) { |
||||||
|
if (field->type() == FieldDescriptor::TYPE_GROUP) { |
||||||
|
return 49; |
||||||
|
} else { |
||||||
|
return GetExperimentalJavaFieldTypeForSingular(field) + 18; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
int GetExperimentalJavaFieldTypeForPacked(const FieldDescriptor* field) { |
||||||
|
int result = field->type(); |
||||||
|
if (result < FieldDescriptor::TYPE_STRING) { |
||||||
|
return result + 34; |
||||||
|
} else if (result > FieldDescriptor::TYPE_BYTES) { |
||||||
|
return result + 30; |
||||||
|
} else { |
||||||
|
ABSL_LOG(FATAL) << field->full_name() << " can't be packed."; |
||||||
|
return 0; |
||||||
|
} |
||||||
|
} |
||||||
|
} // namespace
|
||||||
|
|
||||||
|
int GetExperimentalJavaFieldType(const FieldDescriptor* field) { |
||||||
|
static const int kMapFieldType = 50; |
||||||
|
static const int kOneofFieldTypeOffset = 51; |
||||||
|
|
||||||
|
static const int kRequiredBit = 0x100; |
||||||
|
static const int kUtf8CheckBit = 0x200; |
||||||
|
static const int kCheckInitialized = 0x400; |
||||||
|
static const int kLegacyEnumIsClosedBit = 0x800; |
||||||
|
static const int kHasHasBit = 0x1000; |
||||||
|
int extra_bits = field->is_required() ? kRequiredBit : 0; |
||||||
|
if (field->type() == FieldDescriptor::TYPE_STRING && CheckUtf8(field)) { |
||||||
|
extra_bits |= kUtf8CheckBit; |
||||||
|
} |
||||||
|
if (field->is_required() || (GetJavaType(field) == JAVATYPE_MESSAGE && |
||||||
|
HasRequiredFields(field->message_type()))) { |
||||||
|
extra_bits |= kCheckInitialized; |
||||||
|
} |
||||||
|
if (HasHasbit(field)) { |
||||||
|
extra_bits |= kHasHasBit; |
||||||
|
} |
||||||
|
if (GetJavaType(field) == JAVATYPE_ENUM && !SupportUnknownEnumValue(field)) { |
||||||
|
extra_bits |= kLegacyEnumIsClosedBit; |
||||||
|
} |
||||||
|
|
||||||
|
if (field->is_map()) { |
||||||
|
if (!SupportUnknownEnumValue(MapValueField(field))) { |
||||||
|
const FieldDescriptor* value = field->message_type()->map_value(); |
||||||
|
if (GetJavaType(value) == JAVATYPE_ENUM) { |
||||||
|
extra_bits |= kLegacyEnumIsClosedBit; |
||||||
|
} |
||||||
|
} |
||||||
|
return kMapFieldType | extra_bits; |
||||||
|
} else if (field->is_packed()) { |
||||||
|
return GetExperimentalJavaFieldTypeForPacked(field) | extra_bits; |
||||||
|
} else if (field->is_repeated()) { |
||||||
|
return GetExperimentalJavaFieldTypeForRepeated(field) | extra_bits; |
||||||
|
} else if (IsRealOneof(field)) { |
||||||
|
return (GetExperimentalJavaFieldTypeForSingular(field) + |
||||||
|
kOneofFieldTypeOffset) | |
||||||
|
extra_bits; |
||||||
|
} else { |
||||||
|
return GetExperimentalJavaFieldTypeForSingular(field) | extra_bits; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace java
|
||||||
|
} // namespace compiler
|
||||||
|
} // namespace protobuf
|
||||||
|
} // namespace google
|
||||||
|
|
||||||
|
#include "google/protobuf/port_undef.inc" |
@ -0,0 +1,69 @@ |
|||||||
|
// Protocol Buffers - Google's data interchange format
|
||||||
|
// Copyright 2008 Google Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file or at
|
||||||
|
// https://developers.google.com/open-source/licenses/bsd
|
||||||
|
|
||||||
|
// Author: kenton@google.com (Kenton Varda)
|
||||||
|
// Based on original Protocol Buffers design by
|
||||||
|
// Sanjay Ghemawat, Jeff Dean, and others.
|
||||||
|
|
||||||
|
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_INTERNAL_HELPERS_H__ |
||||||
|
#define GOOGLE_PROTOBUF_COMPILER_JAVA_INTERNAL_HELPERS_H__ |
||||||
|
|
||||||
|
#include "google/protobuf/compiler/java/generator.h" |
||||||
|
#include "google/protobuf/compiler/java/java_features.pb.h" |
||||||
|
#include "google/protobuf/compiler/java/names.h" |
||||||
|
#include "google/protobuf/descriptor.h" |
||||||
|
#include "google/protobuf/descriptor.pb.h" |
||||||
|
|
||||||
|
// Must be last.
|
||||||
|
#include "google/protobuf/port_def.inc" |
||||||
|
|
||||||
|
namespace google { |
||||||
|
namespace protobuf { |
||||||
|
namespace compiler { |
||||||
|
namespace java { |
||||||
|
|
||||||
|
// Whether unknown enum values are kept (i.e., not stored in UnknownFieldSet
|
||||||
|
// but in the message and can be queried using additional getters that return
|
||||||
|
// ints.
|
||||||
|
inline bool SupportUnknownEnumValue(const FieldDescriptor* field) { |
||||||
|
if (JavaGenerator::GetResolvedSourceFeatures(*field) |
||||||
|
.GetExtension(pb::java) |
||||||
|
.legacy_closed_enum()) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
return field->enum_type() != nullptr && !field->enum_type()->is_closed(); |
||||||
|
} |
||||||
|
|
||||||
|
inline bool CheckUtf8(const FieldDescriptor* descriptor) { |
||||||
|
if (JavaGenerator::GetResolvedSourceFeatures(*descriptor) |
||||||
|
.GetExtension(pb::java) |
||||||
|
.utf8_validation() == pb::JavaFeatures::VERIFY) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
return JavaGenerator::GetResolvedSourceFeatures(*descriptor) |
||||||
|
.utf8_validation() == FeatureSet::VERIFY || |
||||||
|
// For legacy syntax. This is not allowed under Editions.
|
||||||
|
descriptor->file()->options().java_string_check_utf8(); |
||||||
|
} |
||||||
|
|
||||||
|
// Only the lowest two bytes of the return value are used. The lowest byte
|
||||||
|
// is the integer value of a j/c/g/protobuf/FieldType enum. For the other
|
||||||
|
// byte:
|
||||||
|
// bit 0: whether the field is required.
|
||||||
|
// bit 1: whether the field requires UTF-8 validation.
|
||||||
|
// bit 2: whether the field needs isInitialized check.
|
||||||
|
// bit 3: whether the field is a map field with proto2 enum value.
|
||||||
|
// bits 4-7: unused
|
||||||
|
int GetExperimentalJavaFieldType(const FieldDescriptor* field); |
||||||
|
|
||||||
|
} // namespace java
|
||||||
|
} // namespace compiler
|
||||||
|
} // namespace protobuf
|
||||||
|
} // namespace google
|
||||||
|
|
||||||
|
#include "google/protobuf/port_undef.inc" |
||||||
|
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_INTERNAL_HELPERS_H__
|
@ -0,0 +1,79 @@ |
|||||||
|
cc_library( |
||||||
|
name = "field_generators", |
||||||
|
srcs = [ |
||||||
|
"enum_field.cc", |
||||||
|
"extension.cc", |
||||||
|
"make_field_generators.cc", |
||||||
|
"map_field.cc", |
||||||
|
"message_field.cc", |
||||||
|
"primitive_field.cc", |
||||||
|
"string_field.cc", |
||||||
|
], |
||||||
|
hdrs = [ |
||||||
|
"field_generator.h", |
||||||
|
"make_field_generators.h", |
||||||
|
# We don't actually want to put the remaining headers in `hdrs`. |
||||||
|
# They are logically private, and should be in srcs=[], but |
||||||
|
# unfortunately `strip_include_prefix` doesn't have any effect |
||||||
|
# on headers listed in `srcs.` |
||||||
|
"map_field.h", |
||||||
|
"enum_field.h", |
||||||
|
"extension.h", |
||||||
|
"message_field.h", |
||||||
|
"primitive_field.h", |
||||||
|
"string_field.h", |
||||||
|
], |
||||||
|
strip_include_prefix = "/src", |
||||||
|
visibility = ["//src/google/protobuf/compiler/java:__subpackages__"], |
||||||
|
deps = [ |
||||||
|
"//src/google/protobuf", |
||||||
|
"//src/google/protobuf:port", |
||||||
|
"//src/google/protobuf/compiler/java:generator_common", |
||||||
|
"//src/google/protobuf/compiler/java:helpers", |
||||||
|
"//src/google/protobuf/compiler/java:internal_helpers", |
||||||
|
"//src/google/protobuf/io:printer", |
||||||
|
"@com_google_absl//absl/container:flat_hash_map", |
||||||
|
"@com_google_absl//absl/log:absl_check", |
||||||
|
"@com_google_absl//absl/log:absl_log", |
||||||
|
"@com_google_absl//absl/strings", |
||||||
|
], |
||||||
|
) |
||||||
|
|
||||||
|
cc_library( |
||||||
|
name = "lite", |
||||||
|
srcs = [ |
||||||
|
"enum.cc", |
||||||
|
"extension.cc", |
||||||
|
"generator_factory.cc", |
||||||
|
"message.cc", |
||||||
|
"message_builder.cc", |
||||||
|
], |
||||||
|
hdrs = [ |
||||||
|
"generator_factory.h", |
||||||
|
# We don't actually want to put the remaining headers in `hdrs`. |
||||||
|
# They are logically private, and should be in srcs=[], but |
||||||
|
# unfortunately `strip_include_prefix` doesn't have any effect |
||||||
|
# on headers listed in `srcs.` |
||||||
|
"enum.h", |
||||||
|
"extension.h", |
||||||
|
"message.h", |
||||||
|
"message_builder.h", |
||||||
|
], |
||||||
|
strip_include_prefix = "/src", |
||||||
|
visibility = ["//src/google/protobuf/compiler/java:__subpackages__"], |
||||||
|
deps = [ |
||||||
|
":field_generators", |
||||||
|
"//src/google/protobuf", |
||||||
|
"//src/google/protobuf:port", |
||||||
|
"//src/google/protobuf/compiler/java:generator_common", |
||||||
|
"//src/google/protobuf/compiler/java:helpers", |
||||||
|
"//src/google/protobuf/compiler/java:internal_helpers", |
||||||
|
"//src/google/protobuf/compiler/java/immutable:service", |
||||||
|
"//src/google/protobuf/io", |
||||||
|
"//src/google/protobuf/io:printer", |
||||||
|
"@com_google_absl//absl/container:btree", |
||||||
|
"@com_google_absl//absl/container:flat_hash_map", |
||||||
|
"@com_google_absl//absl/log:absl_check", |
||||||
|
"@com_google_absl//absl/strings", |
||||||
|
], |
||||||
|
) |
@ -0,0 +1,38 @@ |
|||||||
|
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__ |
||||||
|
#define GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__ |
||||||
|
|
||||||
|
#include <string> |
||||||
|
|
||||||
|
#include "google/protobuf/io/printer.h" |
||||||
|
|
||||||
|
namespace google { |
||||||
|
namespace protobuf { |
||||||
|
namespace compiler { |
||||||
|
namespace java { |
||||||
|
|
||||||
|
class ImmutableFieldLiteGenerator { |
||||||
|
public: |
||||||
|
ImmutableFieldLiteGenerator() = default; |
||||||
|
ImmutableFieldLiteGenerator(const ImmutableFieldLiteGenerator&) = delete; |
||||||
|
ImmutableFieldLiteGenerator& operator=(const ImmutableFieldLiteGenerator&) = |
||||||
|
delete; |
||||||
|
virtual ~ImmutableFieldLiteGenerator() = default; |
||||||
|
|
||||||
|
virtual int GetNumBitsForMessage() const = 0; |
||||||
|
virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0; |
||||||
|
virtual void GenerateMembers(io::Printer* printer) const = 0; |
||||||
|
virtual void GenerateBuilderMembers(io::Printer* printer) const = 0; |
||||||
|
virtual void GenerateInitializationCode(io::Printer* printer) const = 0; |
||||||
|
virtual void GenerateFieldInfo(io::Printer* printer, |
||||||
|
std::vector<uint16_t>* output) const = 0; |
||||||
|
virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0; |
||||||
|
|
||||||
|
virtual std::string GetBoxedType() const = 0; |
||||||
|
}; |
||||||
|
|
||||||
|
} // namespace java
|
||||||
|
} // namespace compiler
|
||||||
|
} // namespace protobuf
|
||||||
|
} // namespace google
|
||||||
|
|
||||||
|
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_IMMUTABLE_FIELD_GENERATOR_H__
|
@ -0,0 +1,70 @@ |
|||||||
|
// Protocol Buffers - Google's data interchange format
|
||||||
|
// Copyright 2008 Google Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file or at
|
||||||
|
// https://developers.google.com/open-source/licenses/bsd
|
||||||
|
|
||||||
|
// Author: liujisi@google.com (Pherl Liu)
|
||||||
|
|
||||||
|
#include "google/protobuf/compiler/java/generator_factory.h" |
||||||
|
|
||||||
|
#include <memory> |
||||||
|
|
||||||
|
#include "google/protobuf/compiler/java/context.h" |
||||||
|
#include "google/protobuf/compiler/java/immutable/service.h" |
||||||
|
#include "google/protobuf/compiler/java/lite/enum.h" |
||||||
|
#include "google/protobuf/compiler/java/lite/extension.h" |
||||||
|
#include "google/protobuf/compiler/java/lite/message.h" |
||||||
|
#include "google/protobuf/descriptor.h" |
||||||
|
|
||||||
|
namespace google { |
||||||
|
namespace protobuf { |
||||||
|
namespace compiler { |
||||||
|
namespace java { |
||||||
|
|
||||||
|
// Factory that creates generators for immutable-default messages.
|
||||||
|
class ImmutableLiteGeneratorFactory : public GeneratorFactory { |
||||||
|
public: |
||||||
|
explicit ImmutableLiteGeneratorFactory(Context* context) |
||||||
|
: context_(context) {} |
||||||
|
ImmutableLiteGeneratorFactory(const ImmutableLiteGeneratorFactory&) = delete; |
||||||
|
ImmutableLiteGeneratorFactory& operator=( |
||||||
|
const ImmutableLiteGeneratorFactory&) = delete; |
||||||
|
~ImmutableLiteGeneratorFactory() override = default; |
||||||
|
|
||||||
|
std::unique_ptr<MessageGenerator> NewMessageGenerator( |
||||||
|
const Descriptor* descriptor) const override { |
||||||
|
return std::make_unique<ImmutableMessageLiteGenerator>(descriptor, |
||||||
|
context_); |
||||||
|
} |
||||||
|
|
||||||
|
std::unique_ptr<EnumGenerator> NewEnumGenerator( |
||||||
|
const EnumDescriptor* descriptor) const override { |
||||||
|
return std::make_unique<EnumLiteGenerator>(descriptor, true, context_); |
||||||
|
} |
||||||
|
|
||||||
|
std::unique_ptr<ExtensionGenerator> NewExtensionGenerator( |
||||||
|
const FieldDescriptor* descriptor) const override { |
||||||
|
return std::make_unique<ImmutableExtensionLiteGenerator>(descriptor, |
||||||
|
context_); |
||||||
|
} |
||||||
|
|
||||||
|
std::unique_ptr<ServiceGenerator> NewServiceGenerator( |
||||||
|
const ServiceDescriptor* descriptor) const override { |
||||||
|
return std::make_unique<ImmutableServiceGenerator>(descriptor, context_); |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
Context* context_; |
||||||
|
}; |
||||||
|
|
||||||
|
std::unique_ptr<GeneratorFactory> MakeImmutableLiteGeneratorFactory( |
||||||
|
Context* context) { |
||||||
|
return std::make_unique<ImmutableLiteGeneratorFactory>(context); |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace java
|
||||||
|
} // namespace compiler
|
||||||
|
} // namespace protobuf
|
||||||
|
} // namespace google
|
@ -0,0 +1,19 @@ |
|||||||
|
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_GENERATOR_FACTORY_H__ |
||||||
|
#define GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_GENERATOR_FACTORY_H__ |
||||||
|
|
||||||
|
#include "google/protobuf/compiler/java/generator_factory.h" |
||||||
|
|
||||||
|
namespace google { |
||||||
|
namespace protobuf { |
||||||
|
namespace compiler { |
||||||
|
namespace java { |
||||||
|
|
||||||
|
std::unique_ptr<GeneratorFactory> MakeImmutableLiteGeneratorFactory( |
||||||
|
Context* context); |
||||||
|
|
||||||
|
} // namespace java
|
||||||
|
} // namespace compiler
|
||||||
|
} // namespace protobuf
|
||||||
|
} // namespace google
|
||||||
|
|
||||||
|
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_GENERATOR_FACTORY_H__
|
@ -0,0 +1,110 @@ |
|||||||
|
// Protocol Buffers - Google's data interchange format
|
||||||
|
// Copyright 2008 Google Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file or at
|
||||||
|
// https://developers.google.com/open-source/licenses/bsd
|
||||||
|
|
||||||
|
// Author: kenton@google.com (Kenton Varda)
|
||||||
|
// Based on original Protocol Buffers design by
|
||||||
|
// Sanjay Ghemawat, Jeff Dean, and others.
|
||||||
|
|
||||||
|
#include "google/protobuf/compiler/java/lite/make_field_generators.h" |
||||||
|
|
||||||
|
#include <memory> |
||||||
|
#include <utility> |
||||||
|
|
||||||
|
#include "google/protobuf/compiler/java/context.h" |
||||||
|
#include "google/protobuf/compiler/java/generator_common.h" |
||||||
|
#include "google/protobuf/compiler/java/helpers.h" |
||||||
|
#include "google/protobuf/compiler/java/lite/enum_field.h" |
||||||
|
#include "google/protobuf/compiler/java/lite/field_generator.h" |
||||||
|
#include "google/protobuf/compiler/java/lite/map_field.h" |
||||||
|
#include "google/protobuf/compiler/java/lite/message_field.h" |
||||||
|
#include "google/protobuf/compiler/java/lite/primitive_field.h" |
||||||
|
#include "google/protobuf/compiler/java/lite/string_field.h" |
||||||
|
|
||||||
|
namespace google { |
||||||
|
namespace protobuf { |
||||||
|
namespace compiler { |
||||||
|
namespace java { |
||||||
|
|
||||||
|
namespace { |
||||||
|
|
||||||
|
std::unique_ptr<ImmutableFieldLiteGenerator> CreateImmutableLiteGenerator( |
||||||
|
const FieldDescriptor* field, int messageBitIndex, Context* context) { |
||||||
|
if (field->is_repeated()) { |
||||||
|
switch (GetJavaType(field)) { |
||||||
|
case JAVATYPE_MESSAGE: |
||||||
|
if (IsMapEntry(field->message_type())) { |
||||||
|
return std::make_unique<ImmutableMapFieldLiteGenerator>( |
||||||
|
field, messageBitIndex, context); |
||||||
|
} else { |
||||||
|
return std::make_unique<RepeatedImmutableMessageFieldLiteGenerator>( |
||||||
|
field, messageBitIndex, context); |
||||||
|
} |
||||||
|
case JAVATYPE_ENUM: |
||||||
|
return std::make_unique<RepeatedImmutableEnumFieldLiteGenerator>( |
||||||
|
field, messageBitIndex, context); |
||||||
|
case JAVATYPE_STRING: |
||||||
|
return std::make_unique<RepeatedImmutableStringFieldLiteGenerator>( |
||||||
|
field, messageBitIndex, context); |
||||||
|
default: |
||||||
|
return std::make_unique<RepeatedImmutablePrimitiveFieldLiteGenerator>( |
||||||
|
field, messageBitIndex, context); |
||||||
|
} |
||||||
|
} else { |
||||||
|
if (IsRealOneof(field)) { |
||||||
|
switch (GetJavaType(field)) { |
||||||
|
case JAVATYPE_MESSAGE: |
||||||
|
return std::make_unique<ImmutableMessageOneofFieldLiteGenerator>( |
||||||
|
field, messageBitIndex, context); |
||||||
|
case JAVATYPE_ENUM: |
||||||
|
return std::make_unique<ImmutableEnumOneofFieldLiteGenerator>( |
||||||
|
field, messageBitIndex, context); |
||||||
|
case JAVATYPE_STRING: |
||||||
|
return std::make_unique<ImmutableStringOneofFieldLiteGenerator>( |
||||||
|
field, messageBitIndex, context); |
||||||
|
default: |
||||||
|
return std::make_unique<ImmutablePrimitiveOneofFieldLiteGenerator>( |
||||||
|
field, messageBitIndex, context); |
||||||
|
} |
||||||
|
} else { |
||||||
|
switch (GetJavaType(field)) { |
||||||
|
case JAVATYPE_MESSAGE: |
||||||
|
return std::make_unique<ImmutableMessageFieldLiteGenerator>( |
||||||
|
field, messageBitIndex, context); |
||||||
|
case JAVATYPE_ENUM: |
||||||
|
return std::make_unique<ImmutableEnumFieldLiteGenerator>( |
||||||
|
field, messageBitIndex, context); |
||||||
|
case JAVATYPE_STRING: |
||||||
|
return std::make_unique<ImmutableStringFieldLiteGenerator>( |
||||||
|
field, messageBitIndex, context); |
||||||
|
default: |
||||||
|
return std::make_unique<ImmutablePrimitiveFieldLiteGenerator>( |
||||||
|
field, messageBitIndex, context); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
FieldGeneratorMap<ImmutableFieldLiteGenerator> MakeImmutableFieldLiteGenerators( |
||||||
|
const Descriptor* descriptor, Context* context) { |
||||||
|
int messageBitIndex = 0; |
||||||
|
FieldGeneratorMap<ImmutableFieldLiteGenerator> field_generators(descriptor); |
||||||
|
for (int i = 0; i < descriptor->field_count(); i++) { |
||||||
|
const FieldDescriptor* field = descriptor->field(i); |
||||||
|
auto generator = |
||||||
|
CreateImmutableLiteGenerator(field, messageBitIndex, context); |
||||||
|
messageBitIndex += generator->GetNumBitsForMessage(); |
||||||
|
field_generators.Add(field, std::move(generator)); |
||||||
|
} |
||||||
|
return field_generators; |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace java
|
||||||
|
} // namespace compiler
|
||||||
|
} // namespace protobuf
|
||||||
|
} // namespace google
|
@ -0,0 +1,32 @@ |
|||||||
|
// Protocol Buffers - Google's data interchange format
|
||||||
|
// Copyright 2024 Google Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file or at
|
||||||
|
// https://developers.google.com/open-source/licenses/bsd
|
||||||
|
|
||||||
|
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_MAKE_FIELD_GENERATORS_H__ |
||||||
|
#define GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_MAKE_FIELD_GENERATORS_H__ |
||||||
|
|
||||||
|
#include <memory> |
||||||
|
#include <vector> |
||||||
|
|
||||||
|
#include "google/protobuf/compiler/java/context.h" |
||||||
|
#include "google/protobuf/compiler/java/generator_common.h" |
||||||
|
#include "google/protobuf/compiler/java/lite/field_generator.h" |
||||||
|
#include "google/protobuf/descriptor.h" |
||||||
|
|
||||||
|
namespace google { |
||||||
|
namespace protobuf { |
||||||
|
namespace compiler { |
||||||
|
namespace java { |
||||||
|
|
||||||
|
FieldGeneratorMap<ImmutableFieldLiteGenerator> MakeImmutableFieldLiteGenerators( |
||||||
|
const Descriptor* descriptor, Context* context); |
||||||
|
|
||||||
|
} // namespace java
|
||||||
|
} // namespace compiler
|
||||||
|
} // namespace protobuf
|
||||||
|
} // namespace google
|
||||||
|
|
||||||
|
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_LITE_MAKE_FIELD_GENERATORS_H__
|
Loading…
Reference in new issue