Commit f5242682 authored by Jon Skeet's avatar Jon Skeet

First stab at new proto3-only code generator

parent 8fe039a6
...@@ -369,8 +369,6 @@ libprotoc_la_SOURCES = \ ...@@ -369,8 +369,6 @@ libprotoc_la_SOURCES = \
google/protobuf/compiler/csharp/csharp_enum.h \ google/protobuf/compiler/csharp/csharp_enum.h \
google/protobuf/compiler/csharp/csharp_enum_field.cc \ google/protobuf/compiler/csharp/csharp_enum_field.cc \
google/protobuf/compiler/csharp/csharp_enum_field.h \ google/protobuf/compiler/csharp/csharp_enum_field.h \
google/protobuf/compiler/csharp/csharp_extension.cc \
google/protobuf/compiler/csharp/csharp_extension.h \
google/protobuf/compiler/csharp/csharp_field_base.cc \ google/protobuf/compiler/csharp/csharp_field_base.cc \
google/protobuf/compiler/csharp/csharp_field_base.h \ google/protobuf/compiler/csharp/csharp_field_base.h \
google/protobuf/compiler/csharp/csharp_generator.cc \ google/protobuf/compiler/csharp/csharp_generator.cc \
...@@ -391,9 +389,7 @@ libprotoc_la_SOURCES = \ ...@@ -391,9 +389,7 @@ libprotoc_la_SOURCES = \
google/protobuf/compiler/csharp/csharp_source_generator_base.cc \ google/protobuf/compiler/csharp/csharp_source_generator_base.cc \
google/protobuf/compiler/csharp/csharp_source_generator_base.h \ google/protobuf/compiler/csharp/csharp_source_generator_base.h \
google/protobuf/compiler/csharp/csharp_umbrella_class.cc \ google/protobuf/compiler/csharp/csharp_umbrella_class.cc \
google/protobuf/compiler/csharp/csharp_umbrella_class.h \ google/protobuf/compiler/csharp/csharp_umbrella_class.h
google/protobuf/compiler/csharp/csharp_writer.cc \
google/protobuf/compiler/csharp/csharp_writer.h
bin_PROGRAMS = protoc bin_PROGRAMS = protoc
protoc_LDADD = $(PTHREAD_LIBS) libprotobuf.la libprotoc.la protoc_LDADD = $(PTHREAD_LIBS) libprotobuf.la libprotoc.la
......
...@@ -58,7 +58,7 @@ EnumGenerator::~EnumGenerator() { ...@@ -58,7 +58,7 @@ EnumGenerator::~EnumGenerator() {
void EnumGenerator::Generate(io::Printer* printer) { void EnumGenerator::Generate(io::Printer* printer) {
WriteGeneratedCodeAttributes(printer); WriteGeneratedCodeAttributes(printer);
printer->Print("$access_level$ enum $name$ {\n", printer->Print("$access_level$ enum $name$ : long {\n",
"access_level", class_access_level(), "access_level", class_access_level(),
"name", descriptor_->name()); "name", descriptor_->name());
printer->Indent(); printer->Indent();
......
...@@ -55,36 +55,22 @@ EnumFieldGenerator::~EnumFieldGenerator() { ...@@ -55,36 +55,22 @@ EnumFieldGenerator::~EnumFieldGenerator() {
void EnumFieldGenerator::GenerateParsingCode(io::Printer* printer) { void EnumFieldGenerator::GenerateParsingCode(io::Printer* printer) {
printer->Print(variables_, printer->Print(variables_,
"object unknown;\n" "input.ReadEnum(ref $name$_);\n");
"if(input.ReadEnum(ref result.$name$_, out unknown)) {\n");
if (SupportFieldPresence(descriptor_->file())) {
printer->Print(variables_,
" result.has$property_name$ = true;\n");
}
printer->Print("} else if(unknown is int) {\n");
if (!use_lite_runtime()) {
printer->Print(variables_,
" if (unknownFields == null) {\n" // First unknown field - create builder now
" unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);\n"
" }\n"
" unknownFields.MergeVarintField($number$, (ulong)(int)unknown);\n");
}
printer->Print("}\n");
} }
void EnumFieldGenerator::GenerateSerializationCode(io::Printer* printer) { void EnumFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
printer->Print(variables_, printer->Print(variables_,
"if ($has_property_check$) {\n" "if ($has_property_check$) {\n"
" output.WriteEnum($number$, field_names[$field_ordinal$], (int) $property_name$, $property_name$);\n" " output.WriteEnum($number$, fieldNames[$field_ordinal$], (long) $property_name$, $property_name$);\n"
"}\n"); "}\n");
} }
void EnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) { void EnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"if ($has_property_check$) {\n" "if ($has_property_check$) {\n"
" size += pb::CodedOutputStream.ComputeEnumSize($number$, (int) $property_name$);\n" " size += pb::CodedOutputStream.ComputeEnumSize($number$, (long) $property_name$);\n"
"}\n"); "}\n");
} }
EnumOneofFieldGenerator::EnumOneofFieldGenerator(const FieldDescriptor* descriptor, EnumOneofFieldGenerator::EnumOneofFieldGenerator(const FieldDescriptor* descriptor,
...@@ -96,30 +82,21 @@ EnumOneofFieldGenerator::~EnumOneofFieldGenerator() { ...@@ -96,30 +82,21 @@ EnumOneofFieldGenerator::~EnumOneofFieldGenerator() {
} }
void EnumOneofFieldGenerator::GenerateParsingCode(io::Printer* printer) { void EnumOneofFieldGenerator::GenerateParsingCode(io::Printer* printer) {
// TODO(jonskeet): What about if we read the default value?
printer->Print( printer->Print(
variables_, variables_,
"object unknown;\n"
"$type_name$ enumValue = $default_value$;\n" "$type_name$ enumValue = $default_value$;\n"
"if(input.ReadEnum(ref enumValue, out unknown)) {\n" "if(input.ReadEnum(ref enumValue)) {\n"
" result.$oneof_name$_ = enumValue;\n" " result.$oneof_name$_ = enumValue;\n"
" result.$oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n" " result.$oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n"
"} else if(unknown is int) {\n"); "}\n");
if (!use_lite_runtime()) {
printer->Print(
variables_,
" if (unknownFields == null) {\n" // First unknown field - create builder now
" unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);\n"
" }\n"
" unknownFields.MergeVarintField($number$, (ulong)(int)unknown);\n");
}
printer->Print("}\n");
} }
void EnumOneofFieldGenerator::GenerateSerializationCode(io::Printer* printer) { void EnumOneofFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"if ($has_property_check$) {\n" "if ($has_property_check$) {\n"
" output.WriteEnum($number$, field_names[$field_ordinal$], (int) $property_name$, $property_name$);\n" " output.WriteEnum($number$, fieldNames[$field_ordinal$], (long) $property_name$, $property_name$);\n"
"}\n"); "}\n");
} }
...@@ -127,7 +104,7 @@ void EnumOneofFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) { ...@@ -127,7 +104,7 @@ void EnumOneofFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"if ($has_property_check$) {\n" "if ($has_property_check$) {\n"
" size += pb::CodedOutputStream.ComputeEnumSize($number$, (int) $property_name$);\n" " size += pb::CodedOutputStream.ComputeEnumSize($number$, (long) $property_name$);\n"
"}\n"); "}\n");
} }
......
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <sstream>
#include <google/protobuf/compiler/code_generator.h>
#include <google/protobuf/compiler/plugin.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/io/zero_copy_stream.h>
#include <google/protobuf/compiler/csharp/csharp_extension.h>
#include <google/protobuf/compiler/csharp/csharp_helpers.h>
#include <google/protobuf/compiler/csharp/csharp_field_base.h>
using google::protobuf::internal::scoped_ptr;
namespace google {
namespace protobuf {
namespace compiler {
namespace csharp {
ExtensionGenerator::ExtensionGenerator(const FieldDescriptor* descriptor)
: FieldGeneratorBase(descriptor, 0) {
if (descriptor_->extension_scope()) {
variables_["scope"] = GetClassName(descriptor_->extension_scope());
} else {
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";
}
}
ExtensionGenerator::~ExtensionGenerator() {
}
void ExtensionGenerator::Generate(io::Printer* printer) {
printer->Print(
"public const int $constant_name$ = $number$;\n",
"constant_name", GetFieldConstantName(descriptor_),
"number", SimpleItoa(descriptor_->number()));
if (use_lite_runtime()) {
// TODO(jtattermusch): include the following check
//if (Descriptor.MappedType == MappedType.Message && Descriptor.MessageType.Options.MessageSetWireFormat)
//{
// throw new ArgumentException(
// "option message_set_wire_format = true; is not supported in Lite runtime extensions.");
//}
printer->Print(
variables_,
"$access_level$ static pb::$generated_extension$<$extends$, $type_name$> $property_name$;\n");
} else if (descriptor_->is_repeated()) {
printer->Print(
variables_,
"$access_level$ static pb::GeneratedExtensionBase<scg::IList<$type_name$>> $property_name$;\n");
} else {
printer->Print(
variables_,
"$access_level$ static pb::GeneratedExtensionBase<$type_name$> $property_name$;\n");
}
}
void ExtensionGenerator::GenerateStaticVariableInitializers(io::Printer* printer) {
if (use_lite_runtime()) {
printer->Print(
variables_,
"$scope$.$property_name$ = \n");
printer->Indent();
printer->Print(
variables_,
"new pb::$generated_extension$<$extends$, $type_name$>(\n");
printer->Indent();
printer->Print(
variables_,
"\"$full_name$\",\n"
"$extends$.DefaultInstance,\n");
if (!descriptor_->is_repeated()) {
std::string default_val;
if (descriptor_->has_default_value()) {
default_val = default_value();
} else {
default_val = is_nullable_type() ? "null" : ("default(" + type_name() + ")");
}
printer->Print("$default_val$,\n", "default_val", default_val);
}
printer->Print(
"$message_val$,\n",
"message_val",
(GetCSharpType(descriptor_->type()) == CSHARPTYPE_MESSAGE) ?
type_name() + ".DefaultInstance" : "null");
printer->Print(
"$enum_val$,\n",
"enum_val",
(GetCSharpType(descriptor_->type()) == CSHARPTYPE_ENUM) ?
"new EnumLiteMap<" + type_name() + ">()" : "null");
printer->Print(
variables_,
"$scope$.$property_name$FieldNumber,\n"
"pbd::FieldType.$capitalized_type_name$");
if (descriptor_->is_repeated()) {
printer->Print(
",\n"
"$is_packed$",
"is_packed", descriptor_->is_packed() ? "true" : "false");
}
printer->Outdent();
printer->Print(");\n");
printer->Outdent();
}
else if (descriptor_->is_repeated())
{
printer->Print(
variables_,
"$scope$.$property_name$ = pb::GeneratedRepeatExtension<$type_name$>.CreateInstance($scope$.Descriptor.Extensions[$index$]);\n");
}
else
{
printer->Print(
variables_,
"$scope$.$property_name$ = pb::GeneratedSingleExtension<$type_name$>.CreateInstance($scope$.Descriptor.Extensions[$index$]);\n");
}
}
void ExtensionGenerator::GenerateExtensionRegistrationCode(io::Printer* printer) {
printer->Print(
variables_,
"registry.Add($scope$.$property_name$);\n");
}
void ExtensionGenerator::WriteHash(io::Printer* printer) {
}
void ExtensionGenerator::WriteEquals(io::Printer* printer) {
}
void ExtensionGenerator::WriteToString(io::Printer* printer) {
}
} // namespace csharp
} // namespace compiler
} // namespace protobuf
} // namespace google
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef GOOGLE_PROTOBUF_COMPILER_CSHARP_EXTENSION_H__
#define GOOGLE_PROTOBUF_COMPILER_CSHARP_EXTENSION_H__
#include <string>
#include <google/protobuf/compiler/code_generator.h>
#include <google/protobuf/compiler/csharp/csharp_field_base.h>
namespace google {
namespace protobuf {
namespace compiler {
namespace csharp {
class ExtensionGenerator : public FieldGeneratorBase {
public:
ExtensionGenerator(const FieldDescriptor* descriptor);
~ExtensionGenerator();
void GenerateStaticVariableInitializers(io::Printer* printer);
void GenerateExtensionRegistrationCode(io::Printer* printer);
void Generate(io::Printer* printer);
virtual void WriteHash(io::Printer* printer);
virtual void WriteEquals(io::Printer* printer);
virtual void WriteToString(io::Printer* printer);
virtual void GenerateMembers(io::Printer* printer) {};
virtual void GenerateBuilderMembers(io::Printer* printer) {};
virtual void GenerateMergingCode(io::Printer* printer) {};
virtual void GenerateBuildingCode(io::Printer* printer) {};
virtual void GenerateParsingCode(io::Printer* printer) {};
virtual void GenerateSerializationCode(io::Printer* printer) {};
virtual void GenerateSerializedSizeCode(io::Printer* printer) {};
private:
std::string scope_;
std::string extends_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ExtensionGenerator);
};
} // namespace csharp
} // namespace compiler
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_CSHARP_EXTENSION_H__
...@@ -263,37 +263,13 @@ bool AllPrintableAscii(const std::string& text) { ...@@ -263,37 +263,13 @@ bool AllPrintableAscii(const std::string& text) {
} }
std::string FieldGeneratorBase::GetStringDefaultValueInternal() { std::string FieldGeneratorBase::GetStringDefaultValueInternal() {
if (!descriptor_->has_default_value()) { // No other default values needed for proto3...
return "\"\""; return "\"\"";
}
if (AllPrintableAscii(descriptor_->default_value_string())) {
// All chars are ASCII and printable. In this case we only
// need to escape quotes and backslashes.
std::string temp = descriptor_->default_value_string();
temp = StringReplace(temp, "\\", "\\\\", true);
temp = StringReplace(temp, "'", "\\'", true);
temp = StringReplace(temp, "\"", "\\\"", true);
return "\"" + temp + "\"";
}
if (use_lite_runtime()) {
return "pb::ByteString.FromBase64(\""
+ StringToBase64(descriptor_->default_value_string())
+ "\").ToStringUtf8()";
}
return "(string) " + GetClassName(descriptor_->containing_type())
+ ".Descriptor.Fields[" + SimpleItoa(descriptor_->index())
+ "].DefaultValue";
} }
std::string FieldGeneratorBase::GetBytesDefaultValueInternal() { std::string FieldGeneratorBase::GetBytesDefaultValueInternal() {
if (!descriptor_->has_default_value()) { // No other default values needed for proto3...
return "pb::ByteString.Empty"; return "pb::ByteString.Empty";
}
if (use_lite_runtime()) {
return "pb::ByteString.FromBase64(\"" + StringToBase64(descriptor_->default_value_string()) + "\")";
}
return "(pb::ByteString) "+ GetClassName(descriptor_->containing_type()) +
".Descriptor.Fields[" + SimpleItoa(descriptor_->index()) + "].DefaultValue";
} }
std::string FieldGeneratorBase::default_value() { std::string FieldGeneratorBase::default_value() {
...@@ -365,11 +341,6 @@ std::string FieldGeneratorBase::number() { ...@@ -365,11 +341,6 @@ std::string FieldGeneratorBase::number() {
return SimpleItoa(descriptor_->number()); return SimpleItoa(descriptor_->number());
} }
std::string FieldGeneratorBase::message_or_group() {
return
(descriptor_->type() == FieldDescriptor::TYPE_GROUP) ? "Group" : "Message";
}
std::string FieldGeneratorBase::capitalized_type_name() { std::string FieldGeneratorBase::capitalized_type_name() {
switch (descriptor_->type()) { switch (descriptor_->type()) {
case FieldDescriptor::TYPE_ENUM: case FieldDescriptor::TYPE_ENUM:
......
...@@ -42,17 +42,13 @@ namespace protobuf { ...@@ -42,17 +42,13 @@ namespace protobuf {
namespace compiler { namespace compiler {
namespace csharp { namespace csharp {
class Writer;
class FieldGeneratorBase : public SourceGeneratorBase { class FieldGeneratorBase : public SourceGeneratorBase {
public: public:
FieldGeneratorBase(const FieldDescriptor* descriptor, int fieldOrdinal); FieldGeneratorBase(const FieldDescriptor* descriptor, int fieldOrdinal);
~FieldGeneratorBase(); ~FieldGeneratorBase();
virtual void GenerateMembers(io::Printer* printer) = 0; virtual void GenerateMembers(io::Printer* printer) = 0;
virtual void GenerateBuilderMembers(io::Printer* printer) = 0;
virtual void GenerateMergingCode(io::Printer* printer) = 0; virtual void GenerateMergingCode(io::Printer* printer) = 0;
virtual void GenerateBuildingCode(io::Printer* printer) = 0;
virtual void GenerateParsingCode(io::Printer* printer) = 0; virtual void GenerateParsingCode(io::Printer* printer) = 0;
virtual void GenerateSerializationCode(io::Printer* printer) = 0; virtual void GenerateSerializationCode(io::Printer* printer) = 0;
virtual void GenerateSerializedSizeCode(io::Printer* printer) = 0; virtual void GenerateSerializedSizeCode(io::Printer* printer) = 0;
...@@ -82,7 +78,6 @@ class FieldGeneratorBase : public SourceGeneratorBase { ...@@ -82,7 +78,6 @@ class FieldGeneratorBase : public SourceGeneratorBase {
bool is_nullable_type(); bool is_nullable_type();
std::string default_value(); std::string default_value();
std::string number(); std::string number();
std::string message_or_group();
std::string capitalized_type_name(); std::string capitalized_type_name();
std::string field_ordinal(); std::string field_ordinal();
......
...@@ -43,7 +43,6 @@ namespace protobuf { ...@@ -43,7 +43,6 @@ namespace protobuf {
namespace compiler { namespace compiler {
namespace csharp { namespace csharp {
class Writer;
class FieldGeneratorBase; class FieldGeneratorBase;
class MessageGenerator : public SourceGeneratorBase { class MessageGenerator : public SourceGeneratorBase {
...@@ -53,7 +52,6 @@ class MessageGenerator : public SourceGeneratorBase { ...@@ -53,7 +52,6 @@ class MessageGenerator : public SourceGeneratorBase {
void GenerateStaticVariables(io::Printer* printer); void GenerateStaticVariables(io::Printer* printer);
void GenerateStaticVariableInitializers(io::Printer* printer); void GenerateStaticVariableInitializers(io::Printer* printer);
void GenerateExtensionRegistrationCode(io::Printer* printer);
void Generate(io::Printer* printer); void Generate(io::Printer* printer);
private: private:
...@@ -61,17 +59,8 @@ class MessageGenerator : public SourceGeneratorBase { ...@@ -61,17 +59,8 @@ class MessageGenerator : public SourceGeneratorBase {
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(io::Printer* printer);
void GenerateMessageSerializationMethods(io::Printer* printer); void GenerateMessageSerializationMethods(io::Printer* printer);
void GenerateSerializeOneField(io::Printer* printer, void GenerateMergingMethods(io::Printer* printer);
const FieldDescriptor* fieldDescriptor);
void GenerateSerializeOneExtensionRange(
io::Printer* printer, const Descriptor::ExtensionRange* extendsionRange);
void GenerateParseFromMethods(io::Printer* printer);
void GenerateBuilder(io::Printer* printer);
void GenerateCommonBuilderMethods(io::Printer* printer);
void GenerateBuilderParsingMethods(io::Printer* printer);
void GenerateIsInitialized(io::Printer* printer);
int GetFieldOrdinal(const FieldDescriptor* descriptor); int GetFieldOrdinal(const FieldDescriptor* descriptor);
FieldGeneratorBase* CreateFieldGeneratorInternal( FieldGeneratorBase* CreateFieldGeneratorInternal(
...@@ -95,4 +84,3 @@ class MessageGenerator : public SourceGeneratorBase { ...@@ -95,4 +84,3 @@ class MessageGenerator : public SourceGeneratorBase {
} // namespace google } // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_CSHARP_MESSAGE_H__ #endif // GOOGLE_PROTOBUF_COMPILER_CSHARP_MESSAGE_H__
...@@ -41,17 +41,13 @@ namespace protobuf { ...@@ -41,17 +41,13 @@ namespace protobuf {
namespace compiler { namespace compiler {
namespace csharp { namespace csharp {
class Writer;
class MessageFieldGenerator : public FieldGeneratorBase { class MessageFieldGenerator : public FieldGeneratorBase {
public: public:
MessageFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); MessageFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
~MessageFieldGenerator(); ~MessageFieldGenerator();
virtual void GenerateMembers(io::Printer* printer); virtual void GenerateMembers(io::Printer* printer);
virtual void GenerateBuilderMembers(io::Printer* printer);
virtual void GenerateMergingCode(io::Printer* printer); virtual void GenerateMergingCode(io::Printer* printer);
virtual void GenerateBuildingCode(io::Printer* printer);
virtual void GenerateParsingCode(io::Printer* printer); virtual void GenerateParsingCode(io::Printer* printer);
virtual void GenerateSerializationCode(io::Printer* printer); virtual void GenerateSerializationCode(io::Printer* printer);
virtual void GenerateSerializedSizeCode(io::Printer* printer); virtual void GenerateSerializedSizeCode(io::Printer* printer);
...@@ -70,8 +66,6 @@ class MessageOneofFieldGenerator : public MessageFieldGenerator { ...@@ -70,8 +66,6 @@ class MessageOneofFieldGenerator : public MessageFieldGenerator {
~MessageOneofFieldGenerator(); ~MessageOneofFieldGenerator();
virtual void GenerateMembers(io::Printer* printer); virtual void GenerateMembers(io::Printer* printer);
virtual void GenerateBuilderMembers(io::Printer* printer);
virtual void WriteEquals(io::Printer* printer);
virtual void WriteToString(io::Printer* printer); virtual void WriteToString(io::Printer* printer);
virtual void GenerateParsingCode(io::Printer* printer); virtual void GenerateParsingCode(io::Printer* printer);
......
...@@ -49,82 +49,37 @@ namespace csharp { ...@@ -49,82 +49,37 @@ namespace csharp {
PrimitiveFieldGenerator::PrimitiveFieldGenerator( PrimitiveFieldGenerator::PrimitiveFieldGenerator(
const FieldDescriptor* descriptor, int fieldOrdinal) const FieldDescriptor* descriptor, int fieldOrdinal)
: FieldGeneratorBase(descriptor, fieldOrdinal) { : FieldGeneratorBase(descriptor, fieldOrdinal) {
// TODO(jonskeet): Make this cleaner...
is_value_type = descriptor->type() != FieldDescriptor::TYPE_STRING
&& descriptor->type() != FieldDescriptor::TYPE_BYTES;
} }
PrimitiveFieldGenerator::~PrimitiveFieldGenerator() { PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {
} }
void PrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) { void PrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) {
if (SupportFieldPresence(descriptor_->file())) { // TODO(jonskeet): Work out whether we want to prevent the fields from ever being
printer->Print(variables_, "private bool has$property_name$;\n"); // null, or whether we just handle it, in the cases of bytes and string.
} // (Basically, should null-handling code be in the getter or the setter?)
printer->Print( printer->Print(
variables_, variables_,
"private $type_name$ $name_def_message$;\n"); "private $type_name$ $name_def_message$;\n");
AddDeprecatedFlag(printer); AddDeprecatedFlag(printer);
if (SupportFieldPresence(descriptor_->file())) {
printer->Print(
variables_,
"public bool Has$property_name$ {\n"
" get { return has$property_name$; }\n"
"}\n");
}
AddPublicMemberAttributes(printer);
printer->Print( printer->Print(
variables_, variables_,
"public $type_name$ $property_name$ {\n" "public $type_name$ $property_name$ {\n"
" get { return $name$_; }\n" " get { return $name$_; }\n");
"}\n"); if (is_value_type) {
}
void PrimitiveFieldGenerator::GenerateBuilderMembers(io::Printer* printer) {
AddDeprecatedFlag(printer);
if (SupportFieldPresence(descriptor_->file())) {
printer->Print(
variables_,
"public bool Has$property_name$ {\n"
" get { return result.has$property_name$; }\n"
"}\n");
}
AddPublicMemberAttributes(printer);
printer->Print(
variables_,
"public $type_name$ $property_name$ {\n"
" get { return result.$property_name$; }\n"
" set { Set$property_name$(value); }\n"
"}\n");
AddPublicMemberAttributes(printer);
printer->Print(
variables_,
"public Builder Set$property_name$($type_name$ value) {\n");
AddNullCheck(printer);
printer->Print(" PrepareBuilder();\n");
if (SupportFieldPresence(descriptor_->file())) {
printer->Print( printer->Print(
variables_, variables_,
" result.has$property_name$ = true;\n"); " set { $name$_ = value; }\n");
}
printer->Print( } else {
variables_,
" result.$name$_ = value;\n"
" return this;\n"
"}\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public Builder Clear$property_name$() {\n"
" PrepareBuilder();\n");
if (SupportFieldPresence(descriptor_->file())) {
printer->Print( printer->Print(
variables_, variables_,
" result.has$property_name$ = false;\n"); " set { $name$_ = value ?? $default_value$; }\n");
} }
printer->Print( printer->Print("}\n\n");
variables_,
" result.$name$_ = $default_value$;\n"
" return this;\n"
"}\n");
} }
void PrimitiveFieldGenerator::GenerateMergingCode(io::Printer* printer) { void PrimitiveFieldGenerator::GenerateMergingCode(io::Printer* printer) {
...@@ -135,27 +90,17 @@ void PrimitiveFieldGenerator::GenerateMergingCode(io::Printer* printer) { ...@@ -135,27 +90,17 @@ void PrimitiveFieldGenerator::GenerateMergingCode(io::Printer* printer) {
"}\n"); "}\n");
} }
void PrimitiveFieldGenerator::GenerateBuildingCode(io::Printer* printer) {
// Nothing to do here for primitive types
}
void PrimitiveFieldGenerator::GenerateParsingCode(io::Printer* printer) { void PrimitiveFieldGenerator::GenerateParsingCode(io::Printer* printer) {
if (SupportFieldPresence(descriptor_->file())) { printer->Print(
printer->Print( variables_,
variables_, "input.Read$capitalized_type_name$(ref result.$name$_);\n");
"result.has$property_name$ = input.Read$capitalized_type_name$(ref result.$name$_);\n");
} else {
printer->Print(
variables_,
"input.Read$capitalized_type_name$(ref result.$name$_);\n");
}
} }
void PrimitiveFieldGenerator::GenerateSerializationCode(io::Printer* printer) { void PrimitiveFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"if ($has_property_check$) {\n" "if ($has_property_check$) {\n"
" output.Write$capitalized_type_name$($number$, field_names[$field_ordinal$], $property_name$);\n" " output.Write$capitalized_type_name$($number$, fieldNames[$field_ordinal$], $property_name$);\n"
"}\n"); "}\n");
} }
...@@ -171,24 +116,18 @@ void PrimitiveFieldGenerator::WriteHash(io::Printer* printer) { ...@@ -171,24 +116,18 @@ void PrimitiveFieldGenerator::WriteHash(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"if ($has_property_check$) {\n" "if ($has_property_check$) {\n"
" hash ^= $name$_.GetHashCode();\n" " hash ^= $property_name$.GetHashCode();\n"
"}\n"); "}\n");
} }
void PrimitiveFieldGenerator::WriteEquals(io::Printer* printer) { void PrimitiveFieldGenerator::WriteEquals(io::Printer* printer) {
if (SupportFieldPresence(descriptor_->file())) { printer->Print(
printer->Print( variables_,
variables_, "if ($property_name$ != other.$property_name$) return false;\n");
"if (has$property_name$ != other.has$property_name$ || (has$property_name$ && !$name$_.Equals(other.$name$_))) return false;\n");
} else {
printer->Print(
variables_,
"if (!$name$_.Equals(other.$name$_)) return false;\n");
}
} }
void PrimitiveFieldGenerator::WriteToString(io::Printer* printer) { void PrimitiveFieldGenerator::WriteToString(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"PrintField(\"$descriptor_name$\", $has_property_check$, $name$_, writer);\n"); "PrintField(\"$descriptor_name$\", $has_property_check$, $property_name$, writer);\n");
} }
PrimitiveOneofFieldGenerator::PrimitiveOneofFieldGenerator( PrimitiveOneofFieldGenerator::PrimitiveOneofFieldGenerator(
...@@ -201,79 +140,43 @@ PrimitiveOneofFieldGenerator::~PrimitiveOneofFieldGenerator() { ...@@ -201,79 +140,43 @@ PrimitiveOneofFieldGenerator::~PrimitiveOneofFieldGenerator() {
} }
void PrimitiveOneofFieldGenerator::GenerateMembers(io::Printer* printer) { void PrimitiveOneofFieldGenerator::GenerateMembers(io::Printer* printer) {
// TODO(jonskeet): What should foo.OneofIntField = 0; do? Clear the oneof?
// Currently foo.OneOfStringField = null will clear the oneof, but foo.OneOfStringField = "" won't. Ick.
AddDeprecatedFlag(printer); AddDeprecatedFlag(printer);
if (SupportFieldPresence(descriptor_->file())) {
printer->Print(
variables_,
"public bool Has$property_name$ {\n"
" get { return $has_property_check$; }\n"
"}\n");
}
AddPublicMemberAttributes(printer);
printer->Print( printer->Print(
variables_, variables_,
"public $type_name$ $property_name$ {\n" "public $type_name$ $property_name$ {\n"
" get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : $default_value$; }\n" " get { return $has_property_check$ ? ($type_name$) $oneof_name$_ : $default_value$; }\n"
"}\n"); " set {");
} if (is_value_type) {
printer->Print(
void PrimitiveOneofFieldGenerator::GenerateBuilderMembers(io::Printer* printer) { variables_,
AddDeprecatedFlag(printer); " $oneof_name$_ = value;\n"
if (SupportFieldPresence(descriptor_->file())) { " $oneof_name$Case_ = $oneof_property_name$Case.$property_name$;\n");
} else {
printer->Print(
variables_,
" $oneof_name$_ = value ?? $default_value$;\n"
" $oneof_name$Case_ = value == null ? $oneof_property_name$Case.None : $oneof_property_name$Case.$property_name$;\n");
}
printer->Print( printer->Print(
variables_, " }\n"
"public bool Has$property_name$ {\n"
" get { return result.$has_property_check$; }\n"
"}\n"); "}\n");
}
AddPublicMemberAttributes(printer);
printer->Print(
variables_,
"public $type_name$ $property_name$ {\n"
" get { return result.$has_property_check$ ? ($type_name$) result.$oneof_name$_ : $default_value$; }\n"
" set { Set$property_name$(value); }\n"
"}\n");
AddPublicMemberAttributes(printer);
printer->Print(
variables_,
"public Builder Set$property_name$($type_name$ value) {\n");
AddNullCheck(printer);
printer->Print(
variables_,
" PrepareBuilder();\n"
" result.$oneof_name$_ = value;\n"
" result.$oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n"
" return this;\n"
"}\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public Builder Clear$property_name$() {\n"
" PrepareBuilder();\n"
" if (result.$has_property_check$) {\n"
" result.$oneof_name$Case_ = $oneof_property_name$OneofCase.None;\n"
" }\n"
" return this;\n"
"}\n");
} }
void PrimitiveOneofFieldGenerator::WriteEquals(io::Printer* printer) {
printer->Print(
variables_,
"if (!$property_name$.Equals(other.$property_name$)) return false;\n");
}
void PrimitiveOneofFieldGenerator::WriteToString(io::Printer* printer) { void PrimitiveOneofFieldGenerator::WriteToString(io::Printer* printer) {
printer->Print(variables_, printer->Print(variables_,
"PrintField(\"$descriptor_name$\", $has_property_check$, $oneof_name$_, writer);\n"); "PrintField(\"$descriptor_name$\", $has_property_check$, $oneof_name$_, writer);\n");
} }
void PrimitiveOneofFieldGenerator::GenerateParsingCode(io::Printer* printer) { void PrimitiveOneofFieldGenerator::GenerateParsingCode(io::Printer* printer) {
// TODO(jonskeet): What if the value we read is the default value for the type?
printer->Print( printer->Print(
variables_, variables_,
"$type_name$ value = $default_value$;\n" "$type_name$ value = $default_value$;\n"
"if (input.Read$capitalized_type_name$(ref value)) {\n" "if (input.Read$capitalized_type_name$(ref value)) {\n"
" result.$oneof_name$_ = value;\n" " $oneof_name$_ = value;\n"
" result.$oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n" " $oneof_name$Case_ = $oneof_property_name$OneofCase.$property_name$;\n"
"}\n"); "}\n");
} }
......
...@@ -41,17 +41,13 @@ namespace protobuf { ...@@ -41,17 +41,13 @@ namespace protobuf {
namespace compiler { namespace compiler {
namespace csharp { namespace csharp {
class Writer;
class PrimitiveFieldGenerator : public FieldGeneratorBase { class PrimitiveFieldGenerator : public FieldGeneratorBase {
public: public:
PrimitiveFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); PrimitiveFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
~PrimitiveFieldGenerator(); ~PrimitiveFieldGenerator();
virtual void GenerateMembers(io::Printer* printer); virtual void GenerateMembers(io::Printer* printer);
virtual void GenerateBuilderMembers(io::Printer* printer);
virtual void GenerateMergingCode(io::Printer* printer); virtual void GenerateMergingCode(io::Printer* printer);
virtual void GenerateBuildingCode(io::Printer* printer);
virtual void GenerateParsingCode(io::Printer* printer); virtual void GenerateParsingCode(io::Printer* printer);
virtual void GenerateSerializationCode(io::Printer* printer); virtual void GenerateSerializationCode(io::Printer* printer);
virtual void GenerateSerializedSizeCode(io::Printer* printer); virtual void GenerateSerializedSizeCode(io::Printer* printer);
...@@ -60,6 +56,9 @@ class PrimitiveFieldGenerator : public FieldGeneratorBase { ...@@ -60,6 +56,9 @@ class PrimitiveFieldGenerator : public FieldGeneratorBase {
virtual void WriteEquals(io::Printer* printer); virtual void WriteEquals(io::Printer* printer);
virtual void WriteToString(io::Printer* printer); virtual void WriteToString(io::Printer* printer);
protected:
bool is_value_type;
private: private:
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PrimitiveFieldGenerator); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(PrimitiveFieldGenerator);
}; };
...@@ -70,8 +69,6 @@ class PrimitiveOneofFieldGenerator : public PrimitiveFieldGenerator { ...@@ -70,8 +69,6 @@ class PrimitiveOneofFieldGenerator : public PrimitiveFieldGenerator {
~PrimitiveOneofFieldGenerator(); ~PrimitiveOneofFieldGenerator();
virtual void GenerateMembers(io::Printer* printer); virtual void GenerateMembers(io::Printer* printer);
virtual void GenerateBuilderMembers(io::Printer* printer);
virtual void WriteEquals(io::Printer* printer);
virtual void WriteToString(io::Printer* printer); virtual void WriteToString(io::Printer* printer);
virtual void GenerateParsingCode(io::Printer* printer); virtual void GenerateParsingCode(io::Printer* printer);
......
...@@ -56,141 +56,46 @@ RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() { ...@@ -56,141 +56,46 @@ RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {
} }
void RepeatedEnumFieldGenerator::GenerateMembers(io::Printer* printer) { void RepeatedEnumFieldGenerator::GenerateMembers(io::Printer* printer) {
if (descriptor_->is_packed() && optimize_speed()) {
printer->Print(variables_, "private int $name$MemoizedSerializedSize;\n");
}
printer->Print(variables_,
"private pbc::PopsicleList<$type_name$> $name$_ = new pbc::PopsicleList<$type_name$>();\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public scg::IList<$type_name$> $property_name$List {\n"
" get { return pbc::Lists.AsReadOnly($name$_); }\n"
"}\n");
// TODO(jonskeet): Redundant API calls? Possibly - include for portability though. Maybe create an option.
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public int $property_name$Count {\n"
" get { return $name$_.Count; }\n"
"}\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public $type_name$ Get$property_name$(int index) {\n"
" return $name$_[index];\n"
"}\n");
}
void RepeatedEnumFieldGenerator::GenerateBuilderMembers(io::Printer* printer) {
// 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.
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public pbc::IPopsicleList<$type_name$> $property_name$List {\n"
" get { return PrepareBuilder().$name$_; }\n"
"}\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public int $property_name$Count {\n"
" get { return result.$property_name$Count; }\n"
"}\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public $type_name$ Get$property_name$(int index) {\n"
" return result.Get$property_name$(index);\n"
"}\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public Builder Set$property_name$(int index, $type_name$ value) {\n");
printer->Print(
variables_,
" PrepareBuilder();\n"
" result.$name$_[index] = value;\n"
" return this;\n"
"}\n");
AddDeprecatedFlag(printer);
printer->Print(variables_, printer->Print(variables_,
"public Builder Add$property_name$($type_name$ value) {\n"); "private readonly pbc::RepeatedField<$type_name$> $name$_ = new pbc::RepeatedField<$type_name$>();\n");
printer->Print(
variables_,
" PrepareBuilder();\n"
" result.$name$_.Add(value);\n"
" return this;\n"
"}\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public Builder AddRange$property_name$(scg::IEnumerable<$type_name$> values) {\n"
" PrepareBuilder();\n"
" result.$name$_.Add(values);\n"
" return this;\n"
"}\n");
AddDeprecatedFlag(printer); AddDeprecatedFlag(printer);
printer->Print( printer->Print(
variables_, variables_,
"public Builder Clear$property_name$() {\n" "public pbc::RepeatedField<$type_name$> $property_name$ {\n"
" PrepareBuilder();\n" " get { return $name$_; }\n"
" result.$name$_.Clear();\n"
" return this;\n"
"}\n"); "}\n");
} }
void RepeatedEnumFieldGenerator::GenerateMergingCode(io::Printer* printer) { void RepeatedEnumFieldGenerator::GenerateMergingCode(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"if (other.$name$_.Count != 0) {\n" "$name$_.Add(other.$name$_);\n");
" result.$name$_.Add(other.$name$_);\n"
"}\n");
}
void RepeatedEnumFieldGenerator::GenerateBuildingCode(io::Printer* printer) {
printer->Print(variables_, "$name$_.MakeReadOnly();\n");
} }
void RepeatedEnumFieldGenerator::GenerateParsingCode(io::Printer* printer) { void RepeatedEnumFieldGenerator::GenerateParsingCode(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"scg::ICollection<object> unknownItems;\n" "input.ReadEnumArray<$type_name$>(tag, fieldName, result.$name$_);\n");
"input.ReadEnumArray<$type_name$>(tag, field_name, result.$name$_, out unknownItems);\n");
if (!use_lite_runtime()) {
printer->Print(
variables_,
"if (unknownItems != null) {\n"
" if (unknownFields == null) {\n"
" unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);\n"
" }\n"
" foreach (object rawValue in unknownItems)\n"
" if (rawValue is int)\n"
" unknownFields.MergeVarintField($number$, (ulong)(int)rawValue);\n"
"}\n");
}
} }
void RepeatedEnumFieldGenerator::GenerateSerializationCode(io::Printer* printer) { void RepeatedEnumFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
printer->Print(variables_, "if ($name$_.Count > 0) {\n"); // TODO(jonskeet): Originally, this checked for Count > 0 first.
printer->Indent(); // The Write* call should make that cheap though - no need to generate it every time.
if (descriptor_->is_packed()) { if (descriptor_->is_packed()) {
printer->Print( printer->Print(
variables_, variables_,
"output.WritePackedEnumArray($number$, field_names[$field_ordinal$], $name$MemoizedSerializedSize, $name$_);\n"); "output.WritePackedEnumArray($number$, fieldNames[$field_ordinal$], $name$_);\n");
} else { } else {
printer->Print(variables_, printer->Print(variables_,
"output.WriteEnumArray($number$, field_names[$field_ordinal$], $name$_);\n"); "output.WriteEnumArray($number$, fieldNames[$field_ordinal$], $name$_);\n");
} }
printer->Outdent();
printer->Print("}\n"); printer->Print("}\n");
} }
void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) { void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
printer->Print("{\n"); printer->Print("{\n");
printer->Indent(); printer->Indent();
// TODO(jonskeet): Move all this code into CodedOutputStream? It's a lot to repeat everywhere...
printer->Print( printer->Print(
variables_, variables_,
"int dataSize = 0;\n" "int dataSize = 0;\n"
...@@ -199,7 +104,7 @@ void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer ...@@ -199,7 +104,7 @@ void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer
printer->Print( printer->Print(
variables_, variables_,
"foreach ($type_name$ element in $name$_) {\n" "foreach ($type_name$ element in $name$_) {\n"
" dataSize += pb::CodedOutputStream.ComputeEnumSizeNoTag((int) element);\n" " dataSize += pb::CodedOutputStream.ComputeEnumSizeNoTag((long) element);\n"
"}\n" "}\n"
"size += dataSize;\n"); "size += dataSize;\n");
int tagSize = internal::WireFormat::TagSize(descriptor_->number(), descriptor_->type()); int tagSize = internal::WireFormat::TagSize(descriptor_->number(), descriptor_->type());
...@@ -215,11 +120,6 @@ void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer ...@@ -215,11 +120,6 @@ void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer
} }
printer->Outdent(); printer->Outdent();
printer->Print("}\n"); printer->Print("}\n");
// cache the data size for packed fields.
if (descriptor_->is_packed()) {
printer->Print(variables_,
"$name$MemoizedSerializedSize = dataSize;\n");
}
printer->Outdent(); printer->Outdent();
printer->Print("}\n"); printer->Print("}\n");
} }
...@@ -227,16 +127,15 @@ void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer ...@@ -227,16 +127,15 @@ void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer
void RepeatedEnumFieldGenerator::WriteHash(io::Printer* printer) { void RepeatedEnumFieldGenerator::WriteHash(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"foreach($type_name$ i in $name$_)\n" "foreach($type_name$ i in $name$_) {\n"
" hash ^= i.GetHashCode();\n"); " hash ^= i.GetHashCode();\n"
"}\n");
} }
void RepeatedEnumFieldGenerator::WriteEquals(io::Printer* printer) { void RepeatedEnumFieldGenerator::WriteEquals(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"if($name$_.Count != other.$name$_.Count) return false;\n" "if(!$name$_.Equals(other.$name$)) return false;\n");
"for(int ix=0; ix < $name$_.Count; ix++)\n"
" if(!$name$_[ix].Equals(other.$name$_[ix])) return false;\n");
} }
void RepeatedEnumFieldGenerator::WriteToString(io::Printer* printer) { void RepeatedEnumFieldGenerator::WriteToString(io::Printer* printer) {
......
...@@ -41,17 +41,15 @@ namespace protobuf { ...@@ -41,17 +41,15 @@ namespace protobuf {
namespace compiler { namespace compiler {
namespace csharp { namespace csharp {
class Writer; // TODO(jonskeet): Refactor repeated field support; all the implementations are *really* similar. We
// should probably have a RepeatedFieldGeneratorBase.
class RepeatedEnumFieldGenerator : public FieldGeneratorBase { class RepeatedEnumFieldGenerator : public FieldGeneratorBase {
public: public:
RepeatedEnumFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); RepeatedEnumFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
~RepeatedEnumFieldGenerator(); ~RepeatedEnumFieldGenerator();
virtual void GenerateMembers(io::Printer* printer); virtual void GenerateMembers(io::Printer* printer);
virtual void GenerateBuilderMembers(io::Printer* printer);
virtual void GenerateMergingCode(io::Printer* printer); virtual void GenerateMergingCode(io::Printer* printer);
virtual void GenerateBuildingCode(io::Printer* printer);
virtual void GenerateParsingCode(io::Printer* printer); virtual void GenerateParsingCode(io::Printer* printer);
virtual void GenerateSerializationCode(io::Printer* printer); virtual void GenerateSerializationCode(io::Printer* printer);
virtual void GenerateSerializedSizeCode(io::Printer* printer); virtual void GenerateSerializedSizeCode(io::Printer* printer);
......
...@@ -48,7 +48,6 @@ namespace csharp { ...@@ -48,7 +48,6 @@ 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() {
...@@ -58,11 +57,11 @@ RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() { ...@@ -58,11 +57,11 @@ RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {
void RepeatedMessageFieldGenerator::GenerateMembers(io::Printer* printer) { void RepeatedMessageFieldGenerator::GenerateMembers(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"private pbc::PopsicleList<$type_name$> $name$_ = new pbc::PopsicleList<$type_name$>();\n"); "private readonly pbc::RepeatedField<$type_name$> $name$_ = new pbc::RepeatedField<$type_name$>();\n");
AddDeprecatedFlag(printer); AddDeprecatedFlag(printer);
printer->Print( printer->Print(
variables_, variables_,
"public scg::IList<$type_name$> $property_name$List {\n" "public pbc::RepeatedField<$type_name$> $property_name$ {\n"
" get { return $name$_; }\n" " get { return $name$_; }\n"
"}\n"); "}\n");
...@@ -82,137 +81,46 @@ void RepeatedMessageFieldGenerator::GenerateMembers(io::Printer* printer) { ...@@ -82,137 +81,46 @@ void RepeatedMessageFieldGenerator::GenerateMembers(io::Printer* printer) {
"}\n"); "}\n");
} }
void RepeatedMessageFieldGenerator::GenerateBuilderMembers(io::Printer* printer) {
// 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.
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public pbc::IPopsicleList<$type_name$> $property_name$List {\n"
" get { return PrepareBuilder().$name$_; }\n"
"}\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public int $property_name$Count {\n"
" get { return result.$property_name$Count; }\n"
"}\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public $type_name$ Get$property_name$(int index) {\n"
" return result.Get$property_name$(index);\n"
"}\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"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)
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public Builder Set$property_name$(int index, $type_name$.Builder builderForValue) {\n");
AddNullCheck(printer, "builderForValue");
printer->Print(
variables_,
" PrepareBuilder();\n"
" result.$name$_[index] = builderForValue.Build();\n"
" return this;\n"
"}\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public Builder Add$property_name$($type_name$ value) {\n");
AddNullCheck(printer);
printer->Print(
variables_,
" PrepareBuilder();\n"
" result.$name$_.Add(value);\n"
" return this;\n"
"}\n");
// Extra overload for builder (just on messages)
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public Builder Add$property_name$($type_name$.Builder builderForValue) {\n");
AddNullCheck(printer, "builderForValue");
printer->Print(
variables_,
" PrepareBuilder();\n"
" result.$name$_.Add(builderForValue.Build());\n"
" return this;\n"
"}\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"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 RepeatedMessageFieldGenerator::GenerateMergingCode(io::Printer* printer) { void RepeatedMessageFieldGenerator::GenerateMergingCode(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"if (other.$name$_.Count != 0) {\n" "$name$_.Add(other.$name$_);\n");
" result.$name$_.Add(other.$name$_);\n"
"}\n");
}
void RepeatedMessageFieldGenerator::GenerateBuildingCode(io::Printer* printer) {
printer->Print(variables_, "$name$_.MakeReadOnly();\n");
} }
void RepeatedMessageFieldGenerator::GenerateParsingCode(io::Printer* printer) { void RepeatedMessageFieldGenerator::GenerateParsingCode(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"input.Read$message_or_group$Array(tag, field_name, result.$name$_, $type_name$.DefaultInstance, extensionRegistry);\n"); "input.ReadMessageArray(tag, fieldName, $name$_);\n");
} }
void RepeatedMessageFieldGenerator::GenerateSerializationCode(io::Printer* printer) { void RepeatedMessageFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
// TODO(jonskeet): Originally, this checked for Count > 0 first.
// The Write* call should make that cheap though - no need to generate it every time.
printer->Print( printer->Print(
variables_, variables_,
"if ($name$_.Count > 0) {\n" "output.WriteMessageArray($number$, fieldNames[$field_ordinal$], $name$_);\n");
" output.Write$message_or_group$Array($number$, field_names[$field_ordinal$], $name$_);\n"
"}\n");
} }
void RepeatedMessageFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) { void RepeatedMessageFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
// TODO(jonskeet): Put this into CodedOutputStream.
printer->Print( printer->Print(
variables_, variables_,
"foreach ($type_name$ element in $property_name$List) {\n" "foreach ($type_name$ element in $property_name$) {\n"
" size += pb::CodedOutputStream.Compute$message_or_group$Size($number$, element);\n" " size += pb::CodedOutputStream.ComputeMessageSize($number$, element);\n"
"}\n"); "}\n");
} }
void RepeatedMessageFieldGenerator::WriteHash(io::Printer* printer) { void RepeatedMessageFieldGenerator::WriteHash(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"foreach($type_name$ i in $name$_)\n" "foreach($type_name$ i in $name$_) {\n"
" hash ^= i.GetHashCode();\n"); " hash ^= i.GetHashCode();\n"
"}\n");
} }
void RepeatedMessageFieldGenerator::WriteEquals(io::Printer* printer) { void RepeatedMessageFieldGenerator::WriteEquals(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"if($name$_.Count != other.$name$_.Count) return false;\n" "if(!$name$_.Equals(other.$name$)) return false;\n");
"for(int ix=0; ix < $name$_.Count; ix++)\n"
" if(!$name$_[ix].Equals(other.$name$_[ix])) return false;\n");
} }
void RepeatedMessageFieldGenerator::WriteToString(io::Printer* printer) { void RepeatedMessageFieldGenerator::WriteToString(io::Printer* printer) {
variables_["field_name"] = GetFieldName(descriptor_); variables_["field_name"] = GetFieldName(descriptor_);
......
...@@ -41,17 +41,13 @@ namespace protobuf { ...@@ -41,17 +41,13 @@ namespace protobuf {
namespace compiler { namespace compiler {
namespace csharp { namespace csharp {
class Writer;
class RepeatedMessageFieldGenerator : public FieldGeneratorBase { class RepeatedMessageFieldGenerator : public FieldGeneratorBase {
public: public:
RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
~RepeatedMessageFieldGenerator(); ~RepeatedMessageFieldGenerator();
virtual void GenerateMembers(io::Printer* printer); virtual void GenerateMembers(io::Printer* printer);
virtual void GenerateBuilderMembers(io::Printer* printer);
virtual void GenerateMergingCode(io::Printer* printer); virtual void GenerateMergingCode(io::Printer* printer);
virtual void GenerateBuildingCode(io::Printer* printer);
virtual void GenerateParsingCode(io::Printer* printer); virtual void GenerateParsingCode(io::Printer* printer);
virtual void GenerateSerializationCode(io::Printer* printer); virtual void GenerateSerializationCode(io::Printer* printer);
virtual void GenerateSerializedSizeCode(io::Printer* printer); virtual void GenerateSerializedSizeCode(io::Printer* printer);
......
...@@ -56,129 +56,43 @@ RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() { ...@@ -56,129 +56,43 @@ RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {
} }
void RepeatedPrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) { void RepeatedPrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) {
if (descriptor_->is_packed() && optimize_speed()) {
printer->Print(variables_, "private int $name$MemoizedSerializedSize;\n");
}
printer->Print(variables_, printer->Print(variables_,
"private pbc::PopsicleList<$type_name$> $name$_ = new pbc::PopsicleList<$type_name$>();\n"); "private readonly pbc::RepeatedField<$type_name$> $name$_ = new pbc::RepeatedField<$type_name$>();\n");
AddPublicMemberAttributes(printer);
printer->Print(
variables_,
"public scg::IList<$type_name$> $property_name$List {\n"
" get { return pbc::Lists.AsReadOnly($name$_); }\n"
"}\n");
// TODO(jonskeet): Redundant API calls? Possibly - include for portability though. Maybe create an option.
AddDeprecatedFlag(printer); AddDeprecatedFlag(printer);
printer->Print( printer->Print(
variables_, variables_,
"public int $property_name$Count {\n" "public pbc::RepeatedField<$type_name$> $property_name$ {\n"
" get { return $name$_.Count; }\n" " get { return $name$_; }\n"
"}\n");
AddPublicMemberAttributes(printer);
printer->Print(
variables_,
"public $type_name$ Get$property_name$(int index) {\n"
" return $name$_[index];\n"
"}\n");
}
void RepeatedPrimitiveFieldGenerator::GenerateBuilderMembers(io::Printer* printer) {
// 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.
AddPublicMemberAttributes(printer);
printer->Print(
variables_,
"public pbc::IPopsicleList<$type_name$> $property_name$List {\n"
" get { return PrepareBuilder().$name$_; }\n"
"}\n");
AddDeprecatedFlag(printer);
printer->Print(
variables_,
"public int $property_name$Count {\n"
" get { return result.$property_name$Count; }\n"
"}\n");
AddPublicMemberAttributes(printer);
printer->Print(
variables_,
"public $type_name$ Get$property_name$(int index) {\n"
" return result.Get$property_name$(index);\n"
"}\n");
AddPublicMemberAttributes(printer);
printer->Print(
variables_,
"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");
AddPublicMemberAttributes(printer);
printer->Print(
variables_,
"public Builder Add$property_name$($type_name$ value) {\n");
AddNullCheck(printer);
printer->Print(
variables_,
" PrepareBuilder();\n"
" result.$name$_.Add(value);\n"
" return this;\n"
"}\n");
AddPublicMemberAttributes(printer);
printer->Print(
variables_,
"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"); "}\n");
} }
void RepeatedPrimitiveFieldGenerator::GenerateMergingCode(io::Printer* printer) { void RepeatedPrimitiveFieldGenerator::GenerateMergingCode(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"if (other.$name$_.Count != 0) {\n" "$name$_.Add(other.$name$_);\n");
" result.$name$_.Add(other.$name$_);\n"
"}\n");
}
void RepeatedPrimitiveFieldGenerator::GenerateBuildingCode(io::Printer* printer) {
printer->Print(variables_, "$name$_.MakeReadOnly();\n");
} }
void RepeatedPrimitiveFieldGenerator::GenerateParsingCode(io::Printer* printer) { void RepeatedPrimitiveFieldGenerator::GenerateParsingCode(io::Printer* printer) {
printer->Print(variables_, printer->Print(variables_,
"input.Read$capitalized_type_name$Array(tag, field_name, result.$name$_);\n"); "input.Read$capitalized_type_name$Array(tag, fieldName, $name$_);\n");
} }
void RepeatedPrimitiveFieldGenerator::GenerateSerializationCode( void RepeatedPrimitiveFieldGenerator::GenerateSerializationCode(
io::Printer* printer) { io::Printer* printer) {
printer->Print(variables_, "if ($name$_.Count > 0) {\n"); // TODO(jonskeet): Originally, this checked for Count > 0 first.
printer->Indent(); // The Write* call should make that cheap though - no need to generate it every time.
if (descriptor_->is_packed()) { if (descriptor_->is_packed()) {
printer->Print(variables_, printer->Print(variables_,
"output.WritePacked$capitalized_type_name$Array($number$, field_names[$field_ordinal$], $name$MemoizedSerializedSize, $name$_);\n"); "output.WritePacked$capitalized_type_name$Array($number$, fieldNames[$field_ordinal$], $name$_);\n");
} else { } else {
printer->Print(variables_, printer->Print(variables_,
"output.Write$capitalized_type_name$Array($number$, field_names[$field_ordinal$], $name$_);\n"); "output.Write$capitalized_type_name$Array($number$, fieldNames[$field_ordinal$], $name$_);\n");
} }
printer->Outdent();
printer->Print("}\n");
} }
void RepeatedPrimitiveFieldGenerator::GenerateSerializedSizeCode( void RepeatedPrimitiveFieldGenerator::GenerateSerializedSizeCode(
io::Printer* printer) { io::Printer* printer) {
// TODO(jonskeet): Get rid of most of this - move it into the runtime.
printer->Print("{\n"); printer->Print("{\n");
printer->Indent(); printer->Indent();
printer->Print("int dataSize = 0;\n"); printer->Print("int dataSize = 0;\n");
...@@ -207,10 +121,6 @@ void RepeatedPrimitiveFieldGenerator::GenerateSerializedSizeCode( ...@@ -207,10 +121,6 @@ void RepeatedPrimitiveFieldGenerator::GenerateSerializedSizeCode(
"size += $tag_size$ * $name$_.Count;\n", "size += $tag_size$ * $name$_.Count;\n",
"tag_size", SimpleItoa(tagSize), "name", name()); "tag_size", SimpleItoa(tagSize), "name", name());
} }
// cache the data size for packed fields.
if (descriptor_->is_packed()) {
printer->Print(variables_, "$name$MemoizedSerializedSize = dataSize;\n");
}
printer->Outdent(); printer->Outdent();
printer->Print("}\n"); printer->Print("}\n");
} }
...@@ -218,15 +128,14 @@ void RepeatedPrimitiveFieldGenerator::GenerateSerializedSizeCode( ...@@ -218,15 +128,14 @@ void RepeatedPrimitiveFieldGenerator::GenerateSerializedSizeCode(
void RepeatedPrimitiveFieldGenerator::WriteHash(io::Printer* printer) { void RepeatedPrimitiveFieldGenerator::WriteHash(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"foreach($type_name$ i in $name$_)\n" "foreach($type_name$ i in $name$_)\n {"
" hash ^= i.GetHashCode();\n"); " hash ^= i.GetHashCode();\n"
"}\n");
} }
void RepeatedPrimitiveFieldGenerator::WriteEquals(io::Printer* printer) { void RepeatedPrimitiveFieldGenerator::WriteEquals(io::Printer* printer) {
printer->Print( printer->Print(
variables_, variables_,
"if($name$_.Count != other.$name$_.Count) return false;\n" "if(!$name$_.Equals(other.$name$)) return false;\n");
"for(int ix=0; ix < $name$_.Count; ix++)\n"
" if(!$name$_[ix].Equals(other.$name$_[ix])) return false;\n");
} }
void RepeatedPrimitiveFieldGenerator::WriteToString(io::Printer* printer) { void RepeatedPrimitiveFieldGenerator::WriteToString(io::Printer* printer) {
printer->Print(variables_, printer->Print(variables_,
......
...@@ -41,17 +41,13 @@ namespace protobuf { ...@@ -41,17 +41,13 @@ namespace protobuf {
namespace compiler { namespace compiler {
namespace csharp { namespace csharp {
class Writer;
class RepeatedPrimitiveFieldGenerator : public FieldGeneratorBase { class RepeatedPrimitiveFieldGenerator : public FieldGeneratorBase {
public: public:
RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal); RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor, int fieldOrdinal);
~RepeatedPrimitiveFieldGenerator(); ~RepeatedPrimitiveFieldGenerator();
virtual void GenerateMembers(io::Printer* printer); virtual void GenerateMembers(io::Printer* printer);
virtual void GenerateBuilderMembers(io::Printer* printer);
virtual void GenerateMergingCode(io::Printer* printer); virtual void GenerateMergingCode(io::Printer* printer);
virtual void GenerateBuildingCode(io::Printer* printer);
virtual void GenerateParsingCode(io::Printer* printer); virtual void GenerateParsingCode(io::Printer* printer);
virtual void GenerateSerializationCode(io::Printer* printer); virtual void GenerateSerializationCode(io::Printer* printer);
virtual void GenerateSerializedSizeCode(io::Printer* printer); virtual void GenerateSerializedSizeCode(io::Printer* printer);
......
...@@ -47,14 +47,6 @@ namespace csharp { ...@@ -47,14 +47,6 @@ namespace csharp {
SourceGeneratorBase::SourceGeneratorBase(const FileDescriptor* descriptor) SourceGeneratorBase::SourceGeneratorBase(const FileDescriptor* descriptor)
: descriptor_(descriptor) { : descriptor_(descriptor) {
optimizeSize_ = (descriptor->options().optimize_for()
== FileOptions::CODE_SIZE);
optimizeSpeed_ = (descriptor->options().optimize_for() == FileOptions::SPEED);
useLiteRuntime_ = (descriptor->options().optimize_for()
== FileOptions::LITE_RUNTIME);
optimizeSpeed_ |= useLiteRuntime_;
runtimeSuffix_ = useLiteRuntime_ ? "Lite" : "";
} }
SourceGeneratorBase::~SourceGeneratorBase() { SourceGeneratorBase::~SourceGeneratorBase() {
......
...@@ -47,27 +47,10 @@ class SourceGeneratorBase { ...@@ -47,27 +47,10 @@ class SourceGeneratorBase {
std::string class_access_level(); std::string class_access_level();
bool optimize_size() {
return optimizeSize_;
}
bool optimize_speed() {
return optimizeSpeed_;
}
bool use_lite_runtime() {
return useLiteRuntime_;
}
std::string runtime_suffix() {
return runtimeSuffix_;
}
void WriteGeneratedCodeAttributes(io::Printer* printer); void WriteGeneratedCodeAttributes(io::Printer* printer);
private: private:
const FileDescriptor* descriptor_; const FileDescriptor* descriptor_;
bool optimizeSize_;
bool optimizeSpeed_;
bool useLiteRuntime_;
std::string runtimeSuffix_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SourceGeneratorBase); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SourceGeneratorBase);
}; };
......
...@@ -39,7 +39,6 @@ ...@@ -39,7 +39,6 @@
#include <google/protobuf/compiler/csharp/csharp_umbrella_class.h> #include <google/protobuf/compiler/csharp/csharp_umbrella_class.h>
#include <google/protobuf/compiler/csharp/csharp_enum.h> #include <google/protobuf/compiler/csharp/csharp_enum.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>
...@@ -61,18 +60,6 @@ UmbrellaClassGenerator::~UmbrellaClassGenerator() { ...@@ -61,18 +60,6 @@ UmbrellaClassGenerator::~UmbrellaClassGenerator() {
void UmbrellaClassGenerator::Generate(io::Printer* printer) { void UmbrellaClassGenerator::Generate(io::Printer* printer) {
WriteIntroduction(printer); WriteIntroduction(printer);
WriteExtensionRegistration(printer);
// write children: Extensions
if (file_->extension_count() > 0) {
printer->Print("#region Extensions\n");
for (int i = 0; i < file_->extension_count(); i++) {
ExtensionGenerator extensionGenerator(file_->extension(i));
extensionGenerator.Generate(printer);
}
printer->Print("#endregion\n");
printer->Print("\n");
}
printer->Print("#region Static variables\n"); 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++) {
...@@ -80,11 +67,7 @@ void UmbrellaClassGenerator::Generate(io::Printer* printer) { ...@@ -80,11 +67,7 @@ void UmbrellaClassGenerator::Generate(io::Printer* printer) {
messageGenerator.GenerateStaticVariables(printer); messageGenerator.GenerateStaticVariables(printer);
} }
printer->Print("#endregion\n"); printer->Print("#endregion\n");
if (!use_lite_runtime()) { WriteDescriptor(printer);
WriteDescriptor(printer);
} else {
WriteLiteExtensions(printer);
}
// Close the class declaration. // Close the class declaration.
printer->Outdent(); printer->Outdent();
printer->Print("}\n"); printer->Print("}\n");
...@@ -134,9 +117,9 @@ void UmbrellaClassGenerator::WriteIntroduction(io::Printer* printer) { ...@@ -134,9 +117,9 @@ void UmbrellaClassGenerator::WriteIntroduction(io::Printer* printer) {
"#pragma warning disable 1591, 0612, 3021\n" "#pragma warning disable 1591, 0612, 3021\n"
"#region Designer generated code\n" "#region Designer generated code\n"
"\n" "\n"
"using pb = global::Google.ProtocolBuffers;\n" "using pb = global::Google.Protobuf;\n"
"using pbc = global::Google.ProtocolBuffers.Collections;\n" "using pbc = global::Google.Protobuf.Collections;\n"
"using pbd = global::Google.ProtocolBuffers.Descriptors;\n" "using pbd = global::Google.Protobuf.Descriptors;\n"
"using scg = global::System.Collections.Generic;\n", "using scg = global::System.Collections.Generic;\n",
"file_name", file_->name()); "file_name", file_->name());
...@@ -165,24 +148,6 @@ void UmbrellaClassGenerator::WriteIntroduction(io::Printer* printer) { ...@@ -165,24 +148,6 @@ void UmbrellaClassGenerator::WriteIntroduction(io::Printer* printer) {
printer->Indent(); printer->Indent();
} }
void UmbrellaClassGenerator::WriteExtensionRegistration(io::Printer* printer) {
printer->Print(
"#region Extension registration\n"
"public static void RegisterAllExtensions(pb::ExtensionRegistry registry) {\n");
printer->Indent();
for (int i = 0; i < file_->extension_count(); i++) {
ExtensionGenerator extensionGenerator(file_->extension(i));
extensionGenerator.GenerateExtensionRegistrationCode(printer);
}
for (int i = 0; i < file_->message_type_count(); i++) {
MessageGenerator messageGenerator(file_->message_type(i));
messageGenerator.GenerateExtensionRegistrationCode(printer);
}
printer->Outdent();
printer->Print("}\n");
printer->Print("#endregion\n");
}
void UmbrellaClassGenerator::WriteDescriptor(io::Printer* printer) { void UmbrellaClassGenerator::WriteDescriptor(io::Printer* printer) {
printer->Print( printer->Print(
"#region Descriptor\n" "#region Descriptor\n"
...@@ -219,25 +184,7 @@ void UmbrellaClassGenerator::WriteDescriptor(io::Printer* printer) { ...@@ -219,25 +184,7 @@ void UmbrellaClassGenerator::WriteDescriptor(io::Printer* printer) {
MessageGenerator messageGenerator(file_->message_type(i)); MessageGenerator messageGenerator(file_->message_type(i));
messageGenerator.GenerateStaticVariableInitializers(printer); messageGenerator.GenerateStaticVariableInitializers(printer);
} }
for (int i = 0; i < file_->extension_count(); i++) {
ExtensionGenerator extensionGenerator(file_->extension(i));
extensionGenerator.GenerateStaticVariableInitializers(printer);
}
if (uses_extensions()) {
// Must construct an ExtensionRegistry containing all possible extensions
// and return it.
printer->Print(
"pb::ExtensionRegistry registry = pb::ExtensionRegistry.CreateInstance();\n");
printer->Print("RegisterAllExtensions(registry);\n");
for (int i = 0; i < file_->dependency_count(); i++) {
printer->Print("$dependency$.RegisterAllExtensions(registry);\n",
"dependency", GetFullUmbrellaClassName(file_->dependency(i)));
}
printer->Print("return registry;\n");
} else {
printer->Print("return null;\n");
}
printer->Outdent(); printer->Outdent();
printer->Print("};\n"); printer->Print("};\n");
...@@ -258,33 +205,6 @@ void UmbrellaClassGenerator::WriteDescriptor(io::Printer* printer) { ...@@ -258,33 +205,6 @@ void UmbrellaClassGenerator::WriteDescriptor(io::Printer* printer) {
printer->Print("#endregion\n\n"); printer->Print("#endregion\n\n");
} }
void UmbrellaClassGenerator::WriteLiteExtensions(io::Printer* printer) {
printer->Print(
"#region Extensions\n"
"internal static readonly object Descriptor;\n"
"static $umbrella_class_name$() {\n",
"umbrella_class_name", umbrellaClassname_);
printer->Indent();
printer->Print("Descriptor = null;\n");
for (int i = 0; i < file_->message_type_count(); i++) {
MessageGenerator messageGenerator(file_->message_type(i));
messageGenerator.GenerateStaticVariableInitializers(printer);
}
for (int i = 0; i < file_->extension_count(); i++) {
ExtensionGenerator extensionGenerator(file_->extension(i));
extensionGenerator.GenerateStaticVariableInitializers(printer);
}
printer->Outdent();
printer->Print("}\n");
printer->Print("#endregion\n\n");
}
bool UmbrellaClassGenerator::uses_extensions() {
// TODO(jtattermusch): implement recursive descent that looks for extensions.
// For now, we conservatively assume that extensions are used.
return true;
}
} // namespace csharp } // namespace csharp
} // namespace compiler } // namespace compiler
} // namespace protobuf } // namespace protobuf
......
...@@ -41,8 +41,6 @@ namespace protobuf { ...@@ -41,8 +41,6 @@ namespace protobuf {
namespace compiler { namespace compiler {
namespace csharp { namespace csharp {
class Writer;
class UmbrellaClassGenerator : public SourceGeneratorBase { class UmbrellaClassGenerator : public SourceGeneratorBase {
public: public:
UmbrellaClassGenerator(const FileDescriptor* file); UmbrellaClassGenerator(const FileDescriptor* file);
...@@ -58,11 +56,7 @@ class UmbrellaClassGenerator : public SourceGeneratorBase { ...@@ -58,11 +56,7 @@ class UmbrellaClassGenerator : public SourceGeneratorBase {
std::string umbrellaNamespace_; std::string umbrellaNamespace_;
void WriteIntroduction(io::Printer* printer); void WriteIntroduction(io::Printer* printer);
void WriteExtensionRegistration(io::Printer* printer);
void WriteDescriptor(io::Printer* printer); void WriteDescriptor(io::Printer* printer);
void WriteLiteExtensions(io::Printer* printer);
bool uses_extensions();
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UmbrellaClassGenerator); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UmbrellaClassGenerator);
}; };
...@@ -73,4 +67,3 @@ class UmbrellaClassGenerator : public SourceGeneratorBase { ...@@ -73,4 +67,3 @@ class UmbrellaClassGenerator : public SourceGeneratorBase {
} // namespace google } // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_CSHARP_UMBRELLA_CLASS_H__ #endif // GOOGLE_PROTOBUF_COMPILER_CSHARP_UMBRELLA_CLASS_H__
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include <algorithm>
#include <google/protobuf/stubs/hash.h>
#include <limits>
#include <vector>
#include <google/protobuf/compiler/csharp/csharp_writer.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/wire_format.h>
#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/stubs/substitute.h>
#include <google/protobuf/compiler/csharp/csharp_field_base.h>
#include <google/protobuf/compiler/csharp/csharp_enum_field.h>
#include <google/protobuf/compiler/csharp/csharp_message_field.h>
#include <google/protobuf/compiler/csharp/csharp_primitive_field.h>
#include <google/protobuf/compiler/csharp/csharp_repeated_enum_field.h>
#include <google/protobuf/compiler/csharp/csharp_repeated_message_field.h>
#include <google/protobuf/compiler/csharp/csharp_repeated_primitive_field.h>
namespace google {
namespace protobuf {
namespace compiler {
namespace csharp {
Writer::Writer(google::protobuf::io::Printer* printer)
: printer_(printer),
newline_("\n") {
// TODO(jtattermusch): make newline customizable.
}
Writer::~Writer() {
}
void Writer::Indent() {
printer_->Indent();
}
void Writer::Outdent() {
printer_->Outdent();
}
void Writer::Write(const char* text) {
printer_->Print(text);
}
void Writer::Write(const char* text, const string& value0) {
printer_->Print(text, "0", value0);
}
void Writer::Write(const char* text, const string& value0,
const string& value1) {
printer_->Print(text, "0", value0, "1", value1);
}
void Writer::Write(const char* text, const string& value0, const string& value1,
const string& value2) {
printer_->Print(text, "0", value0, "1", value1, "2", value2);
}
void Writer::Write(const char* text, const string& value0, const string& value1,
const string& value2, const string& value3) {
printer_->Print(text, "0", value0, "1", value1, "2", value2, "3", value3);
}
void Writer::WriteLine() {
printer_->Print(newline_);
}
void Writer::WriteLine(const char* text) {
Write(text);
WriteLine();
}
void Writer::WriteLine(const char* text, const string& value0) {
Write(text, value0);
WriteLine();
}
void Writer::WriteLine(const char* text, const string& value0,
const string& value1) {
Write(text, value0, value1);
WriteLine();
}
void Writer::WriteLine(const char* text, const string& value0,
const string& value1, const string& value2) {
Write(text, value0, value1, value2);
WriteLine();
}
void Writer::WriteLine(const char* text, const string& value0,
const string& value1, const string& value2,
const string& value3) {
Write(text, value0, value1, value2, value3);
WriteLine();
}
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#ifndef GOOGLE_PROTOBUF_COMPILER_CSHARP_WRITER_H__
#define GOOGLE_PROTOBUF_COMPILER_CSHARP_WRITER_H__
#include <string>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/compiler/code_generator.h>
#include <google/protobuf/io/printer.h>
namespace google {
namespace protobuf {
namespace compiler {
namespace csharp {
// Simple wrapper around Printer that supports customizable line endings
// and number-based variables (e.g. $0$).
class Writer {
public:
Writer(io::Printer* printer);
~Writer();
void Indent();
void Outdent();
void Write(const char* text);
void Write(const char* text, const string& value0);
void Write(const char* text, const string& value0, const string& value1);
void Write(const char* text, const string& value0, const string& value1,
const string& value2);
void Write(const char* text, const string& value0, const string& value1,
const string& value2, const string& value3);
void WriteLine();
void WriteLine(const char* text);
void WriteLine(const char* text, const string& value0);
void WriteLine(const char* text, const string& value0, const string& value1);
void WriteLine(const char* text, const string& value0, const string& value1,
const string& value2);
void WriteLine(const char* text, const string& value0, const string& value1,
const string& value2, const string& value3);
private:
io::Printer* printer_;
const char* newline_;
};
} // namespace csharp
} // namespace compiler
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_CSHARP_WRITER_H__
...@@ -169,7 +169,6 @@ ...@@ -169,7 +169,6 @@
<File RelativePath="..\src\google\protobuf\compiler\cpp\cpp_string_field.h"></File> <File RelativePath="..\src\google\protobuf\compiler\cpp\cpp_string_field.h"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_enum.h"></File> <File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_enum.h"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_enum_field.h"></File> <File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_enum_field.h"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_extension.h"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_field_base.h"></File> <File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_field_base.h"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_generator.h"></File> <File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_generator.h"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_helpers.h"></File> <File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_helpers.h"></File>
...@@ -181,7 +180,6 @@ ...@@ -181,7 +180,6 @@
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_repeated_primitive_field.h"></File> <File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_repeated_primitive_field.h"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_source_generator_base.h"></File> <File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_source_generator_base.h"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_umbrella_class.h"></File> <File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_umbrella_class.h"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_writer.h"></File>
<File RelativePath="..\src\google\protobuf\compiler\java\java_context.h"></File> <File RelativePath="..\src\google\protobuf\compiler\java\java_context.h"></File>
<File RelativePath="..\src\google\protobuf\compiler\java\java_enum.h"></File> <File RelativePath="..\src\google\protobuf\compiler\java\java_enum.h"></File>
<File RelativePath="..\src\google\protobuf\compiler\java\java_enum_field.h"></File> <File RelativePath="..\src\google\protobuf\compiler\java\java_enum_field.h"></File>
...@@ -269,7 +267,6 @@ ...@@ -269,7 +267,6 @@
<File RelativePath="..\src\google\protobuf\compiler\cpp\cpp_string_field.cc"></File> <File RelativePath="..\src\google\protobuf\compiler\cpp\cpp_string_field.cc"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_enum.cc"></File> <File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_enum.cc"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_enum_field.cc"></File> <File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_enum_field.cc"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_extension.cc"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_field_base.cc"></File> <File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_field_base.cc"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_generator.cc"></File> <File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_generator.cc"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_helpers.cc"></File> <File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_helpers.cc"></File>
...@@ -281,7 +278,6 @@ ...@@ -281,7 +278,6 @@
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_repeated_primitive_field.cc"></File> <File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_repeated_primitive_field.cc"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_source_generator_base.cc"></File> <File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_source_generator_base.cc"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_umbrella_class.cc"></File> <File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_umbrella_class.cc"></File>
<File RelativePath="..\src\google\protobuf\compiler\csharp\csharp_writer.cc"></File>
<File RelativePath="..\src\google\protobuf\compiler\java\java_context.cc"></File> <File RelativePath="..\src\google\protobuf\compiler\java\java_context.cc"></File>
<File RelativePath="..\src\google\protobuf\compiler\java\java_doc_comment.cc"></File> <File RelativePath="..\src\google\protobuf\compiler\java\java_doc_comment.cc"></File>
<File RelativePath="..\src\google\protobuf\compiler\java\java_enum.cc"></File> <File RelativePath="..\src\google\protobuf\compiler\java\java_enum.cc"></File>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment