Jon Skeet | 0aac0e4 | 2009-09-09 18:48:02 +0100 | [diff] [blame] | 1 | #region Copyright notice and license |
Jon Skeet | ad74853 | 2009-06-25 16:55:58 +0100 | [diff] [blame] | 2 | // Protocol Buffers - Google's data interchange format |
| 3 | // Copyright 2008 Google Inc. All rights reserved. |
| 4 | // http://github.com/jskeet/dotnet-protobufs/ |
| 5 | // Original C++/Java/Python code: |
| 6 | // http://code.google.com/p/protobuf/ |
| 7 | // |
| 8 | // Redistribution and use in source and binary forms, with or without |
| 9 | // modification, are permitted provided that the following conditions are |
| 10 | // met: |
| 11 | // |
| 12 | // * Redistributions of source code must retain the above copyright |
| 13 | // notice, this list of conditions and the following disclaimer. |
| 14 | // * Redistributions in binary form must reproduce the above |
| 15 | // copyright notice, this list of conditions and the following disclaimer |
| 16 | // in the documentation and/or other materials provided with the |
| 17 | // distribution. |
| 18 | // * Neither the name of Google Inc. nor the names of its |
| 19 | // contributors may be used to endorse or promote products derived from |
| 20 | // this software without specific prior written permission. |
| 21 | // |
| 22 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Jon Skeet | 0aac0e4 | 2009-09-09 18:48:02 +0100 | [diff] [blame] | 33 | #endregion |
| 34 | |
Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame] | 35 | using System.Collections.Generic; |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 36 | using Google.ProtocolBuffers.Descriptors; |
| 37 | |
| 38 | namespace Google.ProtocolBuffers.ProtoGen { |
| 39 | internal abstract class SourceGeneratorBase<T> where T : IDescriptor { |
| 40 | |
| 41 | private readonly T descriptor; |
| 42 | |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame^] | 43 | protected readonly bool OptimizeSpeed; |
| 44 | protected readonly bool OptimizeSize; |
| 45 | protected readonly bool UseLiteRuntime; |
| 46 | |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 47 | protected SourceGeneratorBase(T descriptor) { |
| 48 | this.descriptor = descriptor; |
csharptest | 804b6d8 | 2010-11-07 10:49:33 -0600 | [diff] [blame^] | 49 | |
| 50 | OptimizeSize = descriptor.File.Options.OptimizeFor == Google.ProtocolBuffers.DescriptorProtos.FileOptions.Types.OptimizeMode.CODE_SIZE; |
| 51 | OptimizeSpeed = descriptor.File.Options.OptimizeFor == Google.ProtocolBuffers.DescriptorProtos.FileOptions.Types.OptimizeMode.SPEED; |
| 52 | UseLiteRuntime = descriptor.File.Options.OptimizeFor == Google.ProtocolBuffers.DescriptorProtos.FileOptions.Types.OptimizeMode.LITE_RUNTIME; |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | protected T Descriptor { |
| 56 | get { return descriptor; } |
| 57 | } |
| 58 | |
Jon Skeet | d6343be | 2008-11-12 23:39:44 +0000 | [diff] [blame] | 59 | internal static string GetClassName(IDescriptor descriptor) { |
| 60 | return ToCSharpName(descriptor.FullName, descriptor.File); |
| 61 | } |
| 62 | |
| 63 | // Groups are hacky: The name of the field is just the lower-cased name |
| 64 | // of the group type. In C#, though, we would like to retain the original |
| 65 | // capitalization of the type name. |
| 66 | internal static string GetFieldName(FieldDescriptor descriptor) { |
| 67 | if (descriptor.FieldType == FieldType.Group) { |
| 68 | return descriptor.MessageType.Name; |
| 69 | } else { |
| 70 | return descriptor.Name; |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Jon Skeet | 7ee85c4 | 2009-05-28 21:11:15 +0100 | [diff] [blame] | 74 | internal static string GetFieldConstantName(FieldDescriptor field) { |
Jon Skeet | df67f14 | 2009-06-05 19:29:36 +0100 | [diff] [blame] | 75 | return field.CSharpOptions.PropertyName + "FieldNumber"; |
Jon Skeet | 7ee85c4 | 2009-05-28 21:11:15 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Jon Skeet | d6343be | 2008-11-12 23:39:44 +0000 | [diff] [blame] | 78 | private static string ToCSharpName(string name, FileDescriptor file) { |
| 79 | string result = file.CSharpOptions.Namespace; |
| 80 | if (file.CSharpOptions.NestClasses) { |
| 81 | if (result != "") { |
| 82 | result += "."; |
| 83 | } |
| 84 | result += file.CSharpOptions.UmbrellaClassname; |
| 85 | } |
| 86 | if (result != "") { |
| 87 | result += '.'; |
| 88 | } |
| 89 | string classname; |
| 90 | if (file.Package == "") { |
| 91 | classname = name; |
| 92 | } else { |
| 93 | // Strip the proto package from full_name since we've replaced it with |
| 94 | // the C# namespace. |
| 95 | classname = name.Substring(file.Package.Length + 1); |
| 96 | } |
| 97 | result += classname.Replace(".", ".Types."); |
| 98 | return "global::" + result; |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Jon Skeet | d6343be | 2008-11-12 23:39:44 +0000 | [diff] [blame] | 101 | protected string ClassAccessLevel { |
| 102 | get { |
| 103 | return descriptor.File.CSharpOptions.PublicClasses ? "public" : "internal"; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | protected void WriteChildren<TChild>(TextGenerator writer, string region, IEnumerable<TChild> children) |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 108 | where TChild : IDescriptor { |
| 109 | // Copy the set of children; makes access easier |
| 110 | List<TChild> copy = new List<TChild>(children); |
| 111 | if (copy.Count == 0) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | if (region != null) { |
| 116 | writer.WriteLine("#region {0}", region); |
| 117 | } |
| 118 | foreach (TChild child in children) { |
| 119 | SourceGenerators.CreateGenerator(child).Generate(writer); |
| 120 | } |
| 121 | if (region != null) { |
| 122 | writer.WriteLine("#endregion"); |
| 123 | writer.WriteLine(); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |