Migrate writer to io::Printer for C#

This commit is contained in:
Jie Luo 2015-06-03 18:02:17 -07:00
parent a21a2cf7d3
commit 90da3514cd
28 changed files with 1845 additions and 1631 deletions

View File

@ -6432,7 +6432,7 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public bool HasOneofNestedMessage { public bool HasOneofNestedMessage {
get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage; }
} }
public global::Google.ProtocolBuffers.TestProtos.TestAllTypes.Types.NestedMessage OneofNestedMessage { public global::Google.ProtocolBuffers.TestProtos.TestAllTypes.Types.NestedMessage OneofNestedMessage {
get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage ? (global::Google.ProtocolBuffers.TestProtos.TestAllTypes.Types.NestedMessage) result.oneofField_ : global::Google.ProtocolBuffers.TestProtos.TestAllTypes.Types.NestedMessage.DefaultInstance; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage ? (global::Google.ProtocolBuffers.TestProtos.TestAllTypes.Types.NestedMessage) result.oneofField_ : global::Google.ProtocolBuffers.TestProtos.TestAllTypes.Types.NestedMessage.DefaultInstance; }
@ -22008,7 +22008,7 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public bool HasFooMessage { public bool HasFooMessage {
get { return result.fooCase_ == FooOneofCase.FooMessage; } get { return result.fooCase_ == FooOneofCase.FooMessage; }
} }
public global::Google.ProtocolBuffers.TestProtos.TestAllTypes FooMessage { public global::Google.ProtocolBuffers.TestProtos.TestAllTypes FooMessage {
get { return result.fooCase_ == FooOneofCase.FooMessage ? (global::Google.ProtocolBuffers.TestProtos.TestAllTypes) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestAllTypes.DefaultInstance; } get { return result.fooCase_ == FooOneofCase.FooMessage ? (global::Google.ProtocolBuffers.TestProtos.TestAllTypes) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestAllTypes.DefaultInstance; }
@ -22050,7 +22050,7 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public bool HasFooGroup { public bool HasFooGroup {
get { return result.fooCase_ == FooOneofCase.FooGroup; } get { return result.fooCase_ == FooOneofCase.FooGroup; }
} }
public global::Google.ProtocolBuffers.TestProtos.TestOneof.Types.FooGroup FooGroup { public global::Google.ProtocolBuffers.TestProtos.TestOneof.Types.FooGroup FooGroup {
get { return result.fooCase_ == FooOneofCase.FooGroup ? (global::Google.ProtocolBuffers.TestProtos.TestOneof.Types.FooGroup) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestOneof.Types.FooGroup.DefaultInstance; } get { return result.fooCase_ == FooOneofCase.FooGroup ? (global::Google.ProtocolBuffers.TestProtos.TestOneof.Types.FooGroup) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestOneof.Types.FooGroup.DefaultInstance; }
@ -24405,7 +24405,7 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public bool HasFooMessage { public bool HasFooMessage {
get { return result.fooCase_ == FooOneofCase.FooMessage; } get { return result.fooCase_ == FooOneofCase.FooMessage; }
} }
public global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage FooMessage { public global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage FooMessage {
get { return result.fooCase_ == FooOneofCase.FooMessage ? (global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage.DefaultInstance; } get { return result.fooCase_ == FooOneofCase.FooMessage ? (global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage.DefaultInstance; }
@ -24447,7 +24447,7 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public bool HasFooGroup { public bool HasFooGroup {
get { return result.fooCase_ == FooOneofCase.FooGroup; } get { return result.fooCase_ == FooOneofCase.FooGroup; }
} }
public global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.FooGroup FooGroup { public global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.FooGroup FooGroup {
get { return result.fooCase_ == FooOneofCase.FooGroup ? (global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.FooGroup) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.FooGroup.DefaultInstance; } get { return result.fooCase_ == FooOneofCase.FooGroup ? (global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.FooGroup) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.FooGroup.DefaultInstance; }
@ -24489,7 +24489,7 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public bool HasFooLazyMessage { public bool HasFooLazyMessage {
get { return result.fooCase_ == FooOneofCase.FooLazyMessage; } get { return result.fooCase_ == FooOneofCase.FooLazyMessage; }
} }
public global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage FooLazyMessage { public global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage FooLazyMessage {
get { return result.fooCase_ == FooOneofCase.FooLazyMessage ? (global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage.DefaultInstance; } get { return result.fooCase_ == FooOneofCase.FooLazyMessage ? (global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage.DefaultInstance; }
@ -25377,7 +25377,7 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public bool HasFooMessage { public bool HasFooMessage {
get { return result.fooCase_ == FooOneofCase.FooMessage; } get { return result.fooCase_ == FooOneofCase.FooMessage; }
} }
public global::Google.ProtocolBuffers.TestProtos.TestRequiredOneof.Types.NestedMessage FooMessage { public global::Google.ProtocolBuffers.TestProtos.TestRequiredOneof.Types.NestedMessage FooMessage {
get { return result.fooCase_ == FooOneofCase.FooMessage ? (global::Google.ProtocolBuffers.TestProtos.TestRequiredOneof.Types.NestedMessage) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestRequiredOneof.Types.NestedMessage.DefaultInstance; } get { return result.fooCase_ == FooOneofCase.FooMessage ? (global::Google.ProtocolBuffers.TestProtos.TestRequiredOneof.Types.NestedMessage) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestRequiredOneof.Types.NestedMessage.DefaultInstance; }

View File

@ -6432,7 +6432,7 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public bool HasOneofNestedMessage { public bool HasOneofNestedMessage {
get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage; }
} }
public global::Google.ProtocolBuffers.TestProtos.TestAllTypes.Types.NestedMessage OneofNestedMessage { public global::Google.ProtocolBuffers.TestProtos.TestAllTypes.Types.NestedMessage OneofNestedMessage {
get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage ? (global::Google.ProtocolBuffers.TestProtos.TestAllTypes.Types.NestedMessage) result.oneofField_ : global::Google.ProtocolBuffers.TestProtos.TestAllTypes.Types.NestedMessage.DefaultInstance; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage ? (global::Google.ProtocolBuffers.TestProtos.TestAllTypes.Types.NestedMessage) result.oneofField_ : global::Google.ProtocolBuffers.TestProtos.TestAllTypes.Types.NestedMessage.DefaultInstance; }
@ -22008,7 +22008,7 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public bool HasFooMessage { public bool HasFooMessage {
get { return result.fooCase_ == FooOneofCase.FooMessage; } get { return result.fooCase_ == FooOneofCase.FooMessage; }
} }
public global::Google.ProtocolBuffers.TestProtos.TestAllTypes FooMessage { public global::Google.ProtocolBuffers.TestProtos.TestAllTypes FooMessage {
get { return result.fooCase_ == FooOneofCase.FooMessage ? (global::Google.ProtocolBuffers.TestProtos.TestAllTypes) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestAllTypes.DefaultInstance; } get { return result.fooCase_ == FooOneofCase.FooMessage ? (global::Google.ProtocolBuffers.TestProtos.TestAllTypes) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestAllTypes.DefaultInstance; }
@ -22050,7 +22050,7 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public bool HasFooGroup { public bool HasFooGroup {
get { return result.fooCase_ == FooOneofCase.FooGroup; } get { return result.fooCase_ == FooOneofCase.FooGroup; }
} }
public global::Google.ProtocolBuffers.TestProtos.TestOneof.Types.FooGroup FooGroup { public global::Google.ProtocolBuffers.TestProtos.TestOneof.Types.FooGroup FooGroup {
get { return result.fooCase_ == FooOneofCase.FooGroup ? (global::Google.ProtocolBuffers.TestProtos.TestOneof.Types.FooGroup) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestOneof.Types.FooGroup.DefaultInstance; } get { return result.fooCase_ == FooOneofCase.FooGroup ? (global::Google.ProtocolBuffers.TestProtos.TestOneof.Types.FooGroup) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestOneof.Types.FooGroup.DefaultInstance; }
@ -24405,7 +24405,7 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public bool HasFooMessage { public bool HasFooMessage {
get { return result.fooCase_ == FooOneofCase.FooMessage; } get { return result.fooCase_ == FooOneofCase.FooMessage; }
} }
public global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage FooMessage { public global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage FooMessage {
get { return result.fooCase_ == FooOneofCase.FooMessage ? (global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage.DefaultInstance; } get { return result.fooCase_ == FooOneofCase.FooMessage ? (global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage.DefaultInstance; }
@ -24447,7 +24447,7 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public bool HasFooGroup { public bool HasFooGroup {
get { return result.fooCase_ == FooOneofCase.FooGroup; } get { return result.fooCase_ == FooOneofCase.FooGroup; }
} }
public global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.FooGroup FooGroup { public global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.FooGroup FooGroup {
get { return result.fooCase_ == FooOneofCase.FooGroup ? (global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.FooGroup) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.FooGroup.DefaultInstance; } get { return result.fooCase_ == FooOneofCase.FooGroup ? (global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.FooGroup) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.FooGroup.DefaultInstance; }
@ -24489,7 +24489,7 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public bool HasFooLazyMessage { public bool HasFooLazyMessage {
get { return result.fooCase_ == FooOneofCase.FooLazyMessage; } get { return result.fooCase_ == FooOneofCase.FooLazyMessage; }
} }
public global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage FooLazyMessage { public global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage FooLazyMessage {
get { return result.fooCase_ == FooOneofCase.FooLazyMessage ? (global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage.DefaultInstance; } get { return result.fooCase_ == FooOneofCase.FooLazyMessage ? (global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestOneof2.Types.NestedMessage.DefaultInstance; }
@ -25377,7 +25377,7 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public bool HasFooMessage { public bool HasFooMessage {
get { return result.fooCase_ == FooOneofCase.FooMessage; } get { return result.fooCase_ == FooOneofCase.FooMessage; }
} }
public global::Google.ProtocolBuffers.TestProtos.TestRequiredOneof.Types.NestedMessage FooMessage { public global::Google.ProtocolBuffers.TestProtos.TestRequiredOneof.Types.NestedMessage FooMessage {
get { return result.fooCase_ == FooOneofCase.FooMessage ? (global::Google.ProtocolBuffers.TestProtos.TestRequiredOneof.Types.NestedMessage) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestRequiredOneof.Types.NestedMessage.DefaultInstance; } get { return result.fooCase_ == FooOneofCase.FooMessage ? (global::Google.ProtocolBuffers.TestProtos.TestRequiredOneof.Types.NestedMessage) result.foo_ : global::Google.ProtocolBuffers.TestProtos.TestRequiredOneof.Types.NestedMessage.DefaultInstance; }

View File

@ -6440,7 +6440,7 @@ namespace Google.ProtocolBuffers.TestProtos {
} }
public bool HasOneofNestedMessage { public bool HasOneofNestedMessage {
get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage; }
} }
public global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage OneofNestedMessage { public global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage OneofNestedMessage {
get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage ? (global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage) result.oneofField_ : global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage.DefaultInstance; } get { return result.oneofFieldCase_ == OneofFieldOneofCase.OneofNestedMessage ? (global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage) result.oneofField_ : global::Google.ProtocolBuffers.TestProtos.TestAllTypesLite.Types.NestedMessage.DefaultInstance; }

View File

@ -40,7 +40,6 @@
#include <google/protobuf/compiler/csharp/csharp_enum.h> #include <google/protobuf/compiler/csharp/csharp_enum.h>
#include <google/protobuf/compiler/csharp/csharp_helpers.h> #include <google/protobuf/compiler/csharp/csharp_helpers.h>
#include <google/protobuf/compiler/csharp/csharp_writer.h>
using google::protobuf::internal::scoped_ptr; using google::protobuf::internal::scoped_ptr;
@ -57,20 +56,20 @@ EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor) :
EnumGenerator::~EnumGenerator() { EnumGenerator::~EnumGenerator() {
} }
void EnumGenerator::Generate(Writer* writer) { void EnumGenerator::Generate(io::Printer* printer) {
WriteGeneratedCodeAttributes(writer); WriteGeneratedCodeAttributes(printer);
writer->WriteLine("$0$ enum $1$ {", printer->Print("$access_level$ enum $name$ {\n",
class_access_level(), "access_level", class_access_level(),
descriptor_->name()); "name", descriptor_->name());
writer->Indent(); printer->Indent();
for (int i = 0; i < descriptor_->value_count(); i++) { for (int i = 0; i < descriptor_->value_count(); i++) {
writer->WriteLine("$0$ = $1$,", printer->Print("$name$ = $number$,\n",
descriptor_->value(i)->name(), "name", descriptor_->value(i)->name(),
SimpleItoa(descriptor_->value(i)->number())); "number", SimpleItoa(descriptor_->value(i)->number()));
} }
writer->Outdent(); printer->Outdent();
writer->WriteLine("}"); printer->Print("}\n");
writer->WriteLine(); printer->Print("\n");
} }
} // namespace csharp } // namespace csharp

View File

@ -41,14 +41,12 @@ namespace protobuf {
namespace compiler { namespace compiler {
namespace csharp { namespace csharp {
class Writer;
class EnumGenerator : public SourceGeneratorBase { class EnumGenerator : public SourceGeneratorBase {
public: public:
EnumGenerator(const EnumDescriptor* descriptor); EnumGenerator(const EnumDescriptor* descriptor);
~EnumGenerator(); ~EnumGenerator();
void Generate(Writer* writer); void Generate(io::Printer* printer);
private: private:
const EnumDescriptor* descriptor_; const EnumDescriptor* descriptor_;

View File

@ -39,7 +39,6 @@
#include <google/protobuf/compiler/csharp/csharp_helpers.h> #include <google/protobuf/compiler/csharp/csharp_helpers.h>
#include <google/protobuf/compiler/csharp/csharp_enum_field.h> #include <google/protobuf/compiler/csharp/csharp_enum_field.h>
#include <google/protobuf/compiler/csharp/csharp_writer.h>
namespace google { namespace google {
namespace protobuf { namespace protobuf {
@ -54,39 +53,38 @@ EnumFieldGenerator::EnumFieldGenerator(const FieldDescriptor* descriptor,
EnumFieldGenerator::~EnumFieldGenerator() { EnumFieldGenerator::~EnumFieldGenerator() {
} }
void EnumFieldGenerator::GenerateParsingCode(Writer* writer) { void EnumFieldGenerator::GenerateParsingCode(io::Printer* printer) {
writer->WriteLine("object unknown;"); printer->Print(variables_,
writer->WriteLine("if(input.ReadEnum(ref result.$0$_, out unknown)) {", name()); "object unknown;\n"
"if(input.ReadEnum(ref result.$name$_, out unknown)) {\n");
if (SupportFieldPresence(descriptor_->file())) { if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" result.has$0$ = true;", property_name()); printer->Print(variables_,
" result.has$property_name$ = true;\n");
} }
writer->WriteLine("} else if(unknown is int) {"); printer->Print("} else if(unknown is int) {\n");
if (!use_lite_runtime()) { if (!use_lite_runtime()) {
writer->WriteLine(" if (unknownFields == null) {"); // First unknown field - create builder now printer->Print(variables_,
writer->WriteLine( " if (unknownFields == null) {\n" // First unknown field - create builder now
" unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);"); " unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);\n"
writer->WriteLine(" }"); " }\n"
writer->WriteLine( " unknownFields.MergeVarintField($number$, (ulong)(int)unknown);\n");
" unknownFields.MergeVarintField($0$, (ulong)(int)unknown);",
number());
} }
writer->WriteLine("}"); printer->Print("}\n");
} }
void EnumFieldGenerator::GenerateSerializationCode(Writer* writer) { void EnumFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
writer->WriteLine("if ($0$) {", has_property_check); printer->Print(variables_,
writer->WriteLine( "if ($has_property_check$) {\n"
" output.WriteEnum($0$, field_names[$2$], (int) $1$, $1$);", number(), " output.WriteEnum($number$, field_names[$field_ordinal$], (int) $property_name$, $property_name$);\n"
property_name(), field_ordinal()); "}\n");
writer->WriteLine("}");
} }
void EnumFieldGenerator::GenerateSerializedSizeCode(Writer* writer) { void EnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
writer->WriteLine("if ($0$) {", has_property_check); printer->Print(
writer->WriteLine( variables_,
" size += pb::CodedOutputStream.ComputeEnumSize($0$, (int) $1$);", "if ($has_property_check$) {\n"
number(), property_name()); " size += pb::CodedOutputStream.ComputeEnumSize($number$, (int) $property_name$);\n"
writer->WriteLine("}"); "}\n");
} }
EnumOneofFieldGenerator::EnumOneofFieldGenerator(const FieldDescriptor* descriptor, EnumOneofFieldGenerator::EnumOneofFieldGenerator(const FieldDescriptor* descriptor,
@ -97,41 +95,40 @@ EnumOneofFieldGenerator::EnumOneofFieldGenerator(const FieldDescriptor* descript
EnumOneofFieldGenerator::~EnumOneofFieldGenerator() { EnumOneofFieldGenerator::~EnumOneofFieldGenerator() {
} }
void EnumOneofFieldGenerator::GenerateParsingCode(Writer* writer) { void EnumOneofFieldGenerator::GenerateParsingCode(io::Printer* printer) {
writer->WriteLine("object unknown;"); printer->Print(
writer->WriteLine("$0$ enumValue = $1$;", type_name(), default_value()); variables_,
writer->WriteLine("if(input.ReadEnum(ref enumValue, out unknown)) {", "object unknown;\n"
name()); "$type_name$ enumValue = $default_value$;\n"
writer->WriteLine(" result.$0$_ = enumValue;", oneof_name()); "if(input.ReadEnum(ref enumValue, out unknown)) {\n"
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.$2$;", " result.$oneof_name$_ = enumValue;\n"
oneof_name(), oneof_property_name(), property_name()); " result.$oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n"
writer->WriteLine("} else if(unknown is int) {"); "} else if(unknown is int) {\n");
if (!use_lite_runtime()) { if (!use_lite_runtime()) {
writer->WriteLine(" if (unknownFields == null) {"); // First unknown field - create builder now printer->Print(
writer->WriteLine( variables_,
" unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);"); " if (unknownFields == null) {\n" // First unknown field - create builder now
writer->WriteLine(" }"); " unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);\n"
writer->WriteLine( " }\n"
" unknownFields.MergeVarintField($0$, (ulong)(int)unknown);", " unknownFields.MergeVarintField($number$, (ulong)(int)unknown);\n");
number());
} }
writer->WriteLine("}"); printer->Print("}\n");
} }
void EnumOneofFieldGenerator::GenerateSerializationCode(Writer* writer) { void EnumOneofFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
writer->WriteLine("if ($0$) {", has_property_check); printer->Print(
writer->WriteLine( variables_,
" output.WriteEnum($0$, field_names[$2$], (int) $1$, $1$);", number(), "if ($has_property_check$) {\n"
property_name(), field_ordinal()); " output.WriteEnum($number$, field_names[$field_ordinal$], (int) $property_name$, $property_name$);\n"
writer->WriteLine("}"); "}\n");
} }
void EnumOneofFieldGenerator::GenerateSerializedSizeCode(Writer* writer) { void EnumOneofFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
writer->WriteLine("if ($0$) {", has_property_check); printer->Print(
writer->WriteLine( variables_,
" size += pb::CodedOutputStream.ComputeEnumSize($0$, (int) $1$);", "if ($has_property_check$) {\n"
number(), property_name()); " size += pb::CodedOutputStream.ComputeEnumSize($number$, (int) $property_name$);\n"
writer->WriteLine("}"); "}\n");
} }
} // namespace csharp } // namespace csharp

View File

@ -41,16 +41,14 @@ namespace protobuf {
namespace compiler { namespace compiler {
namespace csharp { namespace csharp {
class Writer;
class EnumFieldGenerator : public PrimitiveFieldGenerator { class EnumFieldGenerator : public PrimitiveFieldGenerator {
public: public:
EnumFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); EnumFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
~EnumFieldGenerator(); ~EnumFieldGenerator();
virtual void GenerateParsingCode(Writer* writer); virtual void GenerateParsingCode(io::Printer* printer);
virtual void GenerateSerializationCode(Writer* writer); virtual void GenerateSerializationCode(io::Printer* printer);
virtual void GenerateSerializedSizeCode(Writer* writer); virtual void GenerateSerializedSizeCode(io::Printer* printer);
private: private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumFieldGenerator); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumFieldGenerator);
@ -61,9 +59,9 @@ class EnumOneofFieldGenerator : public PrimitiveOneofFieldGenerator {
EnumOneofFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); EnumOneofFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
~EnumOneofFieldGenerator(); ~EnumOneofFieldGenerator();
virtual void GenerateParsingCode(Writer* writer); virtual void GenerateParsingCode(io::Printer* printer);
virtual void GenerateSerializationCode(Writer* writer); virtual void GenerateSerializationCode(io::Printer* printer);
virtual void GenerateSerializedSizeCode(Writer* writer); virtual void GenerateSerializedSizeCode(io::Printer* printer);
private: private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumOneofFieldGenerator); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumOneofFieldGenerator);

View File

@ -39,7 +39,6 @@
#include <google/protobuf/compiler/csharp/csharp_extension.h> #include <google/protobuf/compiler/csharp/csharp_extension.h>
#include <google/protobuf/compiler/csharp/csharp_helpers.h> #include <google/protobuf/compiler/csharp/csharp_helpers.h>
#include <google/protobuf/compiler/csharp/csharp_writer.h>
#include <google/protobuf/compiler/csharp/csharp_field_base.h> #include <google/protobuf/compiler/csharp/csharp_field_base.h>
using google::protobuf::internal::scoped_ptr; using google::protobuf::internal::scoped_ptr;
@ -52,20 +51,34 @@ namespace csharp {
ExtensionGenerator::ExtensionGenerator(const FieldDescriptor* descriptor) ExtensionGenerator::ExtensionGenerator(const FieldDescriptor* descriptor)
: FieldGeneratorBase(descriptor, 0) { : FieldGeneratorBase(descriptor, 0) {
if (descriptor_->extension_scope()) { if (descriptor_->extension_scope()) {
scope_ = GetClassName(descriptor_->extension_scope()); variables_["scope"] = GetClassName(descriptor_->extension_scope());
} else { } else {
scope_ = GetFullUmbrellaClassName(descriptor_->file()); variables_["scope"] = GetFullUmbrellaClassName(descriptor_->file());
}
variables_["extends"] = GetClassName(descriptor_->containing_type());
variables_["capitalized_type_name"] = capitalized_type_name();
variables_["full_name"] = descriptor_->full_name();
variables_["access_level"] = class_access_level();
variables_["index"] = SimpleItoa(descriptor_->index());
variables_["property_name"] = property_name();
variables_["type_name"] = type_name();
if (use_lite_runtime()) {
variables_["generated_extension"] = descriptor_->is_repeated() ?
"GeneratedRepeatExtensionLite" : "GeneratedExtensionLite";
} else {
variables_["generated_extension"] = descriptor_->is_repeated() ?
"GeneratedRepeatExtension" : "GeneratedExtension";
} }
extends_ = GetClassName(descriptor_->containing_type());
} }
ExtensionGenerator::~ExtensionGenerator() { ExtensionGenerator::~ExtensionGenerator() {
} }
void ExtensionGenerator::Generate(Writer* writer) { void ExtensionGenerator::Generate(io::Printer* printer) {
writer->WriteLine("public const int $0$ = $1$;", printer->Print(
GetFieldConstantName(descriptor_), "public const int $constant_name$ = $number$;\n",
SimpleItoa(descriptor_->number())); "constant_name", GetFieldConstantName(descriptor_),
"number", SimpleItoa(descriptor_->number()));
if (use_lite_runtime()) { if (use_lite_runtime()) {
// TODO(jtattermusch): include the following check // TODO(jtattermusch): include the following check
@ -75,36 +88,34 @@ void ExtensionGenerator::Generate(Writer* writer) {
// "option message_set_wire_format = true; is not supported in Lite runtime extensions."); // "option message_set_wire_format = true; is not supported in Lite runtime extensions.");
//} //}
writer->Write("$0$ ", class_access_level()); printer->Print(
writer->WriteLine( variables_,
"static pb::$3$<$0$, $1$> $2$;", "$access_level$ static pb::$generated_extension$<$extends$, $type_name$> $property_name$;\n");
extends_,
type_name(),
property_name(),
descriptor_->is_repeated() ?
"GeneratedRepeatExtensionLite" : "GeneratedExtensionLite");
} else if (descriptor_->is_repeated()) { } else if (descriptor_->is_repeated()) {
writer->WriteLine( printer->Print(
"$0$ static pb::GeneratedExtensionBase<scg::IList<$1$>> $2$;", variables_,
class_access_level(), type_name(), property_name()); "$access_level$ static pb::GeneratedExtensionBase<scg::IList<$type_name$>> $property_name$;\n");
} else { } else {
writer->WriteLine("$0$ static pb::GeneratedExtensionBase<$1$> $2$;", printer->Print(
class_access_level(), type_name(), property_name()); variables_,
"$access_level$ static pb::GeneratedExtensionBase<$type_name$> $property_name$;\n");
} }
} }
void ExtensionGenerator::GenerateStaticVariableInitializers(Writer* writer) { void ExtensionGenerator::GenerateStaticVariableInitializers(io::Printer* printer) {
if (use_lite_runtime()) { if (use_lite_runtime()) {
writer->WriteLine("$0$.$1$ = ", scope_, property_name()); printer->Print(
writer->Indent(); variables_,
writer->WriteLine( "$scope$.$property_name$ = \n");
"new pb::$0$<$1$, $2$>(", printer->Indent();
descriptor_->is_repeated() ? printer->Print(
"GeneratedRepeatExtensionLite" : "GeneratedExtensionLite", variables_,
extends_, type_name()); "new pb::$generated_extension$<$extends$, $type_name$>(\n");
writer->Indent(); printer->Indent();
writer->WriteLine("\"$0$\",", descriptor_->full_name()); printer->Print(
writer->WriteLine("$0$.DefaultInstance,", extends_); variables_,
"\"$full_name$\",\n"
"$extends$.DefaultInstance,\n");
if (!descriptor_->is_repeated()) { if (!descriptor_->is_repeated()) {
std::string default_val; std::string default_val;
if (descriptor_->has_default_value()) { if (descriptor_->has_default_value()) {
@ -112,52 +123,59 @@ void ExtensionGenerator::GenerateStaticVariableInitializers(Writer* writer) {
} else { } else {
default_val = is_nullable_type() ? "null" : ("default(" + type_name() + ")"); default_val = is_nullable_type() ? "null" : ("default(" + type_name() + ")");
} }
writer->WriteLine("$0$,", default_val); printer->Print("$default_val$,\n", "default_val", default_val);
} }
writer->WriteLine( printer->Print(
"$0$,", "$message_val$,\n",
(GetCSharpType(descriptor_->type()) == CSHARPTYPE_MESSAGE) ? "message_val",
type_name() + ".DefaultInstance" : "null"); (GetCSharpType(descriptor_->type()) == CSHARPTYPE_MESSAGE) ?
writer->WriteLine( type_name() + ".DefaultInstance" : "null");
"$0$,", printer->Print(
(GetCSharpType(descriptor_->type()) == CSHARPTYPE_ENUM) ? "$enum_val$,\n",
"new EnumLiteMap<" + type_name() + ">()" : "null"); "enum_val",
writer->WriteLine("$0$.$1$FieldNumber,", scope_, (GetCSharpType(descriptor_->type()) == CSHARPTYPE_ENUM) ?
GetPropertyName(descriptor_)); "new EnumLiteMap<" + type_name() + ">()" : "null");
writer->Write("pbd::FieldType.$0$", capitalized_type_name()); printer->Print(
variables_,
"$scope$.$property_name$FieldNumber,\n"
"pbd::FieldType.$capitalized_type_name$");
if (descriptor_->is_repeated()) { if (descriptor_->is_repeated()) {
writer->WriteLine(","); printer->Print(
writer->Write(descriptor_->is_packed() ? "true" : "false"); ",\n"
"$is_packed$",
"is_packed", descriptor_->is_packed() ? "true" : "false");
} }
writer->Outdent(); printer->Outdent();
writer->WriteLine(");"); printer->Print(");\n");
writer->Outdent(); printer->Outdent();
} }
else if (descriptor_->is_repeated()) else if (descriptor_->is_repeated())
{ {
writer->WriteLine( printer->Print(
"$0$.$1$ = pb::GeneratedRepeatExtension<$2$>.CreateInstance($0$.Descriptor.Extensions[$3$]);", variables_,
scope_, property_name(), type_name(), SimpleItoa(descriptor_->index())); "$scope$.$property_name$ = pb::GeneratedRepeatExtension<$type_name$>.CreateInstance($scope$.Descriptor.Extensions[$index$]);\n");
} }
else else
{ {
writer->WriteLine( printer->Print(
"$0$.$1$ = pb::GeneratedSingleExtension<$2$>.CreateInstance($0$.Descriptor.Extensions[$3$]);", variables_,
scope_, property_name(), type_name(), SimpleItoa(descriptor_->index())); "$scope$.$property_name$ = pb::GeneratedSingleExtension<$type_name$>.CreateInstance($scope$.Descriptor.Extensions[$index$]);\n");
} }
} }
void ExtensionGenerator::GenerateExtensionRegistrationCode(Writer* writer) { void ExtensionGenerator::GenerateExtensionRegistrationCode(io::Printer* printer) {
writer->WriteLine("registry.Add($0$.$1$);", scope_, property_name()); printer->Print(
variables_,
"registry.Add($scope$.$property_name$);\n");
} }
void ExtensionGenerator::WriteHash(Writer* writer) { void ExtensionGenerator::WriteHash(io::Printer* printer) {
} }
void ExtensionGenerator::WriteEquals(Writer* writer) { void ExtensionGenerator::WriteEquals(io::Printer* printer) {
} }
void ExtensionGenerator::WriteToString(Writer* writer) { void ExtensionGenerator::WriteToString(io::Printer* printer) {
} }
} // namespace csharp } // namespace csharp

View File

@ -41,28 +41,26 @@ namespace protobuf {
namespace compiler { namespace compiler {
namespace csharp { namespace csharp {
class Writer;
class ExtensionGenerator : public FieldGeneratorBase { class ExtensionGenerator : public FieldGeneratorBase {
public: public:
ExtensionGenerator(const FieldDescriptor* descriptor); ExtensionGenerator(const FieldDescriptor* descriptor);
~ExtensionGenerator(); ~ExtensionGenerator();
void GenerateStaticVariableInitializers(Writer* writer); void GenerateStaticVariableInitializers(io::Printer* printer);
void GenerateExtensionRegistrationCode(Writer* writer); void GenerateExtensionRegistrationCode(io::Printer* printer);
void Generate(Writer* writer); void Generate(io::Printer* printer);
virtual void WriteHash(Writer* writer); virtual void WriteHash(io::Printer* printer);
virtual void WriteEquals(Writer* writer); virtual void WriteEquals(io::Printer* printer);
virtual void WriteToString(Writer* writer); virtual void WriteToString(io::Printer* printer);
virtual void GenerateMembers(Writer* writer) {}; virtual void GenerateMembers(io::Printer* printer) {};
virtual void GenerateBuilderMembers(Writer* writer) {}; virtual void GenerateBuilderMembers(io::Printer* printer) {};
virtual void GenerateMergingCode(Writer* writer) {}; virtual void GenerateMergingCode(io::Printer* printer) {};
virtual void GenerateBuildingCode(Writer* writer) {}; virtual void GenerateBuildingCode(io::Printer* printer) {};
virtual void GenerateParsingCode(Writer* writer) {}; virtual void GenerateParsingCode(io::Printer* printer) {};
virtual void GenerateSerializationCode(Writer* writer) {}; virtual void GenerateSerializationCode(io::Printer* printer) {};
virtual void GenerateSerializedSizeCode(Writer* writer) {}; virtual void GenerateSerializedSizeCode(io::Printer* printer) {};
private: private:
std::string scope_; std::string scope_;

View File

@ -41,7 +41,6 @@
#include <google/protobuf/compiler/csharp/csharp_field_base.h> #include <google/protobuf/compiler/csharp/csharp_field_base.h>
#include <google/protobuf/compiler/csharp/csharp_helpers.h> #include <google/protobuf/compiler/csharp/csharp_helpers.h>
#include <google/protobuf/compiler/csharp/csharp_writer.h>
using google::protobuf::internal::scoped_ptr; using google::protobuf::internal::scoped_ptr;
@ -50,35 +49,72 @@ namespace protobuf {
namespace compiler { namespace compiler {
namespace csharp { namespace csharp {
void FieldGeneratorBase::SetCommonFieldVariables(
map<string, string>* variables) {
(*variables)["property_name"] = property_name();
(*variables)["type_name"] = type_name();
(*variables)["name"] = name();
(*variables)["descriptor_name"] = descriptor_->name();
(*variables)["default_value"] = default_value();
if (has_default_value()) {
(*variables)["name_def_message"] =
(*variables)["name"] + "_ = " + (*variables)["default_value"];
} else {
(*variables)["name_def_message"] = (*variables)["name"] + "_";
}
(*variables)["capitalized_type_name"] = capitalized_type_name();
(*variables)["number"] = number();
(*variables)["field_ordinal"] = field_ordinal();
if (SupportFieldPresence(descriptor_->file())) {
(*variables)["has_property_check"] = "has" + (*variables)["property_name"];
(*variables)["other_has_property_check"] = "other.Has" + (*variables)["property_name"];
} else {
(*variables)["has_property_check"] =
(*variables)["property_name"] + " != " + (*variables)["default_value"];
(*variables)["other_has_property_check"] = "other." +
(*variables)["property_name"] + " != " + (*variables)["default_value"];
}
}
void FieldGeneratorBase::SetCommonOneofFieldVariables(
map<string, string>* variables) {
(*variables)["oneof_name"] = oneof_name();
(*variables)["has_property_check"] = oneof_name() + "Case_ == " + oneof_property_name() +
"OneofCase." + property_name();
(*variables)["oneof_property_name"] = oneof_property_name();
}
FieldGeneratorBase::FieldGeneratorBase(const FieldDescriptor* descriptor, FieldGeneratorBase::FieldGeneratorBase(const FieldDescriptor* descriptor,
int fieldOrdinal) int fieldOrdinal)
: SourceGeneratorBase(descriptor->file()), : SourceGeneratorBase(descriptor->file()),
descriptor_(descriptor), descriptor_(descriptor),
fieldOrdinal_(fieldOrdinal) { fieldOrdinal_(fieldOrdinal) {
SetCommonFieldVariables(&variables_);
} }
FieldGeneratorBase::~FieldGeneratorBase() { FieldGeneratorBase::~FieldGeneratorBase() {
} }
void FieldGeneratorBase::AddDeprecatedFlag(Writer* writer) { void FieldGeneratorBase::AddDeprecatedFlag(io::Printer* printer) {
if (descriptor_->options().deprecated()) if (descriptor_->options().deprecated())
{ {
writer->WriteLine("[global::System.ObsoleteAttribute()]"); printer->Print("[global::System.ObsoleteAttribute()]\n");
} }
} }
void FieldGeneratorBase::AddNullCheck(Writer* writer) { void FieldGeneratorBase::AddNullCheck(io::Printer* printer) {
AddNullCheck(writer, "value"); AddNullCheck(printer, "value");
} }
void FieldGeneratorBase::AddNullCheck(Writer* writer, const std::string& name) { void FieldGeneratorBase::AddNullCheck(io::Printer* printer, const std::string& name) {
if (is_nullable_type()) { if (is_nullable_type()) {
writer->WriteLine(" pb::ThrowHelper.ThrowIfNull($0$, \"$0$\");", name); printer->Print(" pb::ThrowHelper.ThrowIfNull($name$, \"$name$\");\n",
"name", name);
} }
} }
void FieldGeneratorBase::AddPublicMemberAttributes(Writer* writer) { void FieldGeneratorBase::AddPublicMemberAttributes(io::Printer* printer) {
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
} }
std::string FieldGeneratorBase::oneof_property_name() { std::string FieldGeneratorBase::oneof_property_name() {

View File

@ -49,27 +49,29 @@ class FieldGeneratorBase : public SourceGeneratorBase {
FieldGeneratorBase(const FieldDescriptor* descriptor, int fieldOrdinal); FieldGeneratorBase(const FieldDescriptor* descriptor, int fieldOrdinal);
~FieldGeneratorBase(); ~FieldGeneratorBase();
virtual void GenerateMembers(Writer* writer) = 0; virtual void GenerateMembers(io::Printer* printer) = 0;
virtual void GenerateBuilderMembers(Writer* writer) = 0; virtual void GenerateBuilderMembers(io::Printer* printer) = 0;
virtual void GenerateMergingCode(Writer* writer) = 0; virtual void GenerateMergingCode(io::Printer* printer) = 0;
virtual void GenerateBuildingCode(Writer* writer) = 0; virtual void GenerateBuildingCode(io::Printer* printer) = 0;
virtual void GenerateParsingCode(Writer* writer) = 0; virtual void GenerateParsingCode(io::Printer* printer) = 0;
virtual void GenerateSerializationCode(Writer* writer) = 0; virtual void GenerateSerializationCode(io::Printer* printer) = 0;
virtual void GenerateSerializedSizeCode(Writer* writer) = 0; virtual void GenerateSerializedSizeCode(io::Printer* printer) = 0;
virtual void WriteHash(Writer* writer) = 0; virtual void WriteHash(io::Printer* printer) = 0;
virtual void WriteEquals(Writer* writer) = 0; virtual void WriteEquals(io::Printer* printer) = 0;
virtual void WriteToString(Writer* writer) = 0; virtual void WriteToString(io::Printer* printer) = 0;
protected: protected:
const FieldDescriptor* descriptor_; const FieldDescriptor* descriptor_;
const int fieldOrdinal_; const int fieldOrdinal_;
map<string, string> variables_;
void AddDeprecatedFlag(Writer* writer); void AddDeprecatedFlag(io::Printer* printer);
void AddNullCheck(Writer* writer); void AddNullCheck(io::Printer* printer);
void AddNullCheck(Writer* writer, const std::string& name); void AddNullCheck(io::Printer* printer, const std::string& name);
void AddPublicMemberAttributes(Writer* writer); void AddPublicMemberAttributes(io::Printer* printer);
void SetCommonOneofFieldVariables(map<string, string>* variables);
std::string oneof_property_name(); std::string oneof_property_name();
std::string oneof_name(); std::string oneof_name();
@ -85,6 +87,7 @@ class FieldGeneratorBase : public SourceGeneratorBase {
std::string field_ordinal(); std::string field_ordinal();
private: private:
void SetCommonFieldVariables(map<string, string>* variables);
std::string GetStringDefaultValueInternal(); std::string GetStringDefaultValueInternal();
std::string GetBytesDefaultValueInternal(); std::string GetBytesDefaultValueInternal();

View File

@ -40,7 +40,6 @@
#include <google/protobuf/compiler/csharp/csharp_generator.h> #include <google/protobuf/compiler/csharp/csharp_generator.h>
#include <google/protobuf/compiler/csharp/csharp_umbrella_class.h> #include <google/protobuf/compiler/csharp/csharp_umbrella_class.h>
#include <google/protobuf/compiler/csharp/csharp_helpers.h> #include <google/protobuf/compiler/csharp/csharp_helpers.h>
#include <google/protobuf/compiler/csharp/csharp_writer.h>
using google::protobuf::internal::scoped_ptr; using google::protobuf::internal::scoped_ptr;
@ -55,9 +54,9 @@ std::string GetOutputFile(const google::protobuf::FileDescriptor* file, const st
} }
void GenerateFile(const google::protobuf::FileDescriptor* file, void GenerateFile(const google::protobuf::FileDescriptor* file,
Writer* writer) { io::Printer* printer) {
UmbrellaClassGenerator umbrellaGenerator(file); UmbrellaClassGenerator umbrellaGenerator(file);
umbrellaGenerator.Generate(writer); umbrellaGenerator.Generate(printer);
} }
bool Generator::Generate( bool Generator::Generate(
@ -83,9 +82,8 @@ bool Generator::Generate(
scoped_ptr<io::ZeroCopyOutputStream> output( scoped_ptr<io::ZeroCopyOutputStream> output(
generator_context->Open(filename)); generator_context->Open(filename));
io::Printer printer(output.get(), '$'); io::Printer printer(output.get(), '$');
Writer writer(&printer);
GenerateFile(file, &writer); GenerateFile(file, &printer);
return true; return true;
} }

File diff suppressed because it is too large Load Diff

View File

@ -51,27 +51,27 @@ class MessageGenerator : public SourceGeneratorBase {
MessageGenerator(const Descriptor* descriptor); MessageGenerator(const Descriptor* descriptor);
~MessageGenerator(); ~MessageGenerator();
void GenerateStaticVariables(Writer* printer); void GenerateStaticVariables(io::Printer* printer);
void GenerateStaticVariableInitializers(Writer* printer); void GenerateStaticVariableInitializers(io::Printer* printer);
void GenerateExtensionRegistrationCode(Writer* printer); void GenerateExtensionRegistrationCode(io::Printer* printer);
void Generate(Writer* printer); void Generate(io::Printer* printer);
private: private:
const Descriptor* descriptor_; const Descriptor* descriptor_;
std::vector<std::string> field_names_; std::vector<std::string> field_names_;
std::vector<const FieldDescriptor*> fields_by_number_; std::vector<const FieldDescriptor*> fields_by_number_;
void GenerateLiteRuntimeMethods(Writer* writer); void GenerateLiteRuntimeMethods(io::Printer* printer);
void GenerateMessageSerializationMethods(Writer* writer); void GenerateMessageSerializationMethods(io::Printer* printer);
void GenerateSerializeOneField(Writer* writer, void GenerateSerializeOneField(io::Printer* printer,
const FieldDescriptor* fieldDescriptor); const FieldDescriptor* fieldDescriptor);
void GenerateSerializeOneExtensionRange( void GenerateSerializeOneExtensionRange(
Writer* writer, const Descriptor::ExtensionRange* extendsionRange); io::Printer* printer, const Descriptor::ExtensionRange* extendsionRange);
void GenerateParseFromMethods(Writer* writer); void GenerateParseFromMethods(io::Printer* printer);
void GenerateBuilder(Writer* writer); void GenerateBuilder(io::Printer* printer);
void GenerateCommonBuilderMethods(Writer* writer); void GenerateCommonBuilderMethods(io::Printer* printer);
void GenerateBuilderParsingMethods(Writer* writer); void GenerateBuilderParsingMethods(io::Printer* printer);
void GenerateIsInitialized(Writer* writer); void GenerateIsInitialized(io::Printer* printer);
int GetFieldOrdinal(const FieldDescriptor* descriptor); int GetFieldOrdinal(const FieldDescriptor* descriptor);
FieldGeneratorBase* CreateFieldGeneratorInternal( FieldGeneratorBase* CreateFieldGeneratorInternal(

View File

@ -40,7 +40,6 @@
#include <google/protobuf/compiler/csharp/csharp_helpers.h> #include <google/protobuf/compiler/csharp/csharp_helpers.h>
#include <google/protobuf/compiler/csharp/csharp_message_field.h> #include <google/protobuf/compiler/csharp/csharp_message_field.h>
#include <google/protobuf/compiler/csharp/csharp_writer.h>
namespace google { namespace google {
namespace protobuf { namespace protobuf {
@ -50,245 +49,291 @@ namespace csharp {
MessageFieldGenerator::MessageFieldGenerator(const FieldDescriptor* descriptor, MessageFieldGenerator::MessageFieldGenerator(const FieldDescriptor* descriptor,
int fieldOrdinal) int fieldOrdinal)
: FieldGeneratorBase(descriptor, fieldOrdinal) { : FieldGeneratorBase(descriptor, fieldOrdinal) {
has_property_check = "has" + property_name(); variables_["has_property_check"] = "has" + property_name();
variables_["message_or_group"] = message_or_group();
} }
MessageFieldGenerator::~MessageFieldGenerator() { MessageFieldGenerator::~MessageFieldGenerator() {
} }
void MessageFieldGenerator::GenerateMembers(Writer* writer) { void MessageFieldGenerator::GenerateMembers(io::Printer* printer) {
writer->WriteLine("private bool has$0$;", property_name()); printer->Print(
writer->WriteLine("private $0$ $1$_;", type_name(), name()); variables_,
AddDeprecatedFlag(writer); "private bool has$property_name$;\n"
writer->WriteLine("public bool Has$0$ {", property_name()); "private $type_name$ $name$_;\n");
writer->WriteLine(" get { return has$0$; }", property_name()); AddDeprecatedFlag(printer);
writer->WriteLine("}"); printer->Print(
AddDeprecatedFlag(writer); variables_,
writer->WriteLine("public $0$ $1$ {", type_name(), property_name()); "public bool Has$property_name$ {\n"
writer->WriteLine(" get { return $0$_ ?? $1$; }", name(), default_value()); " get { return has$property_name$; }\n"
writer->WriteLine("}"); "}\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public $type_name$ $property_name$ {\n"
" get { return $name$_ ?? $default_value$; }\n"
"}\n");
} }
void MessageFieldGenerator::GenerateBuilderMembers(Writer* writer) { void MessageFieldGenerator::GenerateBuilderMembers(io::Printer* printer) {
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
writer->WriteLine("public bool Has$0$ {", property_name()); printer->Print(
writer->WriteLine(" get { return result.has$0$; }", property_name()); variables_,
writer->WriteLine("}"); "public bool Has$property_name$ {\n"
AddDeprecatedFlag(writer); " get { return result.has$property_name$; }\n"
writer->WriteLine("public $0$ $1$ {", type_name(), property_name()); "}\n");
writer->WriteLine(" get { return result.$0$; }", property_name()); AddDeprecatedFlag(printer);
writer->WriteLine(" set { Set$0$(value); }", property_name()); printer->Print(
writer->WriteLine("}"); variables_,
AddDeprecatedFlag(writer); "public $type_name$ $property_name$ {\n"
writer->WriteLine("public Builder Set$0$($1$ value) {", property_name(), " get { return result.$property_name$; }\n"
type_name()); " set { Set$property_name$(value); }\n"
AddNullCheck(writer); "}\n");
writer->WriteLine(" PrepareBuilder();"); AddDeprecatedFlag(printer);
writer->WriteLine(" result.has$0$ = true;", property_name()); printer->Print(
writer->WriteLine(" result.$0$_ = value;", name()); variables_,
writer->WriteLine(" return this;"); "public Builder Set$property_name$($type_name$ value) {\n");
writer->WriteLine("}"); AddNullCheck(printer);
AddDeprecatedFlag(writer); printer->Print(
writer->WriteLine("public Builder Set$0$($1$.Builder builderForValue) {", variables_,
property_name(), type_name()); " PrepareBuilder();\n"
AddNullCheck(writer, "builderForValue"); " result.has$property_name$ = true;\n"
writer->WriteLine(" PrepareBuilder();"); " result.$name$_ = value;\n"
writer->WriteLine(" result.has$0$ = true;", property_name()); " return this;\n"
writer->WriteLine(" result.$0$_ = builderForValue.Build();", name()); "}\n");
writer->WriteLine(" return this;"); AddDeprecatedFlag(printer);
writer->WriteLine("}"); printer->Print(
AddDeprecatedFlag(writer); variables_,
writer->WriteLine("public Builder Merge$0$($1$ value) {", property_name(), "public Builder Set$property_name$($type_name$.Builder builderForValue) {\n");
type_name()); AddNullCheck(printer, "builderForValue");
AddNullCheck(writer); printer->Print(
writer->WriteLine(" PrepareBuilder();"); variables_,
writer->WriteLine(" if (result.has$0$ &&", property_name()); " PrepareBuilder();\n"
writer->WriteLine(" result.$0$_ != $1$) {", name(), default_value()); " result.has$property_name$ = true;\n"
writer->WriteLine( " result.$name$_ = builderForValue.Build();\n"
" result.$0$_ = $1$.CreateBuilder(result.$0$_).MergeFrom(value).BuildPartial();", " return this;\n"
name(), type_name()); "}\n");
writer->WriteLine(" } else {"); AddDeprecatedFlag(printer);
writer->WriteLine(" result.$0$_ = value;", name()); printer->Print(
writer->WriteLine(" }"); variables_,
writer->WriteLine(" result.has$0$ = true;", property_name()); "public Builder Merge$property_name$($type_name$ value) {\n");
writer->WriteLine(" return this;"); AddNullCheck(printer);
writer->WriteLine("}"); printer->Print(
AddDeprecatedFlag(writer); variables_,
writer->WriteLine("public Builder Clear$0$() {", property_name()); " PrepareBuilder();\n"
writer->WriteLine(" PrepareBuilder();"); " if (result.has$property_name$ &&\n"
writer->WriteLine(" result.has$0$ = false;", property_name()); " result.$name$_ != $default_value$) {\n"
writer->WriteLine(" result.$0$_ = null;", name()); " result.$name$_ = $type_name$.CreateBuilder(result.$name$_).MergeFrom(value).BuildPartial();\n"
writer->WriteLine(" return this;"); " } else {\n"
writer->WriteLine("}"); " result.$name$_ = value;\n"
" }\n"
" result.has$property_name$ = true;\n"
" return this;\n"
"}\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public Builder Clear$property_name$() {\n"
" PrepareBuilder();\n"
" result.has$property_name$ = false;\n"
" result.$name$_ = null;\n"
" return this;\n"
"}\n");
} }
void MessageFieldGenerator::GenerateMergingCode(Writer* writer) { void MessageFieldGenerator::GenerateMergingCode(io::Printer* printer) {
writer->WriteLine("if (other.Has$0$) {", property_name()); printer->Print(
writer->WriteLine(" Merge$0$(other.$0$);", property_name()); variables_,
writer->WriteLine("}"); "if (other.Has$property_name$) {\n"
" Merge$property_name$(other.$property_name$);\n"
"}\n");
} }
void MessageFieldGenerator::GenerateBuildingCode(Writer* writer) { void MessageFieldGenerator::GenerateBuildingCode(io::Printer* printer) {
// Nothing to do for singular fields // Nothing to do for singular fields
} }
void MessageFieldGenerator::GenerateParsingCode(Writer* writer) { void MessageFieldGenerator::GenerateParsingCode(io::Printer* printer) {
writer->WriteLine("$0$.Builder subBuilder = $0$.CreateBuilder();", printer->Print(
type_name()); variables_,
writer->WriteLine("if (result.has$0$) {", property_name()); "$type_name$.Builder subBuilder = $type_name$.CreateBuilder();\n"
writer->WriteLine(" subBuilder.MergeFrom($0$);", property_name()); "if (result.has$property_name$) {\n"
writer->WriteLine("}"); " subBuilder.MergeFrom($property_name$);\n"
"}\n");
if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) { if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
writer->WriteLine("input.ReadGroup($0$, subBuilder, extensionRegistry);", printer->Print(
number()); variables_,
"input.ReadGroup($number$, subBuilder, extensionRegistry);\n");
} else { } else {
writer->WriteLine("input.ReadMessage(subBuilder, extensionRegistry);"); printer->Print("input.ReadMessage(subBuilder, extensionRegistry);\n");
} }
writer->WriteLine("$0$ = subBuilder.BuildPartial();", property_name()); printer->Print(
variables_,
"$property_name$ = subBuilder.BuildPartial();\n");
} }
void MessageFieldGenerator::GenerateSerializationCode(Writer* writer) { void MessageFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
writer->WriteLine("if ($0$) {", has_property_check); printer->Print(
writer->WriteLine(" output.Write$0$($1$, field_names[$3$], $2$);", variables_,
message_or_group(), number(), property_name(), "if ($has_property_check$) {\n"
field_ordinal()); " output.Write$message_or_group$($number$, field_names[$field_ordinal$], $property_name$);\n"
writer->WriteLine("}"); "}\n");
} }
void MessageFieldGenerator::GenerateSerializedSizeCode(Writer* writer) { void MessageFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
writer->WriteLine("if ($0$) {", has_property_check); printer->Print(
writer->WriteLine(" size += pb::CodedOutputStream.Compute$0$Size($1$, $2$);", variables_,
message_or_group(), number(), property_name()); "if ($has_property_check$) {\n"
writer->WriteLine("}"); " size += pb::CodedOutputStream.Compute$message_or_group$Size($number$, $property_name$);\n"
"}\n");
} }
void MessageFieldGenerator::WriteHash(Writer* writer) { void MessageFieldGenerator::WriteHash(io::Printer* printer) {
writer->WriteLine("if (has$0$) hash ^= $1$_.GetHashCode();", property_name(), printer->Print(
name()); variables_,
"if (has$property_name$) hash ^= $name$_.GetHashCode();\n");
} }
void MessageFieldGenerator::WriteEquals(Writer* writer) { void MessageFieldGenerator::WriteEquals(io::Printer* printer) {
writer->WriteLine( printer->Print(
"if (has$0$ != other.has$0$ || (has$0$ && !$1$_.Equals(other.$1$_))) return false;", variables_,
property_name(), name()); "if (has$property_name$ != other.has$property_name$ || (has$property_name$ && !$name$_.Equals(other.$name$_))) return false;\n");
} }
void MessageFieldGenerator::WriteToString(Writer* writer) { void MessageFieldGenerator::WriteToString(io::Printer* printer) {
writer->WriteLine("PrintField(\"$2$\", has$0$, $1$_, writer);", variables_["field_name"] = GetFieldName(descriptor_);
property_name(), name(), GetFieldName(descriptor_)); printer->Print(
variables_,
"PrintField(\"$field_name$\", has$property_name$, $name$_, writer);\n");
} }
MessageOneofFieldGenerator::MessageOneofFieldGenerator(const FieldDescriptor* descriptor, MessageOneofFieldGenerator::MessageOneofFieldGenerator(const FieldDescriptor* descriptor,
int fieldOrdinal) int fieldOrdinal)
: MessageFieldGenerator(descriptor, fieldOrdinal) { : MessageFieldGenerator(descriptor, fieldOrdinal) {
has_property_check = oneof_name() + "Case_ == " + oneof_property_name() + SetCommonOneofFieldVariables(&variables_);
"OneofCase." + property_name();
} }
MessageOneofFieldGenerator::~MessageOneofFieldGenerator() { MessageOneofFieldGenerator::~MessageOneofFieldGenerator() {
} }
void MessageOneofFieldGenerator::GenerateMembers(Writer* writer) { void MessageOneofFieldGenerator::GenerateMembers(io::Printer* printer) {
if (SupportFieldPresence(descriptor_->file())) { if (SupportFieldPresence(descriptor_->file())) {
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
writer->WriteLine("public bool Has$0$ {", property_name()); printer->Print(
writer->WriteLine(" get { return $0$; }", has_property_check); variables_,
writer->WriteLine("}"); "public bool Has$property_name$ {\n"
" get { return $has_property_check$; }\n"
"}\n");
} }
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name()); printer->Print(
writer->WriteLine(" get { return $0$ ? ($1$) $2$_ : $3$; }", variables_,
has_property_check, type_name(), oneof_name(), default_value()); "public $type_name$ $property_name$ {\n"
writer->WriteLine("}"); " get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : $default_value$; }\n"
"}\n");
} }
void MessageOneofFieldGenerator::GenerateBuilderMembers(Writer* writer) { void MessageOneofFieldGenerator::GenerateBuilderMembers(io::Printer* printer) {
if (SupportFieldPresence(descriptor_->file())) { if (SupportFieldPresence(descriptor_->file())) {
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
writer->WriteLine("public bool Has$0$ {", property_name()); printer->Print(
writer->WriteLine(" get { return result.$0$; }", has_property_check); variables_,
writer->WriteLine("}"); "public bool Has$property_name$ {\n"
" get { return result.$has_property_check$; }\n"
"}\n");
} }
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name()); printer->Print(
writer->WriteLine(" get { return result.$0$ ? ($1$) result.$2$_ : $3$; }", variables_,
has_property_check, type_name(), oneof_name(), default_value()); "public $type_name$ $property_name$ {\n"
writer->WriteLine(" set { Set$0$(value); }", property_name()); " get { return result.$has_property_check$ ? ($type_name$) result.$oneof_name$_ : $default_value$; }\n"
writer->WriteLine("}"); " set { Set$property_name$(value); }\n"
AddDeprecatedFlag(writer); "}\n");
writer->WriteLine("public Builder Set$0$($1$ value) {", property_name(), AddDeprecatedFlag(printer);
type_name()); printer->Print(
AddNullCheck(writer); variables_,
writer->WriteLine(" PrepareBuilder();"); "public Builder Set$property_name$($type_name$ value) {\n");
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.$2$;", AddNullCheck(printer);
oneof_name(), oneof_property_name(), property_name()); printer->Print(
writer->WriteLine(" result.$0$_ = value;", oneof_name()); variables_,
writer->WriteLine(" return this;"); " PrepareBuilder();\n"
writer->WriteLine("}"); " result.$oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n"
AddDeprecatedFlag(writer); " result.$oneof_name$_ = value;\n"
writer->WriteLine("public Builder Set$0$($1$.Builder builderForValue) {", " return this;\n"
property_name(), type_name()); "}\n");
AddNullCheck(writer, "builderForValue"); AddDeprecatedFlag(printer);
writer->WriteLine(" PrepareBuilder();"); printer->Print(
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.$2$;", variables_,
oneof_name(), oneof_property_name(), property_name()); "public Builder Set$property_name$($type_name$.Builder builderForValue) {\n");
writer->WriteLine(" result.$0$_ = builderForValue.Build();", oneof_name()); AddNullCheck(printer, "builderForValue");
writer->WriteLine(" return this;"); printer->Print(
writer->WriteLine("}"); variables_,
AddDeprecatedFlag(writer); " PrepareBuilder();\n"
writer->WriteLine("public Builder Merge$0$($1$ value) {", property_name(), " result.$oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n"
type_name()); " result.$oneof_name$_ = builderForValue.Build();\n"
AddNullCheck(writer); " return this;\n"
writer->WriteLine(" PrepareBuilder();"); "}\n");
writer->WriteLine(" if (result.$0$ &&", has_property_check); AddDeprecatedFlag(printer);
writer->WriteLine(" result.$0$ != $1$) {", property_name(), default_value()); printer->Print(
writer->WriteLine( variables_,
" result.$0$_ = $1$.CreateBuilder(result.$2$).MergeFrom(value).BuildPartial();", "public Builder Merge$property_name$($type_name$ value) {\n");
oneof_name(), type_name(), property_name()); AddNullCheck(printer);
writer->WriteLine(" } else {"); printer->Print(
writer->WriteLine(" result.$0$_ = value;", oneof_name()); variables_,
writer->WriteLine(" }"); " PrepareBuilder();\n"
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.$2$;", " if (result.$has_property_check$ &&\n"
oneof_name(), oneof_property_name(), property_name()); " result.$property_name$ != $default_value$) {\n"
writer->WriteLine(" return this;"); " result.$oneof_name$_ = $type_name$.CreateBuilder(result.$property_name$).MergeFrom(value).BuildPartial();\n"
writer->WriteLine("}"); " } else {\n"
AddDeprecatedFlag(writer); " result.$oneof_name$_ = value;\n"
writer->WriteLine("public Builder Clear$0$() {", property_name()); " }\n"
writer->WriteLine(" if (result.$0$) {", has_property_check); " result.$oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n"
writer->WriteLine(" PrepareBuilder();"); " return this;\n"
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.None;", "}\n");
oneof_name(), oneof_property_name()); AddDeprecatedFlag(printer);
writer->WriteLine(" result.$0$_ = null;", oneof_name()); printer->Print(
writer->WriteLine(" }"); variables_,
writer->WriteLine(" return this;"); "public Builder Clear$property_name$() {\n"
writer->WriteLine("}"); " if (result.$has_property_check$) {\n"
" PrepareBuilder();\n"
" result.$oneof_name$Case_ = $oneof_property_name$OneofCase.None;\n"
" result.$oneof_name$_ = null;\n"
" }\n"
" return this;\n"
"}\n");
} }
void MessageOneofFieldGenerator::GenerateParsingCode(Writer* writer) { void MessageOneofFieldGenerator::GenerateParsingCode(io::Printer* printer) {
writer->WriteLine("$0$.Builder subBuilder = $0$.CreateBuilder();", printer->Print(
type_name()); variables_,
writer->WriteLine("if (result.$0$) {", has_property_check); "$type_name$.Builder subBuilder = $type_name$.CreateBuilder();\n"
writer->WriteLine(" subBuilder.MergeFrom($0$);", property_name()); "if (result.$has_property_check$) {\n"
writer->WriteLine("}"); " subBuilder.MergeFrom($property_name$);\n"
"}\n");
if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) { if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
writer->WriteLine("input.ReadGroup($0$, subBuilder, extensionRegistry);", printer->Print(
number()); variables_,
"input.ReadGroup($number$, subBuilder, extensionRegistry);\n");
} else { } else {
writer->WriteLine("input.ReadMessage(subBuilder, extensionRegistry);"); printer->Print("input.ReadMessage(subBuilder, extensionRegistry);\n");
} }
writer->WriteLine("result.$0$_ = subBuilder.BuildPartial();", oneof_name()); printer->Print(
writer->WriteLine("result.$0$Case_ = $1$OneofCase.$2$;", variables_,
oneof_name(), oneof_property_name(), property_name()); "result.$oneof_name$_ = subBuilder.BuildPartial();\n"
"result.$oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n");
} }
void MessageOneofFieldGenerator::WriteEquals(Writer* writer) { void MessageOneofFieldGenerator::WriteEquals(io::Printer* printer) {
writer->WriteLine("if (!$0$.Equals(other.$0$)) return false;", property_name()); printer->Print(
variables_,
"if (!$property_name$.Equals(other.$property_name$)) return false;\n");
} }
void MessageOneofFieldGenerator::WriteToString(Writer* writer) { void MessageOneofFieldGenerator::WriteToString(io::Printer* printer) {
writer->WriteLine("PrintField(\"$0$\", $1$, $2$_, writer);", printer->Print(
descriptor_->name(), has_property_check, oneof_name()); variables_,
"PrintField(\"$descriptor_name$\", $has_property_check$, $oneof_name$_, writer);\n");
} }
} // namespace csharp } // namespace csharp

View File

@ -48,20 +48,17 @@ class MessageFieldGenerator : public FieldGeneratorBase {
MessageFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); MessageFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
~MessageFieldGenerator(); ~MessageFieldGenerator();
virtual void GenerateMembers(Writer* writer); virtual void GenerateMembers(io::Printer* printer);
virtual void GenerateBuilderMembers(Writer* writer); virtual void GenerateBuilderMembers(io::Printer* printer);
virtual void GenerateMergingCode(Writer* writer); virtual void GenerateMergingCode(io::Printer* printer);
virtual void GenerateBuildingCode(Writer* writer); virtual void GenerateBuildingCode(io::Printer* printer);
virtual void GenerateParsingCode(Writer* writer); virtual void GenerateParsingCode(io::Printer* printer);
virtual void GenerateSerializationCode(Writer* writer); virtual void GenerateSerializationCode(io::Printer* printer);
virtual void GenerateSerializedSizeCode(Writer* writer); virtual void GenerateSerializedSizeCode(io::Printer* printer);
virtual void WriteHash(Writer* writer); virtual void WriteHash(io::Printer* printer);
virtual void WriteEquals(Writer* writer); virtual void WriteEquals(io::Printer* printer);
virtual void WriteToString(Writer* writer); virtual void WriteToString(io::Printer* printer);
protected:
string has_property_check;
private: private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageFieldGenerator); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageFieldGenerator);
@ -72,11 +69,11 @@ class MessageOneofFieldGenerator : public MessageFieldGenerator {
MessageOneofFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); MessageOneofFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
~MessageOneofFieldGenerator(); ~MessageOneofFieldGenerator();
virtual void GenerateMembers(Writer* writer); virtual void GenerateMembers(io::Printer* printer);
virtual void GenerateBuilderMembers(Writer* writer); virtual void GenerateBuilderMembers(io::Printer* printer);
virtual void WriteEquals(Writer* writer); virtual void WriteEquals(io::Printer* printer);
virtual void WriteToString(Writer* writer); virtual void WriteToString(io::Printer* printer);
virtual void GenerateParsingCode(Writer* writer); virtual void GenerateParsingCode(io::Printer* printer);
private: private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageOneofFieldGenerator); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageOneofFieldGenerator);

View File

@ -40,7 +40,6 @@
#include <google/protobuf/compiler/csharp/csharp_helpers.h> #include <google/protobuf/compiler/csharp/csharp_helpers.h>
#include <google/protobuf/compiler/csharp/csharp_primitive_field.h> #include <google/protobuf/compiler/csharp/csharp_primitive_field.h>
#include <google/protobuf/compiler/csharp/csharp_writer.h>
namespace google { namespace google {
namespace protobuf { namespace protobuf {
@ -50,202 +49,232 @@ namespace csharp {
PrimitiveFieldGenerator::PrimitiveFieldGenerator( PrimitiveFieldGenerator::PrimitiveFieldGenerator(
const FieldDescriptor* descriptor, int fieldOrdinal) const FieldDescriptor* descriptor, int fieldOrdinal)
: FieldGeneratorBase(descriptor, fieldOrdinal) { : FieldGeneratorBase(descriptor, fieldOrdinal) {
if (SupportFieldPresence(descriptor_->file())) {
has_property_check = "has" + property_name();
} else {
has_property_check = property_name() + " != " + default_value();
}
} }
PrimitiveFieldGenerator::~PrimitiveFieldGenerator() { PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {
} }
void PrimitiveFieldGenerator::GenerateMembers(Writer* writer) { void PrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) {
if (SupportFieldPresence(descriptor_->file())) { if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("private bool has$0$;", property_name()); printer->Print(variables_, "private bool has$property_name$;\n");
} }
writer->WriteLine("private $0$ $1$_$2$;", type_name(), name(), printer->Print(
has_default_value() ? " = " + default_value() : ""); variables_,
AddDeprecatedFlag(writer); "private $type_name$ $name_def_message$;\n");
AddDeprecatedFlag(printer);
if (SupportFieldPresence(descriptor_->file())) { if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("public bool Has$0$ {", property_name()); printer->Print(
writer->WriteLine(" get { return has$0$; }", property_name()); variables_,
writer->WriteLine("}"); "public bool Has$property_name$ {\n"
" get { return has$property_name$; }\n"
"}\n");
} }
AddPublicMemberAttributes(writer); AddPublicMemberAttributes(printer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name()); printer->Print(
writer->WriteLine(" get { return $0$_; }", name()); variables_,
writer->WriteLine("}"); "public $type_name$ $property_name$ {\n"
" get { return $name$_; }\n"
"}\n");
} }
void PrimitiveFieldGenerator::GenerateBuilderMembers(Writer* writer) { void PrimitiveFieldGenerator::GenerateBuilderMembers(io::Printer* printer) {
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
if (SupportFieldPresence(descriptor_->file())) { if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("public bool Has$0$ {", property_name()); printer->Print(
writer->WriteLine(" get { return result.has$0$; }", property_name()); variables_,
writer->WriteLine("}"); "public bool Has$property_name$ {\n"
" get { return result.has$property_name$; }\n"
"}\n");
} }
AddPublicMemberAttributes(writer); AddPublicMemberAttributes(printer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name()); printer->Print(
writer->WriteLine(" get { return result.$0$; }", property_name()); variables_,
writer->WriteLine(" set { Set$0$(value); }", property_name()); "public $type_name$ $property_name$ {\n"
writer->WriteLine("}"); " get { return result.$property_name$; }\n"
AddPublicMemberAttributes(writer); " set { Set$property_name$(value); }\n"
writer->WriteLine("public Builder Set$0$($1$ value) {", property_name(), "}\n");
type_name()); AddPublicMemberAttributes(printer);
AddNullCheck(writer); printer->Print(
writer->WriteLine(" PrepareBuilder();"); variables_,
"public Builder Set$property_name$($type_name$ value) {\n");
AddNullCheck(printer);
printer->Print(" PrepareBuilder();\n");
if (SupportFieldPresence(descriptor_->file())) { if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" result.has$0$ = true;", property_name()); printer->Print(
variables_,
" result.has$property_name$ = true;\n");
} }
writer->WriteLine(" result.$0$_ = value;", name()); printer->Print(
writer->WriteLine(" return this;"); variables_,
writer->WriteLine("}"); " result.$name$_ = value;\n"
AddDeprecatedFlag(writer); " return this;\n"
writer->WriteLine("public Builder Clear$0$() {", property_name()); "}\n");
writer->WriteLine(" PrepareBuilder();"); AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public Builder Clear$property_name$() {\n"
" PrepareBuilder();\n");
if (SupportFieldPresence(descriptor_->file())) { if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine(" result.has$0$ = false;", property_name()); printer->Print(
variables_,
" result.has$property_name$ = false;\n");
} }
writer->WriteLine(" result.$0$_ = $1$;", name(), default_value()); printer->Print(
writer->WriteLine(" return this;"); variables_,
writer->WriteLine("}"); " result.$name$_ = $default_value$;\n"
" return this;\n"
"}\n");
} }
void PrimitiveFieldGenerator::GenerateMergingCode(Writer* writer) { void PrimitiveFieldGenerator::GenerateMergingCode(io::Printer* printer) {
if (SupportFieldPresence(descriptor_->file())) { printer->Print(
writer->WriteLine("if (other.Has$0$) {", property_name()); variables_,
} else { "if ($other_has_property_check$) {\n"
writer->WriteLine("if (other.$0$ != $1$) {", property_name(), default_value()); " $property_name$ = other.$property_name$;\n"
} "}\n");
writer->WriteLine(" $0$ = other.$0$;", property_name());
writer->WriteLine("}");
} }
void PrimitiveFieldGenerator::GenerateBuildingCode(Writer* writer) { void PrimitiveFieldGenerator::GenerateBuildingCode(io::Printer* printer) {
// Nothing to do here for primitive types // Nothing to do here for primitive types
} }
void PrimitiveFieldGenerator::GenerateParsingCode(Writer* writer) { void PrimitiveFieldGenerator::GenerateParsingCode(io::Printer* printer) {
if (SupportFieldPresence(descriptor_->file())) { if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("result.has$0$ = input.Read$1$(ref result.$2$_);", printer->Print(
property_name(), capitalized_type_name(), name()); variables_,
"result.has$property_name$ = input.Read$capitalized_type_name$(ref result.$name$_);\n");
} else { } else {
writer->WriteLine("input.Read$0$(ref result.$1$_);", printer->Print(
capitalized_type_name(), name()); variables_,
"input.Read$capitalized_type_name$(ref result.$name$_);\n");
} }
} }
void PrimitiveFieldGenerator::GenerateSerializationCode(Writer* writer) { void PrimitiveFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
writer->WriteLine("if ($0$) {", has_property_check); printer->Print(
writer->WriteLine(" output.Write$0$($1$, field_names[$3$], $2$);", variables_,
capitalized_type_name(), number(), property_name(), "if ($has_property_check$) {\n"
field_ordinal()); " output.Write$capitalized_type_name$($number$, field_names[$field_ordinal$], $property_name$);\n"
writer->WriteLine("}"); "}\n");
} }
void PrimitiveFieldGenerator::GenerateSerializedSizeCode(Writer* writer) { void PrimitiveFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
writer->WriteLine("if ($0$) {", has_property_check); printer->Print(
writer->WriteLine(" size += pb::CodedOutputStream.Compute$0$Size($1$, $2$);", variables_,
capitalized_type_name(), number(), property_name()); "if ($has_property_check$) {\n"
writer->WriteLine("}"); " size += pb::CodedOutputStream.Compute$capitalized_type_name$Size($number$, $property_name$);\n"
"}\n");
} }
void PrimitiveFieldGenerator::WriteHash(Writer* writer) { void PrimitiveFieldGenerator::WriteHash(io::Printer* printer) {
writer->WriteLine("if ($0$) {", has_property_check); printer->Print(
writer->WriteLine(" hash ^= $0$_.GetHashCode();", name()); variables_,
writer->WriteLine("}"); "if ($has_property_check$) {\n"
" hash ^= $name$_.GetHashCode();\n"
"}\n");
} }
void PrimitiveFieldGenerator::WriteEquals(Writer* writer) { void PrimitiveFieldGenerator::WriteEquals(io::Printer* printer) {
if (SupportFieldPresence(descriptor_->file())) { if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine( printer->Print(
"if (has$0$ != other.has$0$ || (has$0$ && !$1$_.Equals(other.$1$_))) return false;", variables_,
property_name(), name()); "if (has$property_name$ != other.has$property_name$ || (has$property_name$ && !$name$_.Equals(other.$name$_))) return false;\n");
} else { } else {
writer->WriteLine( printer->Print(
"if (!$0$_.Equals(other.$0$_)) return false;", name()); variables_,
"if (!$name$_.Equals(other.$name$_)) return false;\n");
} }
} }
void PrimitiveFieldGenerator::WriteToString(Writer* writer) { void PrimitiveFieldGenerator::WriteToString(io::Printer* printer) {
writer->WriteLine("PrintField(\"$0$\", $1$, $2$_, writer);", printer->Print(
descriptor_->name(), has_property_check, name()); variables_,
"PrintField(\"$descriptor_name$\", $has_property_check$, $name$_, writer);\n");
} }
PrimitiveOneofFieldGenerator::PrimitiveOneofFieldGenerator( PrimitiveOneofFieldGenerator::PrimitiveOneofFieldGenerator(
const FieldDescriptor* descriptor, int fieldOrdinal) const FieldDescriptor* descriptor, int fieldOrdinal)
: PrimitiveFieldGenerator(descriptor, fieldOrdinal) { : PrimitiveFieldGenerator(descriptor, fieldOrdinal) {
has_property_check = oneof_name() + "Case_ == " + oneof_property_name() + SetCommonOneofFieldVariables(&variables_);
"OneofCase." + property_name();
} }
PrimitiveOneofFieldGenerator::~PrimitiveOneofFieldGenerator() { PrimitiveOneofFieldGenerator::~PrimitiveOneofFieldGenerator() {
} }
void PrimitiveOneofFieldGenerator::GenerateMembers(Writer* writer) { void PrimitiveOneofFieldGenerator::GenerateMembers(io::Printer* printer) {
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
if (SupportFieldPresence(descriptor_->file())) { if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("public bool Has$0$ {", property_name()); printer->Print(
writer->WriteLine(" get { return $0$; }", has_property_check); variables_,
writer->WriteLine("}"); "public bool Has$property_name$ {\n"
" get { return $has_property_check$; }\n"
"}\n");
} }
AddPublicMemberAttributes(writer); AddPublicMemberAttributes(printer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name()); printer->Print(
writer->WriteLine(" get { return $0$ ? ($1$) $2$_ : $3$; }", variables_,
has_property_check, type_name(), oneof_name(), default_value()); "public $type_name$ $property_name$ {\n"
writer->WriteLine("}"); " get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : $default_value$; }\n"
"}\n");
} }
void PrimitiveOneofFieldGenerator::GenerateBuilderMembers(Writer* writer) { void PrimitiveOneofFieldGenerator::GenerateBuilderMembers(io::Printer* printer) {
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
if (SupportFieldPresence(descriptor_->file())) { if (SupportFieldPresence(descriptor_->file())) {
writer->WriteLine("public bool Has$0$ {", property_name()); printer->Print(
writer->WriteLine(" get { return result.$0$; }", has_property_check); variables_,
writer->WriteLine("}"); "public bool Has$property_name$ {\n"
" get { return result.$has_property_check$; }\n"
"}\n");
} }
AddPublicMemberAttributes(writer); AddPublicMemberAttributes(printer);
writer->WriteLine("public $0$ $1$ {", type_name(), property_name()); printer->Print(
writer->WriteLine(" get { return result.$0$ ? ($1$) result.$2$_ : $3$; }", variables_,
has_property_check, type_name(), oneof_name(), default_value()); "public $type_name$ $property_name$ {\n"
writer->WriteLine(" set { Set$0$(value); }", property_name()); " get { return result.$has_property_check$ ? ($type_name$) result.$oneof_name$_ : $default_value$; }\n"
writer->WriteLine("}"); " set { Set$property_name$(value); }\n"
AddPublicMemberAttributes(writer); "}\n");
writer->WriteLine("public Builder Set$0$($1$ value) {", property_name(), AddPublicMemberAttributes(printer);
type_name()); printer->Print(
AddNullCheck(writer); variables_,
writer->WriteLine(" PrepareBuilder();"); "public Builder Set$property_name$($type_name$ value) {\n");
writer->WriteLine(" result.$0$_ = value;", oneof_name()); AddNullCheck(printer);
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.$2$;", printer->Print(
oneof_name(), oneof_property_name(), property_name()); variables_,
writer->WriteLine(" return this;"); " PrepareBuilder();\n"
writer->WriteLine("}"); " result.$oneof_name$_ = value;\n"
AddDeprecatedFlag(writer); " result.$oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n"
writer->WriteLine("public Builder Clear$0$() {", property_name()); " return this;\n"
writer->WriteLine(" PrepareBuilder();"); "}\n");
writer->WriteLine(" if (result.$0$) {", has_property_check); AddDeprecatedFlag(printer);
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.None;", printer->Print(
oneof_name(), oneof_property_name()); variables_,
writer->WriteLine(" }"); "public Builder Clear$property_name$() {\n"
writer->WriteLine(" return this;"); " PrepareBuilder();\n"
writer->WriteLine("}"); " if (result.$has_property_check$) {\n"
" result.$oneof_name$Case_ = $oneof_property_name$OneofCase.None;\n"
" }\n"
" return this;\n"
"}\n");
} }
void PrimitiveOneofFieldGenerator::WriteEquals(Writer* writer) { void PrimitiveOneofFieldGenerator::WriteEquals(io::Printer* printer) {
writer->WriteLine("if (!$0$.Equals(other.$0$)) return false;", property_name()); printer->Print(
variables_,
"if (!$property_name$.Equals(other.$property_name$)) return false;\n");
} }
void PrimitiveOneofFieldGenerator::WriteToString(Writer* writer) { void PrimitiveOneofFieldGenerator::WriteToString(io::Printer* printer) {
writer->WriteLine("PrintField(\"$0$\", $1$, $2$_, writer);", printer->Print(variables_,
descriptor_->name(), has_property_check, oneof_name()); "PrintField(\"$descriptor_name$\", $has_property_check$, $oneof_name$_, writer);\n");
} }
void PrimitiveOneofFieldGenerator::GenerateParsingCode(Writer* writer) { void PrimitiveOneofFieldGenerator::GenerateParsingCode(io::Printer* printer) {
writer->WriteLine("$0$ value = $1$;", type_name(), default_value()); printer->Print(
writer->WriteLine("if (input.Read$0$(ref value)) {", variables_,
capitalized_type_name()); "$type_name$ value = $default_value$;\n"
writer->WriteLine(" result.$0$_ = value;", oneof_name()); "if (input.Read$capitalized_type_name$(ref value)) {\n"
writer->WriteLine(" result.$0$Case_ = $1$OneofCase.$2$;", " result.$oneof_name$_ = value;\n"
oneof_name(), oneof_property_name(), property_name()); " result.$oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n"
writer->WriteLine("}"); "}\n");
} }
} // namespace csharp } // namespace csharp

View File

@ -48,20 +48,17 @@ class PrimitiveFieldGenerator : public FieldGeneratorBase {
PrimitiveFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); PrimitiveFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
~PrimitiveFieldGenerator(); ~PrimitiveFieldGenerator();
virtual void GenerateMembers(Writer* writer); virtual void GenerateMembers(io::Printer* printer);
virtual void GenerateBuilderMembers(Writer* writer); virtual void GenerateBuilderMembers(io::Printer* printer);
virtual void GenerateMergingCode(Writer* writer); virtual void GenerateMergingCode(io::Printer* printer);
virtual void GenerateBuildingCode(Writer* writer); virtual void GenerateBuildingCode(io::Printer* printer);
virtual void GenerateParsingCode(Writer* writer); virtual void GenerateParsingCode(io::Printer* printer);
virtual void GenerateSerializationCode(Writer* writer); virtual void GenerateSerializationCode(io::Printer* printer);
virtual void GenerateSerializedSizeCode(Writer* writer); virtual void GenerateSerializedSizeCode(io::Printer* printer);
virtual void WriteHash(Writer* writer); virtual void WriteHash(io::Printer* printer);
virtual void WriteEquals(Writer* writer); virtual void WriteEquals(io::Printer* printer);
virtual void WriteToString(Writer* writer); virtual void WriteToString(io::Printer* printer);
protected:
string has_property_check;
private: private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PrimitiveFieldGenerator); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PrimitiveFieldGenerator);
@ -72,11 +69,11 @@ class PrimitiveOneofFieldGenerator : public PrimitiveFieldGenerator {
PrimitiveOneofFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); PrimitiveOneofFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
~PrimitiveOneofFieldGenerator(); ~PrimitiveOneofFieldGenerator();
virtual void GenerateMembers(Writer* writer); virtual void GenerateMembers(io::Printer* printer);
virtual void GenerateBuilderMembers(Writer* writer); virtual void GenerateBuilderMembers(io::Printer* printer);
virtual void WriteEquals(Writer* writer); virtual void WriteEquals(io::Printer* printer);
virtual void WriteToString(Writer* writer); virtual void WriteToString(io::Printer* printer);
virtual void GenerateParsingCode(Writer* writer); virtual void GenerateParsingCode(io::Printer* printer);
private: private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PrimitiveOneofFieldGenerator); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PrimitiveOneofFieldGenerator);

View File

@ -40,7 +40,6 @@
#include <google/protobuf/compiler/csharp/csharp_helpers.h> #include <google/protobuf/compiler/csharp/csharp_helpers.h>
#include <google/protobuf/compiler/csharp/csharp_repeated_enum_field.h> #include <google/protobuf/compiler/csharp/csharp_repeated_enum_field.h>
#include <google/protobuf/compiler/csharp/csharp_writer.h>
namespace google { namespace google {
namespace protobuf { namespace protobuf {
@ -56,168 +55,193 @@ RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {
} }
void RepeatedEnumFieldGenerator::GenerateMembers(Writer* writer) { void RepeatedEnumFieldGenerator::GenerateMembers(io::Printer* printer) {
if (descriptor_->is_packed() && optimize_speed()) { if (descriptor_->is_packed() && optimize_speed()) {
writer->WriteLine("private int $0$MemoizedSerializedSize;", name()); printer->Print(variables_, "private int $name$MemoizedSerializedSize;\n");
} }
writer->WriteLine( printer->Print(variables_,
"private pbc::PopsicleList<$0$> $1$_ = new pbc::PopsicleList<$0$>();", "private pbc::PopsicleList<$type_name$> $name$_ = new pbc::PopsicleList<$type_name$>();\n");
type_name(), name()); AddDeprecatedFlag(printer);
AddDeprecatedFlag(writer); printer->Print(
writer->WriteLine("public scg::IList<$0$> $1$List {", type_name(), variables_,
property_name()); "public scg::IList<$type_name$> $property_name$List {\n"
writer->WriteLine(" get { return pbc::Lists.AsReadOnly($0$_); }", name()); " get { return pbc::Lists.AsReadOnly($name$_); }\n"
writer->WriteLine("}"); "}\n");
// TODO(jonskeet): Redundant API calls? Possibly - include for portability though. Maybe create an option. // TODO(jonskeet): Redundant API calls? Possibly - include for portability though. Maybe create an option.
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
writer->WriteLine("public int $0$Count {", property_name()); printer->Print(
writer->WriteLine(" get { return $0$_.Count; }", name()); variables_,
writer->WriteLine("}"); "public int $property_name$Count {\n"
" get { return $name$_.Count; }\n"
"}\n");
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
writer->WriteLine("public $0$ Get$1$(int index) {", type_name(), printer->Print(
property_name()); variables_,
writer->WriteLine(" return $0$_[index];", name()); "public $type_name$ Get$property_name$(int index) {\n"
writer->WriteLine("}"); " return $name$_[index];\n"
"}\n");
} }
void RepeatedEnumFieldGenerator::GenerateBuilderMembers(Writer* writer) { void RepeatedEnumFieldGenerator::GenerateBuilderMembers(io::Printer* printer) {
// Note: We can return the original list here, because we make it unmodifiable when we build // Note: We can return the original list here, because we make it unmodifiable when we build
// We return it via IPopsicleList so that collection initializers work more pleasantly. // We return it via IPopsicleList so that collection initializers work more pleasantly.
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
writer->WriteLine("public pbc::IPopsicleList<$0$> $1$List {", type_name(), printer->Print(
property_name()); variables_,
writer->WriteLine(" get { return PrepareBuilder().$0$_; }", name()); "public pbc::IPopsicleList<$type_name$> $property_name$List {\n"
writer->WriteLine("}"); " get { return PrepareBuilder().$name$_; }\n"
AddDeprecatedFlag(writer); "}\n");
writer->WriteLine("public int $0$Count {", property_name()); AddDeprecatedFlag(printer);
writer->WriteLine(" get { return result.$0$Count; }", property_name()); printer->Print(
writer->WriteLine("}"); variables_,
AddDeprecatedFlag(writer); "public int $property_name$Count {\n"
writer->WriteLine("public $0$ Get$1$(int index) {", type_name(), " get { return result.$property_name$Count; }\n"
property_name()); "}\n");
writer->WriteLine(" return result.Get$0$(index);", property_name()); AddDeprecatedFlag(printer);
writer->WriteLine("}"); printer->Print(
AddDeprecatedFlag(writer); variables_,
writer->WriteLine("public Builder Set$0$(int index, $1$ value) {", "public $type_name$ Get$property_name$(int index) {\n"
property_name(), type_name()); " return result.Get$property_name$(index);\n"
writer->WriteLine(" PrepareBuilder();"); "}\n");
writer->WriteLine(" result.$0$_[index] = value;", name()); AddDeprecatedFlag(printer);
writer->WriteLine(" return this;"); printer->Print(
writer->WriteLine("}"); variables_,
AddDeprecatedFlag(writer); "public Builder Set$property_name$(int index, $type_name$ value) {\n");
writer->WriteLine("public Builder Add$0$($1$ value) {", property_name(), printer->Print(
type_name()); variables_,
writer->WriteLine(" PrepareBuilder();"); " PrepareBuilder();\n"
writer->WriteLine(" result.$0$_.Add(value);", name(), type_name()); " result.$name$_[index] = value;\n"
writer->WriteLine(" return this;"); " return this;\n"
writer->WriteLine("}"); "}\n");
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
writer->WriteLine( printer->Print(variables_,
"public Builder AddRange$0$(scg::IEnumerable<$1$> values) {", "public Builder Add$property_name$($type_name$ value) {\n");
property_name(), type_name()); printer->Print(
writer->WriteLine(" PrepareBuilder();"); variables_,
writer->WriteLine(" result.$0$_.Add(values);", name()); " PrepareBuilder();\n"
writer->WriteLine(" return this;"); " result.$name$_.Add(value);\n"
writer->WriteLine("}"); " return this;\n"
AddDeprecatedFlag(writer); "}\n");
writer->WriteLine("public Builder Clear$0$() {", property_name()); AddDeprecatedFlag(printer);
writer->WriteLine(" PrepareBuilder();"); printer->Print(
writer->WriteLine(" result.$0$_.Clear();", name()); variables_,
writer->WriteLine(" return this;"); "public Builder AddRange$property_name$(scg::IEnumerable<$type_name$> values) {\n"
writer->WriteLine("}"); " PrepareBuilder();\n"
" result.$name$_.Add(values);\n"
" return this;\n"
"}\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public Builder Clear$property_name$() {\n"
" PrepareBuilder();\n"
" result.$name$_.Clear();\n"
" return this;\n"
"}\n");
} }
void RepeatedEnumFieldGenerator::GenerateMergingCode(Writer* writer) { void RepeatedEnumFieldGenerator::GenerateMergingCode(io::Printer* printer) {
writer->WriteLine("if (other.$0$_.Count != 0) {", name()); printer->Print(
writer->WriteLine(" result.$0$_.Add(other.$0$_);", name()); variables_,
writer->WriteLine("}"); "if (other.$name$_.Count != 0) {\n"
" result.$name$_.Add(other.$name$_);\n"
"}\n");
} }
void RepeatedEnumFieldGenerator::GenerateBuildingCode(Writer* writer) { void RepeatedEnumFieldGenerator::GenerateBuildingCode(io::Printer* printer) {
writer->WriteLine("$0$_.MakeReadOnly();", name()); printer->Print(variables_, "$name$_.MakeReadOnly();\n");
} }
void RepeatedEnumFieldGenerator::GenerateParsingCode(Writer* writer) { void RepeatedEnumFieldGenerator::GenerateParsingCode(io::Printer* printer) {
writer->WriteLine("scg::ICollection<object> unknownItems;"); printer->Print(
writer->WriteLine( variables_,
"input.ReadEnumArray<$0$>(tag, field_name, result.$1$_, out unknownItems);", "scg::ICollection<object> unknownItems;\n"
type_name(), name()); "input.ReadEnumArray<$type_name$>(tag, field_name, result.$name$_, out unknownItems);\n");
if (!use_lite_runtime()) { if (!use_lite_runtime()) {
writer->WriteLine("if (unknownItems != null) {"); printer->Print(
writer->WriteLine(" if (unknownFields == null) {"); variables_,
writer->WriteLine( "if (unknownItems != null) {\n"
" unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);"); " if (unknownFields == null) {\n"
writer->WriteLine(" }"); " unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);\n"
writer->WriteLine(" foreach (object rawValue in unknownItems)"); " }\n"
writer->WriteLine(" if (rawValue is int)"); " foreach (object rawValue in unknownItems)\n"
writer->WriteLine( " if (rawValue is int)\n"
" unknownFields.MergeVarintField($0$, (ulong)(int)rawValue);", " unknownFields.MergeVarintField($number$, (ulong)(int)rawValue);\n"
number()); "}\n");
writer->WriteLine("}");
} }
} }
void RepeatedEnumFieldGenerator::GenerateSerializationCode(Writer* writer) { void RepeatedEnumFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
writer->WriteLine("if ($0$_.Count > 0) {", name()); printer->Print(variables_, "if ($name$_.Count > 0) {\n");
writer->Indent(); printer->Indent();
if (descriptor_->is_packed()) { if (descriptor_->is_packed()) {
writer->WriteLine( printer->Print(
"output.WritePackedEnumArray($0$, field_names[$2$], $1$MemoizedSerializedSize, $1$_);", variables_,
number(), name(), field_ordinal()); "output.WritePackedEnumArray($number$, field_names[$field_ordinal$], $name$MemoizedSerializedSize, $name$_);\n");
} else { } else {
writer->WriteLine("output.WriteEnumArray($0$, field_names[$2$], $1$_);", printer->Print(variables_,
number(), name(), field_ordinal()); "output.WriteEnumArray($number$, field_names[$field_ordinal$], $name$_);\n");
} }
writer->Outdent(); printer->Outdent();
writer->WriteLine("}"); printer->Print("}\n");
} }
void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(Writer* writer) { void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
writer->WriteLine("{"); printer->Print("{\n");
writer->Indent(); printer->Indent();
writer->WriteLine("int dataSize = 0;"); printer->Print(
writer->WriteLine("if ($0$_.Count > 0) {", name()); variables_,
writer->Indent(); "int dataSize = 0;\n"
writer->WriteLine("foreach ($0$ element in $1$_) {", type_name(), name()); "if ($name$_.Count > 0) {\n");
writer->WriteLine( printer->Indent();
" dataSize += pb::CodedOutputStream.ComputeEnumSizeNoTag((int) element);"); printer->Print(
writer->WriteLine("}"); variables_,
writer->WriteLine("size += dataSize;"); "foreach ($type_name$ element in $name$_) {\n"
" dataSize += pb::CodedOutputStream.ComputeEnumSizeNoTag((int) element);\n"
"}\n"
"size += dataSize;\n");
int tagSize = internal::WireFormat::TagSize(descriptor_->number(), descriptor_->type()); int tagSize = internal::WireFormat::TagSize(descriptor_->number(), descriptor_->type());
if (descriptor_->is_packed()) { if (descriptor_->is_packed()) {
writer->WriteLine("size += $0$;", SimpleItoa(tagSize)); printer->Print(
writer->WriteLine( "size += $tag_size$;\n"
"size += pb::CodedOutputStream.ComputeRawVarint32Size((uint) dataSize);"); "size += pb::CodedOutputStream.ComputeRawVarint32Size((uint) dataSize);\n",
"tag_size", SimpleItoa(tagSize));
} else { } else {
writer->WriteLine("size += $0$ * $1$_.Count;", SimpleItoa(tagSize), name()); printer->Print(
"size += $tag_size$ * $name$_.Count;\n",
"tag_size", SimpleItoa(tagSize), "name", name());
} }
writer->Outdent(); printer->Outdent();
writer->WriteLine("}"); printer->Print("}\n");
// cache the data size for packed fields. // cache the data size for packed fields.
if (descriptor_->is_packed()) { if (descriptor_->is_packed()) {
writer->WriteLine("$0$MemoizedSerializedSize = dataSize;", name()); printer->Print(variables_,
"$name$MemoizedSerializedSize = dataSize;\n");
} }
writer->Outdent(); printer->Outdent();
writer->WriteLine("}"); printer->Print("}\n");
} }
void RepeatedEnumFieldGenerator::WriteHash(Writer* writer) { void RepeatedEnumFieldGenerator::WriteHash(io::Printer* printer) {
writer->WriteLine("foreach($0$ i in $1$_)", type_name(), name()); printer->Print(
writer->WriteLine(" hash ^= i.GetHashCode();"); variables_,
"foreach($type_name$ i in $name$_)\n"
" hash ^= i.GetHashCode();\n");
} }
void RepeatedEnumFieldGenerator::WriteEquals(Writer* writer) { void RepeatedEnumFieldGenerator::WriteEquals(io::Printer* printer) {
writer->WriteLine("if($0$_.Count != other.$0$_.Count) return false;", name()); printer->Print(
writer->WriteLine("for(int ix=0; ix < $0$_.Count; ix++)", name()); variables_,
writer->WriteLine(" if(!$0$_[ix].Equals(other.$0$_[ix])) return false;", "if($name$_.Count != other.$name$_.Count) return false;\n"
name()); "for(int ix=0; ix < $name$_.Count; ix++)\n"
" if(!$name$_[ix].Equals(other.$name$_[ix])) return false;\n");
} }
void RepeatedEnumFieldGenerator::WriteToString(Writer* writer) { void RepeatedEnumFieldGenerator::WriteToString(io::Printer* printer) {
writer->WriteLine("PrintField(\"$0$\", $1$_, writer);", descriptor_->name(), printer->Print(variables_,
name()); "PrintField(\"$descriptor_name$\", $name$_, writer);\n");
} }
} // namespace csharp } // namespace csharp

View File

@ -48,17 +48,17 @@ class RepeatedEnumFieldGenerator : public FieldGeneratorBase {
RepeatedEnumFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); RepeatedEnumFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
~RepeatedEnumFieldGenerator(); ~RepeatedEnumFieldGenerator();
virtual void GenerateMembers(Writer* writer); virtual void GenerateMembers(io::Printer* printer);
virtual void GenerateBuilderMembers(Writer* writer); virtual void GenerateBuilderMembers(io::Printer* printer);
virtual void GenerateMergingCode(Writer* writer); virtual void GenerateMergingCode(io::Printer* printer);
virtual void GenerateBuildingCode(Writer* writer); virtual void GenerateBuildingCode(io::Printer* printer);
virtual void GenerateParsingCode(Writer* writer); virtual void GenerateParsingCode(io::Printer* printer);
virtual void GenerateSerializationCode(Writer* writer); virtual void GenerateSerializationCode(io::Printer* printer);
virtual void GenerateSerializedSizeCode(Writer* writer); virtual void GenerateSerializedSizeCode(io::Printer* printer);
virtual void WriteHash(Writer* writer); virtual void WriteHash(io::Printer* printer);
virtual void WriteEquals(Writer* writer); virtual void WriteEquals(io::Printer* printer);
virtual void WriteToString(Writer* writer); virtual void WriteToString(io::Printer* printer);
private: private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedEnumFieldGenerator); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedEnumFieldGenerator);

View File

@ -39,7 +39,6 @@
#include <google/protobuf/compiler/csharp/csharp_helpers.h> #include <google/protobuf/compiler/csharp/csharp_helpers.h>
#include <google/protobuf/compiler/csharp/csharp_repeated_message_field.h> #include <google/protobuf/compiler/csharp/csharp_repeated_message_field.h>
#include <google/protobuf/compiler/csharp/csharp_writer.h>
namespace google { namespace google {
namespace protobuf { namespace protobuf {
@ -49,150 +48,177 @@ namespace csharp {
RepeatedMessageFieldGenerator::RepeatedMessageFieldGenerator( RepeatedMessageFieldGenerator::RepeatedMessageFieldGenerator(
const FieldDescriptor* descriptor, int fieldOrdinal) const FieldDescriptor* descriptor, int fieldOrdinal)
: FieldGeneratorBase(descriptor, fieldOrdinal) { : FieldGeneratorBase(descriptor, fieldOrdinal) {
variables_["message_or_group"] = message_or_group();
} }
RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() { RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {
} }
void RepeatedMessageFieldGenerator::GenerateMembers(Writer* writer) { void RepeatedMessageFieldGenerator::GenerateMembers(io::Printer* printer) {
writer->WriteLine( printer->Print(
"private pbc::PopsicleList<$0$> $1$_ = new pbc::PopsicleList<$0$>();", variables_,
type_name(), name()); "private pbc::PopsicleList<$type_name$> $name$_ = new pbc::PopsicleList<$type_name$>();\n");
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
writer->WriteLine("public scg::IList<$0$> $1$List {", type_name(), printer->Print(
property_name()); variables_,
writer->WriteLine(" get { return $0$_; }", name()); "public scg::IList<$type_name$> $property_name$List {\n"
writer->WriteLine("}"); " get { return $name$_; }\n"
"}\n");
// TODO(jonskeet): Redundant API calls? Possibly - include for portability though. Maybe create an option. // TODO(jonskeet): Redundant API calls? Possibly - include for portability though. Maybe create an option.
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
writer->WriteLine("public int $0$Count {", property_name()); printer->Print(
writer->WriteLine(" get { return $0$_.Count; }", name()); variables_,
writer->WriteLine("}"); "public int $property_name$Count {\n"
" get { return $name$_.Count; }\n"
"}\n");
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
writer->WriteLine("public $0$ Get$1$(int index) {", type_name(), printer->Print(
property_name()); variables_,
writer->WriteLine(" return $0$_[index];", name()); "public $type_name$ Get$property_name$(int index) {\n"
writer->WriteLine("}"); " return $name$_[index];\n"
"}\n");
} }
void RepeatedMessageFieldGenerator::GenerateBuilderMembers(Writer* writer) { void RepeatedMessageFieldGenerator::GenerateBuilderMembers(io::Printer* printer) {
// Note: We can return the original list here, because we make it unmodifiable when we build // Note: We can return the original list here, because we make it unmodifiable when we build
// We return it via IPopsicleList so that collection initializers work more pleasantly. // We return it via IPopsicleList so that collection initializers work more pleasantly.
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
writer->WriteLine("public pbc::IPopsicleList<$0$> $1$List {", type_name(), printer->Print(
property_name()); variables_,
writer->WriteLine(" get { return PrepareBuilder().$0$_; }", name()); "public pbc::IPopsicleList<$type_name$> $property_name$List {\n"
writer->WriteLine("}"); " get { return PrepareBuilder().$name$_; }\n"
AddDeprecatedFlag(writer); "}\n");
writer->WriteLine("public int $0$Count {", property_name()); AddDeprecatedFlag(printer);
writer->WriteLine(" get { return result.$0$Count; }", property_name()); printer->Print(
writer->WriteLine("}"); variables_,
AddDeprecatedFlag(writer); "public int $property_name$Count {\n"
writer->WriteLine("public $0$ Get$1$(int index) {", type_name(), " get { return result.$property_name$Count; }\n"
property_name()); "}\n");
writer->WriteLine(" return result.Get$0$(index);", property_name()); AddDeprecatedFlag(printer);
writer->WriteLine("}"); printer->Print(
AddDeprecatedFlag(writer); variables_,
writer->WriteLine("public Builder Set$0$(int index, $1$ value) {", "public $type_name$ Get$property_name$(int index) {\n"
property_name(), type_name()); " return result.Get$property_name$(index);\n"
AddNullCheck(writer); "}\n");
writer->WriteLine(" PrepareBuilder();"); AddDeprecatedFlag(printer);
writer->WriteLine(" result.$0$_[index] = value;", name()); printer->Print(
writer->WriteLine(" return this;"); variables_,
writer->WriteLine("}"); "public Builder Set$property_name$(int index, $type_name$ value) {\n");
AddNullCheck(printer);
printer->Print(
variables_,
" PrepareBuilder();\n"
" result.$name$_[index] = value;\n"
" return this;\n"
"}\n");
// Extra overload for builder (just on messages) // Extra overload for builder (just on messages)
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
writer->WriteLine( printer->Print(
"public Builder Set$0$(int index, $1$.Builder builderForValue) {", variables_,
property_name(), type_name()); "public Builder Set$property_name$(int index, $type_name$.Builder builderForValue) {\n");
AddNullCheck(writer, "builderForValue"); AddNullCheck(printer, "builderForValue");
writer->WriteLine(" PrepareBuilder();"); printer->Print(
writer->WriteLine(" result.$0$_[index] = builderForValue.Build();", name()); variables_,
writer->WriteLine(" return this;"); " PrepareBuilder();\n"
writer->WriteLine("}"); " result.$name$_[index] = builderForValue.Build();\n"
AddDeprecatedFlag(writer); " return this;\n"
writer->WriteLine("public Builder Add$0$($1$ value) {", property_name(), "}\n");
type_name()); AddDeprecatedFlag(printer);
AddNullCheck(writer); printer->Print(
writer->WriteLine(" PrepareBuilder();"); variables_,
writer->WriteLine(" result.$0$_.Add(value);", name(), type_name()); "public Builder Add$property_name$($type_name$ value) {\n");
writer->WriteLine(" return this;"); AddNullCheck(printer);
writer->WriteLine("}"); printer->Print(
variables_,
" PrepareBuilder();\n"
" result.$name$_.Add(value);\n"
" return this;\n"
"}\n");
// Extra overload for builder (just on messages) // Extra overload for builder (just on messages)
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
writer->WriteLine("public Builder Add$0$($1$.Builder builderForValue) {", printer->Print(
property_name(), type_name()); variables_,
AddNullCheck(writer, "builderForValue"); "public Builder Add$property_name$($type_name$.Builder builderForValue) {\n");
writer->WriteLine(" PrepareBuilder();"); AddNullCheck(printer, "builderForValue");
writer->WriteLine(" result.$0$_.Add(builderForValue.Build());", name()); printer->Print(
writer->WriteLine(" return this;"); variables_,
writer->WriteLine("}"); " PrepareBuilder();\n"
AddDeprecatedFlag(writer); " result.$name$_.Add(builderForValue.Build());\n"
writer->WriteLine( " return this;\n"
"public Builder AddRange$0$(scg::IEnumerable<$1$> values) {", "}\n");
property_name(), type_name()); AddDeprecatedFlag(printer);
writer->WriteLine(" PrepareBuilder();"); printer->Print(
writer->WriteLine(" result.$0$_.Add(values);", name()); variables_,
writer->WriteLine(" return this;"); "public Builder AddRange$property_name$(scg::IEnumerable<$type_name$> values) {\n"
writer->WriteLine("}"); " PrepareBuilder();\n"
AddDeprecatedFlag(writer); " result.$name$_.Add(values);\n"
writer->WriteLine("public Builder Clear$0$() {", property_name()); " return this;\n"
writer->WriteLine(" PrepareBuilder();"); "}\n");
writer->WriteLine(" result.$0$_.Clear();", name()); AddDeprecatedFlag(printer);
writer->WriteLine(" return this;"); printer->Print(
writer->WriteLine("}"); variables_,
"public Builder Clear$property_name$() {\n"
" PrepareBuilder();\n"
" result.$name$_.Clear();\n"
" return this;\n"
"}\n");
} }
void RepeatedMessageFieldGenerator::GenerateMergingCode(Writer* writer) { void RepeatedMessageFieldGenerator::GenerateMergingCode(io::Printer* printer) {
writer->WriteLine("if (other.$0$_.Count != 0) {", name()); printer->Print(
writer->WriteLine(" result.$0$_.Add(other.$0$_);", name()); variables_,
writer->WriteLine("}"); "if (other.$name$_.Count != 0) {\n"
" result.$name$_.Add(other.$name$_);\n"
"}\n");
} }
void RepeatedMessageFieldGenerator::GenerateBuildingCode(Writer* writer) { void RepeatedMessageFieldGenerator::GenerateBuildingCode(io::Printer* printer) {
writer->WriteLine("$0$_.MakeReadOnly();", name()); printer->Print(variables_, "$name$_.MakeReadOnly();\n");
} }
void RepeatedMessageFieldGenerator::GenerateParsingCode(Writer* writer) { void RepeatedMessageFieldGenerator::GenerateParsingCode(io::Printer* printer) {
writer->WriteLine( printer->Print(
"input.Read$0$Array(tag, field_name, result.$1$_, $2$.DefaultInstance, extensionRegistry);", variables_,
message_or_group(), name(), type_name()); "input.Read$message_or_group$Array(tag, field_name, result.$name$_, $type_name$.DefaultInstance, extensionRegistry);\n");
} }
void RepeatedMessageFieldGenerator::GenerateSerializationCode(Writer* writer) { void RepeatedMessageFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
writer->WriteLine("if ($0$_.Count > 0) {", name()); printer->Print(
writer->Indent(); variables_,
writer->WriteLine("output.Write$0$Array($1$, field_names[$3$], $2$_);", "if ($name$_.Count > 0) {\n"
message_or_group(), number(), name(), field_ordinal()); " output.Write$message_or_group$Array($number$, field_names[$field_ordinal$], $name$_);\n"
writer->Outdent(); "}\n");
writer->WriteLine("}");
} }
void RepeatedMessageFieldGenerator::GenerateSerializedSizeCode(Writer* writer) { void RepeatedMessageFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
writer->WriteLine("foreach ($0$ element in $1$List) {", type_name(), printer->Print(
property_name()); variables_,
writer->WriteLine( "foreach ($type_name$ element in $property_name$List) {\n"
" size += pb::CodedOutputStream.Compute$0$Size($1$, element);", " size += pb::CodedOutputStream.Compute$message_or_group$Size($number$, element);\n"
message_or_group(), number()); "}\n");
writer->WriteLine("}");
} }
void RepeatedMessageFieldGenerator::WriteHash(Writer* writer) { void RepeatedMessageFieldGenerator::WriteHash(io::Printer* printer) {
writer->WriteLine("foreach($0$ i in $1$_)", type_name(), name()); printer->Print(
writer->WriteLine(" hash ^= i.GetHashCode();"); variables_,
"foreach($type_name$ i in $name$_)\n"
" hash ^= i.GetHashCode();\n");
} }
void RepeatedMessageFieldGenerator::WriteEquals(Writer* writer) { void RepeatedMessageFieldGenerator::WriteEquals(io::Printer* printer) {
writer->WriteLine("if($0$_.Count != other.$0$_.Count) return false;", name()); printer->Print(
writer->WriteLine("for(int ix=0; ix < $0$_.Count; ix++)", name()); variables_,
writer->WriteLine(" if(!$0$_[ix].Equals(other.$0$_[ix])) return false;", "if($name$_.Count != other.$name$_.Count) return false;\n"
name()); "for(int ix=0; ix < $name$_.Count; ix++)\n"
" if(!$name$_[ix].Equals(other.$name$_[ix])) return false;\n");
} }
void RepeatedMessageFieldGenerator::WriteToString(Writer* writer) { void RepeatedMessageFieldGenerator::WriteToString(io::Printer* printer) {
writer->WriteLine("PrintField(\"$0$\", $1$_, writer);", variables_["field_name"] = GetFieldName(descriptor_);
GetFieldName(descriptor_), name()); printer->Print(
variables_,
"PrintField(\"$field_name$\", $name$_, writer);\n");
} }
} // namespace csharp } // namespace csharp

View File

@ -48,17 +48,17 @@ class RepeatedMessageFieldGenerator : public FieldGeneratorBase {
RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
~RepeatedMessageFieldGenerator(); ~RepeatedMessageFieldGenerator();
virtual void GenerateMembers(Writer* writer); virtual void GenerateMembers(io::Printer* printer);
virtual void GenerateBuilderMembers(Writer* writer); virtual void GenerateBuilderMembers(io::Printer* printer);
virtual void GenerateMergingCode(Writer* writer); virtual void GenerateMergingCode(io::Printer* printer);
virtual void GenerateBuildingCode(Writer* writer); virtual void GenerateBuildingCode(io::Printer* printer);
virtual void GenerateParsingCode(Writer* writer); virtual void GenerateParsingCode(io::Printer* printer);
virtual void GenerateSerializationCode(Writer* writer); virtual void GenerateSerializationCode(io::Printer* printer);
virtual void GenerateSerializedSizeCode(Writer* writer); virtual void GenerateSerializedSizeCode(io::Printer* printer);
virtual void WriteHash(Writer* writer); virtual void WriteHash(io::Printer* printer);
virtual void WriteEquals(Writer* writer); virtual void WriteEquals(io::Printer* printer);
virtual void WriteToString(Writer* writer); virtual void WriteToString(io::Printer* printer);
private: private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedMessageFieldGenerator); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedMessageFieldGenerator);

View File

@ -40,7 +40,6 @@
#include <google/protobuf/compiler/csharp/csharp_helpers.h> #include <google/protobuf/compiler/csharp/csharp_helpers.h>
#include <google/protobuf/compiler/csharp/csharp_repeated_primitive_field.h> #include <google/protobuf/compiler/csharp/csharp_repeated_primitive_field.h>
#include <google/protobuf/compiler/csharp/csharp_writer.h>
namespace google { namespace google {
namespace protobuf { namespace protobuf {
@ -56,161 +55,182 @@ RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {
} }
void RepeatedPrimitiveFieldGenerator::GenerateMembers(Writer* writer) { void RepeatedPrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) {
if (descriptor_->is_packed() && optimize_speed()) { if (descriptor_->is_packed() && optimize_speed()) {
writer->WriteLine("private int $0$MemoizedSerializedSize;", name()); printer->Print(variables_, "private int $name$MemoizedSerializedSize;\n");
} }
writer->WriteLine( printer->Print(variables_,
"private pbc::PopsicleList<$0$> $1$_ = new pbc::PopsicleList<$0$>();", "private pbc::PopsicleList<$type_name$> $name$_ = new pbc::PopsicleList<$type_name$>();\n");
type_name(), name()); AddPublicMemberAttributes(printer);
AddPublicMemberAttributes(writer); printer->Print(
writer->WriteLine("public scg::IList<$0$> $1$List {", type_name(), variables_,
property_name()); "public scg::IList<$type_name$> $property_name$List {\n"
writer->WriteLine(" get { return pbc::Lists.AsReadOnly($0$_); }", name()); " get { return pbc::Lists.AsReadOnly($name$_); }\n"
writer->WriteLine("}"); "}\n");
// TODO(jonskeet): Redundant API calls? Possibly - include for portability though. Maybe create an option. // TODO(jonskeet): Redundant API calls? Possibly - include for portability though. Maybe create an option.
AddDeprecatedFlag(writer); AddDeprecatedFlag(printer);
writer->WriteLine("public int $0$Count {", property_name()); printer->Print(
writer->WriteLine(" get { return $0$_.Count; }", name()); variables_,
writer->WriteLine("}"); "public int $property_name$Count {\n"
" get { return $name$_.Count; }\n"
"}\n");
AddPublicMemberAttributes(writer); AddPublicMemberAttributes(printer);
writer->WriteLine("public $0$ Get$1$(int index) {", type_name(), printer->Print(
property_name()); variables_,
writer->WriteLine(" return $0$_[index];", name()); "public $type_name$ Get$property_name$(int index) {\n"
writer->WriteLine("}"); " return $name$_[index];\n"
"}\n");
} }
void RepeatedPrimitiveFieldGenerator::GenerateBuilderMembers(Writer* writer) { void RepeatedPrimitiveFieldGenerator::GenerateBuilderMembers(io::Printer* printer) {
// Note: We can return the original list here, because we make it unmodifiable when we build // Note: We can return the original list here, because we make it unmodifiable when we build
// We return it via IPopsicleList so that collection initializers work more pleasantly. // We return it via IPopsicleList so that collection initializers work more pleasantly.
AddPublicMemberAttributes(writer); AddPublicMemberAttributes(printer);
writer->WriteLine("public pbc::IPopsicleList<$0$> $1$List {", type_name(), printer->Print(
property_name()); variables_,
writer->WriteLine(" get { return PrepareBuilder().$0$_; }", name()); "public pbc::IPopsicleList<$type_name$> $property_name$List {\n"
writer->WriteLine("}"); " get { return PrepareBuilder().$name$_; }\n"
AddDeprecatedFlag(writer); "}\n");
writer->WriteLine("public int $0$Count {", property_name()); AddDeprecatedFlag(printer);
writer->WriteLine(" get { return result.$0$Count; }", property_name()); printer->Print(
writer->WriteLine("}"); variables_,
AddPublicMemberAttributes(writer); "public int $property_name$Count {\n"
writer->WriteLine("public $0$ Get$1$(int index) {", type_name(), " get { return result.$property_name$Count; }\n"
property_name()); "}\n");
writer->WriteLine(" return result.Get$0$(index);", property_name()); AddPublicMemberAttributes(printer);
writer->WriteLine("}"); printer->Print(
AddPublicMemberAttributes(writer); variables_,
writer->WriteLine("public Builder Set$0$(int index, $1$ value) {", "public $type_name$ Get$property_name$(int index) {\n"
property_name(), type_name()); " return result.Get$property_name$(index);\n"
AddNullCheck(writer); "}\n");
writer->WriteLine(" PrepareBuilder();"); AddPublicMemberAttributes(printer);
writer->WriteLine(" result.$0$_[index] = value;", name()); printer->Print(
writer->WriteLine(" return this;"); variables_,
writer->WriteLine("}"); "public Builder Set$property_name$(int index, $type_name$ value) {\n");
AddPublicMemberAttributes(writer); AddNullCheck(printer);
writer->WriteLine("public Builder Add$0$($1$ value) {", property_name(), printer->Print(
type_name()); variables_,
AddNullCheck(writer); " PrepareBuilder();\n"
writer->WriteLine(" PrepareBuilder();"); " result.$name$_[index] = value;\n"
writer->WriteLine(" result.$0$_.Add(value);", name(), type_name()); " return this;\n"
writer->WriteLine(" return this;"); "}\n");
writer->WriteLine("}"); AddPublicMemberAttributes(printer);
AddPublicMemberAttributes(writer); printer->Print(
writer->WriteLine( variables_,
"public Builder AddRange$0$(scg::IEnumerable<$1$> values) {", "public Builder Add$property_name$($type_name$ value) {\n");
property_name(), type_name()); AddNullCheck(printer);
writer->WriteLine(" PrepareBuilder();"); printer->Print(
writer->WriteLine(" result.$0$_.Add(values);", name()); variables_,
writer->WriteLine(" return this;"); " PrepareBuilder();\n"
writer->WriteLine("}"); " result.$name$_.Add(value);\n"
AddDeprecatedFlag(writer); " return this;\n"
writer->WriteLine("public Builder Clear$0$() {", property_name()); "}\n");
writer->WriteLine(" PrepareBuilder();"); AddPublicMemberAttributes(printer);
writer->WriteLine(" result.$0$_.Clear();", name()); printer->Print(
writer->WriteLine(" return this;"); variables_,
writer->WriteLine("}"); "public Builder AddRange$property_name$(scg::IEnumerable<$type_name$> values) {\n"
" PrepareBuilder();\n"
" result.$name$_.Add(values);\n"
" return this;\n"
"}\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public Builder Clear$property_name$() {\n"
" PrepareBuilder();\n"
" result.$name$_.Clear();\n"
" return this;\n"
"}\n");
} }
void RepeatedPrimitiveFieldGenerator::GenerateMergingCode(Writer* writer) { void RepeatedPrimitiveFieldGenerator::GenerateMergingCode(io::Printer* printer) {
writer->WriteLine("if (other.$0$_.Count != 0) {", name()); printer->Print(
writer->WriteLine(" result.$0$_.Add(other.$0$_);", name()); variables_,
writer->WriteLine("}"); "if (other.$name$_.Count != 0) {\n"
" result.$name$_.Add(other.$name$_);\n"
"}\n");
} }
void RepeatedPrimitiveFieldGenerator::GenerateBuildingCode(Writer* writer) { void RepeatedPrimitiveFieldGenerator::GenerateBuildingCode(io::Printer* printer) {
writer->WriteLine("$0$_.MakeReadOnly();", name()); printer->Print(variables_, "$name$_.MakeReadOnly();\n");
} }
void RepeatedPrimitiveFieldGenerator::GenerateParsingCode(Writer* writer) { void RepeatedPrimitiveFieldGenerator::GenerateParsingCode(io::Printer* printer) {
writer->WriteLine("input.Read$0$Array(tag, field_name, result.$1$_);", printer->Print(variables_,
capitalized_type_name(), name()); "input.Read$capitalized_type_name$Array(tag, field_name, result.$name$_);\n");
} }
void RepeatedPrimitiveFieldGenerator::GenerateSerializationCode( void RepeatedPrimitiveFieldGenerator::GenerateSerializationCode(
Writer* writer) { io::Printer* printer) {
writer->WriteLine("if ($0$_.Count > 0) {", name()); printer->Print(variables_, "if ($name$_.Count > 0) {\n");
writer->Indent(); printer->Indent();
if (descriptor_->is_packed()) { if (descriptor_->is_packed()) {
writer->WriteLine( printer->Print(variables_,
"output.WritePacked$0$Array($1$, field_names[$3$], $2$MemoizedSerializedSize, $2$_);", "output.WritePacked$capitalized_type_name$Array($number$, field_names[$field_ordinal$], $name$MemoizedSerializedSize, $name$_);\n");
capitalized_type_name(), number(), name(), field_ordinal());
} else { } else {
writer->WriteLine("output.Write$0$Array($1$, field_names[$3$], $2$_);", printer->Print(variables_,
capitalized_type_name(), number(), name(), "output.Write$capitalized_type_name$Array($number$, field_names[$field_ordinal$], $name$_);\n");
field_ordinal());
} }
writer->Outdent(); printer->Outdent();
writer->WriteLine("}"); printer->Print("}\n");
} }
void RepeatedPrimitiveFieldGenerator::GenerateSerializedSizeCode( void RepeatedPrimitiveFieldGenerator::GenerateSerializedSizeCode(
Writer* writer) { io::Printer* printer) {
writer->WriteLine("{"); printer->Print("{\n");
writer->Indent(); printer->Indent();
writer->WriteLine("int dataSize = 0;"); printer->Print("int dataSize = 0;\n");
int fixedSize = GetFixedSize(descriptor_->type()); int fixedSize = GetFixedSize(descriptor_->type());
if (fixedSize == -1) { if (fixedSize == -1) {
writer->WriteLine("foreach ($0$ element in $1$List) {", type_name(), printer->Print(
property_name()); variables_,
writer->WriteLine( "foreach ($type_name$ element in $property_name$List) {\n"
" dataSize += pb::CodedOutputStream.Compute$0$SizeNoTag(element);", " dataSize += pb::CodedOutputStream.Compute$capitalized_type_name$SizeNoTag(element);\n"
capitalized_type_name(), number()); "}\n");
writer->WriteLine("}");
} else { } else {
writer->WriteLine("dataSize = $0$ * $1$_.Count;", SimpleItoa(fixedSize), name()); printer->Print(
"dataSize = $size$ * $name$_.Count;\n",
"size", SimpleItoa(fixedSize), "name", name());
} }
writer->WriteLine("size += dataSize;"); printer->Print("size += dataSize;\n");
int tagSize = internal::WireFormat::TagSize(descriptor_->number(), descriptor_->type()); int tagSize = internal::WireFormat::TagSize(descriptor_->number(), descriptor_->type());
if (descriptor_->is_packed()) { if (descriptor_->is_packed()) {
writer->WriteLine("if ($0$_.Count != 0) {", name()); printer->Print(
writer->WriteLine( "if ($name$_.Count != 0) {\n"
" size += $0$ + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);", " size += $tag_size$ + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);\n"
SimpleItoa(tagSize)); "}\n",
writer->WriteLine("}"); "name", name(), "tag_size", SimpleItoa(tagSize));
} else { } else {
writer->WriteLine("size += $0$ * $1$_.Count;", SimpleItoa(tagSize), name()); printer->Print(
"size += $tag_size$ * $name$_.Count;\n",
"tag_size", SimpleItoa(tagSize), "name", name());
} }
// cache the data size for packed fields. // cache the data size for packed fields.
if (descriptor_->is_packed()) { if (descriptor_->is_packed()) {
writer->WriteLine("$0$MemoizedSerializedSize = dataSize;", name()); printer->Print(variables_, "$name$MemoizedSerializedSize = dataSize;\n");
} }
writer->Outdent(); printer->Outdent();
writer->WriteLine("}"); printer->Print("}\n");
} }
void RepeatedPrimitiveFieldGenerator::WriteHash(Writer* writer) { void RepeatedPrimitiveFieldGenerator::WriteHash(io::Printer* printer) {
writer->WriteLine("foreach($0$ i in $1$_)", type_name(), name()); printer->Print(
writer->WriteLine(" hash ^= i.GetHashCode();"); variables_,
"foreach($type_name$ i in $name$_)\n"
" hash ^= i.GetHashCode();\n");
} }
void RepeatedPrimitiveFieldGenerator::WriteEquals(Writer* writer) { void RepeatedPrimitiveFieldGenerator::WriteEquals(io::Printer* printer) {
writer->WriteLine("if($0$_.Count != other.$0$_.Count) return false;", name()); printer->Print(
writer->WriteLine("for(int ix=0; ix < $0$_.Count; ix++)", name()); variables_,
writer->WriteLine(" if(!$0$_[ix].Equals(other.$0$_[ix])) return false;", "if($name$_.Count != other.$name$_.Count) return false;\n"
name()); "for(int ix=0; ix < $name$_.Count; ix++)\n"
" if(!$name$_[ix].Equals(other.$name$_[ix])) return false;\n");
} }
void RepeatedPrimitiveFieldGenerator::WriteToString(Writer* writer) { void RepeatedPrimitiveFieldGenerator::WriteToString(io::Printer* printer) {
writer->WriteLine("PrintField(\"$0$\", $1$_, writer);", descriptor_->name(), printer->Print(variables_,
name()); "PrintField(\"$descriptor_name$\", $name$_, writer);\n");
} }
} // namespace csharp } // namespace csharp

View File

@ -48,17 +48,17 @@ class RepeatedPrimitiveFieldGenerator : public FieldGeneratorBase {
RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
~RepeatedPrimitiveFieldGenerator(); ~RepeatedPrimitiveFieldGenerator();
virtual void GenerateMembers(Writer* writer); virtual void GenerateMembers(io::Printer* printer);
virtual void GenerateBuilderMembers(Writer* writer); virtual void GenerateBuilderMembers(io::Printer* printer);
virtual void GenerateMergingCode(Writer* writer); virtual void GenerateMergingCode(io::Printer* printer);
virtual void GenerateBuildingCode(Writer* writer); virtual void GenerateBuildingCode(io::Printer* printer);
virtual void GenerateParsingCode(Writer* writer); virtual void GenerateParsingCode(io::Printer* printer);
virtual void GenerateSerializationCode(Writer* writer); virtual void GenerateSerializationCode(io::Printer* printer);
virtual void GenerateSerializedSizeCode(Writer* writer); virtual void GenerateSerializedSizeCode(io::Printer* printer);
virtual void WriteHash(Writer* writer); virtual void WriteHash(io::Printer* printer);
virtual void WriteEquals(Writer* writer); virtual void WriteEquals(io::Printer* printer);
virtual void WriteToString(Writer* writer); virtual void WriteToString(io::Printer* printer);
private: private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedPrimitiveFieldGenerator); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedPrimitiveFieldGenerator);

View File

@ -39,7 +39,6 @@
#include <google/protobuf/compiler/csharp/csharp_source_generator_base.h> #include <google/protobuf/compiler/csharp/csharp_source_generator_base.h>
#include <google/protobuf/compiler/csharp/csharp_helpers.h> #include <google/protobuf/compiler/csharp/csharp_helpers.h>
#include <google/protobuf/compiler/csharp/csharp_writer.h>
namespace google { namespace google {
namespace protobuf { namespace protobuf {
@ -61,7 +60,7 @@ SourceGeneratorBase::SourceGeneratorBase(const FileDescriptor* descriptor)
SourceGeneratorBase::~SourceGeneratorBase() { SourceGeneratorBase::~SourceGeneratorBase() {
} }
void SourceGeneratorBase::WriteGeneratedCodeAttributes(Writer* writer) { void SourceGeneratorBase::WriteGeneratedCodeAttributes(io::Printer* printer) {
// This hook can be used to reintroduce generated code attributes in the future. // This hook can be used to reintroduce generated code attributes in the future.
} }

View File

@ -40,8 +40,6 @@ namespace protobuf {
namespace compiler { namespace compiler {
namespace csharp { namespace csharp {
class Writer;
class SourceGeneratorBase { class SourceGeneratorBase {
protected: protected:
SourceGeneratorBase(const FileDescriptor* descriptor); SourceGeneratorBase(const FileDescriptor* descriptor);
@ -62,7 +60,7 @@ class SourceGeneratorBase {
return runtimeSuffix_; return runtimeSuffix_;
} }
void WriteGeneratedCodeAttributes(Writer* writer); void WriteGeneratedCodeAttributes(io::Printer* printer);
private: private:
const FileDescriptor* descriptor_; const FileDescriptor* descriptor_;

View File

@ -42,7 +42,6 @@
#include <google/protobuf/compiler/csharp/csharp_extension.h> #include <google/protobuf/compiler/csharp/csharp_extension.h>
#include <google/protobuf/compiler/csharp/csharp_helpers.h> #include <google/protobuf/compiler/csharp/csharp_helpers.h>
#include <google/protobuf/compiler/csharp/csharp_message.h> #include <google/protobuf/compiler/csharp/csharp_message.h>
#include <google/protobuf/compiler/csharp/csharp_writer.h>
namespace google { namespace google {
namespace protobuf { namespace protobuf {
@ -60,218 +59,224 @@ UmbrellaClassGenerator::UmbrellaClassGenerator(const FileDescriptor* file)
UmbrellaClassGenerator::~UmbrellaClassGenerator() { UmbrellaClassGenerator::~UmbrellaClassGenerator() {
} }
void UmbrellaClassGenerator::Generate(Writer* writer) { void UmbrellaClassGenerator::Generate(io::Printer* printer) {
WriteIntroduction(writer); WriteIntroduction(printer);
WriteExtensionRegistration(writer); WriteExtensionRegistration(printer);
// write children: Extensions // write children: Extensions
if (file_->extension_count() > 0) { if (file_->extension_count() > 0) {
writer->WriteLine("#region Extensions"); printer->Print("#region Extensions\n");
for (int i = 0; i < file_->extension_count(); i++) { for (int i = 0; i < file_->extension_count(); i++) {
ExtensionGenerator extensionGenerator(file_->extension(i)); ExtensionGenerator extensionGenerator(file_->extension(i));
extensionGenerator.Generate(writer); extensionGenerator.Generate(printer);
} }
writer->WriteLine("#endregion"); printer->Print("#endregion\n");
writer->WriteLine(); printer->Print("\n");
} }
writer->WriteLine("#region Static variables"); printer->Print("#region Static variables\n");
for (int i = 0; i < file_->message_type_count(); i++) { for (int i = 0; i < file_->message_type_count(); i++) {
MessageGenerator messageGenerator(file_->message_type(i)); MessageGenerator messageGenerator(file_->message_type(i));
messageGenerator.GenerateStaticVariables(writer); messageGenerator.GenerateStaticVariables(printer);
} }
writer->WriteLine("#endregion"); printer->Print("#endregion\n");
if (!use_lite_runtime()) { if (!use_lite_runtime()) {
WriteDescriptor(writer); WriteDescriptor(printer);
} else { } else {
WriteLiteExtensions(writer); WriteLiteExtensions(printer);
} }
// Close the class declaration. // Close the class declaration.
writer->Outdent(); printer->Outdent();
writer->WriteLine("}"); printer->Print("}\n");
// Close the namespace around the umbrella class if defined // Close the namespace around the umbrella class if defined
if (!umbrellaNamespace_.empty()) { if (!umbrellaNamespace_.empty()) {
writer->Outdent(); printer->Outdent();
writer->WriteLine("}"); printer->Print("}\n");
} }
// write children: Enums // write children: Enums
if (file_->enum_type_count() > 0) { if (file_->enum_type_count() > 0) {
writer->WriteLine("#region Enums"); printer->Print("#region Enums\n");
for (int i = 0; i < file_->enum_type_count(); i++) { for (int i = 0; i < file_->enum_type_count(); i++) {
EnumGenerator enumGenerator(file_->enum_type(i)); EnumGenerator enumGenerator(file_->enum_type(i));
enumGenerator.Generate(writer); enumGenerator.Generate(printer);
} }
writer->WriteLine("#endregion"); printer->Print("#endregion\n");
writer->WriteLine(); printer->Print("\n");
} }
// write children: Messages // write children: Messages
if (file_->message_type_count() > 0) { if (file_->message_type_count() > 0) {
writer->WriteLine("#region Messages"); printer->Print("#region Messages\n");
for (int i = 0; i < file_->message_type_count(); i++) { for (int i = 0; i < file_->message_type_count(); i++) {
MessageGenerator messageGenerator(file_->message_type(i)); MessageGenerator messageGenerator(file_->message_type(i));
messageGenerator.Generate(writer); messageGenerator.Generate(printer);
} }
writer->WriteLine("#endregion"); printer->Print("#endregion\n");
writer->WriteLine(); printer->Print("\n");
} }
// TODO(jtattermusch): add insertion point for services. // TODO(jtattermusch): add insertion point for services.
if (!namespace_.empty()) { if (!namespace_.empty()) {
writer->Outdent(); printer->Outdent();
writer->WriteLine("}"); printer->Print("}\n");
} }
writer->WriteLine(); printer->Print("\n");
writer->WriteLine("#endregion Designer generated code"); printer->Print("#endregion Designer generated code\n");
} }
void UmbrellaClassGenerator::WriteIntroduction(Writer* writer) { void UmbrellaClassGenerator::WriteIntroduction(io::Printer* printer) {
writer->WriteLine( printer->Print(
"// Generated by the protocol buffer compiler. DO NOT EDIT!"); "// Generated by the protocol buffer compiler. DO NOT EDIT!\n"
writer->WriteLine("// source: $0$", file_->name()); "// source: $file_name$\n"
writer->WriteLine("#pragma warning disable 1591, 0612, 3021"); "#pragma warning disable 1591, 0612, 3021\n"
writer->WriteLine("#region Designer generated code"); "#region Designer generated code\n"
"\n"
writer->WriteLine(); "using pb = global::Google.ProtocolBuffers;\n"
writer->WriteLine("using pb = global::Google.ProtocolBuffers;"); "using pbc = global::Google.ProtocolBuffers.Collections;\n"
writer->WriteLine("using pbc = global::Google.ProtocolBuffers.Collections;"); "using pbd = global::Google.ProtocolBuffers.Descriptors;\n"
writer->WriteLine("using pbd = global::Google.ProtocolBuffers.Descriptors;"); "using scg = global::System.Collections.Generic;\n",
writer->WriteLine("using scg = global::System.Collections.Generic;"); "file_name", file_->name());
if (!namespace_.empty()) { if (!namespace_.empty()) {
writer->WriteLine("namespace $0$ {", namespace_); printer->Print("namespace $namespace$ {\n", "namespace", namespace_);
writer->Indent(); printer->Indent();
writer->WriteLine(); printer->Print("\n");
} }
// Add the namespace around the umbrella class if defined // Add the namespace around the umbrella class if defined
if (!umbrellaNamespace_.empty()) { if (!umbrellaNamespace_.empty()) {
writer->WriteLine("namespace $0$ {", umbrellaNamespace_); printer->Print("namespace $umbrella_namespace$ {\n",
writer->Indent(); "umbrella_namespace", umbrellaNamespace_);
writer->WriteLine(); printer->Indent();
printer->Print("\n");
} }
writer->WriteLine( printer->Print(
"[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]"); "[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]\n");
WriteGeneratedCodeAttributes(writer); WriteGeneratedCodeAttributes(printer);
writer->WriteLine("$0$ static partial class $1$ {", class_access_level(), printer->Print(
umbrellaClassname_); "$access_level$ static partial class $umbrella_class_name$ {\n"
writer->WriteLine(); "\n",
writer->Indent(); "access_level", class_access_level(),
"umbrella_class_name", umbrellaClassname_);
printer->Indent();
} }
void UmbrellaClassGenerator::WriteExtensionRegistration(Writer* writer) { void UmbrellaClassGenerator::WriteExtensionRegistration(io::Printer* printer) {
writer->WriteLine("#region Extension registration"); printer->Print(
writer->WriteLine( "#region Extension registration\n"
"public static void RegisterAllExtensions(pb::ExtensionRegistry registry) {"); "public static void RegisterAllExtensions(pb::ExtensionRegistry registry) {\n");
writer->Indent(); printer->Indent();
for (int i = 0; i < file_->extension_count(); i++) { for (int i = 0; i < file_->extension_count(); i++) {
ExtensionGenerator extensionGenerator(file_->extension(i)); ExtensionGenerator extensionGenerator(file_->extension(i));
extensionGenerator.GenerateExtensionRegistrationCode(writer); extensionGenerator.GenerateExtensionRegistrationCode(printer);
} }
for (int i = 0; i < file_->message_type_count(); i++) { for (int i = 0; i < file_->message_type_count(); i++) {
MessageGenerator messageGenerator(file_->message_type(i)); MessageGenerator messageGenerator(file_->message_type(i));
messageGenerator.GenerateExtensionRegistrationCode(writer); messageGenerator.GenerateExtensionRegistrationCode(printer);
} }
writer->Outdent(); printer->Outdent();
writer->WriteLine("}"); printer->Print("}\n");
writer->WriteLine("#endregion"); printer->Print("#endregion\n");
} }
void UmbrellaClassGenerator::WriteDescriptor(Writer* writer) { void UmbrellaClassGenerator::WriteDescriptor(io::Printer* printer) {
writer->WriteLine("#region Descriptor"); printer->Print(
"#region Descriptor\n"
writer->WriteLine("public static pbd::FileDescriptor Descriptor {"); "public static pbd::FileDescriptor Descriptor {\n"
writer->WriteLine(" get { return descriptor; }"); " get { return descriptor; }\n"
writer->WriteLine("}"); "}\n"
writer->WriteLine("private static pbd::FileDescriptor descriptor;"); "private static pbd::FileDescriptor descriptor;\n"
writer->WriteLine(); "\n"
writer->WriteLine("static $0$() {", umbrellaClassname_); "static $umbrella_class_name$() {\n",
writer->Indent(); "umbrella_class_name", umbrellaClassname_);
writer->WriteLine( printer->Indent();
"byte[] descriptorData = global::System.Convert.FromBase64String("); printer->Print(
writer->Indent(); "byte[] descriptorData = global::System.Convert.FromBase64String(\n");
writer->Indent(); printer->Indent();
writer->WriteLine("string.Concat("); printer->Indent();
writer->Indent(); printer->Print("string.Concat(\n");
printer->Indent();
// TODO(jonskeet): Consider a C#-escaping format here instead of just Base64. // TODO(jonskeet): Consider a C#-escaping format here instead of just Base64.
std::string base64 = FileDescriptorToBase64(file_); std::string base64 = FileDescriptorToBase64(file_);
while (base64.size() > 60) { while (base64.size() > 60) {
writer->WriteLine("\"$0$\", ", base64.substr(0, 60)); printer->Print("\"$base64$\", \n", "base64", base64.substr(0, 60));
base64 = base64.substr(60); base64 = base64.substr(60);
} }
writer->Outdent(); printer->Outdent();
writer->WriteLine("\"$0$\"));", base64); printer->Print("\"$base64$\"));\n", "base64", base64);
writer->Outdent(); printer->Outdent();
writer->Outdent(); printer->Outdent();
writer->WriteLine( printer->Print(
"pbd::FileDescriptor.InternalDescriptorAssigner assigner = delegate(pbd::FileDescriptor root) {"); "pbd::FileDescriptor.InternalDescriptorAssigner assigner = delegate(pbd::FileDescriptor root) {\n");
writer->Indent(); printer->Indent();
writer->WriteLine("descriptor = root;"); printer->Print("descriptor = root;\n");
for (int i = 0; i < file_->message_type_count(); i++) { for (int i = 0; i < file_->message_type_count(); i++) {
MessageGenerator messageGenerator(file_->message_type(i)); MessageGenerator messageGenerator(file_->message_type(i));
messageGenerator.GenerateStaticVariableInitializers(writer); messageGenerator.GenerateStaticVariableInitializers(printer);
} }
for (int i = 0; i < file_->extension_count(); i++) { for (int i = 0; i < file_->extension_count(); i++) {
ExtensionGenerator extensionGenerator(file_->extension(i)); ExtensionGenerator extensionGenerator(file_->extension(i));
extensionGenerator.GenerateStaticVariableInitializers(writer); extensionGenerator.GenerateStaticVariableInitializers(printer);
} }
if (uses_extensions()) { if (uses_extensions()) {
// Must construct an ExtensionRegistry containing all possible extensions // Must construct an ExtensionRegistry containing all possible extensions
// and return it. // and return it.
writer->WriteLine( printer->Print(
"pb::ExtensionRegistry registry = pb::ExtensionRegistry.CreateInstance();"); "pb::ExtensionRegistry registry = pb::ExtensionRegistry.CreateInstance();\n");
writer->WriteLine("RegisterAllExtensions(registry);"); printer->Print("RegisterAllExtensions(registry);\n");
for (int i = 0; i < file_->dependency_count(); i++) { for (int i = 0; i < file_->dependency_count(); i++) {
writer->WriteLine("$0$.RegisterAllExtensions(registry);", printer->Print("$dependency$.RegisterAllExtensions(registry);\n",
GetFullUmbrellaClassName(file_->dependency(i))); "dependency", GetFullUmbrellaClassName(file_->dependency(i)));
} }
writer->WriteLine("return registry;"); printer->Print("return registry;\n");
} else { } else {
writer->WriteLine("return null;"); printer->Print("return null;\n");
} }
writer->Outdent(); printer->Outdent();
writer->WriteLine("};"); printer->Print("};\n");
// ----------------------------------------------------------------- // -----------------------------------------------------------------
// Invoke internalBuildGeneratedFileFrom() to build the file. // Invoke internalBuildGeneratedFileFrom() to build the file.
writer->WriteLine( printer->Print(
"pbd::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,"); "pbd::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData,\n");
writer->WriteLine(" new pbd::FileDescriptor[] {"); printer->Print(" new pbd::FileDescriptor[] {\n");
for (int i = 0; i < file_->dependency_count(); i++) { for (int i = 0; i < file_->dependency_count(); i++) {
writer->WriteLine(" $0$.Descriptor, ", printer->Print(
GetFullUmbrellaClassName(file_->dependency(i))); " $full_umbrella_class_name$.Descriptor, \n",
"full_umbrella_class_name",
GetFullUmbrellaClassName(file_->dependency(i)));
} }
writer->WriteLine(" }, assigner);"); printer->Print(" }, assigner);\n");
writer->Outdent(); printer->Outdent();
writer->WriteLine("}"); printer->Print("}\n");
writer->WriteLine("#endregion"); printer->Print("#endregion\n\n");
writer->WriteLine();
} }
void UmbrellaClassGenerator::WriteLiteExtensions(Writer* writer) { void UmbrellaClassGenerator::WriteLiteExtensions(io::Printer* printer) {
writer->WriteLine("#region Extensions"); printer->Print(
writer->WriteLine("internal static readonly object Descriptor;"); "#region Extensions\n"
writer->WriteLine("static $0$() {", umbrellaClassname_); "internal static readonly object Descriptor;\n"
writer->Indent(); "static $umbrella_class_name$() {\n",
writer->WriteLine("Descriptor = null;"); "umbrella_class_name", umbrellaClassname_);
printer->Indent();
printer->Print("Descriptor = null;\n");
for (int i = 0; i < file_->message_type_count(); i++) { for (int i = 0; i < file_->message_type_count(); i++) {
MessageGenerator messageGenerator(file_->message_type(i)); MessageGenerator messageGenerator(file_->message_type(i));
messageGenerator.GenerateStaticVariableInitializers(writer); messageGenerator.GenerateStaticVariableInitializers(printer);
} }
for (int i = 0; i < file_->extension_count(); i++) { for (int i = 0; i < file_->extension_count(); i++) {
ExtensionGenerator extensionGenerator(file_->extension(i)); ExtensionGenerator extensionGenerator(file_->extension(i));
extensionGenerator.GenerateStaticVariableInitializers(writer); extensionGenerator.GenerateStaticVariableInitializers(printer);
} }
writer->Outdent(); printer->Outdent();
writer->WriteLine("}"); printer->Print("}\n");
writer->WriteLine("#endregion"); printer->Print("#endregion\n\n");
writer->WriteLine();
} }
bool UmbrellaClassGenerator::uses_extensions() { bool UmbrellaClassGenerator::uses_extensions() {

View File

@ -48,7 +48,7 @@ class UmbrellaClassGenerator : public SourceGeneratorBase {
UmbrellaClassGenerator(const FileDescriptor* file); UmbrellaClassGenerator(const FileDescriptor* file);
~UmbrellaClassGenerator(); ~UmbrellaClassGenerator();
void Generate(Writer* write); void Generate(io::Printer* printer);
private: private:
const FileDescriptor* file_; const FileDescriptor* file_;
@ -57,10 +57,10 @@ class UmbrellaClassGenerator : public SourceGeneratorBase {
std::string umbrellaClassname_; std::string umbrellaClassname_;
std::string umbrellaNamespace_; std::string umbrellaNamespace_;
void WriteIntroduction(Writer* writer); void WriteIntroduction(io::Printer* printer);
void WriteExtensionRegistration(Writer* writer); void WriteExtensionRegistration(io::Printer* printer);
void WriteDescriptor(Writer* writer); void WriteDescriptor(io::Printer* printer);
void WriteLiteExtensions(Writer* write); void WriteLiteExtensions(io::Printer* printer);
bool uses_extensions(); bool uses_extensions();