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