Esempio n. 1
0
 private void GenerateBuilder(TextGenerator writer)
 {
     writer.WriteLine("public static Builder CreateBuilder() { return new Builder(); }");
     writer.WriteLine("public override Builder ToBuilder() { return CreateBuilder(this); }");
     writer.WriteLine("public override Builder CreateBuilderForType() { return new Builder(); }");
     writer.WriteLine("public static Builder CreateBuilder({0} prototype) {{", ClassName);
     writer.WriteLine("  return (Builder) new Builder().MergeFrom(prototype);");
     writer.WriteLine("}");
     writer.WriteLine();
     writer.WriteLine("{0} sealed partial class Builder : pb::{2}Builder{3}<{1}, Builder> {{",
                      ClassAccessLevel, ClassName, Descriptor.Proto.ExtensionRangeCount > 0 ? "Extendable" : "Generated", RuntimeSuffix);
     writer.Indent();
     writer.WriteLine("protected override Builder ThisBuilder {");
     writer.WriteLine("  get { return this; }");
     writer.WriteLine("}");
     GenerateCommonBuilderMethods(writer);
     if (OptimizeSpeed)
     {
         GenerateBuilderParsingMethods(writer);
     }
     foreach (FieldDescriptor field in Descriptor.Fields)
     {
         writer.WriteLine();
         // No field comment :(
         SourceGenerators.CreateFieldGenerator(field).GenerateBuilderMembers(writer);
     }
     writer.Outdent();
     writer.WriteLine("}");
 }
        protected void WriteChildren <TChild>(TextGenerator writer, string region, IEnumerable <TChild> children)
            where TChild : IDescriptor
        {
            // Copy the set of children; makes access easier
            List <TChild> copy = new List <TChild>(children);

            if (copy.Count == 0)
            {
                return;
            }

            if (region != null)
            {
                writer.WriteLine("#region {0}", region);
            }
            foreach (TChild child in children)
            {
                SourceGenerators.CreateGenerator(child).Generate(writer);
            }
            if (region != null)
            {
                writer.WriteLine("#endregion");
                writer.WriteLine();
            }
        }
