Commit 44664bb7 authored by Jan Tattermusch's avatar Jan Tattermusch

updated C# codegen to use restricted set of csharp options from descriptor.proto

parent b52cf04b
...@@ -63,7 +63,7 @@ ExtensionGenerator::~ExtensionGenerator() { ...@@ -63,7 +63,7 @@ ExtensionGenerator::~ExtensionGenerator() {
} }
void ExtensionGenerator::Generate(Writer* writer) { void ExtensionGenerator::Generate(Writer* writer) {
if (descriptor_->file()->options().csharp_cls_compliance() if (cls_compliance()
&& (GetFieldConstantName(descriptor_).substr(0, 1) == "_")) { && (GetFieldConstantName(descriptor_).substr(0, 1) == "_")) {
writer->WriteLine("[global::System.CLSCompliant(false)]"); writer->WriteLine("[global::System.CLSCompliant(false)]");
} }
......
...@@ -84,7 +84,7 @@ void FieldGeneratorBase::AddPublicMemberAttributes(Writer* writer) { ...@@ -84,7 +84,7 @@ void FieldGeneratorBase::AddPublicMemberAttributes(Writer* writer) {
} }
void FieldGeneratorBase::AddClsComplianceCheck(Writer* writer) { void FieldGeneratorBase::AddClsComplianceCheck(Writer* writer) {
if (!is_cls_compliant() && descriptor_->file()->options().csharp_cls_compliance()) { if (cls_compliance() && !is_cls_compliant()) {
writer->WriteLine("[global::System.CLSCompliant(false)]"); writer->WriteLine("[global::System.CLSCompliant(false)]");
} }
} }
......
...@@ -61,9 +61,11 @@ bool Generator::Generate( ...@@ -61,9 +61,11 @@ bool Generator::Generate(
GeneratorContext* generator_context, GeneratorContext* generator_context,
string* error) const { string* error) const {
// TODO: parse generator parameters... // TODO(jtattermusch): parse generator parameters:
// cls_compliance
// file_extension
// TODO: file output file naming logic // TODO(jtattermusch): rework output file naming logic
std::string filename = std::string filename =
StripDotProto(file->name()) + ".cs"; StripDotProto(file->name()) + ".cs";
scoped_ptr<io::ZeroCopyOutputStream> output( scoped_ptr<io::ZeroCopyOutputStream> output(
......
...@@ -122,16 +122,12 @@ std::string GetUmbrellaClassNameInternal(const std::string& proto_file) { ...@@ -122,16 +122,12 @@ std::string GetUmbrellaClassNameInternal(const std::string& proto_file) {
} }
std::string GetFileUmbrellaClassname(const FileDescriptor* descriptor) { std::string GetFileUmbrellaClassname(const FileDescriptor* descriptor) {
if (descriptor->options().has_csharp_umbrella_classname()) { // umbrella_classname can no longer be set using message option.
return descriptor->options().csharp_umbrella_namespace(); return GetUmbrellaClassNameInternal(descriptor->name());
} else {
return GetUmbrellaClassNameInternal(descriptor->name());
}
} }
std::string GetFileUmbrellaNamespace(const FileDescriptor* descriptor) { std::string GetFileUmbrellaNamespace(const FileDescriptor* descriptor) {
if (!descriptor->options().csharp_nest_classes() if (!descriptor->options().has_csharp_umbrella_namespace()) {
&& !descriptor->options().has_csharp_umbrella_namespace()) {
bool collision = false; bool collision = false;
// TODO(jtattermusch): detect collisions! // TODO(jtattermusch): detect collisions!
// foreach (IDescriptor d in MessageTypes) // foreach (IDescriptor d in MessageTypes)
...@@ -196,12 +192,6 @@ std::string UnderscoresToPascalCase(const std::string& input) { ...@@ -196,12 +192,6 @@ std::string UnderscoresToPascalCase(const std::string& input) {
std::string ToCSharpName(const std::string& name, const FileDescriptor* file) { std::string ToCSharpName(const std::string& name, const FileDescriptor* file) {
std::string result = GetFileNamespace(file); std::string result = GetFileNamespace(file);
if (file->options().csharp_nest_classes()) {
if (result != "") {
result += ".";
}
result += GetFileUmbrellaClassname(file);
}
if (result != "") { if (result != "") {
result += '.'; result += '.';
} }
...@@ -233,8 +223,7 @@ std::string GetQualifiedUmbrellaClassName(const FileDescriptor* descriptor) { ...@@ -233,8 +223,7 @@ std::string GetQualifiedUmbrellaClassName(const FileDescriptor* descriptor) {
std::string umbrellaClassname = GetFileUmbrellaClassname(descriptor); std::string umbrellaClassname = GetFileUmbrellaClassname(descriptor);
std::string fullName = umbrellaClassname; std::string fullName = umbrellaClassname;
if (!descriptor->options().csharp_nest_classes() if (!umbrellaNamespace.empty()) {
&& !umbrellaNamespace.empty()) {
fullName = umbrellaNamespace + "." + umbrellaClassname; fullName = umbrellaNamespace + "." + umbrellaClassname;
} }
return fullName; return fullName;
......
...@@ -118,9 +118,7 @@ void MessageGenerator::GenerateStaticVariables(Writer* writer) { ...@@ -118,9 +118,7 @@ void MessageGenerator::GenerateStaticVariables(Writer* writer) {
if (!use_lite_runtime()) { if (!use_lite_runtime()) {
// The descriptor for this type. // The descriptor for this type.
std::string access = std::string access = "internal";
descriptor_->file()->options().csharp_nest_classes() ?
"private" : "internal";
writer->WriteLine( writer->WriteLine(
"$0$ static pbd::MessageDescriptor internal__$1$__Descriptor;", access, "$0$ static pbd::MessageDescriptor internal__$1$__Descriptor;", access,
identifier); identifier);
...@@ -175,9 +173,6 @@ void MessageGenerator::GenerateStaticVariableInitializers(Writer* writer) { ...@@ -175,9 +173,6 @@ void MessageGenerator::GenerateStaticVariableInitializers(Writer* writer) {
} }
void MessageGenerator::Generate(Writer* writer) { void MessageGenerator::Generate(Writer* writer) {
if (descriptor_->file()->options().csharp_add_serializable()) {
writer->WriteLine("[global::System.SerializableAttribute()]");
}
writer->WriteLine( writer->WriteLine(
"[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]"); "[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
WriteGeneratedCodeAttributes(writer); WriteGeneratedCodeAttributes(writer);
...@@ -187,9 +182,6 @@ void MessageGenerator::Generate(Writer* writer) { ...@@ -187,9 +182,6 @@ void MessageGenerator::Generate(Writer* writer) {
descriptor_->extension_range_count() > 0 ? "Extendable" : "Generated", descriptor_->extension_range_count() > 0 ? "Extendable" : "Generated",
runtime_suffix()); runtime_suffix());
writer->Indent(); writer->Indent();
if (descriptor_->file()->options().csharp_generate_private_ctor()) {
writer->WriteLine("private $0$() { }", class_name());
}
// Must call MakeReadOnly() to make sure all lists are made read-only // Must call MakeReadOnly() to make sure all lists are made read-only
writer->WriteLine( writer->WriteLine(
"private static readonly $0$ defaultInstance = new $0$().MakeReadOnly();", "private static readonly $0$ defaultInstance = new $0$().MakeReadOnly();",
...@@ -271,7 +263,7 @@ void MessageGenerator::Generate(Writer* writer) { ...@@ -271,7 +263,7 @@ void MessageGenerator::Generate(Writer* writer) {
for (int i = 0; i < descriptor_->field_count(); i++) { for (int i = 0; i < descriptor_->field_count(); i++) {
const FieldDescriptor* fieldDescriptor = descriptor_->field(i); const FieldDescriptor* fieldDescriptor = descriptor_->field(i);
// TODO(jtattermusch): same code for cls compliance is in csharp_extension // TODO(jtattermusch): same code for cls compliance is in csharp_extension
if (descriptor_->file()->options().csharp_cls_compliance() if (cls_compliance()
&& GetFieldConstantName(fieldDescriptor)[0] == '_') { && GetFieldConstantName(fieldDescriptor)[0] == '_') {
writer->WriteLine("[global::System.CLSCompliant(false)]"); writer->WriteLine("[global::System.CLSCompliant(false)]");
} }
...@@ -557,9 +549,6 @@ void MessageGenerator::GenerateBuilder(Writer* writer) { ...@@ -557,9 +549,6 @@ void MessageGenerator::GenerateBuilder(Writer* writer) {
writer->WriteLine(" return new Builder(prototype);"); writer->WriteLine(" return new Builder(prototype);");
writer->WriteLine("}"); writer->WriteLine("}");
writer->WriteLine(); writer->WriteLine();
if (descriptor_->file()->options().csharp_add_serializable()) {
writer->WriteLine("[global::System.SerializableAttribute()]");
}
writer->WriteLine( writer->WriteLine(
"[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]"); "[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
WriteGeneratedCodeAttributes(writer); WriteGeneratedCodeAttributes(writer);
......
...@@ -76,6 +76,11 @@ std::string SourceGeneratorBase::class_access_level() { ...@@ -76,6 +76,11 @@ std::string SourceGeneratorBase::class_access_level() {
return "public"; return "public";
} }
bool SourceGeneratorBase::cls_compliance() {
// TODO(jtattermusch): implement this based on "cls_compliance" cmdline param.
return true;
}
} // namespace csharp } // namespace csharp
} // namespace compiler } // namespace compiler
} // namespace protobuf } // namespace protobuf
......
...@@ -48,6 +48,7 @@ class SourceGeneratorBase { ...@@ -48,6 +48,7 @@ class SourceGeneratorBase {
virtual ~SourceGeneratorBase(); virtual ~SourceGeneratorBase();
std::string class_access_level(); std::string class_access_level();
bool cls_compliance();
bool optimize_size() { bool optimize_size() {
return optimizeSize_; return optimizeSize_;
......
...@@ -86,17 +86,14 @@ void UmbrellaClassGenerator::Generate(Writer* writer) { ...@@ -86,17 +86,14 @@ void UmbrellaClassGenerator::Generate(Writer* writer) {
} else { } else {
WriteLiteExtensions(writer); WriteLiteExtensions(writer);
} }
// The class declaration either gets closed before or after the children are written. // Close the class declaration.
if (!file_->options().csharp_nest_classes()) { writer->Outdent();
writer->WriteLine("}");
// Close the namespace around the umbrella class if defined
if (!umbrellaNamespace_.empty()) {
writer->Outdent(); writer->Outdent();
writer->WriteLine("}"); writer->WriteLine("}");
// Close the namespace around the umbrella class if defined
if (!file_->options().csharp_nest_classes()
&& !umbrellaNamespace_.empty()) {
writer->Outdent();
writer->WriteLine("}");
}
} }
// write children: Enums // write children: Enums
...@@ -121,12 +118,8 @@ void UmbrellaClassGenerator::Generate(Writer* writer) { ...@@ -121,12 +118,8 @@ void UmbrellaClassGenerator::Generate(Writer* writer) {
writer->WriteLine(); writer->WriteLine();
} }
// TODO(jtattermusch): add support for generating services. // TODO(jtattermusch): add insertion point for services.
//WriteChildren(writer, "Services", Descriptor.Services);
if (file_->options().csharp_nest_classes()) {
writer->Outdent();
writer->WriteLine("}");
}
if (!namespace_.empty()) { if (!namespace_.empty()) {
writer->Outdent(); writer->Outdent();
writer->WriteLine("}"); writer->WriteLine("}");
...@@ -155,16 +148,12 @@ void UmbrellaClassGenerator::WriteIntroduction(Writer* writer) { ...@@ -155,16 +148,12 @@ void UmbrellaClassGenerator::WriteIntroduction(Writer* writer) {
} }
// Add the namespace around the umbrella class if defined // Add the namespace around the umbrella class if defined
if (!file_->options().csharp_nest_classes() && !umbrellaNamespace_.empty()) { if (!umbrellaNamespace_.empty()) {
writer->WriteLine("namespace $0$ {", umbrellaNamespace_); writer->WriteLine("namespace $0$ {", umbrellaNamespace_);
writer->Indent(); writer->Indent();
writer->WriteLine(); writer->WriteLine();
} }
if (file_->options().csharp_code_contracts()) {
writer->WriteLine(
"[global::System.Diagnostics.Contracts.ContractVerificationAttribute(false)]");
}
writer->WriteLine( writer->WriteLine(
"[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]"); "[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
WriteGeneratedCodeAttributes(writer); WriteGeneratedCodeAttributes(writer);
......
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