blob: 57244c5d645bc0043de984bce0b6dbe261bf4736 [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
temporal40ee5512008-07-10 02:12:20 +00003// http://code.google.com/p/protobuf/
4//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
temporal40ee5512008-07-10 02:12:20 +000030
31// Author: kenton@google.com (Kenton Varda)
32// Based on original Protocol Buffers design by
33// Sanjay Ghemawat, Jeff Dean, and others.
34
35#include <google/protobuf/compiler/cpp/cpp_primitive_field.h>
36#include <google/protobuf/compiler/cpp/cpp_helpers.h>
37#include <google/protobuf/io/printer.h>
38#include <google/protobuf/wire_format_inl.h>
39#include <google/protobuf/stubs/strutil.h>
40
41namespace google {
42namespace protobuf {
43namespace compiler {
44namespace cpp {
45
46using internal::WireFormat;
47
48namespace {
49
50// For encodings with fixed sizes, returns that size in bytes. Otherwise
51// returns -1.
52int FixedSize(FieldDescriptor::Type type) {
53 switch (type) {
54 case FieldDescriptor::TYPE_INT32 : return -1;
55 case FieldDescriptor::TYPE_INT64 : return -1;
56 case FieldDescriptor::TYPE_UINT32 : return -1;
57 case FieldDescriptor::TYPE_UINT64 : return -1;
58 case FieldDescriptor::TYPE_SINT32 : return -1;
59 case FieldDescriptor::TYPE_SINT64 : return -1;
60 case FieldDescriptor::TYPE_FIXED32 : return WireFormat::kFixed32Size;
61 case FieldDescriptor::TYPE_FIXED64 : return WireFormat::kFixed64Size;
62 case FieldDescriptor::TYPE_SFIXED32: return WireFormat::kSFixed32Size;
63 case FieldDescriptor::TYPE_SFIXED64: return WireFormat::kSFixed64Size;
64 case FieldDescriptor::TYPE_FLOAT : return WireFormat::kFloatSize;
65 case FieldDescriptor::TYPE_DOUBLE : return WireFormat::kDoubleSize;
66
67 case FieldDescriptor::TYPE_BOOL : return WireFormat::kBoolSize;
68 case FieldDescriptor::TYPE_ENUM : return -1;
69
70 case FieldDescriptor::TYPE_STRING : return -1;
71 case FieldDescriptor::TYPE_BYTES : return -1;
72 case FieldDescriptor::TYPE_GROUP : return -1;
73 case FieldDescriptor::TYPE_MESSAGE : return -1;
74
75 // No default because we want the compiler to complain if any new
76 // types are added.
77 }
78 GOOGLE_LOG(FATAL) << "Can't get here.";
79 return -1;
80}
81
82string DefaultValue(const FieldDescriptor* field) {
83 switch (field->cpp_type()) {
84 case FieldDescriptor::CPPTYPE_INT32:
85 return SimpleItoa(field->default_value_int32());
86 case FieldDescriptor::CPPTYPE_UINT32:
87 return SimpleItoa(field->default_value_uint32()) + "u";
88 case FieldDescriptor::CPPTYPE_INT64:
89 return "GOOGLE_LONGLONG(" + SimpleItoa(field->default_value_int64()) + ")";
90 case FieldDescriptor::CPPTYPE_UINT64:
91 return "GOOGLE_ULONGLONG(" + SimpleItoa(field->default_value_uint64())+ ")";
92 case FieldDescriptor::CPPTYPE_DOUBLE:
93 return SimpleDtoa(field->default_value_double());
94 case FieldDescriptor::CPPTYPE_FLOAT:
95 return SimpleFtoa(field->default_value_float());
96 case FieldDescriptor::CPPTYPE_BOOL:
97 return field->default_value_bool() ? "true" : "false";
98
99 case FieldDescriptor::CPPTYPE_ENUM:
100 case FieldDescriptor::CPPTYPE_STRING:
101 case FieldDescriptor::CPPTYPE_MESSAGE:
102 GOOGLE_LOG(FATAL) << "Shouldn't get here.";
103 return "";
104 }
105 // Can't actually get here; make compiler happy. (We could add a default
106 // case above but then we wouldn't get the nice compiler warning when a
107 // new type is added.)
108 return "";
109}
110
111// TODO(kenton): Factor out a "SetCommonFieldVariables()" to get rid of
112// repeat code between this and the other field types.
113void SetPrimitiveVariables(const FieldDescriptor* descriptor,
114 map<string, string>* variables) {
115 (*variables)["name"] = FieldName(descriptor);
116 (*variables)["type"] = PrimitiveTypeName(descriptor->cpp_type());
117 (*variables)["default"] = DefaultValue(descriptor);
118 (*variables)["index"] = SimpleItoa(descriptor->index());
119 (*variables)["number"] = SimpleItoa(descriptor->number());
120 (*variables)["classname"] = ClassName(FieldScope(descriptor), false);
121 (*variables)["declared_type"] = DeclaredTypeMethodName(descriptor->type());
122 (*variables)["tag_size"] = SimpleItoa(
123 WireFormat::TagSize(descriptor->number(), descriptor->type()));
124
125 int fixed_size = FixedSize(descriptor->type());
126 if (fixed_size != -1) {
127 (*variables)["fixed_size"] = SimpleItoa(fixed_size);
128 }
129}
130
131} // namespace
132
133// ===================================================================
134
135PrimitiveFieldGenerator::
136PrimitiveFieldGenerator(const FieldDescriptor* descriptor)
137 : descriptor_(descriptor) {
138 SetPrimitiveVariables(descriptor, &variables_);
139}
140
141PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {}
142
143void PrimitiveFieldGenerator::
144GeneratePrivateMembers(io::Printer* printer) const {
145 printer->Print(variables_, "$type$ $name$_;\n");
146}
147
148void PrimitiveFieldGenerator::
149GenerateAccessorDeclarations(io::Printer* printer) const {
150 printer->Print(variables_,
151 "inline $type$ $name$() const;\n"
152 "inline void set_$name$($type$ value);\n");
153}
154
155void PrimitiveFieldGenerator::
156GenerateInlineAccessorDefinitions(io::Printer* printer) const {
157 printer->Print(variables_,
158 "inline $type$ $classname$::$name$() const {\n"
159 " return $name$_;\n"
160 "}\n"
161 "inline void $classname$::set_$name$($type$ value) {\n"
162 " _set_bit($index$);\n"
163 " $name$_ = value;\n"
164 "}\n");
165}
166
167void PrimitiveFieldGenerator::
168GenerateClearingCode(io::Printer* printer) const {
169 printer->Print(variables_, "$name$_ = $default$;\n");
170}
171
172void PrimitiveFieldGenerator::
173GenerateMergingCode(io::Printer* printer) const {
174 printer->Print(variables_, "set_$name$(from.$name$());\n");
175}
176
177void PrimitiveFieldGenerator::
kenton@google.com26bd9ee2008-11-21 00:06:27 +0000178GenerateSwappingCode(io::Printer* printer) const {
179 printer->Print(variables_, "std::swap($name$_, other->$name$_);\n");
180}
181
182void PrimitiveFieldGenerator::
temporal40ee5512008-07-10 02:12:20 +0000183GenerateInitializer(io::Printer* printer) const {
184 printer->Print(variables_, ",\n$name$_($default$)");
185}
186
187void PrimitiveFieldGenerator::
188GenerateMergeFromCodedStream(io::Printer* printer) const {
189 printer->Print(variables_,
190 "DO_(::google::protobuf::internal::WireFormat::Read$declared_type$(\n"
191 " input, &$name$_));\n"
192 "_set_bit($index$);\n");
193}
194
195void PrimitiveFieldGenerator::
196GenerateSerializeWithCachedSizes(io::Printer* printer) const {
197 printer->Print(variables_,
198 "DO_(::google::protobuf::internal::WireFormat::Write$declared_type$("
199 "$number$, this->$name$(), output));\n");
200}
201
202void PrimitiveFieldGenerator::
203GenerateByteSize(io::Printer* printer) const {
204 int fixed_size = FixedSize(descriptor_->type());
205 if (fixed_size == -1) {
206 printer->Print(variables_,
207 "total_size += $tag_size$ +\n"
208 " ::google::protobuf::internal::WireFormat::$declared_type$Size(\n"
209 " this->$name$());\n");
210 } else {
211 printer->Print(variables_,
212 "total_size += $tag_size$ + $fixed_size$;\n");
213 }
214}
215
216// ===================================================================
217
218RepeatedPrimitiveFieldGenerator::
219RepeatedPrimitiveFieldGenerator(const FieldDescriptor* descriptor)
220 : descriptor_(descriptor) {
221 SetPrimitiveVariables(descriptor, &variables_);
222}
223
224RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {}
225
226void RepeatedPrimitiveFieldGenerator::
227GeneratePrivateMembers(io::Printer* printer) const {
228 printer->Print(variables_,
229 "::google::protobuf::RepeatedField< $type$ > $name$_;\n");
kenton@google.com2d6daa72009-01-22 01:27:00 +0000230 if (descriptor_->options().packed() &&
231 descriptor_->file()->options().optimize_for() == FileOptions::SPEED) {
232 printer->Print(variables_,
233 "mutable int _$name$_cached_byte_size_;\n");
234 }
temporal40ee5512008-07-10 02:12:20 +0000235}
236
237void RepeatedPrimitiveFieldGenerator::
238GenerateAccessorDeclarations(io::Printer* printer) const {
239 printer->Print(variables_,
240 "inline const ::google::protobuf::RepeatedField< $type$ >& $name$() const;\n"
241 "inline ::google::protobuf::RepeatedField< $type$ >* mutable_$name$();\n"
242 "inline $type$ $name$(int index) const;\n"
243 "inline void set_$name$(int index, $type$ value);\n"
244 "inline void add_$name$($type$ value);\n");
245}
246
247void RepeatedPrimitiveFieldGenerator::
248GenerateInlineAccessorDefinitions(io::Printer* printer) const {
249 printer->Print(variables_,
250 "inline const ::google::protobuf::RepeatedField< $type$ >&\n"
251 "$classname$::$name$() const {\n"
252 " return $name$_;\n"
253 "}\n"
254 "inline ::google::protobuf::RepeatedField< $type$ >*\n"
255 "$classname$::mutable_$name$() {\n"
256 " return &$name$_;\n"
257 "}\n"
258 "inline $type$ $classname$::$name$(int index) const {\n"
259 " return $name$_.Get(index);\n"
260 "}\n"
261 "inline void $classname$::set_$name$(int index, $type$ value) {\n"
262 " $name$_.Set(index, value);\n"
263 "}\n"
264 "inline void $classname$::add_$name$($type$ value) {\n"
265 " $name$_.Add(value);\n"
266 "}\n");
267}
268
269void RepeatedPrimitiveFieldGenerator::
270GenerateClearingCode(io::Printer* printer) const {
271 printer->Print(variables_, "$name$_.Clear();\n");
272}
273
274void RepeatedPrimitiveFieldGenerator::
275GenerateMergingCode(io::Printer* printer) const {
276 printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n");
277}
278
279void RepeatedPrimitiveFieldGenerator::
kenton@google.com26bd9ee2008-11-21 00:06:27 +0000280GenerateSwappingCode(io::Printer* printer) const {
281 printer->Print(variables_, "$name$_.Swap(&other->$name$_);\n");
282}
283
284void RepeatedPrimitiveFieldGenerator::
temporal40ee5512008-07-10 02:12:20 +0000285GenerateInitializer(io::Printer* printer) const {
kenton@google.comeb26a1e2009-04-16 22:43:40 +0000286 printer->Print(variables_, ",\n$name$_()");
287 if (descriptor_->options().packed() &&
288 descriptor_->file()->options().optimize_for() == FileOptions::SPEED) {
289 printer->Print(variables_, ",\n_$name$_cached_byte_size_()");
290 }
temporal40ee5512008-07-10 02:12:20 +0000291}
292
293void RepeatedPrimitiveFieldGenerator::
294GenerateMergeFromCodedStream(io::Printer* printer) const {
kenton@google.com2d6daa72009-01-22 01:27:00 +0000295 if (descriptor_->options().packed()) {
296 printer->Print("{\n");
297 printer->Indent();
298 printer->Print(variables_,
299 "::google::protobuf::uint32 length;\n"
300 "DO_(input->ReadVarint32(&length));\n"
301 "::google::protobuf::io::CodedInputStream::Limit limit = "
302 "input->PushLimit(length);\n"
303 "while (input->BytesUntilLimit() > 0) {\n"
304 " $type$ value;\n"
305 " DO_(::google::protobuf::internal::WireFormat::Read$declared_type$("
306 "input, &value));\n"
307 " add_$name$(value);\n"
308 "}\n"
309 "input->PopLimit(limit);\n");
310 printer->Outdent();
311 printer->Print("}\n");
312 } else {
313 printer->Print(variables_,
314 "$type$ value;\n"
315 "DO_(::google::protobuf::internal::WireFormat::Read$declared_type$("
316 "input, &value));\n"
317 "add_$name$(value);\n");
318 }
temporal40ee5512008-07-10 02:12:20 +0000319}
320
321void RepeatedPrimitiveFieldGenerator::
322GenerateSerializeWithCachedSizes(io::Printer* printer) const {
kenton@google.com2d6daa72009-01-22 01:27:00 +0000323 if (descriptor_->options().packed()) {
324 // Write the tag and the size.
325 printer->Print(variables_,
326 "if (this->$name$_size() > 0) {\n"
327 " DO_(::google::protobuf::internal::WireFormat::WriteTag("
328 "$number$, ::google::protobuf::internal::WireFormat::WIRETYPE_LENGTH_DELIMITED,"
329 "output));\n"
330 " DO_(output->WriteVarint32(_$name$_cached_byte_size_));\n"
331 "}\n");
332 }
temporal40ee5512008-07-10 02:12:20 +0000333 printer->Print(variables_,
kenton@google.com2d6daa72009-01-22 01:27:00 +0000334 "for (int i = 0; i < this->$name$_size(); i++) {\n");
335 if (descriptor_->options().packed()) {
336 printer->Print(variables_,
337 " DO_(::google::protobuf::internal::WireFormat::Write$declared_type$NoTag("
338 "this->$name$(i), output));\n");
339 } else {
340 printer->Print(variables_,
341 " DO_(::google::protobuf::internal::WireFormat::Write$declared_type$("
342 "$number$, this->$name$(i), output));\n");
343 }
344 printer->Print("}\n");
temporal40ee5512008-07-10 02:12:20 +0000345}
346
347void RepeatedPrimitiveFieldGenerator::
348GenerateByteSize(io::Printer* printer) const {
kenton@google.com2d6daa72009-01-22 01:27:00 +0000349 printer->Print(variables_,
350 "{\n"
351 " int data_size = 0;\n");
352 printer->Indent();
temporal40ee5512008-07-10 02:12:20 +0000353 int fixed_size = FixedSize(descriptor_->type());
354 if (fixed_size == -1) {
355 printer->Print(variables_,
kenton@google.com2d6daa72009-01-22 01:27:00 +0000356 "for (int i = 0; i < this->$name$_size(); i++) {\n"
357 " data_size += ::google::protobuf::internal::WireFormat::$declared_type$Size(\n"
temporal40ee5512008-07-10 02:12:20 +0000358 " this->$name$(i));\n"
359 "}\n");
360 } else {
361 printer->Print(variables_,
kenton@google.com2d6daa72009-01-22 01:27:00 +0000362 "data_size = $fixed_size$ * this->$name$_size();\n");
temporal40ee5512008-07-10 02:12:20 +0000363 }
kenton@google.com2d6daa72009-01-22 01:27:00 +0000364
365 if (descriptor_->options().packed()) {
366 printer->Print(variables_,
367 "if (data_size > 0) {\n"
368 " total_size += $tag_size$ + "
369 "::google::protobuf::internal::WireFormat::Int32Size(data_size);\n"
370 "}\n"
371 "_$name$_cached_byte_size_ = data_size;\n"
372 "total_size += data_size;\n");
373 } else {
374 printer->Print(variables_,
375 "total_size += $tag_size$ * this->$name$_size() + data_size;\n");
376 }
377 printer->Outdent();
378 printer->Print("}\n");
temporal40ee5512008-07-10 02:12:20 +0000379}
380
381} // namespace cpp
382} // namespace compiler
383} // namespace protobuf
384} // namespace google