Esempio n. 3
0
        private void GenerateLiteRuntimeMethods(TextGenerator writer)
        {
            bool callbase = Descriptor.Proto.ExtensionRangeCount > 0;

            writer.WriteLine("#region Lite runtime methods");
            writer.WriteLine("public override int GetHashCode() {");
            writer.Indent();
            writer.WriteLine("int hash = GetType().GetHashCode();");
            foreach (FieldDescriptor fieldDescriptor in Descriptor.Fields)
            {
                SourceGenerators.CreateFieldGenerator(fieldDescriptor).WriteHash(writer);
            }
            if (callbase)
            {
                writer.WriteLine("hash ^= base.GetHashCode();");
            }
            writer.WriteLine("return hash;");
            writer.Outdent();
            writer.WriteLine("}");
            writer.WriteLine();

            writer.WriteLine("public override bool Equals(object obj) {");
            writer.Indent();
            writer.WriteLine("{0} other = obj as {0};", ClassName);
            writer.WriteLine("if (other == null) return false;");
            foreach (FieldDescriptor fieldDescriptor in Descriptor.Fields)
            {
                SourceGenerators.CreateFieldGenerator(fieldDescriptor).WriteEquals(writer);
            }
            if (callbase)
            {
                writer.WriteLine("if (!base.Equals(other)) return false;");
            }
            writer.WriteLine("return true;");
            writer.Outdent();
            writer.WriteLine("}");
            writer.WriteLine();

            writer.WriteLine("public override void PrintTo(global::System.IO.TextWriter writer) {");
            writer.Indent();
            List <FieldDescriptor> sorted = new List <FieldDescriptor>(Descriptor.Fields);

            sorted.Sort(new Comparison <FieldDescriptor>(delegate(FieldDescriptor a, FieldDescriptor b) { return(a.FieldNumber.CompareTo(b.FieldNumber)); }));
            foreach (FieldDescriptor fieldDescriptor in sorted)
            {
                SourceGenerators.CreateFieldGenerator(fieldDescriptor).WriteToString(writer);
            }
            if (callbase)
            {
                writer.WriteLine("base.PrintTo(writer);");
            }
            writer.Outdent();
            writer.WriteLine("}");
            writer.WriteLine("#endregion");
            writer.WriteLine();
        }
        private void GenerateBuilderParsingMethods(TextGenerator writer)
        {
            List <FieldDescriptor> sortedFields = new List <FieldDescriptor>(Descriptor.Fields);

            sortedFields.Sort((f1, f2) => f1.FieldNumber.CompareTo(f2.FieldNumber));

            writer.WriteLine("public override Builder MergeFrom(pb::CodedInputStream input) {");
            writer.WriteLine("  return MergeFrom(input, pb::ExtensionRegistry.Empty);");
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("public override Builder MergeFrom(pb::CodedInputStream input, pb::ExtensionRegistry extensionRegistry) {");
            writer.Indent();
            writer.WriteLine("pb::UnknownFieldSet.Builder unknownFields = null;");
            writer.WriteLine("while (true) {");
            writer.Indent();
            writer.WriteLine("uint tag = input.ReadTag();");
            writer.WriteLine("switch (tag) {");
            writer.Indent();
            writer.WriteLine("case 0: {"); // 0 signals EOF / limit reached
            writer.WriteLine("  if (unknownFields != null) {");
            writer.WriteLine("    this.UnknownFields = unknownFields.Build();");
            writer.WriteLine("  }");
            writer.WriteLine("  return this;");
            writer.WriteLine("}");
            writer.WriteLine("default: {");
            writer.WriteLine("  if (pb::WireFormat.IsEndGroupTag(tag)) {");
            writer.WriteLine("    if (unknownFields != null) {");
            writer.WriteLine("      this.UnknownFields = unknownFields.Build();");
            writer.WriteLine("    }");
            writer.WriteLine("    return this;");               // it's an endgroup tag
            writer.WriteLine("  }");
            writer.WriteLine("  if (unknownFields == null) {"); // First unknown field - create builder now
            writer.WriteLine("    unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);");
            writer.WriteLine("  }");
            writer.WriteLine("  ParseUnknownField(input, unknownFields, extensionRegistry, tag);");
            writer.WriteLine("  break;");
            writer.WriteLine("}");
            foreach (FieldDescriptor field in sortedFields)
            {
                uint tag = WireFormat.MakeTag(field);
                writer.WriteLine("case {0}: {{", tag);
                writer.Indent();
                SourceGenerators.CreateFieldGenerator(field).GenerateParsingCode(writer);
                writer.WriteLine("break;");
                writer.Outdent();
                writer.WriteLine("}");
            }
            writer.Outdent();
            writer.WriteLine("}");
            writer.Outdent();
            writer.WriteLine("}");
            writer.Outdent();
            writer.WriteLine("}");
            writer.WriteLine();
        }
Esempio n. 5
0
        private void GenerateCommonBuilderMethods(TextGenerator writer)
        {
            writer.WriteLine("{0} Builder() {{}}", ClassAccessLevel);
            writer.WriteLine();
            writer.WriteLine("{0} result = new {0}();", ClassName);
            writer.WriteLine();
            writer.WriteLine("protected override {0} MessageBeingBuilt {{", ClassName);
            writer.WriteLine("  get { return result; }");
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("public override Builder Clear() {");
            writer.WriteLine("  result = new {0}();", ClassName);
            writer.WriteLine("  return this;");
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("public override Builder Clone() {");
            writer.WriteLine("  return new Builder().MergeFrom(result);");
            writer.WriteLine("}");
            writer.WriteLine();
            if (!UseLiteRuntime)
            {
                writer.WriteLine("public override pbd::MessageDescriptor DescriptorForType {");
                writer.WriteLine("  get {{ return {0}.Descriptor; }}", FullClassName);
                writer.WriteLine("}");
                writer.WriteLine();
            }
            writer.WriteLine("public override {0} DefaultInstanceForType {{", ClassName);
            writer.WriteLine("  get {{ return {0}.DefaultInstance; }}", FullClassName);
            writer.WriteLine("}");
            writer.WriteLine();

            writer.WriteLine("public override {0} BuildPartial() {{", ClassName);
            writer.Indent();
            writer.WriteLine("if (result == null) {");
            writer.WriteLine("  throw new global::System.InvalidOperationException(\"build() has already been called on this Builder\");");
            writer.WriteLine("}");
            foreach (FieldDescriptor field in Descriptor.Fields)
            {
                SourceGenerators.CreateFieldGenerator(field).GenerateBuildingCode(writer);
            }
            writer.WriteLine("{0} returnMe = result;", ClassName);
            writer.WriteLine("result = null;");
            writer.WriteLine("return returnMe;");
            writer.Outdent();
            writer.WriteLine("}");
            writer.WriteLine();

            if (OptimizeSpeed)
            {
                writer.WriteLine("public override Builder MergeFrom(pb::IMessage{0} other) {{", RuntimeSuffix);
                writer.WriteLine("  if (other is {0}) {{", ClassName);
                writer.WriteLine("    return MergeFrom(({0}) other);", ClassName);
                writer.WriteLine("  } else {");
                writer.WriteLine("    base.MergeFrom(other);");
                writer.WriteLine("    return this;");
                writer.WriteLine("  }");
                writer.WriteLine("}");
                writer.WriteLine();
                writer.WriteLine("public override Builder MergeFrom({0} other) {{", ClassName);
                // Optimization:  If other is the default instance, we know none of its
                // fields are set so we can skip the merge.
                writer.Indent();
                writer.WriteLine("if (other == {0}.DefaultInstance) return this;", FullClassName);
                foreach (FieldDescriptor field in Descriptor.Fields)
                {
                    SourceGenerators.CreateFieldGenerator(field).GenerateMergingCode(writer);
                }
                // if message type has extensions
                if (Descriptor.Proto.ExtensionRangeCount > 0)
                {
                    writer.WriteLine("  this.MergeExtensionFields(other);");
                }
                if (!UseLiteRuntime)
                {
                    writer.WriteLine("this.MergeUnknownFields(other.UnknownFields);");
                }
                writer.WriteLine("return this;");
                writer.Outdent();
                writer.WriteLine("}");
                writer.WriteLine();
            }
        }
Esempio n. 6
0
 private static void GenerateSerializeOneField(TextGenerator writer, FieldDescriptor fieldDescriptor)
 {
     SourceGenerators.CreateFieldGenerator(fieldDescriptor).GenerateSerializationCode(writer);
 }
