blob: 357614d1dc1c4df76fa2bc286922345985409ed5 [file] [log] [blame]
Jon Skeet60c059b2008-10-23 21:17:56 +01001using System;
Jon Skeet68036862008-10-22 13:30:34 +01002using System.Globalization;
Jon Skeet642a8142009-01-27 12:25:21 +00003using Google.ProtocolBuffers.Descriptors;
Jon Skeet68036862008-10-22 13:30:34 +01004
5namespace Google.ProtocolBuffers.ProtoGen {
6 internal abstract class FieldGeneratorBase : SourceGeneratorBase<FieldDescriptor> {
7 protected FieldGeneratorBase(FieldDescriptor descriptor)
8 : base(descriptor) {
9 }
10
11 private static bool AllPrintableAscii(string text) {
12 foreach (char c in text) {
13 if (c < 0x20 || c > 0x7e) {
14 return false;
15 }
16 }
17 return true;
18 }
19
20 protected string DefaultValue {
21 get {
22 string suffix = "";
23 switch (Descriptor.FieldType) {
24 case FieldType.Float: suffix = "F"; break;
25 case FieldType.Double: suffix = "D"; break;
26 case FieldType.Int64: suffix = "L"; break;
27 case FieldType.UInt64: suffix = "UL"; break;
28 }
29 switch (Descriptor.FieldType) {
30 case FieldType.Float:
31 case FieldType.Double:
32 case FieldType.Int32:
33 case FieldType.Int64:
34 case FieldType.SInt32:
35 case FieldType.SInt64:
36 case FieldType.SFixed32:
37 case FieldType.SFixed64:
38 case FieldType.UInt32:
39 case FieldType.UInt64:
40 case FieldType.Fixed32:
41 case FieldType.Fixed64:
42 // The simple Object.ToString converts using the current culture.
43 // We want to always use the invariant culture so it's predictable.
44 IConvertible value = (IConvertible) Descriptor.DefaultValue;
45 return value.ToString(CultureInfo.InvariantCulture) + suffix;
46 case FieldType.Bool:
47 return (bool) Descriptor.DefaultValue ? "true" : "false";
48
49 case FieldType.Bytes:
50 if (!Descriptor.HasDefaultValue) {
51 return "pb::ByteString.Empty";
52 }
Jon Skeetd6343be2008-11-12 23:39:44 +000053 return string.Format("(pb::ByteString) {0}.Descriptor.Fields[{1}].DefaultValue", GetClassName(Descriptor.ContainingType), Descriptor.Index);
Jon Skeet68036862008-10-22 13:30:34 +010054 case FieldType.String:
55 if (AllPrintableAscii(Descriptor.Proto.DefaultValue)) {
56 // All chars are ASCII and printable. In this case we only
57 // need to escape quotes and backslashes.
58 return "\"" + Descriptor.Proto.DefaultValue
59 .Replace("\\", "\\\\")
60 .Replace("'", "\\'")
61 .Replace("\"", "\\\"")
62 + "\"";
63 }
Jon Skeetd6343be2008-11-12 23:39:44 +000064 return string.Format("(string) {0}.Descriptor.Fields[{1}].DefaultValue", GetClassName(Descriptor.ContainingType), Descriptor.Index);
Jon Skeet68036862008-10-22 13:30:34 +010065 case FieldType.Enum:
66 return TypeName + "." + ((EnumValueDescriptor) Descriptor.DefaultValue).Name;
67 case FieldType.Message:
68 case FieldType.Group:
69 return TypeName + ".DefaultInstance";
70 default:
71 throw new InvalidOperationException("Invalid field descriptor type");
72 }
73 }
74 }
75
Jon Skeet68036862008-10-22 13:30:34 +010076 protected string PropertyName {
77 get {
Jon Skeet1d131c92008-11-13 22:29:48 +000078 return Descriptor.CSharpOptions.PropertyName;
Jon Skeet68036862008-10-22 13:30:34 +010079 }
80 }
81
Jon Skeet68036862008-10-22 13:30:34 +010082 protected string Name {
Jon Skeetd6343be2008-11-12 23:39:44 +000083 get { return NameHelpers.UnderscoresToCamelCase(GetFieldName(Descriptor)); }
Jon Skeet68036862008-10-22 13:30:34 +010084 }
85
86 protected int Number {
87 get { return Descriptor.FieldNumber; }
88 }
89
Jon Skeet642a8142009-01-27 12:25:21 +000090 protected void AddNullCheck(TextGenerator writer) {
91 AddNullCheck(writer, "value");
92 }
93
94 protected void AddNullCheck(TextGenerator writer, string name) {
95 if (IsNullableType) {
96 writer.WriteLine(" pb::ThrowHelper.ThrowIfNull({0}, \"{0}\");", name);
97 }
98 }
99
Jon Skeet25a28582009-02-18 16:06:22 +0000100 /// <summary>
101 /// For encodings with fixed sizes, returns that size in bytes. Otherwise
102 /// returns -1. TODO(jonskeet): Make this less ugly.
103 /// </summary>
104 protected int FixedSize {
105 get {
106 switch (Descriptor.FieldType) {
107 case FieldType.UInt32:
108 case FieldType.UInt64:
109 case FieldType.Int32:
110 case FieldType.Int64:
111 case FieldType.SInt32:
112 case FieldType.SInt64:
113 case FieldType.Enum:
114 case FieldType.Bytes:
115 case FieldType.String:
116 case FieldType.Message:
117 case FieldType.Group:
118 return -1;
119 case FieldType.Float:
120 return WireFormat.FloatSize;
121 case FieldType.SFixed32:
122 return WireFormat.SFixed32Size;
123 case FieldType.Fixed32:
124 return WireFormat.Fixed32Size;
125 case FieldType.Double:
126 return WireFormat.DoubleSize;
127 case FieldType.SFixed64:
128 return WireFormat.SFixed64Size;
129 case FieldType.Fixed64:
130 return WireFormat.Fixed64Size;
131 case FieldType.Bool:
132 return WireFormat.BoolSize;
133 default:
134 throw new InvalidOperationException("Invalid field descriptor type");
135 }
136 }
137 }
138
Jon Skeet642a8142009-01-27 12:25:21 +0000139 protected bool IsNullableType {
140 get {
141 switch (Descriptor.FieldType) {
142 case FieldType.Float:
143 case FieldType.Double:
144 case FieldType.Int32:
145 case FieldType.Int64:
146 case FieldType.SInt32:
147 case FieldType.SInt64:
148 case FieldType.SFixed32:
149 case FieldType.SFixed64:
150 case FieldType.UInt32:
151 case FieldType.UInt64:
152 case FieldType.Fixed32:
153 case FieldType.Fixed64:
154 case FieldType.Bool:
155 case FieldType.Enum:
156 return false;
157 case FieldType.Bytes:
158 case FieldType.String:
159 case FieldType.Message:
160 case FieldType.Group:
161 return true;
162 default:
163 throw new InvalidOperationException("Invalid field descriptor type");
164 }
165 }
166 }
167
Jon Skeet68036862008-10-22 13:30:34 +0100168 protected string TypeName {
169 get {
170 switch (Descriptor.FieldType) {
171 case FieldType.Enum:
Jon Skeetd6343be2008-11-12 23:39:44 +0000172 return GetClassName(Descriptor.EnumType);
Jon Skeet68036862008-10-22 13:30:34 +0100173 case FieldType.Message:
174 case FieldType.Group:
Jon Skeetd6343be2008-11-12 23:39:44 +0000175 return GetClassName(Descriptor.MessageType);
Jon Skeet68036862008-10-22 13:30:34 +0100176 default:
177 return DescriptorUtil.GetMappedTypeName(Descriptor.MappedType);
178 }
179 }
180 }
181
182 protected string MessageOrGroup {
183 get { return Descriptor.FieldType == FieldType.Group ? "Group" : "Message"; }
184 }
185
186 /// <summary>
187 /// Returns the type name as used in CodedInputStream method names: SFixed32, UInt32 etc.
188 /// </summary>
189 protected string CapitalizedTypeName {
190 get {
191 // Our enum names match perfectly. How serendipitous.
192 return Descriptor.FieldType.ToString();
193 }
194 }
195 }
196}