blob: 5fa420fc21f3e6d77e55d1639819f532a9edaddb [file] [log] [blame]
Jon Skeet0aac0e42009-09-09 18:48:02 +01001#region Copyright notice and license
Jon Skeetad748532009-06-25 16:55:58 +01002// 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 Skeet0aac0e42009-09-09 18:48:02 +010033#endregion
34
Jon Skeet60c059b2008-10-23 21:17:56 +010035using System.Collections.Generic;
Jon Skeet68036862008-10-22 13:30:34 +010036using Google.ProtocolBuffers.Descriptors;
37
38namespace Google.ProtocolBuffers.ProtoGen {
39 internal abstract class SourceGeneratorBase<T> where T : IDescriptor {
40
41 private readonly T descriptor;
42
csharptest804b6d82010-11-07 10:49:33 -060043 protected readonly bool OptimizeSpeed;
44 protected readonly bool OptimizeSize;
45 protected readonly bool UseLiteRuntime;
46
Jon Skeet68036862008-10-22 13:30:34 +010047 protected SourceGeneratorBase(T descriptor) {
48 this.descriptor = descriptor;
csharptest804b6d82010-11-07 10:49:33 -060049
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 Skeet68036862008-10-22 13:30:34 +010053 }
54
55 protected T Descriptor {
56 get { return descriptor; }
57 }
58
Jon Skeetd6343be2008-11-12 23:39:44 +000059 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 Skeet68036862008-10-22 13:30:34 +010071 }
72 }
73
Jon Skeet7ee85c42009-05-28 21:11:15 +010074 internal static string GetFieldConstantName(FieldDescriptor field) {
Jon Skeetdf67f142009-06-05 19:29:36 +010075 return field.CSharpOptions.PropertyName + "FieldNumber";
Jon Skeet7ee85c42009-05-28 21:11:15 +010076 }
77
Jon Skeetd6343be2008-11-12 23:39:44 +000078 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 Skeet68036862008-10-22 13:30:34 +010099 }
100
Jon Skeetd6343be2008-11-12 23:39:44 +0000101 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 Skeet68036862008-10-22 13:30:34 +0100108 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}