Esempio n. 7
0
        private void GenerateMessageSerializationMethods(TextGenerator writer)
        {
            List <FieldDescriptor> sortedFields = new List <FieldDescriptor>(Descriptor.Fields);

            sortedFields.Sort((f1, f2) => f1.FieldNumber.CompareTo(f2.FieldNumber));

            List <ExtensionRange> sortedExtensions = new List <ExtensionRange>(Descriptor.Proto.ExtensionRangeList);

            sortedExtensions.Sort((r1, r2) => (r1.Start.CompareTo(r2.Start)));

            writer.WriteLine("public override void WriteTo(pb::CodedOutputStream output) {");
            writer.Indent();
            // Make sure we've computed the serialized length, so that packed fields are generated correctly.
            writer.WriteLine("int size = SerializedSize;");
            if (Descriptor.Proto.ExtensionRangeList.Count > 0)
            {
                writer.WriteLine("pb::ExtendableMessage{1}<{0}, {0}.Builder>.ExtensionWriter extensionWriter = CreateExtensionWriter(this);",
                                 ClassName, RuntimeSuffix);
            }

            // Merge the fields and the extension ranges, both sorted by field number.
            for (int i = 0, j = 0; i < Descriptor.Fields.Count || j < sortedExtensions.Count;)
            {
                if (i == Descriptor.Fields.Count)
                {
                    GenerateSerializeOneExtensionRange(writer, sortedExtensions[j++]);
                }
                else if (j == sortedExtensions.Count)
                {
                    GenerateSerializeOneField(writer, sortedFields[i++]);
                }
                else if (sortedFields[i].FieldNumber < sortedExtensions[j].Start)
                {
                    GenerateSerializeOneField(writer, sortedFields[i++]);
                }
                else
                {
                    GenerateSerializeOneExtensionRange(writer, sortedExtensions[j++]);
                }
            }

            if (!UseLiteRuntime)
            {
                if (Descriptor.Proto.Options.MessageSetWireFormat)
                {
                    writer.WriteLine("UnknownFields.WriteAsMessageSetTo(output);");
                }
                else
                {
                    writer.WriteLine("UnknownFields.WriteTo(output);");
                }
            }

            writer.Outdent();
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("private int memoizedSerializedSize = -1;");
            writer.WriteLine("public override int SerializedSize {");
            writer.Indent();
            writer.WriteLine("get {");
            writer.Indent();
            writer.WriteLine("int size = memoizedSerializedSize;");
            writer.WriteLine("if (size != -1) return size;");
            writer.WriteLine();
            writer.WriteLine("size = 0;");
            foreach (FieldDescriptor field in Descriptor.Fields)
            {
                SourceGenerators.CreateFieldGenerator(field).GenerateSerializedSizeCode(writer);
            }
            if (Descriptor.Proto.ExtensionRangeCount > 0)
            {
                writer.WriteLine("size += ExtensionsSerializedSize;");
            }

            if (!UseLiteRuntime)
            {
                if (Descriptor.Options.MessageSetWireFormat)
                {
                    writer.WriteLine("size += UnknownFields.SerializedSizeAsMessageSet;");
                }
                else
                {
                    writer.WriteLine("size += UnknownFields.SerializedSize;");
                }
            }
            writer.WriteLine("memoizedSerializedSize = size;");
            writer.WriteLine("return size;");
            writer.Outdent();
            writer.WriteLine("}");
            writer.Outdent();
            writer.WriteLine("}");
            writer.WriteLine();
        }
Esempio n. 8
0
        public void Generate(TextGenerator writer)
        {
            writer.WriteLine("{0} sealed partial class {1} : pb::{2}Message{3}<{1}, {1}.Builder> {{",
                             ClassAccessLevel, ClassName,
                             Descriptor.Proto.ExtensionRangeCount > 0 ? "Extendable" : "Generated",
                             RuntimeSuffix);
            writer.Indent();
            // Must call BuildPartial() to make sure all lists are made read-only
            writer.WriteLine("private static readonly {0} defaultInstance = new Builder().BuildPartial();", ClassName);
            writer.WriteLine("public static {0} DefaultInstance {{", ClassName);
            writer.WriteLine("  get { return defaultInstance; }");
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("public override {0} DefaultInstanceForType {{", ClassName);
            writer.WriteLine("  get { return defaultInstance; }");
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("protected override {0} ThisMessage {{", ClassName);
            writer.WriteLine("  get { return this; }");
            writer.WriteLine("}");
            writer.WriteLine();
            if (!UseLiteRuntime)
            {
                writer.WriteLine("public static pbd::MessageDescriptor Descriptor {");
                writer.WriteLine("  get {{ return {0}.internal__{1}__Descriptor; }}", DescriptorUtil.GetFullUmbrellaClassName(Descriptor),
                                 GetUniqueFileScopeIdentifier(Descriptor));
                writer.WriteLine("}");
                writer.WriteLine();
                writer.WriteLine("protected override pb::FieldAccess.FieldAccessorTable<{0}, {0}.Builder> InternalFieldAccessors {{", ClassName);
                writer.WriteLine("  get {{ return {0}.internal__{1}__FieldAccessorTable; }}", DescriptorUtil.GetFullUmbrellaClassName(Descriptor),
                                 GetUniqueFileScopeIdentifier(Descriptor));
                writer.WriteLine("}");
                writer.WriteLine();
            }

            // Extensions don't need to go in an extra nested type
            WriteChildren(writer, null, Descriptor.Extensions);

            if (Descriptor.EnumTypes.Count + Descriptor.NestedTypes.Count > 0)
            {
                writer.WriteLine("#region Nested types");
                writer.WriteLine("public static class Types {");
                writer.Indent();
                WriteChildren(writer, null, Descriptor.EnumTypes);
                WriteChildren(writer, null, Descriptor.NestedTypes);
                writer.Outdent();
                writer.WriteLine("}");
                writer.WriteLine("#endregion");
                writer.WriteLine();
            }

            foreach (FieldDescriptor fieldDescriptor in Descriptor.Fields)
            {
                // Rats: we lose the debug comment here :(
                writer.WriteLine("public const int {0} = {1};", GetFieldConstantName(fieldDescriptor), fieldDescriptor.FieldNumber);
                SourceGenerators.CreateFieldGenerator(fieldDescriptor).GenerateMembers(writer);
                writer.WriteLine();
            }

            if (OptimizeSpeed)
            {
                GenerateIsInitialized(writer);
                GenerateMessageSerializationMethods(writer);
            }
            if (UseLiteRuntime)
            {
                GenerateLiteRuntimeMethods(writer);
            }

            GenerateParseFromMethods(writer);
            GenerateBuilder(writer);

            // Force the static initialization code for the file to run, since it may
            // initialize static variables declared in this class.
            writer.WriteLine("static {0}() {{", ClassName);
            // We call object.ReferenceEquals() just to make it a valid statement on its own.
            // Another option would be GetType(), but that causes problems in DescriptorProtoFile,
            // where the bootstrapping is somewhat recursive - type initializers call
            // each other, effectively. We temporarily see Descriptor as null.
            writer.WriteLine("  object.ReferenceEquals({0}.Descriptor, null);", DescriptorUtil.GetFullUmbrellaClassName(Descriptor));
            writer.WriteLine("}");

            writer.Outdent();
            writer.WriteLine("}");
            writer.WriteLine();
        }
 private IFieldSourceGenerator CreateFieldGenerator(FieldDescriptor fieldDescriptor)
 {
     return(SourceGenerators.CreateFieldGenerator(fieldDescriptor, FieldOrdinal(fieldDescriptor)));
 }
        public void Generate(TextGenerator writer)
        {
            writer.WriteLine("{0} sealed partial class {1} : pb::{2}Message<{1}, {1}.Builder> {{",
                             ClassAccessLevel, ClassName, Descriptor.Proto.ExtensionRangeCount > 0 ? "Extendable" : "Generated");
            writer.Indent();
            // Must call BuildPartial() to make sure all lists are made read-only
            writer.WriteLine("private static readonly {0} defaultInstance = new Builder().BuildPartial();", ClassName);
            writer.WriteLine("public static {0} DefaultInstance {{", ClassName);
            writer.WriteLine("  get { return defaultInstance; }");
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("public override {0} DefaultInstanceForType {{", ClassName);
            writer.WriteLine("  get { return defaultInstance; }");
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("protected override {0} ThisMessage {{", ClassName);
            writer.WriteLine("  get { return this; }");
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("public static pbd::MessageDescriptor Descriptor {");
            writer.WriteLine("  get {{ return {0}.internal__{1}__Descriptor; }}", DescriptorUtil.GetFullUmbrellaClassName(Descriptor),
                             GetUniqueFileScopeIdentifier(Descriptor));
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("protected override pb::FieldAccess.FieldAccessorTable<{0}, {0}.Builder> InternalFieldAccessors {{", ClassName);
            writer.WriteLine("  get {{ return {0}.internal__{1}__FieldAccessorTable; }}", DescriptorUtil.GetFullUmbrellaClassName(Descriptor),
                             GetUniqueFileScopeIdentifier(Descriptor));
            writer.WriteLine("}");
            writer.WriteLine();

            // Extensions don't need to go in an extra nested type
            WriteChildren(writer, null, Descriptor.Extensions);

            if (Descriptor.EnumTypes.Count + Descriptor.NestedTypes.Count > 0)
            {
                writer.WriteLine("#region Nested types");
                writer.WriteLine("public static class Types {");
                writer.Indent();
                WriteChildren(writer, null, Descriptor.EnumTypes);
                WriteChildren(writer, null, Descriptor.NestedTypes);
                writer.Outdent();
                writer.WriteLine("}");
                writer.WriteLine("#endregion");
                writer.WriteLine();
            }

            foreach (FieldDescriptor fieldDescriptor in Descriptor.Fields)
            {
                // Rats: we lose the debug comment here :(
                SourceGenerators.CreateFieldGenerator(fieldDescriptor).GenerateMembers(writer);
                writer.WriteLine();
            }

            if (Descriptor.File.Options.OptimizeFor == FileOptions.Types.OptimizeMode.SPEED)
            {
                GenerateIsInitialized(writer);
                GenerateMessageSerializationMethods(writer);
            }

            GenerateParseFromMethods(writer);
            GenerateBuilder(writer);
        }
        private void GenerateCommonBuilderMethods(TextGenerator writer)
        {
            writer.WriteLine("{0} Builder() {{}}", ClassAccessLevel);
            writer.WriteLine();
            writer.WriteLine("{0} result = new {0}();", ClassName);
            writer.WriteLine();
            writer.WriteLine("protected override {0} MessageBeingBuilt {{", ClassName);
            writer.WriteLine("  get { return result; }");
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("public override Builder Clear() {");
            writer.WriteLine("  result = new {0}();", ClassName);
            writer.WriteLine("  return this;");
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("public override Builder Clone() {");
            writer.WriteLine("  return new Builder().MergeFrom(result);");
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("public override pbd::MessageDescriptor DescriptorForType {");
            writer.WriteLine("  get {{ return {0}.Descriptor; }}", ClassName);
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("public override {0} DefaultInstanceForType {{", ClassName);
            writer.WriteLine("  get {{ return {0}.DefaultInstance; }}", ClassName);
            writer.WriteLine("}");
            writer.WriteLine();

            writer.WriteLine("public override {0} BuildPartial() {{", ClassName);
            writer.Indent();
            foreach (FieldDescriptor field in Descriptor.Fields)
            {
                SourceGenerators.CreateFieldGenerator(field).GenerateBuildingCode(writer);
            }
            writer.WriteLine("{0} returnMe = result;", ClassName);
            writer.WriteLine("result = null;");
            writer.WriteLine("return returnMe;");
            writer.Outdent();
            writer.WriteLine("}");
            writer.WriteLine();

            if (Descriptor.File.Options.OptimizeFor == FileOptions.Types.OptimizeMode.SPEED)
            {
                writer.WriteLine("public override Builder MergeFrom(pb::IMessage other) {");
                writer.WriteLine("  if (other is {0}) {{", ClassName);
                writer.WriteLine("    return MergeFrom(({0}) other);", ClassName);
                writer.WriteLine("  } else {");
                writer.WriteLine("    base.MergeFrom(other);");
                writer.WriteLine("    return this;");
                writer.WriteLine("  }");
                writer.WriteLine("}");
                writer.WriteLine();
                writer.WriteLine("public override Builder MergeFrom({0} other) {{", ClassName);
                // Optimization:  If other is the default instance, we know none of its
                // fields are set so we can skip the merge.
                writer.Indent();
                writer.WriteLine("if (other == {0}.DefaultInstance) return this;", ClassName);
                foreach (FieldDescriptor field in Descriptor.Fields)
                {
                    SourceGenerators.CreateFieldGenerator(field).GenerateMergingCode(writer);
                }
                // if message type has extensions
                if (Descriptor.Proto.ExtensionRangeCount > 0)
                {
                    writer.WriteLine("  this.MergeExtensionFields(other);");
                }
                writer.WriteLine("this.MergeUnknownFields(other.UnknownFields);");
                writer.WriteLine("return this;");
                writer.Outdent();
                writer.WriteLine("}");
                writer.WriteLine();
            }
        }